diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index b9b032b78..ecd75edf1 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -4,6 +4,8 @@ - fixed #12084: Greenportal links are sometimes white on white - rfe #618: Syndicated Content Asset: Make images in the downloaded RSS-feeds available in the template. - fixed #12086: Shop Billing Address Unpopulated + - Snippets can now select a template parser (instead of being restricted to + the configured default) 7.10.12 - fixed #12072: Product, related and accessory assets diff --git a/docs/upgrades/upgrade_7.10.12-7.10.13.pl b/docs/upgrades/upgrade_7.10.12-7.10.13.pl index 35af14a75..cb1f208eb 100644 --- a/docs/upgrades/upgrade_7.10.12-7.10.13.pl +++ b/docs/upgrades/upgrade_7.10.12-7.10.13.pl @@ -32,6 +32,7 @@ my $session = start(); # this line required # upgrade functions go here addAutoPlayToCarousel( $session ); +addProcessorDropdownToSnippet( $session ); finish($session); # this line required @@ -56,6 +57,36 @@ sub addAutoPlayToCarousel { print "DONE!\n" unless $quiet; } +#---------------------------------------------------------------------------- +sub addProcessorDropdownToSnippet { + my $session = shift; + my $db = $session->db; + print "\tUpdating the Snippet table to add templateProcessor option..." + unless $quiet; + + my $rows = $db->buildArrayRefOfHashRefs(q{ + select assetId, revisionDate from snippet where processAsTemplate = 1 + }); + + $db->write(q{ + alter table snippet + drop column processAsTemplate, + add column templateParser char(255) + }); + + my $default = $session->config->get('defaultTemplateParser'); + + for my $row (@$rows) { + $db->write(q{ + update snippet + set templateParser = ? + where assetId = ? and revisionDate = ? + }, [ $default, $row->{assetId}, $row->{revisionDate} ]); + } + + print "Done!\n"; +} + # -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- #---------------------------------------------------------------------------- diff --git a/lib/WebGUI/Asset/Snippet.pm b/lib/WebGUI/Asset/Snippet.pm index 63f12e710..03d56192e 100644 --- a/lib/WebGUI/Asset/Snippet.pm +++ b/lib/WebGUI/Asset/Snippet.pm @@ -63,6 +63,7 @@ sub definition { my $session = shift; my $definition = shift; my $i18n = WebGUI::International->new($session,"Asset_Snippet"); + my $t18n = WebGUI::International->new($session,'Asset_Template'); my %properties; tie %properties, 'Tie::IxHash'; %properties = ( @@ -94,13 +95,14 @@ sub definition { label => $i18n->get("cache timeout"), hoverHelp => $i18n->get("cache timeout help") }, - processAsTemplate=>{ - fieldType=>'yesNo', - label=>$i18n->get('process as template'), - hoverHelp=>$i18n->get('process as template description'), - tab=>"properties", - defaultValue=>0 - }, + templateParser => { + fieldType => 'templateParser', + allowNone => 1, + label => $t18n->get('parser'), + hoverHelp => $t18n->get('parser description'), + tab => 'properties', + defaultValue => '', + }, mimeType=>{ tab=>"properties", hoverHelp=>$i18n->get('mimeType description'), @@ -312,8 +314,10 @@ sub view { : $self->get('snippet') ; $output = $self->getToolbar.$output if ($session->var->isAdminOn && !$calledAsWebMethod); - if ($self->getValue("processAsTemplate")) { - $output = WebGUI::Asset::Template->processRaw($session, $output, $self->get); + if (my $parser = $self->getValue('templateParser')) { + $output = WebGUI::Asset::Template->processRaw( + $session, $output, $self->get, $parser + ); } WebGUI::Macro::process($session,\$output); unless ($noCache) { diff --git a/lib/WebGUI/Asset/Template.pm b/lib/WebGUI/Asset/Template.pm index 9c7dfebb5..eb6658018 100644 --- a/lib/WebGUI/Asset/Template.pm +++ b/lib/WebGUI/Asset/Template.pm @@ -140,9 +140,8 @@ sub definition { defaultValue => 1, }, parser => { - noFormPost => 1, - fieldType => 'selectBox', - defaultValue => [$session->config->get("defaultTemplateParser")], + fieldType => 'templateParser', + defaultValue => $session->config->get('defaultTemplateParser'), }, namespace => { fieldType => 'combo', @@ -404,22 +403,12 @@ sub getEditForm { templatePreview.js ); - if($config->get("templateParsers")){ - my @temparray = @{$config->get("templateParsers")}; - tie my %parsers, 'Tie::IxHash'; - while(my $a = shift @temparray){ - $parsers{$a} = $self->getParser($session, $a)->getName(); - } - my $value = [$self->getValue("parser")]; - $value = \[$config->get("defaultTemplateParser")] if(!$self->getValue("parser")); - $properties->selectBox( - -name=>"parser", - -options=>\%parsers, - -value=>$value, - -label=>$i18n->get('parser'), - -hoverHelp=>$i18n->get('parser description'), - ); - } + $properties->templateParser( + name => 'parser', + label => $i18n->get('parser'), + hoverHelp => $i18n->get('parser description'), + value => $self->getValue('parser'), + ); $properties->jsonTable( name => 'attachmentsJson', diff --git a/lib/WebGUI/Form/TemplateParser.pm b/lib/WebGUI/Form/TemplateParser.pm new file mode 100644 index 000000000..af0b37530 --- /dev/null +++ b/lib/WebGUI/Form/TemplateParser.pm @@ -0,0 +1,122 @@ +package WebGUI::Form::TemplateParser; + +=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; +use base 'WebGUI::Form::SelectBox'; +use WebGUI::International; +use Tie::IxHash; + +=head1 NAME + +Package WebGUI::Form::TemplateParser + +=head1 DESCRIPTION + +A dropdown list for selecting a template parser. + +=head1 SEE ALSO + +This is a subclass of WebGUI::Form::SelectBox. + +=head1 METHODS + +The following methods are specifically available from this class. Check the superclass for additional methods. + +=cut + +#------------------------------------------------------------------- + +=head2 areOptionsSettable + +No, they aren't. + +=cut + +sub areOptionsSettable { 0 } + +#---------------------------------------------------------------------------- + +=head2 definition ( [ additionalTerms ] ) + +See the super class for additional details. + +=head3 additionalTerms + +The following additional parameters have been added via this sub class. + +=head3 allowNone + +Set to true if "None" is an acceptable option for this dropdown. Defaults to +false. + +=head4 defaultValue + +Defaults to the default parser selected in the config file + +=cut + +sub definition { + my ($class, $session, $definition) = @_; + push @{$definition ||= []}, { + allowNone => { + defaultValue => 0, + }, + defaultValue => { + defaultValue => $session->config->get('defaultTemplateParser') + } + }; + return $class->SUPER::definition($session, $definition); +} + +#------------------------------------------------------------------- + +=head2 getName ( session ) + +Returns the human readable name of this control. + +=cut + +sub getName { + my ($self, $session) = @_; + return WebGUI::International->new($session, 'WebGUI')->get('Template Parser'); +} + +#------------------------------------------------------------------- + +=head2 getOptions + +Called by the superclass to determine which options are presented. + +=cut + +sub getOptions { + my $self = shift; + my $session = $self->session; + tie my %o, 'Tie::IxHash'; + if ($self->get('allowNone')) { + $o{''} = WebGUI::International->new($session, 'WebGUI')->get('881'); + } + return \%o unless my $parsers = $session->config->get('templateParsers'); + + for my $class (@$parsers) { + my $parser = WebGUI::Asset::Template->getParser($session, $class); + $o{$class} = $parser->getName; + } + + return \%o; +} + +1; diff --git a/lib/WebGUI/i18n/English/WebGUI.pm b/lib/WebGUI/i18n/English/WebGUI.pm index 440b8138b..a973d2221 100644 --- a/lib/WebGUI/i18n/English/WebGUI.pm +++ b/lib/WebGUI/i18n/English/WebGUI.pm @@ -520,6 +520,11 @@ our $I18N = { lastUpdated => 1133087205 }, + 'Template Parser' => { + message => q|Template Parser|, + lastUpdated => 1301593691 + }, + '391' => { message => q|Delete attached file.|, lastUpdated => 1031514049 diff --git a/t/Asset/Snippet.t b/t/Asset/Snippet.t index 5449c3315..527a63d66 100644 --- a/t/Asset/Snippet.t +++ b/t/Asset/Snippet.t @@ -33,7 +33,7 @@ isa_ok($snippet, 'WebGUI::Asset::Snippet'); # Test to see if we can set values my $properties = { cacheTimeout => 124, - processAsTemplate => 1, + templateParser => 'WebGUI::Asset::Template::HTMLTemplate', mimeType => 'text/plain', snippet => "Gooey's milkshake brings all the girls to the yard...", }; @@ -74,9 +74,9 @@ isnt ($editOutput, undef, 'www_edit returns something'); $snippet->update({ title => "authMethod", - processAsTemplate => 1, + templateParser => 'WebGUI::Asset::Template::TemplateToolkit', cacheTimeout => 1, - snippet => q|^SQL(select value from settings where name="");| + snippet => q|^SQL(select value from settings where name="[% title %]");| }); WebGUI::Test->originalConfig('macros'); diff --git a/t/Asset/Template.t b/t/Asset/Template.t index ca8b13a5b..97274388d 100644 --- a/t/Asset/Template.t +++ b/t/Asset/Template.t @@ -23,6 +23,8 @@ use Test::Exception; use JSON qw{ from_json }; my $session = WebGUI::Test->session; +my $default = $session->config->get('defaultTemplateParser'); +my $ht = 'WebGUI::Asset::Template::HTMLTemplate'; my $list = WebGUI::Asset::Template->getList($session); cmp_deeply($list, {}, 'getList with no classname returns an empty hashref'); @@ -33,16 +35,19 @@ my %var = ( conditional=>1, loop=>[{},{},{},{},{}] ); -my $output = WebGUI::Asset::Template->processRaw($session,$tmplText,\%var); +my $output = WebGUI::Asset::Template->processRaw($session,$tmplText,\%var, $ht); 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=>$tmplText, namespace=>'WebGUI Test Template'}); + +my $template = $importNode->addChild({className=>"WebGUI::Asset::Template"}); +is($template->get('parser'), $default, "default parser is $default"); + +$template = $importNode->addChild({className=>"WebGUI::Asset::Template", title=>"test", url=>"testingtemplates", template=>$tmplText, namespace=>'WebGUI Test Template',parser=>$ht}); isa_ok($template, 'WebGUI::Asset::Template', "creating a template"); -is($template->get('parser'), 'WebGUI::Asset::Template::HTMLTemplate', 'default parser is HTMLTemplate'); $var{variable} = "BBBBB"; $output = $template->process(\%var); @@ -111,6 +116,7 @@ my $template3 = $importNode->addChild({ title => 'headBlock test', headBlock => "tag1 tag2 tag3", template => "this is a template", + parser => $ht, }, undef, time()-5); ok(!$template3->get('headBlock'), 'headBlock is empty'); @@ -205,6 +211,7 @@ my $trashTemplate = $importNode->addChild({ className => "WebGUI::Asset::Template", title => 'Trash template', template => q|Trash Trash Trash Trash|, + parser => $ht, }); $trashTemplate->trash; @@ -229,6 +236,7 @@ my $brokenTemplate = $importNode->addChild({ className => "WebGUI::Asset::Template", title => 'Broken template', template => q|If clause with no ending tag|, + parser => $ht, }); WebGUI::Test->interceptLogging; @@ -251,6 +259,7 @@ my $userStyleTemplate = $importNode->addChild({ url => "ufs", template => "user function style", namespace => 'WebGUI Test Template', + parser => $ht, }); my $someOtherTemplate = $importNode->addChild({ @@ -259,6 +268,7 @@ my $someOtherTemplate = $importNode->addChild({ url => "sot", template => "some other template", namespace => 'WebGUI Test Template', + parser => $ht, }); $session->setting->set('userFunctionStyleId', $userStyleTemplate->getId);