add "asset" property to AssetHelpers.

Simplifies the API and allows us to have getForm and getUrl to make
asset helpers easier to build.
This commit is contained in:
Doug Bell 2011-03-25 14:49:44 -05:00
parent df31c13e13
commit 698f40a6dd
23 changed files with 216 additions and 123 deletions

View file

@ -661,8 +661,8 @@ sub www_processAssetHelper {
my $class = $asset->getHelpers->{ $helperId }->{ className };
WebGUI::Pluggable::load( $class );
my $helper = $class->new( id => $helperId, session => $self->session );
return JSON->new->encode( $helper->process( $asset ) );
my $helper = $class->new( id => $helperId, session => $self->session, asset => $asset );
return JSON->new->encode( $helper->process );
}
#----------------------------------------------------------------------
@ -968,17 +968,19 @@ __DATA__
<div id="yui-tabs" class="yui-content">
<div id="viewTab"><iframe src="<tmpl_var viewUrl>" name="view"></iframe></div>
<div id="treeTab">
<div id="treeButtons">
<input type="button" id="treeUpdate" value="^i18n('update');" />
<input type="button" id="treeDelete" value="^i18n('delete');" />
<input type="button" id="treeCut" value="^i18n('cut');" />
<input type="button" id="treeCopy" value="^i18n('copy');" />
<input type="button" id="treeDuplicate" value="^i18n('duplicate');" />
<input type="button" id="treeCreateShortcut" value="^i18n('create shortcut');" />
</div>
<div id="treeCrumbtrail"></div>
<div id="treeDataTableContainer"></div>
<div id="treePagination"></div>
<div id="treeBottom">
<div id="treeButtons">
<input type="button" id="treeUpdate" value="^i18n('update');" />
<input type="button" id="treeDelete" value="^i18n('delete');" />
<input type="button" id="treeCut" value="^i18n('cut');" />
<input type="button" id="treeCopy" value="^i18n('copy');" />
<input type="button" id="treeDuplicate" value="^i18n('duplicate');" />
<input type="button" id="treeCreateShortcut" value="^i18n('create shortcut');" />
</div>
<div id="treePagination"></div>
</div>
</div>
</div>
</div>

View file

@ -56,6 +56,12 @@ has 'id' => (
isa => 'Str',
);
has 'asset' => (
is => 'ro',
required => 1,
isa => 'WebGUI::Asset',
);
=head1 METHODS
These methods are available from this class:
@ -64,6 +70,47 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 getForm ( $method )
Get a WebGUI::FormBuilder that submits to the given www_ $method.
=cut
sub getForm {
my ( $self, $method ) = @_;
my $f = WebGUI::FormBuilder->new( $self->session, action => $self->session->url->page );
$f->addField( 'hidden', name => 'op', value => 'assetHelper' );
$f->addField( 'hidden', name => 'helperId', value => $self->id );
$f->addField( 'hidden', name => 'method', value => $method );
$f->addField( 'hidden', name => 'assetId', value => $self->asset->assetId );
return $f;
}
#-------------------------------------------------------------------
=head2 getUrl ( $method, $pairs )
Get a URL to call the www_ method of this Asset Helper. $method is the name
of the method, without the www_. $pairs is a string of name=value; pairs to
add to the URL.
=cut
sub getUrl {
my ( $self, $method, $pairs ) = @_;
$method ||= 'view';
$pairs ||= '';
return $self->asset->getUrl(
'op=assetHelper;assetId=' . $self->asset->assetId . ';helperId=' . $self->id
. ';method=' . $method . ';' . $pairs
);
}
#-------------------------------------------------------------------
=head2 process ( $asset )
Process is the default method called by the Admin Console.

View file

