ReportWriter/PagingAndSorting.pm

filter: Statementstmt 8.5% 17 / 198 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 71 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 21.0% 4 / 19 Podpod 100% 12 / 12 totaltotal 7.1% 33 / 464 CC 89 SCAR 58.4
CC
89
Cov
4%
new
870.0
get_paging_table
380.0
generate_paging_array
132.0
CRAP 344.8
↑ Summary ReportWriter.pm »
1 package HTML::ReportWriter::PagingAndSorting;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use List::MoreUtils qw(none firstidx);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '1.5.0';
9  
10 =head1 NAME
11  
12 HTML::ReportWriter::PagingAndSorting - Contains logic for paging/sorting function of ReportWriter
13  
14 =head1 SYNOPSIS
15  
16 Example script:
17  
18 #!/usr/bin/perl -w
19  
20 use strict;
21 use HTML::ReportWriter::PagingAndSorting;
22 use CGI;
23 use Template;
24 use DBI;
25  
26 my $template = Template->new( { INCLUDE_PATH => '/templates' } );
27 my $co = new CGI;
28 my $paging = HTML::ReportWriter::PagingAndSorting->new({
29 CGI_OBJECT => $co,
30 DEFAULT_SORT => 'date',
31 SORTABLE_COLUMNS => [
32 {
33 get => 'name',
34 sql => 'people.name',
35 display => 'Full Name',
36 sortable => 0,
37 },
38 {
39 get => 'age',
40 sql => 'people.age',
41 display => 'Age (in years)',
42 sortable => 1,
43 },
44 ],
45 });
46  
47 my $dbh = DBI->connect('DBI:mysql:foo', 'bar', 'baz');
48  
49 my $sql = "SELECT SQL_CALC_FOUND_ROWS id, name, age FROM people";
50  
51 my $sort = $paging->get_mysql_sort();
52 my $limit = $paging->get_mysql_limit();
53  
54 my $sth = $dbh->prepare("$sql $sort $limit");
55 $sth->execute();
56 my ($count) = $dbh->selectrow_array('SELECT FOUND_ROWS() AS num');
57  
58 $paging->num_results($count);
59  
60 while(my $href = $sth->fetchrow_hashref)
61 {
62 push @{$vars{'results'}}, $href;
63 }
64 $vars{'sorting'} = $paging->get_sortable_table_header();
65 $vars{'paging'} = $paging->get_paging_table();
66  
67 print $co->header;
68 $template->process('display.html', \%vars);
69  
70 Example template (display.html in the above example):
71  
72 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
73 <html>
74 <head>
75 <title>Simple Report</title>
76 <link rel="STYLESHEET" type="text/css" href="/style.css" xmlns="">
77 </head>
78 <body>
79 [% rowcounter = 1 %]
80 <center>
81 <table border="0" width="800">
82 <tr><td>
83 <table id="idtable" border="0" cellspacing="0" cellpadding="4" width="100%">
84 [% sorting %]
85 [%- FOREACH x = results %]
86 [%- IF rowcounter mod 2 %]
87 [%- rowclass = "table_odd" %]
88 [%- ELSE %]
89 [%- rowclass = "table_even" %]
90 [%- END %]
91 <tr class="[% rowclass %]">
92 <td>[% x.name %]</td><td>[% x.age %]</td>
93 </tr>
94 [%- rowcounter = rowcounter + 1 %]
95 [% END %]
96 </table>
97 </td></tr>
98 <tr><td>
99 <table border="0" width="100%">
100 <tr>
101 <td width="75%"></td><td width="25%">[% paging %]</td>
102 </tr>
103 </table>
104 </td></tr>
105 </table>
106 </center>
107 <br /><br />
108 </body>
109 </html>
110  
111 The DB is left to the reader's imagination.
112  
113 =head1 DESCRIPTION
114  
115 Generates HTML table headers and footers to handle sorting and paging for tabular reports.
116 Additionally, generates SQL fragments to insert into queries to handle paging and sorting.
117  
118 =head1 METHODS
119  
120 =over
121  
122 =item B<new($options)>
123  
124 Creates and returns a new paging object. new() accepts a single hashref as an argument, options.
125 Options may be one or more of the following:
126  
127 =over
128  
129 =item CGI_OBJECT:
130  
131 A previously-created CGI object. Saves the overhead of loading a new one.
132  
133 =item RESULTS_PER_PAGE:
134  
135 The number of rows to be displayed per page. Setting this number to 0 will disable paging, but
136 only if you call the get_limit() method. If you directly call get_DRIVER_limit, it is not
137 guaranteed to work. default: 25
138  
139 =item PAGES_IN_LIST:
140  
141 The number of pages that will appear in the paging array. default: 5
142 Note: this number must be odd. If it is even, it will be decremented by 1.
143  
144 =item PAGE_VARIABLE:
145  
146 The GET parameter that defines which page of the results we are viewing. default: page
147  
148 =item NUM_RESULTS:
149  
150 This should not be used when instantiating the object, because it means that in doing so
151 you have bypassed the get_mysql_limit function, which is against the design of this object.
152  
153 =item SORT_VARIABLE:
154  
155 The GET parameter that defines which column is being used for sorting and the direction of the
156 sort. default: sort
157  
158 =item DEFAULT_SORT:
159  
160 Which column should be sorted by when the page is drawn for the first time. User the version
161 of the name that is provided as the C<get> param in the C<SORTABLE_COLUMNS> definition. Sort
162 direction is ascending by default. No column is sorted on by default. Examples:
163  
164 DEFAULT_SORT => 'date',
165 DEFAULT_SORT => 'date,ASC',
166 DEFAULT_SORT => 'date,DESC',
167  
168 =item CURRENT_PAGE:
169  
170 Which page are we currently viewing? Should never need to be overridden.
171 default: $cgi->param(PAGE_VARIABLE)
172  
173 =item CURRENT_SORT_COL:
174  
175 Which column are we currently sorting by? Should never need to be overridden.
176  
177 =item CURRENT_SORT_DIR:
178  
179 Which direction are we currently sorting? Should never need to be overridden.
180  
181 =item LANGUAGE_TOKENS:
182  
183 This is a hashref used to make this module multilingual. Currently, there is only one language token:
184 display_token. It is, by default, 'Displaying Results $1 to $2 of $3' where $1, $2, and $3 are replaced
185 with the appropriate values.
186  
187 =back
188  
189 The following options control formatting, and should be self-explanatory. Their defaults
190 are listed next to the item.
191  
192 =over
193  
194 =item PREV_HTML &lt;
195  
196 =item NEXT_HTML &gt;
197  
198 =item FIRST_HTML &laquo;
199  
200 =item LAST_HTML &raquo;
201  
202 =item ASC_HTML &uarr;
203  
204 =item DESC_HTML &darr;
205  
206 =back
207  
208 Finally, it accepts a list of sortable columns. A column consists of the following properties:
209  
210 =over
211  
212 =item get:
213  
214 The name of the column on the get string. This is used in conjunction with SORT_VARIABLE as in:
215 C<< http://example.com/cgi-bin/reports/foo.cgi?SORT_VARIABLE=SORTABLE_COLUMNS->[0]->{'get'} >>
216  
217 =item sql:
218  
219 The name of the column in sql. Include any necessary table aliases in this definition.
220 Example: C<< sql => 'tablename.columnname' >>
221  
222 =item display:
223  
224 The name of the column in the display. Used by get_sortable_html_table_header
225  
226 =item sortable:
227  
228 True/False (1/0). Defines the behaviour of the column. Does it allow sorting?
229  
230 =item order:
231  
232 Optional property that defines the sql used in the order clause. If not set, the 'sql' property is used.
233 See the ReportWriter documentation for more information on this property.
234  
235 =back
236  
237 Each sortable column definition is a hashref, and SORTABLE_COLUMNS itself is an arrayref containing
238 one or more of these hashrefs. Example:
239  
240 SORTABLE_COLUMNS => [
241 {
242 'get' => 'name',
243 'sql' => 'person.name',
244 'display' => 'Name',
245 'sortable' => 0,
246 },
247 {
248 'get' => 'age',
249 'sql' => 'person.age',
250 'display' => 'Age (in years)',
251 'sortable' => 1,
252 },
253 ]
254  
255 =cut
256  
257 sub new
258 0 ▶{
Subroutine new: not called CC=29 SCAR=67.7
Pod documented
259 0 my ($pkg, $args) = @_;
260  
261 0 ▶ if(!defined($args->{'CGI_OBJECT'}) || !UNIVERSAL::isa($args->{'CGI_OBJECT'}, "CGI"))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"CGI_OBJECT"} and UNIVERSAL::isa($args->{"CGI_OBJECT"}, "CGI")) 0 0 0
Condition0%0/3
defined $args->{"CGI_OBJECT"} and UNIVERSAL::isa($args->{"CGI_OBJECT"}, "CGI")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"CGI_OBJECT"} and UNIVERSAL::isa($args->{"CGI_OBJECT"}, "CGI")
defined $args->{"CGI_OBJECT"} UNIVERSAL::isa($args->{"CGI_OBJECT"}, "CGI")
Truth table0%0/3
A and B
A= defined $args->{"CGI_OBJECT"}B= UNIVERSAL::isa($args->{"CGI_OBJECT"}, "CGI")
ABresult
0X0
100
111
262 {
263 0 $args->{'CGI_OBJECT'} = new CGI;
264 0 warn "Creating new CGI object";
265 }
266  
267 # paging setup
268 0 ▶ defined $args->{'RESULTS_PER_PAGE'} or $args->{'RESULTS_PER_PAGE'} = 25;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"RESULTS_PER_PAGE"} or $args->{"RESULTS_PER_PAGE"} = 25 0 0 0
269 0 ▶ defined $args->{'PAGES_IN_LIST'} or $args->{'PAGES_IN_LIST'} = 5;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"PAGES_IN_LIST"} or $args->{"PAGES_IN_LIST"} = 5 0 0 0
270 0 ▶ defined $args->{'PAGE_VARIABLE'} or $args->{'PAGE_VARIABLE'} = 'page';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"PAGE_VARIABLE"} or $args->{"PAGE_VARIABLE"} = "page" 0 0 0
271 0 ▶ defined $args->{'NUM_RESULTS'} or $args->{'NUM_RESULTS'} = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"NUM_RESULTS"} or $args->{"NUM_RESULTS"} = 0 0 0 0
272  
273 # sorting setup
274 0 ▶ defined $args->{'SORT_VARIABLE'} or $args->{'SORT_VARIABLE'} = 'sort';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"SORT_VARIABLE"} or $args->{"SORT_VARIABLE"} = "sort" 0 0 0
275 0 ▶ defined $args->{'DEFAULT_SORT'} or $args->{'DEFAULT_SORT'} = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"DEFAULT_SORT"} or $args->{"DEFAULT_SORT"} = "" 0 0 0
276  
277 # current page handling
278 0 ▶ my $page = $args->{'CGI_OBJECT'}->param($args->{'PAGE_VARIABLE'}) || 1;
Condition0%0/2
$args->{'CGI_OBJECT'}->param($args->{'PAGE_VARIABLE'}) || 1
l!l
00
MC/DC0%0/1
$args->{'CGI_OBJECT'}->param($args->{'PAGE_VARIABLE'}) || 1
$args->{'CGI_OBJECT'}->param($args->{'PAGE_VARIABLE'})
Truth table0%0/2
A
A= $args->{'CGI_OBJECT'}->param($args->{'PAGE_VARIABLE'})
Aresult
11
00
279 0 ▶ defined $args->{'CURRENT_PAGE'} or $args->{'CURRENT_PAGE'} = $page;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"CURRENT_PAGE"} or $args->{"CURRENT_PAGE"} = $page 0 0 0
280  
281 # current sort handling
282 0 ▶ my $sort = $args->{'CGI_OBJECT'}->param($args->{'SORT_VARIABLE'}) || $args->{'DEFAULT_SORT'};
Condition0%0/3
$args->{'CGI_OBJECT'}->param($args->{'SORT_VARIABLE'}) || $args->{'DEFAULT_SORT'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'CGI_OBJECT'}->param($args->{'SORT_VARIABLE'}) || $args->{'DEFAULT_SORT'}
$args->{'CGI_OBJECT'}->param($args->{'SORT_VARIABLE'}) $args->{'DEFAULT_SORT'}
Truth table0%0/3
A || B
A= $args->{'CGI_OBJECT'}->param($args->{'SORT_VARIABLE'})B= $args->{'DEFAULT_SORT'}
ABresult
1X1
011
000
283 0 my ($sort_col, $sort_dir);
284 0 ▶ if($sort)
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) 0 0 0
285 {
286 0 ($sort_col, $sort_dir) = split /,/, $sort;
287 }
288  
289 0 ▶ if(!$sort_dir && $sort_col)
Branch: 1 branch
BranchTrueFalseTotal
if (not $sort_dir and $sort_col) 0 0 0
Condition0%0/3
not $sort_dir and $sort_col
!ll&&!rl&&r
000
MC/DC0%0/2
not $sort_dir and $sort_col
not $sort_dir $sort_col
Truth table0%0/3
not(A) and B
A= not $sort_dirB= $sort_col
ABresult
0X0
100
111
290 {
291 # defaults to ascending order
292 0 $sort_dir = 'ASC';
293 }
294 0 ▶ defined $args->{'CURRENT_SORT_COL'} or $args->{'CURRENT_SORT_COL'} = $sort_col;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"CURRENT_SORT_COL"} or $args->{"CURRENT_SORT_COL"} = $sort_col 0 0 0
295 0 ▶ defined $args->{'CURRENT_SORT_DIR'} or $args->{'CURRENT_SORT_DIR'} = $sort_dir;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"CURRENT_SORT_DIR"} or $args->{"CURRENT_SORT_DIR"} = $sort_dir 0 0 0
296  
297 # options to control HTML output - Paging
298 0 ▶ defined $args->{'PREV_HTML'} or $args->{'PREV_HTML'} = '&lt;';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"PREV_HTML"} or $args->{"PREV_HTML"} = "&lt;" 0 0 0
299 0 ▶ defined $args->{'NEXT_HTML'} or $args->{'NEXT_HTML'} = '&gt;';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"NEXT_HTML"} or $args->{"NEXT_HTML"} = "&gt;" 0 0 0
300 0 ▶ defined $args->{'FIRST_HTML'} or $args->{'FIRST_HTML'} = '&laquo;';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"FIRST_HTML"} or $args->{"FIRST_HTML"} = "&laquo;" 0 0 0
301 0 ▶ defined $args->{'LAST_HTML'} or $args->{'LAST_HTML'} = '&raquo;';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"LAST_HTML"} or $args->{"LAST_HTML"} = "&raquo;" 0 0 0
302  
303 # options to control HTML output - Sorting
304 0 ▶ defined $args->{'ASC_HTML'} or $args->{'ASC_HTML'} = ' &uarr;';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"ASC_HTML"} or $args->{"ASC_HTML"} = " &uarr;" 0 0 0
305 0 ▶ defined $args->{'DESC_HTML'} or $args->{'DESC_HTML'} = ' &darr;';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"DESC_HTML"} or $args->{"DESC_HTML"} = " &darr;" 0 0 0
306  
307 # round down PAGES_IN_LIST if it isn't odd
308 0 ▶ if(!$args->{'PAGES_IN_LIST'} % 2)
Branch: 1 branch
BranchTrueFalseTotal
if (!$args->{"PAGES_IN_LIST"} % 2) 0 0 0
309 {
310 0 $args->{'PAGES_IN_LIST'} -= 1;
311 0 warn "PAGES_IN_LIST must be odd. See the documentation (if it exists) for the reason why.";
312 }
313  
314 #Provide a way to replace the english text
315 0 ▶ defined $args->{'LANGUAGE_TOKENS'} or $args->{'LANGUAGE_TOKENS'} = { display_token => 'Displaying Results $1 to $2 of $3' };
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"LANGUAGE_TOKENS"} or $args->{"LANGUAGE_TOKENS"} = {"display_token", "Displaying Results \$1 to \$2 of \$3"} 0 0 0
316  
317 # don't die here because they may not want to use the sorting, but may have a GET/POST var whose name is the
318 # same as $args->{'SORT_VARIABLE'}. We'll die if they call a sort function.
319 0 ▶ if(($sort_dir || $sort_col) && !$args->{'SORTABLE_COLUMNS'} || ref($args->{'SORTABLE_COLUMNS'}) ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_dir || $sort_col and not $args->{"SORTABLE_COLUMNS"} or ref $args->{"SORTABLE_COLUMNS"} ne "ARRAY") 0 0 0
Condition0%0/9
$sort_dir || $sort_col and not $args->{"SORTABLE_COLUMNS"} or ref $args->{"SORTABLE_COLUMNS"} ne "ARRAY"
l!l&&r!l&&!r
000
$sort_dir || $sort_col and not $args->{"SORTABLE_COLUMNS"}
!ll&&!rl&&r
000
$sort_dir or $sort_col
l!l&&r!l&&!r
000
MC/DC0%0/4
$sort_dir || $sort_col and not $args->{"SORTABLE_COLUMNS"} or ref $args->{"SORTABLE_COLUMNS"} ne "ARRAY"
$sort_dir $sort_col not $args->{"SORTABLE_COLUMNS"} ref $args->{"SORTABLE_COLUMNS"} ne "ARRAY"
Truth table0%0/8
A or B and not(C) or D
A= $sort_dirB= $sort_colC= not $args->{"SORTABLE_COLUMNS"}D= ref $args->{"SORTABLE_COLUMNS"} ne "ARRAY"
ABCDresult
1X1X1
011X1
00X11
1X011
01011
00X00
1X000
01000
320 {
321 0 warn "SORTABLE_COLUMNS Is either not defined or not an arrayref, sorting disabled";
322 }
323  
324 0 my $self = bless $args, $pkg;
325  
326 0 return $self;
327 }
328  
329 =item B<num_results($int)>
330  
331 Accepts the number of results that will be generated for the query being used. Sets the number of
332 rows internally to the number supplied, and returns that number as confirmation of the setting.
333  
334 If the CURRENT_PAGE has been initialized to something greater than the total number of available pages
335 (where $total_pages = ceil(NUM_RESULTS / RESULTS_PER_PAGE)), then the function will reset CURRENT_PAGE to
336 the last available page and return FALSE. This should signal the calling script to regenerate the result set
337 if the result set has already been generated (as would be the case if using the SQL_CALC_FOUND_ROWS feature
338 of MySQL 4.0+).
339  
340 Without a function like C<FOUND_ROWS()>, this number could be generated by an additional SQL query that
341 counts the total number of rows, using the same WHERE clause:
342  
343 my $count = $dbh->selectrow_array("SELECT count(*) FROM articles WHERE ...");
344 $paging->num_results($count);
345  
346 =cut
347  
348 sub num_results
349 0 ▶{
Subroutine num_results: not called CC=4 SCAR=30.0
Pod documented
350 0 my ($self, $num) = @_;
351  
352 0 $self->{'NUM_RESULTS'} = $num;
353 0 my $to_return = $num;
354  
355 0 ▶ if($self->{'RESULTS_PER_PAGE'} != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'RESULTS_PER_PAGE'} != 0) { } 0 0 0
356 {
357 0 my $total_pages = ceil($self->{'NUM_RESULTS'} / $self->{'RESULTS_PER_PAGE'});
358  
359 # if somehow we paged past the end of the results, get us back on track
360 0 ▶ if($self->{'CURRENT_PAGE'} > $total_pages)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CURRENT_PAGE"} > $total_pages) 0 0 0
361 {
362 0 $self->{'CURRENT_PAGE'} = $total_pages;
363 0 $to_return = 0;
364 }
365 }
366 else
367 {
368 # if paging is disabled (RPP == 0) then we can never go past the
369 # first page
370 0 ▶ if($self->{'CURRENT_PAGE'} > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CURRENT_PAGE"} > 1) 0 0 0
371 {
372 0 $self->{'CURRENT_PAGE'} = 1;
373 0 $to_return = 0;
374 }
375 }
376  
377 0 return $to_return;
378 }
379  
380 =item B<generate_paging_array()>
381  
382 @pages = $pager->generate_paging_array();
383  
384 Determines what page the viewer is currently on, and generates an array representing which
385 pages are previous and next, first and last. Returns that array.
386  
387 =cut
388  
389 sub generate_paging_array
390 0 ▶{
Subroutine generate_paging_array: not called CC=11 SCAR=48.8
Pod documented
391 0 my $self = shift;
392  
393 # not the cleanest way of doing this -- short-circuit out of this function
394 # if paging is disabled with RPP == 0
395 0 ▶ if($self->{'RESULTS_PER_PAGE'} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"RESULTS_PER_PAGE"} == 0) 0 0 0
396 {
397 0 return;
398 }
399  
400 0 my $pages_on_either_side = ceil(($self->{'PAGES_IN_LIST'} - 1) / 2);
401 0 my $total_pages = ceil($self->{'NUM_RESULTS'} / $self->{'RESULTS_PER_PAGE'});
402  
403 # if somehow we paged past the end of the results, get us back on track
404 0 ▶ if($self->{'CURRENT_PAGE'} > $total_pages)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CURRENT_PAGE"} > $total_pages) 0 0 0
405 {
406 0 $self->{'CURRENT_PAGE'} = $total_pages;
407 }
408  
409 0 my @pages = ();
410  
411 # at the end of the results
412 0 ▶ if($self->{'CURRENT_PAGE'} == $total_pages)
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'CURRENT_PAGE'} == $total_pages) { } 0 0 0
elsif ($self->{'CURRENT_PAGE'} - $pages_on_either_side >= 1 and $self->{'CURRENT_PAGE'} + $pages_on_either_side <= $total_pages) { } 0 0 0
elsif ($self->{'CURRENT_PAGE'} - $self->{'PAGES_IN_LIST'} < 1) { } 0 0 0
elsif ($self->{'CURRENT_PAGE'} + $self->{'PAGES_IN_LIST'} > $total_pages) { } 0 0 0
413 {
414 0 my $min = $self->{'CURRENT_PAGE'} - $self->{'PAGES_IN_LIST'};
415 0 ▶ $min = 1 if $min < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min < 1 0 0 0
416  
417 0 push @pages, $min..$self->{'CURRENT_PAGE'};
418 }
419 # just right
420 ▶ elsif(($self->{'CURRENT_PAGE'} - $pages_on_either_side) >= 1 &&
Condition0%0/3
$self->{"CURRENT_PAGE"} - $pages_on_either_side >= 1 and $self->{"CURRENT_PAGE"} + $pages_on_either_side <= $total_pages
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"CURRENT_PAGE"} - $pages_on_either_side >= 1 and $self->{"CURRENT_PAGE"} + $pages_on_either_side <= $total_pages
$self->{"CURRENT_PAGE"} - $pages_on_either_side >= 1 $self->{"CURRENT_PAGE"} + $pages_on_either_side <= $total_pages
Truth table0%0/3
A and B
A= $self->{"CURRENT_PAGE"} - $pages_on_either_side >= 1B= $self->{"CURRENT_PAGE"} + $pages_on_either_side <= $total_pages
ABresult
0X0
100
111
421 ($self->{'CURRENT_PAGE'} + $pages_on_either_side) <= $total_pages)
422 {
423 0 my $min = $self->{'CURRENT_PAGE'} - $pages_on_either_side;
424 0 my $max = $self->{'CURRENT_PAGE'} + $pages_on_either_side;
425  
426 0 push @pages, $min..$max;
427 }
428 # too close to the beginning
429 elsif($self->{'CURRENT_PAGE'} - $self->{'PAGES_IN_LIST'} < 1)
430 {
431 0 my $min = 1;
432 0 ▶ if($self->{'PAGES_IN_LIST'} > $total_pages)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PAGES_IN_LIST'} > $total_pages) { } 0 0 0
433 {
434 0 push @pages, $min..$total_pages;
435 }
436 else
437 {
438 0 push @pages, $min..$self->{'PAGES_IN_LIST'};
439 }
440 }
441 # too close to the end
442 elsif($self->{'CURRENT_PAGE'} + $self->{'PAGES_IN_LIST'} > $total_pages)
443 {
444 0 my $min = $self->{'CURRENT_PAGE'} - ($self->{'PAGES_IN_LIST'} - ($total_pages - $self->{'CURRENT_PAGE'}));
445 0 ▶ $min = 1 if $min < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $min < 1 0 0 0
446  
447 0 push @pages, $min..$total_pages;
448 }
449 else
450 {
451 0 die "This code should never execute";
452 }
453  
454 0 return (@pages);
455 }
456  
457 =item B<get_page_link($page_number)>
458  
459 $url = $pager->get_page_link($page_number);
460  
461 Saves the existing sort and page settings, and then uses some CGI module magic to generate
462 a URL saving all parameters that were passed in except the page number, which is set to the
463 requested page. Used to generate paging html.
464  
465 =cut
466  
467 sub get_page_link
468 0 ▶{
Subroutine get_page_link: not called CC=2 SCAR=17.9
Pod documented
469 0 my ($self, $page) = @_;
470  
471 # save the old page number and sort (this is necessary since we have a shared CGI object)
472 0 my $oldpage = $self->{'CGI_OBJECT'}->param($self->{'PAGE_VARIABLE'});
473  
474 # generate a url with the new page number
475 0 $self->{'CGI_OBJECT'}->param($self->{'PAGE_VARIABLE'}, $page);
476 0 my $url = $self->{'CGI_OBJECT'}->url( -absolute => 1, -query => 1, -path_info => 1 );
477  
478 # restore the old page number
479 0 ▶ if($oldpage)
Branch: 1 branch
BranchTrueFalseTotal
if ($oldpage) { } 0 0 0
480 {
481 0 $self->{'CGI_OBJECT'}->param($self->{'PAGE_VARIABLE'}, $oldpage);
482 }
483 else
484 {
485 0 $self->{'CGI_OBJECT'}->delete($self->{'PAGE_VARIABLE'});
486 }
487  
488 0 return $url;
489 }
490  
491 =item B<get_paging_table()>
492  
493 Gets the paging array, generates links for each part of that array, and then generates HTML for
494 the paging block based on the display settings that were configured during instantiation.
495  
496 =cut
497  
498 sub get_paging_table
499 0 ▶{
Subroutine get_paging_table: not called CC=19 SCAR=59.4
Pod documented
500 0 my ($self) = @_;
501  
502 # if we have no results, or paging is disabled (RPP == 0), exit
503 0 ▶ return '' if $self->{'NUM_RESULTS'} == 0 || $self->{'RESULTS_PER_PAGE'} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"NUM_RESULTS"} == 0 or $self->{"RESULTS_PER_PAGE"} == 0 0 0 0
Condition0%0/3
$self->{"NUM_RESULTS"} == 0 or $self->{"RESULTS_PER_PAGE"} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"NUM_RESULTS"} == 0 or $self->{"RESULTS_PER_PAGE"} == 0
$self->{"NUM_RESULTS"} == 0 $self->{"RESULTS_PER_PAGE"} == 0
Truth table0%0/3
A or B
A= $self->{"NUM_RESULTS"} == 0B= $self->{"RESULTS_PER_PAGE"} == 0
ABresult
1X1
011
000
504  
505 0 my @paging_array = $self->generate_paging_array();
506  
507 0 my $total_pages = ceil($self->{'NUM_RESULTS'} / $self->{'RESULTS_PER_PAGE'});
508 0 my $string = '';
509  
510 0 ▶ my $first = ($self->{'CURRENT_PAGE'} == 1 ? 1 : (($self->{'CURRENT_PAGE'} - 1) * $self->{'RESULTS_PER_PAGE'}));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'CURRENT_PAGE'} == 1 ? : 0 0 0
511 0 ▶ my $last = ($self->{'CURRENT_PAGE'} == $total_pages ? $self->{'NUM_RESULTS'} : ($self->{'CURRENT_PAGE'} * $self->{'RESULTS_PER_PAGE'}));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'CURRENT_PAGE'} == $total_pages ? : 0 0 0
512 0 my $total = $self->{'NUM_RESULTS'};
513  
514 0 my $language = $self->{'LANGUAGE_TOKENS'}->{'display_token'};
515 0 $language =~ s/\$1/$first/g;
516 0 $language =~ s/\$2/$last/g;
517 0 $language =~ s/\$3/$total/g;
518
519 # paging header
520 0 $string = '<table class="paging-table"><tr>';
521 0 $string .= '<td nowrap class="paging-td" style="font-size: 7pt;">'. $language . '</td>';
522  
523 # process the elements in order
524 0 foreach ('FIRST','PREV',@paging_array,'NEXT','LAST')
525 {
526 0 $string .= '<td class="paging-td">';
527  
528 0 ▶ if(($_ eq 'FIRST' || $_ eq 'PREV') && $self->{'CURRENT_PAGE'} != 1)
Branch: 4 branches
BranchTrueFalseTotal
if ($_ eq 'FIRST' || $_ eq 'PREV' and $self->{'CURRENT_PAGE'} != 1) { } 0 0 0
elsif ($_ eq 'NEXT' || $_ eq 'LAST' and $self->{'CURRENT_PAGE'} != $total_pages) { } 0 0 0
elsif ($_ eq 'FIRST' or $_ eq 'PREV' or $_ eq 'NEXT' or $_ eq 'LAST') { } 0 0 0
elsif ($_ != $self->{'CURRENT_PAGE'}) { } 0 0 0
Condition0%0/6
$_ eq "FIRST" || $_ eq "PREV" and $self->{"CURRENT_PAGE"} != 1
!ll&&!rl&&r
000
$_ eq "FIRST" or $_ eq "PREV"
l!l&&r!l&&!r
000
MC/DC0%0/3
$_ eq "FIRST" || $_ eq "PREV" and $self->{"CURRENT_PAGE"} != 1
$_ eq "FIRST" $_ eq "PREV" $self->{"CURRENT_PAGE"} != 1
Truth table0%0/5
A or B and C
A= $_ eq "FIRST"B= $_ eq "PREV"C= $self->{"CURRENT_PAGE"} != 1
ABCresult
00X0
1X00
0100
1X11
0111
529 {
530 0 ▶ my $url = $self->get_page_link(($_ eq 'FIRST' ? 1 : $self->{'CURRENT_PAGE'} - 1));
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'FIRST' ? : 0 0 0
531 0 $string .= qq(<a class="paging-a" href="$url">) . $self->{"${_}_HTML"} . q(</a>);
532 }
533 ▶ elsif(($_ eq 'NEXT' || $_ eq 'LAST') && $self->{'CURRENT_PAGE'} != $total_pages)
Condition0%0/6
$_ eq "NEXT" || $_ eq "LAST" and $self->{"CURRENT_PAGE"} != $total_pages
!ll&&!rl&&r
000
$_ eq "NEXT" or $_ eq "LAST"
l!l&&r!l&&!r
000
MC/DC0%0/3
$_ eq "NEXT" || $_ eq "LAST" and $self->{"CURRENT_PAGE"} != $total_pages
$_ eq "NEXT" $_ eq "LAST" $self->{"CURRENT_PAGE"} != $total_pages
Truth table0%0/5
A or B and C
A= $_ eq "NEXT"B= $_ eq "LAST"C= $self->{"CURRENT_PAGE"} != $total_pages
ABCresult
00X0
1X00
0100
1X11
0111
534 {
535 0 ▶ my $url = $self->get_page_link(($_ eq 'LAST' ? $total_pages : $self->{'CURRENT_PAGE'} + 1));
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'LAST' ? : 0 0 0
536 0 $string .= qq(<a class="paging-a" href="$url">) . $self->{"${_}_HTML"} . q(</a>);
537 }
538 ▶ elsif($_ eq 'FIRST' || $_ eq 'PREV' || $_ eq 'NEXT' || $_ eq 'LAST')
Condition0%0/9
$_ eq "FIRST" or $_ eq "PREV" or $_ eq "NEXT" or $_ eq "LAST"
l!l&&r!l&&!r
000
$_ eq "FIRST" or $_ eq "PREV" or $_ eq "NEXT"
l!l&&r!l&&!r
000
$_ eq "FIRST" or $_ eq "PREV"
l!l&&r!l&&!r
000
MC/DC0%0/4
$_ eq "FIRST" or $_ eq "PREV" or $_ eq "NEXT" or $_ eq "LAST"
$_ eq "FIRST" $_ eq "PREV" $_ eq "NEXT" $_ eq "LAST"
Truth table0%0/5
A or B or C or D
A= $_ eq "FIRST"B= $_ eq "PREV"C= $_ eq "NEXT"D= $_ eq "LAST"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
539 {
540 0 $string .= $self->{"${_}_HTML"};
541 }
542 elsif($_ != $self->{'CURRENT_PAGE'})
543 {
544 0 my $url = $self->get_page_link($_);
545 0 $string .= qq(<a class="paging-a" href="$url">$_</a>);
546 }
547 else
548 {
549 0 $string .= $_;
550 }
551  
552 0 $string .= '</td>';
553 }
554  
555 # paging footer
556 0 $string .= '</tr></table>';
557  
558 0 return $string;
559 }
560  
561 =item B<get_limit()>
562  
563 $sql_limit_snippet = $self->get_limit();
564  
565 Given the driver name, returns a limit clause compatible with that driver. If the driver is not
566 supported, generates a warning and returns false.
567  
568 =cut
569  
570 sub get_limit
571 0 ▶{
Subroutine get_limit: not called CC=3 SCAR=24.8
Pod documented
572 0 my $self = shift;
573 0 my ($driver) = @_;
574 0 my $method = "get_${driver}_limit";
575  
576 0 my $to_return = '';
577  
578 0 ▶ if($self->{'RESULTS_PER_PAGE'} != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"RESULTS_PER_PAGE"} != 0) 0 0 0
579 {
580 eval
581 0 {
582 0 $to_return = $self->$method();
583 };
584  
585 0 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
586 {
587 0 warn "error: driver $driver not supported in get_limit()";
588 }
589 }
590  
591 0 return $to_return;
592 }
593  
594 =item B<get_sort()>
595  
596 $sql_limit_snippet = $self->get_sort();
597  
598 Given the driver name, returns a sort clause compatible with that driver. If the driver is not
599 supported, generates a warning and returns false.
600  
601 =cut
602  
603 sub get_sort
604 0 ▶{
Subroutine get_sort: not called CC=2 SCAR=17.9
Pod documented
605 0 my $self = shift;
606 0 my ($driver) = @_;
607 0 my $method = "get_${driver}_sort";
608  
609 0 my $to_return = '';
610  
611 eval
612 0 {
613 0 $to_return = $self->$method();
614 };
615  
616 0 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
617 {
618 0 warn "error: driver $driver not supported in get_sort()";
619 }
620  
621 0 return $to_return;
622 }
623  
624 =item B<get_mysql_limit()>
625  
626 $sql_limit_snippet = $self->get_mysql_limit();
627  
628 Generates a MySQL-compliant LIMIT clause to be appended to SQL queries in order to get the
629 appropriate rows for a paged report. Example above, in the SYNOPSIS.
630  
631 =cut
632  
633 sub get_mysql_limit
634 0 ▶{
Subroutine get_mysql_limit: not called CC=1 SCAR=6.9
Pod documented
635 0 my $self = shift;
636  
637 0 my $start = ($self->{'CURRENT_PAGE'} - 1) * $self->{'RESULTS_PER_PAGE'};
638 0 my $count = $self->{'RESULTS_PER_PAGE'};
639  
640 0 return "LIMIT $start, $count";
641 }
642  
643 =item B<get_mysql_sort()>
644  
645 $sql_order_by_snippet = $pager->get_mysql_sort();
646  
647 Returns a MySQL-compliant ORDER BY clause based on the current sorting settings, to be appended
648 to the SQL query used to generate the report that this module is being used for. Example above
649 in the SYNOPSIS.
650  
651 =cut
652  
653 sub get_mysql_sort
654 0 ▶{
Subroutine get_mysql_sort: not called CC=11 SCAR=48.8
Pod documented
655 0 my ($self) = @_;
656  
657 0 ▶ if(!$self->{'SORTABLE_COLUMNS'} || ref($self->{'SORTABLE_COLUMNS'}) ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"SORTABLE_COLUMNS"} or ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY") 0 0 0
Condition0%0/3
not $self->{"SORTABLE_COLUMNS"} or ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"SORTABLE_COLUMNS"} or ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
not $self->{"SORTABLE_COLUMNS"} ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not $self->{"SORTABLE_COLUMNS"}B= ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
ABresult
1X1
011
000
658 {
659 0 die "SORTABLE_COLUMNS is either not defined or not an arrayref, sorting disabled";
660 }
661  
662 0 my $dir = uc($self->{'CURRENT_SORT_DIR'});
663 0 my $sort = $self->{'CURRENT_SORT_COL'};
664 0 my $to_return = '';
665  
666 0 ▶ if($dir && $sort && none { $_->{'get'} eq $sort } @{$self->{'SORTABLE_COLUMNS'}})
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($dir and $sort and &none(sub { $_->{"get"} eq $sort; } , @{$$self{"SORTABLE_COLUMNS"};})) 0 0 0
Condition0%0/6
$dir and $sort and &none(sub { $_->{"get"} eq $sort; } , @{$$self{"SORTABLE_COLUMNS"};})
!ll&&!rl&&r
000
$dir and $sort
!ll&&!rl&&r
000
MC/DC0%0/3
$dir and $sort and &none(sub { $_->{"get"} eq $sort; } , @{$$self{"SORTABLE_COLUMNS"};})
$dir $sort &none(sub { $_->{"get"} eq $sort; } , @{$$self{"SORTABLE_COLUMNS"};})
Truth table0%0/4
A and B and C
A= $dirB= $sortC= &none(sub { $_->{"get"} eq $sort; } , @{$$self{"SORTABLE_COLUMNS"};})
ABCresult
0XX0
10X0
1100
1111
667 {
668 0 die "requested sort '$sort' is impossible, not defined in SORTABLE_COLUMNS";
669 }
670  
671 0 ▶ if($dir && $sort)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir and $sort) 0 0 0
Condition0%0/3
$dir and $sort
!ll&&!rl&&r
000
MC/DC0%0/2
$dir and $sort
$dir $sort
Truth table0%0/3
A and B
A= $dirB= $sort
ABresult
0X0
100
111
672 {
673 0 ▶ my $index = firstidx { $_->{'get'} eq $sort } @{$self->{'SORTABLE_COLUMNS'}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
674 0 ▶ if($self->{'SORTABLE_COLUMNS'}->[$index]->{'get'} ne $sort)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SORTABLE_COLUMNS"}[$index]{"get"} ne $sort) 0 0 0
675 {
676 0 die "This should not happen";
677 }
678 0 ▶ $sort = $self->{'SORTABLE_COLUMNS'}->[$index]->{'order'} || $self->{'SORTABLE_COLUMNS'}->[$index]->{'sql'};
Condition0%0/3
$self->{'SORTABLE_COLUMNS'}[$index]{'order'} || $self->{'SORTABLE_COLUMNS'}[$index]{'sql'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'SORTABLE_COLUMNS'}[$index]{'order'} || $self->{'SORTABLE_COLUMNS'}[$index]{'sql'}
$self->{'SORTABLE_COLUMNS'}[$index]{'order'} $self->{'SORTABLE_COLUMNS'}[$index]{'sql'}
Truth table0%0/3
A || B
A= $self->{'SORTABLE_COLUMNS'}[$index]{'order'}B= $self->{'SORTABLE_COLUMNS'}[$index]{'sql'}
ABresult
1X1
011
000
679 0 ▶ if($sort =~ / AS /i)
Branch: 1 branch
BranchTrueFalseTotal
if ($sort =~ / AS /i) 0 0 0
680 {
681 0 $sort =~ s/^.+ AS (.+)$/$1/i;
682 }
683 0 $to_return = "ORDER BY $sort $dir";
684 }
685  
686 0 return $to_return;
687 }
688  
689 =item B<get_Pg_limit()>
690  
691 $sql_limit_snippet = $self->get_Pg_limit();
692  
693 Generates a PostgreSQL-compliant LIMIT clause to be appended to SQL queries in order to get the
694 appropriate rows for a paged report. Example above, in the SYNOPSIS.
695  
696 =cut
697  
698 sub get_Pg_limit
699 0 ▶{
Subroutine get_Pg_limit: not called CC=1 SCAR=6.9
Pod documented
700 0 my $self = shift;
701  
702 0 my $start = ($self->{'CURRENT_PAGE'} - 1) * $self->{'RESULTS_PER_PAGE'};
703 0 my $count = $self->{'RESULTS_PER_PAGE'};
704  
705 0 return "LIMIT $count OFFSET $start";
706 }
707  
708 =item B<get_Pg_sort()>
709  
710 $sql_order_by_snippet = $pager->get_Pg_sort();
711  
712 Returns a PostgreSQL-compliant ORDER BY clause based on the current sorting settings, to be appended
713 to the SQL query used to generate the report that this module is being used for. Example above
714 in the SYNOPSIS.
715  
716 =cut
717  
718 # The MySQL syntax is compatible with PostgreSQL
719 1 *get_Pg_sort = \&get_mysql_sort;
720  
721 =item B<get_SQLite_limit()>
722  
723 $sql_limit_snippet = $self->get_SQLite_limit();
724  
725 Generates a SQLite-compliant LIMIT clause to be appended to SQL queries in order to get the
726 appropriate rows for a paged report. Example above, in the SYNOPSIS.
727  
728 =cut
729  
730 # The PostgreSQL syntax is compatible with SQLite
731 1 *get_SQLite_limit = \&get_Pg_limit;
732  
733 =item B<get_SQLite_sort()>
734  
735 $sql_order_by_snippet = $pager->get_SQLite_sort();
736  
737 Returns a SQLite-compliant ORDER BY clause based on the current sorting settings, to be appended
738 to the SQL query used to generate the report that this module is being used for. Example above
739 in the SYNOPSIS.
740  
741 =cut
742  
743 # The MySQL syntax is compatible with SQLite
744 1 *get_SQLite_sort = \&get_mysql_sort;
745  
746 =item B<get_sort_link($column)>
747  
748 $url = $pager->get_sort_link($column);
749  
750 Same as get_page_link() above, except allows you to specify the new sort instead of the new page.
751 When specifying the sort column, specifying the same column that is currently selected results
752 in the link being generated for the opposite of its current direction. Otherwise, each column
753 defaults to sort ascending.
754  
755 Additionally, when changing the sort, page is not preserved, the logic being that you likely
756 want to start back at the beginning of the report to view the first I<n> records instead of being
757 stuck in the middle of the record set.
758  
759 =cut
760  
761 sub get_sort_link
762 0 ▶{
Subroutine get_sort_link: not called CC=9 SCAR=45.0
Pod documented
763 0 my ($self, $sort) = @_;
764  
765 0 ▶ if(!$self->{'SORTABLE_COLUMNS'} || ref($self->{'SORTABLE_COLUMNS'}) ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"SORTABLE_COLUMNS"} or ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY") 0 0 0
Condition0%0/3
not $self->{"SORTABLE_COLUMNS"} or ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"SORTABLE_COLUMNS"} or ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
not $self->{"SORTABLE_COLUMNS"} ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not $self->{"SORTABLE_COLUMNS"}B= ref $self->{"SORTABLE_COLUMNS"} ne "ARRAY"
ABresult
1X1
011
000
766 {
767 0 die "SORTABLE_COLUMNS is either not defined or not an arrayref, sorting disabled";
768 }
769  
770 0 ▶ if(none { $_->{'get'} eq $sort } @{$self->{'SORTABLE_COLUMNS'}})
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_->{"get"} eq $sort; } , @{$$self{"SORTABLE_COLUMNS"};})) 0 0 0
771 {
772 0 die "requested sort '$sort' is impossible, not defined in SORTABLE_COLUMNS";
773 }
774  
775 # Either you're switching to a new sort col (default ASC) or you're changing the direction of the current sort
776 my $dir = (!defined($self->{'CURRENT_SORT_COL'}) || $sort ne $self->{'CURRENT_SORT_COL'} ? 'ASC' :
777 0 ▶ ($self->{'CURRENT_SORT_DIR'} eq 'ASC' ? 'DESC' : 'ASC'));
Branch: 2 branches
BranchTrueFalseTotal
!defined($self->{'CURRENT_SORT_COL'}) || $sort ne $self->{'CURRENT_SORT_COL'} ? : 0 0 0
$self->{'CURRENT_SORT_DIR'} eq 'ASC' ? : 0 0 0
Condition0%0/3
not defined $self->{"CURRENT_SORT_COL"} or $sort ne $self->{"CURRENT_SORT_COL"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"CURRENT_SORT_COL"} or $sort ne $self->{"CURRENT_SORT_COL"}
not defined $self->{"CURRENT_SORT_COL"} $sort ne $self->{"CURRENT_SORT_COL"}
Truth table0%0/3
not(A) or B
A= not defined $self->{"CURRENT_SORT_COL"}B= $sort ne $self->{"CURRENT_SORT_COL"}
ABresult
1X1
011
000
778  
779 # save the old page number and sort (this is necessary since we have a shared CGI object)
780 0 my $oldpage = $self->{'CGI_OBJECT'}->param($self->{'PAGE_VARIABLE'});
781 0 my $oldsort = $self->{'CGI_OBJECT'}->param($self->{'SORT_VARIABLE'});
782  
783 # set the new sort option, delete page (reset to page 1 on new sort)
784 0 $self->{'CGI_OBJECT'}->param($self->{'SORT_VARIABLE'}, "$sort,$dir");
785 0 $self->{'CGI_OBJECT'}->delete($self->{'PAGE_VARIABLE'});
786 0 my $url = $self->{'CGI_OBJECT'}->url( -absolute => 1, -query => 1, -path_info => 1 );
787  
788 # restore the old page and sort
789 0 ▶ if($oldsort)
Branch: 1 branch
BranchTrueFalseTotal
if ($oldsort) { } 0 0 0
790 {
791 0 $self->{'CGI_OBJECT'}->param($self->{'SORT_VARIABLE'}, $oldsort);
792 }
793 else
794 {
795 0 $self->{'CGI_OBJECT'}->delete($self->{'SORT_VARIABLE'});
796 }
797  
798 0 ▶ if($oldpage)
Branch: 1 branch
BranchTrueFalseTotal
if ($oldpage) 0 0 0
799 {
800 0 $self->{'CGI_OBJECT'}->param($self->{'PAGE_VARIABLE'}, $oldpage);
801 }
802  
803 0 return $url;
804 }
805  
806 =item B<get_sortable_table_header()>
807  
808 $html_table_header_row = $pager->get_sortable_table_header();
809  
810 Generates the HTML for the table header, containing the column names and (where applicable) links
811 to change the sort column/direction.
812  
813 Since the header defines the columns, the columns need to be the same width as they are for the
814 data. Therefore, we only draw a table row, not a full table as we do with the paging html. This row
815 should probably be the first row of the table that contains the result set.
816  
817 The output relies on stylesheet elements that currently do not have a definition anywhere.
818 You will need to define these stylesheet elements on your own. This will be fixed in a future
819 release, which hopefully will come soon.
820  
821 =cut
822  
823 sub get_sortable_table_header
824 0 ▶{
Subroutine get_sortable_table_header: not called CC=8 SCAR=42.8
Pod documented
825 0 my ($self) = @_;
826 # since this function calls get_sort_link, I'm not going to do the error checks -- let them fall through
827  
828 0 my $string = '<tr class="sortable-header-tr">';
829  
830 0 foreach my $col (@{$self->{'SORTABLE_COLUMNS'}})
831 {
832 # $col == the name of the SQL column (used by reportwriter) - also used as the 1st part of the sort GET variable
833 # $self->{'SORTABLE_COLUMNS'}->{$col} = {
834 # display_name => 'Display Name',
835 # sortable => 1|0,
836 #};
837  
838 0 ▶ my $url = ($col->{'sortable'} ? $self->get_sort_link($col->{'get'}) : '');
Branch: 1 branch
BranchTrueFalseTotal
$col->{'sortable'} ? : 0 0 0
839 $string .= '<td class="sortable-header-td">'
840 . ($url ? qq(<a class="sortable-header-a" href="$url">) : '')
841 . $col->{'display'}
842 . ($url ? '</a> <b>' : '')
843 0 ▶ . ( !defined($self->{'CURRENT_SORT_COL'}) || $col->{'get'} ne $self->{'CURRENT_SORT_COL'} ? '' : ($self->{'CURRENT_SORT_DIR'} eq 'ASC' ? $self->{'ASC_HTML'} : $self->{'DESC_HTML'}) )
Branch: 4 branches
BranchTrueFalseTotal
$url ? : 0 0 0
$url ? : 0 0 0
!defined($self->{'CURRENT_SORT_COL'}) || $col->{'get'} ne $self->{'CURRENT_SORT_COL'} ? : 0 0 0
$self->{'CURRENT_SORT_DIR'} eq 'ASC' ? : 0 0 0
Condition0%0/3
not defined $self->{"CURRENT_SORT_COL"} or $col->{"get"} ne $self->{"CURRENT_SORT_COL"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"CURRENT_SORT_COL"} or $col->{"get"} ne $self->{"CURRENT_SORT_COL"}
not defined $self->{"CURRENT_SORT_COL"} $col->{"get"} ne $self->{"CURRENT_SORT_COL"}
Truth table0%0/3
not(A) or B
A= not defined $self->{"CURRENT_SORT_COL"}B= $col->{"get"} ne $self->{"CURRENT_SORT_COL"}
ABresult
1X1
011
000
844 . '</b></td>';
845 }
846  
847 0 $string .= '</tr>';
848  
849 0 return $string;
850 }
851  
852 1 1;
853  
854 =back
855  
856 =head1 TODO
857  
858 =over
859  
860 =item *
861 allow for overrideable class names on the table elements
862  
863 =item *
864 purely CSS design?
865  
866 =back
867  
868 =head1 BUGS
869  
870 None are known at this time.
871  
872 Please report any additional bugs discovered to the author.
873  
874 =head1 SEE ALSO
875  
876 This module relies indirectly on the use of the L<DBI> and the L<Template> modules or equivalent.
877 It relies directly on the use of L<CGI>, L<POSIX>, and L<List::MoreUtils>.
878  
879 =head1 AUTHOR
880  
881 Shane Allen E<lt>opiate@gmail.comE<gt>
882  
883 =head1 ACKNOWLEDGEMENTS
884  
885 This module was developed during my employ at HRsmart, Inc. L<http://www.hrsmart.com> and its
886 public release was graciously approved.
887  
888 =head1 COPYRIGHT
889  
890 Copyright 2004, Shane Allen. All rights reserved.
891  
892 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
893  
894 =cut
↑ Summary ReportWriter.pm »