summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorivan <ivan>2010-05-23 01:59:19 +0000
committerivan <ivan>2010-05-23 01:59:19 +0000
commit940401c77b8455dd47b57e06dcfedd7514c24a7c (patch)
tree1abfdb7847feeba2b6bbba7687f869a62f55dda0
parentfc3c94d637caecb7932c09fb1e7baceaad069eda (diff)
communigate pro rules, RT#7515
-rw-r--r--FS/FS/Schema.pm2
-rw-r--r--FS/FS/cgp_rule.pm121
-rw-r--r--FS/FS/cgp_rule_action.pm10
-rw-r--r--FS/FS/cgp_rule_condition.pm12
-rw-r--r--FS/FS/part_export/communigate_pro.pm96
-rw-r--r--FS/FS/svc_CGPRule_Mixin.pm61
-rw-r--r--FS/FS/svc_acct.pm2
-rw-r--r--FS/FS/svc_domain.pm7
-rw-r--r--FS/t/svc_CGPRule_Mixin.t5
-rw-r--r--httemplate/browse/cgp_rule.html15
10 files changed, 296 insertions, 35 deletions
diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm
index ebe1195..0b54282 100644
--- a/FS/FS/Schema.pm
+++ b/FS/FS/Schema.pm
@@ -1796,7 +1796,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 82e7122..ad5ab1e 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 170ab58..71605a9 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 cfb6710..02ea172 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 07281f1..edfd4b1 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 0000000..4501580
--- /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<FS::cgp_rule>
+
+=cut
+
+1;
diff --git a/FS/FS/svc_acct.pm b/FS/FS/svc_acct.pm
index 5093841..9236067 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 c8af5a8..8823c77 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 0000000..fcccd12
--- /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 ea11d62..6f7b3e0 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(<A HREF="$view">View this $svc_label</A><BR><BR>).
- qq!<A HREF="${p}edit/cgp_rule.html?svcnum=$svcnum">Add new rule</A><BR><BR>!;
+ qq!<A HREF="${p}edit/cgp_rule.html?svcnum=$svcnum">Add new rule</A><BR><BR>!.
+ qq!
+ <SCRIPT>
+ function areyousure(href) {
+ if (confirm("Are you sure you want to delete this rule?") == true)
+ window.location.href = href;
+ }
+ </SCRIPT>
+!;
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')", '' ];
+};
</%init>