add router and addr_block delete features, RT13197
authorlevinse <levinse>
Wed, 22 Jun 2011 23:39:36 +0000 (23:39 +0000)
committerlevinse <levinse>
Wed, 22 Jun 2011 23:39:36 +0000 (23:39 +0000)
FS/FS/addr_block.pm
FS/FS/router.pm
httemplate/browse/addr_block.cgi
httemplate/misc/delete-addr_block.html [new file with mode: 0644]
httemplate/misc/delete-router.html [new file with mode: 0644]

index 0fe2476..be42cb5 100755 (executable)
@@ -76,14 +76,35 @@ otherwise returns false.
 Deletes this record from the database.  If there is an error, returns the
 error, otherwise returns false.
 
+=cut
+
 sub delete {
   my $self = shift;
-  return 'Block must be deallocated before deletion'
-    if $self->router;
-
-  $self->SUPER::delete;
+  return 'Block must be deallocated and have no services before deletion'
+    if $self->router || $self->svc_broadband;
+
+    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::delete;
+    if ( $error ) {
+       $dbh->rollback if $oldAutoCommit;
+       return $error;
+    }
+  
+    $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+    '';
 }
 
+
 =item replace OLD_RECORD
 
 Replaces OLD_RECORD with this one in the database.  If there is an error,
index 7a9fda3..f66f2ce 100755 (executable)
@@ -2,7 +2,7 @@ package FS::router;
 
 use strict;
 use vars qw( @ISA );
-use FS::Record qw( qsearchs qsearch );
+use FS::Record qw( qsearchs qsearch dbh );
 use FS::addr_block;
 
 @ISA = qw( FS::Record FS::m2m_Common );
@@ -90,6 +90,39 @@ sub check {
   $self->SUPER::check;
 }
 
+=item delete
+
+Deletes this router if and only if no address blocks (see L<FS::addr_block>)
+are currently allocated to it.
+
+=cut
+
+sub delete {
+    my $self = shift;
+
+    return 'Router has address blocks allocated to it' if $self->addr_block;
+    
+    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::delete;
+    if ( $error ) {
+       $dbh->rollback if $oldAutoCommit;
+       return $error;
+    }
+  
+    $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+    '';
+}
+
 =item addr_block
 
 Returns a list of FS::addr_block objects (address blocks) associated
@@ -139,8 +172,6 @@ sub agent {
 
 =back
 
-=head1 BUGS
-
 =head1 SEE ALSO
 
 FS::svc_broadband, FS::router, FS::addr_block, FS::part_svc,
index 1bbcdcb..7ff0eaa 100644 (file)
@@ -14,6 +14,7 @@
                                      'Action(s)',
                                      '',
                                      '',
+                                     '',
                                    ],
                 'fields'        => [ 'NetAddr',
                                      sub { my $block = shift;
                                      $allocate_text,
                                      sub { shift->router ? '' : '<FONT SIZE="-2">(split)</FONT>' },
                                      sub { '<FONT SIZE="-2">('. (shift->manual_flag ? 'allow' : 'prevent'). ' automatic ip assignment)</FONT>' },
+                                     sub { 
+                                        my $block = shift;
+                                        if(!$block->router && scalar($block->svc_broadband) == 0) {
+                                            return '<FONT SIZE="-2">(delete)</FONT>';
+                                        }
+                                        '';
+                                     },
                                    ],
                 'links'         => [ '',
                                      '',
                                      [ 'javascript:void(0)', '' ],
                                      $split_link,
                                      $autoassign_link,
+                                     sub {
+                                        my $block = shift;
+                                        if(!$block->router && scalar($block->svc_broadband) == 0) {
+                                            [ "${p}misc/delete-addr_block.html?", 'blocknum' ];
+                                        }
+                                     },
                                    ],
                 'link_onclicks' => [ '',
                                      '',
                                      $allocate_link,
                                      '',
+                                     '',
                                    ],
                 'cell_styles'   => [ '',
                                      '',
                                      'border-right:none;',
                                      'border-left:none;',
+                                     '',
                                    ],
                 'agent_virt'    => 1,
                 'agent_null_right' => 'Broadband global configuration',
@@ -142,4 +158,7 @@ my $autoassign_link = sub {
   [ "$url;blocknum=", 'blocknum' ];
 }; 
 
+my $delete_link = sub {
+};
+
 </%init>
diff --git a/httemplate/misc/delete-addr_block.html b/httemplate/misc/delete-addr_block.html
new file mode 100644 (file)
index 0000000..83a4b68
--- /dev/null
@@ -0,0 +1,19 @@
+% if ( $error ) {
+%   errorpage($error);
+% } else {
+<% $cgi->redirect("${p}browse/addr_block.cgi") %>
+% }
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Broadband configuration')
+  || $FS::CurrentUser::CurrentUser->access_right('Broadband global configuration');
+
+my($query) = $cgi->keywords;
+$query =~ /^(\d+)$/ || die "Illegal blocknum";
+my $blocknum = $1;
+
+my $addr_block = qsearchs('addr_block',{'blocknum'=>$blocknum});
+my $error = $addr_block->delete;
+
+</%init>
diff --git a/httemplate/misc/delete-router.html b/httemplate/misc/delete-router.html
new file mode 100644 (file)
index 0000000..eb1754b
--- /dev/null
@@ -0,0 +1,19 @@
+% if ( $error ) {
+%   errorpage($error);
+% } else {
+<% $cgi->redirect("${p}browse/router.cgi") %>
+% }
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Broadband configuration')
+  || $FS::CurrentUser::CurrentUser->access_right('Broadband global configuration');
+
+my($query) = $cgi->keywords;
+$query =~ /^(\d+)$/ || die "Illegal routernum";
+my $routernum = $1;
+
+my $router = qsearchs('router',{'routernum'=>$routernum});
+my $error = $router->delete;
+
+</%init>