communigate pro rules, RT#7515
authorivan <ivan>
Sun, 23 May 2010 01:59:21 +0000 (01:59 +0000)
committerivan <ivan>
Sun, 23 May 2010 01:59:21 +0000 (01:59 +0000)
FS/FS/Schema.pm
FS/FS/cgp_rule.pm
FS/FS/cgp_rule_action.pm
FS/FS/cgp_rule_condition.pm
FS/FS/part_export/communigate_pro.pm
FS/FS/svc_CGPRule_Mixin.pm [new file with mode: 0644]
FS/FS/svc_acct.pm
FS/FS/svc_domain.pm
FS/t/svc_CGPRule_Mixin.t [new file with mode: 0644]
httemplate/browse/cgp_rule.html

index 3cc853f..0f17d97 100644 (file)
@@ -1631,7 +1631,7 @@ sub tables_hashref {
         'priority',    'int',     '',      '', '', '',
       ],
       'primary_key' => 'rulenum',
-      'unique'      => [],
+      'unique'      => [ [ 'svcnum', 'name' ] ],
       'index'       => [ [ 'svcnum' ] ],
     },
 
index 82e7122..ad5ab1e 100644 (file)
@@ -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
index 170ab58..71605a9 100644 (file)
@@ -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
index cfb6710..02ea172 100644 (file)
@@ -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
index 07281f1..edfd4b1 100644 (file)
@@ -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 (file)
index 0000000..4501580
--- /dev/null
@@ -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;
index 5093841..9236067 100644 (file)
@@ -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
index c8af5a8..8823c77 100644 (file)
@@ -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 (file)
index 0000000..fcccd12
--- /dev/null
@@ -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";
index ea11d62..6f7b3e0 100644 (file)
@@ -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>