From d81db0c2887b62053138aa990468c56f27671f50 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 2 Feb 2005 04:03:39 +0000 Subject: [PATCH] adding first rev of shortcut asset --- lib/WebGUI/Asset/Shortcut.pm | 451 ++++++++++++++++++ .../Help/{WobjectProxy.pm => Shortcut.pm} | 4 +- .../English/{WobjectProxy.pm => Shortcut.pm} | 40 +- 3 files changed, 473 insertions(+), 22 deletions(-) create mode 100644 lib/WebGUI/Asset/Shortcut.pm rename lib/WebGUI/Help/{WobjectProxy.pm => Shortcut.pm} (79%) rename lib/WebGUI/i18n/English/{WobjectProxy.pm => Shortcut.pm} (54%) diff --git a/lib/WebGUI/Asset/Shortcut.pm b/lib/WebGUI/Asset/Shortcut.pm new file mode 100644 index 000000000..87a86481b --- /dev/null +++ b/lib/WebGUI/Asset/Shortcut.pm @@ -0,0 +1,451 @@ +package WebGUI::Asset::Shortcut; + +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2004 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 strict; +use Tie::CPHash; +use WebGUI::DateTime; +use WebGUI::Icon; +use WebGUI::International; +use WebGUI::Privilege; +use WebGUI::Session; +use WebGUI::SQL; +use WebGUI::Page; +use WebGUI::TabForm; +use WebGUI::Template; +use WebGUI::MetaData; + +our @ISA = qw(WebGUI::Asset); + + +#------------------------------------------------------------------- +sub definition { + my $class = shift; + my $definition = shift; + push(@{$definition}, { + tableName=>'FileAsset', + className=>'WebGUI::Asset::File', + properties=>{ + shortcutToAssetId=>{ + fieldType=>"hidden" + defaultValue=>undef + }, + overrideTitle=>{ + fieldType=>"yesNo", + defaultValue=>0 + }, + overrideTemplate=>{ + fieldType=>"yesNo", + defaultValue=>0 + }, + overrideDisplayTitle=>{ + fieldType=>"yesNo", + defaultValue=>0 + }, + overrideDescription=>{ + fieldType=>"yesNo", + defaultValue=>0 + }, + overrideTemplateId=>{ + fieldType=>"template", + defaultValue=>undef + }, + shortcutByCriteria=>{ + fieldType=>"yesNo", + defaultValue=>0, + }, + resolveMultiples=>{ + fieldType=>"selectList", + defaultValue=>"mostRecent", + }, + shortcutCriteria=>{ + fieldType=>"textarea", + defaultValue=>"", + } + } + }); + return $class->SUPER::definition($definition); +} + + +#------------------------------------------------------------------- +sub getEditForm { + my $self = shift; + my $tabform = $self->SUPER::getEditForm(); +# $tabform->getTab("display")->template( +# -name=>"overrideTemplateId", +# -value=>$self->getValue("overrideTemplateId"), +# -namespace=>$self->getShortcut-> +# ); + $tabform->getTab("properties")->yesNo( + -name=>"overrideTitle", + -value=>$self->getValue("overrideTitle"), + -label=>WebGUI::International::get(7,"Shortcut") + ); + $tabform->getTab("display")->yesNo( + -name=>"overrideDisplayTitle", + -value=>$self->getValue("overrideDisplayTitle"), + -label=>WebGUI::International::get(8,"Shortcut") + ); + $tabform->getTab("properties")->yesNo( + -name=>"overrideDescription", + -value=>$self->getValue("overrideDescription"), + -label=>WebGUI::International::get(9,"Shortcut") + ); + $tabform->getTab("display")->yesNo( + -name=>"overrideTemplate", + -value=>$self->getValue("overrideTemplate"), + -label=>WebGUI::International::get(10,"Shortcut") + ); + my @data = WebGUI::SQL->quickArray("select page.urlizedTitle,wobject.title from wobject left join page on wobject.pageId=page.pageId + where wobject.wobjectId=".quote($self->get("proxiedWobjectId"))); + $tabform->getTab("properties")->readOnly( + -label=>WebGUI::International::get(1,"Shortcut"), + -value=>''.$data[1].' ('.$self->get("proxiedWobjectId").')' + ); + if($session{setting}{metaDataEnabled}) { + $tabform->getTab("properties")->yesNo( + -name=>"proxyByCriteria", + -value=>$self->getValue("proxyByCriteria"), + -label=>WebGUI::International::get("Proxy by alternate criteria?","Shortcut"), + -extras=>q|Onchange=" + if (this.form.proxyByCriteria[0].checked) { + this.form.resolveMultiples.disabled=false; + this.form.proxyCriteria.disabled=false; + } else { + this.form.resolveMultiples.disabled=true; + this.form.proxyCriteria.disabled=true; + }"| + ); + if ($self->getValue("proxyByCriteria") == 0) { + $self->{_disabled} = 'disabled=true'; + } + $tabform->getTab("properties")->selectList( + -name=>"resolveMultiples", + -value=>[ $self->getValue("resolveMultiples") ], + -label=>WebGUI::International::get("Resolve Multiples?","Shortcut"), + -options=>{ + mostRecent=>WebGUI::International::get("Most Recent","Shortcut"), + random=>WebGUI::International::get("Random","Shortcut"), + }, + -extras=>$self->{_disabled} + ); + + $tabform->getTab("properties")->readOnly( + -value=>$self->_drawQueryBuilder(), + -label=>WebGUI::International::get("Criteria","Shortcut"), + ); + } + return $tabform; +} + +#------------------------------------------------------------------- +sub getIcon { + my $self = shift; + my $small = shift; + return $session{config}{extrasURL}.'/assets/small/template.gif' if ($small); + return $session{config}{extrasURL}.'/assets/template.gif'; +} + +#------------------------------------------------------------------- +sub getName { + return WebGUI::International::get(3,"Shortcut"); +} + +#------------------------------------------------------------------- +sub getShortcut { + my $self = shift; + return WebGUI::Asset->newByDynamicClass($self->get("shortcutToAssetId")); +} + +#------------------------------------------------------------------- +sub processPropertiesFromFormPost { + my $self = shift; + $self->SUPER::processPropertiesFromFormPost; + my $scratchId = "Shortcut_" . $self->getId; + WebGUI::Session::deleteAllScratch($scratchId); +} + +#------------------------------------------------------------------- +sub view { + my $self = shift; + return $self->getShortcut->view; +} + + +#------------------------------------------------------------------- +sub www_edit { + my $self = shift; + return WebGUI::Privilege::insufficient() unless $self->canEdit; + $self->getAdminConsole->setHelp("shortcut add/edit","Shortcut"); + return $self->getAdminConsole->render($self->getEditForm->print,WebGUI::International::get(2)); +} + +#------------------------------------------------------------------- +sub _drawQueryBuilder { + # Initialize operators + my @textFields = qw|text yesNo selectList radioList|; + my %operator; + foreach (@textFields) { + $operator{$_} = { + "=" => WebGUI::International::get("is",$_[0]->get("namespace")), + "!=" => WebGUI::International::get("isnt",$_[0]->get("namespace")) + }; + } + $operator{integer} = { + "=" => WebGUI::International::get("equal to",$_[0]->get("namespace")), + "!=" => WebGUI::International::get("not equal to",$_[0]->get("namespace")), + "<" => WebGUI::International::get("less than",$_[0]->get("namespace")), + ">" => WebGUI::International::get("greater than",$_[0]->get("namespace")) + }; + + # Get the fields and count them + my $fields = WebGUI::MetaData::getMetaDataFields(); + my $fieldCount = scalar(keys %$fields); + + unless ($fieldCount) { # No fields found.... + return 'No metadata defined yet. + Click here to define metadata attributes.'; + } + + # Static form fields + my $proxyCriteriaField = WebGUI::Form::textarea({ + name=>"proxyCriteria", + value=>$_[0]->getValue("proxyCriteria"), + extras=>'style="width: 100%" '.$_[0]->{_disabled} + }); + my $conjunctionField = WebGUI::Form::selectList({ + name=>"conjunction", + options=>{ + "AND" => WebGUI::International::get("AND",$_[0]->get("namespace")), + "OR" => WebGUI::International::get("OR",$_[0]->get("namespace"))}, + value=>["OR"], + extras=>'class="qbselect"', + }); + + # html + my $output; + $output .= ''; + $output .= ''; + + $output .= qq| + + + + + + + + + + + + + + + + + + |; + + # Here starts the field loop + foreach my $field (keys %$fields) { + my $fieldLabel = $fields->{$field}{fieldName}; + my $fieldType = $fields->{$field}{fieldType} || "text"; + + # The operator select field + my $opFieldName = "op_field".$fields->{$field}{fieldId}; + my $opField = WebGUI::Form::selectList({ + name=>$opFieldName, + uiLevel=>5, + options=>$operator{$fieldType}, + extras=>'class="qbselect"' + }); + # The value select field + my $valFieldName = "val_field".$fields->{$field}{fieldId}; + my $valueField = WebGUI::Form::dynamicField($fieldType, { + name=>$valFieldName, + uiLevel=>5, + extras=>qq/title="$fields->{$field}{description}" class="qbselect"/, + possibleValues=>$fields->{$field}{possibleValues}, + }); + # An empty row + $output .= qq| + + + + + + + + |; + + # Table row with field info + $output .= qq| + + + + + + + + |; + } + # Close the table + $output .= "
$proxyCriteriaField
+
+ $conjunctionField +

