5 include( '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
21 'table' => 'tablename',
22 #everything else is optional...
23 'hashref' => { 'field' => 'value',
24 'field' => { 'op' => '<',
29 'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
30 'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
31 'order_by' => 'ORDER BY something',
34 # "select * from tablename";
36 #required unless 'query' is an SQL query string (shouldn't be...)
37 'count_query' => 'SELECT COUNT(*) FROM tablename',
40 # recommended / common
43 #listref of column labels, <TH>
44 #recommended unless 'query' is an SQL query string
45 # (if not specified the database column names will be used)
48 { 'label' => 'Another Item',
53 #listref - each item is a literal column name (or method) or coderef
54 #if not specified all columns will be shown
57 sub { my $row = shift; $row->column; },
60 #redirect if there's only one item...
61 # listref of URL base and column name (or method)
62 # or a coderef that returns the same
63 'redirect' => sub { my( $record, $cgi ) = @_;
64 [ popurl(2).'view/item.html', 'primary_key' ];
67 #redirect if there's no items
68 # scalar URL or a coderef that returns a URL
69 'redirect_empty' => sub { my( $cgi ) = @_;
70 popurl(2).'view/item.html';
77 # some HTML callbacks...
78 'menubar' => '', #menubar arrayref
79 'html_init' => '', #after the header/menubar and before the pager
80 'html_form' => '', #after the pager, right before the results
81 # (only shown if there are results)
82 # (use this for any form-opening tag rather than
83 # html_init, to avoid a nested form)
84 'html_foot' => '', #at the bottom
85 'html_posttotal' => '', #at the bottom
86 # (these three can be strings or coderefs)
88 'count_addl' => [], #additional count fields listref of sprintf strings or coderefs
89 # [ $money_char.'%.2f total paid', ],
91 #second (smaller) header line, currently only for HTML
94 { 'label' => 'Another Item',
99 #listref of column footers
103 'disable_download' => '', # set true to hide the CSV/Excel download links
104 'disable_total' => '', # set true to hide the total"
105 'disable_maxselect' => '', # set true to disable record/page selection
106 'disable_nonefound' => '', # set true to disable the "No matching Xs found"
109 #handling "disabled" fields in the records
110 'disableable' => 1, # set set to 1 (or column position for "disabled"
111 # status col) to enable if this table has a "disabled"
112 # field, to hide disabled records & have
113 # "show disabled/hide disabled" links
114 #(can't be used with a literal query)
115 'disabled_statuspos' => 3, #optional position (starting from 0) to insert
116 #a Status column when showing disabled records
117 #(query needs to be a qsearch hashref and
118 # header & fields need to be defined)
120 #handling agent virtualization
121 'agent_virt' => 1, # set true if this search should be
123 'agent_null_right' => 'Access Right', # optional right to view global
125 'agent_null_right_link' => 'Access Right' # optional right to link to
126 # global records; defaults to
127 # same as agent_null_right
128 'agent_pos' => 3, # optional position (starting from 0) to
129 # insert an Agent column (query needs to be a
130 # qsearch hashref and header & fields need to
133 # link & display properties for fields
135 #listref - each item is the empty string,
136 # or a listref of link and method name to append,
137 # or a listref of link and coderef to run and append
138 # or a coderef that returns such a listref
141 #listref - each item is the empty string,
142 # or a string onClick handler for the corresponding link
143 # or a coderef that returns string onClick handler
144 'link_onclicks' => [],
146 #one letter for each column, left/right/center/none
147 # or pass a listref with full values: [ 'left', 'right', 'center', '' ]
150 #listrefs of ( scalars or coderefs )
151 #currently only HTML, maybe eventually Excel too
154 'style' => [], #<B> or <I>, etc.
155 'cell_style' => [], #STYLE= attribute of TR, very HTML-specific...
160 % if ( $type eq 'csv' ) {
162 <% include('search-csv.html', header=>$header, rows=>$rows, opt=>\%opt ) %>
164 % #} elsif ( $type eq 'excel' ) {
165 % } elsif ( $type =~ /\.xls$/ ) {
167 <% include('search-xls.html', header=>$header, rows=>$rows, opt=>\%opt ) %>
169 % } else { # regular HTML
171 <% include('search-html.html',
175 link_agentnums => \@link_agentnums,
176 null_link => $null_link,
178 maxrecords => $maxrecords,
188 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
190 my $curuser = $FS::CurrentUser::CurrentUser;
199 $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
200 unless !$opt{align} || ref($opt{align});
202 $opt{disable_download} = 0
203 if $opt{disable_download} && $curuser->access_right('Configuration download');
205 my @link_agentnums = ();
207 if ( $opt{'agent_virt'} ) {
209 @link_agentnums = $curuser->agentnums;
210 $null_link = $curuser->access_right( $opt{'agent_null_right_link'}
211 || $opt{'agent_null_right'} );
213 my $agentnums_sql = $curuser->agentnums_sql(
214 'null_right' => $opt{'agent_null_right'}
217 $opt{'query'}{'extra_sql'} .=
218 ( $opt{'query'}{'extra_sql'} =~ /WHERE/i || keys %{$opt{'query'}{'hashref'}}
220 : ' WHERE ' ). $agentnums_sql;
222 $opt{'count_query'} .=
223 ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ). $agentnums_sql;
225 if ( $opt{'agent_pos'} || $opt{'agent_pos'} eq '0'
226 and scalar($curuser->agentnums) > 1 ) {
227 #false laziness w/statuspos above
228 my $pos = $opt{'agent_pos'};
230 foreach my $att (qw( align style color size )) {
231 $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
234 splice @{ $opt{'header'} }, $pos, 0, 'Agent';
235 splice @{ $opt{'align'} }, $pos, 0, 'c';
236 splice @{ $opt{'style'} }, $pos, 0, '';
237 splice @{ $opt{'size'} }, $pos, 0, '';
238 splice @{ $opt{'fields'} }, $pos, 0,
239 sub { $_[0]->agentnum ? $_[0]->agent->agent : '(global)'; };
240 splice @{ $opt{'color'} }, $pos, 0, '';
241 splice @{ $opt{'links'} }, $pos, 0, '' #[ 'agent link?', 'agentnum' ]
243 splice @{ $opt{'link_onclicks'} }, $pos, 0, ''
244 if $opt{'link_onclicks'};
250 if ( $opt{'disableable'} ) {
252 unless ( $cgi->param('showdisabled') ) { #modify searches
254 $opt{'query'}{'hashref'}{'disabled'} = '';
255 $opt{'query'}{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
257 $opt{'count_query'} .=
258 ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
259 "( disabled = '' OR disabled IS NULL )";
261 } elsif ( $opt{'disabled_statuspos'}
262 || $opt{'disabled_statuspos'} eq '0' ) { #add status column
264 my $pos = $opt{'disabled_statuspos'};
266 foreach my $att (qw( align style color size )) {
267 $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
270 splice @{ $opt{'header'} }, $pos, 0, 'Status';
271 splice @{ $opt{'align'} }, $pos, 0, 'c';
272 splice @{ $opt{'style'} }, $pos, 0, 'b';
273 splice @{ $opt{'size'} }, $pos, 0, '';
274 splice @{ $opt{'fields'} }, $pos, 0,
275 sub { shift->disabled ? 'DISABLED' : 'Active'; };
276 splice @{ $opt{'color'} }, $pos, 0,
277 sub { shift->disabled ? 'FF0000' : '00CC00'; };
278 splice @{ $opt{'links'} }, $pos, 0, ''
280 splice @{ $opt{'link_onlicks'} }, $pos, 0, ''
281 if $opt{'link_onlicks'};
284 #add show/hide disabled links
285 my $items = $opt{'name'} || PL($opt{'name_singular'});
286 if ( $cgi->param('showdisabled') ) {
287 $cgi->param('showdisabled', 0);
288 $opt{'html_posttotal'} .=
289 '( <a href="'. $cgi->self_url. qq!">hide disabled $items</a> )!;
290 $cgi->param('showdisabled', 1);
292 $cgi->param('showdisabled', 1);
293 $opt{'html_posttotal'} .=
294 '( <a href="'. $cgi->self_url. qq!">show disabled $items</a> )!;
295 $cgi->param('showdisabled', 0);
300 my $type = $cgi->param('_type') =~ /^(csv|\w*\.xls|select|html(-print)?)$/
304 my($confmax, $maxrecords, $offset );
306 unless ( $type =~ /^(csv|\w*\.xls)$/ ) {
308 unless (exists($opt{count_query}) && length($opt{count_query})) {
309 ( $opt{count_query} = $opt{query} ) =~
310 s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i; #silly vim:/
313 if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
315 ( ( $opt{count_query} =~ /WHERE/i ) ? ' AND ' : ' WHERE ' ).
316 "( disabled = '' OR disabled IS NULL )";
319 unless ( $type eq 'html-print' ) {
321 #setup some pagination things if we're in html mode
323 my $conf = new FS::Conf;
324 $confmax = $conf->config('maxsearchrecordsperpage');
325 if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
328 $maxrecords ||= $confmax;
331 $limit = $maxrecords ? "LIMIT $maxrecords" : '';
333 $offset = $cgi->param('offset') =~ /^(\d+)$/ ? $1 : 0;
334 $limit .= " OFFSET $offset" if $offset;
342 my $header = [ map { ref($_) ? $_->{'label'} : $_ } @{$opt{header}} ];
344 if ( ref($opt{query}) ) {
346 if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
347 #%search = ( 'disabled' => '' );
348 $opt{'query'}->{'hashref'}->{'disabled'} = '';
349 $opt{'query'}->{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
352 #eval "use FS::$opt{'query'};";
354 'select' => $opt{'query'}->{'select'},
355 'table' => $opt{'query'}->{'table'},
356 'addl_from' => (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : ''),
357 'hashref' => $opt{'query'}->{'hashref'} || {},
358 'extra_sql' => $opt{'query'}->{'extra_sql'},
359 'order_by' => $opt{'query'}->{'order_by'}. " $limit",
362 my $sth = dbh->prepare("$opt{'query'} $limit")
363 or die "Error preparing $opt{'query'}: ". dbh->errstr;
365 or die "Error executing $opt{'query'}: ". $sth->errstr;
367 #can get # of rows without fetching them all?
368 $rows = $sth->fetchall_arrayref;
370 $header ||= $sth->{NAME};