eliminated more definitions

This commit is contained in:
JT Smith 2009-10-22 14:26:09 -05:00
parent fca4cfbbaf
commit 57b67ab4da
5 changed files with 108 additions and 96 deletions

View file

@ -39,6 +39,10 @@ You must migrate your asset to use the new WebGUI::Definition::Asset class inste
9) You no longer have the "visible" element. It was a duplicate of "noFormPost", so use "noFormPost" instead. 9) You no longer have the "visible" element. It was a duplicate of "noFormPost", so use "noFormPost" instead.
10) You no longer have the "displayOnly" element. Make a custom form control instead.
11) You no longer have the "allowEmpty" element. However, you can now specify an initial value in the "value" element, and set "defaultValue" to undef if you want to have an initial value but allow the field to become empty or undef.
Here's an example. Here's an example.
use WebGUI::Definition::Asset ( use WebGUI::Definition::Asset (

View file

@ -908,7 +908,7 @@ sub getEditForm {
$params{value} = [$params{value}]; $params{value} = [$params{value}];
} }
%params = (%params, %fieldHash); %params = (%fieldHash, %params);
delete $params{tab}; delete $params{tab};
delete $params{tableName}; delete $params{tableName};
@ -1587,10 +1587,18 @@ sub new {
my $where = " where asset.assetId=?"; my $where = " where asset.assetId=?";
my $placeHolders = [$assetId]; my $placeHolders = [$assetId];
foreach my $definition (@{$class->definition($session)}) { # join all the tables
my %tables;
foreach my $property ($class->getProperties) {
my $definition = $class->getProperty($property);
%tables{$definition->{tableName}} = 1;
}
foreach my $table (keys %tables) {
$sql .= ",".$definition->{tableName}; $sql .= ",".$definition->{tableName};
$where .= " and (asset.assetId=".$definition->{tableName}.".assetId and ".$definition->{tableName}.".revisionDate=".$revisionDate.")"; $where .= " and (asset.assetId=".$definition->{tableName}.".assetId and ".$definition->{tableName}.".revisionDate=".$revisionDate.")";
} }
# fetch properties
$properties = $session->db->quickHashRef($sql.$where, $placeHolders); $properties = $session->db->quickHashRef($sql.$where, $placeHolders);
unless (exists $properties->{assetId}) { unless (exists $properties->{assetId}) {
$session->errorHandler->error("Asset $assetId $class $revisionDate is missing properties. Consult your database tables for corruption. "); $session->errorHandler->error("Asset $assetId $class $revisionDate is missing properties. Consult your database tables for corruption. ");
@ -1602,13 +1610,12 @@ sub new {
if (defined $properties) { if (defined $properties) {
my $object = { _session=>$session, _properties => $properties }; my $object = { _session=>$session, _properties => $properties };
bless $object, $class; bless $object, $class;
foreach my $definition (@{ $object->definition($session) }) { foreach my $property ($object->getProperties) {
foreach my $property (keys %{ $definition->{properties} }) { my $definition = $object->getProperty($property);
if ($definition->{properties}->{$property}->{serialize} && $object->{_properties}->{$property} ne '') { if ($definition->{serialize} && $object->{_properties}->{$property} ne '') {
$object->{_properties}->{$property} = JSON->new->canonical->decode($object->{_properties}->{$property}); $object->{_properties}->{$property} = JSON->new->canonical->decode($object->{_properties}->{$property});
} }
} }
}
return $object; return $object;
} }
$session->errorHandler->error("Something went wrong trying to instanciate a '$className' with assetId '$assetId', but I don't know what!"); $session->errorHandler->error("Something went wrong trying to instanciate a '$className' with assetId '$assetId', but I don't know what!");
@ -2000,9 +2007,8 @@ sub processPropertiesFromFormPost {
my $form = $self->session->form; my $form = $self->session->form;
my $overrides = $self->session->config->get("assets/".$self->get("className")."/fields"); my $overrides = $self->session->config->get("assets/".$self->get("className")."/fields");
foreach my $definition (@{$self->definition($self->session)}) { foreach my $property ($self->getProperties) {
foreach my $property (keys %{$definition->{properties}}) { my %params = %{$self->getProperty($property)};
my %params = %{$definition->{properties}{$property}};
# apply config file changes # apply config file changes
foreach my $key (keys %{$overrides->{$property}}) { foreach my $key (keys %{$overrides->{$property}}) {
@ -2027,7 +2033,6 @@ sub processPropertiesFromFormPost {
\%params \%params
); );
} }
}
$data{keywords} = $form->process("keywords"); $data{keywords} = $form->process("keywords");
if ($self->session->setting->get("metaDataEnabled")) { if ($self->session->setting->get("metaDataEnabled")) {
my $meta = $self->getMetaDataFields; my $meta = $self->getMetaDataFields;
@ -2283,31 +2288,29 @@ sub update {
} }
# check the definition of all properties against what was given to us # check the definition of all properties against what was given to us
foreach my $definition (reverse @{$self->definition($self->session)}) {
my %setPairs = (); my %setPairs = ();
my %tableFields = ();
foreach my $property ($self->getProperties) {
# skip a property unless it was passed in to update
next unless (exists $properties->{$property});
# get the property definition
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 %tableFields = (); my $table = $propertyDefinition->{tableName};
my $sth = $self->session->db->read('DESCRIBE `'.$definition->{tableName}.'`'); unless (exists $tableFields{$table}) {
my $sth = $self->session->db->read('DESCRIBE `'.$table.'`');
while (my ($col) = $sth->array) { while (my ($col) = $sth->array) {
$tableFields{$col} = 1; $tableFields{$table}{$col} = 1;
}
} }
# deal with all the properties in this part of the definition
foreach my $property (keys %{$definition->{properties}}) {
# # skip a property unless it was specified to be set by the properties field or has a default value
# next unless (exists $properties->{$property} || exists $definition->{properties}{$property}{defaultValue});
# skip a property unless it was specified to be set by the properties field
next unless (exists $properties->{$property});
my $propertyDefinition = $definition->{properties}{$property};
# skip a property if it has the display only flag set
next if ($propertyDefinition->{displayOnly});
# skip properties that aren't yet in the table # skip properties that aren't yet in the table
if (!exists $tableFields{$property}) { if (!exists $tableFields{$table}{$property}) {
$self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$definition->{tableName}."'"); $self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$table."'");
next; next;
} }
@ -2318,39 +2321,24 @@ sub update {
$value = $self->get($property); $value = $self->get($property);
} }
# apply filter logic on a property to validate or fix it's value
if (exists $propertyDefinition->{filter}) {
my $filter = $propertyDefinition->{filter};
$value = $self->$filter($value, $property);
}
# if the value is undefined, use the default if possible
# unless allowEmpty has been set, do this for empty strings as well
if ( ( !defined $value || ( $value eq q{} && ! $propertyDefinition->{allowEmpty} ) )
&& exists $propertyDefinition->{defaultValue} ) {
$value = $propertyDefinition->{defaultValue};
if (ref($value) eq 'ARRAY') {
$value = $value->[0];
}
}
# set the property # set the property
if ($propertyDefinition->{serialize}) { if ($propertyDefinition->{serialize}) {
$setPairs{$property} = JSON->new->canonical->encode($value); $setPairs{$table}{$property} = JSON->new->canonical->encode($value);
} }
else { else {
$setPairs{$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
if (scalar(keys %setPairs) > 0) { my $db = $self->session->db;
my @values = values %setPairs; foreach my $table (keys %setPairs) {
my @columnNames = map { $_.'=?' } keys %setPairs; my @values = values %{$setPairs{$table}};
my @columnNames = map { $_.'=?' } keys %{$setPairs{$table}};
push(@values, $self->getId, $self->get("revisionDate")); push(@values, $self->getId, $self->get("revisionDate"));
$self->session->db->write("update ".$definition->{tableName}." 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

View file

@ -115,8 +115,8 @@ sub definition {
}, },
feedHeaderLinks => { feedHeaderLinks => {
fieldType => "checkList", fieldType => "checkList",
allowEmpty => 1, value => "rss\natom",
defaultValue => "rss\natom", defaultValue => undef,
tab => "rss", tab => "rss",
options => do { options => do {
my %headerLinksOptions; my %headerLinksOptions;

View file

@ -403,6 +403,20 @@ sub getValue {
return $self->getValueFromPost; return $self->getValueFromPost;
} }
#-------------------------------------------------------------------
=head2 getValueAsScalar
Returns the value as a scalar, which means for complex types it's returned as a serialized string of some sort.
=cut
sub getValueAsScalar {
my $self = shift;
return $self->getValue;
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getOriginalValue ( ) =head2 getOriginalValue ( )

View file

@ -2,6 +2,12 @@ package WebGUI::i18n::English::WebGUI;
use strict; use strict;
our $I18N = { our $I18N = {
'JSON Blob' => {
message => q|JSON Blob|,
context => q|The name of hte JSON Blob form control.|,
lastUpdated => 0,
},
'ok' => { 'ok' => {
message => q|OK|, message => q|OK|,
context => q|used by database link and other things to give a message to the user that a test passed|, context => q|used by database link and other things to give a message to the user that a test passed|,