Added new collateral subsystem.

This commit is contained in:
JT Smith 2003-03-16 06:17:17 +00:00
parent e229d9061c
commit bf4fe76d96
9 changed files with 298 additions and 84 deletions

View file

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

252
lib/WebGUI/Collateral.pm Normal file
View file

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

View file

@ -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 = '<a href="'.$file->getURL.'"><img src="'.$file->getIcon.'" align="middle" border="0" /> '.$data{name}.'</a>';
return $temp;
my @param = WebGUI::Macro::getParams($_[0]);
my $collateral = WebGUI::Collateral->find($param[0]);
return '<a href="'.$collateral->getURL.'"><img src="'.$collateral->getIcon.'" align="middle" border="0" /> '.$collateral->get("name").'</a>';
}
1;

View file

@ -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 = '<img src="'.$image->getURL.'" '.$data{parameters}.' />';
return $temp;
my @param = WebGUI::Macro::getParams($_[0]);
my $collateral = WebGUI::Collateral->find($param[0]);
return '<img src="'.$collateral->getURL.'" '.$collateral->get("parameters").' />';
}

View file

@ -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");
}

View file

@ -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;
}

View file

@ -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 = '<a href="'.$image->getURL.'"><img src="'.$image->getThumbnail.
my @param = WebGUI::Macro::getParams($_[0]);
my $collateral = WebGUI::Collateral->find($param[0]);
my $output = '<a href="'.$collateral->getURL.'"><img src="'.$collateral->getThumbnail.
'" border="0"></a><br><b>'.$param[0].'</b><p>';
return $output;
}

View file

@ -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;
}

View file

@ -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=>'<a href="'.WebGUI::URL::page('op=deleteFile&cid='
-value=>'<a href="'.WebGUI::URL::page('op=deleteCollateralFile&cid='
.$collateral->{collateralId}).'">'.WebGUI::International::get(391).'</a>',
-label=>WebGUI::International::get(773)
);
@ -207,7 +207,7 @@ sub www_editCollateral {
if ($canEdit) {
if ($collateral->{filename} ne "") {
$f->readOnly(
-value=>'<a href="'.WebGUI::URL::page('op=deleteFile&cid='
-value=>'<a href="'.WebGUI::URL::page('op=deleteCollateralFile&cid='
.$collateral->{collateralId}).'">'.
WebGUI::International::get(391).'</a>',
-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();
}