add uiLevel asset attribute. Begin to work on write.

This commit is contained in:
Colin Kuskie 2010-01-04 19:14:53 -08:00
parent bfccc1fa6f
commit 0627d7adbf
4 changed files with 76 additions and 75 deletions

View file

@ -24,6 +24,7 @@ use WebGUI::Definition::Asset;
attribute assetName => 'asset'; attribute assetName => 'asset';
attribute tableName => 'assetData'; attribute tableName => 'assetData';
attribute icon => 'assets.gif'; attribute icon => 'assets.gif';
attribute uiLevel => 1;
property title => ( property title => (
tab => "properties", tab => "properties",
label => ['99','Asset'], label => ['99','Asset'],
@ -2274,89 +2275,74 @@ sub toggleToolbar {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 update ( properties ) =head2 write ( )
Updates the properties of an existing revision. If you want to create a new revision, please use addRevision(). Stores the current properties of the asset in the database.
=head3 properties
Hash reference of properties and values to set.
NOTE: C<keywords> is a special property that uses the WebGUI::Keyword API
to set the keywords for this asset.
=cut =cut
sub willWriteDataToDbSomeday { sub write {
my $self = shift; my $self = shift;
my $requestedProperties = shift; $self->lastModified(time());
my $properties = clone($requestedProperties);
$properties->{lastModified} = time();
# if keywords were specified, then let's set them the right way # if keywords were specified, then let's set them the right way
if (exists $properties->{keywords}) { #if (exists $properties->{keywords}) {
WebGUI::Keyword->new($self->session)->setKeywordsForAsset( # WebGUI::Keyword->new($self->session)->setKeywordsForAsset(
{keywords=>$properties->{keywords}, asset=>$self}); # {keywords=>$properties->{keywords}, asset=>$self});
} #}
##If inheritUrlFromParent was sent, and it is true, then muck with the url
##The URL may have been sent too, so use it or the current Asset's URL.
if (exists $properties->{inheritUrlFromParent} and $properties->{inheritUrlFromParent}) {
$properties->{'url'} = $self->url($properties->{'url'} || $self->url);
}
# check the definition of all properties against what was given to us # check the definition of all properties against what was given to us
my %setPairs = (); # my %setPairs = ();
my %tableFields = (); # my %tableFields = ();
foreach my $property ($self->getProperties) { # foreach my $property ($self->getProperties) {
#
# skip a property unless it was passed in to update # # skip a property unless it was passed in to update
next unless (exists $properties->{$property}); # next unless (exists $properties->{$property});
#
# get the property definition # # get the property definition
my $propertyDefinition = $self->getProperty($property); # my $propertyDefinition = $self->getProperty($property);
#
# get a list of the fields available in this table so we don't try to insert # # get a list of the fields available in this table so we don't try to insert
# something for a field that doesn't exist # # something for a field that doesn't exist
my $table = $propertyDefinition->{tableName}; # my $table = $propertyDefinition->{tableName};
unless (exists $tableFields{$table}) { # unless (exists $tableFields{$table}) {
my $sth = $self->session->db->read('DESCRIBE `'.$table.'`'); # my $sth = $self->session->db->read('DESCRIBE `'.$table.'`');
while (my ($col) = $sth->array) { # while (my ($col) = $sth->array) {
$tableFields{$table}{$col} = 1; # $tableFields{$table}{$col} = 1;
} # }
} # }
#
# skip properties that aren't yet in the table # # skip properties that aren't yet in the table
if (!exists $tableFields{$table}{$property}) { # if (!exists $tableFields{$table}{$property}) {
$self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$table."'"); # $self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$table."'");
next; # next;
} # }
#
# use the update value # # use the update value
my $value = $properties->{$property}; # my $value = $properties->{$property};
# use the current value because the update value was undef # # use the current value because the update value was undef
unless (defined $value) { # unless (defined $value) {
$value = $self->get($property); # $value = $self->get($property);
} # }
#
# set the property # # set the property
if ($propertyDefinition->{serialize}) { # if ($propertyDefinition->{serialize}) {
$setPairs{$table}{$property} = JSON->new->canonical->encode($value); # $setPairs{$table}{$property} = JSON->new->canonical->encode($value);
} # }
else { # else {
$setPairs{$table}{$property} = $value; # $setPairs{$table}{$property} = $value;
} # }
$self->{_properties}{$property} = $value; # $self->{_properties}{$property} = $value;
} # }
#
# if there's anything to update, then do so # # if there's anything to update, then do so
my $db = $self->session->db; # my $db = $self->session->db;
foreach my $table (keys %setPairs) { # foreach my $table (keys %setPairs) {
my @values = values %{$setPairs{$table}}; # my @values = values %{$setPairs{$table}};
my @columnNames = map { $_.'=?' } keys %{$setPairs{$table}}; # my @columnNames = map { $_.'=?' } keys %{$setPairs{$table}};
push(@values, $self->getId, $self->get("revisionDate")); # push(@values, $self->getId, $self->get("revisionDate"));
$db->write("update ".$table." set ".join(",",@columnNames)." where assetId=? and revisionDate=?",\@values); # $db->write("update ".$table." set ".join(",",@columnNames)." where assetId=? and revisionDate=?",\@values);
} # }
# we've changed something so we need to update our size # we've changed something so we need to update our size
$self->setSize(); $self->setSize();

View file

@ -55,7 +55,7 @@ sub property_meta {
return 'WebGUI::Definition::Meta::Property::Asset'; return 'WebGUI::Definition::Meta::Property::Asset';
} }
has [ qw{tableName icon assetName} ] => ( has [ qw{tableName icon assetName uiLevel} ] => (
is => 'rw', is => 'rw',
); );
@ -88,6 +88,15 @@ The second is the i18n namespace to find the asset's name.
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 uiLevel ( )
An integer, representing how difficult the Asset will be to use. The default uiLevel is
1. uiLevels for an asset can be overridden in the config file for each site.
=cut
#-------------------------------------------------------------------
=head2 get_tables ( ) =head2 get_tables ( )
Returns an array of the names of all tables in every class used by Returns an array of the names of all tables in every class used by

View file

@ -140,3 +140,8 @@ my $session = WebGUI::Test->session;
isa_ok $asset, 'WebGUI::Asset'; isa_ok $asset, 'WebGUI::Asset';
is $asset->title, 'Root', 'got the right asset'; is $asset->title, 'Root', 'got the right asset';
} }
{
note "uiLevel";
is +WebGUI::Asset->meta->uiLevel, 1, 'uiLevel: default for assets is 1';
}

View file

@ -134,6 +134,7 @@ use WebGUI::Test;
[qw/asset/], [qw/asset/],
'get_tables works for a simple asset' 'get_tables works for a simple asset'
); );
} }
{ {