show imported region and rate #s, RT#83146
[freeside.git] / httemplate / edit / svc_broadband.cgi
index d8a1f7a..5a3692e 100644 (file)
-<!-- mason kludge -->
-<%
-
-my( $svcnum,  $pkgnum, $svcpart, $part_svc, $svc_broadband );
-if ( $cgi->param('error') ) {
-  $svc_broadband = new FS::svc_broadband ( {
-    map { $_, scalar($cgi->param($_)) } fields('svc_broadband')
-  } );
-  $svcnum = $svc_broadband->svcnum;
-  $pkgnum = $cgi->param('pkgnum');
-  $svcpart = $cgi->param('svcpart');
-  $part_svc=qsearchs('part_svc',{'svcpart'=>$svcpart});
-  die "No part_svc entry!" unless $part_svc;
-} else {
-  my($query) = $cgi->keywords;
-  if ( $query =~ /^(\d+)$/ ) { #editing
-    $svcnum=$1;
-    $svc_broadband=qsearchs('svc_broadband',{'svcnum'=>$svcnum})
-      or die "Unknown (svc_broadband) svcnum!";
-
-    my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum})
-      or die "Unknown (cust_svc) svcnum!";
-
-    $pkgnum=$cust_svc->pkgnum;
-    $svcpart=$cust_svc->svcpart;
-  
-    $part_svc=qsearchs('part_svc',{'svcpart'=>$svcpart});
-    die "No part_svc entry!" unless $part_svc;
-
-  } else { #adding
-
-    $svc_broadband = new FS::svc_broadband({});
-
-    foreach $_ (split(/-/,$query)) { #get & untaint pkgnum & svcpart
-      $pkgnum=$1 if /^pkgnum(\d+)$/;
-      $svcpart=$1 if /^svcpart(\d+)$/;
-    }
-    $part_svc=qsearchs('part_svc',{'svcpart'=>$svcpart});
-    die "No part_svc entry!" unless $part_svc;
-
-    $svcnum='';
-
-    #set fixed and default fields from part_svc
-    foreach my $part_svc_column (
-      grep { $_->columnflag } $part_svc->all_part_svc_column
-    ) {
-      $svc_broadband->setfield( $part_svc_column->columnname,
-                                $part_svc_column->columnvalue,
-                              );
-    }
-
-  }
-}
-my $action = $svc_broadband->svcnum ? 'Edit' : 'Add';
-
-my @ac_list;
-
-if ($pkgnum) {
-
-  unless ($svc_broadband->actypenum) {die "actypenum must be set fixed";};
-  @ac_list = qsearch('ac', { actypenum => $svc_broadband->getfield('actypenum') });
-
-} elsif ( $action eq 'Edit' ) {
-
-  #Nothing?
-
-} else {
-  die "\$action eq Add, but \$pkgnum is null!\n";
+<& elements/svc_Common.html,
+     'post_url'             => popurl(1). 'process/svc_broadband.cgi',
+     'name'                 => 'broadband service',
+     'table'                => 'svc_broadband',
+     'fields'               => \@fields, 
+     'svc_field_callback'   => $svc_field_callback,
+     'svc_new_callback'     => $svc_edit_callback,
+     'svc_edit_callback'    => $svc_edit_callback,
+     'svc_error_callback'   => $svc_edit_callback,
+     'dummy'                => $cgi->query_string,
+     'onsubmit'             => 'validate_coords',
+     'html_foot'            => $js,
+&>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Provision customer service'); #something else more specific?
+
+# If it's stupid but it works, it's still stupid.
+#  -Kristian
+
+my $conf = new FS::Conf;
+
+my $js = <<END
+    <script type="text/javascript">
+        function validate_coords(f){
+END
+;
+if ( $conf->exists('svc_broadband-require-nw-coordinates') ) {
+$js .= <<END
+            var lon = f.longitude;
+            var lat = f.latitude;
+            if ( lon == null || lat == null || 
+                lon.value.length == 0 || lat.value.length == 0 ) return true;
+
+            return (ut_coord(lat.value,1,90) && ut_coord(lon.value,-180,-1));
+        } // validate_coords
+
+        /* this is a JS re-implementation of FS::Record::ut_coord */
+        function ut_coord(coord,lower,upper) {
+            var neg = /^-/.test(coord);
+            coord = coord.replace(/^-/,'');
+
+            var d = 0;
+            var m = 0;
+            var s = 0;
+            
+            var t1 = /^(\\s*\\d{1,3}(?:\\.\\d+)?)\\s*\$/.exec(coord);
+            var t2 = /^(\\s*\\d{1,3})\\s+(\\d{1,2}(?:\\.\\d+))\\s*\$/.exec(coord);
+            var t3 = /^(\\s*\\d{1,3})\\s+(\\d{1,2})\\s+(\\d{1,3})\\s*\$/.exec(coord);
+            if ( t1 != null ) {
+                d = t1[1];
+            } else if ( t2 != null ) {
+                d = t2[1];
+                m = t2[2];
+            } else if ( t3 != null ) {
+                d = t3[1];
+                m = t3[2];
+                s = t3[3];
+            } else {
+                alert('Invalid co-ordinates! Latitude must be positive and longitude must be negative.');
+                return false;
+            } 
+            
+            var ts = /^\\d{3}\$/.exec(s);
+            if ( ts != null || s > 59 ) {
+               s /= 1000; 
+            } else {
+                s /= 60;
+            }
+            s /= 60;
+
+            m /= 60;
+            if ( m > 59 ) {
+                alert('Invalid coordinate with minutes > 59');
+                return false;
+            }
+
+            var tmp = parseInt(d)+parseInt(m)+parseInt(s);
+            tmp = tmp.toFixed(8);
+            coord = (neg ? -1 : 1) * tmp;
+
+            if(coord < lower) {
+                alert('Error: invalid coordinate < '+lower);
+                return false;
+            }
+            if(coord > upper) {
+                alert('Error: invalid coordinate > '+upper);
+                return false;
+            }
+
+            return true;
+END
+;
 }
-
-
-my $p1 = popurl(1);
-print header("Broadband Service $action", '');
-
-print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
-      "</FONT>"
-  if $cgi->param('error');
-
-print qq!<FORM ACTION="${p1}process/svc_broadband.cgi" METHOD=POST>!;
-
-#display
-
-
-#svcnum
-print qq!<INPUT TYPE="hidden" NAME="svcnum" VALUE="$svcnum">!;
-print qq!Service #<B>!, $svcnum ? $svcnum : "(NEW)", "</B><BR><BR>";
-
-#pkgnum
-print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="$pkgnum">!;
-#svcpart
-print qq!<INPUT TYPE="hidden" NAME="svcpart" VALUE="$svcpart">!;
-
-#actypenum
-print '<INPUT TYPE="hidden" NAME="actypenum" VALUE="' .
-      $svc_broadband->actypenum . '">';
-
-
-print &ntable("#cccccc",2) . qq!<TR><TD ALIGN="right">AC</TD><TD>!;
-
-#acnum
-if (( $part_svc->part_svc_column('acnum')->columnflag eq 'F' ) or
-    ( !$pkgnum )) {
-
-  my $ac = qsearchs('ac', { acnum => $svc_broadband->acnum });
-  my ($acnum, $acname) = ($ac->acnum, $ac->acname);
-
-  print qq!<INPUT TYPE="hidden" NAME="acnum" VALUE="${acnum}">! .
-        qq!${acnum}: ${acname}</TD></TR>!;
-
-} else {
-
-  my @ac_list = qsearch('ac', { actypenum => $svc_broadband->actypenum });
-  print qq!<SELECT NAME="acnum" SIZE="1"><OPTION VALUE=""></OPTION>!;
-
-  foreach ( @ac_list ) {
-    my ($acnum, $acname) = ($_->acnum, $_->acname);
-    print qq!<OPTION VALUE="${acnum}"! .
-          ($acnum == $svc_broadband->acnum ? ' SELECTED>' : '>') .
-          qq!${acname}</OPTION>!;
+$js .= <<END
+        }
+    </script>
+END
+;
+
+my @fields = (
+  qw( description speed_down speed_up speed_test_down speed_test_up speed_test_latency),
+  { field=>'sectornum', type=>'select-tower_sector',
+    include_opt_callback => sub {
+      my $svc_broadband = shift;
+      my $part_svc = $svc_broadband->part_svc;
+      my $sectors_only;
+      foreach ($part_svc->part_export()) {
+        $sectors_only = '1' if $_->can('require_tower_and_sector');
+      }
+      ## incase export requires a sector and service only has tower attached it will not show on edit.
+      my $non_option_label;
+      my $sector = qsearchs({'table' => 'tower_sector', 'hashref' => { 'sectornum' => $svc_broadband->sectornum }, });
+      $non_option_label = $sector->description if $sector;
+      return ('sectorsonly' => $sectors_only, 'non_option_label' => $non_option_label);
+    },
+  },
+  { field=>'routernum', type=>'select-router_block_ip', 
+    include_opt_callback => sub { 
+      my $svc_broadband = shift;
+      my $part_svc = $svc_broadband->part_svc;
+      return () unless $part_svc; #sanity check
+      my $col = $part_svc->part_svc_column('ip_addr');
+      return () unless $col; #sanity check
+      $col->set('required', 'Y') unless $conf->exists('svc_broadband-allow_null_ip_addr');
+      return ('ip_addr_required' => $col->required);
+    },
+  },
+  { field=>'mac_addr' , type=>'input-mac_addr' },
+  qw(
+      latitude longitude altitude
+      radio_serialnum radio_location poe_location rssi suid
+    ),
+  { field=>'shared_svcnum', type=>'search-svc_broadband', },
+  qw( vlan_profile performance_profile authkey plan_id ),
+);
+
+if ( $conf->exists('svc_broadband-radius') ) {
+  push @fields,
+  { field     => 'usergroup',
+    type      => 'select-radius_group',
+    multiple  => 1,
   }
-  print '</TD></TR>';
-
-}
-
-#speed_up & speed_down
-my ($speed_up, $speed_down) = ($svc_broadband->speed_up,
-                               $svc_broadband->speed_down);
-
-print '<TR><TD ALIGN="right">Download speed</TD><TD>';
-if ( $part_svc->part_svc_column('speed_down')->columnflag eq 'F' ) {
-  print qq!<INPUT TYPE="hidden" NAME="speed_down" VALUE="${speed_down}">! .
-        qq!${speed_down}Kbps</TD></TR>!;
-} else {
-  print qq!<INPUT TYPE="text" NAME="speed_down" SIZE=5 VALUE="${speed_down}">! .
-        qq!Kbps</TD></TR>!;
 }
 
-print '<TR><TD ALIGN="right">Upload speed</TD><TD>';
-if ( $part_svc->part_svc_column('speed_up')->columnflag eq 'F' ) {
-  print qq!<INPUT TYPE="hidden" NAME="speed_up" VALUE="${speed_up}">! .
-        qq!${speed_up}Kbps</TD></TR>!;
-} else {
-  print qq!<INPUT TYPE="text" NAME="speed_up" SIZE=5 VALUE="${speed_up}">! .
-        qq!Kbps</TD></TR>!;
-}
+my $part_svc;
 
-#ip_addr & ip_netmask
-#We're assuming that ip_netmask is fixed if ip_addr is fixed.
-#If it isn't, well, <shudder> what the heck are you doing!?!?
-
-my ($ip_addr, $ip_netmask) = ($svc_broadband->ip_addr,
-                              $svc_broadband->ip_netmask);
-
-print '<TR><TD ALIGN="right">IP address/Mask</TD><TD>';
-if ( $part_svc->part_svc_column('ip_addr')->columnflag eq 'F' ) {
-  print qq!<INPUT TYPE="hidden" NAME="ip_addr" VALUE="${ip_addr}">! .
-        qq!<INPUT TYPE="hidden" NAME="ip_netmask" VALUE="${ip_netmask}">! .
-        qq!${ip_addr}/${ip_netmask}</TD></TR>!;
-} else {
-  $ip_addr =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
-  print <<END;
-  <INPUT TYPE="text" NAME="ip_addr_a" SIZE="3" MAXLENGTH="3" VALUE="${1}">.
-  <INPUT TYPE="text" NAME="ip_addr_b" SIZE="3" MAXLENGTH="3" VALUE="${2}">.
-  <INPUT TYPE="text" NAME="ip_addr_c" SIZE="3" MAXLENGTH="3" VALUE="${3}">.
-  <INPUT TYPE="text" NAME="ip_addr_d" SIZE="3" MAXLENGTH="3" VALUE="${4}">/
-  <INPUT TYPE="text" NAME="ip_netmask" SIZE="2" MAXLENGTH="2" VALUE="${ip_netmask}">
-</TD></TR>
-<TR><TD COLSPAN="2" WIDTH="300">
-<P><SMALL>Leave the IP address and netmask blank for automatic assignment of a /32 address.  Specifing the netmask and not the address will force assignment of a larger block.</SMALL></P>
-</TD></TR>
-END
-}
+my $svc_edit_callback = sub {
+  my ($cgi, $svc_x, $part_svc_x, $cust_pkg, $fields, $opt) = @_;
 
-#mac_addr
-my $mac_addr = $svc_broadband->mac_addr;
-
-unless (( $part_svc->part_svc_column('mac_addr')->columnflag eq 'F' ) and
-        ( $mac_addr eq '' )) {
-  print '<TR><TD ALIGN="right">MAC Address</TD><TD>';
-  if ( $part_svc->part_svc_column('mac_addr')->columnflag eq 'F' ) { #Why?
-    print qq!<INPUT TYPE="hidden" NAME="mac_addr" VALUE="${mac_addr}">! .
-          qq!${mac_addr}</TD></TR>!;
-  } else {
-    #Ewwww
-    $mac_addr =~ /^([a-f0-9]{2}):([a-f0-9]{2}):([a-f0-9]{2}):([a-f0-9]{2}):([a-f0-9]{2}):([a-f0-9]{2})$/i;
-    print <<END;
-  <INPUT TYPE="text" NAME="mac_addr_a" SIZE="2" MACLENGTH="2" VALUE="${1}">:
-  <INPUT TYPE="text" NAME="mac_addr_b" SIZE="2" MACLENGTH="2" VALUE="${2}">:
-  <INPUT TYPE="text" NAME="mac_addr_c" SIZE="2" MACLENGTH="2" VALUE="${3}">:
-  <INPUT TYPE="text" NAME="mac_addr_d" SIZE="2" MACLENGTH="2" VALUE="${4}">:
-  <INPUT TYPE="text" NAME="mac_addr_e" SIZE="2" MACLENGTH="2" VALUE="${5}">:
-  <INPUT TYPE="text" NAME="mac_addr_f" SIZE="2" MACLENGTH="2" VALUE="${6}">
-</TD></TR>
-END
+  $part_svc = $part_svc_x; #for field_callback to use
 
+  my ($nas_export) = $part_svc->part_export('broadband_nas');
+  #can we assume there's only one of these per part_svc?
+  if ( $nas_export ) {
+    my $nas;
+    if ( $svc_x->svcnum ) {
+      $nas = qsearchs('nas', { 'svcnum' => $svc_x->svcnum });
+    }
+    $nas ||= $nas_export->default_nas;
+    $svc_x->set($_, $nas->$_) foreach fields('nas');
+
+    # duplicates the fields in httemplate/edit/nas.html (mostly)
+    push @$fields,
+      { type  => 'tablebreak-tr-title', 
+        #value => 'Attached NAS',
+        value => $nas_export->exportname,
+        colspan => 2,
+      },
+      { field=>'nasnum', type=>'hidden', },
+      { field=>'shortname', size=>16, maxlength=>32 },
+      { field=>'secret', size=>40, maxlength=>60, required=>1 },
+      { field=>'type', type=>'select',
+        options=>[qw( cisco computone livingston max40xx multitech netserver
+        pathras patton portslave tc usrhiper other )],
+      },
+      { field=>'ports', size=>5 },
+      { field=>'server', size=>40, maxlength=>64 },
+      { field=>'community', size=>40, maxlength=>50 },
+    ;
+
+    $opt->{'labels'}{'shortname'} = 'Short name';
+    $opt->{'labels'}{'secret'}    = 'Shared secret';
+    $opt->{'labels'}{'type'}      = 'Type';
+    $opt->{'labels'}{'ports'}     = 'Ports';
+    $opt->{'labels'}{'server'}    = 'Server';
+    $opt->{'labels'}{'community'} = 'Community';
   }
-}
-
-#location
-my $location = $svc_broadband->location;
+};
 
-print '<TR><TD VALIGN="top" ALIGN="right">Location</TD><TD BGCOLOR="#e8e8e8">';
-if ( $part_svc->part_svc_column('location')->columnflag eq 'F' ) {
-  print qq!<PRE>${location}</PRE></TD></TR>!;
-} else {
-  print qq!<TEXTAREA ROWS="4" COLS="30" NAME="location">${location}</TEXTAREA>!;
-}
+my $svc_field_callback = sub {
+  my ($cgi, $object, $fieldref) = @_;
 
-print '</TABLE><BR><INPUT TYPE="submit" VALUE="Submit">';
+  my $columndef = $part_svc->part_svc_column($fieldref->{'field'});
+  if ($fieldref->{field} eq 'usergroup' && $columndef->columnflag eq 'F') {
+    $fieldref->{'formatted_value'} = 
+      [ $object->radius_groups('long_description') ];
+  }
 
-print <<END;
+}; 
 
-    </FORM>
-  </BODY>
-</HTML>
-END
-%>
+</%init>