Optimize "Customer has a referring customer" condition, RT#74452
[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 = {
93     table     => 'cust_attachment',
94     hashref   => { disabled => $disabled ? { op => '>', value => 0 } : '', },
95     order_by  => "ORDER BY $orderby",
96     addl_from => 'JOIN cust_main USING (custnum)',
97     extra_sql => ' AND ' . $curuser->agentnums_sql,
98 };
99
100 my $count_query = 'SELECT COUNT(*) FROM cust_attachment'
101   . ' JOIN cust_main USING (custnum)'
102   . ' WHERE ' . $curuser->agentnums_sql
103   . ' AND disabled ' . ( $disabled ? '> 0' : 'IS NULL' );
104
105 my $sub_cust = sub {
106   my $c = qsearchs('cust_main', { custnum => shift->custnum } );
107   return $c ? encode_entities($c->name) : '<FONT COLOR="red"><B>(not found)</B></FONT>';
108 };
109
110 my $sub_date = sub {
111   time2str("%b %o, %Y", shift->_date);
112 };
113
114 my $sub_size = sub {
115   my $size = shift->size;
116   return $size if $size < 1024;
117   return int($size/1024).'K' if $size < 1048576;
118   return int($size/1048576).'M';
119 };
120
121 my $sub_checkbox = sub {
122   return '' if $noactions;
123   my $attach = shift;
124   my $attachnum = $attach->attachnum;
125   $areboxes = 1;
126   return qq!<INPUT NAME="attachnum$attachnum" TYPE="checkbox" VALUE="1">!;
127 };
128
129 my $sub_edit_link = sub {
130   my $attach = shift;
131   my $attachnum = $attach->attachnum;
132   my $custnum = $attach->custnum;
133   return include('/elements/popup_link_onclick.html',
134            action => popurl(2).'edit/cust_main_attach.cgi?'.
135                      "custnum=$custnum;attachnum=$attachnum",
136            actionlabel => 'Edit attachment properties',
137            width  => 510,
138            height => 315,
139            frame  => 'top',
140            );
141 };
142
143 sub selflink {
144   my $label = shift;
145   my %new_param = @_;
146   my $param = $cgi->Vars;
147   my %old_param = %$param;
148   @{$param}{keys(%new_param)} = values(%new_param);
149   my $link = '<a href="'.$cgi->self_url.'">'.$label.'</a>';
150   %$param = %old_param;
151   return $link;
152 }
153
154 sub confirm {
155   my $action = shift;
156   my $onclick = "return(confirm('$action all selected files?'))";
157   return qq!onclick="$onclick"!;
158 }
159
160 my $sub_foot = sub {
161   return '' if ($noactions or !$areboxes);
162   my $foot = 
163 '<BR><INPUT TYPE="button" VALUE="Select all" onClick="setAll(true)">
164 <INPUT TYPE="button" VALUE="Unselect all" onClick="setAll(false)">';
165   if ($disabled) {
166     if ($curuser->access_right('Undelete attachment')) {
167       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Undelete selected">';
168     }
169     if ($curuser->access_right('Purge attachment')) {
170       $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Purge selected" '.confirm('Purge').'>';
171     }
172   }
173   else {
174     $foot .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="Delete selected" '.confirm('Delete').'>';
175   }
176   $foot .= 
177 '<SCRIPT TYPE="text/javascript">
178   function setAll(setTo) { 
179     theForm = document.attachForm;
180     for (i=0,n=theForm.elements.length;i<n;i++)
181       if (theForm.elements[i].name.indexOf("attachnum") != -1)
182         theForm.elements[i].checked = setTo;
183   }
184 </SCRIPT>';
185   return $foot;
186 };
187
188 </%init>