diff --git a/lib/WebGUI/Content/AssetManager.pm b/lib/WebGUI/Content/AssetManager.pm index dd0049e5b..4285488af 100644 --- a/lib/WebGUI/Content/AssetManager.pm +++ b/lib/WebGUI/Content/AssetManager.pm @@ -2,6 +2,7 @@ package WebGUI::Content::AssetManager; use strict; +use JSON qw( from_json to_json ); use URI; use WebGUI::Form; use WebGUI::Paginator; @@ -97,19 +98,19 @@ sub getManagerPaginator { my $orderByColumn = $session->form->get( 'orderByColumn' ) || "lineage" ; - my $orderByDirection = $session->form->get( 'orderByDirection' ) eq "DESC" + my $orderByDirection = lc $session->form->get( 'orderByDirection' ) eq "desc" ? "DESC" : "ASC" ; - my $p - = WebGUI::Paginator->new( $session, - '?op=assetManager;method=manage;orderByColumn=' . $orderByColumn - . ';orderByDirection=' . $orderByDirection - ); + my $recordOffset = $session->form->get( 'recordOffset' ) || 1; + my $rowsPerPage = $session->form->get( 'rowsPerPage' ) || 15; + my $currentPage = int ( $recordOffset / $rowsPerPage ) + 1; + + my $p = WebGUI::Paginator->new( $session, '', $rowsPerPage, 'pn', $currentPage ); my $orderBy = $session->db->dbh->quote_identifier( $orderByColumn ) . ' ' . $orderByDirection; - $p->setDataByArrayRef( $asset->getLineage( ['children'] ), { orderByClause => $orderBy } ); + $p->setDataByArrayRef( $asset->getLineage( ['children'], { orderByClause => $orderBy } ) ); return $p; } @@ -214,49 +215,6 @@ sub getMoreMenu { #---------------------------------------------------------------------------- -=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. @@ -265,10 +223,6 @@ 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 ) = @_; @@ -282,12 +236,11 @@ sub handler { ; # Validate the method name - if ( !grep { $_ eq $method } qw( www_manage www_search ) ) { + if ( !__PACKAGE__->can( $method ) ) { return "Invalid method"; } else { - # I hate hate hate stringy eval - return eval "$method" . '($session)'; + return __PACKAGE__->can( $method )->( $session ); } } else { @@ -297,14 +250,62 @@ sub handler { #---------------------------------------------------------------------------- +=head2 www_ajaxGetManagerPage ( session ) + +Get a page of Asset Manager data, ajax style. Returns a JSON array to be +formatted in a WebGUI.AssetManager data table. + +=cut + +sub www_ajaxGetManagerPage { + my $session = shift; + my $i18n = WebGUI::International->new( $session, "Asset" ); + my $assetInfo = {}; + my $p = getManagerPaginator( $session ); + + for my $assetId ( @{ $p->getPageData } ) { + my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId ); + + # Populate the required fields to fill in + my %fields = ( + assetId => $asset->getId, + url => $asset->getUrl, + lineage => $asset->get( "lineage" ), + title => $asset->get( "title" ), + revisionDate => $asset->get( "revisionDate" ), + childCount => $asset->getChildCount, + assetSize => $asset->get( 'assetSize' ), + lockedBy => $asset->lockedBy, + canEditIfLocked => $asset->canEditIfLocked, + ); + + $fields{ className } = {}; + # The asset icon + my $icon = [ grep { $_->{ icon } } @{ $asset->definition( $session ) } ]->[ 0 ]->{ icon }; + $fields{ icon } = $session->url->extras( '/assets/small/' . $icon ); + + # The asset type (i18n name) + my $type = [ grep { $_->{ assetName } } @{ $asset->definition( $session ) } ]->[ 0 ]->{ assetName }; + $fields{ className } = $type; + + push @{ $assetInfo->{ assets } }, \%fields; + } + + $assetInfo->{ totalAssets } = $p->getRowCount; + $assetInfo->{ sort } = $session->form->get( 'orderByColumn' ); + $assetInfo->{ dir } = lc $session->form->get( 'orderByDirection' ); + + $session->http->setMimeType( 'application/json' ); + return to_json( $assetInfo ); +} + +#---------------------------------------------------------------------------- + =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 { @@ -360,12 +361,21 @@ sub www_manage { } # Show the page + $session->style->setLink( $session->url->extras('yui/build/datatable/assets/skins/sam/datatable.css'), {rel=>'stylesheet', type=>'text/css'}); $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/build/yahoo/yahoo.js' ) ); + $session->style->setScript( $session->url->extras( 'yui/build/dom/dom.js' ) ); + $session->style->setScript( $session->url->extras( 'yui/build/event/event.js' ) ); + $session->style->setScript( $session->url->extras( 'yui/build/element/element-beta-min.js ' ) ); + $session->style->setScript( $session->url->extras( 'yui/build/connection/connection-min.js ' ) ); + $session->style->setScript( $session->url->extras( 'yui/build/datasource/datasource-beta.js ' ) ); + $session->style->setScript( $session->url->extras( 'yui/build/datatable/datatable-beta.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' ); + my $extras = $session->url->extras; + $session->style->setRawHeadTags( < + WebGUI.AssetManager.extrasUrl = '$extras'; YAHOO.util.Event.onDOMReady( WebGUI.AssetManager.initManager ); ENDHTML @@ -390,129 +400,23 @@ ENDHTML $output .= ''; ### The page of assets - $output .= q{
} + $output .= q{
} + . q{} . q{} . q{} - . q{} - . q{} - . q{} - . q{} # Checkbox column - . q{} # Rank column - . q{} # Edit / More - . q{} # Title - . q{} # Type - . q{} # Revision Date - . q{} # Size - . q{} # Lock - . q{} - . q{} - ; - - # The markup for a single asset - my $row_markup = q{} - . q{} - . q{} - . q{} - . q{} - . q{} - . q{} - . q{} - . q{} - . q{} - ; - - # The field keys to fill in the placeholders - my @row_fields = qw( - alt - assetId - assetId rank - editLink moreMenu - hasChildren url title - iconUrl type - revisionDate - size - url lockIcon - ); - - my $p = getManagerPaginator( $session, { - orderByColumn => $session->form->get( 'orderByColumn' ), - orderByDirection => $session->form->get( 'orderByDirection' ), - } ); - my $count = 0; - for my $assetId ( @{ $p->getPageData } ) { - $count++; - my $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId ); - - # Populate the required fields to fill in - my %fields = ( - alt => ( $count % 3 == 0 ? 'class="alt"' : '' ), - assetId => $asset->getId, - url => $asset->getUrl, - title => $asset->get( "title" ), - revisionDate => $session->datetime->epochToHuman( $asset->get( "revisionDate" ) ), - hasChildren => ( $asset->hasChildren ? "+ " : "  " ), - rank => $asset->getRank, - size => formatBytes( $asset->get( 'assetSize' ) ), - ); - - # The asset icon - my $icon = [ grep { $_->{ icon } } @{ $asset->definition( $session ) } ]->[ 0 ]->{ icon }; - $fields{ iconUrl } = $session->url->extras( '/assets/small/' . $icon ); - - # The asset type (i18n name) - my $type = [ grep { $_->{ assetName } } @{ $asset->definition( $session ) } ]->[ 0 ]->{ assetName }; - $fields{ type } = $type; - - # The lock - if ( $asset->lockedBy ) { # lockedBy in case someone overrides isLocked (like the Collab System Thread ) - $fields{ lockIcon } - = sprintf 'locked by %s', - $session->url->extras( 'assetManager/locked.gif' ), - WebGUI::HTML::format( $asset->lockedBy->username, "text" ), - WebGUI::HTML::format( $asset->lockedBy->username, "text" ), - ; - } - else { - $fields{ lockIcon } - = sprintf 'unlocked', - $session->url->extras( 'assetManager/unlocked.gif' ), - ; - } - - # The edit link - if ( !$asset->lockedBy || $asset->canEditIfLocked ) { - $fields{ editLink } - = sprintf '' . $i18n->get( "edit" ) . ' |', - $asset->getUrl( 'func=edit;proceed=manageAssets' ) - ; - } - - # The More menu - $fields{ moreMenu } = getMoreMenu( $asset, $i18n->get( 'menu label' ) ); - - $output .= sprintf $row_markup, @fields{ @row_fields }; - } - - $output .= q{} - . q{
} . getOrderLink( $session, "lineage", $i18n->get( "rank" ) ) . q{ } . getOrderLink( $session, "title", $i18n->get( "99" ) ) . q{} . getOrderLink( $session, "className", $i18n->get( "type" ) ) . q{} . getOrderLink( $session, "revisionDate", $i18n->get( "last updated" ) ) . q{} . getOrderLink( $session, "assetSize", $i18n->get( "size" ) ) . q{} . getOrderLink( $session, "lockedBy", $i18n->get( "locked" ) ) . q{
%s %s%s%s %s%s%s%s
} - . q{

} . $i18n->get( 'with selected' ) - . q{} - . q{} - . q{} - . q{} - . q{} - . q{

} - . q{} - ; - - ### Page links - $output .= q{}; - - ### Page description - $output .= sprintf q{
} . $i18n->get( 'page indicator' ) . q{
}, - $p->getPageNumber, - $p->getNumberOfPages, + . q{
} + . q{
} + . q{

} . $i18n->get( 'with selected' ) + . q{} + . q{} + . q{} + . q{} + . q{} + . q{

} + . q{} + . q{} + . q{
} ; ### Clearing div @@ -591,6 +495,156 @@ ENDHTML $output .= q{
 
}; $output .= q{}; + ### Write the JavaScript that will take over + $output .= <<'ENDJS'; + +ENDJS + return $ac->render( $output ); }