X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FScrip.pm;h=48547ccfd2316e2bc3590037fd93e90dfc11d206;hp=950661624cc627e2d39af28b288d47f5a3a5b9a0;hb=7322f2afedcc2f427e997d1535a503613a83f088;hpb=85e677b86fc37c54e6de2b06340351a28f5a5916 diff --git a/rt/lib/RT/Scrip.pm b/rt/lib/RT/Scrip.pm index 950661624..48547ccfd 100755 --- a/rt/lib/RT/Scrip.pm +++ b/rt/lib/RT/Scrip.pm @@ -2,7 +2,7 @@ # # COPYRIGHT: # -# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC +# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -67,13 +67,14 @@ package RT::Scrip; use strict; use warnings; - +use base 'RT::Record'; use RT::Queue; use RT::Template; use RT::ScripCondition; use RT::ScripAction; -use base 'RT::Record'; +use RT::Scrips; +use RT::ObjectScrip; sub Table {'Scrips'} @@ -104,7 +105,7 @@ sub Create { my $self = shift; my %args = ( Queue => 0, - Template => 0, # name or id + Template => undef, # name or id ScripAction => 0, # name or id ScripCondition => 0, # name or id Stage => 'TransactionCreate', @@ -112,8 +113,6 @@ sub Create { CustomPrepareCode => undef, CustomCommitCode => undef, CustomIsApplicableCode => undef, - ConditionRules => undef, - ActionRules => undef, @_ ); @@ -147,7 +146,6 @@ sub Create { #TODO +++ validate input - require RT::ScripAction; return ( 0, $self->loc("Action is mandatory argument") ) unless $args{'ScripAction'}; my $action = RT::ScripAction->new( $self->CurrentUser ); @@ -155,15 +153,26 @@ sub Create { return ( 0, $self->loc( "Action '[_1]' not found", $args{'ScripAction'} ) ) unless $action->Id; - require RT::Template; return ( 0, $self->loc("Template is mandatory argument") ) unless $args{'Template'}; my $template = RT::Template->new( $self->CurrentUser ); - $template->Load( $args{'Template'} ); - return ( 0, $self->loc( "Template '[_1]' not found", $args{'Template'} ) ) - unless $template->Id; + if ( $args{'Template'} =~ /\D/ ) { + $template->LoadByName( Name => $args{'Template'}, Queue => $args{'Queue'} ); + return ( 0, $self->loc( "Global template '[_1]' not found", $args{'Template'} ) ) + if !$template->Id && !$args{'Queue'}; + return ( 0, $self->loc( "Global or queue specific template '[_1]' not found", $args{'Template'} ) ) + if !$template->Id; + } else { + $template->Load( $args{'Template'} ); + return ( 0, $self->loc( "Template '[_1]' not found", $args{'Template'} ) ) + unless $template->Id; + + return (0, $self->loc( "Template '[_1]' is not global" )) + if !$args{'Queue'} && $template->Queue; + return (0, $self->loc( "Template '[_1]' is not global nor queue specific" )) + if $args{'Queue'} && $template->Queue && $template->Queue != $args{'Queue'}; + } - require RT::ScripCondition; return ( 0, $self->loc("Condition is mandatory argument") ) unless $args{'ScripCondition'}; my $condition = RT::ScripCondition->new( $self->CurrentUser ); @@ -171,25 +180,33 @@ sub Create { return ( 0, $self->loc( "Condition '[_1]' not found", $args{'ScripCondition'} ) ) unless $condition->Id; + if ( $args{'Stage'} eq 'Disabled' ) { + $RT::Logger->warning("Disabled Stage is deprecated"); + $args{'Stage'} = 'TransactionCreate'; + $args{'Disabled'} = 1; + } + $args{'Disabled'} ||= 0; + my ( $id, $msg ) = $self->SUPER::Create( - Queue => $args{'Queue'}, - Template => $template->Id, + Template => $template->Name, ScripCondition => $condition->id, - Stage => $args{'Stage'}, ScripAction => $action->Id, + Disabled => $args{'Disabled'}, Description => $args{'Description'}, CustomPrepareCode => $args{'CustomPrepareCode'}, CustomCommitCode => $args{'CustomCommitCode'}, CustomIsApplicableCode => $args{'CustomIsApplicableCode'}, - ConditionRules => $args{'ConditionRules'}, - ActionRules => $args{'ActionRules'}, ); - if ( $id ) { - return ( $id, $self->loc('Scrip Created') ); - } - else { - return ( $id, $msg ); - } + return ( $id, $msg ) unless $id; + + (my $status, $msg) = RT::ObjectScrip->new( $self->CurrentUser )->Add( + Scrip => $self, + Stage => $args{'Stage'}, + ObjectId => $args{'Queue'}, + ); + $RT::Logger->error( "Couldn't add scrip: $msg" ) unless $status; + + return ( $id, $self->loc('Scrip Created') ); } @@ -207,33 +224,158 @@ sub Delete { return ( 0, $self->loc('Permission Denied') ); } + RT::ObjectScrip->new( $self->CurrentUser )->DeleteAll( Scrip => $self ); + return ( $self->SUPER::Delete(@_) ); } +sub IsGlobal { return shift->IsAdded(0) } + +sub IsAdded { + my $self = shift; + my $record = RT::ObjectScrip->new( $self->CurrentUser ); + $record->LoadByCols( Scrip => $self->id, ObjectId => shift || 0 ); + return undef unless $record->id; + return $record; +} + +sub IsAddedToAny { + my $self = shift; + my $record = RT::ObjectScrip->new( $self->CurrentUser ); + $record->LoadByCols( Scrip => $self->id ); + return $record->id ? 1 : 0; +} + +sub AddedTo { + my $self = shift; + return RT::ObjectScrip->new( $self->CurrentUser ) + ->AddedTo( Scrip => $self ); +} + +sub NotAddedTo { + my $self = shift; + return RT::ObjectScrip->new( $self->CurrentUser ) + ->NotAddedTo( Scrip => $self ); +} + +=head2 AddToObject + +Adds (applies) the current scrip to the provided queue (ObjectId). +Accepts a param hash of: -=head2 QueueObj +=over -Retuns an RT::Queue object with this Scrip's queue +=item C + +Queue name or id. 0 makes the scrip global. + +=item C + +Stage to run in. Valid stages are TransactionCreate or +TransactionBatch. Defaults to TransactionCreate. As of RT 4.2, Disabled +is no longer a stage. + +=item C