From 0fe2d918669fe77298f95335958edf596a5f9149 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 7 Jun 2010 02:40:15 +0000 Subject: [PATCH] domain rules based on templates (rules from other domains), RT#7514 --- FS/FS/Conf.pm | 7 +++ FS/FS/cgp_rule.pm | 63 ++++++++++++++++++++++----- httemplate/browse/cgp_rule.html | 32 +++++++++++--- httemplate/edit/process/cgp_rule.html | 6 +++ httemplate/edit/process/elements/process.html | 11 +++++ httemplate/misc/clone-cgp_rule.html | 27 ++++++++++++ 6 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 httemplate/misc/clone-cgp_rule.html diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 6658704cc..859342d11 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -2432,6 +2432,13 @@ worry that config_items is freeside-specific and icky. }, { + 'key' => 'cgp_rule-domain_templates', + 'section' => '', + 'description' => 'Communigate Pro rule templates for domains, one per line, "svcnum Name"', + 'type' => 'textarea', + }, + + { 'key' => 'svc_forward-no_srcsvc', 'section' => '', 'description' => "Don't allow forwards from existing accounts, only arbitrary addresses. Useful when exporting to systems such as Communigate Pro which treat forwards in this fashion.", diff --git a/FS/FS/cgp_rule.pm b/FS/FS/cgp_rule.pm index ad5ab1e1b..e9c50901a 100644 --- a/FS/FS/cgp_rule.pm +++ b/FS/FS/cgp_rule.pm @@ -100,11 +100,12 @@ sub insert { return $error; } - $error = $self->svc_export; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; - } + #conditions and actions not in yet + #$error = $self->svc_export; + #if ( $error ) { + # $dbh->rollback if $oldAutoCommit; + # return $error; + #} $dbh->commit or die $dbh->errstr if $oldAutoCommit; ''; @@ -188,11 +189,12 @@ sub replace { return $error; } - $error = $new->svc_export; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; - } + #conditions and actions not in yet + #$error = $new->svc_export; + #if ( $error ) { + # $dbh->rollback if $oldAutoCommit; + # return $error; + #} $dbh->commit or die $dbh->errstr if $oldAutoCommit; ''; @@ -246,6 +248,47 @@ sub check { $self->SUPER::check; } +=item clone NEW_SVCNUM + +Clones this rule into an identical rule for the specified new service. + +If there is an error, returns the error, otherwise returns false. + +=cut + +#should return the newly inserted rule instead? used in misc/clone-cgp_rule.html + +#i should probably be transactionalized so i'm all-or-nothing +sub clone { + my( $self, $svcnum ) = @_; + + my $new = $self->new( { $self->hash } ); + $new->rulenum(''); + $new->svcnum( $svcnum ); + my $error = $new->insert; + return $error if $error; + + my @dup = $self->cgp_rule_condition; + push @dup, $self->cgp_rule_action; + + foreach my $dup (@dup) { + my $new_dup = $dup->new( { $dup->hash } ); + my $pk = $new_dup->primary_key; + $new_dup->$pk(''); + $new_dup->rulenum( $new->rulenum ); + + $error = $new_dup->insert; + return $error if $error; + + } + + $error = $new->svc_export; + return $error if $error; + + ''; + +} + =item cust_svc =cut diff --git a/httemplate/browse/cgp_rule.html b/httemplate/browse/cgp_rule.html index 5be6bb4aa..8a427b828 100644 --- a/httemplate/browse/cgp_rule.html +++ b/httemplate/browse/cgp_rule.html @@ -26,25 +26,45 @@ my $svcnum = $1; #agent virt so you can't do cross-agent communigate rules my $cust_svc = qsearchs('cust_svc', { 'svcnum' => $svcnum }) or die 'unknown svcnum'; +my $part_svc = $cust_svc->part_svc; my $count_query = "SELECT COUNT(*) FROM cgp_rule WHERE svcnum = $svcnum"; my($svc_label, $svc_value, $svcdb) = $cust_svc->label; - my $view = FS::UI::Web::svc_url( 'm' => $m, 'action' => 'view', - 'part_svc' => $cust_svc->part_svc, + 'part_svc' => $part_svc, 'svc' => $cust_svc, ); my $html_init = qq(View this $svc_label

). - qq!Add new rule

