374af4770ce29917dcd6f25e1f77150977f4c63a
[freeside.git] / httemplate / edit / svc_broadband.cgi
1 <& elements/svc_Common.html,
2      'post_url'             => popurl(1). 'process/svc_broadband.cgi',
3      'name'                 => 'broadband service',
4      'table'                => 'svc_broadband',
5      'fields'               => \@fields, 
6      'svc_field_callback'   => $svc_field_callback,
7      'svc_new_callback'     => $svc_edit_callback,
8      'svc_edit_callback'    => $svc_edit_callback,
9      'svc_error_callback'   => $svc_edit_callback,
10      'dummy'                => $cgi->query_string,
11      'onsubmit'             => 'validate_coords',
12      'html_foot'            => $js,
13 &>
14 <%init>
15
16 die "access denied"
17   unless $FS::CurrentUser::CurrentUser->access_right('Provision customer service'); #something else more specific?
18
19 # If it's stupid but it works, it's still stupid.
20 #  -Kristian
21
22 my $conf = new FS::Conf;
23
24 my $js = <<END
25     <script type="text/javascript">
26         function validate_coords(f){
27 END
28 ;
29 if ( $conf->exists('svc_broadband-require-nw-coordinates') ) {
30 $js .= <<END
31             var lon = f.longitude;
32             var lat = f.latitude;
33             if ( lon == null || lat == null || 
34                 lon.value.length == 0 || lat.value.length == 0 ) return true;
35
36             return (ut_coord(lat.value,1,90) && ut_coord(lon.value,-180,-1));
37         } // validate_coords
38
39         /* this is a JS re-implementation of FS::Record::ut_coord */
40         function ut_coord(coord,lower,upper) {
41             var neg = /^-/.test(coord);
42             coord = coord.replace(/^-/,'');
43
44             var d = 0;
45             var m = 0;
46             var s = 0;
47             
48             var t1 = /^(\\s*\\d{1,3}(?:\\.\\d+)?)\\s*\$/.exec(coord);
49             var t2 = /^(\\s*\\d{1,3})\\s+(\\d{1,2}(?:\\.\\d+))\\s*\$/.exec(coord);
50             var t3 = /^(\\s*\\d{1,3})\\s+(\\d{1,2})\\s+(\\d{1,3})\\s*\$/.exec(coord);
51             if ( t1 != null ) {
52                 d = t1[1];
53             } else if ( t2 != null ) {
54                 d = t2[1];
55                 m = t2[2];
56             } else if ( t3 != null ) {
57                 d = t3[1];
58                 m = t3[2];
59                 s = t3[3];
60             } else {
61                 alert('Invalid co-ordinates! Latitude must be positive and longitude must be negative.');
62                 return false;
63             } 
64             
65             var ts = /^\\d{3}\$/.exec(s);
66             if ( ts != null || s > 59 ) {
67                s /= 1000; 
68             } else {
69                 s /= 60;
70             }
71             s /= 60;
72
73             m /= 60;
74             if ( m > 59 ) {
75                 alert('Invalid coordinate with minutes > 59');
76                 return false;
77             }
78
79             var tmp = parseInt(d)+parseInt(m)+parseInt(s);
80             tmp = tmp.toFixed(8);
81             coord = (neg ? -1 : 1) * tmp;
82
83             if(coord < lower) {
84                 alert('Error: invalid coordinate < '+lower);
85                 return false;
86             }
87             if(coord > upper) {
88                 alert('Error: invalid coordinate > '+upper);
89                 return false;
90             }
91
92             return true;
93 END
94 ;
95 }
96 $js .= <<END
97         }
98     </script>
99 END
100 ;
101
102 my @fields = (
103   qw( description speed_down speed_up speed_test_down speed_test_up speed_test_latency),
104   { field=>'sectornum', type=>'select-tower_sector', },
105   { field=>'routernum', type=>'select-router_block_ip', 
106     include_opt_callback => sub { 
107       my $svc_broadband = shift;
108       my $part_svc = $svc_broadband->part_svc;
109       return () unless $part_svc; #sanity check
110       my $col = $part_svc->part_svc_column('ip_addr');
111       return () unless $col; #sanity check
112       $col->set('required', 'Y') unless $conf->exists('svc_broadband-allow_null_ip_addr');
113       return ('ip_addr_required' => $col->required);
114     },
115   },
116   { field=>'mac_addr' , type=>'input-mac_addr' },
117   qw(
118       latitude longitude altitude
119       radio_serialnum radio_location poe_location rssi suid
120     ),
121   { field=>'shared_svcnum', type=>'search-svc_broadband', },
122   qw( vlan_profile performance_profile authkey plan_id ),
123 );
124
125 if ( $conf->exists('svc_broadband-radius') ) {
126   push @fields,
127   { field     => 'usergroup',
128     type      => 'select-radius_group',
129     multiple  => 1,
130   }
131 }
132
133 my $part_svc;
134
135 my $svc_edit_callback = sub {
136   my ($cgi, $svc_x, $part_svc_x, $cust_pkg, $fields, $opt) = @_;
137
138   $part_svc = $part_svc_x; #for field_callback to use
139
140   my ($nas_export) = $part_svc->part_export('broadband_nas');
141   #can we assume there's only one of these per part_svc?
142   if ( $nas_export ) {
143     my $nas;
144     if ( $svc_x->svcnum ) {
145       $nas = qsearchs('nas', { 'svcnum' => $svc_x->svcnum });
146     }
147     $nas ||= $nas_export->default_nas;
148     $svc_x->set($_, $nas->$_) foreach fields('nas');
149
150     # duplicates the fields in httemplate/edit/nas.html (mostly)
151     push @$fields,
152       { type  => 'tablebreak-tr-title', 
153         #value => 'Attached NAS',
154         value => $nas_export->exportname,
155         colspan => 2,
156       },
157       { field=>'nasnum', type=>'hidden', },
158       { field=>'shortname', size=>16, maxlength=>32 },
159       { field=>'secret', size=>40, maxlength=>60, required=>1 },
160       { field=>'type', type=>'select',
161         options=>[qw( cisco computone livingston max40xx multitech netserver
162         pathras patton portslave tc usrhiper other )],
163       },
164       { field=>'ports', size=>5 },
165       { field=>'server', size=>40, maxlength=>64 },
166       { field=>'community', size=>40, maxlength=>50 },
167     ;
168
169     $opt->{'labels'}{'shortname'} = 'Short name';
170     $opt->{'labels'}{'secret'}    = 'Shared secret';
171     $opt->{'labels'}{'type'}      = 'Type';
172     $opt->{'labels'}{'ports'}     = 'Ports';
173     $opt->{'labels'}{'server'}    = 'Server';
174     $opt->{'labels'}{'community'} = 'Community';
175   }
176 };
177
178 my $svc_field_callback = sub {
179   my ($cgi, $object, $fieldref) = @_;
180
181   my $columndef = $part_svc->part_svc_column($fieldref->{'field'});
182   if ($fieldref->{field} eq 'usergroup' && $columndef->columnflag eq 'F') {
183     $fieldref->{'formatted_value'} = 
184       [ $object->radius_groups('long_description') ];
185   }
186
187 }; 
188
189 </%init>