Added more tests. Cleaned up the OO of File and Image a bit (removed a healthy bit of unnecessary code from Image). Done with File and Image for the time being...

This commit is contained in:
Doug Bell 2007-10-09 07:36:54 +00:00
parent fad056cfa5
commit 5746ded766
12 changed files with 523 additions and 119 deletions

View file

@ -16,6 +16,7 @@ package WebGUI::Asset::File;
use strict;
use base 'WebGUI::Asset';
use Carp;
use WebGUI::Cache;
use WebGUI::Storage;
use WebGUI::SQL;
@ -58,7 +59,7 @@ sub addRevision {
my $properties = shift;
if ($self->get("storageId") ne "") {
my $newStorage = WebGUI::Storage->get($self->session,$self->get("storageId"))->copy;
my $newStorage = $self->getStorageClass->get($self->session,$self->get("storageId"))->copy;
$properties->{storageId} = $newStorage->getId;
}
@ -221,20 +222,31 @@ sub getIcon {
}
#----------------------------------------------------------------------------
=head2 getStorageClass
Get the full classname of the WebGUI::Storage we should use for this asset.
=cut
sub getStorageClass {
return 'WebGUI::Storage';
}
#-------------------------------------------------------------------
=head2 getStorageFromPost
We have to wrap this operation because WebGUI::Asset::File::Image calls SUPER processPropertiesFormFormPost,
which gives it the wrong type of Storage object.
Get the storage location created by the form post.
=cut
sub getStorageFromPost {
my $self = shift;
my $self = shift;
my $storageId = shift;
my $fileStorageId = WebGUI::Form::File->new($self->session, {name => 'newFile', value=>$storageId })->getValueFromPost;
return WebGUI::Storage->get($self->session, $fileStorageId);
return $self->getStorageClass->get($self->session, $fileStorageId);
}
@ -300,23 +312,23 @@ sub processPropertiesFromFormPost {
$storageLocation->clear();
}
#Pass in the storage Id to prevent another one from being created.
# Pass in the storage Id to prevent another one from being created.
my $storage = $self->getStorageFromPost($storageId);
if (defined $storage) {
my $filename = $storage->getFiles()->[0];
if (defined $storage) {
my $filename = $storage->getFiles()->[0];
if (defined $filename) {
my %data;
$data{filename} = $filename;
$data{storageId} = $storage->getId;
$data{title} = $filename unless ($session->form->process("title"));
$data{menuTitle} = $filename unless ($session->form->process("menuTitle"));
$data{url} = $self->getParent->get('url').'/'.$filename unless ($session->form->process("url"));
if (defined $filename) {
my %data;
$data{filename} = $filename;
$data{storageId} = $storage->getId;
$data{title} = $filename unless ($session->form->process("title"));
$data{menuTitle} = $filename unless ($session->form->process("menuTitle"));
$data{url} = $self->getParent->get('url').'/'.$filename unless ($session->form->process("url"));
$self->setStorageLocation($storage);
$self->update(\%data);
}
}
$self->update(\%data);
}
}
$self->applyConstraints;
}
@ -327,7 +339,7 @@ sub purge {
my $self = shift;
my $sth = $self->session->db->read("select storageId from FileAsset where assetId=".$self->session->db->quote($self->getId));
while (my ($storageId) = $sth->array) {
WebGUI::Storage->get($self->session,$storageId)->delete;
$self->getStorageClass->get($self->session,$storageId)->delete;
}
$sth->finish;
return $self->SUPER::purge;
@ -355,6 +367,30 @@ sub purgeRevision {
return $self->SUPER::purgeRevision;
}
#----------------------------------------------------------------------------
=head2 setFile ( filename )
Set the file being handled by this storage location with a file from the
system.
=cut
sub setFile {
my $self = shift;
my $filename = shift;
my $storage = $self->getStorageLocation;
# Clear the old file if any
$storage->clear;
$storage->addFileFromFilesystem($filename)
|| croak "Couldn't setFile: " . join(", ",@{ $storage->getErrors });
# NOTE: We should not croak here, the WebGUI::Storage should croak for us.
$self->updatePropertiesFromStorage;
}
#-------------------------------------------------------------------
sub setSize {
my $self = shift;
@ -377,11 +413,11 @@ sub setStorageLocation {
$self->{_storageLocation} = $storage;
}
elsif ($self->get("storageId") eq "") {
$self->{_storageLocation} = WebGUI::Storage->create($self->session);
$self->{_storageLocation} = $self->getStorageClass->create($self->session);
$self->update({storageId=>$self->{_storageLocation}->getId});
}
else {
$self->{_storageLocation} = WebGUI::Storage->get($self->session,$self->get("storageId"));
$self->{_storageLocation} = $self->getStorageClass->get($self->session,$self->get("storageId"));
}
}
@ -411,6 +447,25 @@ sub update {
}
}
#----------------------------------------------------------------------------
=head2 updatePropertiesFromStorage ( )
Updates the asset properties from the file tracked by this asset. Should be
called every time the file is changed to ensure the correct filename is
in the asset properties.
=cut
sub updatePropertiesFromStorage {
my $self = shift;
my $storage = $self->getStorageLocation;
my $filename = $storage->getFiles->[0];
$self->update({
filename => $filename,
});
}
#-------------------------------------------------------------------
sub view {
my $self = shift;

View file

@ -15,16 +15,13 @@ package WebGUI::Asset::File::Image;
=cut
use strict;
use WebGUI::Asset::File;
use base 'WebGUI::Asset::File';
use WebGUI::Storage::Image;
use WebGUI::HTMLForm;
use WebGUI::Utility;
our @ISA = qw(WebGUI::Asset::File);
=head1 NAME
Package WebGUI::Asset::File::Image
@ -106,27 +103,27 @@ A hash reference passed in from a subclass definition.
=cut
sub definition {
my $class = shift;
my $session = shift;
my $definition = shift;
my $i18n = WebGUI::International->new($session,"Asset_Image");
push(@{$definition}, {
assetName=>$i18n->get('assetName'),
tableName=>'ImageAsset',
className=>'WebGUI::Asset::File::Image',
icon=>'image.gif',
properties=>{
thumbnailSize=>{
fieldType=>'integer',
defaultValue=>$session->setting->get("thumbnailSize")
},
parameters=>{
fieldType=>'textarea',
defaultValue=>'style="border-style:none;"'
}
}
});
return $class->SUPER::definition($session,$definition);
my $class = shift;
my $session = shift;
my $definition = shift;
my $i18n = WebGUI::International->new($session,"Asset_Image");
push @{$definition}, {
assetName => $i18n->get('assetName'),
tableName => 'ImageAsset',
className => 'WebGUI::Asset::File::Image',
icon => 'image.gif',
properties => {
thumbnailSize => {
fieldType => 'integer',
defaultValue => $session->setting->get("thumbnailSize"),
},
parameters => {
fieldType => 'textarea',
defaultValue => 'style="border-style:none;"',
},
},
};
return $class->SUPER::definition($session,$definition);
}
@ -192,37 +189,16 @@ sub getEditForm {
return $tabform;
}
#----------------------------------------------------------------------------
=head2 getStorageClass
#-------------------------------------------------------------------
=head2 getStorageFromPost
Sub class this method from WebGUI::Asset::File so the storage object is the correct type.
Returns the class name of the WebGUI::Storage we should use for this asset.
=cut
sub getStorageFromPost {
my $self = shift;
my $storageId = shift;
my $fileStorageId = WebGUI::Form::Image->new($self->session, {name => 'newFile', value=>$storageId })->getValueFromPost;
return WebGUI::Storage::Image->get($self->session, $fileStorageId);
}
#-------------------------------------------------------------------
sub getStorageLocation {
my $self = shift;
unless (exists $self->{_storageLocation}) {
if ($self->get("storageId") eq "") {
$self->{_storageLocation} = WebGUI::Storage::Image->create($self->session);
$self->update({storageId=>$self->{_storageLocation}->getId});
} else {
$self->{_storageLocation} = WebGUI::Storage::Image->get($self->session,$self->get("storageId"));
}
}
return $self->{_storageLocation};
sub getStorageClass {
return 'WebGUI::Storage::Image';
}
#-------------------------------------------------------------------
@ -261,31 +237,6 @@ sub prepareView {
$self->{_viewTemplate} = $template;
}
#-------------------------------------------------------------------
sub processPropertiesFromFormPost {
my $self = shift;
$self->SUPER::processPropertiesFromFormPost;
$self->applyConstraints;
}
#-------------------------------------------------------------------
sub setStorageLocation {
my $self = shift;
my $storage = shift;
if (defined $storage) {
$self->{_storageLocation} = $storage;
}
elsif ($self->get("storageId") eq "") {
$self->{_storageLocation} = WebGUI::Storage::Image->create($self->session);
$self->update({storageId=>$self->{_storageLocation}->getId});
}
else {
$self->{_storageLocation} = WebGUI::Storage::Image->get($self->session,$self->get("storageId"));
}
}
#-------------------------------------------------------------------
sub view {
my $self = shift;
@ -305,6 +256,20 @@ sub view {
return $out;
}
#----------------------------------------------------------------------------
=head2 setFile ( filename )
Extend the superclass setFile to automatically generate thumbnails.
=cut
sub setFile {
my $self = shift;
$self->SUPER::setFile(@_);
$self->generateThumbnail;
}
#-------------------------------------------------------------------
sub www_edit {
my $self = shift;

View file

@ -15,6 +15,7 @@ package WebGUI::Asset;
=cut
use strict;
use WebGUI::Asset::Shortcut;
=head1 NAME