package WebGUI::Content::AssetManager;
use strict;
use URI;
use WebGUI::Form;
use WebGUI::Paginator;
use WebGUI::Utility;
#----------------------------------------------------------------------------
=head2 getClassSelectBox ( session )
Gets a select box to choose a class name.
=cut
sub getClassSelectBox {
my $session = shift;
tie my %classes, "Tie::IxHash", (
"" => "Any Class",
$session->db->buildHash("select distinct(className) from asset"),
);
delete $classes{"WebGUI::Asset"}; # don't want to search for the root asset
return WebGUI::Form::selectBox( $session, {
name => "class",
value => $session->form->process("class","className"),
defaultValue => "",
options => \%classes,
});
}
#----------------------------------------------------------------------------
=head2 getCurrentAsset ( session )
Returns the asset we would be looking at if we weren't looking at the Asset
Manager.
=cut
sub getCurrentAsset {
my $session = shift;
return WebGUI::Asset->newByUrl( $session );
}
#----------------------------------------------------------------------------
=head2 getHeader ( session )
Get a header to pick "Manage" or "Search". Add other things later maybe?
=cut
sub getHeader {
my $session = shift;
my $output = '';
my $i18n = WebGUI::International->new( $session, "Asset" );
if ( $session->form->get( 'method' ) eq "search" ) {
$output .= '
}
. q{}
;
return sprintf $more_markup, @more_fields;
}
#----------------------------------------------------------------------------
=head2 getOrderLink ( session, column, label )
Gets a link to order the results based on a column. Uses some magick
to ensure proper working no matter where we are.
=cut
sub getOrderLink {
my $session = shift;
my $column = shift;
my $label = shift;
my $columnParam = "orderByColumn";
my $directionParam = "orderByDirection";
my $url = URI->new( $session->env->get( "REQUEST_URI" ) );
my $query = $url->query;
# Split query string into param => value hash
my %query = map { /(.+)=(.+)/; $1 => $2 } split /[;&]/, $query;
# Delete unnecessary keys
delete $query{ 'assetId' };
# Add necessary keys
$query{ $columnParam } = $column;
if ( $session->form->get( $columnParam ) eq $column && $session->form->get( $directionParam ) eq "ASC" ) {
$query{ $directionParam } = "DESC";
}
else {
$query{ $directionParam } = "ASC";
}
$url->query_form( %query );
return q{}
. $label
. q{}
;
}
#----------------------------------------------------------------------------
=head2 handler ( session )
Handle the session, if we can. Otherwise pass it on.
Check permissions
=cut
# BAD things about procedural that would be fixed with class methods
# 1) I must use stringy eval to call my method, instead of $class->$method( args )
# 2) I must validate that method using a list of known methods instead of $class->can( $method )
sub handler {
my ( $session ) = @_;
if ( $session->form->get( 'op' ) eq 'assetManager' && getCurrentAsset( $session ) ) {
return $session->privilege->noAccess unless getCurrentAsset( $session )->canEdit;
my $method = $session->form->get( 'method' )
? 'www_' . $session->form->get( 'method' )
: 'www_manage'
;
# Validate the method name
if ( !grep { $_ eq $method } qw( www_manage www_search ) ) {
return "Invalid method";
}
else {
# I hate hate hate stringy eval
return eval "$method" . '($session)';
}
}
else {
return;
}
}
#----------------------------------------------------------------------------
=head2 www_manage ( session )
Show the main screen of the asset manager, paginated. Also load the
JavaScript that will take over if the browser has the cojones.
# BAD things about procedural that would be fixed with class methods
# 3) The "appendSideLinks" method would be better as simply "getAdminStyle" and would do more
=cut
sub www_manage {
my ( $session ) = @_;
my $ac = WebGUI::AdminConsole->new( $session, "assets" );
my $currentAsset = getCurrentAsset( $session );
my $i18n = WebGUI::International->new( $session, "Asset" );
### Do Action
if ( my $action = $session->form->get( 'action' ) ) {
my @assetIds = $session->form->get( 'assetId' );
if ( $action eq "update" ) {
for my $assetId ( @assetIds ) {
my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId );
next unless $asset;
my $rank = $session->form->get( $assetId . '_rank' );
next unless $rank; # There's no such thing as zero
$asset->setRank( $rank );
}
}
elsif ( $action eq "trash" ) {
for my $assetId ( @assetIds ) {
my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId );
next unless $asset;
$asset->trash;
}
}
elsif ( $action eq "cut" ) {
for my $assetId ( @assetIds ) {
my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId );
next unless $asset;
$asset->cut;
}
}
elsif ( $action eq "copy" ) {
for my $assetId ( @assetIds ) {
# Copy == Duplicate + Cut
my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId);
my $newAsset = $asset->duplicate( { skipAutoCommitWorkflows => 1 } );
$newAsset->update( { title => $newAsset->getTitle . ' (copy)' } );
$newAsset->cut;
}
}
elsif ( $action eq "duplicate" ) {
for my $assetId ( @assetIds ) {
my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId );
next unless $asset;
$asset->duplicate( { skipAutoCommitWorkflows => 1 } );
}
}
}
# Show the page
$session->style->setLink( $session->url->extras( 'yui-webgui/build/assetManager/assetManager.css' ), { rel => "stylesheet", type => 'text/css' } );
$session->style->setScript( $session->url->extras( 'yui/build/yahoo-dom-event/yahoo-dom-event.js' ) );
$session->style->setScript( $session->url->extras( 'yui-webgui/build/assetManager/assetManager.js' ) );
$session->style->setScript( $session->url->extras( 'yui-webgui/build/form/form.js' ) );
$session->style->setRawHeadTags( <<'ENDHTML' );
ENDHTML
my $output = '