Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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       if ( $type eq 'title' ) {
187         $html .= qq!<TR><TH COLSPAN=1 ALIGN="right"><FONT SIZE="+1">! .
188                  $label .
189                  '</FONT></TH></TR>';
190         next;
191       }
192
193       # 'freeform': disables table formatting of options.  Instead, each 
194       # option can define "before" and "after" strings which are inserted 
195       # around the selector.
196       my $freeform = $optinfo->{freeform};
197       if ( $freeform ) {
198         $html .= $optinfo->{before} || '';
199       }
200       else {
201         $html .= qq!<TR><TD ALIGN="right">$label</TD><TD>!;
202       }
203       if ( $type eq 'select' ) {
204         my $size = defined($optinfo->{size}) ? " SIZE=" . $optinfo->{size} : '';
205         my $multi = ($optinfo->{multi} || $optinfo->{multiple})
206                       ? ' MULTIPLE' : '';
207         $html .= qq!<SELECT NAME="$option"$multi$size>!;
208         my @values = split '\s+', $value if $multi;
209         my @options;
210         if (defined($optinfo->{option_values})) {
211           my $valsub = $optinfo->{option_values};
212           @options = &$valsub();
213         } elsif (defined($optinfo->{options})) {
214           @options = @{$optinfo->{options}};
215         }
216         foreach my $select_option ( @options ) {
217           #if ( ref($select_option) ) {
218           #} else {
219             my $selected = ($multi ? grep {$_ eq $select_option} @values : $select_option eq $value ) ? ' SELECTED' : '';
220             my $label = $select_option;
221             if (defined($optinfo->{option_label})) {
222               my $labelsub = $optinfo->{option_label};
223               $label = &$labelsub($select_option);
224             }
225             $html .= qq!<OPTION VALUE="$select_option"$selected>!.
226                      qq!$label</OPTION>!;
227           #}
228         }
229         $html .= '</SELECT>';
230       } elsif ( $type eq 'textarea' ) {
231         $html .= qq!<TEXTAREA NAME="$option" COLS=80 ROWS=8 WRAP="virtual">!.
232                  encode_entities($value). '</TEXTAREA>';
233       } elsif ( $type eq 'text' ) {
234         $html .= qq!<INPUT TYPE="text" NAME="$option" VALUE="!. #"
235                  encode_entities($value). '" SIZE=64>';
236       } elsif ( $type eq 'checkbox' ) {
237         $html .= qq!<INPUT TYPE="checkbox" NAME="$option" VALUE="1"!;
238         $html .= ' CHECKED' if $value;
239         $html .= '>';
240       } else {
241         $html .= "unknown type $type";
242       }
243       if ( $freeform ) {
244         $html .= $optinfo->{after} || '';
245       }
246       else {
247         $html .= '</TD></TR>';
248       }
249     }
250
251     if ( $exports->{$layer}{nas} and qsearch('nas',{}) ) {
252       # show NAS checkboxes
253       $html .= '<TR><TD ALIGN="right">Export RADIUS clients</TD><TD>';
254
255       $html .= include('/elements/checkboxes-table.html',
256                         'source_obj'    => $part_export,
257                         'link_table'    => 'export_nas',
258                         'target_table'  => 'nas',
259                         #hashref => {},
260                         'name_callback' => sub { 
261                           $_[0]->shortname . ' (' . $_[0]->nasname . ')',
262                         },
263                         'default'       => 'yes',
264                         'target_link'   => $p.'edit/nas.html?',
265                       );
266
267       $html .= '</TD></TR>';
268     }
269
270     $html .= '</TABLE>';
271
272     $html .= '<INPUT TYPE="hidden" NAME="options" VALUE="'.
273              join(',', keys %{$exports->{$layer}{options}} ). '">';
274
275     $html .= '<INPUT TYPE="hidden" NAME="nodomain" VALUE="'.
276              $exports->{$layer}{nodomain}. '">';
277
278     $html .= '<INPUT TYPE="submit" VALUE="'.
279              ( $part_export->exportnum ? "Apply changes" : "Add export" ).
280              '">';
281
282     $html;
283   },
284 );
285
286 </%init>