2935d68a0e494a840678ef885f4e1d8247494626
[freeside.git] / httemplate / edit / process / cust_main_attach.cgi
1 %if ($error) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). 'cust_main_attach.cgi?'. $cgi->query_string ) %>
4 %} else {
5 % my $act = 'added';
6 % $act = 'updated' if ($attachnum);
7 % $act = 'purged' if($attachnum and $purge);
8 % $act = 'undeleted' if($attachnum and $undelete);
9 % $act = 'deleted' if($attachnum and $delete);
10 <& /elements/header-popup.html, 'Attachment ' . $act  &>
11     <SCRIPT TYPE="text/javascript">
12       topreload();
13     </SCRIPT>
14     </BODY></HTML>
15 % }
16 <%init>
17
18 my $error;
19 $cgi->param('custnum') =~ /^(\d+)$/
20   or die "Illegal custnum: ". $cgi->param('custnum');
21 my $custnum = $1;
22
23 $cgi->param('attachnum') =~ /^(\d*)$/
24   or die "Illegal attachnum: ". $cgi->param('attachnum');
25 my $attachnum = $1;
26
27 my $filename = $cgi->param('file');
28 # strip directory names; thanks, IE7
29 $filename =~ s!.*[\/\\]!!;
30
31 my $curuser = $FS::CurrentUser::CurrentUser;
32
33 my $delete = $cgi->param('delete');
34 my $undelete = $cgi->param('undelete');
35 my $purge = $cgi->param('purge');
36
37 my $new = new FS::cust_attachment ( {
38   attachnum => $attachnum,
39   custnum   => $custnum,
40   _date     => time,
41   usernum   => $curuser->usernum,
42   disabled  => '',
43 });
44 my $old;
45
46 if($attachnum) {
47   $old = qsearchs('cust_attachment', { attachnum => $attachnum });
48   if(!$old) {
49     $error = "Attachnum '$attachnum' not found";
50   }
51   elsif($purge) { # do nothing
52   }
53   else {
54     map { $new->$_($old->$_) } 
55       ('_date', 'otaker', 'body', 'disabled');
56     $new->filename($filename || $old->filename);
57     $new->mime_type($cgi->param('mime_type') || $old->mime_type);
58     $new->title($cgi->param('title'));
59     if($delete and not $old->disabled) {
60       $new->disabled(time);
61     }
62     if($undelete and $old->disabled) {
63       $new->disabled('');
64     }
65   }
66 }
67 else { # This is a new attachment, so require a file.
68
69   if($filename) {
70     $new->filename($filename);
71     # use the original filename here, not the stripped form
72     $new->mime_type($cgi->uploadInfo($cgi->param('file'))->{'Content-Type'});
73     $new->title($cgi->param('title'));
74     
75     local $/;
76     my $fh = $cgi->upload('file');
77     $new->body(<$fh>);
78   }
79   else {
80     $error = 'No file uploaded';
81   }
82 }
83 my $action = 'Add';
84 $action = 'Edit' if $attachnum;
85 $action = 'Delete' if $attachnum and $delete;
86 $action = 'Undelete' if $attachnum and $undelete;
87 $action = 'Purge' if $attachnum and $purge;
88
89 $error = 'access denied' unless $curuser->access_right($action . ' attachment');
90
91 if(!$error) {
92   if($old and $old->disabled and $purge) {
93     $error = $old->delete;
94   }
95   elsif($old) {
96     $error = $new->replace($old);
97   }
98   else {
99     $error = $new->insert;
100   }
101 }
102
103 </%init>