DBForm.pm

filter: Statementstmt 20.6% 48 / 232 Branchbran 5.1% 3 / 58 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 29.7% 11 / 37 Podpod 80.0% 4 / 5 totaltotal 18.8% 66 / 350 CC 42 SCAR 35.5
CC
42
Cov
17%
_select_builder
132.0
_display_form
72.0
_insert_row
42.0
CRAP 34.9
« DBForm/Search/TableList.pm ↑ Summary DBForm/Search/DropDown.pm »
1 package HTML::DBForm;
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 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTML::SuperForm;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '1.05';
14  
15  
16 =head1 NAME
17  
18 HTML::DBForm - Creates a web interface for updating database tables
19  
20 =head1 SYNOPSIS
21  
22 use HTML::DBForm;
23 use HTML::DBForm::Search;
24  
25 my $editor = HTML::DBForm->new(
26 table => 'contacts',
27 primary_key => 'id',
28 );
29  
30 $editor->element(column => 'email');
31  
32 $editor->element(
33 label => 'First Name',
34 column => 'fname',
35 );
36  
37 $editor->element(
38 column => 'interest',
39 type => 'select',
40 options => \@list_of_interests
41 );
42
43 $editor->element(
44 label => 'Reason for Contact',
45 column => 'reason',
46 options => 'select reason from reasons'
47 );
48  
49 $editor->connect(
50 username => 'webuser',
51 password => 'xxxxxxx',
52 datasource => 'dbi:mysql:dbname'
53 );
54  
55 $search = HTML::DBForm::Search->new('dropdown',
56 { columns => ['id', 'lname'] }
57 );
58
59 $editor->run(search => $search);
60  
61  
62 =head1 INTRODUCTION
63  
64 HTML::DBForm provides a web interface to insert, update, and delete
65 rows from a database. This can be used to easily create content editors
66 for websites, content management systems, or anything that stores its
67 data in a database.
68  
69 HTML::DBForm allows easy creation of simple admin screens, but is
70 flexable enough to use in many different situations.
71  
72 =cut
73  
74 =head1 METHODS
75  
76 =over 4
77  
78 =cut
79  
80 =head2 new
81  
82 Creates a new editor object.
83  
84 Required parameters:
85  
86 I<table> the table we are creating a form to update
87  
88 I<primary_key> the primary key of this table. Caveat Programmor: There is
89 no checking done to enforce that the column provided as I<pk> is
90 a primary key.
91  
92 Optional parameters:
93  
94 I<stylesheet> the URL to a custom stylesheet file.
95  
96 I<template> the path to a custom template file. To get a
97 template file to start with, you can do this:
98  
99 perl -MHTML::DBForm -e 'print HTML::DBForm::TEMPLATE()' > sample.tmpl
100  
101 I<verbose_errors> a boolean parameter that determines whether
102 or not the module displays verbose error messages to the
103 browser, this is set to 0 by default for security reasons.
104  
105 I<error_handler> a callback that is triggered any time an
106 exception occurs. The callback is passed a list of errors,
107 and the results of the callback are presented as an error
108 message to the user.
109  
110  
111 B<Examples>
112
113
114 my $editor = HTML::DBForm->new(
115 table => 'table_to_update',
116 primary_key => 'id',
117 );
118
119
120 my $editor = HTML::DBForm->new(
121 table => 'table_to_update',
122 primary_key => 'id',
123 stylesheet => '/styles/custom.css',
124 verbose_errors => 1,
125 error_handler => sub { notify_admin(localtime); return @_ },
126 );
127  
128
129 =cut
130  
131 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
132  
133 1 my $type = shift;
134 1 my $self = {};
135
136 1 ▶ $self->_err_msg("new() got an odd number of parameters!")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 1 1
137 unless ((@_ % 2) == 0);
138  
139 1 my %params = @_;
140  
141 my $tmpl_ref = $params{'template'}
142 0 ▶ ? do { open(FH, "< $params{'template'}"); local $/; <FH> }
Branch: 1 branch
BranchTrueFalseTotal
$params{'template'} ? : 0 1 1
143 : &TEMPLATE;
144  
145  
146 1 $self->{template} = HTML::Template->new(
147 scalarref => \$tmpl_ref,
148 die_on_bad_params => 0,
149 loop_context_vars => 1,
150 );
151  
152 1 $self->{params} = ['type','label','value','column'];
153 1 $self->{table} = $params{'table'};
154 1 $self->{pk} = $params{'primary_key'};
155 1 $self->{query} = CGI->new;
156 1 $self->{form} = HTML::SuperForm->new;
157 1 $self->{elements} = [];
158 1 $self->{verbose} = $params{'verbose_errors'};
159 1 $self->{err_handler}= $params{'error_handler'};
160 1 $self->{css} = $params{'stylesheet'};
161  
162 1 bless $self, $type;
163 }
164  
165  
166  
167  
168 =head2 element
169  
170 Adds a new element to your editor object. Elements are created
171 as HTML::SuperForm objects
172  
173 Required parameters:
174  
175 I<column> the column that this form element represents
176  
177 Optional parameters:
178  
179 I<label> a label that will appear next to the form element.
180 this will default to the name of the column.
181  
182 I<type> the type of form element that will be displayed.
183 Currently, the available options are 'text', 'textarea', 'radio',
184 'select', 'checkbox', and 'date'. The default is 'text'.
185  
186 I<options> this is required for elements of type 'radio',
187 'select', and 'checkbox'. This parameter should hold the values
188 used to create the choice options. This parameter can be one of
189 4 types of parameters:
190  
191 scalar: this should be a SQL SELECT statement that returns 2 columns.
192 The first column will be the value, the next will be the label. This
193 SQL statement can SELECT from any table(s).
194  
195 array: a reference to an array of scalars that will be used as both
196 values and labels
197  
198 array of arrays: a reference to an array of two-element arrays that
199 will be used to populate the values and labels respectively.
200
201 hash: a reference to a hash who's keys will be the HTML element's values,
202 and values will be the HTML element's labels.
203  
204 Any other parameter pairs will be passed unchanged to the HTML::SuperForm
205 object that creates the actual form element HTML. Please see the
206 HTML::SuperForm Documentation for details. Some common examples are:
207  
208 disabled => 1, this creates a read-only field.
209  
210 onclick => "alert('some javascript behavior goes here!'"
211  
212 size => 50
213  
214 maxlength => 50
215  
216  
217  
218  
219 B<Example>
220
221 $editor->element( column => 'Name' );
222
223 $editor->element(
224 column => 'sex',
225 type => 'radio',
226 options => {M => 'Male', F => 'Female'}
227 );
228
229 $editor->element(
230 column => 'color_id',
231 label => 'Product Color'
232 type => 'select',
233 options => 'SELECT id, color FROM colors ORDER BY color'
234 );
235
236 =cut
237  
238 1 ▶sub element {
Subroutine element: called CC=2 SCAR=7.0
Pod documented
239  
240 1 my $self = shift;
241  
242 1 ▶ $self->_err_msg("element() got an odd number of parameters!")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 1 1
243 unless ((@_ % 2) == 0);
244  
245 1 my %params = @_;
246
247 1 push (@{$self->{'elements'}}, \%params);
248
249 }
250  
251  
252  
253  
254 =head2 connect
255  
256 connects to the database.
257  
258 Required parameters:
259  
260 I<dbh> a DBI database handle
261  
262 I<or>
263  
264 I<datasource>, I<username>, and I<password>
265  
266  
267 B<Example>
268
269 $editor->connect( dbh => $dbh );
270
271 $editor->connect(
272 datasource => 'dbi:mysql:my_database',
273 username => 'krailey'
274 password => 'secret'
275 );
276
277 =cut
278  
279 0 ▶sub connect {
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
280  
281 0 my $self = shift;
282  
283 0 ▶ $self->_err_msg("connect() got an odd number of parameters!")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 0 0
284 unless ((@_ % 2) == 0);
285  
286 0 my %params = @_;
287  
288 0 ▶ if ($params{dbh}){
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'dbh'}) { } 0 0 0
289 0 $self->{dbh} = $params{dbh};
290 } else {
291 0 ▶ $self->{dbh} = DBI->connect("$params{datasource}",
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"} = "DBI"->connect(qq[$params{"datasource"}], qq[$params{"username"}], qq[$params{"password"}], {"RaiseError", 1}) or $self->_err_msg($DBI::errstr) 0 0 0
292 "$params{username}",
293 "$params{password}",
294 {RaiseError => 1},
295 )
296 or $self->_err_msg($DBI::errstr);
297 }
298 }
299  
300  
301  
302  
303 =head2 run
304  
305 runs the object
306  
307 Required parameters:
308  
309 I<search> a DBForm::Search object that will create
310 a search interface for the current table
311  
312 I<or>
313  
314 I<primary_key> the value of the primary key for one
315 row that can be updated through the form
316  
317  
318 B<Example>
319
320 $search = HTML::DBForm::Search->new('dropdown',
321 { columns => ['id', 'name']},
322 );
323 $editor->run(search => $search);
324  
325 $editor->run(primary_key => '1234');
326
327 =cut
328  
329 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod documented
330  
331 0 my ($self, %params) = @_;
332  
333 0 $self->{search} = $params{search};
334
335 0 ▶ my $default = ($params{primary_key}) ? 'display':'search';
Branch: 1 branch
BranchTrueFalseTotal
$params{'primary_key'} ? : 0 0 0
336
337 # dispatch table
338 my %mode = (
339 0 ▶ search => sub {$self->{search}->run($self)},
Subroutine __ANON__: not called CC=1 SCAR=6.9
340 0 ▶ display => sub {$self->_display_form($params{primary_key})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
341 0 ▶ insert => sub {$self->_insert_row},
Subroutine __ANON__: not called CC=1 SCAR=6.9
342 0 ▶ update => sub {$self->_update_row},
Subroutine __ANON__: not called CC=1 SCAR=6.9
343 0 ▶ delete => sub {$self->_delete_row},
Subroutine __ANON__: not called CC=1 SCAR=6.9
344 0 );
345
346 0 ▶ my $rm = $mode{$self->{query}->param('rm') || $default};
Condition0%0/3
$self->{'query'}->param('rm') || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'query'}->param('rm') || $default
$self->{'query'}->param('rm') $default
Truth table0%0/3
A || B
A= $self->{'query'}->param('rm')B= $default
ABresult
1X1
011
000
347  
348 0 print $self->{query}->header;
349 0 print $rm->();
350 }
351  
352  
353  
354  
355 # PRIVATE METHODS BELOW
356  
357 # create an HTML form
358 # for adding or updating a record
359  
360 0 ▶sub _display_form {
Subroutine _display_form: not called CC=8 SCAR=42.8
361
362 0 my $self = shift;
363  
364 0 ▶ my $pk_val = shift || $self->{query}->param($self->{pk});
Condition0%0/3
shift() || $self->{'query'}->param($self->{'pk'})
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'query'}->param($self->{'pk'})
shift() $self->{'query'}->param($self->{'pk'})
Truth table0%0/3
A || B
A= shift()B= $self->{'query'}->param($self->{'pk'})
ABresult
1X1
011
000
365  
366 0 my (@form_loop, $db_row);
367  
368 0 ▶ if ($pk_val){
Branch: 1 branch
BranchTrueFalseTotal
if ($pk_val) 0 0 0
369 0 my $SQL = "SELECT * FROM $self->{table} WHERE $self->{pk} = ?";
370 0 $db_row = $self->{dbh}->selectrow_hashref($SQL, undef, $pk_val);
371  
372 $self->{template}->param(
373 DELETE => 1,
374 PK => $self->{pk},
375 0 ID => $pk_val,
376 );
377 }
378  
379 0 for my $element(@{$self->{elements}}){
380  
381 0 my %row;
382  
383 # set the defaults
384 0 ▶ $element->{type} ||= 'text';
Condition0%0/2
$element->{"type"} ||= "text"
l!l
00
MC/DC0%0/1
$element->{"type"} ||= "text"
$element->{"type"}
Truth table0%0/2
A
A= $element->{"type"}
Aresult
11
00
385
386 $element->{label} ||= join(' ',
387 0 ▶ map {ucfirst($_)} split(/_/,$element->{column}));
Condition0%0/3
$element->{"label"} ||= join(" ", map({ucfirst $_;} split(/_/, $element->{"column"}, 0)))
l!l&&r!l&&!r
000
MC/DC0%0/2
$element->{"label"} ||= join(" ", map({ucfirst $_;} split(/_/, $element->{"column"}, 0)))
$element->{"label"} join(" ", map({ucfirst $_;} split(/_/, $element->{"column"}, 0)))
Truth table0%0/3
A ||= B
A= $element->{"label"}B= join(" ", map({ucfirst $_;} split(/_/, $element->{"column"}, 0)))
ABresult
1X1
011
000
388
389 0 $element->{value} = $db_row->{$element->{column}};
390  
391 0 $row{LABEL} = $element->{label};
392 0 $row{ELEMENT} = $self->_build_element($element);
393  
394 0 push(@form_loop, \%row);
395 }
396  
397 0 ▶ my $next_mode = ($pk_val) ? 'update' : 'insert';
Branch: 1 branch
BranchTrueFalseTotal
$pk_val ? : 0 0 0
398
399 0 my $rm = { name => 'rm', default => $next_mode };
400 0 my $id = { name => $self->{pk}, default => $pk_val };
401  
402 0 $self->{template}->param( HIDDEN_LOOP => [
403 {ELEMENT => $self->_build_hidden($rm)},
404 {ELEMENT => $self->_build_hidden($id)},
405 ]);
406  
407 $self->{template}->param(
408 CUSTOM_CSS => "$self->{css}",
409 FORM_LOOP => \@form_loop,
410 URL => $self->{query}->url,
411 0 ▶ ) unless $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"error"} 0 0 0
412
413 0 $self->{template}->output;
414 }
415  
416  
417  
418  
419 # create an HTML::SuperForm object
420 # for each form element
421  
422 0 ▶sub _build_element {
Subroutine _build_element: not called CC=2 SCAR=17.9
423  
424 0 my ($self, $element) = @_;
425  
426 # avoid unlikely (but possible) recursion
427 0 ▶ return if $element->{type} eq 'element';
Branch: 1 branch
BranchTrueFalseTotal
if $element->{"type"} eq "element" 0 0 0
428  
429 # simple dispatch table
430 # for html builder methods
431 my %methods = (
432 0 ▶ checkbox => sub{ $self->_select_builder('checkbox_group', $element) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
433 0 ▶ radio => sub{ $self->_select_builder('radio_group', $element) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
434 0 ▶ select => sub{ $self->_select_builder('select', $element) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
435 0 ▶ text => sub{ $self->_build_text($element) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
436 0 ▶ textarea => sub{ $self->_build_textarea($element) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
437 0 ▶ hidden => sub{ $self->_build_hidden($element) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
438 0 ▶ date => sub{ $self->_build_date($element) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
439 0 );
440  
441 0 my $method = $methods{$element->{type}};
442 0 $self->$method;
443 }
444  
445  
446  
447  
448 # create a text field form element
449  
450 0 ▶sub _build_text {
Subroutine _build_text: not called CC=1 SCAR=6.9
451  
452 0 my ($self, $element) = @_;
453  
454 return $self->{form}->text(
455 name => $element->{column},
456 default => $element->{value},
457 0 $self->_pass_through($element)
458 );
459
460 }
461  
462  
463  
464 # create a textarea form element
465  
466 0 ▶sub _build_textarea {
Subroutine _build_textarea: not called CC=1 SCAR=6.9
467  
468 0 my ($self, $element) = @_;
469  
470 return $self->{form}->textarea(
471 name => $element->{column},
472 default => $element->{value},
473 0 $self->_pass_through($element)
474 );
475
476 }
477  
478  
479  
480 # create a hidden form element
481  
482 0 ▶sub _build_hidden {
Subroutine _build_hidden: not called CC=1 SCAR=6.9
483  
484 0 my ($self, $element) = @_;
485  
486 0 return $self->{form}->hidden($element);
487  
488 }
489  
490  
491  
492 # build a date form element
493 # (MM DD YYYY text fields)
494  
495 0 ▶sub _build_date {
Subroutine _build_date: not called CC=1 SCAR=6.9
496  
497 0 my ($self, $element) = @_;
498  
499 0 my ($YY,$MM,$DD) = ($element->{value} =~ /(\d{4})-(\d\d)-(\d\d)/);
500  
501 0 my $form ='Month ';
502 $form .= $self->{form}->text(
503 0 name => $element->{column} .'_MM',
504 default => $MM,
505 size => 2,
506 );
507
508 0 $form .=' Day ';
509 $form .= $self->{form}->text(
510 0 name => $element->{column} .'_DD',
511 default => $DD,
512 size => 2,
513 );
514
515 0 $form .=' Year ';
516 $form .= $self->{form}->text(
517 0 name => $element->{column} .'_YY',
518 default => $YY,
519 size => 4,
520 );
521  
522 0 return $form;
523  
524 }
525  
526  
527  
528 # build and populate multiple
529 # option form elements
530  
531 0 ▶sub _select_builder {
Subroutine _select_builder: not called CC=11 SCAR=48.8
532  
533 0 my ($self, $type, $element) = @_;
534  
535 0 my (@values, %labels);
536  
537 0 my $o_type;
538  
539 0 eval{
540 0 $o_type = ref $element->{options};
541 0 ▶ $o_type = 'SQL' unless $o_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $o_type 0 0 0
542 0 ▶ $o_type = 'AOA' if $element->{options}->[0][1];
Branch: 1 branch
BranchTrueFalseTotal
if $element->{"options"}[0][1] 0 0 0
543 };
544  
545
546 # load values from an array
547 0 ▶ if ($o_type eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if ($o_type eq "ARRAY") 0 0 0
548
549 0 my @labels;
550 0 for my $item (@{$element->{options}}){
551 0 push @values, $item;
552 0 push @labels, $item;
553 }
554 0 @labels{@values} = @labels;
555  
556 }
557  
558  
559 # load values from a hash
560 0 ▶ if ($o_type eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if ($o_type eq "HASH") 0 0 0
561 0 @values = keys %{$element->{options}};
562 0 %labels = %{$element->{options}};
563 }
564  
565  
566 # load values from a AoA
567 0 ▶ if ($o_type eq 'AOA'){
Branch: 1 branch
BranchTrueFalseTotal
if ($o_type eq "AOA") 0 0 0
568
569 0 my @labels;
570 0 for my $lr (@{$element->{options}}){
571 0 push @values, $lr->[0];
572 0 push @labels, $lr->[1];
573 }
574 0 @labels{@values} = @labels;
575 }
576  
577 # if param is not a reference
578 # assume that it is SQL and
579 # load values from a database
580 0 ▶ if ($o_type eq 'SQL'){
Branch: 1 branch
BranchTrueFalseTotal
if ($o_type eq "SQL") 0 0 0
581
582 0 my $db_return;
583
584 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $db_return = $self->{"dbh"}->selectall_arrayref($element->{"options"}); 1 } } or $self->_err_msg($@) 0 0 0
585 0 $db_return = $self->{dbh}->selectall_arrayref($element->{options}); 1
586 } or $self->_err_msg($@);
587
588 0 my @labels;
589 0 for my $lr (@{$db_return}){
590 0 push @values, $lr->[0];
591 0 push @labels, $lr->[1];
592 }
593 0 @labels{@values} = @labels;
594 }
595  
596 return $self->{form}->$type(
597 name => $element->{column},
598 values => \@values,
599 labels => \%labels,
600 default => $element->{value},
601 0 $self->_pass_through($element)
602 );
603
604 }
605  
606  
607  
608 # pass any unwanted parameters
609 # through to HTML::SuperForm
610  
611 0 ▶sub _pass_through {
Subroutine _pass_through: not called CC=3 SCAR=24.8
612 0 my $self = shift;
613 0 my $element = shift;
614  
615 0 my %params;
616 0 for my $param (keys %$element){
617 0 ▶ next if grep(/$param/, @{$self->{params}});
Branch: 1 branch
BranchTrueFalseTotal
if grep /$param/, @{$$self{"params"};} 0 0 0
618 0 $params{$param} = $element->{$param};
619 }
620
621 0 return %params;
622 }
623  
624  
625  
626 # add a new row
627  
628 0 ▶sub _insert_row {
Subroutine _insert_row: not called CC=6 SCAR=37.4
629  
630 0 my $self = shift;
631  
632 0 my $placeholder_count;
633 my @values;
634  
635 0 my $SQL = "INSERT into $self->{table} (";
636  
637 0 for my $element(@{$self->{elements}}){
638  
639 0 $SQL .= $element->{column} . ",";
640  
641 0 $placeholder_count++;
642  
643 0 ▶ if ($element->{type} eq 'date'){
Branch: 1 branch
BranchTrueFalseTotal
if ($element->{'type'} eq 'date') { } 0 0 0
644 0 my $val = $self->{query}->param("$element->{column}_YY") .'-';
645 0 $val .= $self->{query}->param("$element->{column}_MM") .'-';
646 0 $val .= $self->{query}->param("$element->{column}_DD");
647
648 0 push @values, $val;
649
650 } else {
651 0 push @values, $self->{query}->param($element->{column});
652 }
653 }
654
655 0 chop ($SQL);
656
657 0 $SQL .= ") VALUES (";
658  
659 0 for (1 .. $placeholder_count){
660 0 $SQL .="?,";
661 }
662 0 chop ($SQL);
663
664 0 $SQL .= ")";
665
666 0 ▶ eval{ $self->{dbh}->do($SQL, undef, @values); 1}
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"dbh"}->do($SQL, undef, @values); 1 } } or $self->_err_msg($@) 0 0 0
667 or $self->_err_msg($@);
668
669 0 my $id = $self->{dbh}->{mysql_insertid};
670  
671 0 $self->{primary_key} = $id;
672  
673 $self->{template}->param(
674 MESSAGE => 'New Record Created.',
675 URL=> $self->{query}->url,
676 0 ▶ ) unless $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"error"} 0 0 0
677  
678 0 $self->{template}->output;
679 }
680  
681  
682  
683 # update an existing row
684  
685 0 ▶sub _update_row {
Subroutine _update_row: not called CC=5 SCAR=34.0
686
687 0 my $self = shift;
688 0 my $placeholder_count;
689 0 my @values = ();
690  
691 0 my $SQL = "UPDATE $self->{table} set ";
692 0 my $q = $self->{query};
693  
694 0 for my $element(@{$self->{elements}}){
695  
696 0 $SQL .= $element->{column} . "=?,";
697  
698 0 $placeholder_count++;
699
700 0 ▶ if ($element->{type} eq 'date'){
Branch: 1 branch
BranchTrueFalseTotal
if ($element->{'type'} eq 'date') { } 0 0 0
701 0 my $val = $q->param("$element->{column}_YY") .'-';
702 0 $val .= $q->param("$element->{column}_MM") .'-';
703 0 $val .= $q->param("$element->{column}_DD");
704
705 0 push @values, $val;
706 } else {
707 0 push @values, $q->param($element->{column});
708 }
709 }
710 0 chop ($SQL);
711
712 0 $SQL .= " WHERE $self->{pk}=?";
713
714 0 push @values, $q->param($self->{pk});
715
716 0 my $sth = $self->{dbh}->prepare($SQL);
717
718 0 ▶ eval { $sth->execute(@values); 1 } or $self->_err_msg($@, $SQL);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sth->execute(@values); 1 } } or $self->_err_msg($@, $SQL) 0 0 0
719  
720 $self->{template}->param(
721 MESSAGE => 'Record Updated.',
722 URL=> $self->{query}->url
723 0 ▶ ) unless $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"error"} 0 0 0
724
725 0 $self->{template}->output;
726 }
727  
728  
729  
730 # delete an existing row
731  
732 0 ▶sub _delete_row {
Subroutine _delete_row: not called CC=3 SCAR=24.8
733
734 0 my $self = shift;
735  
736 0 my $SQL = "DELETE FROM $self->{table} WHERE $self->{pk}=?";
737  
738 0 my $sth = $self->{dbh}->prepare($SQL);
739
740 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sth->execute($self->{"query"}->param($self->{"pk"})); 1 } } or $self->_err_msg($@) 0 0 0
741 0 $sth->execute($self->{query}->param($self->{pk}));
742 0 1 } or $self->_err_msg($@);
743  
744 $self->{template}->param(
745 MESSAGE => 'Record Deleted.',
746 URL => $self->{query}->url
747 0 ▶ ) unless $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"error"} 0 0 0
748
749 0 $self->{template}->output;
750 }
751  
752  
753  
754 # display an error message
755  
756 0 ▶sub _err_msg {
Subroutine _err_msg: not called CC=3 SCAR=24.8
757  
758 0 my $self = shift;
759 0 my @errs = @_;
760
761 0 carp(@errs);
762  
763 0 $self->{error}++;
764 0 $self->{err_msg} = 'Please try again.';
765  
766 # call optional error handler
767 0 ▶ if ($self->{err_handler}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'err_handler'}) { } 0 0 0
768 0 $self->{err_msg} = $self->{err_handler}->(@errs);
769 } else {
770 0 ▶ $self->{err_msg} = join('<br />', @errs) if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
771 }
772
773 $self->{template}->param(
774 ERROR_MSG => $self->{err_msg},
775 URL => $self->{query}->url
776 0 );
777 }
778  
779  
780 =head1 SEE ALSO
781  
782 HTML::SuperForm
783 HTML::DBForm::Search
784 HTML::DBForm::Search::DropDown
785 HTML::DBForm::Search::TableList
786  
787  
788 =head1 AUTHOR
789  
790 Ken Railey, E<lt>ken_railey@yahoo.comE<gt>
791  
792 =head1 COPYRIGHT AND LICENSE
793  
794 Copyright 2004 by Ken Railey
795  
796 This library is free software; you can redistribute it and/or modify
797 it under the same terms as Perl itself.
798  
799 =cut
800  
801  
802 1 ▶sub TEMPLATE {
Subroutine TEMPLATE: called CC=1 SCAR=0.0
Pod undocumented
803  
804 1 qq(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
805 <html>
806 <head>
807 <!-- TMPL_IF CUSTOM_CSS -->
808 <link rel="stylesheet" href="<!-- TMPL_VAR CUSTOM_CSS -->" type="text/css">
809 <!-- TMPL_ELSE -->
810 <style>
811
812 body {
813 margin: 15 15 15 15;
814 }
815  
816 .admin_area {
817 padding-top: 25px;
818 padding-bottom: 25px;
819 padding-left: 20px;
820 margin-bottom: 30;
821 float: left;
822 width: 540px;
823 border: solid 1px #ccc;
824 background-color: #fff;
825 }
826
827 .message_area {
828 margin-top: 45px;
829 margin-bottom: auto;
830 margin-left: auto;
831 margin-right: auto;
832 font-family: Verdana, sans-serif, Arial;
833 font-weight: normal;
834 font-size: 12px;
835 width: 340px;
836 padding-left: 10px;
837 padding-right: 10px;
838 padding-top: 10px;
839 padding-bottom: 10px;
840 border-top: solid 2px #dedede;
841 border-left: solid 2px #dedede;
842 border-right: solid 2px #666;
843 border-bottom: solid 2px #666;
844 background-color: #ccc;
845 }
846
847 .error_area {
848 margin-top: 45px;
849 margin-bottom: auto;
850 margin-left: auto;
851 margin-right: auto;
852 font-family: Verdana, sans-serif, Arial;
853 font-weight: normal;
854 font-size: 12px;
855 width: 340px;
856 color: #600;
857 padding-left: 10px;
858 padding-right: 10px;
859 padding-top: 10px;
860 padding-bottom: 10px;
861 border-top: solid 2px #dedede;
862 border-left: solid 2px #dedede;
863 border-right: solid 2px #666;
864 border-bottom: solid 2px #666;
865 background-color: #ccc;
866 }
867
868 .error_message {
869 padding-top: 10px;
870 padding-bottom: 10px;
871 }
872
873 table {
874 font-family: Verdana, sans-serif, Arial;
875 font-weight: normal;
876 font-size: 12px;
877 font-color: #ccc;
878 background-color: white;
879 }
880  
881 td {
882 padding-top: 4px;
883 padding-bottom: 4px;
884 }
885
886 INPUT, TEXTAREA, SELECT, OPTION, SUBMIT {
887 font-family: Arial, Helvetica, Sans-Serif;
888 font-size: 11px;
889 padding:2px;
890 color: #333;
891 /*background-color: #fff;*/
892 border: solid 1px #666;
893 }
894 </style>
895 <!-- /TMPL_IF -->
896  
897 <title><TMPL_VAR NAME=VALUE></title>
898 <script>
899 function delete_record(){
900 var confirmed = window.confirm("Are you sure? Deletions are permanent.");
901 if(confirmed){
902 document.location="<!-- TMPL_VAR URL -->?rm=delete&<!-- TMPL_VAR PK -->=<!-- TMPL_VAR ID -->";
903 }else{
904 return;
905 }
906 }
907 </script>
908
909 </head>
910  
911 <body>
912  
913  
914 <form name="form1" enctype="multipart/form-data" method="post">
915  
916 <!-- -->
917  
918 <!-- TMPL_LOOP HIDDEN_LOOP -->
919 <!-- TMPL_VAR ELEMENT -->
920 <!-- /TMPL_LOOP -->
921  
922 <!-- TMPL_IF FORM_LOOP -->
923
924 <div class="admin_area">
925 <table>
926  
927 <!-- TMPL_LOOP NAME=FORM_LOOP -->
928 <tr>
929 <td>
930 <!-- TMPL_VAR LABEL -->
931 </td>
932 <td>
933 <!-- TMPL_VAR ELEMENT -->
934 </td>
935 </tr>
936 <!-- /TMPL_LOOP -->
937  
938 <tr>
939 <td colspan=2>
940 <input type='submit' name='submit' value='Submit' style="width:80;">
941 <input type='button' name='cancel' value='Cancel' style="width:80;"
942 onclick='document.location="javascript:history.go(-1)"'>
943
944 <!-- TMPL_IF DELETE -->
945  
946 <input type='button' name='delete' value='Delete' style="width:80;"
947 onclick="javascript:delete_record();">
948
949 <!-- /TMPL_IF -->
950 </td>
951 </tr>
952  
953 </table>
954 </div>
955 <!-- /TMPL_IF -->
956 </form>
957  
958  
959
960 <!-- -->
961 <!-- ERROR AREA -->
962 <!-- -->
963  
964 <!-- TMPL_IF ERROR_MSG -->
965 <div class='error_area'>
966 I'm sorry, but there was an error processing your request.<br />
967 <div class='error_message'>
968 <!-- TMPL_VAR ERROR_MSG -->
969 </div>
970 Contact the administrator for more information.
971 </div>
972 <!-- /TMPL_IF -->
973  
974  
975 <!-- -->
976 <!-- MSG AREA -->
977 <!-- -->
978  
979 <!-- TMPL_IF MESSAGE -->
980 <div class='message_area'>
981 <div class='message'>
982 <!-- TMPL_VAR MESSAGE -->
983 </div>
984
985 Your request was processed successfully.<p>
986 <a href='<!-- TMPL_VAR URL -->' class='glink'>Click Here</a>
987 to continue.
988 </div>
989 <!-- /TMPL_IF -->
990  
991  
992 </body>
993 </html>);
994 }
995  
996 1 1;
997  
« DBForm/Search/TableList.pm ↑ Summary DBForm/Search/DropDown.pm »