Search/Display/Table.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 50 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 149 CC 13 SCAR 35.3
CC
13
Cov
0%
render_column_headers
72.0
display_row
30.0
display_field
6.0
CRAP 34.2
1 package CGI::Widget::DBI::Search::Display::Table;
2  
3 use strict;
4  
5 use base qw/ CGI::Widget::DBI::Search::AbstractDisplay /;
6  
7 use constant TABLE_HEADER_BGCOLOR => '';
8 # if these are unset, do not toggle bgcolor via html, but can still do it via CSS class {odd,even}Row
9 use constant TABLE_BGCOLOR1 => '#eeeeee';
10 use constant TABLE_BGCOLOR2 => '#ffffff';
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 sub render_dataset {
55 my ($self) = @_;
56 $self->{'dataset_rows_html'} = [];
57  
58 $self->render_column_headers();
59  
60 # iterate over most recently returned 'results', which should be a (possibly blessed) hashref
61 foreach my $row (@{$self->{'results'}}) {
62 # build a table row
63 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 sub render_column_headers {
75 my ($self) = @_;
76 my $q = $self->{q};
77 my $header_th_html = '';
78 my $header_bgcolor = $self->TABLE_HEADER_BGCOLOR;
79 my %sortable_cols = %{ $self->{-sortable_columns} || {} };
80  
81 # build displayed column headers along with sort links and direction arrow
82 foreach my $col (@{ $self->{'header_columns'} }) {
83 my $align = $self->{-column_align}->{$col} ||
84 ($self->{-numeric_columns}->{$col} || $self->{-currency_columns}->{$col} ? 'right' : undef);
85 my %th_attributes = ( $header_bgcolor ? (-bgcolor => $header_bgcolor) : (), -nowrap => 1, $align ? (-align => $align) : () );
86 my %extra_th_attributes = %{ $self->get_option_value('-extra_table_header_cell_attributes', [$col]) || {} };
87 if ($self->{-unsortable_columns}->{$col} || (%sortable_cols && ! $sortable_cols{$col}) ) {
88 $header_th_html .= $self->{q}->th(
89 {-class => $self->{-css_table_unsortable_header_cell_class} || 'searchWidgetTableUnsortableHeaderCell',
90 %th_attributes, %extra_th_attributes},
91 $self->{-display_columns}->{$col},
92 );
93 } else {
94 my $sortby = $self->{'sortby'} && $col eq $self->{'sortby'};
95 $header_th_html .= $self->{q}->th(
96 {-class => $self->{-css_table_header_cell_class} || 'searchWidgetTableHeaderCell',
97 %th_attributes, %extra_th_attributes},
98 ($sortby ? '<b>' : '')
99 .'<a href="'.$self->sortby_column_uri($col).'" title="'.($self->{-column_titles}->{$col} || 'Sort by: '.$self->{-display_columns}->{$col}).'">'
100 .'<span>'.$self->{-display_columns}->{$col}.'</span></a>'
101 .' '.($sortby ? ($self->{'sort_reverse'}->{$col} ? '\\/' :'/\\').'</b>' : '')
102 );
103 }
104 }
105 $self->{'header_html'} = '<tr class="'.($self->{-css_table_header_row_class} || 'searchWidgetTableHeaderRow').'">'.$header_th_html.'</tr>';
106 }
107  
108 =item display_dataset()
109  
110 Returns HTML rendering of current page in search results, along with navigation links.
111  
112 =cut
113  
114 sub display_dataset {
115 my ($self) = @_;
116 return ($self->{-optional_header}||'')
117 . $self->extra_vars_for_form()
118 . $self->display_pager_links(1, 0)
119 . '<table class="'.($self->{-css_table_class} || 'searchWidgetTableTable').'" width="96%">'
120 . '<thead>'.$self->{'header_html'}.'</thead>'
121 . '<tbody>'.join('', @{ $self->{'dataset_rows_html'} }).'</tbody>'
122 . '</table>'
123 . $self->display_pager_links(0, 1)
124 . ($self->{-optional_footer}||'');
125 }
126  
127 =item display_row( $row )
128  
129 Returns HTML rendering of given $row in dataset: '<tr> ... </tr>'.
130 Calls display_field($row, $header_col) for each header column.
131  
132 =cut
133  
134 sub display_row {
135 my ($self, $row) = @_;
136 $self->{'_row_index'}++;
137 if ($self->TABLE_BGCOLOR1 && $self->TABLE_BGCOLOR2) { # toggle color
138 $self->{'_row_bgcolor'} = ($self->{'_row_bgcolor'}||'') eq $self->TABLE_BGCOLOR2
139 ? $self->TABLE_BGCOLOR1
140 : $self->TABLE_BGCOLOR2;
141 }
142  
143 return '<tr class="'.($self->{-css_table_row_class} || 'searchWidgetTableRow').' '
144 .($self->{'_row_index'} % 2 == 0 ? 'evenRow' : 'oddRow').'"'
145 .($self->{'_row_bgcolor'} ? ' style="background-color: '.$self->{'_row_bgcolor'}.';"' : '').'>'
146 .join('', map { $self->display_field($row, $_) } @{ $self->{'header_columns'} })
147 .'</tr>';
148 }
149  
150 =item display_field( $row, $col )
151  
152 Returns HTML rendering of given $row / $col in dataset: '<td> ... </td>'.
153 Calls display_record($row, $col), inherited from L<CGI::Widget::DBI::Search::AbstractDisplay> for the cell contents.
154  
155 =cut
156  
157 sub display_field {
158 my ($self, $row, $col) = @_;
159 my %extra_attributes = %{ $self->get_option_value('-extra_table_cell_attributes', [$row, $col]) || {} };
160  
161 my $align = $self->{-column_align}->{$col} ||
162 ($self->{-numeric_columns}->{$col} || $self->{-currency_columns}->{$col} ? 'right' : 'left');
163 return $self->{q}->td(
164 {-class => $self->{-css_table_cell_class} || 'searchWidgetTableCell', -align => $align, %extra_attributes},
165 $self->display_record($row, $col)
166 );
167 }
168  
169  
170 1;
171 __END__