service def classes, to support corecom portal, RT#17617
[freeside.git] / httemplate / edit / part_svc.cgi
1 <& /elements/header.html, "$action Service Definition",
2            menubar('View all service definitions' => "${p}browse/part_svc.cgi"),
3            #" onLoad=\"visualize()\""
4 &>
5
6 <& /elements/init_overlib.html &>
7
8 <BR>
9
10 <FORM NAME="dummy">
11
12 <FONT CLASS="fsinnerbox-title">Service Part #<% $part_svc->svcpart ? $part_svc->svcpart : "(NEW)" %></FONT>
13 <TABLE CLASS="fsinnerbox">
14 <TR>
15   <TD ALIGN="right">Service</TD>
16   <TD><INPUT TYPE="text" NAME="svc" VALUE="<% $hashref->{svc} %>"></TD>
17 <TR>
18
19 <& /elements/tr-select-part_svc_class.html, curr_value=>$hashref->{classnum} &>
20
21 <TR>
22   <TD ALIGN="right">Self-service access</TD>
23   <TD>
24     <SELECT NAME="selfservice_access">
25 % tie my %selfservice_access, 'Tie::IxHash', #false laziness w/browse/part_svc
26 %   ''         => 'Yes',
27 %   'hidden'   => 'Hidden',
28 %   'readonly' => 'Read-only',
29 % ;
30 % for (keys %selfservice_access) {
31   <OPTION VALUE="<% $_ %>"
32           <% $_ eq $hashref->{'selfservice_access'} ? 'SELECTED' : '' %>
33   ><% $selfservice_access{$_} %>
34 % }
35     </SELECT>
36   </TD>
37 </TR>
38
39
40 <TR>
41   <TD ALIGN="right">Disable new orders</TD>
42   <TD><INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>></TD>
43 </TR>
44
45 <TR>
46   <TD ALIGN="right">Preserve this service on package cancellation</TD>
47   <TD><INPUT TYPE="checkbox" NAME="preserve" VALUE="Y"<% $hashref->{'preserve'} eq 'Y' ? ' CHECKED' : '' %>>&nbsp;</TD>
48 </TR>
49
50 </TABLE>
51
52 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<% $hashref->{svcpart} %>">
53
54 <BR>
55
56 % my %vfields;
57 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
58 %  # and generalize the subs
59 %  # condition sub is tested to see whether to disable display of this choice
60 %  # params: ( $def, $layer, $field )  (see SUB below)
61 %  my $inv_sub = sub {
62 %                      $_[0]->{disable_inventory}
63 %                        || $_[0]->{'type'} ne 'text'
64 %                    };
65 %  tie my %flag, 'Tie::IxHash',
66 %    ''  => { 'desc' => 'No default', },
67 %    'D' => { 'desc' => 'Default',
68 %             'condition' =>
69 %               sub { $_[0]->{disable_default} }, 
70 %           },
71 %    'F' => { 'desc' => 'Fixed (unchangeable)',
72 %             'condition' =>
73 %               sub { $_[0]->{disable_fixed} }, 
74 %           },
75 %    'S' => { 'desc' => 'Selectable Choice',
76 %             'condition' =>
77 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
78 %           },
79 %    'M' => { 'desc' => 'Manual selection from inventory',
80 %             'condition' => $inv_sub,
81 %           },
82 %    'A' => { 'desc' => 'Automatically fill in from inventory',
83 %             'condition' => $inv_sub,
84 %           },
85 %    'H' => { 'desc' => 'Select from hardware class',
86 %             'condition' => sub { $_[0]->{type} ne 'select-hardware' },
87 %           },
88 %    'X' => { 'desc' => 'Excluded',
89 %             'condition' =>
90 %               sub { ! $vfields{$_[1]}->{$_[2]} },
91 %
92 %           },
93 %  ;
94 %  
95 %  my @dbs = $hashref->{svcdb}
96 %             ? ( $hashref->{svcdb} )
97 %             : FS::part_svc->svc_tables();
98 %
99 %  my $help = '';
100 %  unless ( $hashref->{svcpart} ) {
101 %    $help = '&nbsp;'.
102 %            include('/elements/popup_link.html',
103 %                      'action' => $p.'docs/part_svc-table.html',
104 %                      'label'  => 'help',
105 %                      'actionlabel' => 'Service table help',
106 %                      'width'       => 763,
107 %                      #'height'      => 400,
108 %                    );
109 %  }
110 %
111 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
112 %  my $widget = new HTML::Widgets::SelectLayers(
113 %    #'selected_layer' => $p_svcdb,
114 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
115 %    'options'        => \%svcdb,
116 %    'form_name'      => 'dummy',
117 %    #'form_action'    => 'process/part_svc.cgi',
118 %    'form_action'    => 'part_svc.cgi', #self
119 %    'form_elements'  => [qw( svc svcpart classnum selfservice_access
120 %                             disabled preserve
121 %                        )],
122 %    'html_between'   => $help,
123 %    'layer_callback' => sub {
124 %      my $layer = shift;
125 %      
126 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
127 %
128 %      #$html .= $svcdb_info;
129 %
130 %      my $columns = 3;
131 %      my $count = 0;
132 %      my $communigate = 0;
133 %      my @part_export =
134 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
135 %          keys %{FS::part_export::export_info($layer)};
136 %      $html .= '<BR><BR>'. include('/elements/table.html') . 
137 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
138 %      foreach my $part_export ( @part_export ) {
139 %        $communigate++ if $part_export->exporttype =~ /^communigate/;
140 %        $html .= '<TD><INPUT TYPE="checkbox"'.
141 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
142 %        $html .= 'CHECKED'
143 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
144 %              && qsearchs( 'export_svc', {
145 %                                   exportnum => $part_export->exportnum,
146 %                                   svcpart   => $clone || $part_svc->svcpart });
147 %        $html .= '>'.$part_export->exportnum. ': ';
148 %        $html .= $part_export->exportname . '<DIV ALIGN="right"><FONT SIZE=-1>'
149 %          if ( $part_export->exportname );
150 %        $html .= $part_export->exporttype. ' to '. $part_export->machine;
151 %        $html .= '</FONT></DIV>' if ( $part_export->exportname );
152 %        $html .= '</TD>';
153 %        $count++;
154 %        $html .= '</TR><TR>' unless $count % $columns;
155 %      }
156 %      $html .= '</TR></TABLE><BR><BR>'. $mod_info;
157 %
158 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
159 %               '<TR>'.
160 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
161 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Label</TH>'.
162 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
163 %               '</TR>';
164 %
165 %      my $bgcolor1 = '#eeeeee';
166 %      my $bgcolor2 = '#ffffff';
167 %      my $bgcolor;
168 %
169 %      #yucky kludge
170 %      my @fields = ();
171 %      if ( defined( dbdef->table($layer) ) ) {
172 %        @fields = grep {
173 %            $_ ne 'svcnum'
174 %            && ( $communigate || !$communigate_fields{$layer}->{$_} )
175 %            && ( !FS::part_svc->svc_table_fields($layer)
176 %                   ->{$_}->{disable_part_svc_column}
177 %                 || $part_svc->part_svc_column($_)->columnflag
178 %               )
179 %        } fields($layer);
180 %      }
181 %      push @fields, 'usergroup' 
182 %        if $layer eq 'svc_acct'
183 %          or ( $layer eq 'svc_broadband' and 
184 %               $conf->exists('svc_broadband-radius') ); # double kludge
185 %               # (but we do want to check the config, right?)
186 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
187 %
188 %
189 %      foreach my $field (@fields) {
190 %
191 %        #a few lines of false laziness w/browse/part_svc.cgi
192 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
193 %        my $def_info  = $def->{'def_info'};
194 %        my $formatter = $def->{'format'} || sub { shift };
195 %
196 %        my $part_svc_column = $part_svc->part_svc_column($field);
197 %        my $label = $part_svc_column->columnlabel || $def->{'label'};
198 %        my $value = &$formatter($part_svc_column->columnvalue);
199 %        my $flag  = $part_svc_column->columnflag;
200 %
201 %        if ( $bgcolor eq $bgcolor1 ) {
202 %          $bgcolor = $bgcolor2;
203 %        } else {
204 %          $bgcolor = $bgcolor1;
205 %        }
206 %        
207 %        $html .= qq!<TR><TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
208 %                 ( $def->{'label'} || $field ).
209 %                 "</TD>";
210 %
211 %        $html .= qq!<TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor"><INPUT NAME="${layer}__${field}_label" VALUE="!. encode_entities($label). '" STYLE="text-align:right"></TD>';
212 %
213 %        $flag = '' if $def->{type} eq 'disabled';
214 %
215 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
216 %
217 %        if ( $def->{type} eq 'disabled' ) {
218 %        
219 %          $html .= 'No default';
220 %
221 %        } else {
222 %
223 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
224 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
225 %
226 %          foreach my $f ( keys %flag ) {
227 %
228 %            # need to template-ize more httemplate/edit/svc_* first
229 %            next if $f eq 'M' and $layer !~ /^svc_(broadband|external|phone|dish)$/;
230 %
231 %            #here is where the SUB from above is called, to skip some choices
232 %            next if $flag{$f}->{condition}
233 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
234 %
235 %            $html .= qq!<OPTION VALUE="$f"!.
236 %                     ' SELECTED'x($flag eq $f ).
237 %                     '>'. $flag{$f}->{desc};
238 %
239 %          }
240 %
241 %          $html .= '</SELECT>';
242 %
243 %          $html .= join("\n",
244 %            '<SCRIPT>',
245 %            "  function ${layer}__${field}_flag_changed(what) {",
246 %            '    var f = what.options[what.selectedIndex].value;',
247 %            '    if ( f == "" || f == "X" ) { //disable',
248 %            "      what.form.${layer}__${field}.disabled = true;".
249 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
250 %            "      if ( what.form.${layer}__${field}_classnum ) {".
251 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
252 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
253 %            "      }".
254 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
255 %            "      what.form.${layer}__${field}.disabled = false;".
256 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
257 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
258 %            "        what.form.${layer}__${field}.multiple = true;".
259 %            "      } else {".
260 %            "        what.form.${layer}__${field}.multiple = false;".
261 %            "      }".
262 %            "      what.form.${layer}__${field}.style.display = '';".
263 %            "      if ( what.form.${layer}__${field}_classnum ) {".
264 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
265 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
266 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
267 %            "      }".
268 %            '    } else if ( f == "M" || f == "A" || f == "H" ) { '.
269 %                   '//enable, inventory',
270 %            "      what.form.${layer}__${field}.disabled = false;".
271 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
272 %            "      what.form.${layer}__${field}.style.display = 'none';".
273 %            "      if ( what.form.${layer}__${field}_classnum ) {".
274 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
275 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
276 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
277 %            "      }".
278 %            '    }',
279 %            '  }',
280 %            '</SCRIPT>',
281 %          );
282 %
283 %        }
284 %
285 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
286 %
287 %        my $disabled = $flag ? ''
288 %                             : 'DISABLED STYLE="background-color: #dddddd"';
289 %        my $nodisplay = ' STYLE="display:none"';
290 %
291 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
292 %
293 %          my $is_inv = ( $flag =~ /^[MA]$/ );
294 %
295 %          $html .=
296 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
297 %            $disabled.
298 %            ( $is_inv ? $nodisplay : $disabled ).
299 %            '>';
300 %
301 %          $html .= include('/elements/select-table.html',
302 %                             'element_name' => "${layer}__${field}_classnum",
303 %                             'id'           => "${layer}__${field}_classnum",
304 %                             'element_etc'  => ( $is_inv
305 %                                                   ? $disabled
306 %                                                   : $nodisplay
307 %                                               ),
308 %                             'table'        => 'inventory_class',
309 %                             'name_col'     => 'classname',
310 %                             'value'        => $value,
311 %                             'empty_label'  => 'Select inventory class',
312 %                          );
313 %
314 %        } elsif ( $def->{type} eq 'checkbox' ) {
315 %
316 %          $html .= include('/elements/checkbox.html',
317 %                             'field'      => $layer.'__'.$field,
318 %                             'curr_value' => $value,
319 %                             'value'      => 'Y',
320 %                          );
321 %
322 %        } elsif ( $def->{type} eq 'select' ) {
323 %
324 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
325 %          $html .= ' MULTIPLE' if $flag eq 'S';
326 %          $html .= '>';
327 %          $html .= '<OPTION> </OPTION>' unless $value;
328 %          if ( $def->{select_table} ) {
329 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
330 %              my $rvalue = $record->getfield($def->{select_key});
331 %              my $select_label = $def->{select_label};
332 %              $html .= qq!<OPTION VALUE="$rvalue"!.
333 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
334 %                  $record->$select_label(). '</OPTION>';
335 %            } #next $record
336 %          } elsif ( $def->{select_list} ) {
337 %            foreach my $item ( @{$def->{select_list}} ) {
338 %              $html .= qq!<OPTION VALUE="$item"!.
339 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
340 %                    $item. '</OPTION>';
341 %            } #next $item
342 %          } elsif ( $def->{select_hash} ) {
343 %            if ( ref($def->{select_hash}) eq 'ARRAY' ) {
344 %              tie my %hash, 'Tie::IxHash', @{ $def->{select_hash} };
345 %              $def->{select_hash} = \%hash;
346 %            }
347 %            foreach my $key ( keys %{$def->{select_hash}} ) {
348 %              $html .= qq!<OPTION VALUE="$key"!.
349 %                    (grep(/^$key$/, split(',',$value)) ? ' SELECTED>' : '>' ).
350 %                    $def->{select_hash}{$key}. '</OPTION>';
351 %            } #next $key
352 %          } #endif
353 %          $html .= '</SELECT>';
354 %
355 %        } elsif ( $def->{type} eq 'textarea' ) {
356 %
357 %          $html .=
358 %            qq!<TEXTAREA NAME="${layer}__${field}">!. encode_entities($value).
359 %            '</TEXTAREA>';
360 %
361 %        } elsif ( $def->{type} =~ /select-(.*?).html/ ) {
362 %
363 %          $html .= include("/elements/".$def->{type},
364 %                             'curr_value'   => $value,
365 %                             'element_name' => "${layer}__${field}",
366 %                             'element_etc'  => $disabled,
367 %                             'multiple'     => ($def->{multiple} ||
368 %                                                $flag eq 'S'),
369 %                                 # allow the table def to force 'multiple'
370 %                          );
371 %
372 %        } elsif ( $def->{type} eq 'communigate_pro-accessmodes' ) {
373 %
374 %          $html .= include('/elements/communigate_pro-accessmodes.html',
375 %                             'element_name_prefix' => "${layer}__${field}_",
376 %                             'curr_value'          => $value,
377 %                             #doesn't work#'element_etc'  => $disabled,
378 %                          );
379 %
380 %        } elsif ( $def->{type} eq 'select-hardware' ) {
381 %
382 %          $html .= qq!<INPUT TYPE="text" NAME="${layer}__${field}" $disabled>!;
383 %          $html .= include('/elements/select-hardware_class.html',
384 %                             'curr_value'    => $value,
385 %                             'element_name'  => "${layer}__${field}_classnum",
386 %                             'id'            => "${layer}__${field}_classnum",
387 %                             'element_etc'   => $flag ne 'H' && $nodisplay,
388 %                             'empty_label'   => 'Select hardware class',
389 %                          );
390 %
391 %        } elsif ( $def->{type} eq 'disabled' ) {
392 %
393 %          $html .=
394 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
395 %
396 %        } else {
397 %
398 %          $html .= '<font color="#ff0000">unknown type '. $def->{type};
399 %
400 %        }
401 %
402 %        $html .= "</TD></TR>\n";
403
404 %        $def_info = "($def_info)" if $def_info;
405 %        $html .=
406 %          qq!<TR>!.
407 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
408 %          qq!      STYLE="padding:0; border-top: none">!.
409 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
410 %          qq!  </TD>!.
411 %          qq!</TR>\n!;
412 %
413 %      } #foreach my $field (@fields) {
414 %
415 %      $part_svc->svcpart('') if $clone; #undone
416 %      $html .= "</TABLE>";
417 %
418 %      $html .= include('/elements/progress-init.html',
419 %                         $layer, #form name
420 %                         [ qw(svc svcpart classnum selfservice_access
421 %                              disabled preserve
422 %                              exportnum),
423 %                           @fields ],
424 %                         'process/part_svc.cgi',
425 %                         $p.'browse/part_svc.cgi',
426 %                         $layer,
427 %                      );
428 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
429 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
430 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
431 %               "fixup(document.$layer); $layer". 'process();">';
432 %
433 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
434 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
435 %
436 %      $html;
437 %
438 %    },
439 %  );
440
441 <BR>
442 Table <% $widget->html %>
443
444 <% include('/elements/footer.html') %>
445
446 <%init>
447
448 die "access denied"
449   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
450
451 my $conf = FS::Conf->new;
452 my $part_svc;
453 my $clone = '';
454 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
455   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
456   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
457     or die "unknown svcpart: $1";
458   $clone = $part_svc->svcpart;
459   $part_svc->svcpart('');
460 } elsif ( $cgi->keywords ) { #edit
461   my($query) = $cgi->keywords;
462   $query =~ /^(\d+)$/ or die "malformed query: $query";
463   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
464     or die "unknown svcpart: $1";
465 } else { #adding
466   $part_svc = new FS::part_svc {};
467 }
468
469 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
470 my $hashref = $part_svc->hashref;
471 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
472
473 my %communigate_fields = (
474   'svc_acct'        => { map { $_=>1 }
475                            qw( file_quota file_maxnum file_maxsize
476                                password_selfchange password_recover
477                              ),
478                            grep /^cgp_/, fields('svc_acct')
479                        },
480   'svc_domain'      => { map { $_=>1 }
481                            qw( max_accounts trailer parent_svcnum ),
482                            grep /^(cgp|acct_def)_/, fields('svc_domain')
483                        },
484   #'svc_forward'     => { map { $_=>1 } qw( ) },
485   #'svc_mailinglist' => { map { $_=>1 } qw( ) },
486   #'svc_cert'        => { map { $_=>1 } qw( ) },
487 );
488
489 my $mod_info = '
490 For the selected table, you can give fields default or fixed (unchangable)
491 values, or select an inventory class to manually or automatically fill in
492 that field.
493 ';
494
495 </%init>
496
497
498