From 598a0d0990babd0ad5474f4523dea9c9e84990d5 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 18 Oct 2007 23:17:18 +0000 Subject: [PATCH] fix for choosing uncommitted templates as a User Style Template --- lib/WebGUI/Asset/Template.pm | 20 ++++++++++++++++---- lib/WebGUI/Form/Template.pm | 22 +++++++++++++++------- lib/WebGUI/i18n/English/WebGUI.pm | 4 ++-- t/Asset/Template.t | 23 ++++++++++++++++++----- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/lib/WebGUI/Asset/Template.pm b/lib/WebGUI/Asset/Template.pm index 1683c1eff..452d365a6 100644 --- a/lib/WebGUI/Asset/Template.pm +++ b/lib/WebGUI/Asset/Template.pm @@ -191,7 +191,7 @@ sub getEditForm { #------------------------------------------------------------------- -=head2 getList ( session, namespace ) +=head2 getList ( session, namespace [,clause] ) Returns a hash reference containing template ids and template names of all the templates in the specified namespace. @@ -203,7 +203,12 @@ A reference to the current session. =head3 namespace -Specify the namespace to build the list for. +Specify the namespace to build the list for. If no namespace is specified, +then an empty hash reference will be returned. + +=head3 clause + +An extra clause that can be used to further limit the list, such as "assetData.status='approved' =cut @@ -211,8 +216,15 @@ sub getList { my $class = shift; my $session = shift; my $namespace = shift; - my $sql = "select asset.assetId, assetData.revisionDate from template left join asset on asset.assetId=template.assetId left join assetData on assetData.revisionDate=template.revisionDate and assetData.assetId=template.assetId where template.namespace=".$session->db->quote($namespace)." and template.showInForms=1 and asset.state='published' and assetData.revisionDate=(SELECT max(revisionDate) from assetData where assetData.assetId=asset.assetId and (assetData.status='approved' or assetData.tagId=".$session->db->quote($session->scratch->get("versionTag")).")) order by assetData.title"; - my $sth = $session->dbSlave->read($sql); + my $clause = shift; + if ($clause) { + $clause = ' and ' . $clause; + } + else { + $clause = ''; + } + my $sql = "select asset.assetId, assetData.revisionDate from template left join asset on asset.assetId=template.assetId left join assetData on assetData.revisionDate=template.revisionDate and assetData.assetId=template.assetId where template.namespace=? and template.showInForms=1 and asset.state='published' and assetData.revisionDate=(SELECT max(revisionDate) from assetData where assetData.assetId=asset.assetId and (assetData.status='approved' or assetData.tagId=?)) $clause order by assetData.title"; + my $sth = $session->dbSlave->read($sql, [$namespace, $session->scratch->get("versionTag")]); my %templates; tie %templates, 'Tie::IxHash'; while (my ($id, $version) = $sth->array) { diff --git a/lib/WebGUI/Form/Template.pm b/lib/WebGUI/Form/Template.pm index d7b7d9e9b..1ba969b52 100644 --- a/lib/WebGUI/Form/Template.pm +++ b/lib/WebGUI/Form/Template.pm @@ -59,6 +59,10 @@ The namespace for the list of templates to return. If this is omitted, all templ A text label that will be displayed if toHtmlWithWrapper() is called. Defaults to getName(). +=head4 onlyCommitted + +If true, this will limit the list of template to only include templates that are committed. + =cut sub definition { @@ -79,6 +83,9 @@ sub definition { namespace=>{ defaultValue=>undef }, + onlyCommitted=>{ + defaultValue=>'' + }, dbDataType => { defaultValue => "VARCHAR(22) BINARY", }, @@ -96,14 +103,15 @@ Renders a template picker control. sub toHtml { my $self = shift; - my $templateList = WebGUI::Asset::Template->getList($self->session, $self->get("namespace")); - #Remove entries from template list that the user does not have permission to view. - for my $assetId ( keys %{$templateList} ) { - my $asset = WebGUI::Asset::Template->new($self->session, $assetId); - if (!$asset->canView($self->session->user->userId)) { - delete $templateList->{$assetId}; - } + my $onlyCommitted = $self->get('onlyCommitted') ? "assetData.status='approved'" : $self->get('onlyCommitted'); + my $templateList = WebGUI::Asset::Template->getList($self->session, $self->get("namespace"), $onlyCommitted); + #Remove entries from template list that the user does not have permission to view. + for my $assetId ( keys %{$templateList} ) { + my $asset = WebGUI::Asset::Template->new($self->session, $assetId); + if (!$asset->canView($self->session->user->userId)) { + delete $templateList->{$assetId}; } + } $self->set("options", $templateList); return $self->SUPER::toHtml(); } diff --git a/lib/WebGUI/i18n/English/WebGUI.pm b/lib/WebGUI/i18n/English/WebGUI.pm index c1e4faf87..f0267e38c 100644 --- a/lib/WebGUI/i18n/English/WebGUI.pm +++ b/lib/WebGUI/i18n/English/WebGUI.pm @@ -3138,8 +3138,8 @@ and tracked by WebGUI.|, }, 'user function style description' => { - message => q|Defines which style to be used to style WebGUI operations (profile editing, message log, etc.) when they are available to a user.|, - lastUpdated => 1120239343, + message => q|Defines which style to be used to style WebGUI operations (profile editing, message log, etc.) when they are available to a user. Only templates which have been committed are allowed.|, + lastUpdated => 1192735786, }, 'admin console template description' => { diff --git a/t/Asset/Template.t b/t/Asset/Template.t index 672a83f3a..f42c5f0d8 100644 --- a/t/Asset/Template.t +++ b/t/Asset/Template.t @@ -15,12 +15,14 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; use WebGUI::Asset::Template; -use Test::More tests => 8; # increment this value for each test you create +use Test::More tests => 10; # increment this value for each test you create +use Test::Deep; my $session = WebGUI::Test->session; my $list = WebGUI::Asset::Template->getList($session); -ok(defined $list, "getList()"); +cmp_deeply($list, {}, 'getList with no classname returns an empty hashref'); + my $template = " true XY "; my %var = ( variable=>"AAAAA", @@ -31,11 +33,22 @@ my $output = WebGUI::Asset::Template->processRaw($session,$template,\%var); ok($output =~ m/\bAAAAA\b/, "processRaw() - variables"); ok($output =~ m/true/, "processRaw() - conditionals"); ok($output =~ m/\s(?:XY){5}\s/, "processRaw() - loops"); + my $importNode = WebGUI::Asset::Template->getImportNode($session); -my $template = $importNode->addChild({className=>"WebGUI::Asset::Template", title=>"test", url=>"testingtemplates", template=>$template}); -ok(defined $template, "creating a template"); +my $template = $importNode->addChild({className=>"WebGUI::Asset::Template", title=>"test", url=>"testingtemplates", template=>$template, namespace=>'WebGUI Test Template'}); +isa_ok($template, 'WebGUI::Asset::Template', "creating a template"); + +$var{variable} = "BBBBB"; $output = $template->process(\%var); -ok($output =~ m/\bAAAAA\b/, "process() - variables"); +ok($output =~ m/\bBBBBB\b/, "process() - variables"); ok($output =~ m/true/, "process() - conditionals"); ok($output =~ m/\s(?:XY){5}\s/, "process() - loops"); + +my $newList = WebGUI::Asset::Template->getList($session, 'WebGUI Test Template'); +ok(exists $newList->{$template->getId}, 'Uncommitted template exists returned from getList'); + +my $newList2 = WebGUI::Asset::Template->getList($session, 'WebGUI Test Template', "assetData.status='approved'"); +ok(!exists $newList2->{$template->getId}, 'extra clause to getList prevents uncommitted template from being displayed'); + $template->purge; +