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();
updateFolder();
addRichEditUpload();
updateArticle();
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 {
print "\tAdding the ability to upload from the rich editor.\n";

View file

@ -147,8 +147,7 @@ sub definition {
className=>'WebGUI::Asset::Post',
properties=>{
storageId => {
noFormPost=>1,
fieldType=>"hidden",
fieldType=>"file",
defaultValue=>undef
},
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 ( )
@ -1065,7 +1040,11 @@ sub www_edit {
$var{'form.preview'} = WebGUI::Form::submit($self->session, {
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, {
name=>'contentType',
value=>$self->getValue("contentType") || "mixed"

View file

@ -16,6 +16,7 @@ use WebGUI::International;
use WebGUI::Cache;
use WebGUI::Paginator;
use WebGUI::Asset::Wobject;
use WebGUI::Storage::Image;
our @ISA = qw(WebGUI::Asset::Wobject);
@ -114,6 +115,15 @@ sub definition {
subtext=>' &nbsp; <span style="font-size: 8pt;">'.$i18n->get(11).'</span>',
hoverHelp=>$i18n->get('carriage return description'),
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}, {
@ -127,6 +137,32 @@ sub 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 ( )
@ -178,15 +214,24 @@ sub view {
return $out if $out;
}
my %var;
my $children = $self->getLineage(["children"],{returnObjects=>1,includeOnlyClasses=>["WebGUI::Asset::File","WebGUI::Asset::File::Image"]});
foreach my $child (@{$children}) {
if (ref $child eq "WebGUI::Asset::File") {
$var{"attachment.icon"} = $child->getFileIconUrl;
$var{"attachment.url"} = $child->getFileUrl;
$var{"attachment.name"} = $child->get("filename");
} elsif (ref $child eq "WebGUI::Asset::File::Image") {
$var{"image.url"} = $child->getFileUrl;
$var{"image.thumbnail"} = $child->getThumbnailUrl;
if ($self->get("storageId")) {
my $storage = WebGUI::Storage::Image->get($self->session, $self->get("storageId"));
foreach my $file (@{$storage->getFiles}) {
if ($storage->isImage($file)) {
$var{'image.url'} = $storage->getUrl($file);
$var{'image.thumbnail'} = $storage->getThumbnailUrl($file);
} else {
$var{'attachment.icon'} = $storage->getFileIconUrl($file);
$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");
@ -235,5 +280,25 @@ sub view {
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;

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
=head4 deleteFileUrl
A url that will get a filename appended to it and then links to delete the files will be generated automatically.
=cut
sub definition {
@ -80,6 +84,9 @@ sub definition {
profileEnabled=>{
defaultValue=>1
},
deleteFileUrl=>{
defaultValue=>undef
}
});
return $class->SUPER::definition($session, $definition);
}
@ -125,7 +132,7 @@ sub displayValue {
return '' unless $self->get("value");
my $location = WebGUI::Storage->get($self->session,$self->get("value"));
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;
}
@ -146,14 +153,11 @@ sub getValueFromPost {
my $storage = WebGUI::Storage->get($self->session,$value);
$storage->delete;
return '';
}
elsif ($self->session->form->param($self->privateName('action')) eq 'keep') {
} elsif ($self->session->form->param($self->privateName('action')) eq 'keep') {
return $value;
}
elsif ($self->session->form->param($self->privateName('action')) eq 'upload') {
my $storage;
$storage = WebGUI::Storage::Image->create($self->session);
$storage->addFileFromFormPost($self->get("name"), 1);
} elsif ($self->session->form->param($self->privateName('action')) eq 'upload') {
my $storage = WebGUI::Storage::Image->create($self->session);
$storage->addFileFromFormPost($self->get("name"),1000);
my @files = @{ $storage->getFiles };
if (scalar(@files) < 1) {
$storage->delete;
@ -175,25 +179,42 @@ Renders a file upload control.
sub toHtml {
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);
$uploadControl .= sprintf q!var uploader = new FileUploadControl("%s", fileIcons, "%s","%d");
uploader.addRow();
</script>!, $self->get("name"), $i18n->get("removeLabel"), $self->get('maxAttachments');
$uploadControl .= WebGUI::Form::Hidden->new($self->session, {-name => $self->privateName('action'), -value => 'upload'})->toHtml();
my $uploadControl = undef;
my $storage = WebGUI::Storage->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> ';
}
$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;
}

View file

@ -17,7 +17,7 @@ package WebGUI::Form::Image;
use strict;
use base 'WebGUI::Form::File';
use WebGUI::International;
use WebGUI::Storage;
use WebGUI::Storage::Image;
use WebGUI::Form::YesNo;
=head1 NAME
@ -92,7 +92,7 @@ sub displayForm {
return $self->toHtml unless $self->get('value');
##There are files inside here, for each one, display the image
##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 $fileForm = '';
my $file = shift @{ $location->getFiles };
@ -118,10 +118,83 @@ profile field.
sub displayValue {
my ($self) = @_;
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 $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;
}
#-------------------------------------------------------------------
=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;

View file

@ -225,7 +225,7 @@ sub www_editProfileSave {
return WebGUI::Operation::Auth::www_auth($session, "init") if ($session->user->userId eq '1');
($profile, $error, $warning) = validateProfileData($session);
$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}) {
$session->user->profileField($fieldName,$profile->{$fieldName});
}

View file

@ -147,7 +147,7 @@ sub gateway {
my $url = $self->session->config->get("gateway").'/'.$pageUrl;
$url =~ s/\/+/\//g;
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) {
$url = $self->append($url,$pairs);
@ -319,7 +319,7 @@ sub page {
}
$url .= $self->gateway($self->session->asset ? $self->session->asset->get("url") : $self->getRequestedUrl);
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) {
$url = $self->append($url,$pairs);

View file

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

View file

@ -1,6 +1,16 @@
package WebGUI::i18n::English::Asset_Article;
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' => {
message => q|Cache Timeout|,
lastUpdated => 0
@ -166,6 +176,28 @@ The URL to the thumbnail for the attached image.
The URL to the attached image.
<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/>
The title of the link added to the article.
<p/>

View file

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