blib/lib/HTML/Dashboard.pm

filter: Statementstmt 76.3% 309 / 405 Branchbran 75.5% 68 / 90 Conditioncond 58.3% 21 / 36 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 59.7% 46 / 77 Podpod 95.0% 57 / 60 totaltotal 73.5% 508 / 691 CC 72 SCAR 16.3
CC
72
Cov
75%
dump_opts
20.0
_check_consistency
14.7
_glue
9.0
CRAP 5.1
↑ Summary
1 package HTML::Dashboard;
2  
3 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.03';
10  
11 # ============================================================
12  
13 # Test:
14 # - escape of \t and \n in as_text
15 # - DB access stuff (incl DB errors)
16  
17 # ------------------------------------------------------------
18  
19 3 my ( $HI, $MED, $LOW ) = ( 300, 200, 100 );
20  
21 18 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
22 18 my $arg = shift;
23 18 my $class = $arg; # Always create through class name, not instance
24  
25 # Check _init_data_defaults below for meaning of individual variables...
26 18 my $self = bless { data => [],
27 first => undef,
28 last => undef,
29 records => undef,
30 pagesize => undef,
31 captions => [],
32 view => [],
33 sortrank => [],
34 comparator => undef,
35 format => {} }, $class;
36  
37 # If triggers are set, these will be stored in the following instance
38 # variables as multi-hash, keyed on column (==0 for row triggers) and
39 # priority. The value is an array-ref, containing the trigger sub and
40 # a key, which can be used to find the corresponding formatting options
41 # in $self->{opts}.
42 # Fired_col_triggers is set by _find_triggered_columns - since the
43 # format of the ENTIRE col may depend on the entry of a single cell
44 # in this col, col triggers must be evaluated ahead of time, and can
45 # not be applied row by row, as is possible for cell and row triggers.
46 # If a col trigger fired, its key will be stored in fired_col_triggers.
47 18 $self->{ ROW_TRIGGERS } = undef;
48 18 $self->{ COL_TRIGGERS } = undef;
49 18 $self->{ CELL_TRIGGERS } = undef;
50 18 $self->{ fired_col_triggers } = undef;
51  
52 # For each HTML tag (or Pseudo-tag, such as first_row), there is a hash,
53 # containing HTML/CSS formatting arguments. A hash is used, so that
54 # formatting commands from different (pseudo-)tags can be blended easily.
55 # For instance, if a command for <tr...> has been set, as well as for
56 # even_row, we want to blend both of them together for even rows. Hashes
57 # can do this conveniently, using the following idiom:
58 # %tr = ( %tr, %{ $self->{opts}{even_row} } )
59 #
60 # The value of each $self->{opts}{...} is a hash-ref. Permissible keys
61 # into this hash-ref are "html" (or "color"), "class", and "style", eg
62 # $self->{opts}{table}{html} = "border='1'"
63 # For key "html", the value must be a string which can be inserted
64 # directly into the corresponding HTML tag.
65 # For key "color", the value must be a legal HTML color specification,
66 # ie. either in #rrggbb format or one of the recognized color names.
67 # For key "class", the value must be the name of a CSS class.
68 # For key "style", the value must be a string which can be given directly
69 # as value to a <... style="..."> argument of the corresponding HTML tag.
70 #
71 # The _glue() function is responsible for final formatting of these opts.
72 18 $self->{opts}{ table } = {};
73 18 $self->{opts}{ tr } = {};
74 18 $self->{opts}{ th } = {};
75 18 $self->{opts}{ td } = {};
76  
77 18 $self->{opts}{ first_row } = {};
78 18 $self->{opts}{ odd_row } = {};
79 18 $self->{opts}{ even_row } = {};
80 18 $self->{opts}{ last_row } = {};
81  
82 18 $self->{opts}{ first_col } = {};
83 18 $self->{opts}{ odd_col } = {};
84 18 $self->{opts}{ even_col } = {};
85 18 $self->{opts}{ last_col } = {};
86  
87 18 for my $i ( qw( ROW COL CELL ) ) {
88 54 for my $j ( $HI, $MED, $LOW ) {
89 162 $self->{opts}{ $i . "_TRIGGERS_" . $j } = {};
90 }
91 }
92  
93 # Defaults formats:
94 18 $self->_set_options( 'odd_row', '#eeeeee' );
95 18 $self->_set_options( 'table', 'border="1"' );
96  
97 18 return $self;
98 }
99  
100 # For debugging purposes...
101 0 ▶sub dump_opts {
Subroutine dump_opts: not called CC=4 SCAR=30.0
Pod documented
102 0 my $self = shift;
103  
104 0 my $out = '';
105  
106 0 $out .= "First: " . $self->{first} . "\n";
107 0 $out .= "Last: " . $self->{last} . "\n";
108 0 $out .= "Records: " . $self->{records} . "\n";
109 0 $out .= "Pagesize: " . $self->{pagesize} . "\n";
110 0 $out .= "Pagecount: " . $self->get_pagecount() . "\n";
111 0 $out .= "\n";
112 0 $out .= "Captions: " . join( ' | ', @{ $self->{captions} } ) . "\n";
113 0 $out .= "View: " . join( ', ', @{ $self->{view} } ) . "\n";
114  
115 0 for my $field ( keys %{ $self->{opts} } ) {
116 0 my @keys = keys %{ $self->{opts}{$field} };
117 0 ▶ if( scalar @keys == 0 ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys == 0) 0 0 0
118  
119 0 $out .= "\n=== $field ===\n";
120 0 for my $key ( @keys ) {
121 0 $out .= " $key [ " . $self->{opts}{$field}{$key} . " ]\n";
122 }
123 }
124 0 return $out;
125 }
126  
127 # All of these instance variables can only be set once the data is known.
128 18 ▶sub _init_data_defaults {
Subroutine _init_data_defaults: called CC=3 SCAR=11.0
129 18 my ( $self, $first ) = @_;
130  
131 # Index of first data record:
132 18 $self->{first} = $first;
133  
134 # Index of last data record:
135 18 $self->{last} = scalar( @{ $self->{data} } ) - 1;
136  
137 # Number of records in data set (not counting captions)
138 18 $self->{records} = 1 + $self->{last} - $self->{first};
139  
140 # Number of records in page - defaults to all
141 18 ▶ unless( $self->{pagesize} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pagesize"}) 18 0 18
142 18 $self->{pagesize} = $self->{records};
143 }
144  
145 # Column indices selected for view - defaults: all in natural order
146 18 ▶ unless( @{ $self->{view} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"view"};}) 18 0 18
147 18 $self->{view} = [ 0..( scalar @{ $self->{data}[0] } - 1 ) ];
148 }
149  
150 # Row indices in sort order - default: natural order
151 18 $self->{sortrank} = [ $self->{first}..$self->{last} ];
152  
153  
154 # Further variables referred to in the constructor are:
155 # captions : an array, containing strings to be used as col captions
156 # comparator : a sub-ref, used for sorting (cf. set_sort() and _sort_rank() )
157 # format : a hash-ref, keyed on the column, which will receive the
158 # output of the formatter. The value is an array-ref, containing
159 # the formatter sub-ref, and an indicator whether the sub-ref
160 # is a formatter (single argument) or collater (row argument).
161 }
162  
163 # -----
164  
165 17 ▶sub set_data_without_captions {
Subroutine set_data_without_captions: called CC=3 SCAR=12.7
Pod documented
166 17 my ( $self, $data ) = @_;
167 0 ▶ unless( $data && ref( $data ) eq 'ARRAY' ) { carp "Not an array ref!" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($data and ref $data eq "ARRAY") 0 17 17
Condition33%1/3
$data and ref $data eq "ARRAY"
!ll&&!rl&&r
0017
MC/DC0%0/2
$data and ref $data eq "ARRAY"
$data ref $data eq "ARRAY"
Truth table33%1/3
A and B
A= $dataB= ref $data eq "ARRAY"
ABresult
0X0
100
111
168  
169 17 $self->{data} = $data;
170  
171 17 $self->_init_data_defaults( 0 );
172 }
173  
174 1 ▶sub set_data_with_captions {
Subroutine set_data_with_captions: called CC=3 SCAR=12.0
Pod documented
175 1 my ( $self, $data ) = @_;
176 0 ▶ unless( $data && ref( $data ) eq 'ARRAY' ) { carp "Not an array ref" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($data and ref $data eq "ARRAY") 0 1 1
Condition33%1/3
$data and ref $data eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
$data and ref $data eq "ARRAY"
$data ref $data eq "ARRAY"
Truth table33%1/3
A and B
A= $dataB= ref $data eq "ARRAY"
ABresult
0X0
100
111
177  
178 1 $self->{data} = $data;
179  
180 1 $self->set_captions( @{ $data->[0] } );
181  
182 1 $self->_init_data_defaults( 1 );
183 }
184  
185 # -----
186  
187 0 ▶sub get_query { my $self = shift; return $self->{sql} }
Subroutine get_query: not called CC=1 SCAR=6.9
Pod documented
188  
189 0 ▶sub set_query_without_captions {
Subroutine set_query_without_captions: not called CC=1 SCAR=6.9
Pod documented
190 0 my ( $self, $dbh, $sql ) = @_;
191 0 $self->{sql} = $sql;
192  
193 0 $self->{data} = $dbh->selectall_arrayref( $sql );
194  
195 0 $self->_init_data_defaults( 0 );
196 }
197  
198 0 ▶sub set_query_with_captions {
Subroutine set_query_with_captions: not called CC=1 SCAR=6.9
Pod documented
199 0 my ( $self, $dbh, $sql ) = @_;
200 0 $self->{sql} = $sql;
201  
202 0 $self->{data} = $dbh->selectall_arrayref( $sql );
203  
204 0 $self->set_captions( @{ $self->{data}[0] } );
205  
206 0 $self->_init_data_defaults( 1 );
207 }
208  
209 # -----
210  
211 # UNIMPLEMENTED!
212 #
213 # The idea is to provide a way to set an SQL query, without executing it.
214 # Later, the query can be executed (using exec_query), while supplying
215 # values for bind variables.
216  
217 0 ▶sub prepare_query_without_captions { }
Subroutine prepare_query_without_captions: not called CC=1 SCAR=0.0
Pod documented
218 0 ▶sub prepare_query_with_captions { }
Subroutine prepare_query_with_captions: not called CC=1 SCAR=0.0
Pod documented
219 0 ▶sub exec_query { } # Takes query parameters
Subroutine exec_query: not called CC=1 SCAR=0.0
Pod undocumented
220  
221 # -----
222  
223 2 ▶sub set_sort {
Subroutine set_sort: called CC=1 SCAR=0.0
Pod documented
224 2 my ( $self, $sub ) = @_;
225 2 $self->{comparator} = $sub;
226 }
227  
228 20 ▶sub _sort_rank {
Subroutine _sort_rank: called CC=2 SCAR=6.9
229 20 my $self = shift;
230  
231 20 ▶ unless( $self->{comparator} ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"comparator"}) 18 2 20
232  
233 $self->{sortrank} =
234 35 [ sort { $self->{comparator}( $self->{data}[$a], $self->{data}[$b] ) }
235 2 @{ $self->{sortrank} } ];
236 }
237  
238 # -----
239  
240 # UNIMPLEMENTED!
241 #
242 # When there are subsequent rows, which have identical entries in some
243 # columns it can be neat to neat to suppress (leave blank) the repeated
244 # entries. These functions would take the col indices of the cols to be
245 # monitored for repeating behaviour and suppress them during output.
246  
247 0 ▶sub set_skip_repeats { }
Subroutine set_skip_repeats: not called CC=1 SCAR=0.0
Pod undocumented
248 0 ▶sub get_skip_repeats { }
Subroutine get_skip_repeats: not called CC=1 SCAR=0.0
Pod undocumented
249  
250 # ------------------------------------------------------------
251  
252 2 ▶sub set_view { my ( $self, @cols ) = @_; $self->{view} = \@cols; }
Subroutine set_view: called CC=1 SCAR=0.0
Pod documented
253 0 ▶sub get_view { my $self = shift; return $self->{view}; }
Subroutine get_view: not called CC=1 SCAR=6.9
Pod documented
254  
255 3 ▶sub set_captions { my ( $self, @caps ) = @_; $self->{captions} = \@caps; }
Subroutine set_captions: called CC=1 SCAR=0.0
Pod documented
256 0 ▶sub get_captions { my $self = shift; return $self->{captions}; }
Subroutine get_captions: not called CC=1 SCAR=6.9
Pod documented
257  
258 # Setting pagesize to undef, 0, or a negative value turns off pagination
259 1 ▶sub set_pagesize {
Subroutine set_pagesize: called CC=3 SCAR=13.3
Pod documented
260 1 my ( $self, $arg ) = @_;
261  
262 1 ▶ if( defined $arg && $arg > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg and $arg > 0) { } 1 0 1
Condition33%1/3
defined $arg and $arg > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $arg and $arg > 0
defined $arg $arg > 0
Truth table33%1/3
A and B
A= defined $argB= $arg > 0
ABresult
0X0
100
111
263 1 $self->{pagesize} = $arg;
264 } else {
265 0 $self->{pagesize} = $self->{records};
266 }
267 }
268  
269 4 ▶sub get_pagesize { my $self = shift; return $self->{pagesize}; }
Subroutine get_pagesize: called CC=1 SCAR=0.0
Pod documented
270  
271 3 ▶sub get_pagecount {
Subroutine get_pagecount: called CC=3 SCAR=11.8
Pod documented
272 3 my $self = shift;
273  
274 0 ▶ if( !defined $self->{pagesize} ) { return 1; } # No pagination: 1 page
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"pagesize"}) 0 3 3
275  
276 3 my $full = int( $self->{records}/$self->{pagesize} ); # Count of full pages
277 3 my $frac = $self->{records} % $self->{pagesize}; # Records on last partial pg
278  
279 3 ▶ return $full + ( $frac ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$frac ? : 3 0 3
280 }
281  
282 # ------------------------------------------------------------
283  
284 # Takes a hash-ref - the slice of $self->{opts} for the selected HTML element
285 # and glues them together into a string which can be directly embedded
286 # into an HTML tag.
287 # In doing so, it makes sure that 'color' options are properly embedded
288 # into the 'style' argument, etc.
289  
290 856 ▶sub _glue {
Subroutine _glue: called CC=9 SCAR=22.0
291 856 my ( $self, $args ) = @_;
292  
293 856 ▶ my $out = exists $args->{html} ? $args->{html} . ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'html'} ? : 17 839 856
294 856 ▶ $out .= exists $args->{class} ? 'class="' . $args->{class} .'" ' : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'class'} ? : 0 856 856
295  
296 856 ▶ if( exists $args->{color} && exists $args->{style} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args->{'color'} and exists $args->{'style'}) { } 15 841 856
elsif (not exists $args->{'color'} and exists $args->{'style'}) { } 7 834 841
elsif (exists $args->{'color'} and not exists $args->{'style'}) { } 107 1454 1561
Condition100%3/3
exists $args->{"color"} and exists $args->{"style"}
!ll&&!rl&&r
73410715
MC/DC100%2/2
exists $args->{"color"} and exists $args->{"style"}
exists $args->{"color"} exists $args->{"style"}
Truth table100%3/3
A and B
A= exists $args->{"color"}B= exists $args->{"style"}
ABresult
0X0
100
111
297 15 $out .= 'style="' . $args->{style} . '; ';
298 15 $out .= 'background-color: ' . $args->{color} . '" ';
299  
300 ▶ } elsif( !exists $args->{color} && exists $args->{style} ) {
Condition100%3/3
not exists $args->{"color"} and exists $args->{"style"}
!ll&&!rl&&r
1077277
MC/DC100%2/2
not exists $args->{"color"} and exists $args->{"style"}
not exists $args->{"color"} exists $args->{"style"}
Truth table100%3/3
not(A) and B
A= not exists $args->{"color"}B= exists $args->{"style"}
ABresult
0X0
100
111
301 7 $out .= 'style="' . $args->{style} . '" ';
302  
303 ▶ } elsif( exists $args->{color} && !exists $args->{style} ) {
Condition66%2/3
exists $args->{"color"} and not exists $args->{"style"}
!ll&&!rl&&r
7270107
MC/DC50%1/2
exists $args->{"color"} and not exists $args->{"style"}
exists $args->{"color"} not exists $args->{"style"}
Truth table66%2/3
A and not(B)
A= exists $args->{"color"}B= not exists $args->{"style"}
ABresult
0X0
100
111
304 107 $out .= 'style="background-color: ' . $args->{color} . ';" ';
305 }
306  
307 856 return $out;
308 }
309  
310 # For pagination: finds the index of the first row to display and the
311 # index of the first row NOT to display, in other words, use the following
312 # loop to display: for( $i=$from; $i<$upto; $i++ )
313  
314 20 ▶sub _find_range_for_page {
Subroutine _find_range_for_page: called CC=6 SCAR=18.7
315 20 my ( $self, $page ) = @_;
316  
317 # Find the range of rows to plot:
318 20 my ( $from, $upto ) = ( 0, $self->{records} );
319  
320 20 ▶ if( defined $page ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page) 2 18 20
321 2 ▶ if( $page < 0 || $page > $self->get_pagecount() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($page < 0 or $page > $self->get_pagecount) { } 0 2 2
elsif (defined $self->{'pagesize'}) { } 2 0 2
Condition33%1/3
$page < 0 or $page > $self->get_pagecount
l!l&&r!l&&!r
002
MC/DC0%0/2
$page < 0 or $page > $self->get_pagecount
$page < 0 $page > $self->get_pagecount
Truth table33%1/3
A or B
A= $page < 0B= $page > $self->get_pagecount
ABresult
1X1
011
000
322 0 carp "Out of bounds page $page requested - returning all rows.";
323  
324 } elsif( defined $self->{pagesize} ) { # only do pagination if switched on
325 2 $from = $page * $self->get_pagesize();
326 2 $upto = $from + $self->get_pagesize();
327  
328 2 ▶ if( $upto > $self->{records} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upto > $self->{"records"}) 1 1 2
329 1 $upto = $self->{records}; # Fractional last page
330 }
331 }
332 }
333  
334 20 return ( $from, $upto );
335 }
336  
337 20 ▶sub _check_consistency {
Subroutine _check_consistency: called CC=10 SCAR=26.9
338 20 my $self = shift;
339  
340 20 my ( $warn, $fatal ) = ( '', '' );
341  
342 20 my $colcnt = 0;
343 20 ▶ if( $self->{records} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'records'} < 1) { } 0 20 20
344 0 $warn .= "Empty data set\n";
345 } else {
346 20 my @colcount = map { scalar @{ $_ } } @{ $self->{data} };
347 20 $colcnt = $colcount[0];
348 20 ▶ if( scalar ( grep { $_ != $colcnt } @colcount ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ != $colcnt;} @colcount)) 0 20 20
349 0 $fatal .= "Data not rectangular. Column lengths: ";
350 0 $fatal .= join( ',', @colcount ) . "\n";
351 }
352 }
353  
354 20 ▶ if( @{ $self->{captions} } && $colcnt &&
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"captions"};} and $colcnt and scalar @{$self->{"captions"};} != scalar @{$self->{"data"}[0];}) 0 20 20
Condition66%4/6
@{$self->{"captions"};} and $colcnt and scalar @{$self->{"captions"};} != scalar @{$self->{"data"}[0];}
!ll&&!rl&&r
1640
@{$self->{"captions"};} and $colcnt
!ll&&!rl&&r
1604
MC/DC0%0/3
@{$self->{"captions"};} and $colcnt and scalar @{$self->{"captions"};} != scalar @{$self->{"data"}[0];}
@{$self->{"captions"};} $colcnt scalar @{$self->{"captions"};} != scalar @{$self->{"data"}[0];}
Truth table50%2/4
A and B and C
A= @{$self->{"captions"};}B= $colcntC= scalar @{$self->{"captions"};} != scalar @{$self->{"data"}[0];}
ABCresult
0XX0
10X0
1100
1111
355 4 scalar @{ $self->{captions} } != scalar @{ $self->{data}[0] } ) {
356 0 $fatal .= "Number of captions not equal to number of columns\n";
357 }
358  
359 20 ▶ if( scalar ( grep { $_ < 0 || $_ >= $colcnt } @{ $self->{view} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ < 0 or $_ >= $colcnt;} @{$$self{"view"};})) 0 20 20
Condition33%1/3
$_ < 0 || $_ >= $colcnt
l!l&&r!l&&!r
0093
MC/DC0%0/2
$_ < 0 || $_ >= $colcnt
$_ < 0 $_ >= $colcnt
Truth table33%1/3
A || B
A= $_ < 0B= $_ >= $colcnt
ABresult
1X1
011
000
360 0 $fatal .= "Illegal index in view\n";
361 }
362  
363 0 ▶ if( $fatal ) { croak "$warn $fatal"; }
Branch: 2 branches
BranchTrueFalseTotal
if ($fatal) { } 0 20 20
elsif ($warn) { } 0 20 20
364 0 elsif( $warn ) { carp "$warn"; }
365 }
366  
367 # ------------------------------------------------------------
368 # Output Routines
369 #
370 # Tables are built up outside in (table->row->cell->contents).
371 # For each element, all opts are collected and all rules and
372 # triggers are evaluated. This leads to a fully formed HTML tag.
373 # Then the subsequent element (ie after <tr> come all the <td>, etc)
374 # is evaluated in a similar fashion.
375  
376 # -----
377  
378 # Convention (for the following routines):
379 # $row, $col : the indices of the 'true' row or col in the full data set
380 # $prow, $vcol : the indices of the row in the current page or the column
381 # in the current view
382 # ------------------------------------------------------------
383  
384 3 ▶sub as_text {
Subroutine as_text: called CC=4 SCAR=14.0
Pod documented
385 3 my ( $self, $page ) = @_;
386  
387 3 $self->_check_consistency();
388  
389 3 my ( $from, $upto ) = $self->_find_range_for_page( $page );
390  
391 3 $self->_sort_rank();
392  
393 3 my $body = '';
394 3 ▶ if( @{ $self->{captions} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"captions"};}) 0 3 3
395 # Array slice of @captions, indexed by @view...
396 0 $body .= join( "\t", @{ $self->{captions} }[ @{ $self->{view} } ] ) . "\n";
397 }
398  
399 3 my ( $prow, $vcol, $row, $col ) = ( 0, 0, 0, 0 );
400 3 foreach my $idx ( $from..$upto-1 ) {
401 19 $row = $self->{sortrank}[$idx];
402  
403 19 $vcol = 0;
404 19 foreach my $col ( @{ $self->{view} } ) {
405 91 my $token = $self->_content( $prow, $vcol, $row, $col );
406 91 $token =~ s/([\t\n\\])/\\$1/g; # Escape newline, tab, and backslash
407 91 $body .= "$token\t";
408 91 $vcol += 1;
409 }
410 19 chop $body; # remove the last tab, then replace with newline
411 19 $body .= "\n";
412  
413 19 $prow += 1;
414 }
415 # The last newline is NOT chopped - text ends with a newline.
416  
417 3 return $body;
418 }
419  
420 17 ▶sub as_HTML {
Subroutine as_HTML: called CC=2 SCAR=6.9
Pod documented
421 17 my ( $self, $page ) = @_;
422  
423 17 $self->_check_consistency();
424  
425 17 my ( $from, $upto ) = $self->_find_range_for_page( $page );
426  
427 17 $self->_sort_rank();
428 17 $self->_find_triggered_columns();
429  
430  
431 # Find options - only simple options for table-tag:
432 17 my %table = %{ $self->{opts}{table} };
433  
434 # Build the body of the table:
435 17 my ( $prow, $body ) = ( 0, '' );
436 17 foreach my $idx ( $from..$upto-1 ) {
437 143 my $row = $self->{sortrank}[$idx];
438 143 $body .= $self->_row( $prow++, $row );
439 }
440  
441 # Build output:
442 17 my $out = "\n\n";
443 17 $out .= "<!-- Table generated by HTML::Dashboard - www.cpan.org -->\n";
444 17 $out .= '<table ' . $self->_glue( \%table ) . ">\n";
445 17 $out .= $self->_caption() . "\n";
446 17 $out .= $body . "\n";
447 17 $out .= "</table>\n\n";
448  
449 17 return $out;
450 }
451  
452 # Returns a fully formed caption row: <tr><th>....</tr>
453 17 ▶sub _caption {
Subroutine _caption: called CC=3 SCAR=11.0
454 17 my ( $self ) = @_;
455  
456 17 ▶ unless( @{ $self->{captions} } ) { return ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"captions"};}) 13 4 17
457  
458 4 my %tr = %{ $self->{opts}{tr} };
459 4 my %th = %{ $self->{opts}{th} };
460 4 my $th = '<th ' . $self->_glue( \%th ) . '>';
461  
462 4 my $out = '<tr ' . $self->_glue( \%tr ) . '>';
463 4 foreach my $col ( @{ $self->{view} } ) {
464 20 $out .= $th . $self->{captions}[$col] . '</th>';
465 }
466 4 $out .= '</tr>';
467 4 return $out . "\n";
468 }
469  
470 143 ▶sub _row {
Subroutine _row: called CC=8 SCAR=20.8
471 143 my ( $self, $prow, $row ) = @_;
472  
473 # Build options:
474 143 my %tr = %{ $self->{opts}{tr} };
475  
476 # Odd/Even Row
477 143 ▶ if( $prow %2 == 0 ) { %tr = ( %tr, %{ $self->{opts}{even_row} } ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($prow % 2 == 0) { } 79 64 143
478 64 else { %tr = ( %tr, %{ $self->{opts}{odd_row} } ); }
479  
480 # First/Last Row
481 143 ▶ if( $prow == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prow == 0) { } 17 126 143
elsif ($prow == $self->{'pagesize'} - 1) { } 16 110 126
482 17 %tr = ( %tr, %{ $self->{opts}{first_row} } ); # First row
483 } elsif( $prow == $self->{pagesize} - 1 ) {
484 16 %tr = ( %tr, %{ $self->{opts}{last_row} } ); # Last row
485 }
486  
487 # Triggers: Hi->Med->Low
488 143 ▶ if( exists $self->{ROW_TRIGGERS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"ROW_TRIGGERS"}) 143 0 143
489 143 for my $prio ( sort { $b <=> $a } keys %{ $self->{ROW_TRIGGERS}{0} } ) {
490 34 my ( $trig, $key ) = @{ $self->{ROW_TRIGGERS}{0}{$prio} };
491  
492 34 ▶ if( $trig->( $self->{data}[$row], $prow, $row ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$trig($self->{"data"}[$row], $prow, $row)) 15 19 34
493 15 %tr = ( %tr, %{ $self->{opts}{$key} } );
494 15 last;
495 }
496 }
497 }
498  
499 # Build column body:
500 143 my ( $vcol, $body ) = ( 0, '' );
501 143 foreach my $col ( @{ $self->{view} } ) {
502 688 $body .= $self->_cell( $prow, $vcol++, $row, $col );
503 }
504  
505 143 my $out = '<tr ' . $self->_glue( \%tr ) . '>' . $body . "</tr>\n";
506  
507 143 return $out;
508 }
509  
510 688 ▶sub _cell {
Subroutine _cell: called CC=9 SCAR=22.0
511 688 my ( $self, $prow, $vcol, $row, $col ) = @_;
512  
513 688 my %td = %{ $self->{opts}{td} };
514  
515  
516 # Even/odd column
517 688 ▶ if( $vcol %2 == 0 ) { %td = ( %td, %{ $self->{opts}{even_col} } ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($vcol % 2 == 0) { } 411 277 688
518 277 else { %td = ( %td, %{ $self->{opts}{odd_col} } ); }
519  
520 # First/last column
521 688 ▶ if( $vcol == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($vcol == 0) { } 143 545 688
elsif ($vcol == scalar(@{$self->{'view'};}) - 1) { } 143 402 545
522 143 %td = ( %td, %{ $self->{opts}{first_col} } ); # First column
523 545 } elsif( $vcol == scalar( @{ $self->{view} } )-1 ) {
524 143 %td = ( %td, %{ $self->{opts}{last_col} } ); # Last column
525 }
526  
527  
528 # Cell triggers
529 688 my $flag = 0;
530 688 ▶ if( exists $self->{CELL_TRIGGERS}{ $col } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"CELL_TRIGGERS"}{$col}) 27 661 688
531 27 for my $prio ( sort { $b <=> $a } keys %{ $self->{CELL_TRIGGERS}{$col} } ){
532 39 my ( $trig, $key ) = @{ $self->{CELL_TRIGGERS}{ $col }{ $prio } };
533  
534 39 ▶ if( $trig->( $self->{data}[$row][$col], $vcol, $col ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$trig($self->{"data"}[$row][$col], $vcol, $col)) 11 28 39
535 11 %td = ( %td, %{ $self->{opts}{$key} } );
536 11 $flag = 1;
537 11 last;
538 }
539 }
540 }
541  
542  
543 # Col triggers - only if no cell triggers have fired!
544 688 ▶ if( !$flag && exists $self->{fired_col_triggers}{ $col } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $flag and exists $self->{"fired_col_triggers"}{$col}) 17 671 688
Condition100%3/3
not $flag and exists $self->{"fired_col_triggers"}{$col}
!ll&&!rl&&r
1166017
MC/DC100%2/2
not $flag and exists $self->{"fired_col_triggers"}{$col}
not $flag exists $self->{"fired_col_triggers"}{$col}
Truth table100%3/3
not(A) and B
A= not $flagB= exists $self->{"fired_col_triggers"}{$col}
ABresult
0X0
100
111
545 17 my $key = $self->{fired_col_triggers}{ $col };
546 17 %td = ( %td, %{ $self->{opts}{ $key } } );
547 }
548  
549  
550 688 return join '', '<td ', $self->_glue( \%td ), '>',
551 $self->_content( $prow, $vcol, $row, $col ), '</td>';
552 }
553  
554 779 ▶sub _content {
Subroutine _content: called CC=3 SCAR=11.0
555 779 my ( $self, $prow, $vcol, $row, $col ) = @_;
556  
557 779 ▶ if( exists $self->{format}{$col} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"format"}{$col}) 18 761 779
558 18 my ( $format, $type ) = @{ $self->{format}{$col} };
559  
560 18 ▶ if( $type eq 'format' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'format') { } 9 9 18
561 9 return $format->( $self->{data}[$row][$col] );
562 } else {
563 9 return $format->( @{ $self->{data}[$row] } );
564 }
565 }
566  
567 761 return $self->{data}[$row][$col];
568 }
569  
570 # ------------------------------------------------------------
571  
572 0 ▶sub _get_options {
Subroutine _get_options: not called CC=1 SCAR=6.9
573 0 my ( $self, $opt ) = @_; # $opt must be: first_col, or odd_row, etc
574  
575 # Worry about notfound?
576  
577 # Return value is always: undef or a hash-ref: key/value (?)
578  
579 0 return $self->{opts}{$opt};
580 }
581  
582 54 ▶sub _set_options {
Subroutine _set_options: called CC=5 SCAR=16.8
583 54 my ( $self, $opt, $arg, $val ) = @_;
584  
585 # Decide if one or two arguments are present...
586 54 ▶ if( !defined $val ) { # One argument - figure out the 'key', arg is 'val'
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 49 5 54
elsif ($arg eq 'class' or $arg eq 'style') { } 5 0 5
587 49 my %html = ( table => 1, tr => 1, th => 1, td => 1 );
588 49 ▶ my $field = exists $html{$opt} ? 'html' : 'color';
Branch: 1 branch
BranchTrueFalseTotal
exists $html{$opt} ? : 18 31 49
589  
590 49 $self->{opts}{$opt}{$field} = $arg;
591  
592 ▶ } elsif( $arg eq 'class' || $arg eq 'style' ) { # Two args: key and val
Condition33%1/3
$arg eq "class" or $arg eq "style"
l!l&&r!l&&!r
050
MC/DC0%0/2
$arg eq "class" or $arg eq "style"
$arg eq "class" $arg eq "style"
Truth table33%1/3
A or B
A= $arg eq "class"B= $arg eq "style"
ABresult
1X1
011
000
593 5 $self->{opts}{$opt}{$arg} = $val;
594  
595 } else {
596 0 carp "Illegal arguments - ,$opt, ,$arg, ,$val,";
597 }
598 }
599  
600 # ------------------------------------------------------------
601 # Arguments:
602 # $htmlargs || class => $class || style => $style
603  
604 1 ▶sub set_table { my $s = shift; return $s->_set_options( 'table', @_ ); }
Subroutine set_table: called CC=1 SCAR=0.0
Pod documented
605 0 ▶sub set_tr { my $s = shift; return $s->_set_options( 'tr', @_ ); }
Subroutine set_tr: not called CC=1 SCAR=6.9
Pod documented
606 0 ▶sub set_th { my $s = shift; return $s->_set_options( 'th', @_ ); }
Subroutine set_th: not called CC=1 SCAR=6.9
Pod documented
607 0 ▶sub set_td { my $s = shift; return $s->_set_options( 'td', @_ ); }
Subroutine set_td: not called CC=1 SCAR=6.9
Pod documented
608  
609 0 ▶sub get_table { my $s = shift; return $s->_get_options( 'table' ); }
Subroutine get_table: not called CC=1 SCAR=6.9
Pod documented
610 0 ▶sub get_tr { my $s = shift; return $s->_get_options( 'tr' ); }
Subroutine get_tr: not called CC=1 SCAR=6.9
Pod documented
611 0 ▶sub get_th { my $s = shift; return $s->_get_options( 'th' ); }
Subroutine get_th: not called CC=1 SCAR=6.9
Pod documented
612 0 ▶sub get_td { my $s = shift; return $s->_get_options( 'td' ); }
Subroutine get_td: not called CC=1 SCAR=6.9
Pod documented
613  
614 # ------------------------------------------------------------
615 # Arguments:
616 # $color || class => $class || style => $style
617  
618 0 ▶sub get_first_row { my $s = shift; return $s->_get_options( 'first_row' ); }
Subroutine get_first_row: not called CC=1 SCAR=6.9
Pod documented
619 0 ▶sub get_odd_row { my $s = shift; return $s->_get_options( 'odd_row' ); }
Subroutine get_odd_row: not called CC=1 SCAR=6.9
Pod documented
620 0 ▶sub get_even_row { my $s = shift; return $s->_get_options( 'even_row' ); }
Subroutine get_even_row: not called CC=1 SCAR=6.9
Pod documented
621 0 ▶sub get_last_row { my $s = shift; return $s->_get_options( 'last_row' ); }
Subroutine get_last_row: not called CC=1 SCAR=6.9
Pod documented
622  
623 0 ▶sub get_first_col { my $s = shift; return $s->_get_options( 'first_col' ); }
Subroutine get_first_col: not called CC=1 SCAR=6.9
Pod documented
624 0 ▶sub get_odd_col { my $s = shift; return $s->_get_options( 'odd_col' ); }
Subroutine get_odd_col: not called CC=1 SCAR=6.9
Pod documented
625 0 ▶sub get_even_col { my $s = shift; return $s->_get_options( 'even_col' ); }
Subroutine get_even_col: not called CC=1 SCAR=6.9
Pod documented
626 0 ▶sub get_last_col { my $s = shift; return $s->_get_options( 'last_col' ); }
Subroutine get_last_col: not called CC=1 SCAR=6.9
Pod documented
627  
628 # -----
629  
630 1 ▶sub set_first_row { my $s = shift; $s->_set_options( 'first_row', @_ ); }
Subroutine set_first_row: called CC=1 SCAR=0.0
Pod documented
631 1 ▶sub set_odd_row { my $s = shift; $s->_set_options( 'odd_row', @_ ); }
Subroutine set_odd_row: called CC=1 SCAR=0.0
Pod documented
632 1 ▶sub set_even_row { my $s = shift; $s->_set_options( 'even_row', @_ ); }
Subroutine set_even_row: called CC=1 SCAR=0.0
Pod documented
633 1 ▶sub set_last_row { my $s = shift; $s->_set_options( 'last_row', @_ ); }
Subroutine set_last_row: called CC=1 SCAR=0.0
Pod documented
634  
635 0 ▶sub set_first_col { my $s = shift; $s->_set_options( 'first_col', @_ ); }
Subroutine set_first_col: not called CC=1 SCAR=6.9
Pod documented
636 0 ▶sub set_odd_col { my $s = shift; $s->_set_options( 'odd_col', @_ ); }
Subroutine set_odd_col: not called CC=1 SCAR=6.9
Pod documented
637 1 ▶sub set_even_col { my $s = shift; $s->_set_options( 'even_col', @_ ); }
Subroutine set_even_col: called CC=1 SCAR=0.0
Pod documented
638 1 ▶sub set_last_col { my $s = shift; $s->_set_options( 'last_col', @_ ); }
Subroutine set_last_col: called CC=1 SCAR=0.0
Pod documented
639  
640 # ------------------------------------------------------------
641 # Arguments:
642 # $col, $sub, $color || class => $class || style => $style
643  
644 11 ▶sub _set_trigger {
Subroutine _set_trigger: called CC=1 SCAR=0.0
645 11 my ( $self, $elem, $prio, $col, $sub, @args ) = @_;
646  
647 11 my $trig = join '_', $elem, 'TRIGGERS';
648 11 my $key = join '_', $trig, $prio;
649  
650 11 $self->{ $trig }{ $col }{ $prio } = [ $sub, $key ];
651 11 $self->_set_options( $key, @args );
652 }
653  
654 # sub set_row_trigger { my ( $self, $prio, $sub, @args ) = @_; }
655 4 ▶sub set_row_trigger {
Subroutine set_row_trigger: called CC=1 SCAR=0.0
Pod documented
656 4 my ( $self, $prio, @args ) = @_;
657 4 $self->_set_trigger( 'ROW', $prio, 0, @args );
658 }
659 2 ▶sub set_row_hi { my $self = shift; $self->set_row_trigger( $HI, @_ ); }
Subroutine set_row_hi: called CC=1 SCAR=0.0
Pod documented
660 0 ▶sub set_row_med { my $self = shift; $self->set_row_trigger( $MED, @_ ); }
Subroutine set_row_med: not called CC=1 SCAR=6.9
Pod documented
661 2 ▶sub set_row_low { my $self = shift; $self->set_row_trigger( $LOW, @_ ); }
Subroutine set_row_low: called CC=1 SCAR=0.0
Pod documented
662  
663 # sub set_col_trigger { my ( $self, $prio, $col, $sub, @args ) = @_; }
664 2 ▶sub set_col_trigger { my $self = shift; $self->_set_trigger( 'COL', @_); }
Subroutine set_col_trigger: called CC=1 SCAR=0.0
Pod documented
665 0 ▶sub set_col_hi { my $self = shift; $self->set_col_trigger( $HI, @_ ); }
Subroutine set_col_hi: not called CC=1 SCAR=6.9
Pod documented
666 1 ▶sub set_col_med { my $self = shift; $self->set_col_trigger( $MED, @_ ); }
Subroutine set_col_med: called CC=1 SCAR=0.0
Pod documented
667 1 ▶sub set_col_low { my $self = shift; $self->set_col_trigger( $LOW, @_ ); }
Subroutine set_col_low: called CC=1 SCAR=0.0
Pod documented
668  
669 # sub set_cell_trigger { my ( $self, $prio, $col, $sub, @args ) = @_; }
670 5 ▶sub set_cell_trigger { my $self = shift; $self->_set_trigger( 'CELL', @_); }
Subroutine set_cell_trigger: called CC=1 SCAR=0.0
Pod documented
671 3 ▶sub set_cell_hi { my $self = shift; $self->set_cell_trigger( $HI, @_ ); }
Subroutine set_cell_hi: called CC=1 SCAR=0.0
Pod documented
672 1 ▶sub set_cell_med { my $self = shift; $self->set_cell_trigger( $MED, @_ ); }
Subroutine set_cell_med: called CC=1 SCAR=0.0
Pod documented
673 1 ▶sub set_cell_low { my $self = shift; $self->set_cell_trigger( $LOW, @_ ); }
Subroutine set_cell_low: called CC=1 SCAR=0.0
Pod documented
674  
675  
676 17 ▶sub _find_triggered_columns {
Subroutine _find_triggered_columns: called CC=7 SCAR=19.6
677 17 my $self = shift;
678  
679 0 ▶ unless( exists $self->{COL_TRIGGERS} ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"COL_TRIGGERS"}) 0 17 17
680  
681 17 my $vcol = -1;
682 17 for my $col ( @{ $self->{view} } ) {
683 82 $vcol += 1;
684  
685 82 ▶ unless( exists $self->{COL_TRIGGERS}{ $col } ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"COL_TRIGGERS"}{$col}) 80 2 82
686  
687 # Low->Med->Hi : higher priority clobbers earlier results!
688 0 for my $prio ( sort { $a <=> $b } keys %{ $self->{COL_TRIGGERS}{$col} } ) {
689 2 my ( $trig, $key ) = @{ $self->{COL_TRIGGERS}{$col}{$prio} };
690  
691 2 foreach my $idx ( 0..$self->{records} ) {
692 10 my $row = $self->{sortrank}[$idx];
693 10 ▶ if( $trig->( $self->{data}[$row][$col], $vcol, $col ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$trig($self->{"data"}[$row][$col], $vcol, $col)) 2 8 10
694 2 $self->{fired_col_triggers}{$col} = $key;
695 2 last;
696 }
697 }
698 }
699 }
700 }
701  
702 # ------------------------------------------------------------
703 # Arguments:
704 # $col, $sub
705  
706 # For each column, a formatting function can be set. The function is called
707 # for each cell in the column. The return value of the function is used as
708 # printable content of the cell.
709 # A 'format' function receives as argument the raw value of the cell.
710 # A 'collate' function receives as argument the entire row as an array.
711 #
712 # Examples:
713 # format:
714 # sub { substr $_[0], 0, 3 } prints only first three chars of cell val
715 # collate:
716 # sub { $_[0] . '=' . $_[1] } concates first and second column value
717  
718 1 ▶sub set_format {
Subroutine set_format: called CC=1 SCAR=0.0
Pod documented
719 1 my ( $self, $col, $sub ) = @_;
720 1 $self->{format}{$col} = [ $sub, 'format' ];
721 }
722  
723 1 ▶sub set_collate {
Subroutine set_collate: called CC=1 SCAR=0.0
Pod documented
724 1 my ( $self, $col, $sub ) = @_;
725 1 $self->{format}{$col} = [ $sub, 'collate' ];
726 }
727  
728 3 1;
729  
730 __END__
↑ Summary