select agent and customer fields on attachment report, RT#81809
[freeside.git] / httemplate / search / cust_attachment.html
1 <% include( 'elements/search.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                                FS::UI::Web::cust_header(
19                                  $cgi->param('cust_fields')
20                                ),
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                                \&FS::UI::Web::cust_fields,
32                                $sub_date,
33                                'filename',
34                                $sub_size,
35                                'otaker',
36                                'title',
37                                $sub_checkbox,
38                              ],
39                  'links' => [ '',
40                               ( map { $_ ne 'Cust. Status' ? $clink : '' }
41                                     FS::UI::Web::cust_header(
42                                       $cgi->param('cust_fields')
43                                     )
44                               ),
45                             ],
46                  'link_onclicks' => [
47                               '',
48                               ( map '', FS::UI::Web::cust_header(
49                                           $cgi->param('cust_fields')
50                                         )
51                               ),
52                               '',
53                               $sub_edit_link,
54                               ],
55                  'color' => [
56                               '',
57                               FS::UI::Web::cust_colors(),
58                             ],
59                  'style' => [
60                               '',
61                               FS::UI::Web::cust_styles(),
62                             ],
63                  'align'  => 'r'. FS::UI::Web::cust_aligns(),
64
65                  #'links' =>  [
66                  #              '',
67                  #              '',
68                  #              '',
69                  #              '',
70                  #              '',
71                  #              '', #$acct_link,
72                  #              '',
73                 'html_foot' => $sub_foot,
74              )
75
76 %>
77
78
79 <%init>
80
81 my $curuser = $FS::CurrentUser::CurrentUser;
82 die "access denied" if !$curuser->access_right('View attachments')
83                     or !$curuser->access_right('Browse attachments');
84
85 my $conf = new FS::Conf;
86
87 my $noactions = 1;
88 my $areboxes = 0;
89
90 my $disabled = 0;
91
92 if($cgi->param('show_deleted')) {
93   if ($curuser->access_right('View deleted attachments')) {
94     $disabled = 1;
95     if ($curuser->access_right('Purge attachment') or
96         $curuser->access_right('Undelete attachment')) {
97       $noactions = 0;
98     }
99   }
100   else {
101     die "access denied";
102   }
103 }
104 else {
105   if ($curuser->access_right('Delete attachment')) {
106     $noactions = 0;
107   }
108 }
109
110 my $orderby = $cgi->param('orderby') || 'custnum';
111
112 my $extra_sql = '';
113 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
114   $extra_sql = " AND agentnum = $1 ";
115 }
116
117 my $query = {
118     table     => 'cust_attachment',
119     hashref   => { disabled => $disabled ? { op => '>', value => 0 } : '', },
120     order_by  => "ORDER BY $orderby",
121     addl_from => 'JOIN cust_main USING (custnum)',
122     extra_sql => "$extra_sql AND ". $curuser->agentnums_sql,
123 };
124
125 my $count_query = 'SELECT COUNT(*) FROM cust_attachment'
126   . ' JOIN cust_main USING (custnum)'
127   . ' WHERE ' . $curuser->agentnums_sql
128   . ' AND disabled ' . ( $disabled ? '> 0' : 'IS NULL' );
129
130
131 #still need to report on attachments with deleted customer?  did we ever?
132 #my $sub_cust = sub {
133 #  my $c = qsearchs('cust_main', { custnum => shift->custnum } );
134 #  return $c ? encode_entities($c->name) : '<FONT COLOR="red"><B>(not found)</B></FONT>';
135 #};
136
137 my $clink = [ $p.'view/cust_main.cgi?', 'custnum' ];
138
139 my $sub_date = sub {
140   time2str("%b %o, %Y", shift->_date);
141 };
142
143 my $sub_size = sub {
144   my $size = shift->size;
145   return $size if $size < 1024;
146   return int($size/1024).'K' if $size < 1048576;
147   return int($size/1048576).'M';
148 };
149
150 my $sub_checkbox = sub {
151   return '' if $noactions;
152   my $attach = shift;
153   my $attachnum = $attach->attachnum;
154   $areboxes = 1;
155   return qq!<INPUT NAME="attachnum$attachnum" TYPE="checkbox" VALUE="1">!;
156 };
157
158 my $sub_edit_link = sub {
159   my $attach = shift;
160   my $attachnum = $attach->attachnum;
161   my $custnum = $attach->custnum;
162   return include('/elements/popup_link_onclick.html',
163            action => popurl(2).'edit/cust_main_attach.cgi?'.
164                      "custnum=$custnum;attachnum=$attachnum",
165            actionlabel => 'Edit attachment properties',
166            width  => 510,
167            height => 315,
168            frame  => 'top',
169            );
170 };
171
172 sub selflink {
173   my $label = shift;
174   my %new_param = @_;
175   my $param = $cgi->Vars;
176   my %old_param = %$param;
177   @{$param}{keys(%new_param)} = values(%new_param);
178   my $link = '<a href="'.$cgi->self_url.'">'.$label.'</a>';
179   %$param = %old_param;
180   return $link;
181 }
182
183 sub confirm {
184   my $action = shift;
185   my $onclick = "return(confirm('$action all selected files?'))";
186   return qq!onclick="$onclick"!;
187 }
188
189 my $sub_foot = sub {
190   return '' if ($noactions or !$areboxes);
191   my $foot = 
192 '<BR><INPUT TYPE="button" VALUE="Select all" onClick="setAll(true)">
193 <INPUT TYPE="button" VALUE="Unselect all" onClick="setAll(false)">';
194   if ($disabled) {
195     if ($curuser->access_right('Undelete attachment')) {
196       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Undelete selected">';
197     }
198     if ($curuser->access_right('Purge attachment')) {
199       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Purge selected" '.confirm('Purge').'>';
200     }
201   }
202   else {
203     $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Delete selected" '.confirm('Delete').'>';
204   }
205   $foot .= 
206 '<SCRIPT TYPE="text/javascript">
207   function setAll(setTo) { 
208     theForm = document.attachForm;
209     for (i=0,n=theForm.elements.length;i<n;i++)
210       if (theForm.elements[i].name.indexOf("attachnum") != -1)
211         theForm.elements[i].checked = setTo;
212   }
213 </SCRIPT>';
214   return $foot;
215 };
216
217 </%init>