made the file and image form fields better, but still have some work to do

began converting the article to have it's own attachments, but still have some work to do
This commit is contained in:
JT Smith 2006-04-16 01:10:34 +00:00
parent cedef1c8ac
commit 7fcfb11f20
10 changed files with 248 additions and 76 deletions

View file

@ -46,9 +46,16 @@ addAdManager();
updateMatrix(); updateMatrix();
updateFolder(); updateFolder();
addRichEditUpload(); addRichEditUpload();
updateArticle();
finish($session); # this line required finish($session); # this line required
#-------------------------------------------------
sub updateArticle {
print "\tAllowing articles to have direct attachments.\n";
$session->db->write("alter table Article add column storageId varchar(22) binary");
}
#------------------------------------------------- #-------------------------------------------------
sub addRichEditUpload { sub addRichEditUpload {
print "\tAdding the ability to upload from the rich editor.\n"; print "\tAdding the ability to upload from the rich editor.\n";

View file

@ -147,8 +147,7 @@ sub definition {
className=>'WebGUI::Asset::Post', className=>'WebGUI::Asset::Post',
properties=>{ properties=>{
storageId => { storageId => {
noFormPost=>1, fieldType=>"file",
fieldType=>"hidden",
defaultValue=>undef defaultValue=>undef
}, },
threadId => { threadId => {
@ -504,30 +503,6 @@ sub getThumbnailUrl {
} }
#-------------------------------------------------------------------
sub getUploadControl {
my $self = shift;
my $maxAttachments = $self->getThread->getParent->getValue("attachmentsPerPost");
my $uploadControl;
return undef unless ($maxAttachments);
my $i18n = WebGUI::International->new($self->session);
if ($self->get("storageId")) {
my $i;
foreach my $filename (@{$self->getStorageLocation->getFiles}) {
$uploadControl .= $self->session->icon->delete("func=deleteFile;filename=".$filename,$self->get("url"),$i18n->get("delete file warning","Asset_Collaboration"))
.' <a href="'.$self->getStorageLocation->getUrl($filename).'">'.$filename.'</a>'
.'<br />';
$i++;
}
return $uploadControl unless ($i < $maxAttachments);
}
$uploadControl .= WebGUI::Form::file($self->session,
maxAttachments=>$maxAttachments
);
return $uploadControl;
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 hasRated ( ) =head2 hasRated ( )
@ -1065,7 +1040,11 @@ sub www_edit {
$var{'form.preview'} = WebGUI::Form::submit($self->session, { $var{'form.preview'} = WebGUI::Form::submit($self->session, {
value=>$i18n->get("preview","Asset_Collaboration") value=>$i18n->get("preview","Asset_Collaboration")
}); });
$var{'attachment.form'} = $self->getUploadControl; $var{'attachment.form'} = WebGUI::Form::file($self->session, {
value=>$self->get("storageId"),
maxAttachments=>$self->getThread->getParent->getValue("attachmentsPerPost"),
deleteFileUrl=>$self->getUrl("func=deleteFile;filename=")
});
$var{'contentType.form'} = WebGUI::Form::contentType($self->session, { $var{'contentType.form'} = WebGUI::Form::contentType($self->session, {
name=>'contentType', name=>'contentType',
value=>$self->getValue("contentType") || "mixed" value=>$self->getValue("contentType") || "mixed"

View file

@ -16,6 +16,7 @@ use WebGUI::International;
use WebGUI::Cache; use WebGUI::Cache;
use WebGUI::Paginator; use WebGUI::Paginator;
use WebGUI::Asset::Wobject; use WebGUI::Asset::Wobject;
use WebGUI::Storage::Image;
our @ISA = qw(WebGUI::Asset::Wobject); our @ISA = qw(WebGUI::Asset::Wobject);
@ -114,6 +115,15 @@ sub definition {
subtext=>' &nbsp; <span style="font-size: 8pt;">'.$i18n->get(11).'</span>', subtext=>' &nbsp; <span style="font-size: 8pt;">'.$i18n->get(11).'</span>',
hoverHelp=>$i18n->get('carriage return description'), hoverHelp=>$i18n->get('carriage return description'),
uiLevel=>5 uiLevel=>5
},
storageId=>{
tab=>"properties",
fieldType=>"image",
deleteFileUrl=>$session->url->page("func=deleteFile;filename="),
maxAttachments=>25,
defaultValue=>undef,
label=>$i18n->get("attachments"),
hoverHelp=>$i18n->get("attachments help")
} }
); );
push(@{$definition}, { push(@{$definition}, {
@ -127,6 +137,32 @@ sub definition {
return $class->SUPER::definition($session, $definition); return $class->SUPER::definition($session, $definition);
} }
#-------------------------------------------------------------------
sub duplicate {
my $self = shift;
my $newAsset = $self->SUPER::duplicate(shift);
my $newStorage = $self->getStorageLocation->copy;
$newAsset->update({storageId=>$newStorage->getId});
return $newAsset;
}
#-------------------------------------------------------------------
=head2 exportAssetData() ( )
See WebGUI::AssetPackage::exportAssetData() for details.
=cut
sub exportAssetData {
my $self = shift;
my $data = $self->SUPER::exportAssetData;
push(@{$data->{storage}}, $self->get("storageId")) if ($self->get("storageId") ne "");
return $data;
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 prepareView ( ) =head2 prepareView ( )
@ -178,15 +214,24 @@ sub view {
return $out if $out; return $out if $out;
} }
my %var; my %var;
my $children = $self->getLineage(["children"],{returnObjects=>1,includeOnlyClasses=>["WebGUI::Asset::File","WebGUI::Asset::File::Image"]}); if ($self->get("storageId")) {
foreach my $child (@{$children}) { my $storage = WebGUI::Storage::Image->get($self->session, $self->get("storageId"));
if (ref $child eq "WebGUI::Asset::File") { foreach my $file (@{$storage->getFiles}) {
$var{"attachment.icon"} = $child->getFileIconUrl; if ($storage->isImage($file)) {
$var{"attachment.url"} = $child->getFileUrl; $var{'image.url'} = $storage->getUrl($file);
$var{"attachment.name"} = $child->get("filename"); $var{'image.thumbnail'} = $storage->getThumbnailUrl($file);
} elsif (ref $child eq "WebGUI::Asset::File::Image") { } else {
$var{"image.url"} = $child->getFileUrl; $var{'attachment.icon'} = $storage->getFileIconUrl($file);
$var{"image.thumbnail"} = $child->getThumbnailUrl; $var{'attachment.url'} = $storage->getUrl($file);
$var{'attachment.name'} = $file;
}
push(@{$var{attachment_loop}}, {
filename => $file,
isImage => $storage->isImage($file),
url=> $storage->getUrl($file),
thumbnailUrl => $storage->getUrl($file),
iconUrl => $storage->getFileIconUrl($file)
});
} }
} }
$var{description} = $self->get("description"); $var{description} = $self->get("description");
@ -235,5 +280,25 @@ sub view {
return $out; return $out;
} }
#-------------------------------------------------------------------
=head2 www_deleteFile ( )
Deletes and attached file.
=cut
sub www_deleteFile {
my $self = shift;
return $self->session->privilege->insufficient unless $self->canEdit;
if ($self->get("storageId") ne "") {
my $storage = WebGUI::Storage::Image->get($self->session, $self->get("storageId"));
$storage->deleteFile($self->session->form->param("filename"));
}
return $self->www_edit;
}
1; 1;

View file

@ -60,6 +60,10 @@ only supports displaying/deleting 1 attachment.
Flag that tells the User Profile system that this is a valid form element in a User Profile Flag that tells the User Profile system that this is a valid form element in a User Profile
=head4 deleteFileUrl
A url that will get a filename appended to it and then links to delete the files will be generated automatically.
=cut =cut
sub definition { sub definition {
@ -80,6 +84,9 @@ sub definition {
profileEnabled=>{ profileEnabled=>{
defaultValue=>1 defaultValue=>1
}, },
deleteFileUrl=>{
defaultValue=>undef
}
}); });
return $class->SUPER::definition($session, $definition); return $class->SUPER::definition($session, $definition);
} }
@ -125,7 +132,7 @@ sub displayValue {
return '' unless $self->get("value"); return '' unless $self->get("value");
my $location = WebGUI::Storage->get($self->session,$self->get("value")); my $location = WebGUI::Storage->get($self->session,$self->get("value"));
my $file = shift @{ $location->getFiles }; my $file = shift @{ $location->getFiles };
my $fileValue = sprintf qq!<img src="%s" />&nbsp;%s!, $location->getFileIconUrl($file), $file; my $fileValue = sprintf qq|<img src="%s" />&nbsp;%s|, $location->getFileIconUrl($file), $file;
return $fileValue; return $fileValue;
} }
@ -146,14 +153,11 @@ sub getValueFromPost {
my $storage = WebGUI::Storage->get($self->session,$value); my $storage = WebGUI::Storage->get($self->session,$value);
$storage->delete; $storage->delete;
return ''; return '';
} } elsif ($self->session->form->param($self->privateName('action')) eq 'keep') {
elsif ($self->session->form->param($self->privateName('action')) eq 'keep') {
return $value; return $value;
} } elsif ($self->session->form->param($self->privateName('action')) eq 'upload') {
elsif ($self->session->form->param($self->privateName('action')) eq 'upload') { my $storage = WebGUI::Storage::Image->create($self->session);
my $storage; $storage->addFileFromFormPost($self->get("name"),1000);
$storage = WebGUI::Storage::Image->create($self->session);
$storage->addFileFromFormPost($self->get("name"), 1);
my @files = @{ $storage->getFiles }; my @files = @{ $storage->getFiles };
if (scalar(@files) < 1) { if (scalar(@files) < 1) {
$storage->delete; $storage->delete;
@ -175,25 +179,42 @@ Renders a file upload control.
sub toHtml { sub toHtml {
my $self = shift; my $self = shift;
$self->session->style->setScript($self->session->config->get("extrasURL").'/FileUploadControl.js',{type=>"text/javascript"});
my $uploadControl = '<script type="text/javascript">
var fileIcons = new Array();
';
opendir(DIR,$self->session->config->get("extrasPath").'/fileIcons');
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
unless ($file eq "." || $file eq "..") {
my $ext = $file;
$ext =~ s/(.*?)\.gif/$1/;
$uploadControl .= 'fileIcons["'.$ext.'"] = "'.$self->session->config->get("extrasURL").'/fileIcons/'.$file.'";'."\n";
}
}
my $i18n = WebGUI::International->new($self->session); my $i18n = WebGUI::International->new($self->session);
$uploadControl .= sprintf q!var uploader = new FileUploadControl("%s", fileIcons, "%s","%d"); my $uploadControl = undef;
uploader.addRow(); my $storage = WebGUI::Storage->get($self->session, $self->get("value")) if ($self->get("value"));
</script>!, $self->get("name"), $i18n->get("removeLabel"), $self->get('maxAttachments'); my @files = $storage->getFiles if (defined $storage);
$uploadControl .= WebGUI::Form::Hidden->new($self->session, {-name => $self->privateName('action'), -value => 'upload'})->toHtml(); my $maxFiles = $self->get('maxAttachments') - scalar(@files);
if ($maxFiles > 0) {
$self->session->style->setScript($self->session->config->get("extrasURL").'/FileUploadControl.js',{type=>"text/javascript"});
$uploadControl = '<script type="text/javascript">
var fileIcons = new Array();
';
opendir(DIR,$self->session->config->get("extrasPath").'/fileIcons');
my @icons = readdir(DIR);
closedir(DIR);
foreach my $file (@icons) {
unless ($file eq "." || $file eq "..") {
my $ext = $file;
$ext =~ s/(.*?)\.gif/$1/;
$uploadControl .= 'fileIcons["'.$ext.'"] = "'.$self->session->config->get("extrasURL").'/fileIcons/'.$file.'";'."\n";
}
}
$uploadControl .= sprintf q!var uploader = new FileUploadControl("%s", fileIcons, "%s","%d");
uploader.addRow();
</script>!, $self->get("name"), $i18n->get("removeLabel"), $maxFiles;
$uploadControl .= WebGUI::Form::Hidden->new($self->session, {-name => $self->privateName('action'), -value => 'upload'})->toHtml()."<br />";
}
if (scalar(@files)) {
foreach my $file (@{$storage->getFiles}) {
if ($self->get("deleteFileUrl")) {
$uploadControl .= '<p style="display:inline;vertical-align:middle;"><a href="'.$self->get("deleteFileUrl").$file.'">'
.'<img src="'.$self->session->icon->_getBaseURL().'delete.gif" style="vertical-align:middle;border: 0px;" alt="x" /></a></p> ';
}
$uploadControl .= '<p style="display:inline;vertical-align:middle;"><a href="'.$storage->getUrl($file).'">'
.'<img src="'.$storage->getFileIconUrl($file).'" style="vertical-align:middle;border: 0px;" alt="'
.$file.'" /> '.$file.'</a></p><br />';
}
}
return $uploadControl; return $uploadControl;
} }

View file

@ -17,7 +17,7 @@ package WebGUI::Form::Image;
use strict; use strict;
use base 'WebGUI::Form::File'; use base 'WebGUI::Form::File';
use WebGUI::International; use WebGUI::International;
use WebGUI::Storage; use WebGUI::Storage::Image;
use WebGUI::Form::YesNo; use WebGUI::Form::YesNo;
=head1 NAME =head1 NAME
@ -92,7 +92,7 @@ sub displayForm {
return $self->toHtml unless $self->get('value'); return $self->toHtml unless $self->get('value');
##There are files inside here, for each one, display the image ##There are files inside here, for each one, display the image
##and another form control for deleting it. ##and another form control for deleting it.
my $location = WebGUI::Storage->get($self->session, $self->get('value')); my $location = WebGUI::Storage::Image->get($self->session, $self->get('value'));
my $i18n = WebGUI::International->new($self->session); my $i18n = WebGUI::International->new($self->session);
my $fileForm = ''; my $fileForm = '';
my $file = shift @{ $location->getFiles }; my $file = shift @{ $location->getFiles };
@ -118,10 +118,83 @@ profile field.
sub displayValue { sub displayValue {
my ($self) = @_; my ($self) = @_;
return '' unless $self->get("value"); return '' unless $self->get("value");
my $location = WebGUI::Storage->get($self->session,$self->get("value")); my $location = WebGUI::Storage::Image->get($self->session,$self->get("value"));
my $file = shift @{ $location->getFiles }; my $file = shift @{ $location->getFiles };
my $fileValue = sprintf qq!<img src="%s" />&nbsp;%s!, $location->getUrl($file), $file; my $fileValue = sprintf qq|<img src="%s" />&nbsp;%s|, $location->getUrl($file), $file;
return $fileValue; return $fileValue;
}
#-------------------------------------------------------------------
=head2 getValueFromFormPost ( )
See WebGUI::Form::File::getValueFromFormPost() for details. Generates a thumbnail.
=cut
sub getValueFromFormPost {
my $self = shift;
my $id = $self->SUPER::getValueFromFormPost(@_);
if (defined $id) {
my $storage = WebGUI::Storage::Image->get($self->session, $id);
if (defined $storage) {
foreach my $file (@{$storage->getFiles}) {
$storage->generateThumbnail($file) if ($storage->isImage($file));
}
}
} }
return $id;
}
#-------------------------------------------------------------------
=head2 toHtml ( )
Renders a file upload control.
=cut
sub toHtml {
my $self = shift;
my $i18n = WebGUI::International->new($self->session);
my $uploadControl = undef;
my $storage = WebGUI::Storage::Image->get($self->session, $self->get("value")) if ($self->get("value"));
my @files = $storage->getFiles if (defined $storage);
my $maxFiles = $self->get('maxAttachments') - scalar(@files);
if ($maxFiles > 0) {
$self->session->style->setScript($self->session->config->get("extrasURL").'/FileUploadControl.js',{type=>"text/javascript"});
$uploadControl = '<script type="text/javascript">
var fileIcons = new Array();
';
opendir(DIR,$self->session->config->get("extrasPath").'/fileIcons');
my @icons = readdir(DIR);
closedir(DIR);
foreach my $file (@icons) {
unless ($file eq "." || $file eq "..") {
my $ext = $file;
$ext =~ s/(.*?)\.gif/$1/;
$uploadControl .= 'fileIcons["'.$ext.'"] = "'.$self->session->config->get("extrasURL").'/fileIcons/'.$file.'";'."\n";
}
}
$uploadControl .= sprintf q!var uploader = new FileUploadControl("%s", fileIcons, "%s","%d");
uploader.addRow();
</script>!, $self->get("name"), $i18n->get("removeLabel"), $maxFiles;
$uploadControl .= WebGUI::Form::Hidden->new($self->session, {-name => $self->privateName('action'), -value => 'upload'})->toHtml()."<br />";
}
if (scalar(@files)) {
foreach my $file (@{$storage->getFiles}) {
if ($self->get("deleteFileUrl")) {
$uploadControl .= '<p style="display:inline;vertical-align:middle;"><a href="'.$self->get("deleteFileUrl").$file.'">'
.'<img src="'.$self->session->icon->_getBaseURL().'delete.gif" style="vertical-align:middle;border: 0px;" alt="x" /></a></p> ';
}
my $image = $storage->isImage($file) ? $storage->getThumbnailUrl($file) : $storage->getFileIconUrl($file);
$uploadControl .= '<p style="display:inline;vertical-align:middle;"><a href="'.$storage->getUrl($file).'">'
.'<img src="'.$image.'" style="vertical-align:middle;border: 0px;" alt="'
.$file.'" /> '.$file.'</a></p><br />';
}
}
return $uploadControl;
}
1; 1;

View file

@ -225,7 +225,7 @@ sub www_editProfileSave {
return WebGUI::Operation::Auth::www_auth($session, "init") if ($session->user->userId eq '1'); return WebGUI::Operation::Auth::www_auth($session, "init") if ($session->user->userId eq '1');
($profile, $error, $warning) = validateProfileData($session); ($profile, $error, $warning) = validateProfileData($session);
$error .= $warning; $error .= $warning;
return www_editProfile('<ul>'.$error.'</ul>') if($error ne ""); return www_editProfile($session, '<ul>'.$error.'</ul>') if($error ne "");
foreach $fieldName (keys %{$profile}) { foreach $fieldName (keys %{$profile}) {
$session->user->profileField($fieldName,$profile->{$fieldName}); $session->user->profileField($fieldName,$profile->{$fieldName});
} }

View file

@ -147,7 +147,7 @@ sub gateway {
my $url = $self->session->config->get("gateway").'/'.$pageUrl; my $url = $self->session->config->get("gateway").'/'.$pageUrl;
$url =~ s/\/+/\//g; $url =~ s/\/+/\//g;
if ($self->session->setting->get("preventProxyCache") == 1) { if ($self->session->setting->get("preventProxyCache") == 1) {
$url = $self->append($url,"noCache=".randint(0,1000).';'.$self->session->datetime->time()); $url = $self->append($url,"noCache=".randint(0,1000).','.$self->session->datetime->time());
} }
if ($pairs) { if ($pairs) {
$url = $self->append($url,$pairs); $url = $self->append($url,$pairs);
@ -319,7 +319,7 @@ sub page {
} }
$url .= $self->gateway($self->session->asset ? $self->session->asset->get("url") : $self->getRequestedUrl); $url .= $self->gateway($self->session->asset ? $self->session->asset->get("url") : $self->getRequestedUrl);
if ($self->session->setting->get("preventProxyCache") == 1 && !$skipPreventProxyCache) { if ($self->session->setting->get("preventProxyCache") == 1 && !$skipPreventProxyCache) {
$url = $self->append($url,"noCache=".randint(0,1000).';'.$self->session->datetime->time()); $url = $self->append($url,"noCache=".randint(0,1000).','.$self->session->datetime->time());
} }
if ($pairs) { if ($pairs) {
$url = $self->append($url,$pairs); $url = $self->append($url,$pairs);

View file

@ -40,8 +40,8 @@ use WebGUI::Storage::Image;
These methods are available from this class: These methods are available from this class:
my $boolean = $self->generateThumbnail($filename); my $boolean = $self->generateThumbnail($filename);
my $url = $self->getThumbnailUrl; my $url = $self->getThumbnailUrl($filename);
my $boolean = $self->isImage; my $boolean = $self->isImage($filename);
my ($captchaFile, $challenge) = $self->addFileFromCaptcha; my ($captchaFile, $challenge) = $self->addFileFromCaptcha;
$self->resize($imageFile, $width, $height); $self->resize($imageFile, $width, $height);

View file

@ -1,6 +1,16 @@
package WebGUI::i18n::English::Asset_Article; package WebGUI::i18n::English::Asset_Article;
our $I18N = { our $I18N = {
'attachments' => {
message => q|Attachments|,
lastUpdated => 0
},
'attachments help' => {
message => q|Attach files and images directly to this Article. Please note that these files will not be accessible through the asset manager to other assets.|,
lastUpdated => 0
},
'cache timeout' => { 'cache timeout' => {
message => q|Cache Timeout|, message => q|Cache Timeout|,
lastUpdated => 0 lastUpdated => 0
@ -166,6 +176,28 @@ The URL to the thumbnail for the attached image.
The URL to the attached image. The URL to the attached image.
<p/> <p/>
<b>attachment_loop</b><br />
A loop containing all the attachments.
<blockquote>
<b>filename</b><br />
The name of the file.
<p />
<b>url</b><br />
The url to download the file.
<p />
<b>thumbnailUrl</b><br />
The url of the thumbnail of this file.
<p />
<b>iconUrl</b><br />
The url to the file type icon of this file.
<p />
<b>isImage</b><br />
A boolean indicating whether this is an image or not.
<p />
</blockquote>
<p/>
<b>linkTitle</b><br/> <b>linkTitle</b><br/>
The title of the link added to the article. The title of the link added to the article.
<p/> <p/>

View file

@ -177,11 +177,6 @@ our $I18N = {
lastUpdated => 1141142205, lastUpdated => 1141142205,
}, },
'delete file warning' => {
message => q|Are you sure you wish to delete this file?|,
lastUpdated => 1109618544,
},
'display last reply' => { 'display last reply' => {
message => q|Display last reply?|, message => q|Display last reply?|,
lastUpdated => 1109618544, lastUpdated => 1109618544,