is not defined, will remove all attachments for this revision.
=cut
sub removeAttachments {
my ($self, $urls) = @_;
my @attachments = ();
if ($urls) {
@attachments = grep { ! ($_->{url} ~~ $urls) } @{ $self->getAttachments() };
}
my $json = JSON->new->encode( \@attachments );
$self->update({ attachmentsJson => $json, });
}
#----------------------------------------------------------------------------
=head2 replaceParamName ( oldName, newName )
Replace all instances of oldName with newName. Updates the template instance with
the new names and returns the new template data. This is only to be used to alter
the names of template parameters.
=cut
sub replaceParamName {
my ( $self, $oldName, $newName ) = @_;
# We're lazy here. If this fails, we'll add more checks, or call out to the parser
my $template = $self->template;
$template =~ s/$oldName/$newName/g;
$self->template( $template );
return $template;
}
#-------------------------------------------------------------------
=head2 www_edit
Hand draw this form so that a warning can be displayed to the user when editing a
default template.
=cut
override www_edit => sub {
my $self = shift;
return $self->session->privilege->insufficient() unless $self->canEdit;
return $self->session->privilege->locked() unless $self->canEditIfLocked;
my $session = $self->session;
my $form = $session->form;
my $url = $session->url;
my $i18n = WebGUI::International->new($session, "Asset_Template");
my $template = super();
# Add an unfriendly warning message if this is a default template
if ( $self->get( 'isDefault' ) ) {
# Get a proper URL to make the duplicate
my $duplicateUrl = $self->getUrl( "func=editDuplicate" );
if ( $form->get( "proceed" ) ) {
$duplicateUrl = $url->append( $duplicateUrl, "proceed=" . $form->get( "proceed" ) );
if ( $form->get( "returnUrl" ) ) {
$duplicateUrl = $url->append( $duplicateUrl, "returnUrl=" . $form->get( "returnUrl" ) );
}
}
my $errors = $template->getParam('errors') || [];
my $message .= q{}
. $i18n->get( "warning default template" )
. q{
}
. sprintf( q{%s}, $i18n->get( "make duplicate label" ) )
. q{
}
;
push @$errors, $message;
$template->setParam( 'errors' => $errors );
}
return $template;
};
#-------------------------------------------------------------------
=head2 www_goBackToPage
If set, redirect the user to the URL set by the form variable C. Otherwise, it returns
the user back to the site.
=cut
sub www_goBackToPage {
my $self = shift;
$self->session->response->setRedirect($self->session->form->get("returnUrl")) if ($self->session->form->get("returnUrl"));
return undef;
}
#----------------------------------------------------------------------------
=head2 www_editDuplicate
Make a duplicate of this template and edit that instead.
=cut
sub www_editDuplicate {
my $self = shift;
return $self->session->privilege->insufficient() unless $self->canEdit;
my $session = $self->session;
my $form = $self->session->form;
my $newTemplate = $self->duplicate;
$newTemplate->update( {
isDefault => 0,
title => $self->get( "title" ) . " (copy)",
menuTitle => $self->get( "menuTitle" ) . " (copy)",
} );
# Make the asset that originally invoked edit template use the newly created asset.
if ( $self->session->form->get( "proceed" ) eq "goBackToPage" ) {
if ( my $asset = WebGUI::Asset->newByUrl( $session, $form->get( "returnUrl" ) ) ) {
# Find which property we should set by comparing namespaces and current values
DEF: for my $def ( @{ $asset->definition( $self->session ) } ) {
my $properties = $def->{ properties };
PROP: for my $prop ( keys %{ $properties } ) {
next PROP unless lc $properties->{ $prop }->{ fieldType } eq "template";
next PROP unless $asset->get( $prop ) eq $self->getId;
if ( $properties->{ $prop }->{ namespace } eq $self->get( "namespace" ) ) {
my $tag = WebGUI::VersionTag->getWorking( $session );
$asset->addRevision( { $prop => $newTemplate->getId, tagId => $tag->getId, status => "pending" } );
$asset->setVersionLock;
# Auto-commit our revision if necessary
# TODO: This needs to be handled automatically somehow...
my $status = WebGUI::VersionTag->autoCommitWorkingIfEnabled($self->session);
##get a fresh object from the database
if ($status eq 'commit') {
$newTemplate = $newTemplate->cloneFromDb;
}
last DEF;
}
}
}
}
}
return $newTemplate->www_edit;
}
#-------------------------------------------------------------------
=head2 www_manage
If trying to use the assetManager on this asset, push them back to managing the
template's parent instead.
=cut
sub www_manage {
my $self = shift;
#takes the user to the folder containing this template.
return $self->getParent->www_manageAssets;
}
#-------------------------------------------------------------------
=head2 www_preview
Rendes this template with the given variables (posted as JSON)
=cut
sub www_preview {
my $self = shift;
my $session = $self->session;
return $session->privilege->insufficient unless $self->canEdit;
my $form = $session->form;
my $http = $session->http;
try {
my $output = $self->processRaw(
$session,
$form->get('template'),
from_json($form->get('variables')),
$form->get('parser'),
);
if ($form->get('plainText')) {
$http->setMimeType('text/plain');
}
elsif ($output !~ //) {
$output = $session->style->userStyle($output);
}
return $output;
} catch {
$http->setMimeType('text/plain');
$_[0];
}
}
#-------------------------------------------------------------------
=head2 www_view
Override the default behavior. When a template is viewed, it redirects you
to viewing the template's container instead.
=cut
sub www_view {
my $self = shift;
return $self->session->asset($self->getContainer)->www_view;
}
__PACKAGE__->meta->make_immutable;
1;