communigate (phase 2): rules. RT#7514
[freeside.git] / FS / FS / cgp_rule.pm
1 package FS::cgp_rule;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::cust_svc;
7 use FS::cgp_rule_condition;
8 use FS::cgp_rule_action;
9
10 =head1 NAME
11
12 FS::cgp_rule - Object methods for cgp_rule records
13
14 =head1 SYNOPSIS
15
16   use FS::cgp_rule;
17
18   $record = new FS::cgp_rule \%hash;
19   $record = new FS::cgp_rule { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::cgp_rule object represents a mail filtering rule.  FS::cgp_rule
32 inherits from FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item rulenum
37
38 primary key
39
40 =item name
41
42 name
43
44 =item comment
45
46 comment
47
48 =item svcnum
49
50 svcnum
51
52 =item priority
53
54 priority
55
56
57 =back
58
59 =head1 METHODS
60
61 =over 4
62
63 =item new HASHREF
64
65 Creates a new rule.  To add the rule to the database, see L<"insert">.
66
67 Note that this stores the hash reference, not a distinct copy of the hash it
68 points to.  You can ask the object for a copy with the I<hash> method.
69
70 =cut
71
72 # the new method can be inherited from FS::Record, if a table method is defined
73
74 sub table { 'cgp_rule'; }
75
76 =item insert
77
78 Adds this record to the database.  If there is an error, returns the error,
79 otherwise returns false.
80
81 =cut
82
83 # the insert method can be inherited from FS::Record
84
85 =item delete
86
87 Delete this record from the database.
88
89 =cut
90
91 sub delete {
92   my $self = shift;
93
94   local $SIG{HUP} = 'IGNORE';
95   local $SIG{INT} = 'IGNORE';
96   local $SIG{QUIT} = 'IGNORE';
97   local $SIG{TERM} = 'IGNORE';
98   local $SIG{TSTP} = 'IGNORE';
99   local $SIG{PIPE} = 'IGNORE';
100
101   my $oldAutoCommit = $FS::UID::AutoCommit;
102   local $FS::UID::AutoCommit = 0;
103   my $dbh = dbh;
104
105   my @del = $self->cgp_rule_condition;
106   push @del, $self->cgp_rule_action;
107
108   foreach my $del (@del) {
109     my $error = $del->delete;
110     if ( $error ) {
111       $dbh->rollback if $oldAutoCommit;
112       return $error;
113     }
114   }
115
116   my $error = $self->SUPER::delete(@_);
117   if ( $error ) {
118     $dbh->rollback if $oldAutoCommit;
119     return $error;
120   }
121
122   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
123
124 }
125
126 =item replace OLD_RECORD
127
128 Replaces the OLD_RECORD with this one in the database.  If there is an error,
129 returns the error, otherwise returns false.
130
131 =cut
132
133 # the replace method can be inherited from FS::Record
134
135 =item check
136
137 Checks all fields to make sure this is a valid rule.  If there is
138 an error, returns the error, otherwise returns false.  Called by the insert
139 and replace methods.
140
141 =cut
142
143 # the check method should currently be supplied - FS::Record contains some
144 # data checking routines
145
146 sub check {
147   my $self = shift;
148
149   my $error = 
150     $self->ut_numbern('rulenum')
151     || $self->ut_text('name')
152     || $self->ut_textn('comment')
153     || $self->ut_foreign_key('svcnum', 'cust_svc', 'svcnum')
154     || $self->ut_number('priority')
155   ;
156   return $error if $error;
157
158   $self->SUPER::check;
159 }
160
161 =item cust_svc
162
163 =cut
164
165 sub cust_svc {
166   my $self = shift;
167   qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
168 }
169
170 =item cgp_rule_condition
171
172 Returns the conditions associated with this rule, as FS::cgp_rule_condition
173 objects.
174
175 =cut
176
177 sub cgp_rule_condition {
178   my $self = shift;
179   qsearch('cgp_rule_condition', { 'rulenum' => $self->rulenum } );
180 }
181
182 =item cgp_rule_action
183
184 Returns the actions associated with this rule, as FS::cgp_rule_action
185 objects.
186
187 =cut
188
189 sub cgp_rule_action {
190   my $self = shift;
191   qsearch('cgp_rule_action', { 'rulenum' => $self->rulenum } );
192 }
193
194 =back
195
196 =head1 BUGS
197
198 =head1 SEE ALSO
199
200 L<FS::Record>, schema.html from the base documentation.
201
202 =cut
203
204 1;
205