Added an asset called Shelf, which allows you to create categories of Skus.

This commit is contained in:
JT Smith 2008-04-29 01:26:45 +00:00
parent e9853fdf5c
commit 82056c66aa
10 changed files with 266 additions and 29 deletions

View file

@ -947,9 +947,9 @@ sub getEditForm {
# display keywords field
$tabform->getTab('meta')->text(
name => 'keywords',
value => $self->get('keywords'),
label => $i18n->get('keywords'),
hoverHelp => $i18n->get('keywords help'),
value => $self->get('keywords'),
);
# metadata / content profiling
@ -1954,7 +1954,7 @@ sub prepareView {
my $self = shift;
$self->{_toolbar} = $self->getToolbar;
my $style = $self->session->style;
my @keywords = $self->get('keywords');
my @keywords = @{WebGUI::Keyword->new($self->session)->getKeywordsForAsset({asset=>$self, asArrayRef=>1})};
if (scalar @keywords) {
$style->setMeta( {
name => 'keywords',

View file

@ -279,6 +279,19 @@ sub getTaxRate {
#-------------------------------------------------------------------
=head2 getThumbnailUrl ( )
Returns undef. Should be overridden by any skus that have images.
=cut
sub getThumbnailUrl {
my $self = shift;
return undef;
}
#-------------------------------------------------------------------
=head2 getWeight ( )
Returns 0. Needs to be overriden by subclasses.

View file

@ -277,24 +277,11 @@ sub getPrice {
return $self->get('price');
}
#-------------------------------------------------------------------
sub getThumbnailFilename {
my $self = shift;
my $filestore = $_[0];
my $files = $filestore->getFiles();
foreach my $file (@{$files}){
if($file =~ m/^thumb-/){
return $file;
}
}
return "";
}
#-------------------------------------------------------------------
sub getThumbnailUrl {
my $self = shift;
my $store = $_[0];
return $store->getUrl($self->getThumbnailFilename($store));
my $store = shift || WebGUI::Storage::Image->get($self->session, $self->get('image1'));
return $store->getThumbnailUrl($store->getFiles->[0]);
}
#-------------------------------------------------------------------

View file

@ -0,0 +1,129 @@
package WebGUI::Asset::Wobject::Shelf;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2008 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use Tie::IxHash;
use WebGUI::International;
use base 'WebGUI::Asset::Wobject';
#-------------------------------------------------------------------
=head2 definition ( )
Add our custom properties of templateId to this asset.
=cut
sub definition {
my ($class, $session, $definition) = @_;
my $i18n = WebGUI::International->new($session, 'Asset_Shelf');
my %properties;
tie %properties, 'Tie::IxHash';
%properties = (
templateId =>{
fieldType => "template",
defaultValue => 'nFen0xjkZn8WkpM93C9ceQ',
tab => "display",
namespace => "Shelf",
hoverHelp => $i18n->get('view template help'),
label => $i18n->get('view template'),
}
);
push(@{$definition}, {
assetName => $i18n->get('assetName'),
icon => 'Shelf.gif',
autoGenerateForms => 1,
tableName => 'Shelf',
className => 'WebGUI::Asset::Wobject::Shelf',
properties => \%properties
});
return $class->SUPER::definition($session, $definition);
}
#-------------------------------------------------------------------
=head2 prepareView ( )
See WebGUI::Asset::prepareView() for details.
=cut
sub prepareView {
my $self = shift;
$self->SUPER::prepareView();
my $template = WebGUI::Asset::Template->new($self->session, $self->get("templateId"));
$template->prepare;
$self->{_viewTemplate} = $template;
}
#-------------------------------------------------------------------
=head2 view ( )
method called by the www_view method. Returns a processed template
to be displayed within the page style.
=cut
sub view {
my $self = shift;
my $session = $self->session;
# get other shelves
my @children = ();
foreach my $child (@{$self->getLineage(['children'],{returnObjects=>1,includeOnlyClasses=>['WebGUI::Asset::Wobject::Shelf']})}) {
my $properties = $child->get;
$child->{url} = $self->getUrl;
push @children, $child;
}
# find products based upon keywords
my @keywords = $self->get('keywords');
my $p = WebGUI::Keyword->new($session)->getMatchingAssets({
matchAssetKeywords => $self,
isa => 'WebGUI::Asset::Sku',
usePaginator => 1,
});
$p->setBaseUrl($self->getUrl('func=view'));
# generate template variables
my @skus = ();
foreach my $row (@{$p->getPageData}) {
my $id = $row->{assetId};
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);
push @skus, $sku;
}
else {
$session->errorHandler->error(q|Couldn't instanciate SKU with assetId |.$id.q| on shelf with assetId |.$self->getId);
}
}
my %var = (
shelves => \@children,
products => \@skus,
);
$p->appendTemplateVars(\%var);
# render page
return $self->processTemplate(\%var, undef, $self->{_viewTemplate});
}
1;

View file

@ -157,7 +157,7 @@ sub getKeywordsForAsset {
return \@keywords;
}
else {
return wantarray ? @keywords : join(" ", map({ m/\s/ ? '"' . $_ . '"' : $_ } @keywords));
return join(" ", map({ m/\s/ ? '"' . $_ . '"' : $_ } @keywords));
}
}
@ -166,34 +166,89 @@ sub getKeywordsForAsset {
=head2 getMatchingAssets ( { startAsset => $asset, keyword => $keyword } )
Returns an array reference of asset ids matching the start point + keyword.
Returns an array reference of asset ids matching the params.
=head3 startAsset
An asset object where you'd like to start searching for matching keywords.
An asset object where you'd like to start searching for matching keywords. Doesn't search any particular branch if one isn't specified.
=head3 keyword
The keyword to match.
=head3 keywords
An array reference of keywords to match.
=head3 matchAssetKeywords
A reference to an asset that has a list of keywords to match. This can help locate assets that are similar to another asset.
=head3 isa
A classname pattern to match. For example, if you provide 'WebGUI::Asset::Sku' then everything that has a class name that starts with that including 'WebGUI::Asset::Sku::Product' will be included.
=head3 usePaginator
Instead of returning an array reference of assetId's, return a paginator object.
Instead of returning an array reference of assetId's, return a paginator object.
=cut
sub getMatchingAssets {
my ($self, $options) = @_;
my $query = "select assetKeyword.assetId from assetKeyword left join asset using (assetId)
where lineage like ? and keyword=? order by creationDate desc";
my $params = [$options->{startAsset}->get("lineage").'%', $options->{keyword}];
# base query
my @clauses = ();
my @params = ();
# what lineage are we looking for
if (exists $options->{startAsset}) {
push @clauses, 'lineage like ?';
push @params, $options->{startAsset}->get("lineage").'%';
}
# matching keywords against another asset
if (exists $options->{matchAssetKeywords}) {
$options->{keywords} = $self->getKeywordsForAsset({
asset => $options->{matchAssetKeywords},
asArrayRef => 1,
});
}
# looking for a class name match
if (exists $options->{isa}) {
push @clauses, 'className like ?';
push @params, $options->{isa}.'%';
}
# looking for a single keyword
if (exists $options->{keyword}) {
push @clauses, 'keyword=?';
push @params, $options->{keyword};
}
# looking for a list of keywords
if (exists $options->{keywords}) {
my @placeholders = ();
foreach my $word (@{$options->{keywords}}){
push @placeholders, '?';
push @params, $word;
}
push @clauses, 'keyword in ('.join(',', @placeholders).')';
}
# write the query
my $query = 'select distinct assetKeyword.assetId from assetKeyword left join asset using (assetId)
where '.join(' and ', @clauses).' order by creationDate desc';
# perform the search
if ($options->{usePaginator}) {
my $p = WebGUI::Paginator->new($self->session);
$p->setDataByQuery($query, undef, undef, $params);
$p->setDataByQuery($query, undef, undef, \@params);
return $p;
}
else {
return $self->session->db->buildArrayRef($query, $params);
return $self->session->db->buildArrayRef($query, \@params);
}
}

View file

@ -0,0 +1,31 @@
package WebGUI::i18n::English::Asset_Shelf;
use strict;
our $I18N = {
'subcategories' => {
message => q|Subcategories|,
lastUpdated => 0,
context => q|a template label|,
},
'view template' => {
message => q|View Template|,
lastUpdated => 0,
context => q|a property|,
},
'view template help' => {
message => q|Choose the template that will display the list of products associated with this shelf.|,
lastUpdated => 0,
context => q|help for a property|,
},
'assetName' => {
message => q|Shelf|,
lastUpdated => 0,
context => q|the name of the asset|,
},
};
1;