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 $error = $self->svc_export;
105 $dbh->rollback if $oldAutoCommit;
109 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
115 Delete this record from the database.
122 local $SIG{HUP} = 'IGNORE';
123 local $SIG{INT} = 'IGNORE';
124 local $SIG{QUIT} = 'IGNORE';
125 local $SIG{TERM} = 'IGNORE';
126 local $SIG{TSTP} = 'IGNORE';
127 local $SIG{PIPE} = 'IGNORE';
129 my $oldAutoCommit = $FS::UID::AutoCommit;
130 local $FS::UID::AutoCommit = 0;
133 my @del = $self->cgp_rule_condition;
134 push @del, $self->cgp_rule_action;
136 foreach my $del (@del) {
137 my $error = $del->delete;
139 $dbh->rollback if $oldAutoCommit;
144 my $error = $self->SUPER::delete(@_);
146 $dbh->rollback if $oldAutoCommit;
150 $error = $self->svc_export;
152 $dbh->rollback if $oldAutoCommit;
156 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
160 =item replace OLD_RECORD
162 Replaces the OLD_RECORD with this one in the database. If there is an error,
163 returns the error, otherwise returns false.
170 my $old = ( ref($_[0]) eq ref($new) )
174 local $SIG{HUP} = 'IGNORE';
175 local $SIG{INT} = 'IGNORE';
176 local $SIG{QUIT} = 'IGNORE';
177 local $SIG{TERM} = 'IGNORE';
178 local $SIG{TSTP} = 'IGNORE';
179 local $SIG{PIPE} = 'IGNORE';
181 my $oldAutoCommit = $FS::UID::AutoCommit;
182 local $FS::UID::AutoCommit = 0;
185 my $error = $new->SUPER::replace($old, @_);
187 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
191 $error = $new->svc_export;
193 $dbh->rollback if $oldAutoCommit;
197 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
204 Calls the replace export for any communigate exports attached to this rule's
212 my $cust_svc = $self->cust_svc;
213 my $svc_x = $cust_svc->svc_x;
216 my @exports = $cust_svc->part_svc->part_export('communigate_pro');
217 my @errors = map $_->export_replace($svc_x, $svc_x), @exports;
219 @errors ? join(' / ', @errors) : '';
225 Checks all fields to make sure this is a valid rule. If there is
226 an error, returns the error, otherwise returns false. Called by the insert
231 # the check method should currently be supplied - FS::Record contains some
232 # data checking routines
238 $self->ut_numbern('rulenum')
239 || $self->ut_text('name')
240 || $self->ut_textn('comment')
241 || $self->ut_foreign_key('svcnum', 'cust_svc', 'svcnum')
242 || $self->ut_number('priority')
244 return $error if $error;
255 qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
258 =item cgp_rule_condition
260 Returns the conditions associated with this rule, as FS::cgp_rule_condition
265 sub cgp_rule_condition {
267 qsearch('cgp_rule_condition', { 'rulenum' => $self->rulenum } );
270 =item cgp_rule_action
272 Returns the actions associated with this rule, as FS::cgp_rule_action
277 sub cgp_rule_action {
279 qsearch('cgp_rule_action', { 'rulenum' => $self->rulenum } );
284 Returns an arraref representing this rule, suitable for Communigate Pro API
287 The first element specifies the rule priority.
289 The second element specifies the rule name.
291 The third element specifies the rule conditions.
293 The fourth element specifies the rule actions.
295 The fifth element specifies the rule comment.
303 [ map $_->arrayref, $self->cgp_rule_condition ],
304 [ map $_->arrayref, $self->cgp_rule_action ],
315 L<FS::Record>, schema.html from the base documentation.