Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / lib / RT / Action / SetStatus.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::Action::SetStatus;
50
51 use strict;
52 use warnings;
53 use base qw(RT::Action);
54
55 =head1 NAME
56
57 RT::Action::SetStatus - RT's scrip action to set status of a ticket
58
59 =head1 DESCRIPTION
60
61 This action changes status to a new value according to the rules in L</ARGUMENT>.
62 Status is not changed if the transition is invalid or another error occurs. All
63 issues are logged at apropriate levels.
64
65 =head1 ARGUMENT
66
67 Argument can be one of the following:
68
69 =over 4
70
71 =item status literally
72
73 Status is changed from the current value to a new defined by the argument,
74 but only if it's valid status and allowed by transitions of the current lifecycle,
75 for example:
76
77     * The current status is 'stalled'
78     * Argument of this action is 'open'
79     * The only possible transition in the scheam from 'stalled' is 'open'
80     * Status is changed
81
82 However, in the example above Status is not changed if argument is anything
83 else as it's just not allowed by the lifecycle.
84
85 =item 'initial', 'active' or 'inactive'
86
87 Status is changed from the current value to first possible 'initial',
88 'active' or 'inactive' correspondingly. First possible value is figured
89 according to transitions to the target set, for example:
90
91     * The current status is 'open'
92     * Argument of this action is 'inactive'
93     * Possible transitions from 'open' are 'resolved', 'rejected' or 'deleted'
94     * Status is changed to 'resolved'
95
96 =back
97
98 =cut
99
100 sub Prepare {
101     my $self = shift;
102
103     my $ticket = $self->TicketObj;
104     my $lifecycle = $ticket->LifecycleObj;
105     my $status = $ticket->Status;
106
107     my $argument = $self->Argument;
108     unless ( $argument ) {
109         $RT::Logger->error("Argument is mandatory for SetStatus action");
110         return 0;
111     }
112
113     my $next = '';
114     if ( $argument =~ /^(initial|active|inactive)$/i ) {
115         my $method = 'Is'. ucfirst lc $argument;
116         ($next) = grep $lifecycle->$method($_), $lifecycle->Transitions($status);
117         unless ( $next ) {
118             $RT::Logger->info("No transition from '$status' to $argument set");
119             return 1;
120         }
121     }
122     elsif ( $lifecycle->IsValid( $argument ) ) {
123         unless ( $lifecycle->IsTransition( $status => $argument ) ) {
124             $RT::Logger->warning("Transition '$status -> $argument' is not valid");
125             return 1;
126         }
127         $next = $argument;
128     }
129     else {
130         $RT::Logger->error("Argument for SetStatus action is not valid status or one of set");
131         return 0;
132     }
133
134     $self->{'set_status_to'} = $next;
135
136     return 1;
137 }
138
139 sub Commit {
140     my $self = shift;
141
142     return 1 unless my $new_status = $self->{'set_status_to'};
143
144     my ($val, $msg) = $self->TicketObj->SetStatus( $new_status );
145     unless ( $val ) {
146         $RT::Logger->error( "Couldn't set status: ". $msg );
147         return 0;
148     }
149     return 1;
150 }
151
152 1;