From 3eb0ac756f9a4d04ea16084ffc32c655223429a1 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sun, 29 Nov 2009 16:03:51 -0800 Subject: [PATCH] Flesh out Revisions assetHelper. Add a simple method to WebGUI::HTML to format an array as a table row. Do not join non-asset tables in queries. --- lib/WebGUI/AssetHelper/Revisions.pm | 50 ++++++++++++++++++++++++++++- lib/WebGUI/HTML.pm | 22 +++++++++++++ t/HTML/addToRow.t | 37 +++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 t/HTML/addToRow.t diff --git a/lib/WebGUI/AssetHelper/Revisions.pm b/lib/WebGUI/AssetHelper/Revisions.pm index 92b33a939..6e09fa715 100644 --- a/lib/WebGUI/AssetHelper/Revisions.pm +++ b/lib/WebGUI/AssetHelper/Revisions.pm @@ -3,6 +3,8 @@ package WebGUI::AssetHelper::Revisions; use strict; use Class::C3; use base qw/WebGUI::AssetHelper/; +use WebGUI::User; +use WebGUI::HTML; =head1 LEGAL @@ -40,10 +42,56 @@ Opens a new tab for displaying revisions of this asset. sub process { my ($class, $asset) = @_; + my $session = $asset->session; + my $i18n = WebGUI::International->new($session, "Asset"); + if (! $asset->canEdit) { + return { + error => $i18n->get('38', 'WebGUI'), + } + } return { - open_tab => $asset->getUrl('func=manageRevisions'), + open_tab => $asset->getUrl('op=assetHelper;className=WebGUI::AssetHelper::Revisions;func=manageRevisions'), }; } +#------------------------------------------------------------------- + +=head2 www_manageRevisions ( $class, $asset ) + +Displays a table of revision data for this asset, along with links to edit each revision, view it, or delete it. + +=cut + +sub www_manageRevisions { + my ($class, $asset) = @_; + my $session = $asset->session; + my $i18n = WebGUI::International->new($session, "Asset"); + if (! $asset->canEdit) { + return { + error => $i18n->get('38', 'WebGUI'), + } + } + my $output = sprintf qq{\n + \n}, + $i18n->get('revision date'), $i18n->get('revised by'), $i18n->get('tag name'); + my $sth = $session->db->read("select ad.revisionDate, ad.revisedBy, at.name, ad.tagId from assetData as ad + left join assetVersionTag as at on ad.tagId=at.tagId where ad.assetId=? order by revisionDate desc", [$asset->getId]); + my $url = $asset->get('url'); + while (my ($date, $userId, $tagName, $tagId) = $sth->array) { + my $user = WebGUI::User->new($session, $userId); + $output .= WebGUI::HTML::arrayToRow( + $session->icon->delete("func=purgeRevision;revisionDate=".$date, $url, $i18n->get("purge revision prompt")) + .$session->icon->view( "func=view;revision=" . $date ) + .$session->icon->edit( "func=edit;revision=" . $date ), + $session->datetime->epochToHuman($date), + $user->username, + ''.$tagName.'' + ); + } + $sth->finish; + $output .= '
%s%s%s
'; + return $output; +} + 1; diff --git a/lib/WebGUI/HTML.pm b/lib/WebGUI/HTML.pm index 2e9c80f1d..478698b2d 100644 --- a/lib/WebGUI/HTML.pm +++ b/lib/WebGUI/HTML.pm @@ -39,6 +39,7 @@ A package for manipulating and massaging HTML. $html = WebGUI::HTML::makeAbsolute($session, $html); $html = WebGUI::HTML::processReplacements($session, $html); $html = WebGUI::HTML::splitTag([$tag,]$html[,$count]); # defaults to ( 'p', $html, 1 ) + $html = WebGUI::HTML::arrayToRow(@columnData); =head1 METHODS @@ -47,6 +48,27 @@ These methods are available from this package: =cut +#------------------------------------------------------------------- + +=head2 arrayToRow ( @columnData ) + +Wraps each element of @columnData in a table cell tag, concatenates them all together, +and then wraps that in table row tags. + +=head3 @columnData + +An array of strings to wrap. + +=cut + +sub arrayToRow { + my @columnData = @_; + my $output = ''; + $output .= join '', @columnData; + $output .= ''; + return $output; +} + #------------------------------------------------------------------- =head2 cleanSegment ( html , preserveStyleScript ) diff --git a/t/HTML/addToRow.t b/t/HTML/addToRow.t new file mode 100644 index 000000000..6384a02e4 --- /dev/null +++ b/t/HTML/addToRow.t @@ -0,0 +1,37 @@ +#------------------------------------------------------------------- +# 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 +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; + +use WebGUI::Test; +use WebGUI::HTML; +use WebGUI::Session; + +use Test::More; +use Test::Deep; +use Data::Dumper; + +my $session = WebGUI::Test->session; + +plan tests => 3; + +is WebGUI::HTML::arrayToRow(1), + '1', + 'addToRow: 1 element'; + +is WebGUI::HTML::arrayToRow(1,2), + '12', + '... 2 elements'; + +is WebGUI::HTML::arrayToRow(), + '', + '... 0 elements';