From 8cbf232e7210f2d0034402872fc105b6a303d96b Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 12 Mar 2009 05:08:03 +0000 Subject: [PATCH] Adding Asset History content manager. --- docs/upgrades/upgrade_7.6.14-7.7.0.pl | 30 ++++ etc/WebGUI.conf.original | 8 + lib/WebGUI/Content/AssetHistory.pm | 165 ++++++++++++++++++ lib/WebGUI/Operation/Settings.pm | 1 + lib/WebGUI/i18n/English/Asset.pm | 6 + .../build/assetHistory/assetHistory.js | 142 +++++++++++++++ 6 files changed, 352 insertions(+) create mode 100644 lib/WebGUI/Content/AssetHistory.pm create mode 100644 www/extras/yui-webgui/build/assetHistory/assetHistory.js diff --git a/docs/upgrades/upgrade_7.6.14-7.7.0.pl b/docs/upgrades/upgrade_7.6.14-7.7.0.pl index c3902cae3..c2faa3b62 100644 --- a/docs/upgrades/upgrade_7.6.14-7.7.0.pl +++ b/docs/upgrades/upgrade_7.6.14-7.7.0.pl @@ -37,6 +37,7 @@ addGroupToAddToMatrix( $session ); addScreenshotTemplatesToMatrix( $session ); surveyDoAfterTimeLimit($session); surveyRemoveResponseTemplate($session); +installAssetHistory($session); # Passive Analytics pa_installLoggingTables($session); @@ -91,6 +92,35 @@ sub surveyRemoveResponseTemplate { print "DONE!\n" unless $quiet; } +#---------------------------------------------------------------------------- +sub installAssetHistory { + my $session = shift; + print "\tAdding Asset History content handler... \n" unless $quiet; + ##Content Handler + my $contentHandlers = $session->config->get('contentHandlers'); + if (! isIn('WebGUI::Content::Handler', @{ $contentHandlers }) ) { + my @newHandlers = (); + foreach my $handler (@{ $contentHandlers }) { + push @newHandlers, $handler; + push @newHandlers, 'WebGUI::Content::AssetHistory' if + $handler eq 'WebGUI::Content::Account'; + } + $session->config->set('contentHandlers', \@newHandlers); + } + ##Admin Console + $session->config->addToHash('adminConsole', 'assetHistory', { + "icon" => "assets.gif", + "groupSetting" => "groupIdAdminHistory", + "uiLevel" => 5, + "url" => "^PageUrl(\"\",op=assetHistory);", + "title" => "^International(assetHistory,Asset);" + }); + ##Setting for custom group + $session->setting->add('groupIdAdminHistory', 12); + print "Done.\n" unless $quiet; +} + +#---------------------------------------------------------------------------- sub pa_installLoggingTables { my $session = shift; print "\tInstall logging tables... "; diff --git a/etc/WebGUI.conf.original b/etc/WebGUI.conf.original index 8d553f304..0fd8fd04c 100644 --- a/etc/WebGUI.conf.original +++ b/etc/WebGUI.conf.original @@ -331,6 +331,13 @@ "url" : "^PageUrl(\"\",op=assetManager);", "title" : "^International(assets,Asset);" }, + "assetHistory" : { + "icon" : "assets.gif", + "groupSetting" : "groupIdAdminHistory", + "uiLevel" : 5, + "url" : "^PageUrl(\"\",op=assetHistory);", + "title" : "^International(assetHistory,Asset);" + }, "graphics" : { "icon" : "graphics.gif", "uiLevel" : 5, @@ -895,6 +902,7 @@ "WebGUI::Content::AssetDiscovery", "WebGUI::Content::AjaxI18N", "WebGUI::Content::Account", + "WebGUI::Content::AssetHistory", "WebGUI::Content::Operation", "WebGUI::Content::Setup", "WebGUI::Content::Shop", diff --git a/lib/WebGUI/Content/AssetHistory.pm b/lib/WebGUI/Content/AssetHistory.pm new file mode 100644 index 000000000..2b47ff5a7 --- /dev/null +++ b/lib/WebGUI/Content/AssetHistory.pm @@ -0,0 +1,165 @@ +package WebGUI::Content::AssetHistory; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2009 Plain Black Corporation. + ------------------------------------------------------------------- + Please read the legal notices (docs/legal.txt) and the license + (docs/license.txt) that came with this distribution before using + this software. + ------------------------------------------------------------------- + http://www.plainblack.com info@plainblack.com + ------------------------------------------------------------------- + +=cut + +use strict; + + +=head1 NAME + +Package WebGUI::Content::AssetHistory + +=head1 DESCRIPTION + +Give the admins an interface to view the history of assets on their site. + +=head1 SYNOPSIS + + use WebGUI::Content::AssetHistory; + my $output = WebGUI::Content::AssetHistory::handler($session); + +=head1 SUBROUTINES + +These subroutines are available from this package: + +=cut + +#------------------------------------------------------------------- + +=head2 handler ( session ) + +The content handler for this package. + +=cut + +sub handler { + my ($session) = @_; + return undef unless ($session->form->get('op') eq 'assetHistory'); + my $method = $session->form->get( 'method' ) + ? 'www_' . $session->form->get( 'method' ) + : 'www_view' + ; + + # Validate the method name + if ( !__PACKAGE__->can( $method ) ) { + return "Invalid method"; + } + else { + return __PACKAGE__->can( $method )->( $session ); + } + my $output = ""; + # ... + return $output; +} + +#------------------------------------------------------------------- + +=head2 www_getHistoryAsJson ( ) + +Servers side pagination for asset history data displayed in a YUI DataTable. + +=cut + +sub www_getHistoryAsJson { + my ($session) = @_; + return $session->privilege->insufficient + unless $session->user->isInGroup(12); + my ($db, $form) = $session->quick(qw(db form)); + my $startIndex = $form->get('startIndex') || 0; + my $numberOfResults = $form->get('results') || 25; + my %goodKeys = qw/assetId 1 url 1 username 1 dateStamp 1/; + my $sortKey = $form->get('sortKey'); + $sortKey = $goodKeys{$sortKey} == 1 ? $sortKey : 'dateStamp'; + my $sortDir = $form->get('sortDir'); + $sortDir = lc($sortDir) eq 'desc' ? 'desc' : 'asc'; + my @placeholders = (); + my $sql = <get("keywords"); + if ($keywords ne "") { + $db->buildSearchQuery(\$sql, \@placeholders, $keywords, [qw{url assetId username}]) + } + push(@placeholders, $startIndex, $numberOfResults); + $sql .= sprintf (" order by %s limit ?,?","$sortKey $sortDir"); + my %results = (); + my @records = (); + my $sth = $db->read($sql, \@placeholders); + while (my $record = $sth->hashRef) { + push(@records,$record); + } + $results{'recordsReturned'} = $sth->rows()+0; + $sth->finish; + $results{'records'} = \@records; + $results{'totalRecords'} = $db->quickScalar('select found_rows()')+0; ##Convert to numeric + $results{'startIndex'} = $startIndex; + $results{'sort'} = undef; + $results{'dir'} = $sortDir; + $session->http->setMimeType('application/json'); + my $json = JSON::to_json(\%results); + return $json; +} + +#------------------------------------------------------------------- + +=head2 www_view + +YUI DataTable for browsing asset history. + +=cut + +sub www_view { + my $session = shift; + return $session->privilege->insufficient + unless $session->user->isInGroup(12); + ##YUI specific datatable CSS + my $ac = WebGUI::AdminConsole->new( $session, "assets", { + showAdminBar => 1 + } ); + my ($style, $url) = $session->quick(qw(style url)); + $style->setLink($url->extras('/yui/build/fonts/fonts-min.css'), {rel=>'stylesheet', type=>'text/css'}); + $style->setLink($url->extras('yui/build/datatable/assets/skins/sam/datatable.css'), {rel=>'stylesheet', type => 'text/CSS'}); + $style->setLink($url->extras('yui/build/paginator/assets/skins/sam/paginator.css'), {rel=>'stylesheet', type => 'text/CSS'}); + $style->setScript($url->extras('/yui/build/utilities/utilities.js'), {type=>'text/javascript'}); + $style->setScript($url->extras('yui/build/json/json-min.js'), {type => 'text/javascript'}); + $style->setScript($url->extras('yui/build/paginator/paginator-min.js'), {type => 'text/javascript'}); + $style->setScript($url->extras('yui/build/datasource/datasource-min.js'), {type => 'text/javascript'}); + ##YUI Datatable + $style->setScript($url->extras('yui/build/datatable/datatable-min.js'), {type => 'text/javascript'}); + ##WebGUI YUI AssetHistory + $style->setScript( $url->extras( 'yui-webgui/build/i18n/i18n.js' ), {type => 'text/javascript'} ); + $style->setScript( $url->extras('yui-webgui/build/assetHistory/assetHistory.js'), {type => 'text/javascript'}); + ##Default CSS + $style->setRawHeadTags(''); + my $i18n=WebGUI::International->new($session); + + my $output; + + $output .= q| +
+ +
+
+ +|; + + return $ac->render( $output ); +} + +1; + +#vim:ft=perl diff --git a/lib/WebGUI/Operation/Settings.pm b/lib/WebGUI/Operation/Settings.pm index b1714659a..41677cc83 100644 --- a/lib/WebGUI/Operation/Settings.pm +++ b/lib/WebGUI/Operation/Settings.pm @@ -485,6 +485,7 @@ sub definition { groupIdAdminGraphics groupIdAdminGroup groupIdAdminGroupAdmin + groupIdAdminHistory groupIdAdminHelp groupIdAdminLDAPLink groupIdAdminLoginHistory diff --git a/lib/WebGUI/i18n/English/Asset.pm b/lib/WebGUI/i18n/English/Asset.pm index 257e78871..e165b1f71 100644 --- a/lib/WebGUI/i18n/English/Asset.pm +++ b/lib/WebGUI/i18n/English/Asset.pm @@ -1190,6 +1190,12 @@ Couldn't open %-s because %-s
context => q{Asset Manager label, as in "locked by admin"}, }, + 'assetHistory' => { + message => q{Asset History}, + lastUpdated => 0, + context => q{Admin Console label. Shows the history of assets in this site.}, + }, + }; 1; diff --git a/www/extras/yui-webgui/build/assetHistory/assetHistory.js b/www/extras/yui-webgui/build/assetHistory/assetHistory.js new file mode 100644 index 000000000..33dd31c79 --- /dev/null +++ b/www/extras/yui-webgui/build/assetHistory/assetHistory.js @@ -0,0 +1,142 @@ +/*** The WebGUI Asset History Viewer + * Requires: YAHOO, Dom, Event + * With all due credit to Doug Bell, who wrote the AssetManager. AssetHistory + * is a blatant copy/paste/modify of it. + */ + +if ( typeof WebGUI == "undefined" ) { + WebGUI = {}; +} +if ( typeof WebGUI.AssetHistory == "undefined" ) { + WebGUI.AssetHistory = {}; +} + +/*--------------------------------------------------------------------------- + WebGUI.AssetHistory.DefaultSortedBy ( ) +*/ +WebGUI.AssetHistory.DefaultSortedBy = { + "key" : "dateStamp", + "dir" : YAHOO.widget.DataTable.CLASS_ASC +}; + +/*--------------------------------------------------------------------------- + WebGUI.AssetHistory.BuildQueryString ( ) +*/ +WebGUI.AssetHistory.BuildQueryString = function ( state, dt ) { + var query = "startIndex=" + state.pagination.recordOffset + + ';sortDir=' + ((state.sortedBy.dir === YAHOO.widget.DataTable.CLASS_DESC) ? "DESC" : "ASC") + + ';results=' + state.pagination.rowsPerPage + + ';sortKey=' + state.sortedBy.key + ; + return query; + }; + +/*--------------------------------------------------------------------------- + WebGUI.AssetHistory.formatDate ( ) + Format the date the asset was modified. +*/ +WebGUI.AssetHistory.formatDate = function ( elCell, oRecord, oColumn, orderNumber ) { + var actionDate = new Date( 1000 * oRecord.getData('dateStamp') ); + var formattedDate = YAHOO.util.Date.format(actionDate, { format: '%x %X' }); + elCell.innerHTML = formattedDate; +}; + + +/*--------------------------------------------------------------------------- + WebGUI.AssetHistory.initManager ( ) + Initialize the i18n interface +*/ +WebGUI.AssetHistory.initManager = function (o) { + WebGUI.AssetHistory.i18n + = new WebGUI.i18n( { + namespaces : { + 'WebGUI' : [ + "50", + "104", + "352" + ] + }, + onpreload : { + fn : WebGUI.AssetHistory.initDataTable + } + } ); +}; + +/*--------------------------------------------------------------------------- + WebGUI.AssetHistory.initDataTable ( ) + Initialize the www_manage page +*/ +WebGUI.AssetHistory.initDataTable = function (o) { + var historyPaginator = new YAHOO.widget.Paginator({ + containers : ['pagination'], + pageLinks : 7, + rowsPerPage : 25, + template : "{CurrentPageReport} {PreviousPageLink} {PageLinks} {NextPageLink}" + }); + + + // initialize the data source + WebGUI.AssetHistory.DataSource + = new YAHOO.util.DataSource( '?op=assetHistory;method=getHistoryAsJson;',{connTimeout:30000} ); + WebGUI.AssetHistory.DataSource.responseType + = YAHOO.util.DataSource.TYPE_JSON; + WebGUI.AssetHistory.DataSource.responseSchema + = { + resultsList: 'records', + fields: [ + { key: 'assetId', parser: 'string' }, + { key: 'username', parser: 'string' }, + { key: 'dateStamp', parser: 'number' }, + { key: 'title', parser: 'string' }, + { key: 'actionTaken', parser: 'string' }, + { key: 'url', parser: 'string' } + ], + metaFields: { + totalRecords: "totalRecords" // Access to value in the server response + } + }; + WebGUI.AssetHistory.ColumnDefs = [ // sortable:true enables sorting + {key:"assetId", label:"assetId", sortable: true}, + {key:"username", label:WebGUI.AssetHistory.i18n.get('WebGUI', '50' ), sortable: true}, + {key:"dateStamp", label:WebGUI.AssetHistory.i18n.get('WebGUI', '352'), sortable: true, formatter: WebGUI.AssetHistory.formatDate}, + {key:"url", label:WebGUI.AssetHistory.i18n.get('WebGUI', '104'), sortable: true}, + {key:"actionTaken", label:"actionTaken"} + ]; + + + // Initialize the data table + WebGUI.AssetHistory.DataTable + = new YAHOO.widget.DataTable( 'dynamicdata', + WebGUI.AssetHistory.ColumnDefs, + WebGUI.AssetHistory.DataSource, + { + initialRequest : 'startIndex=0;results=25', + dynamicData : true, + paginator : historyPaginator, + sortedBy : WebGUI.AssetHistory.DefaultSortedBy, + generateRequest : WebGUI.AssetHistory.BuildQueryString + } + ); + + WebGUI.AssetHistory.DataTable.handleDataReturnPayload = function(oRequest, oResponse, oPayload) { + oPayload.totalRecords = oResponse.meta.totalRecords; + return oPayload; + } + + //Setup the form to submit an AJAX request back to the site. + YAHOO.util.Dom.get('keywordSearchForm').onsubmit = function () { + var state = WebGUI.AssetHistory.DataTable.getState(); + state.pagination.recordOffset = 0; + WebGUI.AssetHistory.DataSource.sendRequest( + 'keywords=' + YAHOO.util.Dom.get('keywordsField').value + ';startIndex=0;results=25', + { + success : WebGUI.AssetHistory.DataTable.onDataReturnInitializeTable, + scope : WebGUI.AssetHistory.DataTable, argument:state + } + ); + return false; + }; + +}; + +