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};
@ -1586,11 +1586,19 @@ sub new {
my $sql = "select * from asset"; my $sql = "select * from asset";
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,11 +1610,10 @@ 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;
@ -2000,33 +2007,31 @@ 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}}) {
$params{$key} = $overrides->{$property}{$key}; $params{$key} = $overrides->{$property}{$key};
}
# deal with properties that can't be posted through the form
if ($params{noFormPost}) {
if ($form->process("assetId") eq "new" && $self->get($property) eq "") {
$data{$property} = $params{defaultValue};
}
next;
}
# process the form element
$params{name} = $property;
$params{value} = $self->get($property);
$data{$property} = $form->process(
$property,
$params{fieldType},
$params{defaultValue},
\%params
);
} }
# deal with properties that can't be posted through the form
if ($params{noFormPost}) {
if ($form->process("assetId") eq "new" && $self->get($property) eq "") {
$data{$property} = $params{defaultValue};
}
next;
}
# process the form element
$params{name} = $property;
$params{value} = $self->get($property);
$data{$property} = $form->process(
$property,
$params{fieldType},
$params{defaultValue},
\%params
);
} }
$data{keywords} = $form->process("keywords"); $data{keywords} = $form->process("keywords");
if ($self->session->setting->get("metaDataEnabled")) { if ($self->session->setting->get("metaDataEnabled")) {
@ -2283,76 +2288,59 @@ 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}) {
while (my ($col) = $sth->array) { my $sth = $self->session->db->read('DESCRIBE `'.$table.'`');
$tableFields{$col} = 1; while (my ($col) = $sth->array) {
} $tableFields{$table}{$col} = 1;
}
}
# deal with all the properties in this part of the definition # skip properties that aren't yet in the table
foreach my $property (keys %{$definition->{properties}}) { if (!exists $tableFields{$table}{$property}) {
$self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$table."'");
next;
}
# # skip a property unless it was specified to be set by the properties field or has a default value # use the update value
# next unless (exists $properties->{$property} || exists $definition->{properties}{$property}{defaultValue}); my $value = $properties->{$property};
# skip a property unless it was specified to be set by the properties field # use the current value because the update value was undef
next unless (exists $properties->{$property}); unless (defined $value) {
my $propertyDefinition = $definition->{properties}{$property}; $value = $self->get($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 # set the property
if (!exists $tableFields{$property}) { if ($propertyDefinition->{serialize}) {
$self->session->log->error("update() tried to set field named '".$property."' which doesn't exist in table '".$definition->{tableName}."'"); $setPairs{$table}{$property} = JSON->new->canonical->encode($value);
next; }
} else {
$setPairs{$table}{$property} = $value;
# use the update value }
my $value = $properties->{$property}; $self->{_properties}{$property} = $value;
# use the current value because the update value was undef
unless (defined $value) {
$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
if ($propertyDefinition->{serialize}) {
$setPairs{$property} = JSON->new->canonical->encode($value);
}
else {
$setPairs{$property} = $value;
}
$self->{_properties}{$property} = $value;
}
# if there's anything to update, then do so
if (scalar(keys %setPairs) > 0) {
my @values = values %setPairs;
my @columnNames = map { $_.'=?' } keys %setPairs;
push(@values, $self->getId, $self->get("revisionDate"));
$self->session->db->write("update ".$definition->{tableName}." set ".join(",",@columnNames)." where assetId=? and revisionDate=?",\@values);
} }
} }
# if there's anything to update, then do so
my $db = $self->session->db;
foreach my $table (keys %setPairs) {
my @values = values %{$setPairs{$table}};
my @columnNames = map { $_.'=?' } keys %{$setPairs{$table}};
push(@values, $self->getId, $self->get("revisionDate"));
$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

@ -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|,