export host selection per service, RT#17914
[freeside.git] / httemplate / edit / part_export.cgi
1 <% include('/elements/header.html', "$action Export", '', ' onLoad="visualize()"') %>
2
3 <% include('/elements/error.html') %>
4
5 <FORM NAME="dummy">
6 <INPUT TYPE="hidden" NAME="exportnum" VALUE="<% $part_export->exportnum %>">
7
8 <% ntable("#cccccc",2) %>
9 <TR>
10   <TD ALIGN="right">Export name</TD>
11   <TD>
12     <INPUT TYPE="text" NAME="exportname" VALUE="<% $part_export->exportname %>">
13   </TD>
14 </TR>
15 <TR>
16   <TD ALIGN="right">Export</TD>
17   <TD><% $widget->html %>
18
19 <% include('/elements/footer.html') %>
20 <%init>
21
22 die "access denied"
23   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
24
25 #if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
26 #  $cgi->param('clone', $1);
27 #} else {
28 #  $cgi->param('clone', '');
29 #}
30
31 my($query) = $cgi->keywords;
32 my $action = '';
33 my $part_export = '';
34 if ( $cgi->param('error') ) {
35   $part_export = new FS::part_export ( {
36     map { $_, scalar($cgi->param($_)) } fields('part_export')
37   } );
38 } elsif ( $query =~ /^(\d+)$/ ) {
39   $part_export = qsearchs('part_export', { 'exportnum' => $1 } );
40 } else {
41   $part_export = new FS::part_export;
42 }
43 $action ||= $part_export->exportnum ? 'Edit' : 'Add';
44
45 #my $exports = FS::part_export::export_info($svcdb);
46 my $exports = FS::part_export::export_info();
47
48 tie my %layers, 'Tie::IxHash',
49   '' => '',
50   map { $_ => "$_ - ". $exports->{$_}{desc} } 
51   sort { $a cmp $b }
52   keys %$exports;
53 ;
54
55 my $widget = new HTML::Widgets::SelectLayers(
56   'selected_layer' => $part_export->exporttype,
57   'options'        => \%layers,
58   'form_name'      => 'dummy',
59   'form_action'    => 'process/part_export.cgi',
60   'form_text'      => [qw( exportnum exportname )],
61 #  'form_checkbox'  => [qw()],
62   'html_between'    => "</TD></TR></TABLE>\n",
63   'layer_callback'  => sub {
64     my $layer = shift;
65     my $html = qq!<INPUT TYPE="hidden" NAME="exporttype" VALUE="$layer">!.
66                ntable("#cccccc",2);
67
68     if ( $layer ) {
69       $html .= '<TR><TD ALIGN="right">Description</TD><TD BGCOLOR=#ffffff>'.
70                $exports->{$layer}{notes}. '</TD></TR>';
71
72       if ( $exports->{$layer}{no_machine} ) {
73         $html .= '<INPUT TYPE="hidden" NAME="machine" VALUE="">'.
74                  '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
75       } else {
76         $html .= '<TR><TD ALIGN="right">Hostname or IP</TD><TD>';
77         my $machine = $part_export->machine;
78         if ( $exports->{$layer}{svc_machine} ) {
79           my( $N_CHK, $Y_CHK) = ( 'CHECKED', '' );
80           my( $machine_DISABLED, $pem_DISABLED) = ( '', 'DISABLED' );
81           my $part_export_machine = '';
82           if ( $cgi->param('svc_machine') eq 'Y'
83                  || $machine eq '_SVC_MACHINE'
84              )
85           {
86             $Y_CHK = 'CHECKED';
87             $N_CHK = 'CHECKED';
88             $machine_DISABLED = 'DISABLED';
89             $pem_DISABLED = '';
90             $machine = '';
91             $part_export_machine =
92               $cgi->param('part_export_machine')
93               || join "\n",
94                    map $_->machine,
95                      grep ! $_->disabled,
96                        $part_export->part_export_machine;
97           }
98           my $oc = qq(onChange="${layer}_svc_machine_changed(this)");
99           $html .= qq[
100             <INPUT TYPE="radio" NAME="svc_machine" VALUE="N" $N_CHK $oc>
101             <INPUT TYPE="text" NAME="machine" ID="${layer}_machine" VALUE="$machine" $machine_DISABLED>
102             <BR>
103             <INPUT TYPE="radio" NAME="svc_machine" VALUE="Y" $Y_CHK $oc>
104             Selected in each customer service from these choices
105             <TEXTAREA NAME="part_export_machine" ID="${layer}_part_export_machine" $pem_DISABLED>$part_export_machine</TEXTAREA>
106
107             <SCRIPT TYPE="text/javascript">
108               function ${layer}_svc_machine_changed (what) {
109                 if ( what.checked ) {
110                   var machine = document.getElementById("${layer}_machine");
111                   var part_export_machine = document.getElementById("${layer}_part_export_machine");
112                   if ( what.value == 'Y' ) {
113                     machine.disabled = true;
114                     part_export_machine.disabled = false;
115                   } else if ( what.value == 'N' ) {
116                     machine.disabled = false;
117                     part_export_machine.disabled = true;
118                   }
119                 }
120               }
121             </SCRIPT>
122           ];
123         } else {
124           $html .= qq(<INPUT TYPE="text" NAME="machine" VALUE="$machine">).
125                      '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
126         }
127         $html .= "</TD></TR>";
128       }
129
130     }
131
132     foreach my $option ( keys %{$exports->{$layer}{options}} ) {
133       my $optinfo = $exports->{$layer}{options}{$option};
134       die "Retreived non-ref export info option from $layer export: $optinfo"
135         unless ref($optinfo);
136       my $label = $optinfo->{label};
137       my $type = defined($optinfo->{type}) ? $optinfo->{type} : 'text';
138       my $value = $cgi->param($option)
139                  || ( $part_export->exportnum && $part_export->option($option) )
140                  || ( (exists $optinfo->{default} && !$part_export->exportnum)
141                       ? $optinfo->{default}
142                       : ''
143                     );
144       if ( $type eq 'title' ) {
145         $html .= qq!<TR><TH COLSPAN=1 ALIGN="right"><FONT SIZE="+1">! .
146                  $label .
147                  '</FONT></TH></TR>';
148         next;
149       }
150
151       # 'freeform': disables table formatting of options.  Instead, each 
152       # option can define "before" and "after" strings which are inserted 
153       # around the selector.
154       my $freeform = $optinfo->{freeform};
155       if ( $freeform ) {
156         $html .= $optinfo->{before} || '';
157       }
158       else {
159         $html .= qq!<TR><TD ALIGN="right">$label</TD><TD>!;
160       }
161       if ( $type eq 'select' ) {
162         my $size = defined($optinfo->{size}) ? " SIZE=" . $optinfo->{size} : '';
163         my $multi = ($optinfo->{multi} || $optinfo->{multiple})
164                       ? ' MULTIPLE' : '';
165         $html .= qq!<SELECT NAME="$option"$multi$size>!;
166         my @values = split '\s+', $value if $multi;
167         my @options;
168         if (defined($optinfo->{option_values})) {
169           my $valsub = $optinfo->{option_values};
170           @options = &$valsub();
171         } elsif (defined($optinfo->{options})) {
172           @options = @{$optinfo->{options}};
173         }
174         foreach my $select_option ( @options ) {
175           #if ( ref($select_option) ) {
176           #} else {
177             my $selected = ($multi ? grep {$_ eq $select_option} @values : $select_option eq $value ) ? ' SELECTED' : '';
178             my $label = $select_option;
179             if (defined($optinfo->{option_label})) {
180               my $labelsub = $optinfo->{option_label};
181               $label = &$labelsub($select_option);
182             }
183             $html .= qq!<OPTION VALUE="$select_option"$selected>!.
184                      qq!$label</OPTION>!;
185           #}
186         }
187         $html .= '</SELECT>';
188       } elsif ( $type eq 'textarea' ) {
189         $html .= qq!<TEXTAREA NAME="$option" COLS=80 ROWS=8 WRAP="virtual">!.
190                  encode_entities($value). '</TEXTAREA>';
191       } elsif ( $type eq 'text' ) {
192         $html .= qq!<INPUT TYPE="text" NAME="$option" VALUE="!. #"
193                  encode_entities($value). '" SIZE=64>';
194       } elsif ( $type eq 'checkbox' ) {
195         $html .= qq!<INPUT TYPE="checkbox" NAME="$option" VALUE="1"!;
196         $html .= ' CHECKED' if $value;
197         $html .= '>';
198       } else {
199         $html .= "unknown type $type";
200       }
201       if ( $freeform ) {
202         $html .= $optinfo->{after} || '';
203       }
204       else {
205         $html .= '</TD></TR>';
206       }
207     }
208
209     if ( $exports->{$layer}{nas} and qsearch('nas',{}) ) {
210       # show NAS checkboxes
211       $html .= '<TR><TD ALIGN="right">Export RADIUS clients</TD><TD>';
212
213       $html .= include('/elements/checkboxes-table.html',
214                         'source_obj'    => $part_export,
215                         'link_table'    => 'export_nas',
216                         'target_table'  => 'nas',
217                         #hashref => {},
218                         'name_callback' => sub { 
219                           $_[0]->shortname . ' (' . $_[0]->nasname . ')',
220                         },
221                         'default'       => 'yes',
222                         'target_link'   => $p.'edit/nas.html?',
223                       );
224
225       $html .= '</TD></TR>';
226     }
227
228     $html .= '</TABLE>';
229
230     $html .= '<INPUT TYPE="hidden" NAME="options" VALUE="'.
231              join(',', keys %{$exports->{$layer}{options}} ). '">';
232
233     $html .= '<INPUT TYPE="hidden" NAME="nodomain" VALUE="'.
234              $exports->{$layer}{nodomain}. '">';
235
236     $html .= '<INPUT TYPE="submit" VALUE="'.
237              ( $part_export->exportnum ? "Apply changes" : "Add export" ).
238              '">';
239
240     $html;
241   },
242 );
243
244 </%init>