wisest owl (wisest_owl) wrote in changelog,
wisest owl
wisest_owl
changelog

[ljcom] r12476: LJSUP-13266: Add limit for sending free ...

Committer: wisest-owl
LJSUP-13266: Add limit for sending free user heads

U   trunk/bin/upgrading/proplists-local.dat
U   trunk/bin/upgrading/update-db-local.pl
U   trunk/cgi-bin/LJ/Pay/Payment/PayItem/Addon.pm
U   trunk/cgi-bin/LJ/Pay/Payment/PayItem/UserHead.pm
U   trunk/cgi-bin/LJ/UserHead.pm
U   trunk/cgi-bin/LJ/Widget/Shop/View/UserHeads.pm
U   trunk/htdocs/admin/userheads/manage.bml
U   trunk/htdocs/pay/modify.bml.text.local
Modified: trunk/bin/upgrading/proplists-local.dat
===================================================================
--- trunk/bin/upgrading/proplists-local.dat	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/bin/upgrading/proplists-local.dat	2012-08-16 08:25:19 UTC (rev 12476)
@@ -961,5 +961,12 @@
   des: IDs of paid repost offers
   indexed: 0
 
+userproplist.userheads_restrict:
+  cldversion: 4
+  datatype: num
+  des: The number of userheads with restrictions that user has already bought. Format: "uhid:count,...,..."
+  indexed: 0
+  multihomed: 0
+  prettyname: The number of restricted userheads
 
 

Modified: trunk/bin/upgrading/update-db-local.pl
===================================================================
--- trunk/bin/upgrading/update-db-local.pl	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/bin/upgrading/update-db-local.pl	2012-08-16 08:25:19 UTC (rev 12476)
@@ -1169,6 +1169,7 @@
   `is_enabled` tinyint(4) DEFAULT '0',
   `img_src` varchar(200) NOT NULL,
   `individ_buyer` varchar(15) default '',
+  `buy_limit` int(10) unsigned NOT NULL default '0',
   PRIMARY KEY (`uh_id`)
 )
 EOC
@@ -2434,6 +2435,11 @@
                 "ALTER TABLE shop_userheads ADD individ_buyer VARCHAR(15) NOT NULL DEFAULT ''");
     }
 
+    unless (column_type("shop_userheads", "buy_limit")) {
+        do_alter("shop_userheads",
+                "ALTER TABLE shop_userheads ADD buy_limit SMALLINT UNSIGNED NOT NULL DEFAULT 0");
+    }
+
     unless (column_type("smsru_phones", "phone") =~ /char/i) {
         do_alter("smsru_phones",
             "ALTER TABLE smsru_phones MODIFY phone VARCHAR(12)");

Modified: trunk/cgi-bin/LJ/Pay/Payment/PayItem/Addon.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/Payment/PayItem/Addon.pm	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/cgi-bin/LJ/Pay/Payment/PayItem/Addon.pm	2012-08-16 08:25:19 UTC (rev 12476)
@@ -558,6 +558,25 @@
     $months     = $qty if $qty < 100;
     $exact_time = $qty if $qty >= $now;
 
+    if ($self->isa('LJ::Pay::Payment::PayItem::UserHead')) {
+        ##
+        ## We store the number of purchased special userheads in user property 'userheads_restrict'
+        ## format is "uh_id:number_of_userheads,uh_id2:number_of_userheads2"
+        ##
+        my $subitem = $self->{subitem};
+        my ($uh_id) = $subitem =~ m#uh-(\d+)#;
+        my $uh = LJ::UserHead->get_userhead ($uh_id);
+        my $buyer_u = LJ::load_user ($self->{'userid'});
+        if ($uh->get_buy_limit > 0) {
+            if ($buyer_u) {
+                my $uhs_restrict = $buyer_u->prop('userheads_restrict');
+                my %uhs_restrict = map {split ':', $_} split ',', $uhs_restrict;
+                $uhs_restrict{ $uh_id }++;
+                $buyer_u->set_prop('userheads_restrict', join(",", map {"$_:$uhs_restrict{$_}"} keys %uhs_restrict));
+            } 
+        }
+    }
+
     my $dbh = _get_dbh();
     # insert a new row, or add time to old one
     # - this code somewhat duplicates functionality from activate_service()

Modified: trunk/cgi-bin/LJ/Pay/Payment/PayItem/UserHead.pm
===================================================================
--- trunk/cgi-bin/LJ/Pay/Payment/PayItem/UserHead.pm	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/cgi-bin/LJ/Pay/Payment/PayItem/UserHead.pm	2012-08-16 08:25:19 UTC (rev 12476)
@@ -70,7 +70,71 @@
     return \%result;
 }
 
