Muster/Hook/SqlReport.pm

filter: Statementstmt 19.3% 41 / 212 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 77 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 54.5% 12 / 22 Podpod 100% 2 / 2 totaltotal 12.2% 55 / 449 CC 76 SCAR 53.6
CC
76
Cov
11%
print_select
380.0
process
342.0
make_pagination
272.0
CRAP 213.4
« Muster/Hook/Counter.pm ↑ Summary Muster/MetaDb.pm »
1 package Muster::Hook::SqlReport;
2 1 $Muster::Hook::SqlReport::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::SqlReport - Muster SQL-report directive
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 DESCRIPTION
12  
13 L<Muster::Hook::SqlReport> processes the SQL-report directive.
14 Excpects SQLite databases in the config.
15  
16 =cut
17  
18 1 ▶use Mojo::Base 'Muster::Hook::Directives';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use SQLite::Work;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 METHODS
30  
31 L<Muster::Hook::SqlReport> inherits all methods from L<Muster::Hook::Directives>.
32  
33 =head2 register
34  
35 Initialize and register.
36  
37 =cut
38 0 ▶sub register {
Subroutine register: not called CC=5 SCAR=34.0
Pod documented
39 0 my $self = shift;
40 0 my $hookmaster = shift;
41 0 my $config = shift;
42  
43 0 $self->{databases} = {};
44 0 ▶ while (my ($alias, $file) = each %{$config->{hook_conf}->{'Muster::Hook::SqlReport'}})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$config{"hook_conf"}{"Muster::Hook::SqlReport"};}) 0 0 0
45 {
46 0 ▶ if (!-r $file)
Branch: 1 branch
BranchTrueFalseTotal
if (not -r $file) { } 0 0 0
47 {
48 0 warn __PACKAGE__, " cannot read database '$file'";
49 }
50 else
51 {
52 0 my $rep = SQLite::Work::Muster->new(database=>$file);
53 0 ▶ if (!$rep or !$rep->do_connect())
Branch: 1 branch
BranchTrueFalseTotal
if (not $rep && $rep->do_connect) { } 0 0 0
Condition0%0/3
$rep && $rep->do_connect
!ll&&!rl&&r
000
MC/DC0%0/2
$rep && $rep->do_connect
$rep $rep->do_connect
Truth table0%0/3
A && B
A= $repB= $rep->do_connect
ABresult
0X0
100
111
54 {
55 0 warn __PACKAGE__, "Can't connect to $file: $DBI::errstr";
56 }
57 else
58 {
59 0 $rep->{dbh}->{sqlite_unicode} = 1;
60 0 $self->{databases}->{$alias} = $rep;
61 }
62 }
63 }
64  
65 0 ▶ $hookmaster->add_hook('sqlreport' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my %args = @_;
67  
68 return $self->do_directives(
69 no_scan=>1,
70 directive=>'sqlreport',
71 0 ▶ call=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 my %args2 = @_;
73  
74 0 return $self->process(%args2);
75 },
76 0 %args,
77 );
78 },
79 0 );
80 0 return $self;
81 } # register
82  
83 0 ▶sub process {
Subroutine process: not called CC=18 SCAR=58.3
Pod documented
84 0 my $self = shift;
85 0 my %args = @_;
86  
87 0 my $leaf = $args{leaf};
88 0 my $phase = $args{phase};
89 0 my @p = @{$args{params}};
90 0 my %params = @p;
91  
92 0 my $page = $leaf->pagename;
93 0 foreach my $p (qw(database table where))
94 {
95 0 ▶ if (!exists $params{$p})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{$p}) 0 0 0
96 {
97 0 croak ("sqlreport: missing $p parameter");
98 }
99 }
100 0 ▶ if (!exists $self->{databases}->{$params{database}})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"databases"}{$params{"database"}}) 0 0 0
101 {
102 croak (sprintf('sqlreport: database %s does not exist',
103 0 $params{database}));
104 }
105  
106 # If there is more than one report on a page,
107 # the report will need an ID for pagination and/or query
108 # If there is only one report on the page, then it doesn't.
109
110 # Check for pagination and query
111 # Note that limit either means LIMIT, or records-per-page
112 # depending on whether pagination is true.
113 0 my $c = $args{controller}; # build-phase has a controller
114 0 my $n = 1;
115 0 ▶ my $pagination = (exists $params{pagination} ? $params{pagination} : 0);
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'pagination'} ? : 0 0 0
116 0 ▶ my $limit = (exists $params{limit} ? $params{limit} : 0);
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'limit'} ? : 0 0 0
117 my $n_id = (exists $params{report_id}
118 0 ▶ ? "n_".$params{report_id} : "n");
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'report_id'} ? : 0 0 0
119  
120 # pagination happens when the limit is not zero
121 # and pagination is true
122 0 ▶ if ($limit and $pagination)
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $pagination) 0 0 0
Condition0%0/3
$limit and $pagination
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $pagination
$limit $pagination
Truth table0%0/3
A and B
A= $limitB= $pagination
ABresult
0X0
100
111
123 {
124 0 ▶ $n = $c->param($n_id) || 1;
Condition0%0/2
$c->param($n_id) || 1
l!l
00
MC/DC0%0/1
$c->param($n_id) || 1
$c->param($n_id)
Truth table0%0/2
A
A= $c->param($n_id)
Aresult
11
00
125 }
126  
127 # Check the q parameter and AND it with the "where"
128 my $q_id = (exists $params{report_id}
129 0 ▶ ? "q_".$params{report_id} : "q");
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'report_id'} ? : 0 0 0
130 0 my $q = $c->param($q_id);
131 0 my $w = $params{where};
132 0 ▶ if ($q)
Branch: 1 branch
BranchTrueFalseTotal
if ($q) 0 0 0
133 {
134 0 $params{where} = "($w) AND ($q)";
135 }
136  
137 # if there is query_form enabled:
138 # - print a query form
139 0 my $prefix = '';
140 0 ▶ if (exists $params{query_form} and $params{query_form})
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params{'query_form'} and $params{'query_form'}) { } 0 0 0
elsif (exists $params{'show_query'} and $params{'show_query'}) { } 0 0 0
Condition0%0/3
exists $params{"query_form"} and $params{"query_form"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params{"query_form"} and $params{"query_form"}
exists $params{"query_form"} $params{"query_form"}
Truth table0%0/3
A and B
A= exists $params{"query_form"}B= $params{"query_form"}
ABresult
0X0
100
111
141 {
142 0 my $this_url = $c->req->url->to_string;
143 0 $prefix =<<EOT;
144 <div>
145 <form action="$this_url">
146 <strong>WHERE:</strong> $w
147 <strong>AND</strong><br/>
148 <textarea name="$q_id" rows="4" cols="60">$q</textarea>
149 <input type="submit" value="Search">
150 </form>
151 </div>
152 EOT
153 }
154 ▶ elsif (exists $params{show_query} and $params{show_query})
Condition0%0/3
exists $params{"show_query"} and $params{"show_query"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params{"show_query"} and $params{"show_query"}
exists $params{"show_query"} $params{"show_query"}
Truth table0%0/3
A and B
A= exists $params{"show_query"}B= $params{"show_query"}
ABresult
0X0
100
111
155 {
156 0 $prefix =<<EOT;
157 <div>
158 <strong>WHERE:</strong>
159 <pre>$w</pre>
160 </div>
161 EOT
162 }
163  
164 0 my $result = $self->{databases}->{$params{database}}->do_report(%params,pagination=>$pagination,page=>$n,limit=>$limit,q=>$q);
165  
166 0 ▶ if ($params{ltemplate}
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"ltemplate"} and $result) 0 0 0
Condition0%0/3
$params{"ltemplate"} and $result
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"ltemplate"} and $result
$params{"ltemplate"} $result
Truth table0%0/3
A and B
A= $params{"ltemplate"}B= $result
ABresult
0X0
100
111
167 and $result)
168 {
169 0 my $out2 = $params{ltemplate};
170 0 $out2 =~ s/CONTENTS/$result/g;
171 0 $result = $out2;
172 }
173 0 return $prefix . $result;
174 } # preprocess
175  
176 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=7 SCAR=40.3
177 0 my $self = shift;
178  
179 0 ▶ if (exists $self->{databases}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"databases"} and defined $self->{"databases"} and ref $self->{"databases"} eq "HASH") 0 0 0
Condition0%0/6
exists $self->{"databases"} and defined $self->{"databases"} and ref $self->{"databases"} eq "HASH"
!ll&&!rl&&r
000
exists $self->{"databases"} and defined $self->{"databases"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{"databases"} and defined $self->{"databases"} and ref $self->{"databases"} eq "HASH"
exists $self->{"databases"} defined $self->{"databases"} ref $self->{"databases"} eq "HASH"
Truth table0%0/4
A and B and C
A= exists $self->{"databases"}B= defined $self->{"databases"}C= ref $self->{"databases"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
180 and defined $self->{databases}
181 and ref $self->{databases} eq 'HASH')
182 {
183 0 foreach my $db (keys %{$self->{databases}})
184 {
185 0 ▶ if (defined $self->{databases}->{$db}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"databases"}{$db} and defined $self->{"databases"}{$db}{"dbh"}) 0 0 0
Condition0%0/3
defined $self->{"databases"}{$db} and defined $self->{"databases"}{$db}{"dbh"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"databases"}{$db} and defined $self->{"databases"}{$db}{"dbh"}
defined $self->{"databases"}{$db} defined $self->{"databases"}{$db}{"dbh"}
Truth table0%0/3
A and B
A= defined $self->{"databases"}{$db}B= defined $self->{"databases"}{$db}{"dbh"}
ABresult
0X0
100
111
186 and defined $self->{databases}->{$db}->{dbh})
187 {
188 0 $self->{databases}->{$db}->do_disconnect();
189 }
190 }
191 }
192 } # DESTROY
193  
194 1 1;
195 # =================================================================
196 package SQLite::Work::Muster;
197 1 $SQLite::Work::Muster::VERSION = '0.96';
198 1 ▶use SQLite::Work;
Subroutine BEGIN: called CC=1 SCAR=0.0
199 1 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
200 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
201 1 our @ISA = qw(SQLite::Work);
202  
203 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
204 0 my $class = shift;
205 0 my %parameters = (@_);
206 0 my $self = SQLite::Work->new(%parameters);
207  
208 $self->{report_template} = '<!--sqlr_contents-->'
209 0 ▶ if !defined $parameters{report_template};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parameters{"report_template"} 0 0 0
210 0 ▶ bless ($self, ref ($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
211 } # new
212  
213 =head2 print_select
214  
215 RETURN a selection result.
216  
217 =cut
218 0 ▶sub print_select {
Subroutine print_select: not called CC=19 SCAR=59.4
219 0 my $self = shift;
220 0 my $sth = shift;
221 0 my $sth2 = shift;
222 0 my %args = (
223 table=>'',
224 title=>'',
225 command=>'Search',
226 prev_file=>'',
227 prev_label=>'Prev',
228 next_file=>'',
229 next_label=>'Next',
230 prev_next_template=>'',
231 @_
232 );
233 0 my @columns = @{$args{columns}};
234 0 my @sort_by = @{$args{sort_by}};
235 0 my $table = $args{table};
236 0 my $page = $args{page};
237  
238 # read the template
239 0 my $template = $self->get_template($self->{report_template});
240 0 $self->{report_template} = $template;
241  
242 0 ▶ my $num_pages = ($args{limit} and $args{pagination} ? ceil($args{total} / $args{limit}) : 1);
Branch: 1 branch
BranchTrueFalseTotal
$args{'pagination'} ? : 0 0 0
Condition0%0/3
$args{'limit'} && ($args{'pagination'} ? ceil($args{'total'} / $args{'limit'}) : 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$args{'limit'} && ($args{'pagination'} ? ceil($args{'total'} / $args{'limit'}) : 1)
$args{'limit'} ($args{'pagination'} ? ceil($args{'total'} / $args{'limit'}) : 1)
Truth table0%0/3
A && B
A= $args{'limit'}B= ($args{'pagination'} ? ceil($args{'total'} / $args{'limit'}) : 1)
ABresult
0X0
100
111
243 # generate the HTML table
244 0 my $count = 0;
245 0 my $res_tab = '';
246 ($count, $res_tab) = $self->format_report($sth,
247 %args,
248 table=>$table,
249 table2=>$args{table2},
250 0 columns=>\@columns,
251 sort_by=>\@sort_by,
252 num_pages=>$num_pages,
253 );
254 0 my $buttons = $self->make_pagination(%args);
255 my $main_title = ($args{title} ? $args{title}
256 0 ▶ : "$table $args{command} result");
Branch: 1 branch
BranchTrueFalseTotal
$args{'title'} ? : 0 0 0
257 0 ▶ my $title = ($args{limit} and $args{pagination} ? "$main_title ($page)"
Branch: 1 branch
BranchTrueFalseTotal
$args{'pagination'} ? : 0 0 0
Condition0%0/3
$args{'limit'} && ($args{'pagination'} ? "$main_title ($page)" : $main_title)
!ll&&!rl&&r
000
MC/DC0%0/2
$args{'limit'} && ($args{'pagination'} ? "$main_title ($page)" : $main_title)
$args{'limit'} ($args{'pagination'} ? "$main_title ($page)" : $main_title)
Truth table0%0/3
A && B
A= $args{'limit'}B= ($args{'pagination'} ? "$main_title ($page)" : $main_title)
ABresult
0X0
100
111
258 : $main_title);
259 # fix up random apersands
260 0 ▶ if ($title =~ / & /)
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ / & /u) 0 0 0
261 {
262 0 $title =~ s/ & / &amp; /g;
263 }
264 0 my @result = ();
265 0 ▶ push @result, $buttons if ($args{report_style} ne 'bare');
Branch: 1 branch
BranchTrueFalseTotal
if $args{"report_style"} ne "bare" 0 0 0
266 0 ▶ if ($args{report_style} ne 'bare'
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"report_style"} ne "bare" and $args{"report_style"} ne "compact" and $args{"total"} > 1) 0 0 0
Condition0%0/6
$args{"report_style"} ne "bare" and $args{"report_style"} ne "compact" and $args{"total"} > 1
!ll&&!rl&&r
000
$args{"report_style"} ne "bare" and $args{"report_style"} ne "compact"
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"report_style"} ne "bare" and $args{"report_style"} ne "compact" and $args{"total"} > 1
$args{"report_style"} ne "bare" $args{"report_style"} ne "compact" $args{"total"} > 1
Truth table0%0/4
A and B and C
A= $args{"report_style"} ne "bare"B= $args{"report_style"} ne "compact"C= $args{"total"} > 1
ABCresult
0XX0
10X0
1100
1111
267 and $args{report_style} ne 'compact'
268 and $args{total} > 1)
269 {
270 0 my $summary = '';
271 0 ▶ if ($count == $args{total})
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $args{'total'}) { } 0 0 0
272 {
273 0 $summary = "$args{total} rows match.\n";
274 }
275 else
276 {
277 0 $summary = "$count rows displayed of $args{total}. Page $page of $num_pages.\n";
278 }
279 0 push @result, "<p>$summary</p>\n";
280 }
281 0 push @result, $res_tab;
282 0 ▶ if ($args{limit} and $args{pagination} and $args{report_style} eq 'full')
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"limit"} and $args{"pagination"} and $args{"report_style"} eq "full") 0 0 0
Condition0%0/6
$args{"limit"} and $args{"pagination"} and $args{"report_style"} eq "full"
!ll&&!rl&&r
000
$args{"limit"} and $args{"pagination"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"limit"} and $args{"pagination"} and $args{"report_style"} eq "full"
$args{"limit"} $args{"pagination"} $args{"report_style"} eq "full"
Truth table0%0/4
A and B and C
A= $args{"limit"}B= $args{"pagination"}C= $args{"report_style"} eq "full"
ABCresult
0XX0
10X0
1100
1111
283 {
284 0 push @result, "<p>Page $page of $num_pages.</p>\n"
285 }
286 0 ▶ if (defined $sth2)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth2) 0 0 0
287 {
288 0 my @cols2 = $self->get_colnames($args{table2});
289 0 my $count2;
290 my $tab2;
291 ($count2, $tab2) = $self->format_report($sth2,
292 %args,
293 table=>$args{table2},
294 0 columns=>\@cols2,
295 sort_by=>\@cols2,
296 headers=>[],
297 groups=>[],
298 row_template=>'',
299 num_pages=>0,
300 );
301 0 ▶ if ($count2)
Branch: 1 branch
BranchTrueFalseTotal
if ($count2) 0 0 0
302 {
303 0 push @result,<<EOT;
304 <h2>$args{table2}</h2>
305 $tab2
306 <p>$count2 rows displayed from $args{table2}.</p>
307 EOT
308 }
309 }
310 0 ▶ push @result, $buttons if ($args{report_style} ne 'bare');
Branch: 1 branch
BranchTrueFalseTotal
if $args{"report_style"} ne "bare" 0 0 0
311  
312 # prepend the message
313 0 ▶ unshift @result, "<p><i>$self->{message}</i></p>\n", if $self->{message};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"message"} 0 0 0
314  
315 0 my $contents = join('', @result);
316 0 my $out = $template;
317 0 $out =~ s/<!--sqlr_title-->/$title/g;
318 0 $out =~ s/<!--sqlr_contents-->/$contents/g;
319  
320 # RETURN the result
321 0 return $out;
322 } # print_select
323  
324 =head2 build_where_conditions
325  
326 If "where" is not a hash. treat it like a query.
327  
328 Otherwise do the default of the superclass.
329  
330 =cut
331 0 ▶sub build_where_conditions {
Subroutine build_where_conditions: not called CC=2 SCAR=17.9
332 0 my $self = shift;
333 0 my %args = @_;
334  
335 0 ▶ if (ref $args{where} eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"where"} eq "HASH") 0 0 0
336 {
337 0 return $self->SUPER::build_where_conditions(%args);
338 }
339 0 my @where = ();
340 0 $args{where} =~ s/;//g; # crude prevention of injection
341 0 $where[0] = $args{where};
342  
343 0 return @where;
344 } # build_where_conditions
345  
346 =head2 do_report
347  
348 Do a report, pre-processing the arguments a bit.
349  
350 =cut
351 0 ▶sub do_report {
Subroutine do_report: not called CC=13 SCAR=52.0
352 0 my $self = shift;
353 0 my %args = (
354 command=>'Select',
355 limit=>0,
356 pagination=>0,
357 page=>1,
358 headers=>'',
359 groups=>'',
360 sort_by=>'',
361 not_where=>{},
362 where=>{},
363 show=>'',
364 layout=>'table',
365 row_template=>'',
366 outfile=>'',
367 report_style=>'full',
368 title=>'',
369 prev_file=>'',
370 next_file=>'',
371 report_class=>'report',
372 report_div=>'div',
373 @_
374 );
375 0 my $table = $args{table};
376 0 my $command = $args{command};
377 0 my $report_class = $args{report_class};
378 0 my $report_div = $args{report_div};
379 0 my @headers = (ref $args{headers} ? @{$args{headers}}
380 0 ▶ : split(/\|/, $args{headers}));
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'headers'} ? : 0 0 0
381 0 my @groups = (ref $args{groups} ? @{$args{groups}}
382 0 ▶ : split(/\|/, $args{groups}));
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'groups'} ? : 0 0 0
383 0 my @sort_by = (ref $args{sort_by} ? @{$args{sort_by}}
384 0 ▶ : split(' ', $args{sort_by}));
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'sort_by'} ? : 0 0 0
385  
386  
387 my @columns = (ref $args{show}
388 0 ▶ ? @{$args{show}}
Branch: 1 branch
BranchTrueFalseTotal
$args{'show'} ? : 0 0 0
389 : ($args{show}
390 ? split(' ', $args{show})
391 0 ▶ : $self->get_colnames($table,do_rowid=>0)));
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'show'} ? : 0 0 0
392  
393 0 my $total = $self->get_total_matching(%args);
394 0 ▶ if ($total == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($total == 0) 0 0 0
395 {
396 0 return '';
397 }
398  
399 0 my ($sth1, $sth2) = $self->make_selections(%args,
400 show=>\@columns,
401 sort_by=>\@sort_by,
402 total=>$total);
403 my $out = $self->print_select($sth1,
404 $sth2,
405 %args,
406 show=>\@columns,
407 sort_by=>\@sort_by,
408 message=>$self->{message},
409 0 command=>$command,
410 total=>$total,
411 columns=>\@columns,
412 headers=>\@headers,
413 groups=>\@groups,
414 );
415 0 ▶ if ($args{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"debug"}) 0 0 0
416 {
417 0 my $stmt = $sth1->{Statement};
418 0 $out = "<p>$stmt</p>\n$out";
419 }
420 0 ▶ if ($out and $report_div and $report_class)
Branch: 2 branches
BranchTrueFalseTotal
if ($out and $report_div and $report_class) { } 0 0 0
elsif ($out and $report_div) { } 0 0 0
Condition0%0/6
$out and $report_div and $report_class
!ll&&!rl&&r
000
$out and $report_div
!ll&&!rl&&r
000
MC/DC0%0/3
$out and $report_div and $report_class
$out $report_div $report_class
Truth table0%0/4
A and B and C
A= $outB= $report_divC= $report_class
ABCresult
0XX0
10X0
1100
1111
421 {
422 0 $out =<<EOT;
423 <$report_div class="$report_class">
424 $out
425 </$report_div>
426 EOT
427 }
428 ▶ elsif ($out and $report_div)
Condition0%0/3
$out and $report_div
!ll&&!rl&&r
000
MC/DC0%0/2
$out and $report_div
$out $report_div
Truth table0%0/3
A and B
A= $outB= $report_div
ABresult
0X0
100
111
429 {
430 0 $out =<<EOT;
431 <$report_div>
432 $out
433 </$report_div>
434 EOT
435 }
436 0 return $out;
437 } # do_report
438  
439 =head2 make_pagination
440  
441 Make the buttons for the pagination.
442  
443 Pagination happens only when pagination is true and there's a limit
444 not equal to zero.
445  
446 =cut
447 0 ▶sub make_pagination {
Subroutine make_pagination: not called CC=16 SCAR=56.1
448 0 my $self = shift;
449 0 my %args = @_;
450 0 my $page = $args{page};
451 0 my $limit = $args{limit};
452 0 my $pagination = $args{pagination};
453 0 my $total = $args{total};
454 0 my $q = $args{q};
455  
456 0 ▶ if (!$limit or !$pagination)
Branch: 1 branch
BranchTrueFalseTotal
unless ($limit and $pagination) 0 0 0
Condition0%0/3
$limit and $pagination
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $pagination
$limit $pagination
Truth table0%0/3
A and B
A= $limitB= $pagination
ABresult
0X0
100
111
457 {
458 0 return "";
459 }
460  
461 0 my $num_pages = ceil($total / $limit);
462 my $n_id = (exists $args{report_id}
463 0 ▶ ? "n_".$args{report_id} : "n");
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'report_id'} ? : 0 0 0
464  
465 # If we leave the "this page" part of the url blank, it defaults to the current page,
466 # which is just what we want
467 # If we have a large number of pages, we don't want to show links to them all
468 0 my $middle_pages = ceil($num_pages / 2);
469 0 my $leeway = 3;
470  
471 0 my $out=<<EOT;
472 <div class="pagination">
473 EOT
474 0 my $printing_buttons = 0;
475 0 ▶ for (my $i=1; $i <= $num_pages; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $num_pages) 0 0 0
476 {
477 0 ▶ my $link = sprintf('<span class="button"><a href="?%s=%d%s"> %d </a></span>',
Branch: 1 branch
BranchTrueFalseTotal
$q ? : 0 0 0
478 $n_id, $i, ($q ? "&q=$q" : ''), $i);
479 0 ▶ $link = "<strong style='font-size: 1.5em;'> $i </strong>" if ($i == $page);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $page 0 0 0
480 0 ▶ if ($num_pages <= 20
Branch: 2 branches
BranchTrueFalseTotal
if ($num_pages <= 20 or $i <= $leeway + 1 or $i >= $num_pages - $leeway or $i >= $middle_pages - $leeway and $i <= $middle_pages + $leeway or $i >= $page - $leeway and $i <= $page + $leeway) { } 0 0 0
elsif ($num_pages > 20) { } 0 0 0
Condition0%0/18
$num_pages <= 20 or $i <= $leeway + 1 or $i >= $num_pages - $leeway or $i >= $middle_pages - $leeway and $i <= $middle_pages + $leeway or $i >= $page - $leeway and $i <= $page + $leeway
l!l&&r!l&&!r
000
$num_pages <= 20 or $i <= $leeway + 1 or $i >= $num_pages - $leeway or $i >= $middle_pages - $leeway and $i <= $middle_pages + $leeway
l!l&&r!l&&!r
000
$num_pages <= 20 or $i <= $leeway + 1 or $i >= $num_pages - $leeway
l!l&&r!l&&!r
000
$num_pages <= 20 or $i <= $leeway + 1
l!l&&r!l&&!r
000
$i >= $middle_pages - $leeway and $i <= $middle_pages + $leeway
!ll&&!rl&&r
000
$i >= $page - $leeway and $i <= $page + $leeway
!ll&&!rl&&r
000
MC/DC0%0/7
$num_pages <= 20 or $i <= $leeway + 1 or $i >= $num_pages - $leeway or $i >= $middle_pages - $leeway and $i <= $middle_pages + $leeway or $i >= $page - $leeway and $i <= $page + $leeway
$num_pages <= 20 $i <= $leeway + 1 $i >= $num_pages - $leeway $i >= $middle_pages - $leeway $i <= $middle_pages + $leeway $i >= $page - $leeway $i <= $page + $leeway
Truth table0%0/10
A or B or C or D and E or F and G
A= $num_pages <= 20B= $i <= $leeway + 1C= $i >= $num_pages - $leewayD= $i >= $middle_pages - $leewayE= $i <= $middle_pages + $leewayF= $i >= $page - $leewayG= $i <= $page + $leeway
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
00011XX1
0000X111
00010111
0000X0X0
0000X100
000100X0
00010100
481 or ($i <= $leeway + 1)
482 or ($i >= ($num_pages - $leeway))
483 or ($i >= ($middle_pages - $leeway) and $i <= ($middle_pages + $leeway))
484 or ($i >= ($page - $leeway) and $i <= ($page + $leeway))
485 )
486 {
487 0 $out .= "$link\n";
488 0 $printing_buttons = 1;
489 }
490 elsif ($num_pages > 20)
491 {
492 0 ▶ if ($printing_buttons)
Branch: 1 branch
BranchTrueFalseTotal
if ($printing_buttons) 0 0 0
493 {
494 # put a gap in
495 0 $out .= "...\n";
496 0 $printing_buttons = 0;
497 }
498 }
499 }
500 0 $out.=<<EOT;
501 </div>
502 EOT
503  
504 0 return $out;
505 } # make_pagination
506 1 1;
« Muster/Hook/Counter.pm ↑ Summary Muster/MetaDb.pm »