- api: You may now use a displayOnly attribute in your asset properties list
that will display a field, but is not settable via the update() method. - api: You may now use a customDrawMethod attribute in your asset properties list that will enable you to add custom display options for that fields when the edit form is automatically generated. - Added file attachments to the Wiki. - Added a new attachments form control. - Added a form control skeleton.
This commit is contained in:
parent
591fd954e3
commit
350d7f6e01
14 changed files with 599 additions and 85 deletions
294
lib/WebGUI/Form/Attachments.pm
Normal file
294
lib/WebGUI/Form/Attachments.pm
Normal file
|
|
@ -0,0 +1,294 @@
|
|||
package WebGUI::Form::Attachments;
|
||||
|
||||
=head1 LEGAL
|
||||
|
||||
-------------------------------------------------------------------
|
||||
WebGUI is Copyright 2001-2007 Plain Black Corporation.
|
||||
-------------------------------------------------------------------
|
||||
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 strict;
|
||||
use base 'WebGUI::Form::Control';
|
||||
use WebGUI::Asset;
|
||||
use WebGUI::International;
|
||||
use WebGUI::Storage::Image;
|
||||
use WebGUI::VersionTag;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Form::File
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Creates a javascript driven file upload control for asset attachments.
|
||||
|
||||
B<NOTE:> This is meant to be used in
|
||||
conjunction with one or more Rich Editors (see WebGUI::Form::HTMLArea) and should be placed above them in the field
|
||||
list for ease of use.
|
||||
|
||||
B<WARNING:> This form control is not capable of handling all aspects of the files uploaded to it. So you as the
|
||||
developer need to complete the process after the form has been submitted. See the getValueFromPost() method for
|
||||
details.
|
||||
|
||||
=head1 SEE ALSO
|
||||
|
||||
This is a subclass of WebGUI::Form::Control.
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
The following methods are specifically available from this class. Check the superclass for additional methods.
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 definition ( [ additionalTerms ] )
|
||||
|
||||
See the super class for additional details.
|
||||
|
||||
=head3 additionalTerms
|
||||
|
||||
The following additional parameters have been added via this sub class.
|
||||
|
||||
=head4 name
|
||||
|
||||
If no name is specified a default name of "attachments" will be used.
|
||||
|
||||
=head4 maxAttachments
|
||||
|
||||
How many attachments will be allowed to be uploaded. Defaults to 1.
|
||||
|
||||
=head4 value
|
||||
|
||||
An array reference of asset objects (not ids, but objects) that should be displayed in the attachments box.
|
||||
|
||||
=cut
|
||||
|
||||
sub definition {
|
||||
my $class = shift;
|
||||
my $session = shift;
|
||||
my $definition = shift || [];
|
||||
my $i18n = WebGUI::International->new($session);
|
||||
push(@{$definition}, {
|
||||
formName=>{
|
||||
defaultValue=>$i18n->get("file")
|
||||
},
|
||||
name=>{
|
||||
defaultValue=>"attachments"
|
||||
},
|
||||
maxAttachments=>{
|
||||
defaultValue=>1
|
||||
},
|
||||
profileEnabled=>{
|
||||
defaultValue=>0
|
||||
},
|
||||
dbDataType => {
|
||||
defaultValue => "VARCHAR(22) BINARY",
|
||||
},
|
||||
});
|
||||
return $class->SUPER::definition($session, $definition);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getValueFromPost ( )
|
||||
|
||||
Returns an array reference of asset ids that have been uploaded. New assets are uploaded to a temporary location,
|
||||
and you must move them to the place in the asset tree you want them, or they will be automatically deleted.
|
||||
|
||||
=cut
|
||||
|
||||
sub getValueFromPost {
|
||||
my $self = shift;
|
||||
my @values = $self->session->form->param($self->get("name"));
|
||||
return \@values;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 toHtml ( )
|
||||
|
||||
Renders an attachments control.
|
||||
|
||||
=cut
|
||||
|
||||
sub toHtml {
|
||||
my $self = shift;
|
||||
my @assetIds = @{$self->get("value")};
|
||||
my $attachmentsList = "attachments=".join(";attachments=", @assetIds) if (scalar(@assetIds));
|
||||
return '<iframe src="'
|
||||
.$self->session->url->page("op=formHelper;class=Attachments;sub=show;name=".$self->get("name")
|
||||
.";maxAttachments=".$self->get("maxAttachments")).";".$attachmentsList
|
||||
.'" style="width: 100%; height: 110px;"></iframe><div id="'.$self->get("name").'_formId"></div>';
|
||||
}
|
||||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_delete ()
|
||||
|
||||
Deletes an attachment.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_delete {
|
||||
my $session = shift;
|
||||
my $assetId = $session->form->param("assetId");
|
||||
my @assetIds = $session->form->param("attachments");
|
||||
if ($assetId ne "") {
|
||||
my $asset = WebGUI::Asset->newByDynamicClass($session, $assetId);
|
||||
if (defined $asset) {
|
||||
if ($asset->canEdit) {
|
||||
my $version = WebGUI::VersionTag->new($session, $asset->get("tagId"));
|
||||
$asset->purge;
|
||||
if ($version->getAssetCount == 0) {
|
||||
$version->rollback;
|
||||
}
|
||||
my @tempAssetIds = ();
|
||||
foreach my $id (@assetIds) {
|
||||
push(@tempAssetIds, $id) unless $id eq $assetId;
|
||||
}
|
||||
@assetIds = @tempAssetIds;
|
||||
}
|
||||
}
|
||||
}
|
||||
return www_show($session,\@assetIds);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_show ()
|
||||
|
||||
A web accessible method that displays the attachments associated with this attachments control.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_show {
|
||||
my $session = shift;
|
||||
my ($form, $url, $style) = $session->quick(qw(form url style));
|
||||
my $assetIdRef = shift;
|
||||
my @assetIds = [];
|
||||
if (defined $assetIdRef) {
|
||||
$assetIdRef ||= [];
|
||||
@assetIds = @{$assetIdRef};
|
||||
}
|
||||
else {
|
||||
@assetIds = $session->form->param("attachments");
|
||||
}
|
||||
$session->http->setCacheControl("none");
|
||||
$style->setScript($url->extras("/AttachmentsControl/AttachmentsControl.js"),
|
||||
{type=>"text/javascript"});
|
||||
$style->setLink($url->extras("/AttachmentsControl/AttachmentsControl.css"),
|
||||
{type=>"text/css", rel=>"stylesheet"});
|
||||
my $uploadControl = '';
|
||||
my $i18n = WebGUI::International->new($session, "Control_Attachments");
|
||||
my $maxFiles = $form->param('maxAttachments') - scalar(@assetIds) ;
|
||||
my $attachmentForms = '';
|
||||
foreach my $assetId (@assetIds) {
|
||||
$attachmentForms .= '<input type="hidden" name="attachments" value="'.$assetId.'" />';
|
||||
}
|
||||
if ($maxFiles > 0) {
|
||||
$uploadControl = '<div id="uploadForm">
|
||||
<a href="#" onclick="WebguiAttachmentUploadForm.hide();" id="uploadFormCloser">X</a>
|
||||
<form action="'.$url->page.'" enctype="multipart/form-data" method="post">
|
||||
<input type="hidden" name="maxAttachments" value="'.$form->param("maxAttachments").'" />
|
||||
<input type="hidden" name="name" value="'.$form->param("name").'" />
|
||||
<input type="hidden" name="op" value="formHelper" />
|
||||
<input type="hidden" name="class" value="Attachments" />
|
||||
<input type="hidden" name="sub" value="upload" /> '. $attachmentForms
|
||||
.'<input type="file" name="attachment" />
|
||||
<input type="submit" value="Upload" /> </form> </div>
|
||||
<a id="upload" href="#" onclick="WebguiAttachmentUploadForm.show();">Upload an attachment.</a>
|
||||
';
|
||||
}
|
||||
my $attachments = '';
|
||||
my $attachmentsList = "attachments=".join(";attachments=", @assetIds) if (scalar(@assetIds));
|
||||
foreach my $assetId (@assetIds) {
|
||||
my $asset = WebGUI::Asset->newByDynamicClass($session, $assetId);
|
||||
if (defined $asset) {
|
||||
$attachments .= '<div class="attachment"><a href="'
|
||||
.$url->page("op=formHelper;class=Attachments;sub=delete;maxAttachments=".$form->param("maxAttachments").";"
|
||||
.$attachmentsList.";assetId=".$assetId.";name=".$form->param("name")).'" class="deleteAttachment">X</a>
|
||||
';
|
||||
if ($asset->isa("WebGUI::Asset::File::Image")) {
|
||||
$attachments .= '
|
||||
<div class="thumbnail"><img src="'.$asset->getThumbnailUrl.'" alt="'.$asset->getTitle.'" /></div>
|
||||
<a class="imageLink" href="'.$asset->getUrl.'">'.$asset->getTitle.'</a>
|
||||
<img src="'.$asset->getUrl.'" alt="'.$asset->getTitle.'" />';
|
||||
}
|
||||
else {
|
||||
$attachments .= '
|
||||
<div class="thumbnail"><img src="'.$asset->getFileIconUrl.'" alt="'.$asset->getTitle.'" /></div>
|
||||
<a href="'.$asset->getUrl.'">'.$asset->getTitle.'</a>';
|
||||
}
|
||||
$attachments .= '</div>';
|
||||
}
|
||||
}
|
||||
my $output = '<html><head> '.$style->generateAdditionalHeadTags.'
|
||||
<script type="text/javascript">
|
||||
parent.document.getElementById("'.$form->get("name").'_formId").innerHTML = \''.$attachmentForms.'\';
|
||||
</script>
|
||||
</head> <body>
|
||||
'.$uploadControl.' <div id="instructions">Upload attachments here. Copy and paste attachments into the editor.</div>
|
||||
<div id="attachments">'.$attachments.' </div> </body> </html> ';
|
||||
return $output;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_upload
|
||||
|
||||
A web accessible method that uploads an attachment to tempsace.
|
||||
|
||||
=cut
|
||||
|
||||
sub www_upload {
|
||||
my $session = shift;
|
||||
my $form = $session->form;
|
||||
my @assetIds = $form->param("attachments");
|
||||
my $storage = WebGUI::Storage::Image->createTemp($session);
|
||||
my $filename = $storage->addFileFromFormPost("attachment");
|
||||
my $tempspace = WebGUI::Asset->getTempspace($session);
|
||||
if ($storage->isImage($filename)) {
|
||||
my $image = $tempspace->addChild({
|
||||
title => $filename,
|
||||
url => "attachments/".$filename,
|
||||
className => "WebGUI::Asset::File::Image",
|
||||
filename => $filename,
|
||||
templateId => "PBtmpl0000000000000088",
|
||||
});
|
||||
$image->getStorageLocation->addFileFromFilesystem($storage->getPath($filename));
|
||||
$image->generateThumbnail();
|
||||
$image->setSize;
|
||||
push(@assetIds, $image->getId);
|
||||
}
|
||||
else {
|
||||
my $file = $tempspace->addChild({
|
||||
title => $filename,
|
||||
url => "attachments/".$filename,
|
||||
className => "WebGUI::Asset::File",
|
||||
filename => $filename,
|
||||
templateId => "PBtmpl0000000000000024",
|
||||
});
|
||||
$file->getStorageLocation->addFileFromFilesystem($storage->getPath($filename));
|
||||
$file->setSize;
|
||||
push(@assetIds, $file->getId);
|
||||
}
|
||||
if ($session->setting->get("autoRequestCommit")) {
|
||||
WebGUI::VersionTag->getWorking($session)->requestCommit;
|
||||
}
|
||||
$storage->delete;
|
||||
return www_show($session, \@assetIds);
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue