afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)
afuna ([personal profile] afuna) wrote in [site community profile] changelog2009-07-23 05:03 pm

[dw-free] allow account creation with "promotional" codes

[commit: http://hg.dwscoalition.org/dw-free/rev/8d97fd81d6c0]

http://bugs.dwscoalition.org/show_bug.cgi?id=1465

Backend work.

Patch by [personal profile] janinedog.

Files modified:
  • bin/upgrading/update-db-general.pl
  • cgi-bin/DW/InviteCodes.pm
  • cgi-bin/LJ/User.pm
  • cgi-bin/LJ/Widget/CreateAccount.pm
  • cgi-bin/LJ/Widget/CreateAccountProgressMeter.pm
--------------------------------------------------------------------------------
diff -r 20fcd6a3bb50 -r 8d97fd81d6c0 bin/upgrading/update-db-general.pl
--- a/bin/upgrading/update-db-general.pl	Thu Jul 23 09:06:54 2009 -0500
+++ b/bin/upgrading/update-db-general.pl	Thu Jul 23 17:02:45 2009 +0000
@@ -3271,6 +3271,17 @@ CREATE TABLE external_site_moods (
 )
 EOC
 
+register_tablecreate('acctcode_promo', <<'EOC');
+CREATE TABLE acctcode_promo (
+    code varchar(20) not null,
+    max_count int(10) unsigned not null default 0,
+    current_count int(10) unsigned not null default 0,
+    active enum('1','0') not null default 1,
+
+    PRIMARY KEY ( code )
+)
+EOC
+
 
 # NOTE: new table declarations go ABOVE here ;)
 
diff -r 20fcd6a3bb50 -r 8d97fd81d6c0 cgi-bin/DW/InviteCodes.pm
--- a/cgi-bin/DW/InviteCodes.pm	Thu Jul 23 09:06:54 2009 -0500
+++ b/cgi-bin/DW/InviteCodes.pm	Thu Jul 23 17:02:45 2009 +0000
@@ -129,6 +129,35 @@ sub could_be_code {
     return 1;
 }
 
+=head2 C<< $class->is_promo_code( code => $code ) >>
+
+Returns if the given code is a promo code or not.
+
+=cut
+
+sub is_promo_code {
+    my ( $class, %opts ) = @_;
+
+    my $promo_code_info = $class->get_promo_code_info( code => $opts{code} );
+
+    return ref $promo_code_info ? 1 : 0;
+}
+
+=head2 C<< $class->get_promo_code_info( code => $code ) >>
+
+Return the info for this promo code in a hashref.
+
+=cut
+
+sub get_promo_code_info {
+    my ( $class, %opts ) = @_;
+    my $dbh = LJ::get_db_writer();
+    my $code = $opts{code};
+
+    return undef unless $code =~ /^[a-z0-9]+$/i; # make sure the code is valid first
+    return $dbh->selectrow_hashref( "SELECT * FROM acctcode_promo WHERE code = ?", undef, $code );
+}
+
 =head2 C<< $class->check_code( code => $invite [, userid => $recipient] ) >>
 
 Checks whether code $invite is valid before trying to create an account. Takes
@@ -140,10 +169,19 @@ sub check_code {
 sub check_code {
     my ($class, %opts) = @_;
     my $dbh = LJ::get_db_writer();
+    my $code = $opts{code};
 
-    return 0 unless $class->could_be_code( string => $opts{code} );
+    # check if this code is a promo code first
+    # if it is, make sure it's active and we're not over the creation limit for the code
+    my $promo_code_info = $class->get_promo_code_info( code => $code );
+    if ( ref $promo_code_info ) {
+        return 0 unless $promo_code_info->{active} && ( $promo_code_info->{current_count} < $promo_code_info->{max_count} );
+        return 1;
+    }
 
-    my ($acid, $auth) = $class->decode( $opts{code} );
+    return 0 unless $class->could_be_code( string => $code );
+
+    my ($acid, $auth) = $class->decode( $code );
     my $ac = $dbh->selectrow_hashref( "SELECT userid, rcptid, auth " .
                                       "FROM acctcode WHERE acid=?",
                                       undef, $acid);
@@ -199,6 +237,24 @@ sub use_code {
         undef, $opts{user}->{userid}, $self->{acid} );
 
     return 1; # 1 means success? Needs error return in that case.
+}
+
+=head2 C<< $class->use_promo_code
+
+Increments the current_count on the given promo code.
+
+=cut
+
+sub use_promo_code {
+    my ( $class, %opts ) = @_;
+    my $dbh = LJ::get_db_writer();
+    my $code = $opts{code};
+
+    return 0 unless $class->is_promo_code( code => $code );
+
+    $dbh->do( "UPDATE acctcode_promo SET current_count = current_count + 1 WHERE code = ?", undef, $code );
+
+    return 1;
 }
 
 =head2 C<< $object->send_code ( [ email => $email ] ) >>
diff -r 20fcd6a3bb50 -r 8d97fd81d6c0 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Thu Jul 23 09:06:54 2009 -0500
+++ b/cgi-bin/LJ/User.pm	Thu Jul 23 17:02:45 2009 +0000
@@ -232,8 +232,11 @@ sub create_personal {
 
     # apply any paid time that this account should get
     if ( $LJ::USE_ACCT_CODES && $opts{code} ) {
+        my $code = $opts{code};
         my $itemidref;
-        if ( my $cart = DW::Shop::Cart->get_from_invite( $opts{code}, itemidref => \$itemidref ) ) {
+        if ( DW::InviteCodes->is_promo_code( code => $code ) ) {
+            LJ::statushistory_add( $u, undef, 'create_from_promo', "Created new account from promo code '$code'." );
+        } elsif ( my $cart = DW::Shop::Cart->get_from_invite( $code, itemidref => \$itemidref ) ) {
             my $item = $cart->get_item( $itemidref );
             if ( $item && $item->isa( 'DW::Shop::Item::Account' ) ) {
                 # first update the item's target user and the cart
diff -r 20fcd6a3bb50 -r 8d97fd81d6c0 cgi-bin/LJ/Widget/CreateAccount.pm
--- a/cgi-bin/LJ/Widget/CreateAccount.pm	Thu Jul 23 09:06:54 2009 -0500
+++ b/cgi-bin/LJ/Widget/CreateAccount.pm	Thu Jul 23 17:02:45 2009 +0000
@@ -307,7 +307,7 @@ sub render_body {
         $ret .= "</td></tr>\n";
     }
 
-    if ( $LJ::USE_ACCT_CODES ) {
+    if ( $LJ::USE_ACCT_CODES && !DW::InviteCodes->is_promo_code( code => $code ) ) {
         my $item = DW::InviteCodes->paid_status( code => $code );
         if ( $item ) {
             $ret .= "<tr valign='top'><td class='field-name'>&nbsp;</td>\n<td>";
@@ -544,8 +544,12 @@ sub handle_post {
 
         # we're all done; mark the invite code as used
         if ( $LJ::USE_ACCT_CODES && $code ) {
-            my $invitecode = DW::InviteCodes->new( code => $code );
-            $invitecode->use_code( user => $nu );
+            if ( DW::InviteCodes->is_promo_code( code => $code ) ) {
+                DW::InviteCodes->use_promo_code( code => $code );
+            } else {
+                my $invitecode = DW::InviteCodes->new( code => $code );
+                $invitecode->use_code( user => $nu );
+            }
         }
 
         my $stop_output;
diff -r 20fcd6a3bb50 -r 8d97fd81d6c0 cgi-bin/LJ/Widget/CreateAccountProgressMeter.pm
--- a/cgi-bin/LJ/Widget/CreateAccountProgressMeter.pm	Thu Jul 23 09:06:54 2009 -0500
+++ b/cgi-bin/LJ/Widget/CreateAccountProgressMeter.pm	Thu Jul 23 17:02:45 2009 +0000
@@ -14,7 +14,7 @@ sub render_body {
 
     my $given_step = $opts{step} || 1;
     my @steps_to_show = !LJ::is_enabled( 'payments' )
-                    || ( $LJ::USE_ACCT_CODES && $given_step == 1 && DW::InviteCodes->paid_status( code => $opts{code} ) )
+                    || ( $LJ::USE_ACCT_CODES && $given_step == 1 && !DW::InviteCodes->is_promo_code( code => $opts{code} ) && DW::InviteCodes->paid_status( code => $opts{code} ) )
                     || ( $given_step > 1 && $u && $u->is_paid )
                     ? ( 1, 2, 4 ) : ( 1..4 );
 
--------------------------------------------------------------------------------