Browse.pm

filter: Statementstmt 8.9% 23 / 257 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 80 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 21.2% 7 / 33 Podpod 52.9% 9 / 17 totaltotal 7.5% 39 / 516 CC 76 SCAR 39.4
CC
76
Cov
5%
add_breadcrumbs_to_header
182.0
display_results
110.0
configure_grid_or_table_view
56.0
CRAP 51.2
↑ Summary TEST/Browse.pm »
1 package CGI::Widget::DBI::Browse;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use base qw/ CGI::Widget::DBI::Search::Base /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw/ $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $CGI::Widget::DBI::Browse::VERSION = '0.18';
8  
9 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use CGI::Widget::DBI::Search 0.31;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape qw/uri_escape uri_escape_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #use constant DEFAULT_CACHE_EXPIRATION => '30 days'; # SQL interval format
15  
16 =head1 NAME
17  
18 CGI::Widget::DBI::Browse - Database browsing widget
19  
20 =head1 SYNOPSIS
21  
22 use CGI;
23 use CGI::Widget::DBI::Browse;
24  
25 my $q = CGI->new;
26 my $wb = CGI::Widget::DBI::Browse->new(
27 q => $q,
28 # database connection info
29 -dbi_connect_dsn => 'DBI:Pg:dbname=my_pg_database;host=localhost',
30 -dbi_user => 'pguser',
31 -dbi_pass => 'pgpass',
32 # database schema info
33 -sql_table => 'table1 t1 inner join table2 t2 using (key_col)',
34 -sql_retrieve_columns => [ qw/t1.name t1.description t2.extra_information/ ],
35 );
36 # or if you already have a search widget, you can use the settings from it
37 # my $wb = CGI::Widget::DBI::Browse->new(ws => $search_widget_obj);
38  
39 # list of category columns (how to represent data as a tree)
40 $wb->{-category_columns} = [ qw/main_category sub_category1 sub_category2/ ];
41  
42 # output to browser
43 print $q->header;
44 print $q->start_html;
45  
46 # show current page in tree
47 # (default is a list of distinct values in the first category_column)
48 print $wb->display_results();
49  
50 print $q->end_html;
51  
52  
53 =head1 DESCRIPTION
54  
55 Implements user-interface for browsing a database table or joined tables.
56 Just provide a list of hierarchical categories for your data, and it allows
57 the user to walk down the category tree to find the database records they
58 want.
59  
60 Specifically, this module simply makes successive calls to the search widget
61 module (L<CGI::Widget::DBI::Search>) filtering results by the category of
62 records the user is currently viewing (node in a tree representation of
63 your data).
64  
65 =head1 CONSTRUCTOR
66  
67 =over 4
68  
69 =item new(@config_options)
70  
71 Creates and initializes a new CGI::Widget::DBI::Browse object.
72  
73 =head2 Possible configuration options:
74  
75 =item Database connection options
76  
77 -dbi_connect_dsn => DBI data source name (full connection string)
78 -dbi_user => database username
79 -dbi_pass => database password
80 -dbi_host => host to connect to database (overridden by -dbi_connect_dsn)
81 -sql_database => database to connect to (overridden by -dbi_connect_dsn)
82  
83 =item Database retrieval options
84  
85 -sql_table => Database table(s) to query,
86 -sql_table_columns => [ARRAY] List of all columns in sql_table,
87 -sql_retrieve_columns => [ARRAY] List of columns for retrieval,
88  
89 ... Also accepts all the options that CGI::Widget::DBI::Search does;
90 they are simply passed through ...
91  
92 =item Schema description configuration
93  
94 -category_columns => [ARRAY] List of columns to be used as "categories,"
95 or internal nodes, allowing hierarchical navigation of
96 a tree-based representation of your data
97 -category_sql_retrieve_columns
98 => {HASH} Extra columns to be retrieved for a given category
99 column. Keys should be a subset of -category_columns, and
100 values should be an arrayref containing the extra columns
101 (or column aliases) to retrieve for that category column.
102 Typically used in conjunction with -category_column_closures.
103 -category_column_closures
104 => {HASH} of (CODE): Reference to a hash containing a code
105 reference for each category column which should be passed
106 through before displaying. The content returned by the closure
107 is displayed inside the <a/> navigation tag for the category.
108 Similar to the -columndata_closures option of CGI::Widget::DBI::Search.
109 -category_before_link_closures
110 => {HASH} of (CODE): Reference to a hash containing a code
111 reference for each category column which is called to render
112 content to display before the <a/> navigation tag for the category.
113  
114 =item Search result display options
115  
116 -max_category_results_per_page
117 => Maximum number of database records to display
118 on a page while navigating by category (default: 100)
119 -skip_to_results => If true, skips directly to display of database records
120 while in category browsing mode
121 -auto_skip_to_results => If set, skips directly to results if in category browsing mode
122 but there are no category members at the current level, or if
123 there is exactly one category member with value '' (empty string).
124 Note: this will have no effect if the search widget ('ws' object)
125 is not set to -show_total_numresults.
126 -post_auto_skip_callback
127 => (CODE): If set along with -auto_skip_to_results, and auto-skip
128 mode is in effect, this callback routine will be called before
129 the actual search and displaying results are executed. Useful
130 when behavior of the widget is dependent on whether we are
131 browsing or not. (i.e. the return value of is_browsing())
132 -default_grid_layout => If true, defaults to -display_mode => 'grid' mode for viewing
133 results, with link to switch to 'table' mode.
134  
135 =item Performance options
136  
137 -cache_categories => Turn on automatic caching of categories. Creates a table
138 called browse_widget_category_cache which stores records
139 for each internal node in the browse tree.
140 BEWARE: currently there is no cache expiration mechanism;
141 also, the the cache table is a hard-coded name, so be careful
142 if you use more than one browse widget in the same database.
143  
144 =back
145  
146 =cut
147  
148 0 ▶sub initialize {
Subroutine initialize: not called CC=4 SCAR=30.0
Pod undocumented
149 0 my ($self) = @_;
150 0 ▶ if (blessed($self->{ws}) && $self->{ws}->isa('CGI::Widget::DBI::Search')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self->{'ws'}) and $self->{'ws'}->isa('CGI::Widget::DBI::Search')) { } 0 0 0
Condition0%0/3
builtin::blessed($self->{"ws"}) and $self->{"ws"}->isa("CGI::Widget::DBI::Search")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self->{"ws"}) and $self->{"ws"}->isa("CGI::Widget::DBI::Search")
builtin::blessed($self->{"ws"}) $self->{"ws"}->isa("CGI::Widget::DBI::Search")
Truth table0%0/3
A and B
A= builtin::blessed($self->{"ws"})B= $self->{"ws"}->isa("CGI::Widget::DBI::Search")
ABresult
0X0
100
111
151 0 $self->{q} = $self->{ws}->{q};
152 } else {
153 0 $self->{ws} = CGI::Widget::DBI::Search->new($self);
154 # search results shown in grid format by default, when using browse widget
155 0 ▶ $self->{ws}->{-display_mode} ||= 'grid';
Condition0%0/2
$self->{"ws"}{"-display_mode"} ||= "grid"
l!l
00
MC/DC0%0/1
$self->{"ws"}{"-display_mode"} ||= "grid"
$self->{"ws"}{"-display_mode"}
Truth table0%0/2
A
A= $self->{"ws"}{"-display_mode"}
Aresult
11
00
156 }
157 }
158  
159 0 ▶sub _break_circular_references {
Subroutine _break_circular_references: not called CC=1 SCAR=6.9
160 0 my ($self) = @_;
161 0 delete $self->{old_ws}->{display}->{b};
162 0 delete $self->{ws}->{display}->{b};
163 }
164  
165 =head1 METHODS
166  
167 =over 4
168  
169 =item is_browsing()
170  
171 Returns true if the widget is in browse mode, viewing navigable categories (branch nodes)
172 rather than viewing actual results (leaf nodes). Returns the actual category column name
173 currently browsing on.
174  
175 =item parent_category_column()
176  
177 Returns the previous category column in -category_columns relative to the node currently
178 being viewed. If we are at the top-level category (root node in the browse tree), then
179 this method returns undef.
180  
181 =item ancestor_category_columns()
182  
183 Returns a list of all previous category columns in -category_columns relative to the
184 current node. If we are the root node, an empty list is returned.
185  
186 =item display_results()
187  
188 Displays results from CGI::Widget::DBI::Search (in grid display mode) of data values
189 specified by -category_columns and current state of query object in object variable 'q'.
190  
191 =cut
192  
193 0 ▶sub is_browsing {
Subroutine is_browsing: not called CC=3 SCAR=24.8
Pod documented
194 0 my ($self) = @_;
195 0 ▶ $self->_set_category_index() if ! defined $self->{_category_idx};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_category_idx"} 0 0 0
196 0 ▶ return $self->{-category_columns}->[ $self->{_category_idx} ] if $self->_is_browsing();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_browsing 0 0 0
197 0 return undef;
198 }
199  
200 0 ▶sub _is_browsing {
Subroutine _is_browsing: not called CC=3 SCAR=24.8
201 0 my ($self) = @_;
202 0 ▶ return defined($self->{_category_idx}) && ! $self->{-skip_to_results} && ! $self->{q}->param('_browse_skip_to_results_');
Condition0%0/6
defined $self->{'_category_idx'} && !$self->{'-skip_to_results'} && !$self->{'q'}->param('_browse_skip_to_results_')
!ll&&!rl&&r
000
defined $self->{"_category_idx"} and not $self->{"-skip_to_results"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->{'_category_idx'} && !$self->{'-skip_to_results'} && !$self->{'q'}->param('_browse_skip_to_results_')
defined $self->{"_category_idx"} not $self->{"-skip_to_results"} !$self->{'q'}->param('_browse_skip_to_results_')
Truth table0%0/4
A and not(B) && not(C)
A= defined $self->{"_category_idx"}B= not $self->{"-skip_to_results"}C= !$self->{'q'}->param('_browse_skip_to_results_')
ABCresult
0XX0
10X0
1100
1111
203 }
204  
205 0 ▶sub parent_category_column {
Subroutine parent_category_column: not called CC=5 SCAR=34.0
Pod documented
206 0 my ($self) = @_;
207 0 ▶ $self->_set_category_index() if ! defined $self->{_category_idx};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_category_idx"} 0 0 0
208 0 ▶ return $self->{-category_columns}->[ -1 ] if !( $self->_is_browsing() || $self->{'auto_skip_in_effect'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_browsing or $self->{"auto_skip_in_effect"} 0 0 0
Condition0%0/3
$self->_is_browsing or $self->{"auto_skip_in_effect"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_is_browsing or $self->{"auto_skip_in_effect"}
$self->_is_browsing $self->{"auto_skip_in_effect"}
Truth table0%0/3
A or B
A= $self->_is_browsingB= $self->{"auto_skip_in_effect"}
ABresult
1X1
011
000
209 0 ▶ return undef if $self->{_category_idx} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_category_idx"} == 0 0 0 0
210 0 return $self->{-category_columns}->[ $self->{_category_idx} - 1 ];
211 }
212  
213 0 ▶sub ancestor_category_columns {
Subroutine ancestor_category_columns: not called CC=4 SCAR=30.0
Pod documented
214 0 my ($self) = @_;
215 0 ▶ $self->_set_category_index() if ! defined $self->{_category_idx};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_category_idx"} 0 0 0
216 my $end_idx = $self->_is_browsing() || $self->{'auto_skip_in_effect'}
217 0 ▶ ? $self->{_category_idx} - 1 : $#{ $self->{-category_columns} };
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_browsing || $self->{'auto_skip_in_effect'} ? : 0 0 0
Condition0%0/3
$self->_is_browsing or $self->{"auto_skip_in_effect"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_is_browsing or $self->{"auto_skip_in_effect"}
$self->_is_browsing $self->{"auto_skip_in_effect"}
Truth table0%0/3
A or B
A= $self->_is_browsingB= $self->{"auto_skip_in_effect"}
ABresult
1X1
011
000
218 0 return @{ $self->{-category_columns} }[0 .. $end_idx];
219 }
220  
221 0 ▶sub _set_category_index {
Subroutine _set_category_index: not called CC=3 SCAR=24.8
222 0 my ($self) = @_;
223 0 my $q = $self->{q};
224  
225 0 my @categories = @{ $self->{-category_columns} };
226 0 foreach my $i (0 .. $#categories) {
227 0 ▶ if (! $q->param($categories[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($q->param($categories[$i])) 0 0 0
228 0 $self->{_category_idx} = $i;
229 0 last;
230 }
231 }
232 }
233  
234 0 ▶sub configure_search_for_category_browse {
Subroutine configure_search_for_category_browse: not called CC=5 SCAR=34.0
Pod undocumented
235 0 my ($self) = @_;
236 0 my $ws = $self->{ws};
237 0 my $category_column = $self->is_browsing();
238  
239 # first, save search widget object in case we need it back, if -auto_skip_to_results is on
240 0 ▶ $self->{old_ws} = $self->{ws}->new( $self->{ws} ) if $self->{-auto_skip_to_results};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-auto_skip_to_results"} 0 0 0
241  
242 0 $ws->{-display_class} = undef; # need to clear in case it was manually set
243 0 $ws->{-display_mode} = 'grid';
244  
245 # display a list of (sub)categories/branch nodes instead of items/leaf nodes
246 0 $ws->{-opt_precols_sql} = 'DISTINCT';
247  
248 0 my @extra_category_retr_cols = map { $ws->full_sql_column_for_alias($_) }
249 0 ▶ @{ $self->{-category_sql_retrieve_columns}->{$category_column} || [] };
Condition0%0/2
$self->{'-category_sql_retrieve_columns'}{$category_column} || []
l!l
00
MC/DC0%0/1
$self->{'-category_sql_retrieve_columns'}{$category_column} || []
$self->{'-category_sql_retrieve_columns'}{$category_column}
Truth table0%0/2
A
A= $self->{'-category_sql_retrieve_columns'}{$category_column}
Aresult
11
00
250 0 $ws->{-sql_search_columns} = undef;
251 $ws->{-sql_retrieve_columns} = [
252 0 q|COALESCE(|.$ws->sql_column_with_tbl_alias_for_alias($category_column).q|, '') AS |.$category_column, @extra_category_retr_cols,
253 ];
254 $ws->{-display_columns} = {
255 0 ▶ $category_column => $ws->{-display_columns}->{$category_column} || $category_column
Condition0%0/3
$ws->{'-display_columns'}{$category_column} || $category_column
l!l&&r!l&&!r
000
MC/DC0%0/2
$ws->{'-display_columns'}{$category_column} || $category_column
$ws->{'-display_columns'}{$category_column} $category_column
Truth table0%0/3
A || B
A= $ws->{'-display_columns'}{$category_column}B= $category_column
ABresult
1X1
011
000
256 };
257 $ws->{-columndata_closures} = {
258 0 $category_column => _category_columndata_closure($self, $category_column)
259 };
260 0 $ws->{-default_orderby_columns} = [ $ws->sql_column_with_tbl_alias_for_alias($category_column) ];
261 0 ▶ $ws->{-max_results_per_page} = $self->{-max_category_results_per_page} || 100;
Condition0%0/2
$self->{'-max_category_results_per_page'} || 100
l!l
00
MC/DC0%0/1
$self->{'-max_category_results_per_page'} || 100
$self->{'-max_category_results_per_page'}
Truth table0%0/2
A
A= $self->{'-max_category_results_per_page'}
Aresult
11
00
262 0 $ws->{-browse_mode} = 1;
263 }
264  
265 =back
266  
267 =head1 CACHING METHODS
268  
269 =over 4
270  
271 =item category_value_is_cached($category, $value)
272  
273 Returns true if cache entries exist in the category cache table for the key/value
274 pair $category/$value.
275  
276 =item cache_results_for_category_value($category, $value)
277  
278 Executes a category search as normal, using CGI::Widget::DBI::Search in -dry_run
279 mode to just generate the SQL, and stores the results in category cache table.
280  
281 =cut
282  
283 0 ▶sub _create_category_cache_table {
Subroutine _create_category_cache_table: not called CC=1 SCAR=6.9
284 0 my ($self) = @_;
285 0 $self->{ws}->{-dbh}->do(<<'END_SQL');
286 CREATE TABLE IF NOT EXISTS browse_widget_category_cache (
287 category_column VARCHAR(32),
288 category_value VARCHAR(96),
289 child_value VARCHAR(96),
290 last_modified TIMESTAMP,
291 UNIQUE INDEX unq_all (category_column, category_value, child_value),
292 INDEX idx_category_value (category_column, category_value)
293 ) ENGINE = MyISAM
294 END_SQL
295 }
296  
297 0 ▶sub _where_clause_for_category_value {
Subroutine _where_clause_for_category_value: not called CC=3 SCAR=24.8
298 0 my ($self, $category, $value) = @_;
299 0 ▶ return 'category_column '.(defined $category ? '=?' : 'IS NULL')
Branch: 2 branches
BranchTrueFalseTotal
defined $category ? : 0 0 0
defined $value ? : 0 0 0
300 .' AND category_value '.(defined $value ? '=?' : 'IS NULL');
301 }
302  
303 0 ▶sub _bind_params_for_category_value {
Subroutine _bind_params_for_category_value: not called CC=3 SCAR=24.8
304 0 my ($self, $category, $value) = @_;
305 0 ▶ return (defined $category ? $category : (), defined $value ? $value : ());
Branch: 2 branches
BranchTrueFalseTotal
defined $category ? : 0 0 0
defined $value ? : 0 0 0
306 }
307  
308 0 ▶sub category_value_is_cached {
Subroutine category_value_is_cached: not called CC=1 SCAR=6.9
Pod documented
309 0 my ($self, $category, $value) = @_;
310 my $sth = $self->{ws}->{-dbh}->prepare_cached(
311 0 'SELECT count(1) FROM browse_widget_category_cache WHERE '
312 . $self->_where_clause_for_category_value($category, $value)
313 );
314 0 $sth->execute($self->_bind_params_for_category_value($category, $value));
315 0 my $cnt = $sth->fetchrow_arrayref()->[0];
316 0 $sth->finish();
317 0 return $cnt;
318 }
319  
320 0 ▶sub cache_results_for_category_value {
Subroutine cache_results_for_category_value: not called CC=1 SCAR=6.9
Pod documented
321 0 my ($self, $category, $value) = @_;
322 0 my $ws = $self->{ws};
323 0 my $category_column = $self->is_browsing();
324  
325 0 local $ws->{-dry_run} = 1;
326 0 local $ws->{-show_total_numresults} = 0;
327  
328 0 $ws->search();
329  
330 0 my $sth = $ws->{-dbh}->prepare_cached(qq{
331 INSERT INTO browse_widget_category_cache (category_column, category_value, child_value)
332 SELECT DISTINCT ? AS category_column, ? AS category_value, search.$category_column AS child_value
333 0 FROM ( @{[ $ws->{'_sql'} ]} ) search
334 });
335 0 $sth->execute($category, $value, @{ $ws->{-bind_params} });
336 0 $sth->finish();
337 }
338  
339 =back
340  
341 =head1 DISPLAY METHODS
342  
343 =over 4
344  
345 =item display_results()
346  
347 Main rendering method of the database browse. Typically this is the only method that actually *needs*
348 to be called from the delegating class, after configuration, since this calls all other methods as necessary.
349  
350 =item display_cached_category_results()
351  
352 Displays a node in the browse tree, identically to how display_results() would, only by using the cache table.
353 Specifically, it configures the search widget by directly injecting data retrieved from the cache table
354 and calling the search widget's display_results() method, skipping the call to its search() method.
355 Also, if there is no cache entry for the current node in the browse tree, this calls cache_results_for_category_value()
356 first to cache the results.
357  
358 Note: this method is called automatically by display_results() if -cache_categories is true.
359  
360 =item add_breadcrumbs_to_header()
361  
362 Appends breadcrumb navigation HTML to the -optional_header of the enclosed search widget object
363 based on the current state of the browse widget. Also sets the 'category_title' object of this
364 browse widget object to a string representation of the current location in the browse tree.
365  
366 Note: this method is called automatically by display_results().
367  
368 =item link_for_category_column($category_col, $row)
369  
370 Emits an href link to the requested $category_col in the browse tree for the category data from
371 $row. This is typically used as a callback from a column's -columndata_closures rendering. E.g.
372  
373 $wb->{ws}->{-columndata_closures}->{'state_or_province'} = sub {
374 my ($obj, $row) = @_;
375 return $obj->{b}->link_for_category_column('state_or_province', $row);
376 };
377  
378 For it to work as expected, all columns listed in -category_columns must also be in the select
379 results, i.e. present in -sql_retrieve_columns.
380  
381 =cut
382  
383 0 ▶sub display_results {
Subroutine display_results: not called CC=10 SCAR=47.0
Pod documented
384 0 my ($self, $override_disp_cols) = @_;
385 0 my $q = $self->{q};
386 0 my $ws = $self->{ws};
387  
388 die 'Required configuration setting -category_columns not set.'
389 0 ▶ if ref $self->{-category_columns} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"-category_columns"} ne "ARRAY" 0 0 0
390  
391 # set category filter condition
392 0 $self->{'filter_columns'} = [ grep($q->param($_), @{ $self->{-category_columns} }) ];
393 0 ▶ if (@{ $self->{'filter_columns'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"filter_columns"};}) 0 0 0
394 $ws->{-where_clause} = ($ws->{-where_clause} ? '('.$ws->{-where_clause}.') AND ' : '').join(
395 0 ▶ ' AND ', map { q|COALESCE(|.$_.q|, '') = ?| } map { $ws->sql_column_with_tbl_alias_for_alias($_) } @{ $self->{'filter_columns'} }
Branch: 1 branch
BranchTrueFalseTotal
$ws->{'-where_clause'} ? : 0 0 0
396 );
397 $ws->{-bind_params} = [
398 0 ▶ @{ $ws->{-bind_params} || [] },
Condition0%0/2
$ws->{'-bind_params'} || []
l!l
00
MC/DC0%0/1
$ws->{'-bind_params'} || []
$ws->{'-bind_params'}
Truth table0%0/2
A
A= $ws->{'-bind_params'}
Aresult
11
00
399 0 ▶ map { my $val = $q->param($_); $val eq '(empty)' ? '' : $val; } @{ $self->{'filter_columns'} },
Branch: 1 branch
BranchTrueFalseTotal
$val eq '(empty)' ? : 0 0 0
400 ];
401  
402 map {
403 0 $ws->{-href_extra_vars}->{$_} = undef;
404 0 $ws->{-form_extra_vars}->{$_} = undef;
405 0 } @{ $self->{'filter_columns'} };
406 }
407  
408 0 my $category_column = $self->is_browsing();
409 0 ▶ if ($category_column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($category_column) 0 0 0
410 0 $self->configure_search_for_category_browse();
411 0 ▶ return $self->display_cached_category_results() if $self->{-cache_categories};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-cache_categories"} 0 0 0
412 }
413  
414 0 $ws->{-href_extra_vars}->{'_browse_skip_to_results_'} = undef;
415 0 $ws->{-form_extra_vars}->{'_browse_skip_to_results_'} = undef;
416  
417 0 $self->add_breadcrumbs_to_header();
418 0 $self->build_results();
419  
420 0 my $html;
421 0 eval {
422 0 ▶ if ($self->{ws}->init_display_class($override_disp_cols)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ws"}->init_display_class($override_disp_cols)) 0 0 0
423 0 $self->{ws}->{display}->{b} = $self; # circular ref, must break with _break_circular_references()
424 0 $html = $self->{ws}->{display}->display();
425 }
426 };
427 0 $self->_break_circular_references(); # must always break ref to avoid memory leak, even if error occurred
428 0 ▶ if ($@) { die $@; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
429 0 return $html;
430 }
431  
432 0 ▶sub build_results {
Subroutine build_results: not called CC=1 SCAR=6.9
Pod undocumented
433 0 my ($self) = @_;
434 0 $self->{ws}->search();
435 0 $self->auto_skip_to_results();
436 }
437  
438 0 ▶sub auto_skip_to_results {
Subroutine auto_skip_to_results: not called CC=6 SCAR=37.4
Pod undocumented
439 0 my ($self) = @_;
440 0 my $ws = $self->{ws};
441 0 my $category_column = $self->is_browsing();
442  
443 0 ▶ if ($self->{-auto_skip_to_results} && $category_column && $ws->{-show_total_numresults} && $ws->{'numresults'} <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-auto_skip_to_results"} and $category_column and $ws->{"-show_total_numresults"} and $ws->{"numresults"} <= 1) 0 0 0
Condition0%0/9
$self->{"-auto_skip_to_results"} and $category_column and $ws->{"-show_total_numresults"} and $ws->{"numresults"} <= 1
!ll&&!rl&&r
000
$self->{"-auto_skip_to_results"} and $category_column and $ws->{"-show_total_numresults"}
!ll&&!rl&&r
000
$self->{"-auto_skip_to_results"} and $category_column
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"-auto_skip_to_results"} and $category_column and $ws->{"-show_total_numresults"} and $ws->{"numresults"} <= 1
$self->{"-auto_skip_to_results"} $category_column $ws->{"-show_total_numresults"} $ws->{"numresults"} <= 1
Truth table0%0/5
A and B and C and D
A= $self->{"-auto_skip_to_results"}B= $category_columnC= $ws->{"-show_total_numresults"}D= $ws->{"numresults"} <= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
444 # skip to results if we are browsing but we reach a category which has just 0 or 1 members
445 0 $self->{ws} = $self->{old_ws};
446 0 $self->{-skip_to_results} = $self->{'auto_skip_in_effect'} = 1;
447 0 $self->re_add_breadcrumbs_to_header();
448 0 ▶ $self->{-post_auto_skip_callback}->($self) if ref $self->{-post_auto_skip_callback} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"-post_auto_skip_callback"} eq "CODE" 0 0 0
449 0 $self->{ws}->search();
450 }
451 }
452  
453 0 ▶sub display_cached_category_results {
Subroutine display_cached_category_results: not called CC=6 SCAR=37.4
Pod documented
454 0 my ($self) = @_;
455 0 my $ws = $self->{ws};
456 0 my $category_column = $self->is_browsing();
457  
458 0 $self->_create_category_cache_table();
459  
460 0 my $parent_category = $self->parent_category_column();
461 0 my $parent_value = $self->decode_utf8(scalar $self->{q}->param($parent_category));
462  
463 0 ▶ if (! $self->category_value_is_cached($parent_category, $parent_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->category_value_is_cached($parent_category, $parent_value)) 0 0 0
464 0 $self->cache_results_for_category_value($parent_category, $parent_value);
465 }
466  
467 0 my $sth = $ws->{-dbh}->prepare_cached(qq|
468 SELECT child_value AS $category_column
469 FROM browse_widget_category_cache
470 0 WHERE @{[ $self->_where_clause_for_category_value($parent_category, $parent_value) ]}
471 ORDER BY $category_column
472 |);
473 0 $sth->execute($self->_bind_params_for_category_value($parent_category, $parent_value));
474  
475 0 $ws->{'results'} = [];
476 0 ▶ while (my $row = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
477 0 push(@{ $ws->{'results'} }, $row);
478 }
479 0 $sth->finish();
480  
481 # extra cost to using -cache_categories with -category_sql_retrieve_columns
482 # - presumably this extra query would be faster than not caching categories at all
483 0 ▶ if (my $extra_cols = $self->{-category_sql_retrieve_columns}->{$category_column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_cols = $self->{"-category_sql_retrieve_columns"}{$category_column}) 0 0 0
484 0 $sth = $ws->{-dbh}->prepare_cached(qq|
485 0 SELECT DISTINCT @{[ join ',', map { $ws->full_sql_column_for_alias($_) } ($category_column, @$extra_cols) ]}
486 0 FROM @{[ $ws->{-sql_table} ]}
487 0 WHERE @{[ $ws->sql_column_with_tbl_alias_for_alias($category_column) ]}
488 0 IN ( @{[ join ',', map {'?'} @{ $ws->{'results'} } ]} )
489 ORDER BY $category_column
490 |);
491 0 $sth->execute(map { $_->{$category_column} } @{ $ws->{'results'} });
492 0 my $i = 0;
493 0 ▶ while (my $row = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
494 0 ▶ $ws->{'results'}->[$i] = { %{ $ws->{'results'}->[$i] || {} }, %$row };
Condition0%0/2
$ws->{'results'}[$i] || {}
l!l
00
MC/DC0%0/1
$ws->{'results'}[$i] || {}
$ws->{'results'}[$i]
Truth table0%0/2
A
A= $ws->{'results'}[$i]
Aresult
11
00
495 0 $i++;
496 }
497 0 $sth->finish();
498 }
499  
500 0 $self->add_breadcrumbs_to_header();
501  
502 0 return $ws->display_results();
503 }
504  
505 0 ▶sub category_title { return shift->{'category_title'} || '' }
Subroutine category_title: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
+(shift())->{'category_title'} || ''
l!l
00
MC/DC0%0/1
+(shift())->{'category_title'} || ''
+(shift())->{'category_title'}
Truth table0%0/2
A
A= +(shift())->{'category_title'}
Aresult
11
00
506  
507 0 ▶sub re_add_breadcrumbs_to_header {
Subroutine re_add_breadcrumbs_to_header: not called CC=1 SCAR=6.9
Pod undocumented
508 0 my ($self) = @_;
509 0 $self->{'_added_breadcrumbs'} = 0;
510 0 $self->add_breadcrumbs_to_header();
511 }
512  
513 0 ▶sub add_breadcrumbs_to_header {
Subroutine add_breadcrumbs_to_header: not called CC=13 SCAR=52.0
Pod documented
514 0 my ($self) = @_;
515 0 ▶ return if $self->{'_added_breadcrumbs'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_added_breadcrumbs"} 0 0 0
516 0 my $q = $self->{q};
517 0 my $ws = $self->{ws};
518  
519 # add breadcrumbs in search header
520 0 my @cume_category_filters;
521  
522 my $extra_vars = $ws->extra_vars_for_uri([
523 0 ▶ @{ $self->{'filter_columns'} }, '_browse_skip_to_results_', @{ $self->{-exclude_vars_from_breadcrumbs}||[] },
Condition0%0/2
$self->{'-exclude_vars_from_breadcrumbs'} || []
l!l
00
MC/DC0%0/1
$self->{'-exclude_vars_from_breadcrumbs'} || []
$self->{'-exclude_vars_from_breadcrumbs'}
Truth table0%0/2
A
A= $self->{'-exclude_vars_from_breadcrumbs'}
Aresult
11
00
524 ]);
525 0 $self->{'category_title'} = '';
526 0 $self->{'breadcrumbs'} = [ 'Top' ];
527 0 $self->{'breadcrumb_links'} = [ '<a href="?'.$extra_vars.'" class="breadcrumbNavLink" id="breadcrumbNavLink-Top"><span>'.$ws->translate('Top').'</span></a>' ];
528  
529 0 foreach (@{ $self->{'filter_columns'} }) {
530 0 my $breadcrumb_name = $self->decode_utf8(scalar $q->param($_));
531 0 ▶ $self->{'category_title'} = join(' > ', $self->{'category_title'} || (), $breadcrumb_name || ());
Condition0%0/6
$self->{'category_title'} || ()
l!l&&r!l&&!r
000
$breadcrumb_name || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'category_title'} || ()
$self->{'category_title'} ()
MC/DC0%0/2
$breadcrumb_name || ()
$breadcrumb_name ()
Truth table0%0/3
A || B
A= $self->{'category_title'}B= ()
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $breadcrumb_nameB= ()
ABresult
1X1
011
000
532 0 push(@cume_category_filters, _uri_param_pair($_, $breadcrumb_name));
533 0 push(@{ $self->{'breadcrumbs'} }, $breadcrumb_name);
534 0 ▶ push(@{ $self->{'breadcrumb_links'} },
Condition0%0/3
$extra_vars || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$extra_vars || ()
$extra_vars ()
Truth table0%0/3
A || B
A= $extra_varsB= ()
ABresult
1X1
011
000
535 '<a href="?'.join('&amp;', @cume_category_filters, $extra_vars||()).'" class="breadcrumbNavLink" id="breadcrumbNavLink-'.CGI::escapeHTML($breadcrumb_name).'"><span>'.$breadcrumb_name.'</span></a>');
536 }
537  
538 0 my $skip_to_results = '';
539 0 ▶ if ($self->is_browsing() && ! $self->{-skip_to_results}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->is_browsing and not $self->{'-skip_to_results'}) { } 0 0 0
elsif ($q->param('_browse_skip_to_results_')) { } 0 0 0
elsif (not $self->{'-skip_to_results'}) { } 0 0 0
Condition0%0/3
$self->is_browsing and not $self->{"-skip_to_results"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_browsing and not $self->{"-skip_to_results"}
$self->is_browsing not $self->{"-skip_to_results"}
Truth table0%0/3
A and not(B)
A= $self->is_browsingB= not $self->{"-skip_to_results"}
ABresult
0X0
100
111
540 0 ▶ $skip_to_results = '&nbsp;&nbsp;&nbsp;&nbsp;<i> &rarr; <a href="?'.join(
Condition0%0/3
$extra_vars || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$extra_vars || ()
$extra_vars ()
Truth table0%0/3
A || B
A= $extra_varsB= ()
ABresult
1X1
011
000
541 '&amp;', @cume_category_filters, '_browse_skip_to_results_=1', $extra_vars||(),
542 ).'" class="breadcrumbNavLink" id="skipToResultsLink"><span>'.$ws->translate('Show all items in this category').'</span></a></i>';
543 } elsif ($q->param('_browse_skip_to_results_')) {
544 0 $self->{'category_title'} .= ' ('.$ws->translate('all results').')';
545 } elsif (! $self->{-skip_to_results}) {
546 # -skip_to_results typically used for searching, so don't add anything to the title
547 0 $self->{'category_title'} .= ' ('.$ws->translate('results').')';
548 }
549  
550 0 ▶ $self->configure_grid_or_table_view() if ! $self->is_browsing();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_browsing 0 0 0
551 0 $ws->{-optional_header} .= '<div id="breadcrumbNavDiv">'.join('&nbsp;&gt;&nbsp;', @{ $self->{'breadcrumb_links'} }).$skip_to_results.'</div>'
552 0 . '<div class="categoryContentDiv" id="categoryContentDiv-'.join('__', @{ $self->{'breadcrumbs'} }).'"></div>'; # available for CSS-configured content
553  
554 0 $self->{'_added_breadcrumbs'} = 1;
555 }
556  
557 0 ▶sub grid_layout_enabled {
Subroutine grid_layout_enabled: not called CC=4 SCAR=30.0
Pod undocumented
558 0 my ($self) = @_;
559 return ! $self->{-default_grid_layout} && $self->{q}->param('grid_layout')
560 0 ▶ || $self->{-default_grid_layout} && ! $self->{q}->param('no_grid_layout');
Condition0%0/9
!$self->{'-default_grid_layout'} && $self->{'q'}->param('grid_layout') || $self->{'-default_grid_layout'} && !$self->{'q'}->param('no_grid_layout')
l!l&&r!l&&!r
000
not $self->{"-default_grid_layout"} and $self->{"q"}->param("grid_layout")
!ll&&!rl&&r
000
$self->{"-default_grid_layout"} and not $self->{"q"}->param("no_grid_layout")
!ll&&!rl&&r
000
MC/DC0%0/4
!$self->{'-default_grid_layout'} && $self->{'q'}->param('grid_layout') || $self->{'-default_grid_layout'} && !$self->{'q'}->param('no_grid_layout')
not $self->{"-default_grid_layout"} $self->{"q"}->param("grid_layout") $self->{"-default_grid_layout"} not $self->{"q"}->param("no_grid_layout")
Truth table0%0/7
not(A) and B || C and not(D)
A= not $self->{"-default_grid_layout"}B= $self->{"q"}->param("grid_layout")C= $self->{"-default_grid_layout"}D= not $self->{"q"}->param("no_grid_layout")
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
561 }
562  
563 0 ▶sub configure_grid_or_table_view {
Subroutine configure_grid_or_table_view: not called CC=7 SCAR=40.3
Pod undocumented
564 0 my ($self) = @_;
565 0 my $ws = $self->{ws};
566  
567 0 my $grid_layout_on = $self->grid_layout_enabled();
568 0 ▶ if ($grid_layout_on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grid_layout_on) 0 0 0
569 0 $ws->{-display_mode} = 'grid';
570 0 ▶ $ws->{-grid_columns} ||= 4; # TODO: should call _set_display_defaults() somehow, but can't from outside $ws->display_results()
Condition0%0/2
$ws->{"-grid_columns"} ||= 4
l!l
00
MC/DC0%0/1
$ws->{"-grid_columns"} ||= 4
$ws->{"-grid_columns"}
Truth table0%0/2
A
A= $ws->{"-grid_columns"}
Aresult
11
00
571 0 ▶ if ($ws->{-max_results_per_page} % $ws->{-grid_columns} != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ws->{"-max_results_per_page"} % $ws->{"-grid_columns"} != 0) 0 0 0
572 # logic to round -max_results_per_page up to next integer large enough to be divisible by -grid_columns
573 0 $ws->{-max_results_per_page} = (int($ws->{-max_results_per_page} / $ws->{-grid_columns}) + 1) * $ws->{-grid_columns};
574 }
575 }
576  
577 0 my $extra_vars = $ws->extra_vars_for_uri([ qw/grid_layout no_grid_layout/ ]);
578 $ws->{-optional_header} .= '<div id="gridTableViewLinkDiv" style="float: right;">'.$ws->translate('view').': '
579 . ($grid_layout_on
580 ? '<a href="?'.($self->{-default_grid_layout} ? 'no_grid_layout=1&amp;' : '').$extra_vars.'">'.$ws->translate('table').'</a> | <b>'.$ws->translate('grid').'</b>'
581 0 ▶ : '<b>'.$ws->translate('table').'</b> | <a href="?'.($self->{-default_grid_layout} ? '' : 'grid_layout=1&amp;').$extra_vars.'">'.$ws->translate('grid').'</a>')
Branch: 3 branches
BranchTrueFalseTotal
$grid_layout_on ? : 0 0 0
$self->{'-default_grid_layout'} ? : 0 0 0
$self->{'-default_grid_layout'} ? : 0 0 0
582 . '</div>';
583 }
584  
585 0 ▶sub _uri_param_pair {
Subroutine _uri_param_pair: not called CC=2 SCAR=17.9
586 0 my ($key, $val) = @_;
587 0 ▶ return uri_escape($key).'='.uri_escape_utf8($val||'');
Condition0%0/2
$val || ''
l!l
00
MC/DC0%0/1
$val || ''
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
588 }
589  
590 0 ▶sub link_for_category_column {
Subroutine link_for_category_column: not called CC=5 SCAR=34.0
Pod documented
591 0 my ($self, $category_col, $row, @exclude_params) = @_;
592 0 my (@cols, $col_found);
593 0 foreach (@{ $self->{-category_columns} }) {
594 0 push(@cols, $_);
595 0 ▶ if ($_ eq $category_col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $category_col) 0 0 0
596 0 $col_found = 1;
597 0 last;
598 }
599 }
600 0 my $extra_vars = $self->{ws}->extra_vars_for_uri([ @{ $self->{'filter_columns'} }, '_browse_skip_to_results_', @exclude_params ]);
601 0 my $category_decoded = $self->decode_utf8($row->{$category_col});
602 return $col_found
603 0 ▶ ? '<a href="?'.join('&amp;', (map { _uri_param_pair($_, $self->decode_utf8($row->{$_})) } @cols), $extra_vars || ()).'" id="jumpToCategoryLink">'
Branch: 1 branch
BranchTrueFalseTotal
$col_found ? : 0 0 0
Condition0%0/3
$extra_vars || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$extra_vars || ()
$extra_vars ()
Truth table0%0/3
A || B
A= $extra_varsB= ()
ABresult
1X1
011
000
604 .$category_decoded.'</a>'
605 : $category_decoded;
606 }
607  
608 0 ▶sub _category_columndata_closure {
Subroutine _category_columndata_closure: not called CC=1 SCAR=6.9
609 0 my ($self, $category_col) = @_;
610 my $existing_category_filters =
611 0 join('&amp;', map { _uri_param_pair($_, $self->decode_utf8(scalar $self->{q}->param($_))) } @{ $self->{'filter_columns'} });
612 0 my $before_link_closure = $self->{-category_before_link_closures}->{$category_col};
613 0 my $column_closure = $self->{-category_column_closures}->{$category_col};
614 0 my @filter_columns = @{ $self->{'filter_columns'} };
615 0 ▶ return sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
616 0 my ($sd, $row) = @_;
617 0 ▶ my $category_decoded = $sd->decode_utf8($row->{$category_col}) || '(empty)';
Condition0%0/2
$sd->decode_utf8($row->{$category_col}) || '(empty)'
l!l
00
MC/DC0%0/1
$sd->decode_utf8($row->{$category_col}) || '(empty)'
$sd->decode_utf8($row->{$category_col})
Truth table0%0/2
A
A= $sd->decode_utf8($row->{$category_col})
Aresult
11
00
618 0 ▶ my $category_display_value = ref $column_closure eq 'CODE' ? $column_closure->($sd, $row) : $category_decoded;
Branch: 1 branch
BranchTrueFalseTotal
ref $column_closure eq 'CODE' ? : 0 0 0
619 0 my $extra_vars = $sd->extra_vars_for_uri([ @filter_columns, '_browse_skip_to_results_' ]);
620 0 ▶ return ($before_link_closure ? $before_link_closure->($sd, $row) : '').'<a href="?'.join(
Branch: 1 branch
BranchTrueFalseTotal
$before_link_closure ? : 0 0 0
Condition0%0/6
$existing_category_filters || ()
l!l&&r!l&&!r
000
$extra_vars || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$existing_category_filters || ()
$existing_category_filters ()
MC/DC0%0/2
$extra_vars || ()
$extra_vars ()
Truth table0%0/3
A || B
A= $existing_category_filtersB= ()
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $extra_varsB= ()
ABresult
1X1
011
000
621 '&amp;', $existing_category_filters || (),
622 _uri_param_pair($category_col, $category_decoded),
623 $extra_vars || (),
624 ).'" id="categoryNavLink-'.CGI::escapeHTML($category_decoded).'">'.$category_display_value.'</a>'; # TODO: HTML::Escape is faster than CGI
625 0 };
626 }
627  
628  
629 1 1;
630 __END__
↑ Summary TEST/Browse.pm »