DBForm/Search/TableList.pm

filter: Statementstmt 12.7% 15 / 118 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 28.5% 4 / 14 Podpod 27.2% 3 / 11 totaltotal 11.2% 22 / 195 CC 32 SCAR 45.4
CC
32
Cov
9%
populate_search
240.0
constrain_results
42.0
get_select
30.0
CRAP 93.2
↑ Summary DBForm.pm »
1 package HTML::DBForm::Search::TableList;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 1 our $VERSION = '1.05';
9  
10 =head1 NAME
11  
12 HTML::DBForm::Search::TableList - Creates a web interface for searching database tables
13  
14 =head1 SYNOPSIS
15  
16 $search = HTML::DBForm::Search->new('tablelist', { column => 'name' });
17
18 $editor->run(search => $search);
19  
20  
21 =head1 INTRODUCTION
22  
23 HTML::DBForm::Search::TableList provides a web interface to search for rows
24 in a database to be updated by HTML::DBForm.
25  
26 =cut
27  
28 =head1 METHODS
29  
30 =over 4
31  
32 =cut
33  
34  
35 =head2 new
36  
37 Constructor inherited from HTML::DBForm::Search
38  
39 takes a scalar indicating the type of search module
40 to create (in this case 'tablelist'), and a list of
41 hash refs designating which columns to display as HTML
42 select form elements, and in which order.
43  
44 Each hash should have one of the following keys:
45 'column', 'columns', or 'sql'. 'column' should be the db
46 column to search, 'columns' should be two db columns, the
47 first of which will be the column to search, and the second of
48 which will be the values to display as option labels. 'sql'
49 can be used to populate the select options with an arbitrary SQL
50 statement. If one column is returned from the SQL statement, then
51 it will be used as choice values and labels. If two columns are
52 returned, then the first will be the specified column value, while
53 the second will be used as option labels.
54  
55  
56 B<Example>
57  
58 $search = HTML::DBForm::Search->new('tablelist',
59 { column => 'category' },
60 { columns => ['id', ' CONCAT(fname, ' ', lname) '] }
61 );
62  
63
64  
65 This would create a two step search, the first screen would be a
66 selection of existing categories, and the next screen would be a
67 selection of names within the chosen categories. When picking
68 columns to display in the search, be aware that the final choice
69 should result in the primary key being chosen.
70  
71
72 B<Example>
73  
74 $search = HTML::DBForm::Search->new('tablelist',
75 { sql => ['id','SELECT id, label FROM table ORDER BY label'] }
76 );
77  
78 This would create a simple one step search.
79  
80 You can use as many hashrefs as needed, each one will generate
81 a new search step, (e.g three hash references will create a three
82 step search). Just keep in mind that the last column chosen must be
83 the column given to DBForm->new() as a primary key.
84  
85 =cut
86  
87  
88 # implementation of this method is required
89 # constructor inherited from Class::Factory
90 # via HTML::DBForm::Search
91  
92  
93 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
94  
95 1 my $self = shift;
96 1 $self->{params} = \@_;
97  
98 1 $self->{html_cols} = [];
99  
100 1 return $self;
101 }
102  
103  
104  
105 # implementation of this method is required
106 # main subroutine called by HTML::DBForm
107  
108 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod undocumented
109  
110 0 my ($self, $editor) = @_;
111  
112 my $tmpl_ref = $self->{'tmpl_file'}
113 0 ▶ ? do { open(FH, "< $self->{'tmpl_file'}"); local $/; <FH> }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tmpl_file'} ? : 0 0 0
114 : &TEMPLATE;
115  
116  
117 0 $self->{template} = HTML::Template->new(
118 scalarref => \$tmpl_ref,
119 die_on_bad_params => 0,
120 loop_context_vars => 1,
121 );
122  
123  
124 0 $self->{editor} = $editor;
125
126 # find out what step we are on
127 0 ▶ $self->{step} = $self->{editor}->{query}->param('step') || 0;
Condition0%0/2
$self->{'editor'}{'query'}->param('step') || 0
l!l
00
MC/DC0%0/1
$self->{'editor'}{'query'}->param('step') || 0
$self->{'editor'}{'query'}->param('step')
Truth table0%0/2
A
A= $self->{'editor'}{'query'}->param('step')
Aresult
11
00
128
129 0 $self->{template}->param(STEP => $self->{step} + 1);
130
131 0 $self->get_choices;
132  
133 return ($self->{editor}->{error}) ?
134 $self->{editor}->{template}->output :
135 0 ▶ $self->{template}->output ;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'editor'}{'error'} ? : 0 0 0
136  
137 }
138  
139  
140 =head2 set_stylesheet
141  
142 Sets an optional css file
143  
144 Takes a scalar holding the path to a stylesheet.
145  
146  
147 B<Example>
148  
149 $search->set_stylesheet('/styles/site_styles.css');
150  
151 =cut
152  
153 0 ▶sub set_stylesheet {
Subroutine set_stylesheet: not called CC=1 SCAR=6.9
Pod documented
154  
155 0 my $self = shift;
156 0 $self->{css} = shift ;
157 }
158  
159  
160  
161 =head2 set_template
162  
163 Sets an optional template file
164  
165 Takes a scalar holding the path to an HTML::Template template.
166 To get a template file to start with, you can do this:
167 perl -MHTML::DBForm::Search::DropDown -e 'print
168 HTML::DBForm::Search::DropDown::TEMPLATE()' > sample.tmpl
169  
170 B<Example>
171  
172 $search->set_template('/www/templates/my.tmpl');
173  
174 =cut
175  
176 0 ▶sub set_template {
Subroutine set_template: not called CC=1 SCAR=6.9
Pod documented
177  
178 0 my $self = shift;
179 0 $self->{tmpl_file} = shift ;
180 }
181  
182  
183  
184  
185 =head2 add_column
186  
187 Adds a new column to your search list table.
188 Only affects the last search screen.
189  
190 Required parameters:
191  
192 I<column> the column that this form element represents
193  
194 Optional parameters:
195  
196 I<label> a label that will appear as the column header.
197 this will default to the name of the column.
198  
199 I<callback> a subroutine reference that will be passed each
200 value for processing before display.
201  
202 B<Examples>
203
204 $editor->add_column( column => 'date' );
205
206 $editor->add_column(
207 column => 'fname',
208 label => 'First Name',
209 callback => sub { ucfirst(shift) }
210 );
211  
212 =cut
213  
214 0 ▶sub add_column {
Subroutine add_column: not called CC=2 SCAR=17.9
Pod documented
215
216 0 my $self = shift;
217  
218 0 ▶ $self->_err_msg("add_column() got an odd number of parameters!")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 0 0
219 unless ((@_ % 2) == 0);
220  
221 0 my %params = @_;
222
223 0 push (@{$self->{td_cols}}, \%params);
224  
225 }
226  
227  
228  
229 # get choices to display
230  
231 0 ▶sub get_choices {
Subroutine get_choices: not called CC=2 SCAR=17.9
Pod undocumented
232  
233 0 my $self = shift;
234  
235 0 ▶ if ($self->{params}->[$self->{step}]->{sql}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'params'}[$self->{'step'}]{'sql'}) { } 0 0 0
236  
237 # use sql parameter
238 $self->populate_search(
239 0 $self->{params}->[$self->{step}]->{sql}->[1]
240 );
241  
242 } else {
243  
244 # generate our own sql
245 $self->populate_search(
246 0 $self->get_select($self->parse_params($self->{step}))
247 );
248 }
249 }
250  
251  
252  
253 # parse search parameters
254
255 0 ▶sub parse_params {
Subroutine parse_params: not called CC=4 SCAR=30.0
Pod undocumented
256  
257 0 my $self = shift;
258 0 my $i = shift;
259  
260 0 my $c_param = $self->{params}->[$i];
261  
262 0 ▶ if ($c_param->{column}){
Branch: 1 branch
BranchTrueFalseTotal
if ($c_param->{"column"}) 0 0 0
263 0 return ($c_param->{column}, $c_param->{column});
264 }
265  
266 0 ▶ if ($c_param->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_param->{"columns"}) 0 0 0
267 0 return ($c_param->{columns}->[0], $c_param->{columns}->[1]);
268 }
269  
270 0 ▶ if ($c_param->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_param->{"sql"}) 0 0 0
271 0 return ($c_param->{sql}->[0], $c_param->{sql}->[1]);
272 }
273 }
274  
275  
276  
277 # build a select statement
278
279 0 ▶sub get_select {
Subroutine get_select: not called CC=5 SCAR=34.0
Pod undocumented
280  
281 0 my $self = shift;
282 0 my ($col1, $col2) = @_;
283  
284 0 my $sql = qq( SELECT DISTINCT $col1, $col2
285 FROM $self->{editor}->{table}
286 );
287  
288 0 ▶ return $sql unless $self->{step};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"step"} 0 0 0
289  
290 0 my (@values, $i);
291
292 0 for my $step(0 .. $self->{step}-1){
293  
294 0 ▶ $sql .= ' WHERE ' unless $i++;
Branch: 1 branch
BranchTrueFalseTotal
unless $i++ 0 0 0
295 0 $sql .= ($self->parse_params($step))[0] ." = ?";
296 0 ▶ $sql .= ' AND ' unless $step >= $self->{step}-1;
Branch: 1 branch
BranchTrueFalseTotal
unless $step >= $self->{"step"} - 1 0 0 0
297  
298 push @values,
299 0 $self->{editor}->{query}->param(($self->parse_params($step))[0]);
300 }
301  
302 0 $sql .= " ORDER BY $col2";
303  
304 # the sql is the first element
305 # the rest of the array is
306 # filled with placeholder vals
307  
308 0 unshift @values, $sql;
309 0 return @values;
310  
311 }
312  
313  
314  
315 # populate search choices
316  
317 0 ▶sub populate_search {
Subroutine populate_search: not called CC=15 SCAR=54.8
Pod undocumented
318  
319 0 my $self = shift;
320 0 my ($sql, @params) = @_;
321 0 my (@tmpl_loop, @headers, $db_return);
322
323 eval {
324 0 $db_return = $self->{editor}->{dbh}->selectall_arrayref($sql,undef,@params);
325 0 ▶ 1 } or $self->{editor}->_err_msg($@, $sql);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $db_return = $self->{"editor"}{"dbh"}->selectall_arrayref($sql, undef, @params); 1 } } or $self->{"editor"}->_err_msg($@, $sql) 0 0 0
326  
327 # is this the last step?
328 0 ▶ my $last_step = (($self->{step}+1) >= scalar(@{$self->{params}})) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'step'} + 1 >= scalar @{$self->{'params'};} ? : 0 0 0
329  
330 0 ▶ my $rm = 'display' if $last_step;
Branch: 1 branch
BranchTrueFalseTotal
if $last_step 0 0 0
331  
332 # is it the first?
333 0 ▶ my $cancel = ($self->{step} > 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'step'} > 0 ? : 0 0 0
334  
335  
336  
337 # workaround for servers that lack
338 # subqueries ( e.g mysql < 4.1 )
339  
340 0 ▶ if ($self->{params}->[$self->{step}]->{sql}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"}[$self->{"step"}]{"sql"}) 0 0 0
341 0 $db_return = $self->constrain_results($db_return);
342 };
343  
344  
345 0 for my $row_ref(@$db_return){
346 my %row = (
347 VALUE => $row_ref->[0],
348 LABEL => $row_ref->[1],
349 0 RADIO_NAME => ($self->parse_params($self->{step}))[0],
350 );
351  
352 # if final screen, list any
353 # additional columns specified
354
355 0 ▶ if ($last_step){
Branch: 1 branch
BranchTrueFalseTotal
if ($last_step) 0 0 0
356 0 my @td_loop;
357
358 0 for my $col_hash(@{$self->{td_cols}}){
359 0 my $sql = qq( SELECT $col_hash->{column}
360 FROM $self->{editor}->{table}
361 WHERE $self->{editor}->{pk} = ?
362 );
363  
364 0 my $ar;
365 eval{
366 $ar = $self->{editor}->{dbh}->selectrow_arrayref(
367 0 $sql, undef, $row_ref->[0]
368 0 ); 1
369 0 ▶ } or $self->{editor}->_err_msg($@, $sql);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ar = $self->{"editor"}{"dbh"}->selectrow_arrayref($sql, undef, $row_ref->[0]); 1 } } or $self->{"editor"}->_err_msg($@, $sql) 0 0 0
370
371 $ar->[0] = $col_hash->{callback}->($ar->[0])
372 0 ▶ if $col_hash->{callback};
Branch: 1 branch
BranchTrueFalseTotal
if $col_hash->{"callback"} 0 0 0
373
374 0 my %td = (
375 VALUE => $ar->[0]
376 );
377 0 push(@td_loop, \%td);
378 }
379 0 $row{TDS} = \@td_loop;
380 }
381
382 0 push(@tmpl_loop, \%row);
383 }
384  
385 # if final screen, list any
386 # additional headers specified
387
388 0 ▶ if ($last_step){
Branch: 1 branch
BranchTrueFalseTotal
if ($last_step) 0 0 0
389 0 for my $col_hash(@{$self->{td_cols}}){
390  
391 0 my $label = $col_hash->{label};
392
393 0 $label ||= join(' ', map {ucfirst($_)}
394 0 ▶ split(/_/, $col_hash->{column}));
Condition0%0/3
$label ||= join(" ", map({ucfirst $_;} split(/_/, $col_hash->{"column"}, 0)))
l!l&&r!l&&!r
000
MC/DC0%0/2
$label ||= join(" ", map({ucfirst $_;} split(/_/, $col_hash->{"column"}, 0)))
$label join(" ", map({ucfirst $_;} split(/_/, $col_hash->{"column"}, 0)))
Truth table0%0/3
A ||= B
A= $labelB= join(" ", map({ucfirst $_;} split(/_/, $col_hash->{"column"}, 0)))
ABresult
1X1
011
000
395
396 0 my %row = (
397 HEADER => $label
398 );
399 0 push(@headers, \%row);
400 }
401 }
402  
403 # keep track of old choices
404 0 my @prev_vals;
405 0 for my $step(0 .. $self->{step}-1){
406 0 my %row;
407 0 $row{LABEL} = ($self->parse_params($step))[0];
408 0 $row{VALUE} = $self->{editor}->{query}->param(($self->parse_params($step))[0]);
409 0 push @prev_vals, \%row;
410 }
411  
412  
413  
414  
415 $self->{template}->param( SEARCH_LOOP => \@tmpl_loop,
416 FORM => 1,
417 RUN_MODE => $rm,
418 CANCEL => $cancel,
419 PREV_VALS => \@prev_vals,
420 HEADERS => \@headers,
421 CUSTOM_CSS => $self->{css},
422 0 );
423 }
424  
425  
426  
427 # discard extra sql returns
428  
429 0 ▶sub constrain_results {
Subroutine constrain_results: not called CC=6 SCAR=37.4
Pod undocumented
430
431 # this would be much cleaner
432 # but less portable using subqueries
433 # instead of two seperate queries
434  
435 0 my ($self, $list) = @_;
436  
437 # get a list of all possible
438 # results based on previous selections
439  
440 0 my $sql = qq( SELECT DISTINCT
441 T.$self->{params}->[$self->{step}]->{sql}->[0]
442 FROM $self->{editor}->{table} T
443 );
444  
445 0 my (@values, $i, @results);
446
447 0 for my $step(0 .. $self->{step}-2){
448  
449 0 ▶ $sql .= ' WHERE ' unless $i++;
Branch: 1 branch
BranchTrueFalseTotal
unless $i++ 0 0 0
450 0 $sql .= ($self->parse_params($step))[0] ." = ?";
451 0 ▶ $sql .= ' AND ' unless $step >= $self->{step}-2;
Branch: 1 branch
BranchTrueFalseTotal
unless $step >= $self->{"step"} - 2 0 0 0
452  
453 push @values,
454 0 $self->{editor}->{query}->param(($self->parse_params($step))[0]);
455 }
456  
457 0 my $selections = $self->{editor}->{dbh}->selectcol_arrayref($sql, undef, @values);
458  
459 0 for my $lr(@$list){
460 0 ▶ push (@results, $lr) if grep{/^$lr->[0]$/} @$selections;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/^$lr->[0]$/;} @$selections 0 0 0
461 }
462
463 0 return \@results;
464 }
465  
466  
467  
468 # create an HTML::Template
469  
470 0 ▶sub TEMPLATE {
Subroutine TEMPLATE: not called CC=1 SCAR=6.9
Pod undocumented
471 0 qq(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
472 <html>
473 <head>
474 <!-- TMPL_IF CUSTOM_CSS -->
475 <link rel="stylesheet" href="<!-- TMPL_VAR CUSTOM_CSS -->" type="text/css">
476 <!-- TMPL_ELSE -->
477 <style>
478 body {
479 margin:0 0 0 0;
480 border:1px solid #fff;
481 }
482  
483 .admin_area {
484 padding-top: 15px;
485 padding-bottom: 25px;
486 padding-left:15px;
487 float: left;
488 width:585px;
489 min-width: 585px;
490 background-color: #ffffff;
491 }
492
493 table {
494 font-family: Verdana, sans-serif, Arial;
495 font-weight: normal;
496 font-size: 12px;
497 color: #333333;
498 background-color: white;
499 text-align: left;
500 width: 580px;
501 border:1px solid #cccccc;
502 }
503  
504 tr.odd {
505 background-color: #EEF4FE;
506 }
507  
508 td {
509 padding-left: 23px;
510 padding-bottom: 4px;
511 padding-top: 4px;
512 padding-right: 23px;
513 }
514  
515 td.odd {
516 padding-left: 23px;
517 padding-bottom: 4px;
518 padding-top: 4px;
519 padding-right: 23px;
520 }
521  
522 td.header {
523 font-family: Verdana, sans-serif, Arial;
524 font-weight: bold;
525 font-color: #383272;
526 padding-left: 23px;
527 padding-bottom: 4px;
528 padding-top: 4px;
529 padding-right: 23px;
530 }
531  
532 a:visited {
533 color:#698A60;
534 }
535 a:active {
536 color:#273B80;
537 }
538 a:hover {
539 color:#A49580;
540 }
541 a:link {
542 color:#383272;
543 }
544  
545 INPUT, TEXTAREA, SELECT, OPTION, SUBMIT {
546 font-family: Arial, Helvetica, Sans-Serif;
547 font-size: 11px;
548 padding:2px;
549 color: #333;
550 /*background-color: #fff;*/
551 border: solid 1px #666;
552 }
553 </style>
554 <!-- /TMPL_IF -->
555  
556 <title></title>
557 </head>
558 <body>
559 <form name="form1" method="post">
560  
561 <div class="admin_area">
562 <table id="searchlist">
563 <tbody>
564 <tr>
565 <td class="header">
566 Select
567 </td>
568  
569 <!-- HEADER ROW -->
570
571 <!-- TMPL_LOOP HEADERS -->
572 <td class="header">
573 <!-- TMPL_VAR HEADER -->
574 </td>
575 <!-- /TMPL_LOOP -->
576
577 </tr>
578
579 <!-- TMPL_LOOP SEARCH_LOOP -->
580  
581 <tr <!-- TMPL_IF __ODD__ -->class="odd"<!-- /TMPL_IF -->>
582 <td <!-- TMPL_IF __ODD__ -->class="odd"<!-- /TMPL_IF -->>
583 <input type='radio' name='<!-- TMPL_VAR RADIO_NAME -->'
584 value='<!--TMPL_VAR VALUE -->' <TMPL_IF __FIRST__>checked</TMPL_IF>>
585 <!-- TMPL_VAR LABEL -->
586 </td>
587 <!-- TMPL_LOOP TDS -->
588 <td <!-- TMPL_IF __ODD__ -->class="odd"<!-- /TMPL_IF -->>
589 <!-- TMPL_VAR VALUE -->
590 </td>
591 <!-- /TMPL_LOOP -->
592 </tr>
593  
594 <!-- /TMPL_LOOP -->
595 </tbody>
596 </table>
597  
598 <!-- HIDDEN HTML FORM ELEMENTS -->
599  
600 <input type="hidden" name="step" value="<!-- TMPL_VAR STEP -->">
601 <input type="hidden" name="rm" value="<!-- TMPL_VAR RUN_MODE -->">
602
603 <!-- TMPL_LOOP PREV_VALS -->
604 <input type="hidden" name="<!-- TMPL_VAR LABEL -->" value="<!-- TMPL_VAR VALUE -->">
605 <!-- /TMPL_LOOP -->
606  
607 <!-- END HIDDEN HTML ELEMENTS -->
608  
609  
610 <input type="submit" value="Submit" style="width:80;">
611
612 <!-- TMPL_IF CANCEL -->
613
614 <input type='button' value='Cancel ' onclick='document.location="javascript:history.go(-1)"'
615 style="width:80;">
616
617 <!-- /TMPL_IF -->
618
619 <input type='button' value='Add New'
620 onclick='document.location="<!-- TMPL_VAR URL -->?rm=display"'
621 style="width:80;">
622 </div>
623  
624 </form>
625 </body>
626 </html>);
627 };
628  
629  
630  
631 1 1;
↑ Summary DBForm.pm »