+sub is_disabled {
+    my $self = shift;
+
+    my $subitem = $self->{subitem};
+    my ($uh_id) = $subitem =~ m#uh-(\d+)#;
+    my $uh = LJ::UserHead->get_userhead ($uh_id);
+
+    return 1 if $uh->get_visibility eq 'D';
+
+    return 0;
+}
+
+sub can_purchase {
+    my $self = shift;
+    my $cart = shift;
+
+    return 0 if $self->is_disabled;
+
+    my $subitem = $self->{subitem};
+    my ($uh_id) = $subitem =~ m#uh-(\d+)#;
+    my $uh = LJ::UserHead->get_userhead ($uh_id);
+
+    # global var is used for current cart
+    $LJ::UH_CURRENT_PAYED->{$uh_id} += 0;
+
+    # user must be logged in for purchase limited vgifts
+    my $remote = LJ::get_remote ();
+    # do calc count already buyed
+    if ($remote && $uh->{'buy_limit'} > 0) {
+        my $uhs_restrict = $remote->prop('userheads_restrict');
+        my %uhs_restrict = map {split ':', $_} split ',', $uhs_restrict;
+        $LJ::UH_CURRENT_PAYED->{$uh_id} += $uhs_restrict{ $uh_id } || 0;
+    } else {
+        return !$uh->{'buy_limit'};
+    }
+
+    # count in cart
+    if ($uh->{'buy_limit'} > 0 && !$LJ::UH_CURRENT_PAYED->{'already_checked'}{$uh_id}) {
+        if ($cart) {
+            foreach my $item ($cart->get_items()) {
+                $LJ::UH_CURRENT_PAYED->{$uh_id}++
+                    if $item->{subitem} eq "uh-".$uh_id && $item->{'rcptid'} ne $cart->get_userid;
+            }
+        }
+    }
+    $LJ::UH_CURRENT_PAYED->{'already_checked'}{$uh_id}++;
+
+    # limit exhausted
+    return 0 if $uh->get_buy_limit > 0 && $LJ::UH_CURRENT_PAYED->{$uh_id} > $uh->get_buy_limit;
+    
+    return 1;
+}
+
 sub can_belong_to {
+    my ($self, $cartobj, $errs_ref, $warns_ref, $action) = @_;
+
+    my $subitem = $self->{subitem};
+    my ($uh_id) = $subitem =~ m#uh-(\d+)#;
+    my $uh = LJ::UserHead->get_userhead ($uh_id);
+    unless ($self->can_purchase ($cartobj)) {
+        push @$errs_ref, LJ::Lang::ml('/pay/modify.bml.error.cannot_gift_limited_uh', { number => $uh->get_buy_limit, userhead => $uh->get_uh_img })
+            unless $LJ::UH_CURRENT_PAYED->{'already_checked'}{$uh_id} && $LJ::UH_CURRENT_PAYED->{'already_checked'}{$uh_id} > 1;
+        return 0;
+    }
+
     return 1;
 }
 
@@ -84,7 +148,7 @@
     my $res = $dbh->selectall_arrayref("SELECT item, UNIX_TIMESTAMP(expdate) as expdate, size " .
                                                  "FROM paidexp WHERE userid=?",
                                                  { Slice => {} }, $userid);
