2 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( dbh );
9 FS::cgp_rule - Object methods for cgp_rule records
15 $record = new FS::cgp_rule \%hash;
16 $record = new FS::cgp_rule { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
28 An FS::cgp_rule object represents a mail filtering rule. FS::cgp_rule
29 inherits from FS::Record. The following fields are currently supported:
62 Creates a new rule. To add the rule to the database, see L<"insert">.
64 Note that this stores the hash reference, not a distinct copy of the hash it
65 points to. You can ask the object for a copy with the I<hash> method.
69 # the new method can be inherited from FS::Record, if a table method is defined
71 sub table { 'cgp_rule'; }
75 Adds this record to the database. If there is an error, returns the error,
76 otherwise returns false.
83 # local $SIG{HUP} = 'IGNORE';
84 # local $SIG{INT} = 'IGNORE';
85 # local $SIG{QUIT} = 'IGNORE';
86 # local $SIG{TERM} = 'IGNORE';
87 # local $SIG{TSTP} = 'IGNORE';
88 # local $SIG{PIPE} = 'IGNORE';
90 # my $oldAutoCommit = $FS::UID::AutoCommit;
91 # local $FS::UID::AutoCommit = 0;
94 # my $error = $self->SUPER::insert(@_);
96 # $dbh->rollback if $oldAutoCommit;
100 # #conditions and actions not in yet
101 # #$error = $self->svc_export;
103 # # $dbh->rollback if $oldAutoCommit;
107 # $dbh->commit or die $dbh->errstr if $oldAutoCommit;
113 Delete this record from the database.
120 local $SIG{HUP} = 'IGNORE';
121 local $SIG{INT} = 'IGNORE';
122 local $SIG{QUIT} = 'IGNORE';
123 local $SIG{TERM} = 'IGNORE';
124 local $SIG{TSTP} = 'IGNORE';
125 local $SIG{PIPE} = 'IGNORE';
127 my $oldAutoCommit = $FS::UID::AutoCommit;
128 local $FS::UID::AutoCommit = 0;
131 my @del = $self->cgp_rule_condition;
132 push @del, $self->cgp_rule_action;
134 foreach my $del (@del) {
135 my $error = $del->delete;
137 $dbh->rollback if $oldAutoCommit;
142 my $error = $self->SUPER::delete(@_);
144 $dbh->rollback if $oldAutoCommit;
148 $error = $self->svc_export;
150 $dbh->rollback if $oldAutoCommit;
154 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
158 =item replace OLD_RECORD
160 Replaces the OLD_RECORD with this one in the database. If there is an error,
161 returns the error, otherwise returns false.
168 my $old = ( ref($_[0]) eq ref($new) )
172 local $SIG{HUP} = 'IGNORE';
173 local $SIG{INT} = 'IGNORE';
174 local $SIG{QUIT} = 'IGNORE';
175 local $SIG{TERM} = 'IGNORE';
176 local $SIG{TSTP} = 'IGNORE';
177 local $SIG{PIPE} = 'IGNORE';
179 my $oldAutoCommit = $FS::UID::AutoCommit;
180 local $FS::UID::AutoCommit = 0;
183 my $error = $new->SUPER::replace($old, @_);
185 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
189 #conditions and actions not in yet
190 #$error = $new->svc_export;
192 # $dbh->rollback if $oldAutoCommit;
196 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
203 Calls the replace export for any communigate exports attached to this rule's
211 my $cust_svc = $self->cust_svc;
212 my $svc_x = $cust_svc->svc_x;
215 my @exports = $cust_svc->part_svc->part_export('communigate_pro');
216 my @errors = map $_->export_replace($svc_x, $svc_x), @exports;
218 @errors ? join(' / ', @errors) : '';
224 Checks all fields to make sure this is a valid rule. If there is
225 an error, returns the error, otherwise returns false. Called by the insert
230 # the check method should currently be supplied - FS::Record contains some
231 # data checking routines
237 $self->ut_numbern('rulenum')
238 || $self->ut_text('name')
239 || $self->ut_textn('comment')
240 || $self->ut_foreign_key('svcnum', 'cust_svc', 'svcnum')
241 || $self->ut_number('priority')
243 return $error if $error;
248 =item clone NEW_SVCNUM
250 Clones this rule into an identical rule for the specified new service.
252 If there is an error, returns the error, otherwise returns false.
256 #should return the newly inserted rule instead? used in misc/clone-cgp_rule.html
258 #i should probably be transactionalized so i'm all-or-nothing
260 my( $self, $svcnum ) = @_;
262 my $new = $self->new( { $self->hash } );
264 $new->svcnum( $svcnum );
265 my $error = $new->insert;
266 return $error if $error;
268 my @dup = $self->cgp_rule_condition;
269 push @dup, $self->cgp_rule_action;
271 foreach my $dup (@dup) {
272 my $new_dup = $dup->new( { $dup->hash } );
273 my $pk = $new_dup->primary_key;
275 $new_dup->rulenum( $new->rulenum );
277 $error = $new_dup->insert;
278 return $error if $error;
282 $error = $new->svc_export;
283 return $error if $error;
291 =item cgp_rule_condition
293 Returns the conditions associated with this rule, as FS::cgp_rule_condition
298 Returns an arraref representing this rule, suitable for Communigate Pro API
301 The first element specifies the rule priority.
303 The second element specifies the rule name.
305 The third element specifies the rule conditions.
307 The fourth element specifies the rule actions.
309 The fifth element specifies the rule comment.
317 [ map $_->arrayref, $self->cgp_rule_condition ],
318 [ map $_->arrayref, $self->cgp_rule_action ],
329 L<FS::Record>, schema.html from the base documentation.