summaryrefslogtreecommitdiff
path: root/httemplate/edit/part_export.cgi
blob: 30e4218e2e2837499b761444830153bc9d7b4c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<% include('/elements/header.html', "$action Export", '', ' onLoad="visualize()"') %>

<% include('/elements/error.html') %>

<SCRIPT TYPE="text/javascript">
  function svc_machine_changed (what, layer) {
    if ( what.checked ) {
      var machine = document.getElementById(layer + "_machine");
      var part_export_machine = 
        document.getElementById(layer + "_part_export_machine");
      if ( what.value == 'Y' ) {
        machine.disabled = true;
        part_export_machine.disabled = false;
      } else if ( what.value == 'N' ) {
        machine.disabled = false;
        part_export_machine.disabled = true;
      }
    }
  }

  function part_export_machine_changed (what, layer) {
    var select_default = document.getElementById(layer + '_default_machine');
    var selected = select_default.value;
    select_default.options.length = 0;
    var choices = what.value.split("\n");
    for (var i = 0; i < choices.length; i++) {
      select_default.options[i] = new Option(choices[i]);
    }
    select_default.value = selected;
  }

</SCRIPT>
<FORM NAME="dummy">
<INPUT TYPE="hidden" NAME="exportnum" VALUE="<% $part_export->exportnum %>">

<% ntable("#cccccc",2) %>
<TR>
  <TD ALIGN="right">Export name</TD>
  <TD>
    <INPUT TYPE="text" NAME="exportname" VALUE="<% $part_export->exportname %>">
  </TD>
</TR>
<TR>
  <TD ALIGN="right">Export</TD>
  <TD><% $widget->html %>

<% include('/elements/footer.html') %>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');

#if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
#  $cgi->param('clone', $1);
#} else {
#  $cgi->param('clone', '');
#}

my($query) = $cgi->keywords;
my $action = '';
my $part_export = '';
if ( $cgi->param('error') ) {
  $part_export = new FS::part_export ( {
    map { $_, scalar($cgi->param($_)) } fields('part_export')
  } );
} elsif ( $cgi->param('exportnum') ) {
  $part_export = qsearchs('part_export', { 'exportnum' => $cgi->param('exportnum') } );
} elsif ( $query =~ /^(\d+)$/ ) {
  $part_export = qsearchs('part_export', { 'exportnum' => $1 } );
} else {
  $part_export = new FS::part_export;
}
$action ||= $part_export->exportnum ? 'Edit' : 'Add';

#my $exports = FS::part_export::export_info($svcdb);
my $exports = FS::part_export::export_info();

tie my %layers, 'Tie::IxHash',
  '' => '',
  map { $_ => "$_ - ". $exports->{$_}{desc} } 
  sort { $a cmp $b }
  keys %$exports;
;

