Committer: ailyin
LJSUP-9763 (create an admin page to view recent selfpromo promotions)U trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm U trunk/cgi-bin/LJ/Pay/SelfPromo.pm U trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm
Modified: trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm =================================================================== --- trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm 2011-09-08 09:32:01 UTC (rev 10983) +++ trunk/cgi-bin/LJ/Pay/Payment/PayItem/SelfPromo.pm 2011-09-09 04:19:57 UTC (rev 10984) @@ -101,7 +101,7 @@ my $refund_to_userid = $self->get_prop('selfpromo_refund_userid'); my $refund_to = LJ::load_userid($refund_to_userid); - my $info = LJ::Pay::SelfPromo->current_entry_info($promoid); + my $promo = LJ::Pay::SelfPromo->current_entry_info($promoid); # credit them: LJ::Pay::Wallet->try_add( $refund_to, $refund ); @@ -115,8 +115,7 @@ 'time_end' => time, ); - my $entry_url = - $refund_to->journal_base . '/' . $info->{'ditemid'} . '.html'; + my $entry_url = $promo->entry_url; my $reason_map = { 'buyout' => 'buyout', @@ -129,7 +128,7 @@ 'reason' => $reason_map->{$type}, 'entry_url' => $entry_url, 'refund_amount' => $refund, - 'duration' => time - $info->{'started'}, + 'duration' => time - $promo->started, ); } } Modified: trunk/cgi-bin/LJ/Pay/SelfPromo.pm =================================================================== --- trunk/cgi-bin/LJ/Pay/SelfPromo.pm 2011-09-08 09:32:01 UTC (rev 10983) +++ trunk/cgi-bin/LJ/Pay/SelfPromo.pm 2011-09-09 04:19:57 UTC (rev 10984) @@ -29,12 +29,12 @@ sub buyout_cost { my ($class) = @_; - if ( my $info = $class->current_entry_info ) { + if ( my $promo = $class->current_entry_info ) { # there actually is a promoted entry right now, so let's # return however much its author paid for it plus the bidding step - return $info->{'cost'} + $LJ::SELF_PROMO_CONF->{'step'}; + return $promo->cost + $LJ::SELF_PROMO_CONF->{'step'}; } return $LJ::SELF_PROMO_CONF->{'min_cost'}; @@ -122,10 +122,10 @@ # buyout, and a part of it is a rounding error that # goes to a special account made for these purposes my ( $refund, $refund_promoid, $refund_userid ) = ( 0, 0, 0 ); - if ( my $info = $class->current_entry_info ) { - $refund = $class->calculate_refund($info); - $refund_promoid = $info->{'promoid'}; - $refund_userid = $info->{'posterid'}; + if ( my $promo = $class->current_entry_info ) { + $refund = $class->calculate_refund($promo); + $refund_promoid = $promo->promoid; + $refund_userid = $promo->posterid; } my $refund_remainder = @@ -180,10 +180,10 @@ sub admin_cancel_promo { my ( $class, $entry, $admin ) = @_; - my $info = $class->current_entry_info; - unless ( $info - && $info->{'journalid'} == $entry->journalid - && $info->{'jitemid'} == $entry->jitemid ) + my $promo = $class->current_entry_info; + unless ( $promo + && $promo->journalid == $entry->journalid + && $promo->jitemid == $entry->jitemid ) { # your princess is in another castle, sorry @@ -200,9 +200,9 @@ # user who purchased the selfpromo being cancelled, # and a part of it is a rounding error that goes to a # special account made for these purposes - my $refund = $class->calculate_refund($info); - my $refund_promoid = $info->{'promoid'}; - my $refund_userid = $info->{'posterid'}; + my $refund = $class->calculate_refund($promo); + my $refund_promoid = $promo->promoid; + my $refund_userid = $promo->posterid; my $refund_remainder = $refund % $LJ::SELF_PROMO_CONF->{'refund_step'}; @@ -253,14 +253,14 @@ sub withdraw_entry { my ( $class, $entry ) = @_; - my $info = $class->current_entry_info; + my $promo = $class->current_entry_info; die 'entry mismatch' - unless $info->{'journalid'} == $entry->journalid - && $info->{'jitemid'} == $entry->jitemid; + unless $promo->journalid == $entry->journalid + && $promo->jitemid == $entry->jitemid; $class->deactivate_promo( - $info->{'promoid'}, $entry->poster, + $promo->promoid, $entry->poster, 'reason' => 'withdraw', 'entry' => $entry, 'entry_url' => $entry->url, @@ -270,7 +270,7 @@ $entry->poster, 'deactivate', 'reason' => 'withdraw', 'entry_url' => $entry->url, - 'duration' => $info->{'exptime'} - $info->{'started'}, + 'duration' => $promo->exptime - $promo->started, ); } @@ -278,11 +278,9 @@ sub current_entry { my ($class) = @_; - my $info = $class->current_entry_info; - return unless $info; - - return LJ::Entry->new( $info->{'journalid'}, - 'jitemid' => $info->{'jitemid'}, ); + my $promo = $class->current_entry_info; + return unless $promo; + return $promo->entry; } sub check_current_entry { @@ -290,31 +288,30 @@ my $lock = $class->lock; - my $info = $class->current_entry_info; + my $promo = $class->current_entry_info; my $entry = $class->current_entry; - return unless $info; # everything's fine if nothing's promoted + return unless $promo; # everything's fine if nothing's promoted unless ($entry) { - my $poster = LJ::load_userid( $info->{'posterid'} ); + my $poster = $promo->poster; # we no longer have an entry because it's been deleted, # so let's reconstruct the URL - my $entry_url = - $poster->journal_base . '/' . $info->{'ditemid'} . '.html'; + my $entry_url = $promo->entry_url; $class->deactivate_promo( - $info->{'promoid'}, $poster, + $promo->promoid, $poster, 'reason' => 'deleted', 'entry_url' => $entry_url, - 'promoinfo' => $info, + 'promoinfo' => $promo, ); $class->send_notification( $poster, 'deactivate', 'reason' => 'deleted', 'entry_url' => $entry->url, - 'duration' => time - $info->{'started'}, + 'duration' => time - $promo->started, ); } @@ -332,11 +329,11 @@ 'details' => $reason, 'entry' => $entry, 'entry_url' => $entry->url, - 'duration' => time - $info->{'started'}, + 'duration' => time - $promo->started, ); } - unless ( $info->{'exptime'} > time ) { + unless ( $promo->exptime > time ) { $class->deactivate_entry( 'entry' => $entry, 'reason' => 'expired' ); $class->send_notification( @@ -344,11 +341,45 @@ 'reason' => 'expired', 'entry' => $entry, 'entry_url' => $entry->url, - 'duration' => time - $info->{'started'}, + 'duration' => time - $promo->started, ); } } +sub find_promos { + my ( $class, $params ) = @_; + + my @where = (1); + my @binds; + + if ( my $started_min = delete $params->{'started_min'} ) { + push @where, 'started > ?'; + push @binds, $started_min; + } + + if ( my $promoid = delete $params->{'promoid'} ) { + push @where, 'promoid = ?'; + push @binds, $promoid; + } + + if ( delete $params->{'require_active'} ) { + push @where, 'active = 1'; + } + + my $where_sql = join( ' AND ', @where ); + + my $dbh = LJ::get_db_writer(); + my $rows = $dbh->selectall_arrayref( + "SELECT * FROM selfpromo WHERE $where_sql", + { 'Slice' => {} }, @binds, + ); + + return $rows if $params->{'return_rows'}; + + my $promos = [ map { LJ::Pay::SelfPromo::Promo->new($_) } @$rows ]; + return $promos; +} + ### LOW LEVEL INTERFACE ### # !$promoid => currently promoted entry @@ -358,15 +389,14 @@ my ( $class, $promoid ) = @_; unless ($promoid) { - return LJ::MemCache::get_or_set( + my $row = LJ::MemCache::get_or_set( $class->memcache_key, sub { my $dbh = LJ::get_db_writer(); - my $res = - $dbh->selectall_arrayref( - 'SELECT * FROM selfpromo WHERE active=1', - { 'Slice' => {} } ); + my $res = $class->find_promos( + { 'require_active' => 1, 'return_rows' => 1 } + ); return 0 unless @$res; @@ -378,18 +408,18 @@ }, 3600 ); + + return unless $row; + return LJ::Pay::SelfPromo::Promo->new($row); } return do { my $dbh = LJ::get_db_writer(); - my $info = - $dbh->selectrow_hashref( - 'SELECT * FROM selfpromo WHERE promoid=?', - undef, $promoid, ); + my $promos = $class->find_promos( { 'promoid' => $promoid } ); - return unless defined $info; - return $info; + return unless @$promos; + return $promos->[0]; }; } @@ -439,12 +469,12 @@ sub deactivate_entry { my ( $class, %opts ) = @_; - my $info = $class->current_entry_info; + my $promo = $class->current_entry_info; my $entry = $opts{'entry'}; - unless ( $info - && $info->{'journalid'} == $entry->journalid - && $info->{'jitemid'} == $entry->jitemid ) + unless ( $promo + && $promo->journalid == $entry->journalid + && $promo->jitemid == $entry->jitemid ) { # your princess is in another castle, sorry @@ -452,7 +482,7 @@ } $class->deactivate_promo( - $info->{'promoid'}, $entry->poster, + $promo->promoid, $entry->poster, 'reason' => $opts{'reason'}, 'details' => $opts{'details'}, 'entry' => $opts{'entry'}, @@ -591,16 +621,16 @@ } sub calculate_refund { - my ( $class, $promoinfo ) = @_; + my ( $class, $promo ) = @_; my $unit = $LJ::SELF_PROMO_CONF->{'refund_time_unit'}; - my $duration = $promoinfo->{'exptime'} - $promoinfo->{'started'}; + my $duration = $promo->exptime - $promo->started; my $refund_step = $LJ::SELF_PROMO_CONF->{'refund_step'}; - my $remaining_time = $promoinfo->{'exptime'} - time; + my $remaining_time = $promo->exptime - time; my $remaining_time_units = int( $remaining_time / $unit ); - my $unit_cost = $promoinfo->{'cost'} * $unit / $duration; + my $unit_cost = $promo->cost * $unit / $duration; my $refund_amount = int( $remaining_time_units * $unit_cost ); return $refund_amount; @@ -673,6 +703,42 @@ return LJ::statushistory_add( $poster, $systemu, 'selfpromo', $message ); } +package LJ::Pay::SelfPromo::Promo; + +use base qw( Class::Accessor ); + +__PACKAGE__->mk_accessors( + qw( + promoid journalid posterid jitemid ditemid + started exptime cost active finished + ) +); + +sub new { + my ( $class, $data ) = @_; + return bless $data, $class; +} + +sub journal { + my ($self) = @_; + return LJ::load_userid( $self->journalid ); +} + +sub poster { + my ($self) = @_; + return LJ::load_userid( $self->posterid ); +} + +sub entry { + my ($self) = @_; + return LJ::Entry->new( $self->journalid, 'jitemid' => $self->jitemid ); +} + +sub entry_url { + my ($self) = @_; + return $self->journal->journal_base . '/' . $self->ditemid . '.html'; +} + package LJ::Pay::SelfPromo::Error; sub raise { Modified: trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm =================================================================== --- trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm 2011-09-08 09:32:01 UTC (rev 10983) +++ trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm 2011-09-09 04:19:57 UTC (rev 10984) @@ -38,12 +38,12 @@ $row->{'can_withdraw'} = $entry->poster->equals($remote); - my $info = + my $promo = LJ::Pay::SelfPromo->current_entry_info( $row->{'promoid'} ); # if for some reason the entry has already expired, this silently # casts its remaining time to zero: - my $remtime = List::Util::max( $info->{'exptime'} - time, 0 ); + my $remtime = List::Util::max( $promo->exptime - time, 0 ); my $remtime_hours = int ( $remtime / 3600 ); my $remtime_minutes = int ( ( $remtime % 3600 ) / 60 );