0fdc745a2cb21843a28b6e5ce96708cff022bf79
[freeside.git] / httemplate / browse / cust_attachment.html
1 <% include( 'elements/browse.html',
2                  'title'       => 'Attachments',
3                  'menubar'     => '',
4                  'name'        => ($disabled ? 'deleted' : '') .' attachments',
5                  'html_init'   => include('/elements/init_overlib.html') .
6                     ($curuser->access_right('View deleted attachments') ? (
7                     selflink('Show '.($disabled ? 'active' : 'deleted'),
8                              show_deleted => (1-$disabled))) : ''),
9                  'html_form'   => 
10                     qq!<FORM NAME="attachForm" ACTION="$p/misc/cust_attachment.cgi" METHOD="POST">
11                     <INPUT TYPE="hidden" NAME="orderby" VALUE="$orderby">
12                     <INPUT TYPE="hidden" NAME="show_deleted" VALUE="$disabled">!
13                     , 
14                  'query'       => { 'table'     => 'cust_attachment',
15                                     'hashref'   => $hashref,
16                                     'extra_sql' => 'ORDER BY '.$orderby,
17                                   },
18                  'count_query' => $count_query,
19                  'header' => [ selflink('#',orderby => 'attachnum'),
20                                selflink('Customer',orderby => 'custnum'),
21                                selflink('Date',orderby => '_date'),
22                                selflink('Filename',orderby => 'filename'),
23                                selflink('Size',orderby => 'length(body)'),
24                                selflink('Uploaded by',orderby => 'otaker'),
25                                selflink('Description',orderby => 'title'),
26                                '', # checkbox column
27                              ],
28                  'fields' => [
29                                'attachnum',
30                                $sub_cust,
31                                $sub_date,
32                                'filename',
33                                $sub_size,
34                                'otaker',
35                                'title',
36                                $sub_checkbox,
37                              ],
38                  'links' => [ '',
39                               [ $p.'view/cust_main.cgi?', 'custnum' ],
40                             ],
41                  'link_onclicks' => [
42                               '',
43                               '',
44                               '',
45                               $sub_edit_link,
46                               ],
47
48                  #'links' =>  [
49                  #              '',
50                  #              '',
51                  #              '',
52                  #              '',
53                  #              '',
54                  #              '', #$acct_link,
55                  #              '',
56                 'html_foot' => $sub_foot,
57              )
58
59 %>
60
61
62 <%init>
63
64 my $curuser = $FS::CurrentUser::CurrentUser;
65
66 my $conf = new FS::Conf;
67
68 my $noactions = 1;
69 my $areboxes = 0;
70
71 my $disabled = 0;
72
73 if($cgi->param('show_deleted')) {
74   if ($curuser->access_right('View deleted attachments')) {
75     $disabled = 1;
76     if ($curuser->access_right('Purge attachment') or
77         $curuser->access_right('Undelete attachment')) {
78       $noactions = 0;
79     }
80   }
81   else {
82     die "access denied";
83   }
84 }
85 else {
86   if ($curuser->access_right('Delete attachment')) {
87     $noactions = 0;
88   }
89 }
90
91 my $hashref = $disabled ? 
92   { disabled => { op => '>', value => 0 } } :
93   { disabled => '' };
94
95 my $count_query = 'SELECT COUNT(*) FROM cust_attachment WHERE '. ($disabled ?
96   'disabled > 0' : 'disabled IS NULL');
97
98 my $orderby = $cgi->param('orderby') || 'custnum';
99
100 my $sub_cust = sub {
101   my $c = qsearchs('cust_main', { custnum => shift->custnum } );
102   return $c ? $c->name : '<FONT COLOR="red"><B>(not found)</B></FONT>';
103 };
104
105 my $sub_date = sub {
106   time2str("%b %o, %Y", shift->_date);
107 };
108
109 my $sub_size = sub {
110   my $size = shift->size;
111   return $size if $size < 1024;
112   return int($size/1024).'K' if $size < 1048576;
113   return int($size/1048576).'M';
114 };
115
116 my $sub_checkbox = sub {
117   return '' if $noactions;
118   my $attach = shift;
119   my $attachnum = $attach->attachnum;
120   $areboxes = 1;
121   return qq!<INPUT NAME="attachnum$attachnum" TYPE="checkbox" VALUE="1">!;
122 };
123
124 my $sub_edit_link = sub {
125   my $attach = shift;
126   my $attachnum = $attach->attachnum;
127   my $custnum = $attach->custnum;
128   return include('/elements/popup_link_onclick.html',
129            action => popurl(2).'edit/cust_main_attach.cgi?'.
130                      "custnum=$custnum;attachnum=$attachnum",
131            actionlabel => 'Edit attachment properties',
132            width  => 510,
133            height => 315,
134            frame  => 'top',
135            );
136 };
137
138 sub selflink {
139   my $label = shift;
140   my %new_param = @_;
141   my $param = $cgi->Vars;
142   my %old_param = %$param;
143   @{$param}{keys(%new_param)} = values(%new_param);
144   my $link = '<a href="'.$cgi->self_url.'">'.$label.'</a>';
145   %$param = %old_param;
146   return $link;
147 }
148
149 sub confirm {
150   my $action = shift;
151   my $onclick = "return(confirm('$action all selected files?'))";
152   return qq!onclick="$onclick"!;
153 }
154
155 my $sub_foot = sub {
156   return '' if ($noactions or !$areboxes);
157   my $foot = 
158 '<BR><INPUT TYPE="button" VALUE="Select all" onClick="setAll(true)">
159 <INPUT TYPE="button" VALUE="Unselect all" onClick="setAll(false)">';
160   if ($disabled) {
161     if ($curuser->access_right('Undelete attachment')) {
162       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Undelete selected">';
163     }
164     if ($curuser->access_right('Purge attachment')) {
165       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Purge selected" '.confirm('Purge').'>';
166     }
167   }
168   else {
169     $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Delete selected" '.confirm('Delete').'>';
170   }
171   $foot .= 
172 '<SCRIPT TYPE="text/javascript">
173   function setAll(setTo) { 
174     theForm = document.attachForm;
175     for (i=0,n=theForm.elements.length;i<n;i++)
176       if (theForm.elements[i].name.indexOf("attachnum") != -1)
177         theForm.elements[i].checked = setTo;
178   }
179 </SCRIPT>';
180   return $foot;
181 };
182
183 </%init>