my $widget = new HTML::Widgets::SelectLayers(
  'selected_layer' => $part_export->exporttype,
  'options'        => \%layers,
  'form_name'      => 'dummy',
  'form_action'    => 'process/part_export.cgi',
  'form_text'      => [qw( exportnum exportname )],
  'html_between'    => "</TD></TR></TABLE>\n",
  'layer_callback'  => sub {
    my $layer = shift;
    # create 'config_element' to generate the whole layer with a Mason component
    if ( my $include = $exports->{$layer}{config_element} ) {
      # might need to adjust the scope of  this at some point
      return $m->scomp($include, 
        part_export => $part_export,
        layer       => $layer,
        export_info => $exports->{$layer}
      );
    }
    my $html = qq!<INPUT TYPE="hidden" NAME="exporttype" VALUE="$layer">!.
               ntable("#cccccc",2);

    if ( $layer ) {
      $html .= '<TR><TD ALIGN="right">Description</TD><TD BGCOLOR=#ffffff>'.
               $exports->{$layer}{notes}. '</TD></TR>';

      if ( $exports->{$layer}{no_machine} ) {
        $html .= '<INPUT TYPE="hidden" NAME="machine" VALUE="">'.
                 '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
      } else {
        $html .= '<TR><TD ALIGN="right">Hostname or IP</TD><TD>';
        my $machine = $part_export->machine;
        if ( $exports->{$layer}{svc_machine} ) {
          my( $N_CHK, $Y_CHK) = ( 'CHECKED', '' );
          my( $machine_DISABLED, $pem_DISABLED) = ( '', 'DISABLED' );
          my @part_export_machine;
          my $default_machine = '';
          if ( $cgi->param('svc_machine') eq 'Y'
                 || $machine eq '_SVC_MACHINE'
             )
          {
            $Y_CHK = 'CHECKED';
            $N_CHK = 'CHECKED';
            $machine_DISABLED = 'DISABLED';
            $pem_DISABLED = '';
            $machine = '';
            @part_export_machine = $cgi->param('part_export_machine');
            if (!@part_export_machine) {
              @part_export_machine = 
                   map $_->machine,
                     grep ! $_->disabled,
                       $part_export->part_export_machine;
            }
            $default_machine =
              $cgi->param('default_machine_name')
              || $part_export->default_export_machine;
          }
          my $oc = qq(onChange="svc_machine_changed(this, '$layer')");
          $html .= qq[
            <INPUT TYPE="radio" NAME="svc_machine" VALUE="N" $N_CHK $oc>
            <INPUT TYPE="text" NAME="machine" ID="${layer}_machine" VALUE="$machine" $machine_DISABLED>
            <BR>
            <INPUT TYPE="radio" NAME="svc_machine" VALUE="Y" $Y_CHK $oc>
            <DIV STYLE="display:inline-block; vertical-align: top; text-align: right">
              Selected in each customer service from these choices:
              <TEXTAREA STYLE="vertical-align: top" NAME="part_export_machine"
                ID="${layer}_part_export_machine"
                onchange="part_export_machine_changed(this, '$layer')"
                $pem_DISABLED>] .
                
                join("\n", @part_export_machine) .
                
                qq[</TEXTAREA>
              <BR>
              Default: 
              <SELECT NAME="default_machine_name" ID="${layer}_default_machine">
          ];
          foreach (@part_export_machine) {
            $_ = encode_entities($_); # oh noes, XSS
            my $sel = ($default_machine eq $_) ? ' SELECTED' : '';
            $html .= qq!<OPTION VALUE="$_"$sel>$_</OPTION>\n!;
          }
          $html .= '</DIV></SELECT>'
        } else {
          $html .= qq(<INPUT TYPE="text" NAME="machine" VALUE="$machine">).
                     '<INPUT TYPE="hidden" NAME="svc_machine" VALUE=N">';
        }
        $html .= "</TD></TR>";
      }

    }

    foreach my $option ( keys %{$exports->{$layer}{options}} ) {
      my $optinfo = $exports->{$layer}{options}{$option};
      die "Retreived non-ref export info option from $layer export: $optinfo"
        unless ref($optinfo);
      my $label = $optinfo->{label};
      my $type = defined($optinfo->{type}) ? $optinfo->{type} : 'text';
      my $value = $cgi->param($option)
                 || ( $part_export->exportnum && $part_export->option($option) )
                 || ( (exists $optinfo->{default} && !$part_export->exportnum)
                      ? $optinfo->{default}
                      : ''
                    );

      #handle these with post_config_element
      next if $type eq 'custom';

      if ( $type eq 'title' ) {
        $html .= qq!<TR><TH COLSPAN=1 ALIGN="right"><FONT SIZE="+1">! .
                 $label .
                 '</FONT></TH></TR>';
        next;
      }

      # 'freeform': disables table formatting of options.  Instead, each 
      # option can define "before" and "after" strings which are inserted 
      # around the selector.
      my $freeform = $optinfo->{freeform};
      if ( $freeform ) {
        $html .= $optinfo->{before} || '';
      }
      else {
        $html .= qq!<TR><TD ALIGN="right">$label</TD><TD>!;
      }
      if ( $type eq 'select' ) {

        # 'select' options can specify options one of two ways:
        # the "preferred" way:
        #   options: arrayref of allowed option values
        #   option_labels: hashref of option value => label
        # OR the weird and semi-deprecated way:
        #   option_values: coderef to return a list of allowed option values
        #   option_label: coderef to take an option value and return its label

        my $size = defined($optinfo->{size}) ? " SIZE=" . $optinfo->{size} : '';
        my $multi = ($optinfo->{multi} || $optinfo->{multiple})
                      ? ' MULTIPLE' : '';
        $html .= qq!<SELECT NAME="$option"$multi$size>!;
        my @values = split '\s+', $value if $multi;
        my @options;
        if (defined($optinfo->{option_values})) {
          my $valsub = $optinfo->{option_values};
          @options = &$valsub();
        } elsif (defined($optinfo->{options})) {
          @options = @{$optinfo->{options}};
        }
        foreach my $select_option ( @options ) {
          #if ( ref($select_option) ) {
          #} else {
            my $selected = ($multi ? grep {$_ eq $select_option} @values : $select_option eq $value ) ? ' SELECTED' : '';
            my $label = $select_option;
            if ( defined $optinfo->{option_label} ) {
              my $labelsub = $optinfo->{option_label};
              $label = &$labelsub($select_option);
            } elsif ( defined $optinfo->{option_labels} ) {
              if (exists $optinfo->{option_labels}->{$select_option}) {
                $label = $optinfo->{option_labels}->{$select_option};
              }
            }
    
            $html .= qq!<OPTION VALUE="$select_option"$selected>!.
                     qq!$label</OPTION>!;
          #}
        }
        $html .= '</SELECT>';
      } elsif ( $type eq 'textarea' ) {
        $html .= qq!<TEXTAREA NAME="$option" COLS=80 ROWS=8 WRAP="virtual">!.
                 encode_entities($value). '</TEXTAREA>';
      } elsif ( $type eq 'text' ) {
        $html .= qq!<INPUT TYPE="text" NAME="$option" VALUE="!. #"
                 encode_entities($value). '" SIZE=64>';
      } elsif ( $type eq 'checkbox' ) {
        $html .= qq!<INPUT TYPE="checkbox" NAME="$option" VALUE="1"!;
        $html .= ' CHECKED' if $value;
        $html .= '>';
      } else {
        $html .= "unknown type $type";
      }
      if ( $freeform ) {
        $html .= $optinfo->{after} || '';
      }
      else {
        $html .= '</TD></TR>';
      }
    }

    if ( $exports->{$layer}{nas} and qsearch('nas',{}) ) {
      # show NAS checkboxes
      $html .= '<TR><TD ALIGN="right">Export RADIUS clients</TD><TD>';

      $html .= include('/elements/checkboxes-table.html',
                        'source_obj'    => $part_export,
                        'link_table'    => 'export_nas',
                        'target_table'  => 'nas',
                        #hashref => {},
                        'name_callback' => sub { 
                          $_[0]->shortname . ' (' . $_[0]->nasname . ')',
                        },
                        'default'       => 'yes',
                        'target_link'   => $p.'edit/nas.html?',
                      );

      $html .= '</TD></TR>';
    }

    $html .= '<TR><TD ALIGN="right">Do not suspend services</TD>' .
                '<TD><INPUT TYPE="checkbox" NAME="no_suspend" VALUE="Y"';
    $html .= ' CHECKED' if $part_export->no_suspend eq 'Y';
    $html .= '></TD></TR>';

    foreach my $script ( keys %{$exports->{$layer}{scripts}} ) {
      $html .= '<TR><TD ALIGN="left" COLSPAN=2>' .
        include('/elements/progress-init.html',
              $part_export->exporttype,
              [ $script.'_exportnum' ],
              rooturl().'view/svc_export/run_script.cgi',
              {
                'error_url' => rooturl().$exports->{$layer}{scripts}{$script}->{error_url}."exportnum=".$part_export->{Hash}->{exportnum},
                'reload_with_error' => '1',
                'url'       => rooturl().'edit/part_export.cgi?'.$part_export->{Hash}->{exportnum},
                'message'   => $exports->{$layer}{scripts}{$script}->{success_message},
              },
              $script,
        ) .
        '<INPUT TYPE="hidden" NAME="'.$script.'_exportnum" VALUE="'.$part_export->{Hash}->{exportnum}.'">
        <A HREF="#" onClick="'.$script.'process();">'.$exports->{$layer}{scripts}{$script}->{html_label}.'</A></TD></TR>';
    }

    $html .= '</TABLE>';

    # false laziness with config_element above
    # create 'post_config_element' to generate the whole layer with a Mason component
    if ( my $include = $exports->{$layer}{post_config_element} ) {
      # might need to adjust the scope of  this at some point
      $html .= $m->scomp($include, 
        part_export => $part_export,
        layer       => $layer,
        export_info => $exports->{$layer}
      );
    }

    $html .= '<INPUT TYPE="hidden" NAME="options" VALUE="'.
             join(',', keys %{$exports->{$layer}{options}} ). '">';

    $html .= '<INPUT TYPE="hidden" NAME="nodomain" VALUE="'.
             $exports->{$layer}{nodomain}. '">';

    $html .= '<INPUT TYPE="submit" VALUE="'.
             ( $part_export->exportnum ? "Apply changes" : "Add export" ).
             '">';

    $html;
  },
);

</%init>