5 <& elements/search.html,
11 'title' => 'Page title',
13 'name_singular' => 'item', #singular name for the records returned
14 #OR# # (preferred, will be pluralized automatically)
15 'name' => 'items', #plural name for the records returned
16 # (deprecated, will be singularlized
19 #literal SQL query string (deprecated?) or qsearch hashref or arrayref
20 #of qsearch hashrefs for a union of qsearches
22 'table' => 'tablename',
23 #everything else is optional...
24 'hashref' => { 'field' => 'value',
25 'field' => { 'op' => '<',
30 'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
31 'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
32 'order_by' => 'ORDER BY something',
35 # "select * from tablename";
37 #required unless 'query' is an SQL query string (shouldn't be...)
38 'count_query' => 'SELECT COUNT(*) FROM tablename',
41 # recommended / common
44 #listref of column labels, <TH>
45 #recommended unless 'query' is an SQL query string
46 # (if not specified the database column names will be used) (XXX this is not currently working either)
49 { 'label' => 'Another Item',
54 #listref - each item is a literal column name (or method) or coderef
55 #if not specified all columns will be shown (XXX this is not currently working?)
58 sub { my $row = shift; $row->column; },
61 #redirect if there's only one item...
62 # listref of URL base and column name (or method)
63 # or a coderef that returns the same
64 'redirect' => sub { my( $record, $cgi ) = @_;
65 [ popurl(2).'view/item.html', 'primary_key' ];
68 #redirect if there's no items
69 # scalar URL or a coderef that returns a URL
70 'redirect_empty' => sub { my( $cgi ) = @_;
71 popurl(2).'view/item.html';
78 # some HTML callbacks...
79 'menubar' => '', #menubar arrayref
80 'html_init' => '', #after the header/menubar and before the pager
81 'html_form' => '', #after the pager, right before the results
82 # (only shown if there are results)
83 # (use this for any form-opening tag rather than
84 # html_init, to avoid a nested form)
85 'html_foot' => '', #at the bottom
86 'html_posttotal' => '', #at the bottom
87 # (these three can be strings or coderefs)
89 'count_addl' => [], #additional count fields listref of sprintf strings or coderefs
90 # [ $money_char.'%.2f total paid', ],
92 #second (smaller) header line, currently only for HTML
95 { 'label' => 'Another Item',
100 #listref of column footers
104 'disable_download' => '', # set true to hide the CSV/Excel download links
105 'disable_total' => '', # set true to hide the total"
106 'disable_maxselect' => '', # set true to disable record/page selection
107 'disable_nonefound' => '', # set true to disable the "No matching Xs found"
109 'nohtmlheader' => '', # set true to remove the header and menu bar
111 #handling "disabled" fields in the records
112 'disableable' => 1, # set set to 1 (or column position for "disabled"
113 # status col) to enable if this table has a "disabled"
114 # field, to hide disabled records & have
115 # "show disabled/hide disabled" links
116 #(can't be used with a literal query)
117 'disabled_statuspos' => 3, #optional position (starting from 0) to insert
118 #a Status column when showing disabled records
119 #(query needs to be a qsearch hashref and
120 # header & fields need to be defined)
122 #handling agent virtualization
123 'agent_virt' => 1, # set true if this search should be
125 'agent_null' => 1, # set true to view global records always
126 'agent_null_right' => 'Access Right', # optional right to view global
128 'agent_null_right_link' => 'Access Right' # optional right to link to
129 # global records; defaults to
130 # same as agent_null_right
131 'agent_pos' => 3, # optional position (starting from 0) to
132 # insert an Agent column (query needs to be a
133 # qsearch hashref and header & fields need to
134 # be defined)cust_pkg_susp.html
136 # sort, link & display properties for fields
138 'sort_fields' => [], #optional list of field names or SQL expressions for
141 #listref - each item is the empty string,
142 # or a listref of link and method name to append,
143 # or a listref of link and coderef to run and append
144 # or a coderef that returns such a listref
147 #listref - each item is the empty string,
148 # or a string onClick handler for the corresponding link
149 # or a coderef that returns string onClick handler
150 'link_onclicks' => [],
152 #one letter for each column, left/right/center/none
153 # or pass a listref with full values: [ 'left', 'right', 'center', '' ]
156 #listrefs of ( scalars or coderefs )
157 # currently only HTML, maybe eventually Excel too
160 'style' => [], #<B> or <I>, etc.
161 'cell_style' => [], #STYLE= attribute of TR, very HTML-specific...
163 # Excel-specific listref of ( hashrefs or coderefs )
164 # each hashref: http://search.cpan.org/dist/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm#Format_methods_and_Format_properties
165 'xls_format' => => [],
169 'download_label' => 'Download this report',
170 # defaults to 'Download full results'
171 'link_field' => 'pkgpart'
172 # will create internal links for each row,
173 # with the value of this field as the NAME attribute
174 # If this is a coderef, will evaluate it, passing the
175 # row as an argument, and use the result as the NAME.
179 % if ( $type eq 'csv' ) {
181 <% include('search-csv.html', header=>$header, rows=>$rows, opt=>\%opt ) %>
183 % } elsif ( $type =~ /\.xls$/ ) {
185 <& 'search-xls.html', header=>$header, rows=>$rows, opt=>\%opt &>\
186 % # prevent the caller from polluting our output stream
189 % } elsif ( $type eq 'xml' ) {
191 <% include('search-xml.html', rows=>$rows, opt=>\%opt ) %>
195 <% include('search-html.html',
199 link_agentnums => \@link_agentnums,
200 null_link => $null_link,
202 maxrecords => $maxrecords,
213 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
215 my $curuser = $FS::CurrentUser::CurrentUser;
217 my $type = $cgi->param('_type') =~ /^(csv|\w*\.xls|xml|select|html(-print)?)$/
220 if ( !$curuser->access_right('Download report data') ) {
221 $opt{'disable_download'} = 1;
225 if($type =~ /csv|xls/) {
226 my $h = $opt{'header'};
230 if( ref($h->[$i]) and exists($h->[$i]->{'nodownload'}) ) {
231 splice(@{$opt{$_}}, $i, 1) foreach
232 qw(header footer fields links link_onclicks
233 align color size style cell_style xls_format);
238 } while ( exists($h->[$i]) );
242 $opt{disable_download} = 0
243 if $opt{disable_download} && $curuser->access_right('Configuration download');
245 $opt{disable_download} = 1
246 if $opt{really_disable_download};
248 # get our queries ready
249 my $query = $opt{query} or die "query required";
250 my $count_query = $opt{count_query} or die "count_query required";
251 # there was a default count_query but it hasn't worked in about ten years
253 # set up agent restriction
254 my @link_agentnums = ();
256 if ( $opt{'agent_virt'} ) {
258 @link_agentnums = $curuser->agentnums;
259 $null_link = $curuser->access_right( $opt{'agent_null_right_link'}
260 || $opt{'agent_null_right'} );
262 my $agentnums_sql = $curuser->agentnums_sql(
263 'null' => $opt{'agent_null'},
264 'null_right' => $opt{'agent_null_right'},
265 'table' => $query->{'table'},
268 if ( $query->{'extra_sql'} =~ /\bWHERE\b/i or keys %{$query->{hashref}} ) {
269 $query->{'extra_sql'} .= " AND $agentnums_sql";
270 $count_query .= " AND $agentnums_sql";
272 $query->{'extra_sql'} .= " WHERE $agentnums_sql";
273 $count_query .= " WHERE $agentnums_sql";
276 if ( $opt{'agent_pos'} || $opt{'agent_pos'} eq '0'
277 and scalar($curuser->agentnums) > 1 ) {
278 #false laziness w/statuspos above
279 my $pos = $opt{'agent_pos'};
281 foreach my $att (qw( align color size style cell_style xls_format )) {
282 $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
285 splice @{ $opt{'header'} }, $pos, 0, 'Agent';
286 splice @{ $opt{'align'} }, $pos, 0, 'c';
287 splice @{ $opt{'style'} }, $pos, 0, '';
288 splice @{ $opt{'size'} }, $pos, 0, '';
289 splice @{ $opt{'fields'} }, $pos, 0,
290 sub { $_[0]->agentnum ? $_[0]->agent->agent : '(global)'; };
291 splice @{ $opt{'color'} }, $pos, 0, '';
292 splice @{ $opt{'links'} }, $pos, 0, '' #[ 'agent link?', 'agentnum' ]
294 splice @{ $opt{'link_onclicks'} }, $pos, 0, ''
295 if $opt{'link_onclicks'};
301 if ( $opt{'disableable'} ) {
303 unless ( $cgi->param('showdisabled') ) { #modify searches
305 $query->{'hashref'}{'disabled'} = '';
306 $query->{'extra_sql'} =~ s/^\s*\bWHERE\b/ AND/i;
308 my $table = $query->{'table'};
311 ( $count_query =~ /\bWHERE\b/i ? ' AND ' : ' WHERE ' ).
312 "( $table.disabled = '' OR $table.disabled IS NULL )";
314 } elsif ( $opt{'disabled_statuspos'}
315 || $opt{'disabled_statuspos'} eq '0' ) { #add status column
317 my $pos = $opt{'disabled_statuspos'};
319 foreach my $att (qw( align style color size )) {
320 $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
323 splice @{ $opt{'header'} }, $pos, 0, 'Status';
324 splice @{ $opt{'align'} }, $pos, 0, 'c';
325 splice @{ $opt{'style'} }, $pos, 0, 'b';
326 splice @{ $opt{'size'} }, $pos, 0, '';
327 splice @{ $opt{'fields'} }, $pos, 0,
328 sub { shift->disabled ? 'DISABLED' : 'Active'; };
329 splice @{ $opt{'color'} }, $pos, 0,
330 sub { shift->disabled ? 'FF0000' : '00CC00'; };
331 splice @{ $opt{'links'} }, $pos, 0, ''
333 splice @{ $opt{'link_onlicks'} }, $pos, 0, ''
334 if $opt{'link_onlicks'};
337 #add show/hide disabled links
338 my $items = $opt{'name'} || PL($opt{'name_singular'});
339 if ( $cgi->param('showdisabled') ) {
340 $cgi->param('showdisabled', 0);
341 $opt{'html_posttotal'} .=
342 '( <a href="'. $cgi->self_url. qq!">hide disabled $items</a> )!; #"
343 $cgi->param('showdisabled', 1);
345 $cgi->param('showdisabled', 1);
346 $opt{'html_posttotal'} .=
347 '( <a href="'. $cgi->self_url. qq!">show disabled $items</a> )!; #"
348 $cgi->param('showdisabled', 0);
354 my($confmax, $maxrecords, $offset );
356 unless ( $type =~ /^(csv|xml|\w*.xls)$/) {
359 unless ( $type eq 'html-print' ) {
361 #setup some pagination things if we're in html mode
363 my $conf = new FS::Conf;
364 $confmax = $conf->config('maxsearchrecordsperpage');
365 if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
368 $maxrecords ||= $confmax;
371 $opt{'disable_maxselect'} ||= $conf->exists('disable_maxselect');
373 $limit = $maxrecords ? "LIMIT $maxrecords" : '';
375 $offset = $cgi->param('offset') =~ /^(\d+)$/ ? $1 : 0;
376 $limit .= " OFFSET $offset" if $offset;
383 my $order_by = $opt{order_by} || '';
384 $order_by = $cgi->param('order_by') if $cgi->param('order_by');
388 my $header = [ map { ref($_) ? $_->{'label'} : $_ } @{$opt{header}} ];
392 if (ref($query) eq 'HASH') {
396 if ( $query->{'order_by'} ) {
397 if ( $query->{'order_by'} =~ /^(\s*ORDER\s+BY\s+)?(\S.*)$/is ) {
398 $query->{'order_by'} = "ORDER BY $order_by, $2";
400 warn "unparsable query order_by: ". $query->{'order_by'};
401 die "unparsable query order_by: ". $query->{'order_by'};
404 $query->{'order_by'} = "ORDER BY $order_by";
408 $query->{'order_by'} .= " $limit";
410 } elsif (ref($query) eq 'ARRAY') {
411 # do we still use this? it was for the old 477 report.
412 @query = @{ $query };
414 die "invalid query reference";
417 if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
418 #%search = ( 'disabled' => '' );
419 $opt{'query'}->{'hashref'}->{'disabled'} = '';
420 $opt{'query'}->{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
423 #eval "use FS::$opt{'query'};";
424 my @param = qw( select table addl_from hashref extra_sql order_by debug );
425 $rows = [ qsearch( [ map { my $query = $_;
426 ({ map { $_ => $query->{$_} } @param });
430 #'order_by' => $opt{order_by}. " ". $limit,
434 # run the count query to get number of rows and other totals
435 my $count_sth = dbh->prepare($count_query);
437 or die "Error executing '$count_query': ".$count_sth->errstr;
438 my $totals = $count_sth->fetchrow_arrayref;
440 push @$rows, $opt{'footer_data'} if $opt{'footer_data'};