4 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::cgp_rule_condition;
8 use FS::cgp_rule_action;
12 FS::cgp_rule - Object methods for cgp_rule records
18 $record = new FS::cgp_rule \%hash;
19 $record = new FS::cgp_rule { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
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:
65 Creates a new rule. To add the rule to the database, see L<"insert">.
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.
72 # the new method can be inherited from FS::Record, if a table method is defined
74 sub table { 'cgp_rule'; }
78 Adds this record to the database. If there is an error, returns the error,
79 otherwise returns false.
86 local $SIG{HUP} = 'IGNORE';
87 local $SIG{INT} = 'IGNORE';
88 local $SIG{QUIT} = 'IGNORE';
89 local $SIG{TERM} = 'IGNORE';
90 local $SIG{TSTP} = 'IGNORE';
91 local $SIG{PIPE} = 'IGNORE';
93 my $oldAutoCommit = $FS::UID::AutoCommit;
94 local $FS::UID::AutoCommit = 0;
97 my $error = $self->SUPER::insert(@_);
99 $dbh->rollback if $oldAutoCommit;
103 #conditions and actions not in yet
104 #$error = $self->svc_export;
106 # $dbh->rollback if $oldAutoCommit;
110 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
116 Delete this record from the database.
123 local $SIG{HUP} = 'IGNORE';
124 local $SIG{INT} = 'IGNORE';
125 local $SIG{QUIT} = 'IGNORE';
126 local $SIG{TERM} = 'IGNORE';
127 local $SIG{TSTP} = 'IGNORE';
128 local $SIG{PIPE} = 'IGNORE';
130 my $oldAutoCommit = $FS::UID::AutoCommit;
131 local $FS::UID::AutoCommit = 0;
134 my @del = $self->cgp_rule_condition;
135 push @del, $self->cgp_rule_action;
137 foreach my $del (@del) {
138 my $error = $del->delete;
140 $dbh->rollback if $oldAutoCommit;
145 my $error = $self->SUPER::delete(@_);
147 $dbh->rollback if $oldAutoCommit;
151 $error = $self->svc_export;
153 $dbh->rollback if $oldAutoCommit;
157 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
161 =item replace OLD_RECORD
163 Replaces the OLD_RECORD with this one in the database. If there is an error,
164 returns the error, otherwise returns false.
171 my $old = ( ref($_[0]) eq ref($new) )
175 local $SIG{HUP} = 'IGNORE';
176 local $SIG{INT} = 'IGNORE';
177 local $SIG{QUIT} = 'IGNORE';
178 local $SIG{TERM} = 'IGNORE';
179 local $SIG{TSTP} = 'IGNORE';
180 local $SIG{PIPE} = 'IGNORE';
182 my $oldAutoCommit = $FS::UID::AutoCommit;
183 local $FS::UID::AutoCommit = 0;
186 my $error = $new->SUPER::replace($old, @_);
188 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
192 #conditions and actions not in yet
193 #$error = $new->svc_export;
195 # $dbh->rollback if $oldAutoCommit;
199 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
206 Calls the replace export for any communigate exports attached to this rule's
214 my $cust_svc = $self->cust_svc;
215 my $svc_x = $cust_svc->svc_x;
218 my @exports = $cust_svc->part_svc->part_export('communigate_pro');
219 my @errors = map $_->export_replace($svc_x, $svc_x), @exports;
221 @errors ? join(' / ', @errors) : '';
227 Checks all fields to make sure this is a valid rule. If there is
228 an error, returns the error, otherwise returns false. Called by the insert
233 # the check method should currently be supplied - FS::Record contains some
234 # data checking routines
240 $self->ut_numbern('rulenum')
241 || $self->ut_text('name')
242 || $self->ut_textn('comment')
243 || $self->ut_foreign_key('svcnum', 'cust_svc', 'svcnum')
244 || $self->ut_number('priority')
246 return $error if $error;
251 =item clone NEW_SVCNUM
253 Clones this rule into an identical rule for the specified new service.
255 If there is an error, returns the error, otherwise returns false.
259 #should return the newly inserted rule instead? used in misc/clone-cgp_rule.html
261 #i should probably be transactionalized so i'm all-or-nothing
263 my( $self, $svcnum ) = @_;
265 my $new = $self->new( { $self->hash } );
267 $new->svcnum( $svcnum );
268 my $error = $new->insert;
269 return $error if $error;
271 my @dup = $self->cgp_rule_condition;
272 push @dup, $self->cgp_rule_action;
274 foreach my $dup (@dup) {
275 my $new_dup = $dup->new( { $dup->hash } );
276 my $pk = $new_dup->primary_key;
278 $new_dup->rulenum( $new->rulenum );
280 $error = $new_dup->insert;
281 return $error if $error;
285 $error = $new->svc_export;
286 return $error if $error;
298 qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
301 =item cgp_rule_condition
303 Returns the conditions associated with this rule, as FS::cgp_rule_condition
308 sub cgp_rule_condition {
310 qsearch('cgp_rule_condition', { 'rulenum' => $self->rulenum } );
313 =item cgp_rule_action
315 Returns the actions associated with this rule, as FS::cgp_rule_action
320 sub cgp_rule_action {
322 qsearch('cgp_rule_action', { 'rulenum' => $self->rulenum } );
327 Returns an arraref representing this rule, suitable for Communigate Pro API
330 The first element specifies the rule priority.
332 The second element specifies the rule name.
334 The third element specifies the rule conditions.
336 The fourth element specifies the rule actions.
338 The fifth element specifies the rule comment.
346 [ map $_->arrayref, $self->cgp_rule_condition ],
347 [ map $_->arrayref, $self->cgp_rule_action ],
358 L<FS::Record>, schema.html from the base documentation.