Search/Display/Table.pm

filter: Statementstmt 23.1% 16 / 69 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 50.0% 5 / 10 Podpod 100% 5 / 5 totaltotal 12.9% 26 / 201 CC 42 SCAR 54.3
CC
42
Cov
10%
render_column_headers
462.0
display_row
132.0
display_dataset
42.0
CRAP 228.9
« Search.pm ↑ Summary Search/AbstractDisplay.pm »
1 package CGI::Widget::DBI::Search::Display::Table;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use base qw/ CGI::Widget::DBI::Search::AbstractDisplay /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use constant TABLE_HEADER_BGCOLOR => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # if these are unset, do not toggle bgcolor via html, but can still do it via CSS class {odd,even}Row
9 3 ▶use constant TABLE_BGCOLOR1 => '#eeeeee';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use constant TABLE_BGCOLOR2 => '#ffffff';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 CGI::Widget::DBI::Search::Display::Table - HTML table display class for Search widget
15  
16 =head1 SYNOPSIS
17  
18 my $ws = CGI::Widget::DBI::Search->new(q => CGI->new);
19 ...
20 $ws->{-display_class} = 'CGI::Widget::DBI::Search::Display::Table';
21  
22 # or instead, simply:
23 $ws->{-display_mode} = 'table';
24  
25 # note: this is default behavior for the search widget, so this is all just for
26 # informational purposes, e.g. to write your own display class
27  
28 =head1 DESCRIPTION
29  
30 This class displays search results retrieved in the search widget in table format,
31 much like the output from a typical relational database client. The dataset can be
32 sorted via column header anchor tags, and paging links appear on all corners of
33 the table display, as well as a list of navigable page ranges at bottom center.
34  
35 =head1 METHODS
36  
37 =over 4
38  
39 =cut
40  
41 =item render_dataset()
42  
43 Builds HTML table rows for current page in the dataset, including a header row
44 with the visible columns with sortable columns as clickable anchor tags.
45  
46 Builds data in object variables:
47  
48 dataset_rows_html
49 header_columns
50 header_html
51  
52 =cut
53  
54 0 ▶sub render_dataset {
Subroutine render_dataset: not called CC=2 SCAR=17.9
Pod documented
55 0 my ($self) = @_;
56 0 $self->{'dataset_rows_html'} = [];
57  
58 0 $self->render_column_headers();
59  
60 # iterate over most recently returned 'results', which should be a (possibly blessed) hashref
61 0 foreach my $row (@{$self->{'results'}}) {
62 # build a table row
63 0 push(@{ $self->{'dataset_rows_html'} }, $self->display_row($row));
64 }
65 }
66  
67 =item render_column_headers()
68  
69 Called by render_dataset() to render just the column headers (along with sort
70 links) for the most recent search.
71  
72 =cut
73  
74 0 ▶sub render_column_headers {
Subroutine render_column_headers: not called CC=21 SCAR=61.4
Pod documented
75 0 my ($self) = @_;
76 0 my $q = $self->{q};
77 0 my $header_th_html = '';
78 0 my $header_bgcolor = $self->TABLE_HEADER_BGCOLOR;
79 0 ▶ my %sortable_cols = %{ $self->{-sortable_columns} || {} };
Condition0%0/2
$self->{'-sortable_columns'} || {}
l!l
00
MC/DC0%0/1
$self->{'-sortable_columns'} || {}
$self->{'-sortable_columns'}
Truth table0%0/2
A
A= $self->{'-sortable_columns'}
Aresult
11
00
80  
81 # build displayed column headers along with sort links and direction arrow
82 0 foreach my $col (@{ $self->{'header_columns'} }) {
83 my $align = $self->{-column_align}->{$col} ||
84 0 ▶ ($self->{-numeric_columns}->{$col} || $self->{-currency_columns}->{$col} ? 'right' : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'-numeric_columns'}{$col} || $self->{'-currency_columns'}{$col} ? : 0 0 0
Condition0%0/6
$self->{'-column_align'}{$col} || ($self->{'-numeric_columns'}{$col} || $self->{'-currency_columns'}{$col} ? 'right' : undef)
l!l&&r!l&&!r
000
$self->{"-numeric_columns"}{$col} or $self->{"-currency_columns"}{$col}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{'-column_align'}{$col} || ($self->{'-numeric_columns'}{$col} || $self->{'-currency_columns'}{$col} ? 'right' : undef)
$self->{'-column_align'}{$col} $self->{"-numeric_columns"}{$col} $self->{"-currency_columns"}{$col}
Truth table0%0/4
A || B or C
A= $self->{'-column_align'}{$col}B= $self->{"-numeric_columns"}{$col}C= $self->{"-currency_columns"}{$col}
ABCresult
1XX1
01X1
0011
0000
85 0 ▶ my %th_attributes = ( $header_bgcolor ? (-bgcolor => $header_bgcolor) : (), -nowrap => 1, $align ? (-align => $align) : () );
Branch: 2 branches
BranchTrueFalseTotal
$header_bgcolor ? : 0 0 0
$align ? : 0 0 0
86 0 ▶ my %extra_th_attributes = %{ $self->get_option_value('-extra_table_header_cell_attributes', [$col]) || {} };
Condition0%0/2
$self->get_option_value('-extra_table_header_cell_attributes', [$col]) || {}
l!l
00
MC/DC0%0/1
$self->get_option_value('-extra_table_header_cell_attributes', [$col]) || {}
$self->get_option_value('-extra_table_header_cell_attributes', [$col])
Truth table0%0/2
A
A= $self->get_option_value('-extra_table_header_cell_attributes', [$col])
Aresult
11
00
87 0 ▶ if ($self->{-unsortable_columns}->{$col} || (%sortable_cols && ! $sortable_cols{$col}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'-unsortable_columns'}{$col} or %sortable_cols and not $sortable_cols{$col}) { } 0 0 0
Condition0%0/6
$self->{"-unsortable_columns"}{$col} or %sortable_cols and not $sortable_cols{$col}
l!l&&r!l&&!r
000
%sortable_cols and not $sortable_cols{$col}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"-unsortable_columns"}{$col} or %sortable_cols and not $sortable_cols{$col}
$self->{"-unsortable_columns"}{$col} %sortable_cols not $sortable_cols{$col}
Truth table0%0/4
A or B and not(C)
A= $self->{"-unsortable_columns"}{$col}B= %sortable_colsC= not $sortable_cols{$col}
ABCresult
1XX1
0111
00X0
0100
88 $header_th_html .= $self->{q}->th(
89 {-class => $self->{-css_table_unsortable_header_cell_class} || 'searchWidgetTableUnsortableHeaderCell',
90 %th_attributes, %extra_th_attributes},
91 0 ▶ '<span>'.$self->{-display_columns}->{$col}.'</span>',
Condition0%0/2
$self->{'-css_table_unsortable_header_cell_class'} || 'searchWidgetTableUnsortableHeaderCell'
l!l
00
MC/DC0%0/1
$self->{'-css_table_unsortable_header_cell_class'} || 'searchWidgetTableUnsortableHeaderCell'
$self->{'-css_table_unsortable_header_cell_class'}
Truth table0%0/2
A
A= $self->{'-css_table_unsortable_header_cell_class'}
Aresult
11
00
92 );
93  
94 } else {
95 0 ▶ my $sortby = $self->{'sortby'} && $col eq $self->{'sortby'};
Condition0%0/3
$self->{'sortby'} && $col eq $self->{'sortby'}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'sortby'} && $col eq $self->{'sortby'}
$self->{'sortby'} $col eq $self->{'sortby'}
Truth table0%0/3
A && B
A= $self->{'sortby'}B= $col eq $self->{'sortby'}
ABresult
0X0
100
111
96 $header_th_html .= $self->{q}->th(
97 {-class => $self->{-css_table_header_cell_class} || 'searchWidgetTableHeaderCell',
98 %th_attributes, %extra_th_attributes},
99 ($sortby ? '<b>' : '')
100 .'<a href="'.($self->{'action_uri_jsfunc'} ? 'javascript:' : '').$self->sortby_column_uri($col).'" title="'.($self->{-column_titles}->{$col} || $self->translate('Sort by').': '.$self->{-display_columns}->{$col}).'">'
101 .'<span>'.$self->{-display_columns}->{$col}.'</span></a> '
102 0 ▶ .($sortby ? ($self->{'sort_reverse'}->{$col} ? '\\/' :'/\\').'</b>' : '')
Branch: 4 branches
BranchTrueFalseTotal
$sortby ? : 0 0 0
$self->{'action_uri_jsfunc'} ? : 0 0 0
$sortby ? : 0 0 0
$self->{'sort_reverse'}{$col} ? : 0 0 0
Condition0%0/4
$self->{'-css_table_header_cell_class'} || 'searchWidgetTableHeaderCell'
l!l
00
$self->{'-column_titles'}{$col} || $self->translate('Sort by') . ': ' . $self->{'-display_columns'}{$col}
l!l
00
MC/DC0%0/1
$self->{'-css_table_header_cell_class'} || 'searchWidgetTableHeaderCell'
$self->{'-css_table_header_cell_class'}
MC/DC0%0/1
$self->{'-column_titles'}{$col} || $self->translate('Sort by') . ': ' . $self->{'-display_columns'}{$col}
$self->{'-column_titles'}{$col}
Truth table0%0/2
A
A= $self->{'-css_table_header_cell_class'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'-column_titles'}{$col}
Aresult
11
00
103 );
104 }
105 }
106 0 ▶ $self->{'header_html'} = '<tr class="'.($self->{-css_table_header_row_class} || 'searchWidgetTableHeaderRow').'">'.$header_th_html.'</tr>';
Condition0%0/2
$self->{'-css_table_header_row_class'} || 'searchWidgetTableHeaderRow'
l!l
00
MC/DC0%0/1
$self->{'-css_table_header_row_class'} || 'searchWidgetTableHeaderRow'
$self->{'-css_table_header_row_class'}
Truth table0%0/2
A
A= $self->{'-css_table_header_row_class'}
Aresult
11
00
107 }
108  
109 =item display_dataset()
110  
111 Returns HTML rendering of current page in search results, along with navigation links.
112  
113 =cut
114  
115 0 ▶sub display_dataset {
Subroutine display_dataset: not called CC=6 SCAR=37.4
Pod documented
116 0 my ($self) = @_;
117 return ($self->{-optional_header}||'')
118 . $self->extra_vars_for_form()
119 . '<div id="'.($self->{-css_dataset_container_id} || 'searchWidgetContainerId').'">'
120 . $self->display_pager_links(1, 0)
121 . '<table id="'.($self->{-css_table_id} || 'searchWidgetTableId').'" class="'.($self->{-css_table_class} || 'searchWidgetTableTable').'">'
122 . '<thead>'.$self->{'header_html'}.'</thead>'
123 0 ▶ . '<tbody>'.join('', @{ $self->{'dataset_rows_html'} }).'</tbody>'
Condition0%0/2
$self->{'-optional_footer'} || ''
l!l
00
MC/DC0%0/1
$self->{'-optional_footer'} || ''
$self->{'-optional_footer'}
Truth table0%0/2
A
A= $self->{'-optional_footer'}
Aresult
11
00
124 . '</table>'
125 . $self->display_pager_links(0, 1)
126 . '</div>'
127 0 ▶ . ($self->{-optional_footer}||'');
Condition0%0/8
$self->{'-optional_header'} || ''
l!l
00
$self->{'-css_dataset_container_id'} || 'searchWidgetContainerId'
l!l
00
$self->{'-css_table_id'} || 'searchWidgetTableId'
l!l
00
$self->{'-css_table_class'} || 'searchWidgetTableTable'
l!l
00
MC/DC0%0/1
$self->{'-optional_header'} || ''
$self->{'-optional_header'}
MC/DC0%0/1
$self->{'-css_dataset_container_id'} || 'searchWidgetContainerId'
$self->{'-css_dataset_container_id'}
MC/DC0%0/1
$self->{'-css_table_id'} || 'searchWidgetTableId'
$self->{'-css_table_id'}
MC/DC0%0/1
$self->{'-css_table_class'} || 'searchWidgetTableTable'
$self->{'-css_table_class'}
Truth table0%0/2
A
A= $self->{'-optional_header'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'-css_dataset_container_id'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'-css_table_id'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'-css_table_class'}
Aresult
11
00
128 }
129  
130 =item display_row( $row )
131  
132 Returns HTML rendering of given $row in dataset: '<tr> ... </tr>'.
133 Calls display_field($row, $header_col) for each header column.
134  
135 =cut
136  
137 0 ▶sub display_row {
Subroutine display_row: not called CC=11 SCAR=48.8
Pod documented
138 0 my ($self, $row) = @_;
139 0 $self->{'_row_index'}++;
140 0 ▶ if (! $self->{-skip_table_row_color} && $self->TABLE_BGCOLOR1 && $self->TABLE_BGCOLOR2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"-skip_table_row_color"} and $self->TABLE_BGCOLOR1 and $self->TABLE_BGCOLOR2) 0 0 0
Condition0%0/6
not $self->{"-skip_table_row_color"} and $self->TABLE_BGCOLOR1 and $self->TABLE_BGCOLOR2
!ll&&!rl&&r
000
not $self->{"-skip_table_row_color"} and $self->TABLE_BGCOLOR1
!ll&&!rl&&r
000
MC/DC0%0/3
not $self->{"-skip_table_row_color"} and $self->TABLE_BGCOLOR1 and $self->TABLE_BGCOLOR2
not $self->{"-skip_table_row_color"} $self->TABLE_BGCOLOR1 $self->TABLE_BGCOLOR2
Truth table0%0/4
not(A) and B and C
A= not $self->{"-skip_table_row_color"}B= $self->TABLE_BGCOLOR1C= $self->TABLE_BGCOLOR2
ABCresult
0XX0
10X0
1100
1111
141 0 ▶ $self->{'_row_bgcolor'} = ($self->{'_row_bgcolor'}||'') eq $self->TABLE_BGCOLOR2
Branch: 1 branch
BranchTrueFalseTotal
($self->{'_row_bgcolor'} || '') eq $self->TABLE_BGCOLOR2 ? : 0 0 0
Condition0%0/2
$self->{'_row_bgcolor'} || ''
l!l
00
MC/DC0%0/1
$self->{'_row_bgcolor'} || ''
$self->{'_row_bgcolor'}
Truth table0%0/2
A
A= $self->{'_row_bgcolor'}
Aresult
11
00
142 ? $self->TABLE_BGCOLOR1 : $self->TABLE_BGCOLOR2; # toggle color
143 }
144 0 ▶ my $base_class = $self->{-css_table_row_class} || 'searchWidgetTableRow';
Condition0%0/2
$self->{'-css_table_row_class'} || 'searchWidgetTableRow'
l!l
00
MC/DC0%0/1
$self->{'-css_table_row_class'} || 'searchWidgetTableRow'
$self->{'-css_table_row_class'}
Truth table0%0/2
A
A= $self->{'-css_table_row_class'}
Aresult
11
00
145 # my %extra_attr = %{ $self->{-css_table_row_extra_attr}||{} };
146 0 ▶ my %extra_attr = %{ $self->get_option_value('-css_table_row_extra_attr', [$row]) || {} };
Condition0%0/2
$self->get_option_value('-css_table_row_extra_attr', [$row]) || {}
l!l
00
MC/DC0%0/1
$self->get_option_value('-css_table_row_extra_attr', [$row]) || {}
$self->get_option_value('-css_table_row_extra_attr', [$row])
Truth table0%0/2
A
A= $self->get_option_value('-css_table_row_extra_attr', [$row])
Aresult
11
00
147 0 my $extra_class = delete $extra_attr{-class}; delete $extra_attr{class};
148 0 $extra_class =~ s/\s*$base_class\s*//;
149 0 delete $extra_attr{-style}; delete $extra_attr{style};
150  
151 return '<tr class="'.$base_class.($extra_class ? ' '.$extra_class : '')
152 .' '.($self->{'_row_index'} % 2 == 0 ? 'evenRow' : 'oddRow').'"'
153 .($self->{'_row_bgcolor'} ? ' style="background-color: '.$self->{'_row_bgcolor'}.';"' : '')
154 .' '.join(' ', map {
155 0 my $attr = $_; $attr =~ s/^-//;
156 0 my $val = $extra_attr{$_};
157 0 $val =~ s/\$rowindex/ $self->{'_row_index'} /ge;
158 0 $attr.'="'.$val.'"';
159 } keys %extra_attr)
160 0 ▶ .'>'.join('', map { $self->display_field($row, $_) } @{ $self->{'header_columns'} }).'</tr>';
Branch: 3 branches
BranchTrueFalseTotal
$extra_class ? : 0 0 0
$self->{'_row_index'} % 2 == 0 ? : 0 0 0
$self->{'_row_bgcolor'} ? : 0 0 0
161 }
162  
163 =item display_field( $row, $col )
164  
165 Returns HTML rendering of given $row / $col in dataset: '<td> ... </td>'.
166 Calls display_record($row, $col), inherited from L<CGI::Widget::DBI::Search::AbstractDisplay> for the cell contents.
167  
168 =cut
169  
170 0 ▶sub display_field {
Subroutine display_field: not called CC=6 SCAR=37.4
Pod documented
171 0 my ($self, $row, $col) = @_;
172 0 ▶ my %extra_attributes = %{ $self->get_option_value('-extra_table_cell_attributes', [$row, $col]) || {} };
Condition0%0/2
$self->get_option_value('-extra_table_cell_attributes', [$row, $col]) || {}
l!l
00
MC/DC0%0/1
$self->get_option_value('-extra_table_cell_attributes', [$row, $col]) || {}
$self->get_option_value('-extra_table_cell_attributes', [$row, $col])
Truth table0%0/2
A
A= $self->get_option_value('-extra_table_cell_attributes', [$row, $col])
Aresult
11
00
173  
174 my $align = $self->{-column_align}->{$col} ||
175 0 ▶ ($self->{-numeric_columns}->{$col} || $self->{-currency_columns}->{$col} ? 'right' : 'left');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'-numeric_columns'}{$col} || $self->{'-currency_columns'}{$col} ? : 0 0 0
Condition0%0/6
$self->{'-column_align'}{$col} || ($self->{'-numeric_columns'}{$col} || $self->{'-currency_columns'}{$col} ? 'right' : 'left')
l!l&&r!l&&!r
000
$self->{"-numeric_columns"}{$col} or $self->{"-currency_columns"}{$col}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{'-column_align'}{$col} || ($self->{'-numeric_columns'}{$col} || $self->{'-currency_columns'}{$col} ? 'right' : 'left')
$self->{'-column_align'}{$col} $self->{"-numeric_columns"}{$col} $self->{"-currency_columns"}{$col}
Truth table0%0/4
A || B or C
A= $self->{'-column_align'}{$col}B= $self->{"-numeric_columns"}{$col}C= $self->{"-currency_columns"}{$col}
ABCresult
1XX1
01X1
0011
0000
176 return $self->{q}->td(
177 0 ▶ {-class => $self->{-css_table_cell_class} || 'searchWidgetTableCell', -align => $align, %extra_attributes},
Condition0%0/2
$self->{'-css_table_cell_class'} || 'searchWidgetTableCell'
l!l
00
MC/DC0%0/1
$self->{'-css_table_cell_class'} || 'searchWidgetTableCell'
$self->{'-css_table_cell_class'}
Truth table0%0/2
A
A= $self->{'-css_table_cell_class'}
Aresult
11
00
178 $self->display_record($row, $col)
179 );
180 }
181  
182  
183 3 1;
184 __END__
« Search.pm ↑ Summary Search/AbstractDisplay.pm »