diff --git a/lib/WebGUI/Attachment.pm b/lib/WebGUI/Attachment.pm index 010f6e5da..beaa2e291 100644 --- a/lib/WebGUI/Attachment.pm +++ b/lib/WebGUI/Attachment.pm @@ -40,6 +40,8 @@ Package WebGUI::Attachment Package to manipulate WebGUI Attachments. +NOTE: It is typically not recommended to use this package directly, especially when building wobjects, unless you have special needs. The attachments system has been superceded by the collateral management system. See WebGUI::Collateral for details. + =head1 SYNOPSIS use WebGUI::Attachment; diff --git a/lib/WebGUI/Collateral.pm b/lib/WebGUI/Collateral.pm new file mode 100644 index 000000000..4d8832067 --- /dev/null +++ b/lib/WebGUI/Collateral.pm @@ -0,0 +1,252 @@ +package WebGUI::Collateral; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2003 Plain Black LLC. + ------------------------------------------------------------------- + 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 WebGUI::Attachment; +use WebGUI::DateTime; +use WebGUI::Session; +use WebGUI::SQL; +use WebGUI::Utility; + +our @ISA = qw(WebGUI::Attachment); + +=head1 NAME + +Package WebGUI::Collateral + +=head1 DESCRIPTION + +Package to manipulate items in WebGUI's collateral manager. + +=head1 SYNOPSIS + + use WebGUI::Collateral; + + $collateral = WebGUI::Collateral->new(1234); + + $collateral = WebGUI::Collateral->find("My Snippet"); + + $collateral->delete; + $collateral->deleteFile; + $collateral->get("parameters"); + $collateral->set(\%hash); + +=head1 SEE ALSO + +This package is derived from WebGUI::Attachment. See that package for documentation of its methods. + +=head1 METHODS + +These methods are available from this class: + +=cut + +#------------------------------------------------------------------- +# extended only to save info to database +sub createThumbnail { + $_[0]->SUPER::createThumbnail(@_); + if ($_[1] != $_[0]->get("thumbnailSize")) { + $_[0]->set({thumbnailSize=>$_[1]}); + } +} + + +#------------------------------------------------------------------- + +=head2 delete ( ) + +Delete's this collateral item. + +=cut + +sub delete { + $_[0]->deleteNode; + WebGUI::SQL->write("delete from collateral where collateralId=".$_[0]->get("collateralId")); +} + + +#------------------------------------------------------------------- + +=head2 deleteFile ( ) + +Deletes the file attached to this collateral item. + +=cut + +sub deleteFile { + $_[0]->SUPER::delete; + WebGUI::SQL->write("update collateral set filename='' where collateralId=".$_[0]->get("collateralId")); + $_[0]->{_properties}{filename}=''; +} + + +#------------------------------------------------------------------- + +=head2 find ( name ) + +An alternative to the constructor "new", use find as a constructor by name rather than id. + +=over + +=item name + +The name of the collateral item you wish to instanciate. + +=back + +=cut + +sub find { + my ($collateralId) = WebGUI::SQL->quickArray("select collateralId from collateral where name=".quote($_[1])); + return WebGUI::Collateral->new($collateralId); +} + +#------------------------------------------------------------------- + +=head2 get ( [ propertyName ] ) + +Returns a hash reference containing all of the properties of this collateral item. + +=over + +=item propertyName + +If an individual propertyName is specified, then only that property value is returned as a scalar. + +=back + +=cut + +sub get { + if ($_[1] ne "") { + return $_[0]->{_properties}{$_[1]}; + } else { + return $_[0]->{_properties}; + } +} + + +#------------------------------------------------------------------- + +=head2 new ( collateralId ) + +Constructor. + +=over + +=item collateralId + +The unique identifier for this piece of collateral. If set to "new" an id will be generated. + +=back + +=cut + +sub new { + my ($class, $collateralId) = @_; + my $properties; + if ($collateralId eq "new") { + $properties = { + collateralId=>getNextId("collateralId"), + collateralFolderId=>0, + collateralType=>"image", + userId=>$session{user}{userId}, + dateUploaded=>time(), + thumbnailSize=>$session{setting}{thumbnailSize}, + name=>"untitled", + username=>$session{user}{username} + }; + WebGUI::SQL->write("insert into collateral (collateralId, collateralFolderId, collateraltype, userId, + dateUploaded, thumbnailSize, name, username) values ( ".$properties->{collateralId}.", + ".$properties->{collateralFolderId}.", ".quote($properties->{collateralType}).", + ".$properties->{userId}.", ".$properties->{dateUploaded}.", ".$properties->{thumbnailSize}.", + ".quote($properties->{name}).", ".quote($properties->{username}).")"); + } else { + $properties = WebGUI::SQL->quickHashRef("select * from collateral where collateralId=".$collateralId); + } + my $self = WebGUI::Attachment->new($properties->{filename},"images",$properties->{collateralId}); + $self->{_properties} = $properties; + bless $self, $class; +} + + +#------------------------------------------------------------------- + +=head2 set ( properties ) + +Sets the value of a property for this collateral item. + +=over + +=item properties + +A hash reference containing the list of properties to set. The valid property names are "name", "parameters", "userId", "username", "collateralFolderId", "collateralType", and "thumbnailSize". + +If username or userId are not specified, the current user will be used. + +=back + +=cut + +sub set { + my ($key, $sql, @update, $i); + my $self = shift; + my $properties = shift; + $self->{_properties}->{dateUploaded} = time(); + $properties->{userId} = $session{user}{userId} if ($properties->{userId} eq ""); + $properties->{username} = $session{user}{username} if ($properties->{username} eq ""); + $properties->{thumbnailSize} = $session{setting}{thumbnailSize} if ($properties->{thumbnailSize} eq ""); + $sql = "update collateral set"; + foreach $key (keys %{$properties}) { + $self->{_property}{$key} = $properties->{$key}; + if (isIn($key, qw(name parameters userId username collateralFolderId collateralType thumbnailSize))) { + $sql .= " ".$key."=".quote($properties->{$key}).","; + } + } + $sql .= " dateUploaded=".$self->{_properties}{dateUploaded}." + where collateralid=".$self->get("collateralId"); + WebGUI::SQL->write($sql); +} + + +#------------------------------------------------------------------- +# extended only to save info to database +sub save { + my $filename = $_[0]->SUPER::save($_[1],$_[2],$_[3]); + if ($filename) { + WebGUI::SQL->write("update collateral set filename=".quote($filename) + ." where collateralId=".$_[0]->get("collateralId")); + $_[0]->{_properties}{filename} = $filename; + } + return $filename; +} + +#------------------------------------------------------------------- +# extended only to save info to database +sub saveFromFilesystem { + my $filename = $_[0]->SUPER::saveFromFilesystem($_[1],$_[2],$_[3]); + if ($filename) { + WebGUI::SQL->write("update collateral set filename=".quote($filename) + ." where collateralId=".$_[0]->get("collateralId")); + $_[0]->{_properties}{filename} = $filename; + } + return $filename; +} + + + +1; + + diff --git a/lib/WebGUI/Macro/File.pm b/lib/WebGUI/Macro/File.pm index 288c09a8a..b58c7dc9d 100644 --- a/lib/WebGUI/Macro/File.pm +++ b/lib/WebGUI/Macro/File.pm @@ -11,21 +11,15 @@ package WebGUI::Macro::File; #------------------------------------------------------------------- use strict; -use Tie::CPHash; -use WebGUI::Attachment; +use WebGUI::Collateral; use WebGUI::Macro; use WebGUI::Session; -use WebGUI::SQL; #------------------------------------------------------------------- sub process { - my (@param, $temp, %data, $file); - tie %data, 'Tie::CPHash'; - @param = WebGUI::Macro::getParams($_[0]); - %data = WebGUI::SQL->quickHash("select collateralId,filename,name from collateral where name=".quote($param[0])); - $file = WebGUI::Attachment->new($data{filename},"images",$data{collateralId}); - $temp = ' '.$data{name}.''; - return $temp; + my @param = WebGUI::Macro::getParams($_[0]); + my $collateral = WebGUI::Collateral->find($param[0]); + return ' '.$collateral->get("name").''; } 1; diff --git a/lib/WebGUI/Macro/I_imageWithTags.pm b/lib/WebGUI/Macro/I_imageWithTags.pm index c77918b7a..9a348ac8c 100644 --- a/lib/WebGUI/Macro/I_imageWithTags.pm +++ b/lib/WebGUI/Macro/I_imageWithTags.pm @@ -11,21 +11,15 @@ package WebGUI::Macro::I_imageWithTags; #------------------------------------------------------------------- use strict; -use Tie::CPHash; -use WebGUI::Attachment; +use WebGUI::Collateral; use WebGUI::Macro; use WebGUI::Session; -use WebGUI::SQL; #------------------------------------------------------------------- sub process { - my (@param, $temp, %data, $image); - tie %data, 'Tie::CPHash'; - @param = WebGUI::Macro::getParams($_[0]); - %data = WebGUI::SQL->quickHash("select filename,parameters,collateralId from collateral where name=".quote($param[0])); - $image = WebGUI::Attachment->new($data{filename},"images",$data{collateralId}); - $temp = ''; - return $temp; + my @param = WebGUI::Macro::getParams($_[0]); + my $collateral = WebGUI::Collateral->find($param[0]); + return 'get("parameters").' />'; } diff --git a/lib/WebGUI/Macro/Snippet.pm b/lib/WebGUI/Macro/Snippet.pm index 5f4d7ffbe..784e179ae 100644 --- a/lib/WebGUI/Macro/Snippet.pm +++ b/lib/WebGUI/Macro/Snippet.pm @@ -12,16 +12,16 @@ package WebGUI::Macro::Snippet; use strict; use Tie::CPHash; +use WebGUI::Collateral; use WebGUI::Macro; use WebGUI::Session; -use WebGUI::SQL; #------------------------------------------------------------------- sub process { my (@param, $temp); @param = WebGUI::Macro::getParams($_[0]); - ($temp) = WebGUI::SQL->quickArray("select parameters from collateral where name=".quote($param[0])); - return $temp; + my $collateral = WebGUI::Collateral->find($param[0]); + return $collateral->get("parameters"); } diff --git a/lib/WebGUI/Macro/Thumbnail.pm b/lib/WebGUI/Macro/Thumbnail.pm index 9090cf227..2d3f14b99 100644 --- a/lib/WebGUI/Macro/Thumbnail.pm +++ b/lib/WebGUI/Macro/Thumbnail.pm @@ -11,20 +11,15 @@ package WebGUI::Macro::Thumbnail; #------------------------------------------------------------------- use strict; -use Tie::CPHash; -use WebGUI::Attachment; +use WebGUI::Collateral; use WebGUI::Macro; use WebGUI::Session; -use WebGUI::SQL; #------------------------------------------------------------------- sub process { - my (@param, %data, $image); - tie %data, 'Tie::CPHash'; - @param = WebGUI::Macro::getParams($_[0]); - %data = WebGUI::SQL->quickHash("select filename,collateralId from collateral where name='$param[0]'"); - $image = WebGUI::Attachment->new($data{filename},"images",$data{collateralId}); - return $image->getThumbnail; + my @param = WebGUI::Macro::getParams($_[0]); + my $collateral = WebGUI::Collateral->find($param[0]); + return $collateral->getThumbnail; } diff --git a/lib/WebGUI/Macro/ThumbnailLinker.pm b/lib/WebGUI/Macro/ThumbnailLinker.pm index 016cf30ad..09e60e35d 100644 --- a/lib/WebGUI/Macro/ThumbnailLinker.pm +++ b/lib/WebGUI/Macro/ThumbnailLinker.pm @@ -11,20 +11,15 @@ package WebGUI::Macro::ThumbnailLinker; #------------------------------------------------------------------- use strict; -use Tie::CPHash; -use WebGUI::Attachment; +use WebGUI::Collateral; use WebGUI::Macro; use WebGUI::Session; -use WebGUI::SQL; #------------------------------------------------------------------- sub process { - my (@param, %data, $image, $output); - tie %data, 'Tie::CPHash'; - @param = WebGUI::Macro::getParams($_[0]); - %data = WebGUI::SQL->quickHash("select filename,collateralId from collateral where name='$param[0]'"); - $image = WebGUI::Attachment->new($data{filename},"images",$data{collateralId}); - $output = 'getURL.'">
'.$param[0].'

