From 129016325deb6d72599ba81aecb470d3ff4b586e Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Wed, 30 Jun 2010 15:15:32 -0500 Subject: [PATCH] migrate attachments to JsonTable --- lib/WebGUI/Asset/Template.pm | 173 ++++++++--------------------------- 1 file changed, 36 insertions(+), 137 deletions(-) diff --git a/lib/WebGUI/Asset/Template.pm b/lib/WebGUI/Asset/Template.pm index 2a71fffe9..a55394f0e 100644 --- a/lib/WebGUI/Asset/Template.pm +++ b/lib/WebGUI/Asset/Template.pm @@ -112,6 +112,9 @@ sub definition { storageIdExample => { fieldType => 'image', }, + attachmentsJson => { + fieldType => 'JsonTable', + }, }, }; return $class->SUPER::definition($session,$definition); @@ -119,33 +122,6 @@ sub definition { #------------------------------------------------------------------- -=head2 addAttachments ( attachments ) - -Adds attachments to this template. Attachments is an arrayref of hashrefs, -where each hashref should have at least url, type, and sequence as keys. - -=cut - -sub addAttachments { - my ($self, $attachments) = @_; - - my $db = $self->session->db; - - foreach my $a (@$attachments) { - my %params = ( - templateId => $self->getId, - revisionDate => $self->get('revisionDate'), - url => $a->{url}, - type => $a->{type}, - sequence => $a->{sequence}, - attachId => 'new', - ); - $db->setRow('template_attachments', 'attachId', \%params); - } -} - -#------------------------------------------------------------------- - =head2 addRevision ( ) Override the master addRevision to copy attachments @@ -156,7 +132,6 @@ sub addRevision { my ( $self, $properties, @args ) = @_; my $asset = $self->SUPER::addRevision($properties, @args); delete $properties->{templatePacked}; - $asset->addAttachments($self->getAttachments); return $asset; } @@ -192,7 +167,6 @@ sub duplicate { my $self = shift; my $newTemplate = $self->SUPER::duplicate; $newTemplate->update({isDefault => 0}); - $newTemplate->addAttachments($self->getAttachments); if ( my $storageId = $self->get('storageIdExample') ) { my $newStorage = WebGUI::Storage->get( $self->session, $storageId )->copy; $newTemplate->update({ storageIdExample => $newStorage->getId }); @@ -211,7 +185,6 @@ Override to add attachments to package data sub exportAssetData { my ( $self ) = @_; my $data = $self->SUPER::exportAssetData; - $data->{template_attachments} = $self->getAttachments; if ( $self->get('storageIdExample') ) { push @{$data->{storage}}, $self->get('storageIdExample'); } @@ -234,27 +207,18 @@ If defined, will limit the attachments to this type; e.g., passing sub getAttachments { my ( $self, $type ) = @_; - my @params = ($self->getId, $self->get('revisionDate')); - my $typeString; + if ( !$self->{_attachments} ) { + $self->{_attachments} = JSON->new->decode( $self->get('attachmentsJson') ); + } - if ($type) { - $typeString = 'AND type = ?'; - push(@params, $type); - } + my $output = []; + for my $attach ( @{$self->{_attachments}} ) { + if ( $attach->{type} eq $type ) { + push @{$output}, $attach; + } + } - my $sql = qq{ - SELECT - * - FROM - template_attachments - WHERE - templateId = ? - AND revisionDate = ? - $typeString - ORDER BY - type, sequence ASC - }; - return $self->session->db->buildArrayRefOfHashRefs($sql, \@params); + return $output; } #------------------------------------------------------------------- @@ -329,74 +293,30 @@ sub getEditForm { ); } - ### Template attachments - my $session = $self->session; - my @headers = map { '' . $i18n->get("attachment header $_") . '' } - qw(index type url remove); - - tie my %attachmentTypeNames, 'Tie::IxHash' => ( - stylesheet => $i18n->get('css label'), - headScript => $i18n->get('js head label'), - bodyScript => $i18n->get('js body label'), + $properties->jsonTable( + name => 'attachmentsJson', + value => $self->get('attachmentsJson'), + label => $i18n->get("attachments display label"), + fields => [ + { + type => "text", + name => "url", + label => $i18n->get('attachment header url'), + size => '48', + }, + { + type => "select", + name => "type", + label => $i18n->get('attachment header type'), + options => [ + stylesheet => $i18n->get('css label'), + headScript => $i18n->get('js head label'), + bodyScript => $i18n->get('js body label'), + ], + }, + ], ); - my $table = ''; - $table .= "@headers"; - foreach my $a ( @{ $self->getAttachments } ) { - my ($seq, $type, $url) = @{$a}{qw(sequence type url)}; - # escape macros in the url so they don't get processed - $url =~ s/\^/^/g; - my $del = WebGUI::Form::checkbox( - $session, { - name => 'removeAttachment', - value => $url, - extras => 'class="id"', - } - ); - my @data = ( - "", - "", - "", - "", - ); - $table .= "@data"; - } - $table .= '
$seq$type$url$del
'; - my $properties = $tabform->getTab('properties'); - my $label = $i18n->get('attachment display label'); - $properties->raw("$label$table"); - - my @data = map { "$_" } ( - WebGUI::Form::integer( - $session, { size => '2', id => 'addBoxIndex' } - ), - WebGUI::Form::selectBox( - $session, { options => \%attachmentTypeNames, id => 'addBoxType' } - ), - WebGUI::Form::text($session, { id => 'addBoxUrl', size => 40 }), - WebGUI::Form::button( - $session, { - value => $i18n->get('attachment add button'), - extras => 'onclick="addClick()"' - } - ), - ); - - my ($style, $url) = $self->session->quick(qw(style url)); - $style->setScript($url->extras('yui/build/yahoo/yahoo-min.js'), {type => 'text/javascript'}); - $style->setScript($url->extras('yui/build/json/json-min.js'), {type => 'text/javascript'}); - $style->setScript($url->extras('yui/build/dom/dom-min.js'), {type => 'text/javascript'}); - $style->setScript($url->extras('yui/build/event/event-min.js'), {type => 'text/javascript'}); - $style->setScript($url->extras('yui/build/connection/connection-min.js'), {type => 'text/javascript'}); - $style->setScript($url->extras('yui-webgui/build/i18n/i18n.js'), {type => 'text/javascript'}); - - pop(@headers); - my $scriptUrl = $url->extras('templateAttachments.js'); - $table = "@headers@data
"; - $table .= qq(); - $label = $i18n->get('attachment add field label'); - $properties->raw("$label$table"); - $properties->image( name => 'storageIdExample', value => $self->getValue('storageIdExample'), @@ -690,28 +610,7 @@ sub processPropertiesFromFormPost { } ### Template attachments - my $f = $self->session->form; - my $p = $f->paramsHashRef; - my @nums = grep {$_} map { my ($i) = /^attachmentUrl(\d+)$/; $i } keys %$p; - my @add; - - # Remove all attachments first, then re-add whatever's left in the form - $self->removeAttachments; - - foreach my $n (@nums) { - my ($index, $type, $url) = - map { $f->process('attachment' . $_ . $n) } - qw(Index Type Url); - - push( - @add, { - sequence => $index, - url => $url, - type => $type, - } - ); - } - $self->addAttachments(\@add); + $self->update({ attachmentsJson => $f->process( 'attachmentsJson', 'JsonTable' ), }); return; }