diff --git a/docs/upgrades/packages-7.7.16/root_import_shelf-default.wgpkg b/docs/upgrades/packages-7.7.16/root_import_shelf-default.wgpkg new file mode 100644 index 000000000..177518f11 Binary files /dev/null and b/docs/upgrades/packages-7.7.16/root_import_shelf-default.wgpkg differ diff --git a/lib/WebGUI/Asset/Wobject/Shelf.pm b/lib/WebGUI/Asset/Wobject/Shelf.pm index 3c13cef0e..545ad24cd 100644 --- a/lib/WebGUI/Asset/Wobject/Shelf.pm +++ b/lib/WebGUI/Asset/Wobject/Shelf.pm @@ -288,8 +288,9 @@ sub view { # get other shelves my @childShelves = (); - foreach my $child (@{$self->getLineage(['children'],{returnObjects=>1,includeOnlyClasses=>['WebGUI::Asset::Wobject::Shelf']})}) { - my $properties = $child->get; + SHELF: foreach my $child (@{$self->getLineage(['children'],{returnObjects=>1,includeOnlyClasses=>['WebGUI::Asset::Wobject::Shelf']})}) { + next SHELF unless $child->canView; + my $properties = $child->get; $child->{url} = $child->getUrl; $child->{title} = $child->getTitle; push @childShelves, $child; @@ -305,30 +306,38 @@ sub view { isa => 'WebGUI::Asset::Sku', }); + ##Prescreen to only paginate viewable products + my @productIds = List::MoreUtils::uniq(@childSkus, @{$keywordBasedAssetIds}); + my @products = (); + PRODUCT: foreach my $id (@productIds) { + my $asset = WebGUI::Asset->newByDynamicClass($session, $id); + if (defined $asset && $asset->canView) { + push @products, $asset; + } + else { + $session->errorHandler->error(q|Couldn't instanciate SKU with assetId |.$id.q| on shelf with assetId |.$self->getId); + } + } + # create paginator - my @products = List::MoreUtils::uniq(@childSkus, @{$keywordBasedAssetIds}); my $p = WebGUI::Paginator->new($session, $self->getUrl('func=view')); $p->setDataByArrayRef(\@products); # generate template variables - my @skus = (); - foreach my $id (@{$p->getPageData}) { - my $asset = WebGUI::Asset->newByDynamicClass($session, $id); - if (defined $asset) { - my $sku = $asset->get; - $sku->{url} = $asset->getUrl; - $sku->{thumbnailUrl} = $asset->getThumbnailUrl; - $sku->{price} = sprintf("%.2f", $asset->getPrice); - $sku->{addToCartForm} = $asset->getAddToCartForm; - push @skus, $sku; - } - else { - $session->errorHandler->error(q|Couldn't instanciate SKU with assetId |.$id.q| on shelf with assetId |.$self->getId); - } - } + my @skus = (); + foreach my $asset (@{$p->getPageData}) { + my $sku = $asset->get; + $sku->{url} = $asset->getUrl; + $sku->{thumbnailUrl} = $asset->getThumbnailUrl; + $sku->{price} = sprintf("%.2f", $asset->getPrice); + $sku->{addToCartForm} = $asset->getAddToCartForm; + push @skus, $sku; + } my %var = ( - shelves => \@childShelves, - products => \@skus, + shelves => \@childShelves, + products => \@skus, + noViewableSkus => scalar(@skus) ? 0 : 1, + emptyShelf => scalar(@productIds) ? 0 : 1, ); $p->appendTemplateVars(\%var); diff --git a/lib/WebGUI/Help/Asset_Shelf.pm b/lib/WebGUI/Help/Asset_Shelf.pm index 9e67a20db..3cb818b23 100644 --- a/lib/WebGUI/Help/Asset_Shelf.pm +++ b/lib/WebGUI/Help/Asset_Shelf.pm @@ -29,6 +29,8 @@ our $HELP = { ], }, { name => "templateId", description=>"shelf template help" }, + { name => "noViewableSkus", }, + { name => "emptyShelf", }, ], related => [ ], diff --git a/lib/WebGUI/i18n/English/Asset_Shelf.pm b/lib/WebGUI/i18n/English/Asset_Shelf.pm index 52aad39ba..5538cdf8d 100644 --- a/lib/WebGUI/i18n/English/Asset_Shelf.pm +++ b/lib/WebGUI/i18n/English/Asset_Shelf.pm @@ -46,14 +46,14 @@ our $I18N = { }, 'shelves' => { - message => q|A loop containing the list of shelves that are children of this one in the asset tree. Each record in the loop contains all the properties of a shelf.|, + message => q|A loop containing the list of shelves that are children of this one in the asset tree. Each record in the loop contains all the properties of a shelf. Only shelves that the user can see will be in the loop.|, lastUpdated => 0, context => q|a template variable|, }, 'products' => { - message => q|A loop containing the list of products that match the keywords specified in this shelf. Each record in the loop contains all the properties of the matching sku, plus the following variables.|, - lastUpdated => 0, + message => q|A loop containing the list of products that match the keywords specified in this shelf, or that are children of this shelf. Only products that the user can see will be in the loop. Each record in the loop contains all the properties of the matching sku, plus the following variables.|, + lastUpdated => 1247603018, context => q|a template variable|, }, @@ -75,6 +75,30 @@ our $I18N = { context => q|help for a property|, }, + 'noViewableSkus' => { + message => q|A boolean which is true if there are no products on this shelf which the current user can view.|, + lastUpdated => 0, + context => q|Template variable help|, + }, + + 'emptyShelf' => { + message => q|A boolean which is true if this shelf has any Products at all.|, + lastUpdated => 0, + context => q|Template variable help|, + }, + + 'this shelf is empty' => { + message => q|This shelf is empty.|, + lastUpdated => 0, + context => q|template label|, + }, + + 'You do not have permission to view the products on this shelf' => { + message => q|You do not have permission to view the products on this shelf.|, + lastUpdated => 0, + context => q|template label|, + }, + 'assetName' => { message => q|Shelf|, lastUpdated => 0, diff --git a/t/Asset/Wobject/Shelf.t b/t/Asset/Wobject/Shelf.t index 4ed68af15..dd0bf23f9 100644 --- a/t/Asset/Wobject/Shelf.t +++ b/t/Asset/Wobject/Shelf.t @@ -20,11 +20,13 @@ use Test::More; use Test::Deep; use Exception::Class; use Data::Dumper; +use JSON; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; use WebGUI::Text; use WebGUI::Asset::Sku::Product; +use WebGUI::VersionTag; #---------------------------------------------------------------------------- # Init @@ -33,7 +35,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 55; +my $tests = 61; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -42,15 +44,14 @@ plan tests => 1 + $tests; my $class = 'WebGUI::Asset::Wobject::Shelf'; my $loaded = use_ok($class); -my $storage; my ($e, $failure); -my ($shelf, $shelf2); SKIP: { skip "Unable to load module $class", $tests unless $loaded; - $shelf = WebGUI::Asset->getRoot($session)->addChild({className => $class}); + my $root = WebGUI::Asset->getRoot($session); + my $shelf = $root->addChild({className => $class}); ####################################################################### # @@ -383,7 +384,7 @@ SKIP: { # ####################################################################### - $shelf2 = WebGUI::Asset->getRoot($session)->addChild({className => $class}); + my $shelf2 = $root->addChild({className => $class}); $pass = 0; eval { @@ -443,15 +444,64 @@ SKIP: { $shelf2->purge; undef $shelf2; -} + ##Clear out this tag so we can do downstream work. + my $tag = WebGUI::VersionTag->getWorking($session); + $tag->commit; + WebGUI::Test->tagsToRollback($tag); + + ####################################################################### + # + # Template variables + # + ####################################################################### + + my $tommy = WebGUI::User->create($session); + my $warden = WebGUI::User->create($session); + WebGUI::Test->usersToDelete($tommy, $warden); + my $inGroup = WebGUI::Group->new($session, 'new'); + WebGUI::Test->groupsToDelete($inGroup); + $inGroup->addUsers([$tommy->getId]); + + my $testTemplate = $root->addChild({ + className => 'WebGUI::Asset::Template', + template => q|{ "noViewableSkus":"","emptyShelf":""}|, + }); + my $testShelf = $root->addChild({ + className => $class, + templateId => $testTemplate->getId, + }); + my $tag2 = WebGUI::VersionTag->getWorking($session); + WebGUI::Test->tagsToRollback($tag2); + $tag2->commit; + $session->user({userId => 1}); + $testShelf->prepareView; + my $json = $testShelf->view; + my $vars = eval { from_json($json) }; + ok( $vars->{emptyShelf}, 'empty shelf: yes'); + ok( $vars->{noViewableSkus}, 'viewable skus: none'); + + my $privateSku = $testShelf->addChild({ + className => 'WebGUI::Asset::Sku::Product', + groupIdView => $inGroup->getId, + title => 'Private Product', + }); + my $tag3 = WebGUI::VersionTag->getWorking($session); + WebGUI::Test->tagsToRollback($tag3); + $tag3->commit; + + $session->user({user => $tommy}); + $testShelf->prepareView; + $json = $testShelf->view; + $vars = eval { from_json($json) }; + ok( !$vars->{emptyShelf}, 'empty shelf, no'); + ok( !$vars->{noViewableSkus}, 'viewable skus: yes for user in group'); + + $session->user({user => $warden}); + $testShelf->prepareView; + $json = $testShelf->view; + $vars = eval { from_json($json) }; + + ok( !$vars->{emptyShelf}, 'empty shelf, no'); + ok( $vars->{noViewableSkus}, 'viewable skus: none for user not in viewable group'); -#---------------------------------------------------------------------------- -# Cleanup -END { - if (defined $shelf and ref $shelf eq $class) { - $shelf->purge; - } - if (defined $shelf2 and ref $shelf2 eq $class) { - $shelf2->purge; - } }