notify on custom field change, #11274
[freeside.git] / rt / lib / RT / Condition / CustomFieldTransaction.pm
1 package RT::Condition::CustomFieldTransaction;
2 use base 'RT::Condition';
3 use strict;
4
5 =head1 NAME
6
7 RT::Condition::CustomFieldTransaction
8
9 =head1 DESCRIPTION
10
11 Returns true if a transaction changed the value of a custom field.  Unlike 
12 CustomFieldChange, this condition doesn't care what the value was, only that 
13 it changed.
14
15 =head2 Parameters
16
17 =over 4
18
19 =item field (string)
20
21 Only return true if the transaction changed a custom field with this name.  
22 If empty, returns true for any CustomField-type transaction.
23
24 =item include_create (boolean) - Also return true for Create-type transactions.
25 If 'field' is specified, return true if that field is non-empty in the newly 
26 created object.
27
28 =back
29
30 =head2 IsApplicable
31
32 If the transaction has changed the value of a custom field.
33
34 =head1 BUGS
35
36 Probably interacts badly with multiple custom fields with the same name.
37
38 =cut
39
40 sub IsApplicable {
41     my $self = shift;
42     my $trans = $self->TransactionObj;
43     my $scrip = $self->ScripObj;
44     my %Rules = $self->Rules;
45     my ($field, $include_create) = @Rules{'field', 'include_create'};
46
47     if ( $include_create and $trans->Type eq 'Create' ) {
48         return 1 if !defined($field);
49         return 1 if defined($trans->TicketObj->FirstCustomFieldValue($field));
50     }
51     if ($trans->Type eq 'CustomField') {
52         return 1 if !defined($field);
53         my $cf = RT::CustomField->new($self->CurrentUser);
54         $cf->Load($field);
55         return 1 if defined($cf->Id) and $trans->Field == $cf->Id;
56     }
57     return undef;
58 }
59
60 sub Options {
61   my $self = shift;
62   my %args = ( 'QueueObj' => undef, @_ );
63   my $cfs = RT::CustomFields->new($self->CurrentUser);
64   # Allow any ticket custom field to be selected; if it doesn't apply to the 
65   # ticket, it will never contain a value and that's fine.
66   $cfs->LimitToLookupType('RT::Queue-RT::Ticket');
67   my @fieldnames = ('', '(any field)');
68   while ( my $cf = $cfs->Next ) {
69     push @fieldnames, $cf->Name, $cf->Name;
70   }
71   return (
72     { 
73       'name'    => 'field',
74       'label'   => 'Custom Field',
75       'type'    => 'select',
76       'options' => \@fieldnames,
77     },
78     {
79       'name'    => 'include_create',
80       'label'   => 'Trigger on ticket creation',
81       'type'    => 'checkbox',
82     },
83   );
84 }
85 1;
86