f6ec208be94a548d5398f2a6f633c4de882ef566
[freeside.git] / httemplate / edit / part_export.cgi
1 <% include('/elements/header.html', "$action Export", '', ' onLoad="visualize()"') %>
2
3 <% include('/elements/error.html') %>
4
5 <SCRIPT TYPE="text/javascript">
6   function svc_machine_changed (what, layer) {
7     if ( what.checked ) {
8       var machine = document.getElementById(layer + "_machine");
9       var part_export_machine = 
10         document.getElementById(layer + "_part_export_machine");
11       if ( what.value == 'Y' ) {
12         machine.disabled = true;
13         part_export_machine.disabled = false;
14       } else if ( what.value == 'N' ) {
15         machine.disabled = false;
16         part_export_machine.disabled = true;
17       }
18     }
19   }
20
21   function part_export_machine_changed (what, layer) {
22     var select_default = document.getElementById(layer + '_default_machine');
23     var selected = select_default.value;
24     select_default.options.length = 0;
25     var choices = what.value.split("\n");
26     for (var i = 0; i < choices.length; i++) {
27       select_default.options[i] = new Option(choices[i]);
28     }
29     select_default.value = selected;
30   }
31
32 </SCRIPT>
33 <FORM NAME="dummy">
34 <INPUT TYPE="hidden" NAME="exportnum" VALUE="<% $part_export->exportnum %>">
35
36 <% ntable("#cccccc",2) %>
37 <TR>
38   <TD ALIGN="right">Export name</TD>
39   <TD>
40     <INPUT TYPE="text" NAME="exportname" VALUE="<% $part_export->exportname %>">
41   </TD>
42 </TR>
43 <TR>
44   <TD ALIGN="right">Export</TD>
45   <TD><% $widget->html %>
46
47 <% include('/elements/footer.html') %>
48 <%init>
49
50 die "access denied"
51   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
52
53 #if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
54 #  $cgi->param('clone', $1);
55 #} else {
56 #  $cgi->param('clone', '');
57 #}
58
59 my($query) = $cgi->keywords;
60 my $action = '';
61 my $part_export = '';
62 if ( $cgi->param('error') ) {
63   $part_export = new FS::part_export ( {
64     map { $_, scalar($cgi->param($_)) } fields('part_export')
65   } );
66 } elsif ( $query =~ /^(\d+)$/ ) {
67   $part_export = qsearchs('part_export', { 'exportnum' => $1 } );
68 } else {
69   $part_export = new FS::part_export;
70 }
71 $action ||= $part_export->exportnum ? 'Edit' : 'Add';
72
73 #my $exports = FS::part_export::export_info($svcdb);
74 my $exports = FS::part_export::export_info();
75
76 tie my %layers, 'Tie::IxHash',
77   '' => '',
78   map { $_ => "$_ - ". $exports->{$_}{desc} } 
79   sort { $a cmp $b }
80   keys %$exports;
81 ;
82
83 my $widget = new HTML::Widgets::SelectLayers(
84   'selected_layer' => $part_export->exporttype,
85   'options'        => \%layers,
86   'form_name'      => 'dummy',
87   'form_action'    => 'process/part_export.cgi',
88   'form_text'      => [qw( exportnum exportname )],
89   'html_between'    => "</TD></TR></TABLE>\n",
90   'layer_callback'  => sub {
91     my $layer = shift;
92     # create 'config_element' to generate the whole layer with a Mason component
93     if ( my $include = $exports->{$layer}{config_element} ) {
94       # might need to adjust the scope of  this at some point
95       return $m->scomp($include, 
96         part_export => $part_export,
97         layer       => $layer,
98         export_info => $exports->{$layer}
99       );
100     }
101     my $html = qq!<INPUT TYPE="hidden" NAME="exporttype" VALUE="$layer">!.
102                ntable("#cccccc",2);
103
104     if ( $layer ) {
105       $html .= '<TR><TD ALIGN="right">Description</TD><TD BGCOLOR=#ffffff>'.
106                $exports->{$layer}{notes}. '</TD></TR>';
107
108       if ( $exports->{$layer}{no_machine} ) {
109         $html .= '<INPUT TYPE="hidden" NAME="machine" VALUE="">'.
110                  '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
111       } else {
112         $html .= '<TR><TD ALIGN="right">Hostname or IP</TD><TD>';
113         my $machine = $part_export->machine;
114         if ( $exports->{$layer}{svc_machine} ) {
115           my( $N_CHK, $Y_CHK) = ( 'CHECKED', '' );
116           my( $machine_DISABLED, $pem_DISABLED) = ( '', 'DISABLED' );
117           my @part_export_machine;
118           my $default_machine = '';
119           if ( $cgi->param('svc_machine') eq 'Y'
120                  || $machine eq '_SVC_MACHINE'
121              )
122           {
123             $Y_CHK = 'CHECKED';
124             $N_CHK = 'CHECKED';
125             $machine_DISABLED = 'DISABLED';
126             $pem_DISABLED = '';
127             $machine = '';
128             @part_export_machine = $cgi->param('part_export_machine');
129             if (!@part_export_machine) {
130               @part_export_machine = 
131                    map $_->machine,
132                      grep ! $_->disabled,
133                        $part_export->part_export_machine;
134             }
135             $default_machine =
136               $cgi->param('default_machine_name')
137               || $part_export->default_export_machine;
138           }
139           my $oc = qq(onChange="svc_machine_changed(this, '$layer')");
140           $html .= qq[
141             <INPUT TYPE="radio" NAME="svc_machine" VALUE="N" $N_CHK $oc>
142             <INPUT TYPE="text" NAME="machine" ID="${layer}_machine" VALUE="$machine" $machine_DISABLED>
143             <BR>
144             <INPUT TYPE="radio" NAME="svc_machine" VALUE="Y" $Y_CHK $oc>
145             <DIV STYLE="display:inline-block; vertical-align: top; text-align: right">
146               Selected in each customer service from these choices:
147               <TEXTAREA STYLE="vertical-align: top" NAME="part_export_machine"
148                 ID="${layer}_part_export_machine"
149                 onchange="part_export_machine_changed(this, '$layer')"
150                 $pem_DISABLED>] .
151                 
152                 join("\n", @part_export_machine) .
153                 
154                 qq[</TEXTAREA>
155               <BR>
156               Default: 
157               <SELECT NAME="default_machine_name" ID="${layer}_default_machine">
158           ];
159           foreach (@part_export_machine) {
160             $_ = encode_entities($_); # oh noes, XSS
161             my $sel = ($default_machine eq $_) ? ' SELECTED' : '';
162             $html .= qq!<OPTION VALUE="$_"$sel>$_</OPTION>\n!;
163           }
164           $html .= '</DIV></SELECT>'
165         } else {
166           $html .= qq(<INPUT TYPE="text" NAME="machine" VALUE="$machine">).
167                      '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
168         }
169         $html .= "</TD></TR>";
170       }
171
172     }
173
174     foreach my $option ( keys %{$exports->{$layer}{options}} ) {
175       my $optinfo = $exports->{$layer}{options}{$option};
176       die "Retreived non-ref export info option from $layer export: $optinfo"
177         unless ref($optinfo);
178       my $label = $optinfo->{label};
179       my $type = defined($optinfo->{type}) ? $optinfo->{type} : 'text';
180       my $value = $cgi->param($option)
181                  || ( $part_export->exportnum && $part_export->option($option) )
182                  || ( (exists $optinfo->{default} && !$part_export->exportnum)
183                       ? $optinfo->{default}
184                       : ''
185                     );
186
187       #handle these with post_config_element
188       next if $type eq 'custom';
189
190       if ( $type eq 'title' ) {
191         $html .= qq!<TR><TH COLSPAN=1 ALIGN="right"><FONT SIZE="+1">! .
192                  $label .
193                  '</FONT></TH></TR>';
194         next;
195       }
196
197       # 'freeform': disables table formatting of options.  Instead, each 
198       # option can define "before" and "after" strings which are inserted 
199       # around the selector.
200       my $freeform = $optinfo->{freeform};
201       if ( $freeform ) {
202         $html .= $optinfo->{before} || '';
203       }
204       else {
205         $html .= qq!<TR><TD ALIGN="right">$label</TD><TD>!;
206       }
207       if ( $type eq 'select' ) {
208
209         # 'select' options can specify options one of two ways:
210         # the "preferred" way:
211         #   options: arrayref of allowed option values
212         #   option_labels: hashref of option value => label
213         # OR the weird and semi-deprecated way:
214         #   option_values: coderef to return a list of allowed option values
215         #   option_label: coderef to take an option value and return its label
216
217         my $size = defined($optinfo->{size}) ? " SIZE=" . $optinfo->{size} : '';
218         my $multi = ($optinfo->{multi} || $optinfo->{multiple})
219                       ? ' MULTIPLE' : '';
220         $html .= qq!<SELECT NAME="$option"$multi$size>!;
221         my @values = split '\s+', $value if $multi;
222         my @options;
223         if (defined($optinfo->{option_values})) {
224           my $valsub = $optinfo->{option_values};
225           @options = &$valsub();
226         } elsif (defined($optinfo->{options})) {
227           @options = @{$optinfo->{options}};
228         }
229         foreach my $select_option ( @options ) {
230           #if ( ref($select_option) ) {
231           #} else {
232             my $selected = ($multi ? grep {$_ eq $select_option} @values : $select_option eq $value ) ? ' SELECTED' : '';
233             my $label = $select_option;
234             if ( defined $optinfo->{option_label} ) {
235               my $labelsub = $optinfo->{option_label};
236               $label = &$labelsub($select_option);
237             } elsif ( defined $optinfo->{option_labels} ) {
238               if (exists $optinfo->{option_labels}->{$select_option}) {
239                 $label = $optinfo->{option_labels}->{$select_option};
240               }
241             }
242     
243             $html .= qq!<OPTION VALUE="$select_option"$selected>!.
244                      qq!$label</OPTION>!;
245           #}
246         }
247         $html .= '</SELECT>';
248       } elsif ( $type eq 'textarea' ) {
249         $html .= qq!<TEXTAREA NAME="$option" COLS=80 ROWS=8 WRAP="virtual">!.
250                  encode_entities($value). '</TEXTAREA>';
251       } elsif ( $type eq 'text' ) {
252         $html .= qq!<INPUT TYPE="text" NAME="$option" VALUE="!. #"
253                  encode_entities($value). '" SIZE=64>';
254       } elsif ( $type eq 'checkbox' ) {
255         $html .= qq!<INPUT TYPE="checkbox" NAME="$option" VALUE="1"!;
256         $html .= ' CHECKED' if $value;
257         $html .= '>';
258       } else {
259         $html .= "unknown type $type";
260       }
261       if ( $freeform ) {
262         $html .= $optinfo->{after} || '';
263       }
264       else {
265         $html .= '</TD></TR>';
266       }
267     }
268
269     if ( $exports->{$layer}{nas} and qsearch('nas',{}) ) {
270       # show NAS checkboxes
271       $html .= '<TR><TD ALIGN="right">Export RADIUS clients</TD><TD>';
272
273       $html .= include('/elements/checkboxes-table.html',
274                         'source_obj'    => $part_export,
275                         'link_table'    => 'export_nas',
276                         'target_table'  => 'nas',
277                         #hashref => {},
278                         'name_callback' => sub { 
279                           $_[0]->shortname . ' (' . $_[0]->nasname . ')',
280                         },
281                         'default'       => 'yes',
282                         'target_link'   => $p.'edit/nas.html?',
283                       );
284
285       $html .= '</TD></TR>';
286     }
287
288     $html .= '<TR><TD ALIGN="right">Do not suspend services</TD>' .
289                 '<TD><INPUT TYPE="checkbox" NAME="no_suspend" VALUE="Y"';
290     $html .= ' CHECKED' if $part_export->no_suspend eq 'Y';
291     $html .= '></TD></TR>';
292
293     foreach my $script ( keys %{$exports->{$layer}{scripts}} ) {
294       $html .= '<TR><TD ALIGN="left" COLSPAN=2>' .
295         include('/elements/progress-init.html',
296               $part_export->exporttype,
297               [ $script.'_exportnum', $script.'_script' ],
298               rooturl().'view/svc_export/run_script.cgi',
299               rooturl().'edit/part_export.cgi?'.$part_export->{Hash}->{exportnum},
300               $script,
301         ) .
302         '<INPUT TYPE="hidden" NAME="'.$script.'_exportnum" VALUE="'.$part_export->{Hash}->{exportnum}.'">
303          <INPUT TYPE="hidden" NAME="'.$script.'_script" VALUE="'.$script.'">
304         <A HREF="#" onClick="'.$script.'process();">'.$exports->{$layer}{scripts}{$script}->{html_label}.'</A></TD></TR>';
305     }
306
307     $html .= '</TABLE>';
308
309     # false laziness with config_element above
310     # create 'post_config_element' to generate the whole layer with a Mason component
311     if ( my $include = $exports->{$layer}{post_config_element} ) {
312       # might need to adjust the scope of  this at some point
313       $html .= $m->scomp($include, 
314         part_export => $part_export,
315         layer       => $layer,
316         export_info => $exports->{$layer}
317       );
318     }
319
320     $html .= '<INPUT TYPE="hidden" NAME="options" VALUE="'.
321              join(',', keys %{$exports->{$layer}{options}} ). '">';
322
323     $html .= '<INPUT TYPE="hidden" NAME="nodomain" VALUE="'.
324              $exports->{$layer}{nodomain}. '">';
325
326     $html .= '<INPUT TYPE="submit" VALUE="'.
327              ( $part_export->exportnum ? "Apply changes" : "Add export" ).
328              '">';
329
330     $html;
331   },
332 );
333
334 </%init>