webgui/lib/WebGUI/Definition/Asset.pm

130 lines
4.1 KiB
Perl

package WebGUI::Definition::Asset;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 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
-------------------------------------------------------------------
=cut
use strict;
use warnings;
use 5.010;
use base qw(WebGUI::Definition);
use WebGUI::International;
use WebGUI::Exception;
our $VERSION = '0.0.1';
#-------------------------------------------------------------------
sub import {
my $class = shift;
if (! @_) {
return;
}
my $definition = (@_ == 1 && ref $_[0]) ? $_[0] : { @_ };
my $table = $definition->{tableName}
|| WebGUI::Error::InvalidParam->throw(param => 'tableName');
if ( my $properties = $definition->{properties} ) {
for ( my $i = 0; $i < $#{ $properties }; $i += 2) {
my ($name, $value) = @{ $properties }[$i, $i + 1];
$value->{tableName} ||= $table;
if ( ! $value->{tableName}|| ref $value->{tableName}) {
WebGUI::Error::InvalidParam->throw(param => 'tableName');
}
elsif ( ! $value->{fieldType} || ref $value->{fieldType}) {
WebGUI::Error::InvalidParam->throw(param => 'fieldType');
}
elsif ( ( ! $value->{noFormPost} || ref $value->{noFormPost} ) && ! $value->{label}) {
WebGUI::Error::InvalidParam->throw(param => 'label');
}
}
}
$class->_install($super, 'getTables', $class->_gen_getTables());
# WebGUI::Definition->import uses caller, so avoid the extra entry in the call stack
my $next = $class->next::can;
@_ = ($class, $definition);
goto $next;
}
#-------------------------------------------------------------------
sub _gen_getTables {
my $class = shift;
return sub {
my $self = shift;
my %tables;
foreach my $property ($self->getProperties) {
my $definition = $self->getProperty($property);
%tables{$definition->{tableName}} = 1;
}
return keys %tables;
};
}
#-------------------------------------------------------------------
sub _gen_getProperty {
my $class = shift;
my $superGetProperty = $class->next::method(@_);
return sub {
my $self = shift;
my $property = $self->$superGetProperty(@_);
for my $element (qw(label hoverHelp)) {
if ($property->{$element} && ref $property->{$element} eq 'ARRAY') {
$property->{$element}
= WebGUI::International->new($self->session)->get(@{$property->{$element}});
}
}
return $property;
};
}
1;
__END__
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 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
-------------------------------------------------------------------
=head1 NAME
WebGUI::Definition::Asset
=head1 DESCRIPTION
Extends WebGUI::Definition with asset specific methods and convienences. It automatically inserts the tableName attribute as an element of each property if the property doesn't explicitly set it.
=head1 METHODS
The following methods are exposed through this class.
=head2 getProperty ( property )
Extends getProperty() method generated by WebGUI::Definition by automatically converting the label and hoverHelp elements into strings.
=head2 getTables ( )
Returns a list of the tables that this asset's properties are stored in.
=cut