default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / edit / process / part_export.cgi
1 %if ( $error ) {
2 %  $cgi->param('error', $error );
3 <% $cgi->redirect(popurl(2). "part_export.cgi?". $cgi->query_string ) %>
4 %} else {
5 <% $cgi->redirect(popurl(3). "browse/part_export.cgi") %>
6 %}
7 <%init>
8
9 die "access denied"
10   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
11
12 my $exportnum = $cgi->param('exportnum');
13
14 my $old = qsearchs('part_export', { 'exportnum'=>$exportnum } ) if $exportnum;
15
16 my %vars = $cgi->Vars;
17 #fixup options
18 #warn join('-', split(',',$cgi->param('options')));
19 my %options = map {
20   my $value = $vars{$_};
21   $value =~ s/\0/ /g; # deal with multivalued options
22   $value =~ s/\r\n/\n/g; #browsers? (textarea)
23   $_ => $value;
24 } split(',', $cgi->param('options'));
25
26 # deal with multiline options
27 # %vars should never contain incomplete rows, but just in case it does, 
28 # we make a list of all the row indices that contain values, and 
29 # then write a line in each option for each row, even if it's empty.
30 # This ensures that all values with the same row index line up.
31 my %optionrows;
32 foreach my $option (split(',', $cgi->param('multi_options'))) {
33   $optionrows{$option} = {};
34   my %values; # bear with me
35   for (keys %vars) {
36     /^$option(\d+)/ or next;
37     $optionrows{$option}{$1} = $vars{$option.$1};
38     $optionrows{_ALL_}{$1} = 1 if length($vars{$option.$1});
39   }
40 }
41 foreach my $option (split(',', $cgi->param('multi_options'))) {
42   my $value = '';
43   foreach my $row (sort keys %{$optionrows{_ALL_}}) {
44     $value .= ($optionrows{$option}{$row} // '') . "\n";
45   }
46   chomp($value);
47   $options{$option} = $value;
48 }
49
50 my $new = new FS::part_export ( {
51   map {
52     $_, scalar($cgi->param($_));
53   } fields('part_export')
54 } );
55
56 if ( $cgi->param('svc_machine') eq 'Y' ) {
57   $new->machine('_SVC_MACHINE');
58   $new->part_export_machine_textarea( $cgi->param('part_export_machine') );
59   $new->default_machine_name( $cgi->param('default_machine_name') );
60 }
61
62 my $error;
63 if ( $exportnum ) {
64   #warn $old;
65   #warn $exportnum;
66   #warn $new->machine;
67   $error = $new->replace($old,\%options);
68 } else {
69   $error = $new->insert(\%options);
70 #  $exportnum = $new->exportnum;
71 }
72
73 my $info = FS::part_export::export_info()->{$new->exporttype};
74 if ( $info->{nas} ) {
75   my @nasnums = map { /^nasnum(\d+)$/ ? $1 : () } keys %{ $cgi->Vars };
76   $error ||= $new->process_m2m(
77     link_table    => 'export_nas',
78     target_table  => 'nas',
79     params        => \@nasnums
80   );
81 }
82
83 </%init>