Committer: nnikulochkina
LJSUP-13538: Refactoring of shop promo pagesU trunk/bin/upgrading/en_LJ.dat U trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm
Modified: trunk/bin/upgrading/en_LJ.dat =================================================================== --- trunk/bin/upgrading/en_LJ.dat 2012-09-04 13:54:21 UTC (rev 12607) +++ trunk/bin/upgrading/en_LJ.dat 2012-09-05 09:37:02 UTC (rev 12608) @@ -269,6 +269,9 @@ api.error.auth_not_supported|staleness=1 api.error.auth_not_supported=Auth not supported +api.error.cannot_promote|staleness=1 +api.error.cannot_promote=Cannot promote + api.error.comment_not_found|staleness=1 api.error.comment_not_found=Comment not found @@ -281,6 +284,9 @@ api.error.entry_not_found|staleness=1 api.error.entry_not_found=Entry not found +api.error.ineligible_object|staleness=1 +api.error.ineligible_object=Ineligible object + api.error.invalid_action|staleness=1 api.error.invalid_action=Action is incorrect @@ -293,6 +299,9 @@ api.error.need_login|staleness=1 api.error.need_login=You need to login first to perform this action +api.error.promo_disabled|staleness=1 +api.error.promo_disabled=User has not enable promo + api.namespace.not_set|staleness=1 api.namespace.not_set=Namespace is not set Modified: trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm =================================================================== --- trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm 2012-09-04 13:54:21 UTC (rev 12607) +++ trunk/cgi-bin/LJ/Widget/Shop/View/SelfPromo.pm 2012-09-05 09:37:02 UTC (rev 12608) @@ -2,13 +2,13 @@ use strict; use warnings; -use base qw( LJ::Widget::Shop::View ); +use base qw( LJ::Widget::Shop::View::Promo ); use List::Util qw(); use LJ::Pay::PromotedObject qw(:types); use LJ::Pay::PromotionSlot qw(:regions :classes); -use LJ::Pay::SelfPromo qw(:codes); +#use LJ::Pay::SelfPromo qw(:codes); sub require_remote {0} sub require_cart {0} @@ -16,364 +16,25 @@ sub get_subpage {'selfpromo'} sub get_template_file {'SelfPromo'} sub get_item_template_file {'SelfPromoItem.tmpl'}; +sub promo_class {PROMO_SELF} +sub url {'/shop/selfpromo.bml'} +sub promo_types { (PROMO_OBJECT_TYPE_ENTRY(), PROMO_OBJECT_TYPE_JOURNAL(), PROMO_OBJECT_TYPE_COMMUNITY()) } +sub is_regional {1} -sub get_page_params { - my ($self) = @_; - - my $remote = LJ::get_remote(); - - $self->ml_error('/shop/selfpromo.bml.error.remote_non_sup') - unless LJ::SUP->is_remote_sup; - - unless ($remote) { - $self->ml_warning('/shop/selfpromo.bml.warning.log_in'); - } - - my $active_type = LJ::Request->get_param('type'); - $active_type = 'entry' unless ($active_type && $active_type =~ /(entry|journal|community)/); - - my $country = LJ::Request->get_param('country'); - # $country = PROMO_REGION_DEFAULT() unless ($country && $country =~ /(cyr|ua)/); - - LJ::need_string('/shop/selfpromo.bml.confirm.delete.promoted'); - LJ::need_string('/shop/selfpromo.bml.confirm.poster.cancel'); - LJ::need_res('stc/promo/promo.css'); - - my ($promo_regions, $active_region) = LJ::Pay::PromotionSlot->find_available_promo_regions($remote, $country); - - # warn "Active region: $active_region / Regions: [".join(',',@$promo_regions)."]"; - - # prepare template variables - my $result = { - 'self_url' => "/shop/selfpromo.bml", - 'item_template' => $self->get_item_template_file(), - 'type' => $active_type, - 'is_regional_promos' => scalar(@$promo_regions) > 1 ? 1 : 0, - 'country' => $active_region, - 'country_style' => { 'ua' => ' selfpromo-ua', PROMO_REGION_DEFAULT() => '' }->{$active_region}, # TODO: - }; - - foreach my $type ( PROMO_OBJECT_TYPE_ENTRY(), PROMO_OBJECT_TYPE_JOURNAL(), PROMO_OBJECT_TYPE_COMMUNITY() ) { - - $result->{'tab_'.$type.'_active'} = $type eq $active_type; # select active tab - - my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $type, region => $active_region); - - my $ml_info_title_object = LJ::Lang::ml("/shop/selfpromo.bml.info.title.$type"); - - my $elem = $slot->get_template_params(remote => $remote, shop_page => 1); - - $elem->{'ml_info_title_object'} = $ml_info_title_object, - $elem->{'form_disabled'} = !$remote ? 1 : 0; - $elem->{'buyout_price'} = LJ::Request->post_param('price') if LJ::Request->post_param('price'); - - if ($remote && $type eq 'journal') { - $elem->{'ljuser'} = $remote->ljuser_display(); - $elem->{'is_identity'} = $remote->is_identity; - } elsif ($remote && $type eq 'community') { - $elem->{'communities'} = [ map { {'user' => $_ } } grep { $_ ne $remote->{'user'} } LJ::get_authas_list($remote, {type => 'C'}) ]; - $elem->{'communities_cnt'} = scalar(@{$elem->{'communities'}}); - } elsif ($type eq 'entry') { - my %hints = $slot->promo_hints; - map {$elem->{$_} = $hints{$_}} keys %hints; - } - push @{$result->{$type}}, $elem; - } -#warn "Template params: ".Dumper($result); - return $result; +sub after_withdraw { + my ($self, %params) = @_; + return LJ::Request->redirect("$LJ::SITEROOT/shop/selfpromo.bml?type=".$params{type}."&country=".$params{region}); } -sub process_post_request { - my ($self) = @_; +sub after_buyout { + my ($self, %params) = @_; + LJ::Request->redirect("$LJ::SITEROOT/shop/thankyou.bml?cart=" . $params{cart} ); - unless ( LJ::check_form_auth() ) { - $self->ml_error('error.invalidform'); - } - - my $remote = LJ::get_remote(); - unless ($remote) { - $self->ml_error('/shop/selfpromo.bml.error.not_logged_in'); - } - - my $type = LJ::Request->post_param('type'); - $type = 'entry' unless $type =~ /(entry|journal|community)/; - - my $country = LJ::Request->post_param('country'); - # $country = PROMO_REGION_DEFAULT() unless ($country && $country =~ /(cyr|ua)/); - - my ($promo_regions, $active_region) = LJ::Pay::PromotionSlot->find_available_promo_regions($remote, $country); - my $promo_slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $type, region => $active_region); - - my $action = LJ::Request->post_param('action'); - - if ( $action eq 'promote' ) { - unless ( LJ::Request->post_param('confirm') ) { - $self->ml_error('error.unconfirmed'); - return; - } - - return $self->process_promote_request($type, $promo_slot); - } - - if ( $action eq 'withdraw' ) { - return $self->process_withdraw_request($type, $promo_slot); - } - - $self->ml_error('error.invalidform'); - - return; } -sub process_preview_request { - my ($self) = @_; - - my $format_result = sub { - my ( $result, $html ) = @_; - - if ( $result eq 'error' ) { - my $ml_title = LJ::Lang::ml('error.procrequest'); - $html = "<p><strong>$ml_title</strong></p>" . - "<ul><li><p>$html</p></li></ul>"; - } - - return LJ::JSON->to_json( { 'result' => $result, 'html' => $html } ); - }; - my $remote = LJ::get_remote(); - unless ($remote) { - return $format_result->( 'error', LJ::Lang::ml('/shop/selfpromo.bml.error.not_logged_in') ); - } - - unless ( LJ::Request->post_param('confirm') ) { - return $format_result->( 'error', LJ::Lang::ml('error.unconfirmed') ); - } - - my $type = LJ::Request->post_param('type'); - $type = 'entry' unless $type =~ /(entry|journal|community)/; - - my $country = LJ::Request->post_param('country'); - # $country = PROMO_REGION_DEFAULT() unless ($country && $country =~ /(cyr|ua)/); - - my ($promo_regions, $active_region) = LJ::Pay::PromotionSlot->find_available_promo_regions($remote, $country); - my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $type, region => $active_region); - - my $object_url = LJ::Request->post_param('object_link'); - - - unless ($object_url || $type eq 'journal') { - return $format_result->( 'error', LJ::Lang::ml('/shop/selfpromo.bml.error.'.$type.'.not_specified') ); - } - - unless ($type ne 'entry' || $object_url =~ m{^https?://}) { - $object_url = 'http://' . $object_url; - } - - my $promoted_object = $type eq 'journal' ? LJ::Pay::PromotedObject->new_from_object($remote) : LJ::Pay::PromotedObject->new_from_name($object_url, $type); - unless ($promoted_object) { - my $message = - LJ::Lang::ml( - '/shop/selfpromo.bml.error.'.$type.'_ineligible.not_found', - { 'object_url' => LJ::ehtml($object_url) } ); - return $format_result->( 'error', $message ); - } - - $object_url = $promoted_object->url; - - my $ineligible_reason; - - # ensure cross checkings - my $object_eligible = $slot->is_object_eligible($promoted_object, $remote, \$ineligible_reason); - - unless ($object_eligible) { - my $ml_var = '/shop/selfpromo.bml.error.'.$type.'_ineligible.' - . $ineligible_reason; - -# TODO: refactoring: incapsulate that logic in PromoObject -# my $ml_params = $promo_object->get_ml_params(); -# my $message = $promo_object->get_error_message(); - my $message = LJ::Lang::ml( - $ml_var, - { 'object_url' => $object_url, - 'journal_ljuser' => $promoted_object->journal_ljuser, - 'poster_ljuser' => $promoted_object->poster_ljuser, - }, - ); - - return $format_result->( 'error', $message ); - } - - # seems eligible, let's format an html for it - # and return it - my $template = LJ::HTML::Template->new( - { use_expr => 1 }, # force HTML::Template::Pro with Expr support - filename => "$ENV{'LJHOME'}/templates/Shop/SelfPromoPreview.tmpl", - ) or die "Can't open template: $!"; - - # Fill in data required to show the item - my $data = { 'object' => [ $promoted_object->get_template_params() ], - 'country' => $slot->region, - 'country_style' => $slot->country_style, - 'type' => $type, - 'item_template' => $self->get_item_template_file(), - }; - -#warn "Preview template: ".Dumper($data); - - $template->param( 'data_selfpromo' => [ $data ] ); - return $format_result->( 'success', $template->output ); +sub get_page_params { + my $self = shift; + return $self->promo_page(@_); } -# TODO: Need refactoring: Create promotion object from URLs (detect necessary), that call methods of that object Incapsulate all logic inside that object !! -sub process_promote_request { - my ($self, $type, $slot) = @_; - - my $remote = LJ::get_remote(); - my $object_url = LJ::Request->post_param('object_link'); - - unless ($object_url || $type eq 'journal') { - $self->ml_error('/shop/selfpromo.bml.error.'.$type.'.not_specified'); - } - - unless ( $type ne 'entry' || $object_url =~ m{^https?://}) { - $object_url = 'http://' . $object_url; - } - - my $object = $type eq 'journal' ? LJ::Pay::PromotedObject->new_from_object($remote) : LJ::Pay::PromotedObject->new_from_name($object_url, $type); - unless ($object) { - $self->ml_error( - '/shop/selfpromo.bml.error.'.$type.'_ineligible.not_found', - { 'object_url' => LJ::ehtml($object_url) } ); - } - - $object_url = $object->url; - - # Obsolete - #if(LJ::is_enabled('selfpromo_ua')) { - # my $anti_slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_SELF(), type => $type , anti_region => $slot->region()); - # my $anti_object = $anti_slot->current_promoted_object(); - # if ($anti_object) { - # if ( $type eq 'entry' ? $object->ditemid == $anti_object->ditemid : $object->id == $anti_object->id ) { - # $self->ml_error('selfpromo.error.two_promos_'.$type); - # } - # } - #} - - my $ineligible_reason; - my $object_eligible = $slot->is_object_eligible( $object, $remote, \$ineligible_reason ); - unless ($object_eligible) { - my $ml_var = '/shop/selfpromo.bml.error.'.$type.'_ineligible.' - . $ineligible_reason; - -# TODO (refactoring): move that logic into PromoObject abstraction (hereby the entry specific logic): -# so if PromoObject is not eligible based on checking, then show the error message -# (format it inside the object, incapsulate specific details)!!! - $self->ml_error( - $ml_var, - { 'object_url' => $object_url, - 'journal_ljuser' => $object->journal_ljuser, - 'poster_ljuser' => $object->poster_ljuser, - }, - ); - } - - my $price = int( LJ::Request->post_param('price') || 0 ); - my $cart; - - my $buyout_res = eval { - $cart = $slot->buyout( $object, $remote, $price ); - 1; - }; - - unless ($buyout_res) { - my $err = $@; - - if ( ref $err && $err->isa('LJ::Promo::Error') ) { - if ( $err->type == ERROR_OBJECT_INELIGIBLE() ) { - - # unlikely because we already checked for that, but still, - # let's raise it here as well - my $ml_var = '/shop/selfpromo.bml.error.'.$type.'_ineligible.' - . $err->details; - -#TODO (refactoring) actually move that into PromoObject - $self->ml_error( - $ml_var, - { 'object_url' => $object_url, - 'journal_ljuser' => $object->journal_ljuser, - 'poster_ljuser' => $object->poster_ljuser, - }, - ); - } - - if ( $err->type == ERROR_PRICE_INSUFFICIENT() ) { - $self->ml_error( - '/shop/selfpromo.bml.error.price_insufficient', - { 'price' => $price, - 'need' => $slot->buyout_cost, - }, - ); - } - - if ( $err->type == ERROR_INVALID_PRICE() ) { - $self->ml_error( - '/shop/selfpromo.bml.error.price_invalid', - { 'price' => $price, }, - ); - } - - if ( $err->type == ERROR_WALLET_INSUFFICIENT_FUNDS() ) { - $self->ml_error( - '/shop/selfpromo.bml.error.wallet_insufficient_funds', - { 'need' => $price, - 'have' => $err->details, - }, - ); - } - - $self->raise_errors( 'Unknown error, ' . 'code=' - . $err->type . ',' - . 'details=' - . $err->details ); - } - - # some weird error, let's re-raise it anyway - $self->raise_errors("$err"); - } - - LJ::Request->redirect( - "$LJ::SITEROOT/shop/thankyou.bml?cart=" . $cart->get_cart_as_string ); -} - -sub process_withdraw_request { - my ($self, $type, $slot) = @_; - - # TODO: Object may already has been changed - $slot->check_current_promotion; - - my $object_url = LJ::Request->post_param('withdraw_url'); - - # TODO (refactoring): - - my $promo = $slot->current_promotion; - - unless ($promo) { - - # apparently check_current_entry cleared it out, - # let's silently redirect them back - return LJ::Request->redirect("$LJ::SITEROOT/shop/selfpromo.bml?type=$type"); - } - - my $object = $promo->promoted_object; - unless ( $promo->object_url eq $object_url ) { - - # not withdrawing the correct entry, race condition? - # whatever, let's return an invalidform and let them - # figure it out - $self->ml_error('error.invalidform'); - } - - $slot->withdraw_object($object); - - return LJ::Request->redirect("$LJ::SITEROOT/shop/selfpromo.bml?type=$type&country=".$slot->region); -} - 1;