'; return $output; } diff --git a/lib/WebGUI/Macro/i_imageNoTags.pm b/lib/WebGUI/Macro/i_imageNoTags.pm index c43875a13..05a5dd752 100644 --- a/lib/WebGUI/Macro/i_imageNoTags.pm +++ b/lib/WebGUI/Macro/i_imageNoTags.pm @@ -11,20 +11,15 @@ package WebGUI::Macro::i_imageNoTags; #------------------------------------------------------------------- use strict; -use Tie::CPHash; -use WebGUI::Attachment; +use WebGUI::Collateral; use WebGUI::Macro; use WebGUI::Session; -use WebGUI::SQL; #------------------------------------------------------------------- sub process { - my (@param, $image, %data); - tie %data, 'Tie::CPHash'; - @param = WebGUI::Macro::getParams($_[0]); - %data = WebGUI::SQL->quickHash("select collateralId,filename from collateral where name=".quote($param[0])); - $image = WebGUI::Attachment->new($data{filename},"images",$data{collateralId}); - return $image->getURL; + my @param = WebGUI::Macro::getParams($_[0]); + my $collateral = WebGUI::Collateral->find($param[0]); + return $collateral->getURL; } diff --git a/lib/WebGUI/Operation/Collateral.pm b/lib/WebGUI/Operation/Collateral.pm index ee4ed5dc0..c368faa67 100644 --- a/lib/WebGUI/Operation/Collateral.pm +++ b/lib/WebGUI/Operation/Collateral.pm @@ -19,12 +19,11 @@ eval " use Image::Magick; "; $hasImageMagick=0 if $@; use Exporter; use strict; -use WebGUI::Attachment; +use WebGUI::Collateral; use WebGUI::DateTime; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; -use WebGUI::Node; use WebGUI::Operation::Shared; use WebGUI::Paginator; use WebGUI::Privilege; @@ -37,7 +36,7 @@ use WebGUI::HTML; our @ISA = qw(Exporter); our @EXPORT = qw(&www_editCollateral &www_editCollateralSave &www_deleteCollateral &www_deleteCollateralConfirm &www_listCollateral - &www_deleteFile &www_editCollateralFolder &www_editCollateralFolderSave &www_deleteCollateralFolder + &www_deleteCollateralFile &www_editCollateralFolder &www_editCollateralFolderSave &www_deleteCollateralFolder &www_deleteCollateralFolderConfirm); #------------------------------------------------------------------- @@ -74,9 +73,8 @@ sub www_deleteCollateral { #------------------------------------------------------------------- sub www_deleteCollateralConfirm { return WebGUI::Privilege::insufficient unless (WebGUI::Privilege::isInGroup(4)); - my $node = WebGUI::Node->new("images",$session{form}{cid}); - $node->delete; - WebGUI::SQL->write("delete from collateral where collateralId=".$session{form}{cid}); + my $collateral = WebGUI::Collateral->new($session{form}{cid}); + $collateral->delete; return www_listCollateral(); } @@ -108,10 +106,11 @@ sub www_deleteCollateralFolderConfirm { } #------------------------------------------------------------------- -sub www_deleteFile { +sub www_deleteCollateralFile { return WebGUI::Privilege::insufficient unless (WebGUI::Privilege::isInGroup(4)); - WebGUI::SQL->write("update collateral set filename='' where collateralId=".$session{form}{cid}); - return www_editCollateral(); + my $collateral = WebGUI::Collateral->new($session{form}{cid}); + $collateral->delete; + return www_editCollateral($collateral); } #------------------------------------------------------------------- @@ -126,7 +125,8 @@ sub www_editCollateral { $collateral->{parameters} = 'border="0"' if ($session{form}{type} eq "image"); $collateral->{thumbnailSize} = $session{setting}{thumbnailSize}; } else { - $collateral = WebGUI::SQL->quickHashRef("select * from collateral where collateralId=".$session{form}{cid}); + my $c = $_[1] || WebGUI::Collateral->new($session{form}{cid}); + $collateral = $c->get; } $canEdit = ($collateral->{userId} == $session{user}{userId} || WebGUI::Privilege::isInGroup(3)); $folderId = $session{scratch}{collateralFolderId} || 0; @@ -183,7 +183,7 @@ sub www_editCollateral { if ($canEdit) { if ($collateral->{filename} ne "") { $f->readOnly( - -value=>'{collateralId}).'">'.WebGUI::International::get(391).'', -label=>WebGUI::International::get(773) ); @@ -207,7 +207,7 @@ sub www_editCollateral { if ($canEdit) { if ($collateral->{filename} ne "") { $f->readOnly( - -value=>'{collateralId}).'">'. WebGUI::International::get(391).'', -label=>WebGUI::International::get(384) @@ -264,38 +264,25 @@ sub www_editCollateral { sub www_editCollateralSave { return WebGUI::Privilege::insufficient unless (WebGUI::Privilege::isInGroup(4)); WebGUI::Session::setScratch("collateralFolderId",$session{form}{collateralFolderId}); - my ($test, $file, $addFile, $thumbnailSize, $collateral); - $collateral = WebGUI::SQL->quickHashRef("select * from collateral where collateralId=".$session{form}{cid}) unless ($session{form}{cid} eq "new"); - $thumbnailSize = $session{form}{thumbnailSize} || $session{setting}{thumbnailSize}; - + my ($test, $file, $addFile); + my $collateral = WebGUI::Collateral->new($session{form}{cid}); + $session{form}{thumbnailSize} ||= $session{setting}{thumbnailSize}; if ($session{form}{cid} eq "new") { - $session{form}{cid} = getNextId("collateralId"); - WebGUI::SQL->write("insert into collateral (collateralId,userId,username,collateralType) - values ($session{form}{cid}, - $session{user}{userId}, ".quote($session{user}{username}).", - ".quote($session{form}{collateralType}).")"); - } elsif ($collateral->{thumbnailSize} != $thumbnailSize) { - $file = WebGUI::Attachment->new($collateral->{filename},"images", $session{form}{cid}); - $file->createThumbnail($thumbnailSize); - } - $file = WebGUI::Attachment->new("","images",$session{form}{cid}); - $file->save("filename", $thumbnailSize); - if ($file->getFilename ne "") { - $addFile = ", filename=".quote($file->getFilename); - $session{form}{name} = $file->getFilename if ($session{form}{name} eq ""); + $session{form}{cid} = $collateral->get("collateralId"); + } elsif ($collateral->get("thumbnailSize") != $session{form}{thumbnailSize}) { + $collateral->createThumbnail($session{form}{thumbnailSize}); } + $collateral->save("filename", $session{form}{thumbnailSize}); $session{form}{name} = "untitled" if ($session{form}{name} eq ""); while (($test) = WebGUI::SQL->quickArray("select name from collateral - where name=".quote($session{form}{name})." and collateralId<>$session{form}{cid}")) { + where name=".quote($session{form}{name})." and collateralId<>".$collateral->get("collateralId"))) { if ($session{form}{name} =~ /(.*)(\d+$)/) { $session{form}{name} = $1.($2+1); } elsif ($test ne "") { $session{form}{name} .= "2"; } } - WebGUI::SQL->write("update collateral set thumbnailSize=$thumbnailSize, name=".quote($session{form}{name}).", parameters=" - .quote($session{form}{parameters}).", collateralFolderId=$session{form}{collateralFolderId}, dateUploaded=" - .time()." $addFile where collateralId=$session{form}{cid}"); + $collateral->set($session{form}); $session{form}{collateralType} = ""; return www_listCollateral(); }