!. + qq!Add new rule
!; + +if ( $part_svc->svcdb eq 'svc_domain' ) { + + #areyousure for adding these? + + foreach my $line ( FS::Conf->new->config('cgp_rule-domain_templates') ) { + $line =~ /^\s*(\d+)\s+(.+)\s*$/ or next; + my($t_svcnum, $t_name) = ( $1, $2 ); + $html_init .= + qq!! + ."Add $t_name rule
"; + } + +} + +$html_init .= + '
'. qq! @@ -87,7 +107,7 @@ my $action_sub = sub { my $edit_sub = [ $p.'edit/cgp_rule.html?', 'rulenum' ]; my $del_sub = sub { my $rulenum = shift->rulenum; - [ "javascript:areyousure('${p}misc/delete-cgp_rule.html?$rulenum')", '' ]; + [ "javascript:areyousure_delete('${p}misc/delete-cgp_rule.html?$rulenum')", '' ]; }; diff --git a/httemplate/edit/process/cgp_rule.html b/httemplate/edit/process/cgp_rule.html index 965341384..5326587cb 100644 --- a/httemplate/edit/process/cgp_rule.html +++ b/httemplate/edit/process/cgp_rule.html @@ -11,6 +11,12 @@ 'fields' => [qw( action params )], }, ], + 'noerror_callback' => sub { + my( $cgi, $object ) = @_; + my $error = $object->svc_export; + #shit, not a good place for error handling :/ + die $error if $error; + }, ) %> <%init> diff --git a/httemplate/edit/process/elements/process.html b/httemplate/edit/process/elements/process.html index 87cadb774..ebbc2f778 100644 --- a/httemplate/edit/process/elements/process.html +++ b/httemplate/edit/process/elements/process.html @@ -65,6 +65,9 @@ Example: #return an error string or empty for no error 'precheck_callback' => sub { my( $cgi ) = @_; }, + #after everything's inserted + 'noerror_callback' => sub { my( $cgi, $object ) = @_; }, + #supplies arguments to insert() and replace() # for use with tables that are FS::option_Common (among other things) 'args_callback' => sub { my( $cgi, $object ) = @_; }, @@ -298,12 +301,20 @@ if ( !$error && $opt{'process_o2m'} ) { if ( $error ) { + $cgi->param('error', $error); if ( $opt{'clear_on_error'} && scalar(@{$opt{'clear_on_error'}}) ) { foreach my $field (@{$opt{'clear_on_error'}}) { $cgi->param($field, '') } } + +} else { + + if ( $opt{'noerror_callback'} ) { + &{ $opt{'noerror_callback'} }( $cgi, $new ); + } + } diff --git a/httemplate/misc/clone-cgp_rule.html b/httemplate/misc/clone-cgp_rule.html new file mode 100644 index 000000000..d821a2dff --- /dev/null +++ b/httemplate/misc/clone-cgp_rule.html @@ -0,0 +1,27 @@ +% if ( $error ) { +% errorpage($error); +% } else { +<% $cgi->redirect($p. "browse/cgp_rule.html?svcnum=". $svcnum) %> +% } +<%init> + +# :/ needs agent-virt so you can't futz with arbitrary rules + +#die "access denied" +# unless $FS::CurrentUser::CurrentUser->access_right('Provision customer service'); + +#untaint svcnum and clone +$cgi->param('svcnum') =~ /^(\d+)$/ || die "Illegal svcnum"; +my $svcnum = $1; +$cgi->param('clone') =~ /^(\d+)$/ || die "Illegal clone"; +my $clone = $1; + +my @cgp_rule = qsearch('cgp_rule', { 'svcnum' => $clone } ); + +my $error = ''; +foreach my $cgp_rule ( @cgp_rule ) { + $error = $cgp_rule->clone( $svcnum ); + last if $error; +} + + -- 2.11.0