-    
+
     $u = LJ::load_userid ($userid) unless $u;
     my $propval = $u->prop ('custom_usericon_individual');
     if ($propval) {
@@ -136,7 +200,7 @@
     if ($uh && ($uh->get_uh_img eq $u->custom_usericon)) {
         $u->set_custom_usericon (undef);
     }
- 
+
     # Drop this item from the list
     my $dbh = _get_dbh();
     $dbh->do("DELETE FROM paidexp where userid=? AND item=?", undef, $u->userid, $self->{item});

Modified: trunk/cgi-bin/LJ/UserHead.pm
===================================================================
--- trunk/cgi-bin/LJ/UserHead.pm	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/cgi-bin/LJ/UserHead.pm	2012-08-16 08:25:19 UTC (rev 12476)
@@ -4,10 +4,12 @@
 
 use base qw(Class::Accessor);
 __PACKAGE__->follow_best_practice;
-__PACKAGE__->mk_accessors( qw( uh_id uh_author uh_desc uh_desc_sup price visibility individ_buyer ) );
+__PACKAGE__->mk_accessors( qw( uh_id uh_author uh_desc uh_desc_sup price visibility individ_buyer buy_limit ) );
 
 use LJ::FileStore;
 
+$LJ::UH_CURRENT_PAYED = {};
+
 sub new {
     my $class = shift;
     my $args = shift || {};
@@ -23,6 +25,17 @@
     my $res = $dbh->do ("DELETE FROM shop_userheads WHERE uh_id = ?", undef, $self->get_uh_id);
 }
 
+######################################################
+# Calculate count userhead was buyed
+######################################################
+sub clean_userhead_counter {
+    $LJ::UH_CURRENT_PAYED = {};
+}
+
+require "ljhooks.pl";
+require "ljcom.pl";
+LJ::register_hook( start_request  => \&clean_userhead_counter );
+
 sub set_enabled {
     my $self = shift;
 
@@ -32,13 +45,14 @@
 sub get_userhead {
     my $class = shift;
     my $uh_id = shift;
+    my %opts  = @_;
 
     return unless $uh_id;
 
     my $dbh = LJ::get_db_writer();
 
     my $uh = $dbh->selectrow_hashref ("SELECT * FROM shop_userheads WHERE uh_id = $uh_id");
-    return $uh ? LJ::UserHead->new ($uh) : undef;
+    return $uh ? LJ::UserHead->new ($uh, \%opts) : undef;
 }
 
 sub save {

Modified: trunk/cgi-bin/LJ/Widget/Shop/View/UserHeads.pm
===================================================================
--- trunk/cgi-bin/LJ/Widget/Shop/View/UserHeads.pm	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/cgi-bin/LJ/Widget/Shop/View/UserHeads.pm	2012-08-16 08:25:19 UTC (rev 12476)
@@ -134,8 +134,6 @@
 
     $self->raise_errors(LJ::Lang::ml('shop.error.userhead.nothing_selected')) unless $uh;
 
-    $self->raise_errors(LJ::Lang::ml('shop.error.userhead.nothing_selected')) unless $uh;
-
     my $recipient = 
         (LJ::Request->get_param('gift') and LJ::Request->get_param('giftto'))
         ? LJ::load_user(LJ::Request->get_param('giftto'))
@@ -156,6 +154,10 @@
         rcptid  => LJ::Request->post_param('formyself') eq '1' && $remote ? $remote->userid : $recipient->userid,
     );
 
+    $it->can_purchase ($cart);
+    $self->raise_errors(LJ::Lang::ml('/pay/modify.bml.error.cannot_gift_limited_uh', { number => $uh->get_buy_limit, userhead => $uh->get_uh_img }))
+        if $uh->get_buy_limit > 0 && $LJ::UH_CURRENT_PAYED->{$uh_id} >= $uh->get_buy_limit;
+
     if ($giveafter_yyyy) {
         my $err_msg = $it->set_giveafter(
             $giveafter_yyyy, $giveafter_mm, $giveafter_dd,
@@ -171,3 +173,4 @@
 sub get_template_file { 'UserHeads' }
 
 1;
+

Modified: trunk/htdocs/admin/userheads/manage.bml
===================================================================
--- trunk/htdocs/admin/userheads/manage.bml	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/htdocs/admin/userheads/manage.bml	2012-08-16 08:25:19 UTC (rev 12476)
@@ -85,12 +85,14 @@
         my $desc_sup = $FORM{"$id-desc-sup"};
         my $price = $FORM{"$id-price"};
         my $visibility = $FORM{"$id-visibility"};
+        my $buy_limit = $FORM{"$id-buy-limit"};
         my $uh = LJ::UserHead->get_userhead ($FORM{"$id-uh_id"});
         $uh->set_uh_author ($author);
         $uh->set_price ($price);
         $uh->set_uh_desc ($desc);
         $uh->set_uh_desc_sup ($desc_sup);
         $uh->set_visibility ($visibility);
+        $uh->set_buy_limit ($buy_limit);
         my $buyer = $FORM{"$id-buyer"};
         $uh->set_individ_buyer ($buyer);
         $uh->save;
@@ -186,7 +188,7 @@
 
 $ret .= "<form method='POST'>";
 $ret .= "<table border='0' cellspacing='1' cellpadding='4'>";
-$ret .= "<tr><th>Id</th><th>Img Enb</th><th>Img Dis</th><th>Cost</th><th>&nbsp;</th><th>Author</th><th>Description</th><th>Description SUP</th><th>Individual user buyer</th><th>Indv. expdate (<small>YYYYMMDD</small>)</th><th>Edit</th></tr>";
+$ret .= "<tr><th>Id</th><th>Img Enb</th><th>Img Dis</th><th>Cost</th><th>&nbsp;</th><th>Buy Limit</th><th>Author</th><th>Description</th><th>Description SUP</th><th>Individual user buyer</th><th>Indv. expdate (<small>YYYYMMDD</small>)</th><th>Edit</th></tr>";
 foreach my $uh (@$uhs) {
     $ret .= "<tr>\n";
     $ret .= "<td><input type='hidden' name='".$uh->get_uh_id."-uh_id' value='".$uh->get_uh_id."' />".$uh->get_uh_id."</td>\n";
@@ -222,6 +224,7 @@
             }
         }
     }
+    $ret .= "<td><input type='text' size='5' name='".$uh->get_uh_id."-buy-limit' value='".$uh->get_buy_limit."' /></td>";
     $ret .= "<td><input type='text' name='".$uh->get_uh_id."-author' value='".$uh->get_uh_author."' /></td>\n";
     $ret .= "<td><input type='text' name='".$uh->get_uh_id."-desc' value='".$uh->get_uh_desc."' /></td>\n";
     $ret .= "<td><input type='text' name='".$uh->get_uh_id."-desc-sup' value='".$uh->get_uh_desc_sup."' /></td>\n";

Modified: trunk/htdocs/pay/modify.bml.text.local
===================================================================
--- trunk/htdocs/pay/modify.bml.text.local	2012-08-16 08:18:57 UTC (rev 12475)
+++ trunk/htdocs/pay/modify.bml.text.local	2012-08-16 08:25:19 UTC (rev 12476)
@@ -50,6 +50,8 @@
 .error.cannot.purchase|staleness=1
 .error.cannot.purchase=Can't be purchased
 
+.error.cannot_gift_limited_uh=Sorry, you can't give as a gift more than [[number]] userheads like this: <img src="[[userhead]]"/>
+
 .error.cannot_load_old_cart|staleness=1
 .error.cannot_load_old_cart=Unknown cart
 

Tags: bml, dat, ljcom, local, pl, pm, wisest-owl
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments