add router and addr_block delete features, RT13197
[freeside.git] / httemplate / browse / addr_block.cgi
1 <% include('elements/browse.html',
2                 'title'         => 'Address Blocks',
3                 'name'          => 'address block',
4                 'html_init'     => $html_init,
5                 'html_foot'     => $html_foot,
6                 'query'         => { 'table'     => 'addr_block',
7                                      'hashref'   => {},
8                                      'extra_sql' => $extra_sql,
9                                      'order_by'  => $order_by,
10                                    },
11                 'count_query'   => "SELECT count(*) from addr_block $count_sql",
12                 'header'        => [ 'Address Block',
13                                      'Router',
14                                      'Action(s)',
15                                      '',
16                                      '',
17                                      '',
18                                    ],
19                 'fields'        => [ 'NetAddr',
20                                      sub { my $block = shift;
21                                            my $router = $block->router;
22                                            my $result = '';
23                                            if ($router) {
24                                              $result .= $router->routername. ' (';
25                                              $result .= scalar($block->svc_broadband). ' services)';
26                                            }
27                                            $result;
28                                          },
29                                      $allocate_text,
30                                      sub { shift->router ? '' : '<FONT SIZE="-2">(split)</FONT>' },
31                                      sub { '<FONT SIZE="-2">('. (shift->manual_flag ? 'allow' : 'prevent'). ' automatic ip assignment)</FONT>' },
32                                      sub { 
33                                         my $block = shift;
34                                         if(!$block->router && scalar($block->svc_broadband) == 0) {
35                                             return '<FONT SIZE="-2">(delete)</FONT>';
36                                         }
37                                         '';
38                                      },
39                                    ],
40                 'links'         => [ '',
41                                      '',
42                                      [ 'javascript:void(0)', '' ],
43                                      $split_link,
44                                      $autoassign_link,
45                                      sub {
46                                         my $block = shift;
47                                         if(!$block->router && scalar($block->svc_broadband) == 0) {
48                                             [ "${p}misc/delete-addr_block.html?", 'blocknum' ];
49                                         }
50                                      },
51                                    ],
52                 'link_onclicks' => [ '',
53                                      '',
54                                      $allocate_link,
55                                      '',
56                                      '',
57                                    ],
58                 'cell_styles'   => [ '',
59                                      '',
60                                      'border-right:none;',
61                                      'border-left:none;',
62                                      '',
63                                    ],
64                 'agent_virt'    => 1,
65                 'agent_null_right' => 'Broadband global configuration',
66                 'agent_pos'     => 1,
67           )
68 %>
69 <%init>
70
71 die "access denied"
72   unless $FS::CurrentUser::CurrentUser->access_right('Broadband configuration')
73   || $FS::CurrentUser::CurrentUser->access_right('Broadband global configuration');
74
75 my $p2 = popurl(2);
76 my $path = $p2 . "edit/process/addr_block";
77
78 my $extra_sql = "";
79
80 my $count_sql = "WHERE ". $FS::CurrentUser::CurrentUser->agentnums_sql(
81   'null_right' => 'Broadband global configuration',
82 );
83
84 my $order_by = "ORDER BY ";
85 $order_by .= "inet(ip_gateway), " if driver_name =~ /^Pg/i;
86 $order_by .= "inet_aton(ip_gateway), " if driver_name =~ /^mysql/i;
87 $order_by .= "ip_netmask";
88
89 my $html_init = qq(
90 <SCRIPT>
91   function addr_block_areyousure(href, word) {
92     if(confirm("Are you sure you want to "+word+" this address block?") == true)
93       window.location.href = href;
94   }
95 </SCRIPT>
96 );
97
98 $html_init .= include('/elements/error.html');
99
100 my $confirm = sub {
101   my ($verb, $num) = (shift, shift);
102   "javascript:addr_block_areyousure('$path/$verb.cgi?blocknum=$num', '$verb')";
103 };
104
105 my $html_foot = qq(
106   <FORM ACTION="$path/add.cgi" METHOD="POST">
107   Gateway/Netmask: 
108   <INPUT TYPE="text" NAME="ip_gateway" SIZE="15">/<INPUT TYPE="text" NAME="ip_netmask" SIZE="2">
109 );
110 $html_foot .= include( '/elements/select-agent.html',
111                          'agent_null_right' => 'Broadband global configuration',
112                      );
113 $html_foot .= qq(
114   <INPUT TYPE="submit" NAME="submit" VALUE="Add">
115   </FORM>
116 );
117
118 my $allocate_text = sub { my $block = shift;
119                           my $router = $block->router;
120                           my $result = '';
121                           if ($router) {
122                             $result = '<FONT SIZE="-2">(deallocate)</FONT>'
123                               unless scalar($block->svc_broadband);
124                           }else{
125                             $result .= '<FONT SIZE="-2">(allocate)</FONT>'
126                           }
127                           $result;
128 };
129
130 my $allocate_link = sub {
131   my $block = shift;
132   if ($block->router) { 
133     if (scalar($block->svc_broadband) == 0) { 
134       &{$confirm}('deallocate', $block->blocknum);
135     } else { 
136       "";
137     } 
138   } else { 
139     include( '/elements/popup_link_onclick.html',
140              'action' => "${p2}edit/allocate.html?blocknum=". $block->blocknum,
141              'actionlabel' => 'Allocate block to router',
142            );
143   } 
144 }; 
145
146 my $split_link = sub {
147   my $block = shift;
148   my $ref = [ '', '' ];
149   $ref = [ &{$confirm}('split', $block->blocknum), '' ]
150     unless ($block->router);
151   $ref;
152 }; 
153
154 my $autoassign_link = sub {
155   my $block = shift;
156   my $url = "$path/manual_flag.cgi?manual_flag=";
157   $url .=  $block->manual_flag ? '' : 'Y';
158   [ "$url;blocknum=", 'blocknum' ];
159 }; 
160
161 </%init>