Search/Display/Grid.pm

filter: Statementstmt 12.5% 7 / 56 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 28.5% 2 / 7 Podpod 100% 4 / 4 totaltotal 9.9% 13 / 131 CC 27 SCAR 43.3
CC
27
Cov
7%
display_dataset
110.0
display_cell
90.0
display_sort_popup
72.0
CRAP 76.1
« Search/AbstractDisplay.pm ↑ Summary Search/Base.pm »
1 package CGI::Widget::DBI::Search::Display::Grid;
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 =head1 NAME
8  
9 CGI::Widget::DBI::Search::Display::Grid - Grid display class for Search widget
10  
11 =head1 SYNOPSIS
12  
13 my $ws = CGI::Widget::DBI::Search->new(q => CGI->new);
14 ...
15 $ws->{-display_class} = 'CGI::Widget::DBI::Search::Display::Grid';
16  
17 # or instead, simply:
18 $ws->{-display_mode} = 'grid';
19  
20 =head1 DESCRIPTION
21  
22 This class displays search results retrieved in the search widget in a grid format
23 with each row in the dataset inhabiting its own cell. The dataset can be sorted
24 via a drop-down menu at the upper-right of the grid, and paging links appear at the
25 lower right.
26  
27 =head1 METHODS
28  
29 =over 4
30  
31 =item render_dataset()
32  
33 Builds an HTML table in grid layout for current page in the dataset.
34  
35 Builds data in object variables:
36  
37 dataset_cells_html
38  
39 =cut
40  
41 0 ▶sub render_dataset {
Subroutine render_dataset: not called CC=2 SCAR=17.9
Pod documented
42 0 my ($self) = @_;
43 0 $self->{'dataset_cells_html'} = [];
44  
45 # iterate over most recently returned 'results', which should be a
46 # (possibly blessed) hashref
47 0 foreach my $row (@{$self->{'results'}}) {
48 # build a cell in the grid
49 0 push(@{ $self->{'dataset_cells_html'} }, $self->display_cell($row));
50 }
51 }
52  
53 =item display_cell( $row )
54  
55 Returns an HTML table cell rendering for row $row in the dataset. Called by
56 render_dataset() for each row in the current page of search results.
57  
58 =cut
59  
60 0 ▶sub display_cell {
Subroutine display_cell: not called CC=9 SCAR=45.0
Pod documented
61 0 my ($self, $row) = @_;
62 0 ▶ my %extra_attributes = %{ $self->get_option_value('-extra_grid_cell_attributes', [$row]) || {} };
Condition0%0/2
$self->get_option_value('-extra_grid_cell_attributes', [$row]) || {}
l!l
00
MC/DC0%0/1
$self->get_option_value('-extra_grid_cell_attributes', [$row]) || {}
$self->get_option_value('-extra_grid_cell_attributes', [$row])
Truth table0%0/2
A
A= $self->get_option_value('-extra_grid_cell_attributes', [$row])
Aresult
11
00
63  
64 0 my $cell_html = '';
65 0 foreach my $col (@{ $self->{'header_columns'} }) {
66 0 ▶ next if ! defined $self->{-display_columns}->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-display_columns"}{$col} 0 0 0
67 0 my $record_html = $self->display_record($row, $col);
68 0 ▶ next if ! $record_html;
Branch: 1 branch
BranchTrueFalseTotal
unless $record_html 0 0 0
69 0 ▶ if ($self->{-browse_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-browse_mode"}) 0 0 0
70 0 ▶ $cell_html .= ($cell_html ? '<br/>' : '').$record_html;
Branch: 1 branch
BranchTrueFalseTotal
$cell_html ? : 0 0 0
71 0 next;
72 }
73 0 my $hdr = $self->{-display_columns}->{$col};
74 0 ▶ $hdr .= ': ' if $hdr; # can be empty, which still displays without column heading
Branch: 1 branch
BranchTrueFalseTotal
if $hdr 0 0 0
75 0 $cell_html .= '<div class="column_'.$col.'"><span class="headerLabel">'.$hdr.'</span><span class="cellContent">'.$record_html.'</span></div>';
76 }
77  
78 0 ▶ return $self->{q}->td({-class => $self->{-css_grid_cell_class} || 'searchWidgetGridCell', %extra_attributes}, $cell_html);
Condition0%0/2
$self->{'-css_grid_cell_class'} || 'searchWidgetGridCell'
l!l
00
MC/DC0%0/1
$self->{'-css_grid_cell_class'} || 'searchWidgetGridCell'
$self->{'-css_grid_cell_class'}
Truth table0%0/2
A
A= $self->{'-css_grid_cell_class'}
Aresult
11
00
79 }
80  
81 =item display_dataset()
82  
83 Returns HTML rendering of current page in search results, along with navigation links.
84  
85 =cut
86  
87 0 ▶sub display_dataset {
Subroutine display_dataset: not called CC=10 SCAR=47.0
Pod documented
88 0 my ($self) = @_;
89 0 my @grid_rows;
90 0 foreach my $i (0 .. $#{ $self->{'dataset_cells_html'} }) {
91 0 ▶ if ($i % $self->{-grid_columns} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % $self->{'-grid_columns'} == 0) { } 0 0 0
92 0 push(@grid_rows, $self->{'dataset_cells_html'}->[$i]);
93 } else {
94 0 $grid_rows[-1] .= $self->{'dataset_cells_html'}->[$i];
95 }
96 }
97  
98 return ($self->{-optional_header}||'')
99 . $self->extra_vars_for_form()
100 . '<div id="'.($self->{-css_dataset_container_id} || 'searchWidgetContainerId').'">'
101 . ($self->{-browse_mode}
102 ? $self->display_pager_links(1, 0, 1)
103 : '<div align="right">'.$self->translate('Sort by').': '.$self->display_sort_popup().'</div>'.$self->display_pager_links(1, 0))
104 . '<table id="'.($self->{-css_grid_id} || 'searchWidgetGridId').'" class="'.($self->{-css_grid_class} || 'searchWidgetGridTable').'">'
105 . $self->{q}->Tr([ @grid_rows ])
106 . '</table>'
107 . ($self->{-browse_mode}
108 ? $self->display_pager_links(0, 1, 1)
109 : $self->display_pager_links(0, 1))
110 . '</div>'
111 0 ▶ . ($self->{-optional_footer}||'');
Branch: 2 branches
BranchTrueFalseTotal
$self->{'-browse_mode'} ? : 0 0 0
$self->{'-browse_mode'} ? : 0 0 0
Condition0%0/10
$self->{'-optional_header'} || ''
l!l
00
$self->{'-css_dataset_container_id'} || 'searchWidgetContainerId'
l!l
00
$self->{'-css_grid_id'} || 'searchWidgetGridId'
l!l
00
$self->{'-css_grid_class'} || 'searchWidgetGridTable'
l!l
00
$self->{'-optional_footer'} || ''
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_grid_id'} || 'searchWidgetGridId'
$self->{'-css_grid_id'}
MC/DC0%0/1
$self->{'-css_grid_class'} || 'searchWidgetGridTable'
$self->{'-css_grid_class'}
MC/DC0%0/1
$self->{'-optional_footer'} || ''
$self->{'-optional_footer'}
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_grid_id'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'-css_grid_class'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'-optional_footer'}
Aresult
11
00
112 }
113  
114 =item display_sort_popup()
115  
116 Returns an HTML popup with possible columns to sort dataset by, whose values are the
117 full navigation URIs. An onChange event causes the URI to be loaded, resorting the
118 dataset.
119  
120 =cut
121  
122 0 ▶sub display_sort_popup {
Subroutine display_sort_popup: not called CC=8 SCAR=42.8
Pod documented
123 0 my ($self) = @_;
124 0 my $q = $self->{q};
125  
126 my @sortable_cols = ref $self->{-sortable_columns} eq 'HASH'
127 0 ▶ ? sort { ($self->{-display_columns}->{$a} || $a) cmp ($self->{-display_columns}->{$b} || $b) } keys %{$self->{-sortable_columns}}
Condition0%0/6
$self->{'-display_columns'}{$a} || $a
l!l&&r!l&&!r
000
$self->{'-display_columns'}{$b} || $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'-display_columns'}{$a} || $a
$self->{'-display_columns'}{$a} $a
MC/DC0%0/2
$self->{'-display_columns'}{$b} || $b
$self->{'-display_columns'}{$b} $b
Truth table0%0/3
A || B
A= $self->{'-display_columns'}{$a}B= $a
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $self->{'-display_columns'}{$b}B= $b
ABresult
1X1
011
000
128 0 ▶ : @{$self->{'sql_table_display_columns'}};
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'-sortable_columns'} eq 'HASH' ? : 0 0 0
129 $self->{'sortable_columns'} ||= [
130 0 ▶ map { $self->_column_name($_) } grep { ! $self->{-unsortable_columns}->{$_} } @sortable_cols
Condition0%0/2
$self->{"sortable_columns"} ||= [map({$self->_column_name($_);} grep({not $self->{"-unsortable_columns"}{$_};} @sortable_cols))]
l!l
00
MC/DC0%0/1
$self->{"sortable_columns"} ||= [map({$self->_column_name($_);} grep({not $self->{"-unsortable_columns"}{$_};} @sortable_cols))]
$self->{"sortable_columns"}
Truth table0%0/2
A
A= $self->{"sortable_columns"}
Aresult
11
00
131 ];
132 return $q->popup_menu(
133 -name => 'sortby_columns_popup',
134 # call unescape version of sortby_column_uri() to convert &amp; back to & because CGI will autoEscape() - we can't have double-escaping
135 0 -values => [ '', map { $self->sortby_column_uri($_, 1) } @{ $self->{'sortable_columns'} } ],
136 -labels => {
137 '' => '<'.$self->translate('Sort field').'>',
138 map {
139 0 my $lbl = $self->{-display_columns}->{$_};
140 0 $lbl =~ s|<[^<>]+>||g;
141 0 ▶ $self->sortby_column_uri($_, 1) => $lbl || $_;
Condition0%0/3
$lbl || $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$lbl || $_
$lbl $_
Truth table0%0/3
A || B
A= $lblB= $_
ABresult
1X1
011
000
142 0 ▶ } @{$self->{'sortable_columns'}}
Branch: 2 branches
BranchTrueFalseTotal
$self->{'action_uri_jsfunc'} ? : 0 0 0
$q->param('sortby') ? : 0 0 0
143 },
144 0 -onchange => $self->{'action_uri_jsfunc'} ? 'var code = this.value; eval(code);' : 'if (this.value) window.location=this.value;',
145 -default => $q->param('sortby') ? $self->sortby_column_uri(scalar $q->param('sortby'), 1) : undef,
146 );
147 }
148  
149 =item _set_display_defaults()
150  
151 Sets grid-layout specific default settings in addition to settings in
152 AbstractDisplay.
153  
154 =cut
155  
156 0 ▶sub _set_display_defaults {
Subroutine _set_display_defaults: not called CC=2 SCAR=17.9
157 0 my ($self) = @_;
158 0 $self->SUPER::_set_display_defaults();
159 0 ▶ $self->{-grid_columns} ||= 4;
Condition0%0/2
$self->{"-grid_columns"} ||= 4
l!l
00
MC/DC0%0/1
$self->{"-grid_columns"} ||= 4
$self->{"-grid_columns"}
Truth table0%0/2
A
A= $self->{"-grid_columns"}
Aresult
11
00
160 }
161  
162  
163 3 1;
164 __END__
« Search/AbstractDisplay.pm ↑ Summary Search/Base.pm »