@ -34,15 +34,16 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ( )
Opens a new tab for displaying the form to change the Asset's URL.
=cut
sub process {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, "Asset");
if (! $asset->canEdit) {
return {
@ -51,7 +52,7 @@ sub process {
}
return {
openDialog => $asset->getUrl('op=assetHelper;helperId=' . $self->id . ';method=changeUrl;assetId=' . $asset->getId ),
openDialog => $self->getUrl( 'changeUrl' ),
};
}
@ -64,19 +65,16 @@ Displays a form to change the URL for this asset.
=cut
sub www_changeUrl {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, "Asset");
if (! $asset->canEdit) {
return {
error => $i18n->get('38', 'WebGUI'),
}
}
my $f = WebGUI::FormBuilder->new($session, method => 'POST', action => $asset->getUrl );
$f->addField( "hidden", name => 'op', value => 'assetHelper' );
$f->addField( "hidden", name => 'helperId', value => $self->id );
$f->addField( "hidden", name => "method", value=>"changeUrlSave" );
$f->addField( "hidden", name => 'assetId', value => $asset->getId );
my $f = $self->getForm( 'changeUrlSave' );
$f->addField( "text",
name => "url",
value => $asset->get('url'),
@ -103,8 +101,9 @@ This actually does the change url of the www_changeUrl() function.
=cut
sub www_changeUrlSave {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, "Asset");
if (! $asset->canEdit) {
return {

View file

@ -33,14 +33,15 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ( )
Fork the copy operation
=cut
sub process {
my ($self, $asset) = @_;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
# Should we autocommit?

View file

@ -3,6 +3,7 @@ package WebGUI::AssetHelper::CopyBranch;
use strict;
use Class::C3;
use base qw/WebGUI::AssetHelper::Copy/;
use Scalar::Util qw{ blessed };
=head1 LEGAL
@ -32,69 +33,106 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ()
Open a progress dialog for the copy operation
=cut
sub process {
my ($self, $asset) = @_;
my ($self) = @_;
return {
openDialog => '?op=assetHelper;helperId=' . $self->id . ';method=getWith;assetId=' . $asset->getId
openDialog => $self->getUrl( 'getWith' ),
};
}
#----------------------------------------------------------------------------
=head2 www_getWith ( $asset )
=head2 www_getWith ()
Get the "with" configuration. "Descendants" or "Children".
=cut
sub www_getWith {
my ( $self, $asset ) = @_;
my $session = $asset->session;
my ( $self ) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, 'Asset');
return '<form style="text-align: center">'
. '<input type="hidden" name="op" value="assetHelper" />'
. '<input type="hidden" name="helperId" value="' . $self->id . '" />'
. '<input type="hidden" name="assetId" value="' . $asset->getId . '" />'
. '<input type="hidden" name="method" value="copy" />'
. '<input type="submit" name="with" value="Children" />'
. '<input type="submit" name="with" value="Descendants" />'
. '</form>'
;
my $f = $self->getForm( 'copy' );
$f->addField( 'submit', name => 'with', value => 'Children' );
$f->addField( 'submit', name => 'with', value => 'Descendants' );
return $f->toHtml;
}
#----------------------------------------------------------------------------
=head2 www_copy ( $asset )
=head2 www_copy ()
Perform the copy operation, showing the progress.
Perform the copy operation in a fork
=cut
sub www_copy {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
$asset->forkWithStatusPage({
plugin => 'ProgressTree',
title => 'Copy Assets',
method => 'copyInFork',
dialog => 1,
message => 'Your assets are now copied!',
args => {
childrenOnly => $session->form->get('with') eq 'children',
assetId => $asset->getId,
}
}
my $childrenOnly = 1 if lc $session->form->get('with') eq 'children';
# Should we autocommit?
my $commit = $session->setting->get('versionTagMode') eq 'autoCommit';
# Fork the copy. Forking makes sure it won't get interrupted
my $fork = WebGUI::Fork->start(
$session, blessed( $self ), 'copyBranch', { childrenOnly => $childrenOnly, assetId => $asset->getId, commit => $commit },
);
return {
forkId => $fork->getId,
};
}
#-------------------------------------------------------------------
=head2 copyBranch ( $process, $args )
Perform the copy stuff in a forked process
=cut
sub copyBranch {
my ($process, $args) = @_;
my $session = $process->session;
my $asset = WebGUI::Asset->newById($session, $args->{assetId});
# Get the assets we need to duplicate
my $assetIds = [];
if ( $args->{childrenOnly} ) {
$assetIds = $asset->getLineage(['children']);
}
else {
$assetIds = $asset->getLineage(['descendants']);
}
my $tree = WebGUI::ProgressTree->new($session, $assetIds );
$process->update(sub { $tree->json });
my $newAsset = $asset->duplicateBranch( $args->{childrenOnly} ? 1 : 0, 'clipboard' );
# If we aren't committing, add to a tag
if ( !$args->{commit} ) {
$newAsset->update({
status => "pending",
tagId => WebGUI::VersionTag->getWorking( $session )->getId,
});
}
$newAsset->update({ title => $newAsset->getTitle . ' (copy)'});
$tree->success($asset->getId);
$process->update(sub { $tree->json });
}
1;

View file

@ -32,15 +32,16 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ( )
Create a shortcut to the asset on the clipboard.
=cut
sub process {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $session = $self->session;
my $asset = $self->asset;
my $i18n = WebGUI::International->new( $session, 'WebGUI' );
return { error => $i18n->get('39') } if !$asset->canView;

View file

@ -34,7 +34,7 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ()
Cuts the asset to the clipboard. If the user cannot edit the asset, or the asset is a
system asset, it returns an error message.
@ -42,8 +42,9 @@ system asset, it returns an error message.
=cut
sub process {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, 'WebGUI');
if (! $asset->canEdit) {

View file

@ -33,14 +33,15 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ()
Fork the Delete operation
=cut
sub process {
my ($self, $asset) = @_;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, 'WebGUI');

View file

@ -33,14 +33,15 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ()
Fork the duplicate operation
=cut
sub process {
my ($self, $asset) = @_;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
# Should we autocommit?

View file

@ -34,15 +34,16 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ()
Opens a new tab for displaying the form and the output for editing a branch.
=cut
sub process {
my ($class, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, "Asset");
if (! $asset->canEdit) {
return {
@ -51,7 +52,7 @@ sub process {
}
return {
openDialog => '?op=assetHelper;className=' . $class . ';method=editBranch;assetId=' . $asset->getId,
openDialog => $self->getUrl( 'editBranch' ),
};
}
@ -64,8 +65,9 @@ Creates a tabform to edit the Asset Tree. If canEdit returns False, returns insu
=cut
sub www_editBranch {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my ( $style, $url ) = $session->quick( qw( style url ) );
$style->setCss( $url->extras('hoverhelp.css'));
$style->setScript( $url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js') );
@ -312,8 +314,9 @@ Verifies proper inputs in the Asset Tree and saves them. Returns ManageAssets me
=cut
sub www_editBranchSave {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
return $session->privilege->insufficient() unless ($asset->canEdit && $session->user->isInGroup('4'));
my $form = $session->form;
my %data;

View file

@ -34,15 +34,16 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ()
Opens a new tab for displaying the form and the output for exporting a branch.
=cut
sub process {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, "Asset");
if (! $asset->canEdit) {
return {
@ -51,7 +52,7 @@ sub process {
}
return {
openDialog => '?op=assetHelper;helperId=' . $self->id . ';method=export;assetId=' . $asset->getId,
openDialog => $self->getUrl( 'export' ),
};
}
@ -64,8 +65,9 @@ Displays the export page administrative interface
=cut
sub www_export {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
return $session->privilege->insufficient() unless ($session->user->isInGroup(13));
my ( $style, $url ) = $session->quick(qw{ style url });
$style->setCss( $url->extras('hoverhelp.css'));
@ -79,14 +81,7 @@ sub www_export {
ENDHTML
my $i18n = WebGUI::International->new($session, "Asset");
my $f = WebGUI::FormBuilder->new($session, action => $asset->getUrl);
$f->addField( "hidden", name => 'op', value => 'assetHelper' );
$f->addField( "hidden", name => 'helperId', value => $self->id );
$f->addField( "hidden", name => 'assetId', value => $asset->getId );
$f->addField( "hidden",
name => "method",
value => "exportStatus"
);
my $f = $self->getForm( 'exportStatus' );
$f->addField( "integer",
label => $i18n->get('Depth'),
hoverHelp => $i18n->get('Depth description'),
@ -155,8 +150,9 @@ Displays the export status page
=cut
sub www_exportStatus {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
return $session->privilege->insufficient
unless $session->user->isInGroup(13);
my $form = $session->form;

View file

@ -33,7 +33,7 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $asset )
=head2 process ()
Locks the asset with a version tag. If the user cannot edit the asset, or the asset is
already locked, it returns an error message.
@ -41,8 +41,9 @@ already locked, it returns an error message.
=cut
sub process {
my ($self, $asset) = @_;
my $session = $asset->session;
my ($self) = @_;
my $asset = $self->asset;
my $session = $self->session;
my $i18n = WebGUI::International->new($session, 'Asset');
if (! $asset->canEdit) {

View file

@ -89,11 +89,11 @@ sub handler {
my $helperId = $session->form->get('helperId');
my $class = $asset->getHelpers->{ $helperId }->{ className };
WebGUI::Pluggable::load( $class );
my $helper = $class->new( id => $helperId, session => $session );
my $helper = $class->new( id => $helperId, session => $session, asset => $asset );
my $method = $session->form->get('method') || "view";
if ( $helper->can( "www_" . $method ) ) {
return $helper->can( "www_" . $method )->( $helper, $asset );
return $helper->can( "www_" . $method )->( $helper );
}
else {
$session->log->error( sprintf 'Invalid asset helper "%s" calling method "%s"', $helperId, $method );