+
+
+
+Returns (retval, msg);
+retval is 0 for failure or scrip id. msg is a textual description of what happened.
+
+=cut
+
+sub Create {
+ my $self = shift;
+ my %args = (
+ Queue => 0,
+ Template => undef, # name or id
+ ScripAction => 0, # name or id
+ ScripCondition => 0, # name or id
+ Stage => 'TransactionCreate',
+ Description => undef,
+ CustomPrepareCode => undef,
+ CustomCommitCode => undef,
+ CustomIsApplicableCode => undef,
+ ConditionRules => undef,
+ ActionRules => undef,
+ @_
+ );
+
+ if ($args{CustomPrepareCode} || $args{CustomCommitCode} || $args{CustomIsApplicableCode}) {
+ unless ( $self->CurrentUser->HasRight( Object => $RT::System,
+ Right => 'ExecuteCode' ) )
+ {
+ return ( 0, $self->loc('Permission Denied') );
+ }
+ }
+
+ unless ( $args{'Queue'} ) {
+ unless ( $self->CurrentUser->HasRight( Object => $RT::System,
+ Right => 'ModifyScrips' ) )
+ {
+ return ( 0, $self->loc('Permission Denied') );
+ }
+ $args{'Queue'} = 0; # avoid undef sneaking in
+ }
+ else {
+ my $QueueObj = RT::Queue->new( $self->CurrentUser );
+ $QueueObj->Load( $args{'Queue'} );
+ unless ( $QueueObj->id ) {
+ return ( 0, $self->loc('Invalid queue') );
+ }
+ unless ( $QueueObj->CurrentUserHasRight('ModifyScrips') ) {
+ return ( 0, $self->loc('Permission Denied') );
+ }
+ $args{'Queue'} = $QueueObj->id;
+ }
+
+ #TODO +++ validate input
+
+ return ( 0, $self->loc("Action is mandatory argument") )
+ unless $args{'ScripAction'};
+ my $action = RT::ScripAction->new( $self->CurrentUser );
+ $action->Load( $args{'ScripAction'} );
+ return ( 0, $self->loc( "Action '[_1]' not found", $args{'ScripAction'} ) )
+ unless $action->Id;
+
+ return ( 0, $self->loc("Template is mandatory argument") )
+ unless $args{'Template'};
+ my $template = RT::Template->new( $self->CurrentUser );
+ 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'};
+ }
+
+ return ( 0, $self->loc("Condition is mandatory argument") )
+ unless $args{'ScripCondition'};
+ my $condition = RT::ScripCondition->new( $self->CurrentUser );
+ $condition->Load( $args{'ScripCondition'} );
+ 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(
+ Template => $template->Name,
+ ScripCondition => $condition->id,
+ ScripAction => $action->Id,
+ Disabled => $args{'Disabled'},
+ Description => $args{'Description'},
+ CustomPrepareCode => $args{'CustomPrepareCode'},
+ CustomCommitCode => $args{'CustomCommitCode'},
+ CustomIsApplicableCode => $args{'CustomIsApplicableCode'},
+ ConditionRules => $args{'ConditionRules'},
+ ActionRules => $args{'ActionRules'},
+ );
+ 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') );