package WebGUI::Content::AssetManager;
use strict;
use JSON qw( from_json to_json );
use URI;
use WebGUI::Form;
use WebGUI::Paginator;
use WebGUI::Utility;
use WebGUI::Macro::AdminBar;
#----------------------------------------------------------------------------
=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 .= '
};
tie my %options, 'Tie::IxHash';
my $hasClips = 0;
my $clipNum = 0;
foreach my $asset (@{$currentAsset->getAssetsInClipboard(1)}) {
$options{$asset->getId} = ' '.$asset->getTitle;
$hasClips = 1;
$clipNum++;
}
if ($hasClips) {
$output .= ' '
.'';
}
## Packages
$output .= '';
### Clearing div
$output .= q{
};
$output .= q{
};
### Write the JavaScript that will take over
$output .= '
ENDJS
return $ac->render( $output );
}
#----------------------------------------------------------------------------
=head2 www_search ( session )
Search assets underneath this asset.
=cut
sub www_search {
my $session = shift;
my $ac = WebGUI::AdminConsole->new( $session, "assets" );
my $i18n = WebGUI::International->new( $session, "Asset" );
my $currentAsset = getCurrentAsset($session);
my $output = '
' . getHeader( $session );
$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' ) );
### Show the form
$output .= q{}
;
### Actions
if ( my $action = lc $session->form->get( 'action' ) ) {
my @assetIds = $session->form->get( 'assetId' );
if ( $action eq "delete" ) { ##aka trash
for my $assetId ( @assetIds ) {
my $asset = WebGUI::Asset->newById( $session, $assetId );
next unless $asset;
$asset->trash;
}
}
elsif ( $action eq "cut" ) {
for my $assetId ( @assetIds ) {
my $asset = WebGUI::Asset->newById( $session, $assetId );
next unless $asset;
$asset->cut;
}
}
elsif ( $action eq "copy" ) {
for my $assetId ( @assetIds ) {
# Copy == Duplicate + Cut
my $asset = WebGUI::Asset->newById( $session, $assetId);
my $newAsset = $asset->duplicate( { skipAutoCommitWorkflows => 1 } );
$newAsset->update( { title => $newAsset->getTitle . ' (copy)' } );
$newAsset->cut;
}
}
}
### Run the search
if ( $session->form->get( 'keywords' ) || $session->form->get( 'class' ) ) {
my $keywords = $session->form->get( 'keywords' );
my @classes = $session->form->get( 'class' );
# Detect a helper word key
my @assetIds = ($keywords =~ /assetid:\s*([^\s]+)/gi);
# purge helper word keys
if (@assetIds) {
$keywords =~ s/\bassetid:\s*[^\s]+//gi;
}
$keywords =~ s/^\s+//g;
$keywords =~ s/\s+$//g;
my $p = getSearchPaginator( $session, {
assetIds => \@assetIds,
keywords => $keywords,
classes => \@classes,
orderByColumn => $session->form->get( 'orderByColumn' ),
orderByDirection => $session->form->get( 'orderByDirection' ),
} );
if ( $p->getRowCount == 0 ) {
$output .= q{