$fieldLabel

+ $opField + + $valueField + +
"; + + return $output; +} + +#------------------------------------------------------------------- +sub www_view { + my $self = shift; + return $self->getShortcut->www_view; +} + + +1; + + +#------------------------------------------------------------------- + +=head2 getAssetByCriteria ( hashRef ) + +This function will search for a asset that match a metadata criteria set. +If no asset is found, undef will be returned. + +=head3 hashRef + +A typical hashRef for this function will look like: + +{ + proxiedNamespace => "Article", + resolveMultiples => "random", + proxyCriteria => "State = Wisconsin AND Country != Sauk" +} + +Most of the time this will be a: + +WebGUI::SQL->quickHashRef("select * from AssetProxy where assetId=".quote($proxiedId)); + +=cut + +sub getAssetByCriteria { + my $assetProxy = shift; + my $criteria = $assetProxy->{proxyCriteria}; + my $order = $assetProxy->{resolveMultiples}; + my $namespace = $assetProxy->{proxiedNamespace}; + my $assetId = $assetProxy->{assetId}; + + # Parse macro's in criteria + $criteria = WebGUI::Macro::process($criteria); + + # Once a asset is found, we will stick to that asset, + # to prevent the proxying of multiple- depth assets like Surveys and USS. + my $scratchId; + if ($assetId) { + $scratchId = "AssetProxy_" . $assetId; + if($session{scratch}{$scratchId}) { + return $session{scratch}{$scratchId} unless ($session{var}{adminOn}); + } + } + + # $criteria = "State = Wisconsin AND Country != Sauk"; + # + # State = Wisconsin AND Country != Sauk + # | | | + # |- $field |_ $operator |- $value + # |_ $attribute |_ $attribute + my $operator = qr/<>|!=|=|>=|<=|>|<|like/i; + my $attribute = qr/['"][^()|=>unconditionalRead($sql); + while (my ($data) = $sth->array) { + push (@wids, $data); + } + $sth->finish; + + # No matching assets found. + if (scalar(@wids) == 0) { + return undef; # fall back to the originally mirrored asset. + } + my $wid; + # Grab a wid from the results + if ($order eq 'random') { + $wid = $wids[ rand @wids ]; + } else { + #default order is mostRecent + $wid = $wids[0]; # 1st element in list is most recent. + } + + # Store the matching assetId in user scratch. + WebGUI::Session::setScratch($scratchId,$wid) if ($scratchId); + + return $wid; +} + + diff --git a/lib/WebGUI/Help/WobjectProxy.pm b/lib/WebGUI/Help/Shortcut.pm similarity index 79% rename from lib/WebGUI/Help/WobjectProxy.pm rename to lib/WebGUI/Help/Shortcut.pm index 21e300f2b..cded3d023 100644 --- a/lib/WebGUI/Help/WobjectProxy.pm +++ b/lib/WebGUI/Help/Shortcut.pm @@ -1,7 +1,7 @@ -package WebGUI::Help::WobjectProxy; +package WebGUI::Help::Shortcut; our $HELP = { - 'wobject proxy add/edit' => { + 'shortcut add/edit' => { title => '5', body => '6', related => [ diff --git a/lib/WebGUI/i18n/English/WobjectProxy.pm b/lib/WebGUI/i18n/English/Shortcut.pm similarity index 54% rename from lib/WebGUI/i18n/English/WobjectProxy.pm rename to lib/WebGUI/i18n/English/Shortcut.pm index 4fe57266b..48cebc542 100644 --- a/lib/WebGUI/i18n/English/WobjectProxy.pm +++ b/lib/WebGUI/i18n/English/Shortcut.pm @@ -1,4 +1,4 @@ -package WebGUI::i18n::English::WobjectProxy; +package WebGUI::i18n::English::Shortcut; our $I18N = { 'Criteria' => { @@ -32,7 +32,7 @@ our $I18N = { }, '2' => { - message => q|Edit Wobject Proxy|, + message => q|Edit Shortcut|, lastUpdated => 1031514049 }, @@ -42,51 +42,51 @@ our $I18N = { }, '1' => { - message => q|Wobject To Proxy|, + message => q|Asset to Mirror|, lastUpdated => 1031514049 }, '6' => { - message => q|With the Wobject Proxy (aka Shortcut) you can mirror a wobject from another page to any other page. This is useful if you want to reuse the same content in multiple sections of your site. + message => q|With the shortcut you can mirror an asset. This is useful if you want to reuse the same content in multiple sections of your site.

-NOTE: The wobject proxy is not available through the Add Content menu, but instead through the shortcut icon on each wobject's toolbar. +NOTE: The shortcut is not available through the Add Content menu, but instead through the shortcut icon on each wobject's toolbar.

-Wobject To Proxy
+Asset to Mirror
Provides a link to the original wobject being proxied.

Override title?
-Set to "yes" to use the title of the wobject proxy instead of the original title of the wobject. +Set to "yes" to use the title of the shortcut instead of the original title of the wobject.

Override description?
-Set to "yes" to use the description of the wobject proxy instead of the original description of the wobject. +Set to "yes" to use the description of the shortcut instead of the original description of the wobject.

Override display title?
-Set to "yes" to use the display title setting of the wobject proxy instead of the original display title setting of the wobject. +Set to "yes" to use the display title setting of the shortcut instead of the original display title setting of the wobject.

Override template?
-Set to "yes" to use the template of the wobject proxy instead of the original template of the wobject. +Set to "yes" to use the template of the shortcut of the original template of the wobject.

-Proxy by alternate criteria?
+Shortcut by alternate criteria?
Set to "yes" to enable selecting a wobject based upon custom criteria. Metadata must be enabled for this option to function properly.

Resolve Multiples?
-Sets the order to use when multiple wobjects are selected. Random means that if multiple wobjects match the proxy criteria then the wobject proxy will select a random wobject to proxy.
-Most Recent will select the most recent wobject that match the proxy criteria. +Sets the order to use when multiple wobjects are selected. Random means that if multiple assets match the shortcut criteria then the shortcut will select a random asset.
+Most Recent will select the most recent asset that match the shortcut criteria.

Criteria
-A statement to determinate what to proxy, in the form of "color = blue and weight != heavy". Multiple expressions may be joined with "and" and "or".
+A statement to determinate what to mirror, in the form of "color = blue and weight != heavy". Multiple expressions may be joined with "and" and "or".
A property or value must be quoted if it contains spaces. Feel free to use the criteria builder to build your statements.

-NOTE: Wobject Proxy will automatically add a template variable to the wobject it's proxying called 'original.page.url'. You can use that to link to the original content that's being proxied. +NOTE: Shortcut will automatically add a template variable to the asset it's mirroring called 'original.page.url'. You can use that to link to the original content that's being proxied.

|, lastUpdated => 1101795461, @@ -98,7 +98,7 @@ A property or value must be quoted if it contains spaces. Feel free to use the c }, '3' => { - message => q|Wobject Proxy|, + message => q|Shortcut|, lastUpdated => 1031514049 }, @@ -107,8 +107,8 @@ A property or value must be quoted if it contains spaces. Feel free to use the c lastUpdated => 1053183804 }, - 'Proxy by alternate criteria?' => { - message => q|Proxy by alternate criteria?|, + 'Shortcut by alternate criteria?' => { + message => q|Shortcut by alternate criteria?|, lastUpdated => 1053183804 }, @@ -133,7 +133,7 @@ A property or value must be quoted if it contains spaces. Feel free to use the c }, '4' => { - message => q|Wobject proxying failed. Perhaps the proxied wobject has been deleted.|, + message => q|Asset mirroring failed. Perhaps the original asset has been deleted.|, lastUpdated => 1031514049 }, @@ -153,7 +153,7 @@ A property or value must be quoted if it contains spaces. Feel free to use the c }, '5' => { - message => q|Wobject Proxy, Add/Edit|, + message => q|Shortcut, Add/Edit|, lastUpdated => 1031514049 },