Merge branch 'patch-5' of https://github.com/gjones2/Freeside (#13854 as this bug...
[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     # create 'config_element' to generate the whole layer with a Mason component
66     if ( my $include = $exports->{$layer}{config_element} ) {
67       # might need to adjust the scope of  this at some point
68       return $m->scomp($include, 
69         part_export => $part_export,
70         layer       => $layer,
71         export_info => $exports->{$layer}
72       );
73     }
74     my $html = qq!<INPUT TYPE="hidden" NAME="exporttype" VALUE="$layer">!.
75                ntable("#cccccc",2);
76
77     if ( $layer ) {
78       $html .= '<TR><TD ALIGN="right">Description</TD><TD BGCOLOR=#ffffff>'.
79                $exports->{$layer}{notes}. '</TD></TR>';
80
81       if ( $exports->{$layer}{no_machine} ) {
82         $html .= '<INPUT TYPE="hidden" NAME="machine" VALUE="">'.
83                  '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
84       } else {
85         $html .= '<TR><TD ALIGN="right">Hostname or IP</TD><TD>';
86         my $machine = $part_export->machine;
87         if ( $exports->{$layer}{svc_machine} ) {
88           my( $N_CHK, $Y_CHK) = ( 'CHECKED', '' );
89           my( $machine_DISABLED, $pem_DISABLED) = ( '', 'DISABLED' );
90           my $part_export_machine = '';
91           if ( $cgi->param('svc_machine') eq 'Y'
92                  || $machine eq '_SVC_MACHINE'
93              )
94           {
95             $Y_CHK = 'CHECKED';
96             $N_CHK = 'CHECKED';
97             $machine_DISABLED = 'DISABLED';
98             $pem_DISABLED = '';
99             $machine = '';
100             $part_export_machine =
101               $cgi->param('part_export_machine')
102               || join "\n",
103                    map $_->machine,
104                      grep ! $_->disabled,
105                        $part_export->part_export_machine;
106           }
107           my $oc = qq(onChange="${layer}_svc_machine_changed(this)");
108           $html .= qq[
109             <INPUT TYPE="radio" NAME="svc_machine" VALUE="N" $N_CHK $oc>
110             <INPUT TYPE="text" NAME="machine" ID="${layer}_machine" VALUE="$machine" $machine_DISABLED>
111             <BR>
112             <INPUT TYPE="radio" NAME="svc_machine" VALUE="Y" $Y_CHK $oc>
113             Selected in each customer service from these choices
114             <TEXTAREA NAME="part_export_machine" ID="${layer}_part_export_machine" $pem_DISABLED>$part_export_machine</TEXTAREA>
115
116             <SCRIPT TYPE="text/javascript">
117               function ${layer}_svc_machine_changed (what) {
118                 if ( what.checked ) {
119                   var machine = document.getElementById("${layer}_machine");
120                   var part_export_machine = document.getElementById("${layer}_part_export_machine");
121                   if ( what.value == 'Y' ) {
122                     machine.disabled = true;
123                     part_export_machine.disabled = false;
124                   } else if ( what.value == 'N' ) {
125                     machine.disabled = false;
126                     part_export_machine.disabled = true;
127                   }
128                 }
129               }
130             </SCRIPT>
131           ];
132         } else {
133           $html .= qq(<INPUT TYPE="text" NAME="machine" VALUE="$machine">).
134                      '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
135         }
136         $html .= "</TD></TR>";
137       }
138
139     }
140
141     foreach my $option ( keys %{$exports->{$layer}{options}} ) {
142       my $optinfo = $exports->{$layer}{options}{$option};
143       die "Retreived non-ref export info option from $layer export: $optinfo"
144         unless ref($optinfo);
145       my $label = $optinfo->{label};
146       my $type = defined($optinfo->{type}) ? $optinfo->{type} : 'text';
147       my $value = $cgi->param($option)
148                  || ( $part_export->exportnum && $part_export->option($option) )
149                  || ( (exists $optinfo->{default} && !$part_export->exportnum)
150                       ? $optinfo->{default}
151                       : ''
152                     );
153       if ( $type eq 'title' ) {
154         $html .= qq!<TR><TH COLSPAN=1 ALIGN="right"><FONT SIZE="+1">! .
155                  $label .
156                  '</FONT></TH></TR>';
157         next;
158       }
159
160       # 'freeform': disables table formatting of options.  Instead, each 
161       # option can define "before" and "after" strings which are inserted 
162       # around the selector.
163       my $freeform = $optinfo->{freeform};
164       if ( $freeform ) {
165         $html .= $optinfo->{before} || '';
166       }
167       else {
168         $html .= qq!<TR><TD ALIGN="right">$label</TD><TD>!;
169       }
170       if ( $type eq 'select' ) {
171         my $size = defined($optinfo->{size}) ? " SIZE=" . $optinfo->{size} : '';
172         my $multi = ($optinfo->{multi} || $optinfo->{multiple})
173                       ? ' MULTIPLE' : '';
174         $html .= qq!<SELECT NAME="$option"$multi$size>!;
175         my @values = split '\s+', $value if $multi;
176         my @options;
177         if (defined($optinfo->{option_values})) {
178           my $valsub = $optinfo->{option_values};
179           @options = &$valsub();
180         } elsif (defined($optinfo->{options})) {
181           @options = @{$optinfo->{options}};
182         }
183         foreach my $select_option ( @options ) {
184           #if ( ref($select_option) ) {
185           #} else {
186             my $selected = ($multi ? grep {$_ eq $select_option} @values : $select_option eq $value ) ? ' SELECTED' : '';
187             my $label = $select_option;
188             if (defined($optinfo->{option_label})) {
189               my $labelsub = $optinfo->{option_label};
190               $label = &$labelsub($select_option);
191             }
192             $html .= qq!<OPTION VALUE="$select_option"$selected>!.
193                      qq!$label</OPTION>!;
194           #}
195         }
196         $html .= '</SELECT>';
197       } elsif ( $type eq 'textarea' ) {
198         $html .= qq!<TEXTAREA NAME="$option" COLS=80 ROWS=8 WRAP="virtual">!.
199                  encode_entities($value). '</TEXTAREA>';
200       } elsif ( $type eq 'text' ) {
201         $html .= qq!<INPUT TYPE="text" NAME="$option" VALUE="!. #"
202                  encode_entities($value). '" SIZE=64>';
203       } elsif ( $type eq 'checkbox' ) {
204         $html .= qq!<INPUT TYPE="checkbox" NAME="$option" VALUE="1"!;
205         $html .= ' CHECKED' if $value;
206         $html .= '>';
207       } else {
208         $html .= "unknown type $type";
209       }
210       if ( $freeform ) {
211         $html .= $optinfo->{after} || '';
212       }
213       else {
214         $html .= '</TD></TR>';
215       }
216     }
217
218     if ( $exports->{$layer}{nas} and qsearch('nas',{}) ) {
219       # show NAS checkboxes
220       $html .= '<TR><TD ALIGN="right">Export RADIUS clients</TD><TD>';
221
222       $html .= include('/elements/checkboxes-table.html',
223                         'source_obj'    => $part_export,
224                         'link_table'    => 'export_nas',
225                         'target_table'  => 'nas',
226                         #hashref => {},
227                         'name_callback' => sub { 
228                           $_[0]->shortname . ' (' . $_[0]->nasname . ')',
229                         },
230                         'default'       => 'yes',
231                         'target_link'   => $p.'edit/nas.html?',
232                       );
233
234       $html .= '</TD></TR>';
235     }
236
237     $html .= '</TABLE>';
238
239     $html .= '<INPUT TYPE="hidden" NAME="options" VALUE="'.
240              join(',', keys %{$exports->{$layer}{options}} ). '">';
241
242     $html .= '<INPUT TYPE="hidden" NAME="nodomain" VALUE="'.
243              $exports->{$layer}{nodomain}. '">';
244
245     $html .= '<INPUT TYPE="submit" VALUE="'.
246              ( $part_export->exportnum ? "Apply changes" : "Add export" ).
247              '">';
248
249     $html;
250   },
251 );
252
253 </%init>