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