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)
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
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"
110 #handling "disabled" fields in the records
111 'disableable' => 1, # set set to 1 (or column position for "disabled"
112 # status col) to enable if this table has a "disabled"
113 # field, to hide disabled records & have
114 # "show disabled/hide disabled" links
115 #(can't be used with a literal query)
116 'disabled_statuspos' => 3, #optional position (starting from 0) to insert
117 #a Status column when showing disabled records
118 #(query needs to be a qsearch hashref and
119 # header & fields need to be defined)
121 #handling agent virtualization
122 'agent_virt' => 1, # set true if this search should be
124 'agent_null' => 1, # set true to view global records always
125 'agent_null_right' => 'Access Right', # optional right to view global
127 'agent_null_right_link' => 'Access Right' # optional right to link to
128 # global records; defaults to
129 # same as agent_null_right
130 'agent_pos' => 3, # optional position (starting from 0) to
131 # insert an Agent column (query needs to be a
132 # qsearch hashref and header & fields need to
135 # sort, link & display properties for fields
137 'sort_fields' => [], #optional list of field names or SQL expressions for
140 #listref - each item is the empty string,
141 # or a listref of link and method name to append,
142 # or a listref of link and coderef to run and append
143 # or a coderef that returns such a listref
146 #listref - each item is the empty string,
147 # or a string onClick handler for the corresponding link
148 # or a coderef that returns string onClick handler
149 'link_onclicks' => [],
151 #one letter for each column, left/right/center/none
152 # or pass a listref with full values: [ 'left', 'right', 'center', '' ]
155 #listrefs of ( scalars or coderefs )
156 # currently only HTML, maybe eventually Excel too
159 'style' => [], #<B> or <I>, etc.
160 'cell_style' => [], #STYLE= attribute of TR, very HTML-specific...
162 # Excel-specific listref of ( hashrefs or coderefs )
163 # each hashref: http://search.cpan.org/dist/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm#Format_methods_and_Format_properties
164 'xls_format' => => [],
168 'download_label' => 'Download this report',
169 # defaults to 'Download full results'
173 % if ( $type eq 'csv' ) {
175 <% include('search-csv.html', header=>$header, rows=>$rows, opt=>\%opt ) %>
177 % } elsif ( $type =~ /\.xls$/ ) {
179 <& 'search-xls.html', header=>$header, rows=>$rows, opt=>\%opt &>\
180 % # prevent the caller from polluting our output stream
183 % } elsif ( $type eq 'xml' ) {
185 <% include('search-xml.html', rows=>$rows, opt=>\%opt ) %>
189 <% include('search-html.html',
193 link_agentnums => \@link_agentnums,
194 null_link => $null_link,
196 maxrecords => $maxrecords,
206 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
208 my $curuser = $FS::CurrentUser::CurrentUser;
210 my $type = $cgi->param('_type') =~ /^(csv|\w*\.xls|xml|select|html(-print)?)$/
213 if ( !$curuser->access_right('Download report data') ) {
214 $opt{'disable_download'} = 1;
225 $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
226 unless !$opt{align} || ref($opt{align});
228 if($type =~ /csv|xls/) {
229 my $h = $opt{'header'};
233 if( ref($h->[$i]) and exists($h->[$i]->{'nodownload'}) ) {
234 splice(@{$opt{$_}}, $i, 1) foreach
235 qw(header footer fields links link_onclicks
236 align color size style cell_style xls_format);
241 } while ( exists($h->[$i]) );
245 $opt{disable_download} = 0
246 if $opt{disable_download} && $curuser->access_right('Configuration download');
248 $opt{disable_download} = 1
249 if $opt{really_disable_download};
251 my @link_agentnums = ();
253 if ( $opt{'agent_virt'} ) {
255 @link_agentnums = $curuser->agentnums;
256 $null_link = $curuser->access_right( $opt{'agent_null_right_link'}
257 || $opt{'agent_null_right'} );
259 my $agentnums_sql = $curuser->agentnums_sql(
260 'null' => $opt{'agent_null'},
261 'null_right' => $opt{'agent_null_right'},
262 'table' => $opt{'query'}{'table'},
265 $opt{'query'}{'extra_sql'} .=
266 ( $opt{'query'}{'extra_sql'} =~ /WHERE/i || keys %{$opt{'query'}{'hashref'}}
268 : ' WHERE ' ). $agentnums_sql;
270 $opt{'count_query'} .=
271 ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ). $agentnums_sql;
273 if ( $opt{'agent_pos'} || $opt{'agent_pos'} eq '0'
274 and scalar($curuser->agentnums) > 1 ) {
275 #false laziness w/statuspos above
276 my $pos = $opt{'agent_pos'};
278 foreach my $att (qw( align color size style cell_style xls_format )) {
279 $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
282 splice @{ $opt{'header'} }, $pos, 0, 'Agent';
283 splice @{ $opt{'align'} }, $pos, 0, 'c';
284 splice @{ $opt{'style'} }, $pos, 0, '';
285 splice @{ $opt{'size'} }, $pos, 0, '';
286 splice @{ $opt{'fields'} }, $pos, 0,
287 sub { $_[0]->agentnum ? $_[0]->agent->agent : '(global)'; };
288 splice @{ $opt{'color'} }, $pos, 0, '';
289 splice @{ $opt{'links'} }, $pos, 0, '' #[ 'agent link?', 'agentnum' ]
291 splice @{ $opt{'link_onclicks'} }, $pos, 0, ''
292 if $opt{'link_onclicks'};
298 if ( $opt{'disableable'} ) {
300 unless ( $cgi->param('showdisabled') ) { #modify searches
302 $opt{'query'}{'hashref'}{'disabled'} = '';
303 $opt{'query'}{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
305 $opt{'count_query'} .=
306 ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
307 "( disabled = '' OR disabled IS NULL )";
309 } elsif ( $opt{'disabled_statuspos'}
310 || $opt{'disabled_statuspos'} eq '0' ) { #add status column
312 my $pos = $opt{'disabled_statuspos'};
314 foreach my $att (qw( align style color size )) {
315 $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
318 splice @{ $opt{'header'} }, $pos, 0, 'Status';
319 splice @{ $opt{'align'} }, $pos, 0, 'c';
320 splice @{ $opt{'style'} }, $pos, 0, 'b';
321 splice @{ $opt{'size'} }, $pos, 0, '';
322 splice @{ $opt{'fields'} }, $pos, 0,
323 sub { shift->disabled ? 'DISABLED' : 'Active'; };
324 splice @{ $opt{'color'} }, $pos, 0,
325 sub { shift->disabled ? 'FF0000' : '00CC00'; };
326 splice @{ $opt{'links'} }, $pos, 0, ''
328 splice @{ $opt{'link_onlicks'} }, $pos, 0, ''
329 if $opt{'link_onlicks'};
332 #add show/hide disabled links
333 my $items = $opt{'name'} || PL($opt{'name_singular'});
334 if ( $cgi->param('showdisabled') ) {
335 $cgi->param('showdisabled', 0);
336 $opt{'html_posttotal'} .=
337 '( <a href="'. $cgi->self_url. qq!">hide disabled $items</a> )!; #"
338 $cgi->param('showdisabled', 1);
340 $cgi->param('showdisabled', 1);
341 $opt{'html_posttotal'} .=
342 '( <a href="'. $cgi->self_url. qq!">show disabled $items</a> )!; #"
343 $cgi->param('showdisabled', 0);
349 my($confmax, $maxrecords, $offset );
351 unless ( $type =~ /^(csv|\w*.xls)$/) {
353 unless (exists($opt{count_query}) && length($opt{count_query})) {
354 ( $opt{count_query} = $opt{query} ) =~
355 s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i; #silly vim:/
358 if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
360 ( ( $opt{count_query} =~ /WHERE/i ) ? ' AND ' : ' WHERE ' ).
361 "( disabled = '' OR disabled IS NULL )";
364 unless ( $type eq 'html-print' ) {
366 #setup some pagination things if we're in html mode
368 my $conf = new FS::Conf;
369 $confmax = $conf->config('maxsearchrecordsperpage');
370 if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
373 $maxrecords ||= $confmax;
376 $opt{'disable_maxselect'} ||= $conf->exists('disable_maxselect');
378 $limit = $maxrecords ? "LIMIT $maxrecords" : '';
380 $offset = $cgi->param('offset') =~ /^(\d+)$/ ? $1 : 0;
381 $limit .= " OFFSET $offset" if $offset;
389 #if ( $cgi->param('order_by') =~ /^([\w\, ]+)$/ ) {
392 $order_by = $cgi->param('order_by') if $cgi->param('order_by');
396 my $header = [ map { ref($_) ? $_->{'label'} : $_ } @{$opt{header}} ];
398 if ( ref($opt{query}) ) {
401 if (ref($opt{query}) eq 'HASH') {
402 @query = ( $opt{query} );
405 if ( $opt{query}->{'order_by'} ) {
406 if ( $opt{query}->{'order_by'} =~ /^(\s*ORDER\s+BY\s+)?(\S.*)$/is ) {
407 $opt{query}->{'order_by'} = "ORDER BY $order_by, $2";
409 warn "unparsable query order_by: ". $opt{query}->{'order_by'};
410 die "unparsable query order_by: ". $opt{query}->{'order_by'};
413 $opt{query}->{'order_by'} = "ORDER BY $order_by";
417 } elsif (ref($opt{query}) eq 'ARRAY') {
418 @query = @{ $opt{query} };
420 die "invalid query reference";
423 if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
424 #%search = ( 'disabled' => '' );
425 $opt{'query'}->{'hashref'}->{'disabled'} = '';
426 $opt{'query'}->{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
429 #eval "use FS::$opt{'query'};";
430 my @param = qw( select table addl_from hashref extra_sql order_by );
431 $rows = [ qsearch( [ map { my $query = $_;
432 ({ map { $_ => $query->{$_} } @param });
436 'order_by' => $opt{order_by}. " ". $limit,
440 my $sth = dbh->prepare("$opt{'query'} $limit")
441 or die "Error preparing $opt{'query'}: ". dbh->errstr;
443 or die "Error executing $opt{'query'}: ". $sth->errstr;
445 #can get # of rows without fetching them all?
446 $rows = $sth->fetchall_arrayref;
448 $header ||= $sth->{NAME};