Skip to content

Commit a2a3b2d

Browse files
committedOct 11, 2011
Convert FilePile asset into an AssetHelper.
1 parent c6f4222 commit a2a3b2d

File tree

3 files changed

+260
-1
lines changed

3 files changed

+260
-1
lines changed
 

‎lib/WebGUI/Asset.pm

+4
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,10 @@ sub getHelpers {
13411341
label => $i18n->get('delete'),
13421342
confirm => $i18n->get('43'),
13431343
},
1344+
upload_files => {
1345+
className => 'WebGUI::AssetHelper::UploadFiles',
1346+
label => $i18n->get('upload files'),
1347+
},
13441348
};
13451349

13461350
# Merge additional helpers for this class from config

‎lib/WebGUI/AssetHelper/UploadFiles.pm

+249
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
package WebGUI::AssetHelper::UploadFiles;
2+
3+
use strict;
4+
use Class::C3;
5+
use base qw/WebGUI::AssetHelper/;
6+
use WebGUI::Form::File;
7+
use WebGUI::TabForm;
8+
9+
=head1 LEGAL
10+
11+
-------------------------------------------------------------------
12+
WebGUI is Copyright 2001-2009 Plain Black Corporation.
13+
-------------------------------------------------------------------
14+
Please read the legal notices (docs/legal.txt) and the license
15+
(docs/license.txt) that came with this distribution before using
16+
this software.
17+
-------------------------------------------------------------------
18+
http://www.plainblack.com info@plainblack.com
19+
-------------------------------------------------------------------
20+
21+
=head1 NAME
22+
23+
Package WebGUI::AssetHelper::UploadFiles
24+
25+
=head1 DESCRIPTION
26+
27+
Creates multiple file assets from form uploads beneath the current asset.
28+
29+
=head1 METHODS
30+
31+
These methods are available from this class:
32+
33+
=cut
34+
35+
#-------------------------------------------------------------------
36+
37+
=head2 process ()
38+
39+
Opens a new tab for displaying the form and the output for editing a branch.
40+
41+
=cut
42+
43+
sub process {
44+
my ($self) = @_;
45+
my $asset = $self->asset;
46+
my $session = $self->session;
47+
my $i18n = WebGUI::International->new($session, "Asset");
48+
if (! $asset->canEdit) {
49+
return {
50+
error => $i18n->get('38', 'WebGUI'),
51+
}
52+
}
53+
54+
return {
55+
openDialog => $self->getUrl( 'uploadFiles' ),
56+
};
57+
}
58+
59+
#-------------------------------------------------------------------
60+
61+
=head2 www_uploadFiles ( )
62+
63+
Creates a tabform to edit the Asset Tree. If canEdit returns False, returns insufficient Privilege page.
64+
65+
=cut
66+
67+
sub www_uploadFiles {
68+
my ($self) = @_;
69+
my $asset = $self->asset;
70+
my $session = $self->session;
71+
my $i18n = WebGUI::International->new($session, 'Asset');
72+
my ( $style, $url ) = $session->quick( qw( style url ) );
73+
$style->setCss( $url->extras('hoverhelp.css'));
74+
$style->setScript( $url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js') );
75+
$style->setScript( $url->extras('yui/build/container/container-min.js') );
76+
$style->setScript( $url->extras('hoverhelp.js') );
77+
$style->setRawHeadTags( <<'ENDHTML' );
78+
<style type="text/css">
79+
label.formDescription { display: block; margin-top: 1em; font-weight: bold }
80+
</style>
81+
ENDHTML
82+
my $tabform = WebGUI::TabForm->new($session);
83+
$tabform->hidden({name=>"op",value=>"assetHelper"});
84+
$tabform->hidden({name=>"helperId",value=>$self->id});
85+
$tabform->hidden({name=>"method",value=>"uploadFilesSave"});
86+
if ($session->config->get("enableSaveAndCommit")) {
87+
$tabform->submitAppend(WebGUI::Form::submit($session, {
88+
name => "saveAndCommit",
89+
value => WebGUI::International->new($session, 'Asset')->get("save and commit"),
90+
}));
91+
}
92+
my $prop_tab = $tabform->addTab("properties",$i18n->get("properties","Asset"));
93+
my $sec_tab = $tabform->addTab("security",$i18n->get(107,"Asset"),6);
94+
$prop_tab->yesNo(
95+
name => "isHidden",
96+
value => 1,
97+
label => $i18n->get(886, 'Asset'),
98+
hoverHelp => $i18n->get('886 description', 'Asset'),
99+
uiLevel => 6,
100+
);
101+
$prop_tab->yesNo(
102+
name => "newWindow",
103+
value => 0,
104+
label => $i18n->get(940, 'Asset'),
105+
hoverHelp => $i18n->get('940 description', 'Asset'),
106+
uiLevel => 6,
107+
);
108+
$prop_tab->file(
109+
name => 'upload_files',
110+
label => $i18n->get("upload files"),
111+
hoverHelp => $i18n->get("upload files description"),
112+
maxAttachments => 100,
113+
);
114+
my $subtext;
115+
if ($session->user->isAdmin) {
116+
$subtext = $session->icon->manage('op=listUsers');
117+
}
118+
else {
119+
$subtext = "";
120+
}
121+
my $clause;
122+
if ($session->user->isAdmin) {
123+
my $group = WebGUI::Group->new($session,4);
124+
my $contentManagers = $group->getAllUsers();
125+
push (@$contentManagers, $session->user->userId);
126+
$clause = "userId in (".$session->db->quoteAndJoin($contentManagers).")";
127+
}
128+
else {
129+
$clause = "userId=".$session->db->quote($asset->get("ownerUserId"));
130+
}
131+
my $users = $session->db->buildHashRef("select userId,username from users where $clause order by username");
132+
$sec_tab->selectBox(
133+
name => "ownerUserId",
134+
options => $users,
135+
label => $i18n->get(108, 'Asset'),
136+
hoverHelp => $i18n->get('108 description', 'Asset'),
137+
value => [$asset->get("ownerUserId")],
138+
subtext => $subtext,
139+
uiLevel => 6,
140+
);
141+
$sec_tab->group(
142+
name => "groupIdView",
143+
label => $i18n->get(872, 'Asset'),
144+
hoverHelp => $i18n->get('872 description', 'Asset'),
145+
value => [$asset->get("groupIdView")],
146+
uiLevel => 6,
147+
);
148+
$sec_tab->group(
149+
name => "groupIdEdit",
150+
label => $i18n->get(871, 'Asset'),
151+
hoverHelp => $i18n->get('871 description', 'Asset'),
152+
value => [$asset->get("groupIdEdit")],
153+
excludeGroups => [1,7],
154+
uiLevel => 6,
155+
);
156+
157+
return $session->style->process(
158+
'<div class="yui-skin-sam">' . $tabform->print . '</div>',
159+
"PBtmpl0000000000000137"
160+
);
161+
}
162+
163+
#-------------------------------------------------------------------
164+
165+
=head2 www_uploadFilesSave ( )
166+
167+
Process form output and create child File/Image assets as approriate.
168+
169+
=cut
170+
171+
sub www_uploadFilesSave {
172+
my ($self) = @_;
173+
my $asset = $self->asset;
174+
my $session = $self->session;
175+
return $session->privilege->insufficient() unless ($asset->canEdit && $session->user->isInGroup('4'));
176+
if ($session->config("maximumAssets")) {
177+
my ($count) = $session->db->quickArray("select count(*) from asset");
178+
my $i18n = WebGUI::International->new($session, "Asset");
179+
return $session->style->userStyle($i18n->get("over max assets")) if ($session->config("maximumAssets") <= $count);
180+
}
181+
182+
my $overrides = $session->config->get( "assets/" . $asset->get("className") . "/fields" );
183+
my $form = $session->form;
184+
185+
##Process the form data that is the same for every uploaded file.
186+
my %asset_defaults = ();
187+
foreach my $property_name ( $asset->getProperties ) {
188+
my $property = $asset->meta->find_attribute_by_name($property_name);
189+
next if $property->noFormPost;
190+
191+
my $fieldType = $property->fieldType;
192+
my $fieldOverrides = $overrides->{$property_name} || {};
193+
my $fieldHash = {
194+
tab => "properties",
195+
%{ $asset->getFormProperties($property_name) },
196+
%{$overrides},
197+
name => $property_name,
198+
value => $asset->$property_name,
199+
};
200+
201+
202+
# process the form element
203+
my $defaultValue = $overrides->{defaultValue} // $asset->$property;
204+
$asset_defaults{$property_name} = $form->process( $property_name, $fieldType, $defaultValue, $fieldHash );
205+
} ## end foreach my $property ( $asset...)
206+
207+
##This is a hack. File uploads should go through the WebGUI::Form::File API
208+
my $tempFileStorageId = WebGUI::Form::File->new($session,{name => 'upload_files'})->getValue;
209+
my $tempStorage = WebGUI::Storage->get($session, $tempFileStorageId);
210+
211+
foreach my $filename (@{$tempStorage->getFiles}) {
212+
my $selfName = $tempStorage->isImage($filename)
213+
? "WebGUI::Asset::File::Image"
214+
:'WebGUI::Asset::File';
215+
216+
my %data = %asset_defaults;
217+
218+
$data{className} = $selfName;
219+
$data{filename} = $data{title} = $data{menuTitle} = $filename;
220+
$data{templateId} = 'PBtmpl0000000000000024';
221+
if ($selfName eq "WebGUI::Asset::File::Image") {
222+
$data{templateId} = 'PBtmpl0000000000000088';
223+
}
224+
$data{url} = $asset->get('url').'/'.$filename;
225+
226+
#Create the new asset
227+
my $newAsset = $asset->addChild(\%data);
228+
229+
#Get the current storage location
230+
my $storage = $newAsset->getStorageLocation();
231+
$storage->addFileFromFilesystem($tempStorage->getPath($filename));
232+
$newAsset->applyConstraints;
233+
234+
#Now remove the reference to the storeage location to prevent problems with different revisions.
235+
delete $newAsset->{_storageLocation};
236+
}
237+
$tempStorage->delete;
238+
239+
if (WebGUI::VersionTag->autoCommitWorkingIfEnabled($session, {
240+
override => scalar $session->form->process("saveAndCommit"),
241+
allowComments => 1,
242+
returnUrl => $asset->getUrl,
243+
}) eq 'redirect') {
244+
return undef;
245+
};
246+
247+
}
248+
249+
1;

‎lib/WebGUI/i18n/English/Asset.pm

+7-1
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,13 @@ Couldn't open %-s because %-s <br />
15011501
'Allowed Classes hoverHelp' => {
15021502
message => 'Which assets use this metadata?',
15031503
lastUpdated => 1295986062,
1504-
}
1504+
},
1505+
1506+
'upload files' => {
1507+
message => q|Upload Files|,
1508+
context => q|Name of the asset helper.|,
1509+
lastUpdated => 1107387247,
1510+
},
15051511
};
15061512

15071513
1;

0 commit comments

Comments
 (0)
Please sign in to comment.