change AssetHelpers to have short IDs instead of class names

This will make better code reuse: The Asset Manager will instead
call the AssetHelper with the id of "cut" or "copy" to get the
correct operation.
This commit is contained in:
Doug Bell 2011-02-25 15:44:06 -06:00
parent f249070c50
commit b9e879b7aa
22 changed files with 156 additions and 115 deletions

View file

@ -656,10 +656,13 @@ sub www_processAssetHelper {
my ( $form ) = $session->quick(qw{ form });
my $assetId = $form->get('assetId');
my $class = $form->get('className');
WebGUI::Pluggable::load( $class );
my $helperId = $form->get('helperId');
my $asset = WebGUI::Asset->newById( $session, $assetId );
return JSON->new->encode( $class->process( $asset ) );
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 ) );
}
#----------------------------------------------------------------------

View file

@ -1191,61 +1191,56 @@ sub getHelpers {
my $session = $self->session;
my ( $conf ) = $session->quick(qw{ config });
my $default = [
{
class => 'WebGUI::AssetHelper::ChangeUrl',
my $default = {
change_url => {
className => 'WebGUI::AssetHelper::ChangeUrl',
label => 'Change URL',
},
{
class => 'WebGUI::AssetHelper::Copy',
copy => {
className => 'WebGUI::AssetHelper::Copy',
label => 'Copy',
},
{
class => 'WebGUI::AssetHelper::CopyBranch',
copy_branch => {
className => 'WebGUI::AssetHelper::CopyBranch',
label => 'Copy Branch',
},
{
class => 'WebGUI::AssetHelper::CreateShortcut',
shortcut => {
className => 'WebGUI::AssetHelper::CreateShortcut',
label => 'Create Shortcut',
},
{
class => 'WebGUI::AssetHelper::Cut',
cut => {
className => 'WebGUI::AssetHelper::Cut',
label => 'Cut',
},
{
edit => {
url => $self->getUrl( 'func=edit' ),
label => 'Edit',
},
{
class => 'WebGUI::AssetHelper::EditBranch',
edit_branch => {
className => 'WebGUI::AssetHelper::EditBranch',
label => 'Edit Branch',
},
{
class => 'WebGUI::AssetHelper::ExportHtml',
export_html => {
className => 'WebGUI::AssetHelper::ExportHtml',
label => 'Export As HTML',
},
{
view => {
url => $self->getUrl( 'func=view' ),
label => 'View',
},
{
class => 'WebGUI::AssetHelper::Lock',
lock => {
className => 'WebGUI::AssetHelper::Lock',
label => 'Lock',
},
];
};
# Merge additional helpers for this class from config
my $confHelpers = $conf->get('assets/' . $self->className . '/helpers') || {};
$default = { %$default, %$confHelpers };
# Get additional helpers for this class from config
my $confHelpers = $conf->get('assets/' . $self->className . '/helpers');
# Merge on label
for my $helper ( @$confHelpers ) {
# Process macros in labels
for my $helper ( values %$default ) {
WebGUI::Macro::process( \$helper->{label} );
if ( my $replace = first { $_->{label} eq $helper->{label} } @$default ) {
$replace = $helper; # replace in the default arrayref
}
else {
push @$default, $helper;
}
}
return $default;

View file

@ -23,7 +23,38 @@ the Admin Console.
=head1 SYNOPSIS
Despite using OO style methods, there are no AssetHelper objects. This is simply to provide inheritance.
package MyHelper;
use base 'WebGUI::AssetHelper';
sub process {
my ( $self, $asset ) = @_;
# Do stuff
return { message => 'Done stuff!' };
}
sub www_doOtherStuff {
my ( $self, $asset ) = @_;
# Do other stuff
}
=head1 ATTRIBUTES
=cut
use WebGUI::BestPractices;
use Moose;
has 'session' => (
is => 'ro',
required => 1,
isa => 'WebGUI::Session',
);
has 'id' => (
is => 'ro',
required => 1,
isa => 'Str',
);
=head1 METHODS
@ -33,16 +64,12 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ( $asset )
This is a class method. Process is the default method called by the Asset Helper content handler.
Process is the default method called by the Admin Console.
It returns a hashref, that is converted by the content handler to JSON to be passed back to the
Admin Console.
=head3 $class
The name of the class this method was called as.
=head3 $asset
A WebGUI::Asset object.
@ -82,14 +109,14 @@ An array reference of arguments that, when used with C<scriptMethod>, will be pa
=cut
sub process {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
##This method can do work, or it can delegate out to other methods.
return {
error => q{User, we have a problem.},
message => q{A friendly informational method},
open_tab => '?op=assetHelper;className=WebGUI::AssetHelper;method=editBranch',
open_tab => '?op=assetHelper;helperId=' . $self->id . ';method=editBranch',
redirect => '/home',
scriptFile => q{URL},
scriptMethod => q{methodName},

View file

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

View file

@ -32,30 +32,30 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ( $asset )
Open a progress dialog for the copy operation
=cut
sub process {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
return {
openDialog => '?op=assetHelper;className=' . $class . ';method=copy;assetId=' . $asset->getId,
openDialog => '?op=assetHelper;helperId=' . $self->id . ';method=copy;assetId=' . $asset->getId,
};
}
#----------------------------------------------------------------------------
=head2 www_copy ( $class, $asset )
=head2 www_copy ( $asset )
Perform the copy operation, showing the progress.
=cut
sub www_copy {
my ( $class, $asset ) = @_;
my ( $self, $asset ) = @_;
my $session = $asset->session;
my $i18n = WebGUI::International->new($session, 'Asset');

View file

@ -32,36 +32,36 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ( $asset )
Open a progress dialog for the copy operation
=cut
sub process {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
return {
openDialog => '?op=assetHelper;className=' . $class . ';method=getWith;assetId=' . $asset->getId
openDialog => '?op=assetHelper;helperId=' . $self->id . ';method=getWith;assetId=' . $asset->getId
};
}
#----------------------------------------------------------------------------
=head2 www_getWith ( $class, $asset )
=head2 www_getWith ( $asset )
Get the "with" configuration. "Descendants" or "Children".
=cut
sub www_getWith {
my ( $class, $asset ) = @_;
my ( $self, $asset ) = @_;
my $session = $asset->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="className" value="' . $class . '" />'
. '<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" />'
@ -73,14 +73,14 @@ sub www_getWith {
#----------------------------------------------------------------------------
=head2 www_copy ( $class, $asset )
=head2 www_copy ( $asset )
Perform the copy operation, showing the progress.
=cut
sub www_copy {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
$asset->forkWithStatusPage({

View file

@ -32,14 +32,14 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ( $asset )
Create a shortcut to the asset on the clipboard.
=cut
sub process {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
my $i18n = WebGUI::International->new( $session, 'WebGUI' );

View file

@ -32,7 +32,7 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ( $asset )
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.
@ -40,7 +40,7 @@ system asset, it returns an error message.
=cut
sub process {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
my $i18n = WebGUI::International->new($session, 'WebGUI');
@ -52,20 +52,20 @@ sub process {
}
return {
openDialog => '?op=assetHelper;className=' . $class . ';method=cut;assetId=' . $asset->getId,
openDialog => '?op=assetHelper;helperId=' . $self->id . ';method=cut;assetId=' . $asset->getId,
};
}
#----------------------------------------------------------------------------
=head2 www_cut ( $class, $asset )
=head2 www_cut ( $asset )
Show the progress bar while cutting the asset.
=cut
sub www_cut {
my ( $class, $asset ) = @_;
my ( $self, $asset ) = @_;
my $session = $asset->session;
my $i18n = WebGUI::International->new($session, 'Asset');

View file

@ -64,7 +64,7 @@ Creates a tabform to edit the Asset Tree. If canEdit returns False, returns insu
=cut
sub www_editBranch {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
my ( $style, $url ) = $session->quick( qw( style url ) );
$style->setCss( $url->extras('hoverhelp.css'));
@ -84,7 +84,7 @@ ENDHTML
my $change = '<br />'.$i18n->get("change") . ' ';
my $tabform = WebGUI::TabForm->new($session);
$tabform->hidden({name=>"op",value=>"assetHelper"});
$tabform->hidden({name=>"className",value=>$class});
$tabform->hidden({name=>"helperId",value=>$self->id});
$tabform->hidden({name=>"method",value=>"editBranchSave"});
$tabform->hidden({name=>"assetId",value=>$asset->getId});
$tabform->addTab("properties",$i18n->get("properties"),9);
@ -312,7 +312,7 @@ Verifies proper inputs in the Asset Tree and saves them. Returns ManageAssets me
=cut
sub www_editBranchSave {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
return $session->privilege->insufficient() unless ($asset->canEdit && $session->user->isInGroup('4'));
my $form = $session->form;

View file

@ -34,14 +34,14 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ( $asset )
Opens a new tab for displaying the form and the output for exporting a branch.
=cut
sub process {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
my $i18n = WebGUI::International->new($session, "Asset");
if (! $asset->canEdit) {
@ -51,7 +51,7 @@ sub process {
}
return {
openDialog => '?op=assetHelper;className=' . $class . ';method=export;assetId=' . $asset->getId,
openDialog => '?op=assetHelper;helperId=' . $self->id . ';method=export;assetId=' . $asset->getId,
};
}
@ -64,7 +64,7 @@ Displays the export page administrative interface
=cut
sub www_export {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
return $session->privilege->insufficient() unless ($session->user->isInGroup(13));
my ( $style, $url ) = $session->quick(qw{ style url });
@ -81,7 +81,7 @@ 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 => 'className', value => $class );
$f->addField( "hidden", name => 'helperId', value => $self->id );
$f->addField( "hidden", name => 'assetId', value => $asset->getId );
$f->addField( "hidden",
name => "method",
@ -155,7 +155,7 @@ Displays the export status page
=cut
sub www_exportStatus {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
return $session->privilege->insufficient
unless $session->user->isInGroup(13);

View file

@ -33,7 +33,7 @@ These methods are available from this class:
#-------------------------------------------------------------------
=head2 process ( $class, $asset )
=head2 process ( $asset )
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,7 +41,7 @@ already locked, it returns an error message.
=cut
sub process {
my ($class, $asset) = @_;
my ($self, $asset) = @_;
my $session = $asset->session;
my $i18n = WebGUI::International->new($session, 'Asset');

View file

@ -83,14 +83,20 @@ sub handler {
if ( $session->form->get("op") eq "assetHelper" ) {
# Load and run the requested asset helper www_ method
my $class = $session->form->get('className');
WebGUI::Pluggable::load( $class );
my $method = $session->form->get('method') || "view";
my $assetId = $session->form->get('assetId');
my $asset = WebGUI::Asset->newById( $session, $assetId );
if ( $class->can( "www_" . $method ) ) {
return $class->can( "www_" . $method )->( $class, $asset );
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 $method = $session->form->get('method') || "view";
if ( $helper->can( "www_" . $method ) ) {
return $helper->can( "www_" . $method )->( $helper, $asset );
}
else {
$session->log->error( sprintf 'Invalid asset helper "%s" calling method "%s"', $helperId, $method );
}
}