Add cust_attachment stuff
[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 = 'undeleted' if($attachnum and $undelete);
8 % $act = 'deleted' if($attachnum and $delete);
9 <% header('Attachment ' . $act ) %>
10     <SCRIPT TYPE="text/javascript">
11       window.top.location.reload();
12     </SCRIPT>
13     </BODY></HTML>
14 % }
15 <%init>
16
17 my $error;
18 $cgi->param('custnum') =~ /^(\d+)$/
19   or die "Illegal custnum: ". $cgi->param('custnum');
20 my $custnum = $1;
21
22 $cgi->param('attachnum') =~ /^(\d*)$/
23   or die "Illegal attachnum: ". $cgi->param('attachnum');
24 my $attachnum = $1;
25
26 my $otaker = $FS::CurrentUser::CurrentUser->name;
27 $otaker = $FS::CurrentUser::CurrentUser->username
28   if ($otaker eq "User, Legacy");
29
30 my $delete = $cgi->param('delete');
31 my $undelete = $cgi->param('undelete');
32
33 my $new = new FS::cust_attachment ( {
34   attachnum => $attachnum,
35   custnum   => $custnum,
36   _date     => time,
37   otaker    => $otaker,
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   else {
48     map { $new->$_($old->$_) } 
49       ('_date', 'otaker', 'body', 'disabled');
50     $new->filename($cgi->param('filename') || $old->filename);
51     $new->mime_type($cgi->param('mime_type') || $old->mime_type);
52     if($delete and not $old->disabled) {
53       $new->disabled(time);
54     }
55     if($undelete and $old->disabled) {
56       $new->disabled('');
57     }
58   }
59 }
60 else { # This is a new attachment, so require a file.
61
62   my $filename = $cgi->param('file');
63   if($filename) {
64     $new->filename($filename);
65     $new->mime_type($cgi->uploadInfo($filename)->{'Content-Type'});
66     
67     local $/;
68     my $fh = $cgi->upload('file');
69     $new->body(<$fh>);
70   }
71   else {
72     $error = 'No file uploaded';
73   }
74 }
75 my $user = $FS::CurrentUser::CurrentUser;
76
77 $error = 'access denied' unless $user->access_right(($old ? 'Edit' : 'Add') . ' attachment');
78
79 if(!$error) {
80   if($old) {
81     $error = $new->replace($old);
82   }
83   else {
84     $error = $new->insert;
85   }
86 }
87
88 </%init>