import torrus 1.0.9
[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 <% header('Attachment ' . $act ) %>
11     <SCRIPT TYPE="text/javascript">
12       window.top.location.reload();
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 $curuser = $FS::CurrentUser::CurrentUser;
28
29 my $delete = $cgi->param('delete');
30 my $undelete = $cgi->param('undelete');
31 my $purge = $cgi->param('purge');
32
33 my $new = new FS::cust_attachment ( {
34   attachnum => $attachnum,
35   custnum   => $custnum,
36   _date     => time,
37   usernum   => $curuser->usernum,
38   disabled  => '',
39 });
40 my $old;
41
42 if($attachnum) {
43   $old = qsearchs('cust_attachment', { attachnum => $attachnum });
44   if(!$old) {
45     $error = "Attachnum '$attachnum' not found";
46   }
47   elsif($purge) { # do nothing
48   }
49   else {
50     map { $new->$_($old->$_) } 
51       ('_date', 'otaker', 'body', 'disabled');
52     $new->filename($cgi->param('filename') || $old->filename);
53     $new->mime_type($cgi->param('mime_type') || $old->mime_type);
54     $new->title($cgi->param('title'));
55     if($delete and not $old->disabled) {
56       $new->disabled(time);
57     }
58     if($undelete and $old->disabled) {
59       $new->disabled('');
60     }
61   }
62 }
63 else { # This is a new attachment, so require a file.
64
65   my $filename = $cgi->param('file');
66   if($filename) {
67     $new->filename($filename);
68     $new->mime_type($cgi->uploadInfo($filename)->{'Content-Type'});
69     $new->title($cgi->param('title'));
70     
71     local $/;
72     my $fh = $cgi->upload('file');
73     $new->body(<$fh>);
74   }
75   else {
76     $error = 'No file uploaded';
77   }
78 }
79 my $action = 'Add';
80 $action = 'Edit' if $attachnum;
81 $action = 'Delete' if $attachnum and $delete;
82 $action = 'Undelete' if $attachnum and $undelete;
83 $action = 'Purge' if $attachnum and $purge;
84
85 $error = 'access denied' unless $curuser->access_right($action . ' attachment');
86
87 if(!$error) {
88   if($old and $old->disabled and $purge) {
89     $error = $old->delete;
90   }
91   elsif($old) {
92     $error = $new->replace($old);
93   }
94   else {
95     $error = $new->insert;
96   }
97 }
98
99 </%init>