From: ivan Date: Sun, 23 May 2010 01:59:21 +0000 (+0000) Subject: communigate pro rules, RT#7515 X-Git-Tag: freeside_1_9_4~86 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=450e069ef06ba6bbb58a9f48c5bd9365248e4cb5 communigate pro rules, RT#7515 --- diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index 3cc853f20..0f17d977a 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -1631,7 +1631,7 @@ sub tables_hashref { 'priority', 'int', '', '', '', '', ], 'primary_key' => 'rulenum', - 'unique' => [], + 'unique' => [ [ 'svcnum', 'name' ] ], 'index' => [ [ 'svcnum' ] ], }, diff --git a/FS/FS/cgp_rule.pm b/FS/FS/cgp_rule.pm index 82e712204..ad5ab1e1b 100644 --- a/FS/FS/cgp_rule.pm +++ b/FS/FS/cgp_rule.pm @@ -80,7 +80,35 @@ otherwise returns false. =cut -# the insert method can be inherited from FS::Record +sub insert { + my $self = shift; + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $error = $self->SUPER::insert(@_); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + $error = $self->svc_export; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; +} =item delete @@ -119,8 +147,14 @@ sub delete { return $error; } - $dbh->commit or die $dbh->errstr if $oldAutoCommit; + $error = $self->svc_export; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; } =item replace OLD_RECORD @@ -130,7 +164,61 @@ returns the error, otherwise returns false. =cut -# the replace method can be inherited from FS::Record +sub replace { + my $new = shift; + + my $old = ( ref($_[0]) eq ref($new) ) + ? shift + : $new->replace_old; + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $error = $new->SUPER::replace($old, @_); + if ( $error ) { + $dbh->rollback or die $dbh->errstr if $oldAutoCommit; + return $error; + } + + $error = $new->svc_export; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; + +} + +=item svc_export + +Calls the replace export for any communigate exports attached to this rule's +service. + +=cut + +sub svc_export { + my $self = shift; + + my $cust_svc = $self->cust_svc; + my $svc_x = $cust_svc->svc_x; + + #_singledomain too + my @exports = $cust_svc->part_svc->part_export('communigate_pro'); + my @errors = map $_->export_replace($svc_x, $svc_x), @exports; + + @errors ? join(' / ', @errors) : ''; + +} =item check @@ -191,6 +279,33 @@ sub cgp_rule_action { qsearch('cgp_rule_action', { 'rulenum' => $self->rulenum } ); } +=item arrayref + +Returns an arraref representing this rule, suitable for Communigate Pro API +commands: + +The first element specifies the rule priority. + +The second element specifies the rule name. + +The third element specifies the rule conditions. + +The fourth element specifies the rule actions. + +The fifth element specifies the rule comment. + +=cut + +sub arrayref { + my $self = shift; + [ $self->priority, + $self->name, + [ map $_->arrayref, $self->cgp_rule_condition ], + [ map $_->arrayref, $self->cgp_rule_action ], + $self->comment, + ], +} + =back =head1 BUGS diff --git a/FS/FS/cgp_rule_action.pm b/FS/FS/cgp_rule_action.pm index 170ab58db..71605a977 100644 --- a/FS/FS/cgp_rule_action.pm +++ b/FS/FS/cgp_rule_action.pm @@ -48,7 +48,6 @@ params rulenum - =back =head1 METHODS @@ -119,6 +118,15 @@ sub check { $self->SUPER::check; } +=item arrayref + +=cut + +sub arrayref { + my $self = shift; + [ $self->action, $self->params ]; +} + =back =head1 BUGS diff --git a/FS/FS/cgp_rule_condition.pm b/FS/FS/cgp_rule_condition.pm index cfb671063..02ea1729d 100644 --- a/FS/FS/cgp_rule_condition.pm +++ b/FS/FS/cgp_rule_condition.pm @@ -52,7 +52,6 @@ params rulenum - =back =head1 METHODS @@ -124,6 +123,17 @@ sub check { $self->SUPER::check; } +=item arrayref + +Returns an array reference of the condition, op and params fields. + +=cut + +sub arrayref { + my $self = shift; + [ map $self->$_, qw( condition op params ) ]; +} + =back =head1 BUGS diff --git a/FS/FS/part_export/communigate_pro.pm b/FS/FS/part_export/communigate_pro.pm index 07281f140..edfd4b18e 100644 --- a/FS/FS/part_export/communigate_pro.pm +++ b/FS/FS/part_export/communigate_pro.pm @@ -135,6 +135,15 @@ sub _export_insert_svc_acct { if $alias_err; } + my $rule_error = $self->communigate_pro_queue( + $svc_acct->svcnum, + 'SetAccountMailRules', + $self->export_username($svc_acct), + $svc_acct->cgp_rule_arrayref, + ); + warn "WARNING: error queueing SetAccountMailRules job: $rule_error" + if $rule_error; + ''; } @@ -204,6 +213,15 @@ sub _export_insert_svc_domain { warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err" if $pref_err; + my $rule_error = $self->communigate_pro_queue( + $svc_domain->svcnum, + 'SetDomainMailRules', + $svc_domain->domain, + $svc_domain->cgp_rule_arrayref, + ); + warn "WARNING: error queueing SetDomainMailRules job: $rule_error" + if $rule_error; + ''; } @@ -349,6 +367,15 @@ sub _export_replace_svc_acct { return $error if $error; } + my $rule_error = $self->communigate_pro_queue( + $new->svcnum, + 'SetAccountMailRules', + $self->export_username($new), + $new->cgp_rule_arrayref, + ); + warn "WARNING: error queueing SetAccountMailRules job: $rule_error" + if $rule_error; + ''; } @@ -429,6 +456,15 @@ sub _export_replace_svc_domain { warn "WARNING: error queueing SetAccountDefaultPrefs job: $pref_err" if $pref_err; + my $rule_error = $self->communigate_pro_queue( + $new->svcnum, + 'SetDomainMailRules', + $new->domain, + $new->cgp_rule_arrayref, + ); + warn "WARNING: error queueing SetDomainMailRules job: $rule_error" + if $rule_error; + ''; } @@ -655,24 +691,11 @@ sub export_getsettings_svc_domain { ) }; return $@ if $@; - %$effective_settings = ( - %$effective_settings, - ( map { ("Acct. Default $_" => $acct_defaults->{$_}); } - keys(%$acct_defaults) - ), - ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label?? - keys(%$acct_defaultprefs) - ), - ); - %$settings = ( - %$settings, - ( map { ("Acct. Default $_" => $acct_defaults->{$_}); } - keys(%$acct_defaults) - ), - ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label?? - keys(%$acct_defaultprefs) - ), - ); + my $rules = eval { $self->communigate_pro_runcommand( + 'GetDomainMailRules', + $svc_domain->domain + ) }; + return $@ if $@; #aliases too my $aliases = eval { $self->communigate_pro_runcommand( @@ -681,9 +704,19 @@ sub export_getsettings_svc_domain { ) }; return $@ if $@; - $effective_settings->{'Aliases'} = join(', ', @$aliases); - $settings->{'Aliases'} = join(', ', @$aliases); + my %more = ( + ( map { ("Acct. Default $_" => $acct_defaults->{$_}); } + keys(%$acct_defaults) + ), + ( map { ("Acct. Default $_" => $acct_defaultprefs->{$_}); } #diff label?? + keys(%$acct_defaultprefs) + ), + ( map _rule2string($_), @$rules ), + 'Aliases' => join(', ', @$aliases), + ); + %$effective_settings = ( %$effective_settings, %more ); + %$settings = ( %$settings, %more ); #false laziness w/below @@ -750,8 +783,21 @@ sub export_getsettings_svc_acct { keys(%$prefs) ); - #aliases too + #mail rules + my $rules = eval { $self->communigate_pro_runcommand( + 'GetAccountMailRules', + $svc_acct->email + ) }; + return $@ if $@; + + %$effective_settings = ( %$effective_settings, + map _rule2string($_), @$rules + ); + %$settings = ( %$settings, + map _rule2string($_), @$rules + ); + #aliases too my $aliases = eval { $self->communigate_pro_runcommand( 'GetAccountAliases', $svc_acct->email @@ -785,6 +831,14 @@ sub export_getsettings_svc_acct { } +sub _rule2string { + my $rule = shift; + my($priority, $name, $conditions, $actions, $comment) = @$rule; + $conditions = join(', ', map { my $a = $_; join(' ', @$a); } @$conditions); + $actions = join(', ', map { my $a = $_; join(' ', @$a); } @$actions); + ("Mail rule $name" => "$priority IF $conditions THEN $actions ($comment)"); +} + sub export_getsettings_svc_mailinglist { my($self, $svc_mailinglist, $settingsref, $defaultref ) = @_; diff --git a/FS/FS/svc_CGPRule_Mixin.pm b/FS/FS/svc_CGPRule_Mixin.pm new file mode 100644 index 000000000..45015806c --- /dev/null +++ b/FS/FS/svc_CGPRule_Mixin.pm @@ -0,0 +1,61 @@ +package FS::svc_CGPRule_Mixin; + +use strict; +use FS::Record qw(qsearch); +use FS::cgp_rule; + +=head1 NAME + +FS::svc_CGPRule_Mixin - Mixin class for svc_classes which can be related to cgp_rule + +=head1 SYNOPSIS + +package FS::svc_table; +use base qw( FS::svc_CGPRule_Mixin FS::svc_Common ); + +=head1 DESCRIPTION + +This is a mixin class for svc_ classes that can have Communigate Pro rules +(currently, domains and accounts). + +=head1 METHODS + +=over 4 + +=item + +Returns the rules associated with this service, as FS::cgp_rule objects. + +=cut + +sub cgp_rule { + my $self = shift; + qsearch({ + 'table' => 'cgp_rule', + 'hashref' => { 'svcnum' => $self->svcnum }, + 'order_by' => 'ORDER BY priority ASC', + }); +} + +=item cgp_rule_arrayref + +Returns an arrayref of rules suitable for Communigate Pro API commands. + +=cut + +sub cgp_rule_arrayref { + my $self = shift; + [ map $_->arrayref, $self->cgp_rule ]; +} + +=back + +=head1 BUGS + +=head1 SEE ALSO + +L + +=cut + +1; diff --git a/FS/FS/svc_acct.pm b/FS/FS/svc_acct.pm index 509384170..9236067f0 100644 --- a/FS/FS/svc_acct.pm +++ b/FS/FS/svc_acct.pm @@ -1,7 +1,7 @@ package FS::svc_acct; use strict; -use base qw( FS::svc_Domain_Mixin FS::svc_Common ); +use base qw( FS::svc_Domain_Mixin FS::svc_CGPRule_Mixin FS::svc_Common ); use vars qw( $DEBUG $me $conf $skip_fuzzyfiles $dir_prefix @shells $usernamemin $usernamemax $passwordmin $passwordmax diff --git a/FS/FS/svc_domain.pm b/FS/FS/svc_domain.pm index c8af5a89a..8823c7741 100644 --- a/FS/FS/svc_domain.pm +++ b/FS/FS/svc_domain.pm @@ -1,7 +1,8 @@ package FS::svc_domain; use strict; -use vars qw( @ISA $whois_hack $conf +use base qw( FS::svc_Parent_Mixin FS::svc_CGPRule_Mixin FS::svc_Common ); +use vars qw( $whois_hack $conf @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine $soarefresh $soaretry ); @@ -12,8 +13,6 @@ use Date::Format; use Net::Domain::TLD qw(tld_exists); use FS::Record qw(fields qsearch qsearchs dbh); use FS::Conf; -use FS::svc_Common; -use FS::svc_Parent_Mixin; use FS::cust_svc; use FS::svc_acct; use FS::cust_pkg; @@ -21,8 +20,6 @@ use FS::cust_main; use FS::domain_record; use FS::queue; -@ISA = qw( FS::svc_Parent_Mixin FS::svc_Common ); - #ask FS::UID to run this stuff for us later $FS::UID::callback{'FS::domain'} = sub { $conf = new FS::Conf; diff --git a/FS/t/svc_CGPRule_Mixin.t b/FS/t/svc_CGPRule_Mixin.t new file mode 100644 index 000000000..fcccd12f0 --- /dev/null +++ b/FS/t/svc_CGPRule_Mixin.t @@ -0,0 +1,5 @@ +BEGIN { $| = 1; print "1..1\n" } +END {print "not ok 1\n" unless $loaded;} +use FS::svc_CGPRule_Mixin; +$loaded=1; +print "ok 1\n"; diff --git a/httemplate/browse/cgp_rule.html b/httemplate/browse/cgp_rule.html index ea11d62bf..6f7b3e087 100644 --- a/httemplate/browse/cgp_rule.html +++ b/httemplate/browse/cgp_rule.html @@ -40,7 +40,15 @@ my $view = FS::UI::Web::svc_url( 'm' => $m, my $html_init = qq(View this $svc_label

). - qq!Add new rule

!; + qq!Add new rule

!. + qq! + +!; my $condition_sub = sub { my $cgp_rule = shift; @@ -77,6 +85,9 @@ my $action_sub = sub { }; my $edit_sub = [ $p.'edit/cgp_rule.html?', 'rulenum' ]; -my $del_sub = [ $p.'misc/delete-cgp_rule.html?', 'rulenum' ]; #XXX javascript areyousure or something +my $del_sub = sub { + my $rulenum = shift->rulenum; + [ "javascript:areyousure('${p}misc/delete-cgp_rule.html?$rulenum')", '' ]; +};