1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
use strict;
use warnings;
package RT::Lifecycle;
our %LIFECYCLES;
our %LIFECYCLES_CACHE;
__PACKAGE__->RegisterRights;
# cache structure:
# {
# '' => { # all valid statuses
# '' => [...],
# initial => [...],
# active => [...],
# inactive => [...],
# },
# lifecycle_x => {
# '' => [...], # all valid in lifecycle
# initial => [...],
# active => [...],
# inactive => [...],
# transitions => {
# status_x => [status_next1, status_next2,...],
# },
# rights => {
# 'status_y -> status_y' => 'right',
# ....
# }
# actions => [
# { from => 'a', to => 'b', label => '...', update => '...' },
# ....
# ]
# }
# }
=head1 NAME
RT::Lifecycle - class to access and manipulate lifecycles
=head1 DESCRIPTION
A lifecycle is a list of statuses that a ticket can have. There are three
groups of statuses: initial, active and inactive. A lifecycle also defines
possible transitions between statuses. For example, in the 'default' lifecycle,
you may only change status from 'stalled' to 'open'.
It is also possible to define user-interface labels and the action a user
should perform during a transition. For example, the "open -> stalled"
transition would have a 'Stall' label and the action would be Comment. The
action only defines what form is showed to the user, but actually performing
the action is not required. The user can leave the comment box empty yet still
Stall a ticket. Finally, the user can also just use the Basics or Jumbo form to
change the status with the usual dropdown.
=head1 METHODS
=head2 new
Simple constructor, takes no arguments.
=cut
sub new {
my $proto = shift;
my $self = bless {}, ref($proto) || $proto;
$self->FillCache unless keys %LIFECYCLES_CACHE;
return $self;
}
=head2 Load
Takes a name of the lifecycle and loads it. If name is empty or undefined then
loads the global lifecycle with statuses from all named lifecycles.
Can be called as class method, returns a new object, for example:
my $lifecycle = RT::Lifecycle->Load('default');
=cut
sub Load {
my $self = shift;
my $name = shift || '';
return $self->new->Load( $name, @_ )
unless ref $self;
return unless exists $LIFECYCLES_CACHE{ $name };
$self->{'name'} = $name;
$self->{'data'} = $LIFECYCLES_CACHE{ $name };
return $self;
}
=head2 List
Returns sorted list of the lifecycles' names.
=cut
sub List {
my $self = shift;
$self->FillCache unless keys %LIFECYCLES_CACHE;
return sort grep length && $_ ne '__maps__', keys %LIFECYCLES_CACHE;
}
=head2 Name
Returns name of the laoded lifecycle.
=cut
sub Name { return $_[0]->{'name'} }
=head2 Queues
Returns L<RT::Queues> collection with queues that use this lifecycle.
=cut
sub Queues {
my $self = shift;
require RT::Queues;
my $queues = RT::Queues->new( RT->SystemUser );
$queues->Limit( FIELD => 'Lifecycle', VALUE => $self->Name );
return $queues;
}
=head2 Getting statuses and validating.
Methods to get statuses in different sets or validating them.
=head3 Valid
Returns an array of all valid statuses for the current lifecycle.
Statuses are not sorted alphabetically, instead initial goes first,
then active and then inactive.
Takes optional list of status types, from 'initial', 'active' or
'inactive'. For example:
$lifecycle->Valid('initial', 'active');
=cut
sub Valid {
my $self = shift;
my @types = @_;
unless ( @types ) {
return @{ $self->{'data'}{''} || [] };
}
my @res;
push @res, @{ $self->{'data'}{ $_ } || [] } foreach @types;
return @res;
}
=head3 IsValid
Takes a status and returns true if value is a valid status for the current
lifecycle. Otherwise, returns false.
Takes optional list of status types after the status, so it's possible check
validity in particular sets, for example:
# returns true if status is valid and from initial or active set
$lifecycle->IsValid('some_status', 'initial', 'active');
See also </valid>.
=cut
sub IsValid {
my $self = shift;
my $value = shift or return 0;
return 1 if grep lc($_) eq lc($value), $self->Valid( @_ );
return 0;
}
=head3 StatusType
Takes a status and returns its type, one of 'initial', 'active' or
'inactive'.
=cut
sub StatusType {
my $self = shift;
my $status = shift;
foreach my $type ( qw(initial active inactive) ) {
return $type if $self->IsValid( $status, $type );
}
return '';
}
=head3 Initial
Returns an array of all initial statuses for the current lifecycle.
=cut
sub Initial {
my $self = shift;
return $self->Valid('initial');
}
=head3 IsInitial
Takes a status and returns true if value is a valid initial status.
Otherwise, returns false.
=cut
sub IsInitial {
my $self = shift;
my $value = shift or return 0;
return 1 if grep lc($_) eq lc($value), $self->Valid('initial');
return 0;
}
=head3 Active
Returns an array of all active statuses for this lifecycle.
=cut
sub Active {
my $self = shift;
return $self->Valid('active');
}
=head3 IsActive
Takes a value and returns true if value is a valid active status.
Otherwise, returns false.
=cut
sub IsActive {
my $self = shift;
my $value = shift or return 0;
return 1 if grep lc($_) eq lc($value), $self->Valid('active');
return 0;
}
=head3 inactive
Returns an array of all inactive statuses for this lifecycle.
=cut
sub Inactive {
my $self = shift;
return $self->Valid('inactive');
}
=head3 is_inactive
Takes a value and returns true if value is a valid inactive status.
Otherwise, returns false.
=cut
sub IsInactive {
my $self = shift;
my $value = shift or return 0;
return 1 if grep lc($_) eq lc($value), $self->Valid('inactive');
return 0;
}
=head2 Default statuses
In some cases when status is not provided a default values should
be used.
=head3 DefaultStatus
Takes a situation name and returns value. Name should be
spelled following spelling in the RT config file.
=cut
sub DefaultStatus {
my $self = shift;
my $situation = shift;
return $self->{data}{defaults}{ $situation };
}
=head3 DefaultOnCreate
Returns the status that should be used by default
when ticket is created.
=cut
sub DefaultOnCreate {
my $self = shift;
return $self->DefaultStatus('on_create');
}
=head3 DefaultOnMerge
Returns the status that should be used when tickets
are merged.
=cut
sub DefaultOnMerge {
my $self = shift;
return $self->DefaultStatus('on_merge');
}
=head2 Transitions, rights, labels and actions.
=head3 Transitions
Takes status and returns list of statuses it can be changed to.
Is status is empty or undefined then returns list of statuses for
a new ticket.
If argument is ommitted then returns a hash with all possible
transitions in the following format:
status_x => [ next_status, next_status, ... ],
status_y => [ next_status, next_status, ... ],
=cut
sub Transitions {
my $self = shift;
return %{ $self->{'data'}{'transitions'} || {} }
unless @_;
my $status = shift;
return @{ $self->{'data'}{'transitions'}{ $status || '' } || [] };
}
=head1 IsTransition
Takes two statuses (from -> to) and returns true if it's valid
transition and false otherwise.
=cut
sub IsTransition {
my $self = shift;
my $from = shift;
my $to = shift or return 0;
return 1 if grep lc($_) eq lc($to), $self->Transitions($from);
return 0;
}
=head3 CheckRight
Takes two statuses (from -> to) and returns the right that should
be checked on the ticket.
=cut
sub CheckRight {
my $self = shift;
my $from = shift;
my $to = shift;
if ( my $rights = $self->{'data'}{'rights'} ) {
my $check =
$rights->{ $from .' -> '. $to }
|| $rights->{ '* -> '. $to }
|| $rights->{ $from .' -> *' }
|| $rights->{ '* -> *' };
return $check if $check;
}
return $to eq 'deleted' ? 'DeleteTicket' : 'ModifyTicket';
}
=head3 RegisterRights
Registers all defined rights in the system, so they can be addigned
to users. No need to call it, as it's called when module is loaded.
=cut
sub RegisterRights {
my $self = shift;
my %rights = $self->RightsDescription;
require RT::ACE;
require RT::Queue;
my $RIGHTS = $RT::Queue::RIGHTS;
while ( my ($right, $description) = each %rights ) {
next if exists $RIGHTS->{ $right };
$RIGHTS->{ $right } = $description;
RT::Queue->AddRightCategories( $right => 'Status' );
$RT::ACE::LOWERCASERIGHTNAMES{ lc $right } = $right;
}
}
=head3 RightsDescription
Returns hash with description of rights that are defined for
particular transitions.
=cut
sub RightsDescription {
my $self = shift;
$self->FillCache unless keys %LIFECYCLES_CACHE;
my %tmp;
foreach my $lifecycle ( values %LIFECYCLES_CACHE ) {
next unless exists $lifecycle->{'rights'};
while ( my ($transition, $right) = each %{ $lifecycle->{'rights'} } ) {
push @{ $tmp{ $right } ||=[] }, $transition;
}
}
my %res;
while ( my ($right, $transitions) = each %tmp ) {
my (@from, @to);
foreach ( @$transitions ) {
($from[@from], $to[@to]) = split / -> /, $_;
}
my $description = 'Change status'
. ( (grep $_ eq '*', @from)? '' : ' from '. join ', ', @from )
. ( (grep $_ eq '*', @to )? '' : ' to '. join ', ', @to );
$res{ $right } = $description;
}
return %res;
}
=head3 Actions
Takes a status and returns list of defined actions for the status. Each
element in the list is a hash reference with the following key/value
pairs:
=over 4
=item from - either the status or *
=item to - next status
=item label - label of the action
=item update - 'Respond', 'Comment' or '' (empty string)
=back
=cut
sub Actions {
my $self = shift;
my $from = shift || return ();
$self->FillCache unless keys %LIFECYCLES_CACHE;
my @res = grep $_->{'from'} eq $from || ( $_->{'from'} eq '*' && $_->{'to'} ne $from ),
@{ $self->{'data'}{'actions'} };
# skip '* -> x' if there is '$from -> x'
foreach my $e ( grep $_->{'from'} eq '*', @res ) {
$e = undef if grep $_->{'from'} ne '*' && $_->{'to'} eq $e->{'to'}, @res;
}
return grep defined, @res;
}
=head2 Moving tickets between lifecycles
=head3 MoveMap
Takes lifecycle as a name string or an object and returns a hash reference with
move map from this cycle to provided.
=cut
sub MoveMap {
my $from = shift; # self
my $to = shift;
$to = RT::Lifecycle->Load( $to ) unless ref $to;
return $LIFECYCLES{'__maps__'}{ $from->Name .' -> '. $to->Name } || {};
}
=head3 HasMoveMap
Takes a lifecycle as a name string or an object and returns true if move map
defined for move from this cycle to provided.
=cut
sub HasMoveMap {
my $self = shift;
my $map = $self->MoveMap( @_ );
return 0 unless $map && keys %$map;
return 0 unless grep defined && length, values %$map;
return 1;
}
=head3 NoMoveMaps
Takes no arguments and returns hash with pairs that has no
move maps.
=cut
sub NoMoveMaps {
my $self = shift;
my @list = $self->List;
my @res;
foreach my $from ( @list ) {
foreach my $to ( @list ) {
next if $from eq $to;
push @res, $from, $to
unless RT::Lifecycle->Load( $from )->HasMoveMap( $to );
}
}
return @res;
}
=head2 Localization
=head3 ForLocalization
A class method that takes no arguments and returns list of strings
that require translation.
=cut
sub ForLocalization {
my $self = shift;
$self->FillCache unless keys %LIFECYCLES_CACHE;
my @res = ();
push @res, @{ $LIFECYCLES_CACHE{''}{''} || [] };
foreach my $lifecycle ( values %LIFECYCLES ) {
push @res,
grep defined && length,
map $_->{'label'},
grep ref($_),
@{ $lifecycle->{'actions'} || [] };
}
push @res, $self->RightsDescription;
my %seen;
return grep !$seen{lc $_}++, @res;
}
sub loc { return RT->SystemUser->loc( @_ ) }
sub FillCache {
my $self = shift;
my $map = RT->Config->Get('Lifecycles') or return;
%LIFECYCLES_CACHE = %LIFECYCLES = %$map;
$_ = { %$_ } foreach values %LIFECYCLES_CACHE;
my %all = (
'' => [],
initial => [],
active => [],
inactive => [],
);
foreach my $lifecycle ( values %LIFECYCLES_CACHE ) {
my @res;
foreach my $type ( qw(initial active inactive) ) {
push @{ $all{ $type } }, @{ $lifecycle->{ $type } || [] };
push @res, @{ $lifecycle->{ $type } || [] };
}
my %seen;
@res = grep !$seen{ lc $_ }++, @res;
$lifecycle->{''} = \@res;
unless ( $lifecycle->{'transitions'}{''} ) {
$lifecycle->{'transitions'}{''} = [ grep $_ ne 'deleted', @res ];
}
}
foreach my $type ( qw(initial active inactive), '' ) {
my %seen;
@{ $all{ $type } } = grep !$seen{ lc $_ }++, @{ $all{ $type } };
push @{ $all{''} }, @{ $all{ $type } } if $type;
}
$LIFECYCLES_CACHE{''} = \%all;
foreach my $lifecycle ( values %LIFECYCLES_CACHE ) {
my @res;
if ( ref $lifecycle->{'actions'} eq 'HASH' ) {
foreach my $k ( sort keys %{ $lifecycle->{'actions'} } ) {
push @res, $k, $lifecycle->{'actions'}{ $k };
}
} elsif ( ref $lifecycle->{'actions'} eq 'ARRAY' ) {
@res = @{ $lifecycle->{'actions'} };
}
my @tmp = splice @res;
while ( my ($transition, $info) = splice @tmp, 0, 2 ) {
my ($from, $to) = split /\s*->\s*/, $transition, 2;
push @res, { %$info, from => $from, to => $to };
}
$lifecycle->{'actions'} = \@res;
}
return;
}
1;
|