blib/lib/CGI/Application/Plugin/BREAD.pm

filter: Statementstmt 13.6% 24 / 176 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 31.8% 7 / 22 Podpod 100% 5 / 5 totaltotal 15.2% 36 / 236 CC 23 SCAR 28.5
CC
23
Cov
12%
bread_db
56.0
_browse
20.0
__ANON__
20.0
CRAP 17.2
↑ Summary
1 package CGI::Application::Plugin::BREAD;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use HTML::FillInForm;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 @ISA = qw( Exporter AutoLoader );
13  
14 4 our @EXPORT_OK = qw(
15 bread_db
16 browse_page_size
17 template_path
18 template_type
19 log_directive
20 );
21  
22 4 our $VERSION = '0.12_2';
23  
24 sub import
25 3 ▶{
Subroutine import: called CC=1 SCAR=0.0
26 3 my $caller = scalar( caller );
27 3 $caller->add_callback( 'prerun' => \&_register_runmodes );
28 3 goto &Exporter::import;
29 }
30  
31 sub _register_runmodes
32 0 ▶{
Subroutine _register_runmodes: not called CC=3 SCAR=24.8
33 0 my ( $self, %runmodes, $tables, $uri );
34 0 $self = shift;
35
36 #warn "CGI::Application::Plugin::BREAD::register_runmodes() called!";
37 0 %runmodes = ( 'start' => \&_start );
38 0 $tables = {};
39 0 ▶ if ( $self->{'BREAD'}->{'classes'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'BREAD'}{'classes'}) { } 0 0 0
40 0 foreach my $cdbi_class ( @{$self->{'BREAD'}->{'classes'}} ) {
41 0 my $table = $cdbi_class->table;
42 0 $runmodes{ "browse_$table" } = \&_browse;
43 0 $runmodes{ "read_$table" } = \&_read;
44 0 $runmodes{ "edit_$table" } = \&_edit;
45 0 $runmodes{ "add_$table" } = \&_add;
46 0 $runmodes{ "delete_$table" } = \&_delete;
47 0 $runmodes{ 'add_'.$table.'_submit' } = \&_submit;
48 0 $runmodes{ 'edit_'.$table.'_submit' } = \&_submit;
49 0 $tables->{$table} = $cdbi_class;
50 }
51 } else {
52 0 die "We don't have any db classes! Did you set any up with the bread_db() method?";
53 }
54 0 $self->{'BREAD'}->{'tables'} = $tables;
55 0 $uri = $ENV{'SCRIPT_URI'};
56 # take off any tailing parameters...
57 #$uri =~ s/\?.+//;
58 0 $self->{'BREAD'}->{'uri'} = $uri;
59
60 0 $self->run_modes( %runmodes );
61 }
62  
63 sub bread_db
64 0 ▶{
Subroutine bread_db: not called CC=7 SCAR=40.3
Pod documented
65 0 my ( $self, $parameter, $classes );
66
67 0 ( $self, $parameter ) = @_;
68 #warn "CGI::Application::Plugin::BREAD::bread_db() called!";
69
70 # check parameter type ... it's either an arrayref or a ::Loader ref
71 0 $classes = [];
72 0 ▶ if ( ref( $parameter ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $parameter eq 'ARRAY') { } 0 0 0
elsif (ref($parameter) =~ /^Class::DBI::Loader/) { } 0 0 0
73 0 $self->{'BREAD'}->{'cdbi_type'} = 'classes';
74 0 foreach my $cdbi_class ( @$parameter ) {
75 # check to see if it's loaded already
76 0 ▶ unless ( $cdbi_class:: ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($main::cdbi_class::) 0 0 0
77 0 my ( $file );
78 0 $file = $cdbi_class;
79 0 $file =~ s-::-/-g;
80 0 eval {
81 0 require "$file.pm";
82 0 $cdbi_class->import();
83 };
84 0 ▶ die "CGI::Application::Plugin::BREAD::bread_db(): Couldn't use $cdbi_class class: $@" if ( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
85 }
86 0 push @$classes, $cdbi_class;
87 }
88 } elsif ( ref( $parameter ) =~ /^Class::DBI::Loader/ ) {
89 0 $self->{'BREAD'}->{'cdbi_type'} = 'loader';
90 0 foreach my $class ( $parameter->classes ) {
91 0 push @$classes, $class;
92 }
93 } else {
94 0 my $ref = ref( $parameter );
95 0 die "CGI::Application::Plugin::BREAD::bread_db(): Invalid parameter\nParameter must either be an array reference of Class::DBI classes or a Class::DBI::Loader object\nYou gave me a $ref object.";
96 }
97 0 $self->{'BREAD'}->{'classes'} = $classes;
98 }
99  
100 sub browse_page_size
101 0 ▶{
Subroutine browse_page_size: not called CC=3 SCAR=24.8
Pod documented
102 0 my ( $self, $size ) = @_;
103 0 ▶ if ( $size =~ /^\d+$/ && $size > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size =~ /^\d+$/ and $size > 0) { } 0 0 0
Condition0%0/3
$size =~ /^\d+$/ and $size > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$size =~ /^\d+$/ and $size > 0
$size =~ /^\d+$/ $size > 0
Truth table0%0/3
A and B
A= $size =~ /^\d+$/B= $size > 0
ABresult
0X0
100
111
104 0 $self->{'BREAD'}->{'page_size'} = $size;
105 } else {
106 0 warn "CGI::Application::Plugin::BREAD::browse_page_size(): Invalid page_size ($size) - must be a positive decimal.";
107 }
108 }
109  
110 sub template_path
111 0 ▶{
Subroutine template_path: not called CC=1 SCAR=6.9
Pod documented
112 0 warn "Sorry - this feature (CAP::Bread::template_path()) hasn't been implemented yet!";
113 }
114  
115 sub template_type
116 0 ▶{
Subroutine template_type: not called CC=1 SCAR=6.9
Pod documented
117 0 warn "Sorry - this feature (CAP::Bread::template_type()) hasn't been implemented yet!";
118 }
119  
120 sub log_directive
121 0 ▶{
Subroutine log_directive: not called CC=1 SCAR=6.9
Pod documented
122 0 warn "Sorry - this feature (CAP::Bread::log_directive()) hasn't been implemented yet!";
123 }
124  
125 sub _start
126 0 ▶{
Subroutine _start: not called CC=2 SCAR=17.9
127 0 my ( $self, $table_ar, $table_hr, $uri, $template_html, $template );
128 0 $self = shift;
129
130 0 $table_ar = [];
131 0 $table_hr = $self->{'BREAD'}->{'tables'};
132 0 ▶ while( my ( $table, undef ) = each %$table_hr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($table, undef) = each %$table_hr) 0 0 0
133 0 push @$table_ar, {
134 'table' => $table,
135 };
136 }
137 0 $uri = $self->{'BREAD'}->{'uri'};
138
139 0 $template_html = <<_EOF_;
140 <html>
141 <head>
142 <title>Database Administration</title>
143 </head>
144 <body>
145 This application is setup to manage the following tables:
146 <ul>
147 <TMPL_LOOP NAME="tables">
148 <li><TMPL_VAR NAME="table" ESCAPE=HTML>: <a href="$uri?rm=browse_<TMPL_VAR NAME="table" ESCAPE=HTML>">Browse</a> or <a href="$uri?rm=add_<TMPL_VAR NAME="table" ESCAPE=HTML>">Add</a></li>
149 </TMPL_LOOP>
150 </ul>
151 </body>
152 </html>
153 _EOF_
154
155 0 $template = HTML::Template->new( 'scalarref' => \$template_html );
156 0 $template->param( 'tables' => $table_ar );
157 0 $template->output;
158 }
159  
160 sub _browse
161 0 ▶{
Subroutine _browse: not called CC=4 SCAR=30.0
162 0 my ( $self, $runmode, $table, $class, @columns, $colspan, @other_columns, $objects_iterator, $headers, $records, $subtemplate, $uri, $template_html, $template, $get_data_sub, $pager );
163
164 0 $self = shift;
165 0 $runmode = $self->get_current_runmode();
166 0 ( $table ) = $runmode =~ /^browse_(.+)$/;
167
168 0 $class = $self->{'BREAD'}->{'tables'}->{$table};
169 0 ▶ $self->{'BREAD'}->{'page_size'} = 20 if ! $self->{'BREAD'}->{'page_size'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"BREAD"}{"page_size"} 0 0 0
170
171 0 @columns = $class->columns;
172 0 $colspan = scalar( @columns );
173 0 $objects_iterator = $class->retrieve_all;
174 0 $headers = [];
175 0 $subtemplate = '';
176 0 $uri = $self->{'BREAD'}->{'uri'};
177 # pull out primary column (if we have it) first...
178 0 ▶ if ( my $primary = $class->primary_column ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $primary = $class->primary_column) { } 0 0 0
179 0 push @$headers, { 'header' => $primary };
180 0 $subtemplate .= qq%<td><nobr><A HREF="$uri?rm=edit_$table&id=<TMPL_VAR NAME="$primary" ESCAPE=HTML>"><TMPL_VAR NAME="$primary" ESCAPE=HTML></A> <a href="$uri?rm=delete_<TMPL_VAR NAME="table" ESCAPE=HTML>&$primary=<TMPL_VAR NAME="$primary" ESCAPE=HTML>" onClick="javascript: if (confirm('Are You Sure?')){return true;}else{return false;}">Delete</a></nobr></td>%;
181 # now need to take it out of @columns
182 0 @other_columns = grep( !/^$primary$/, @columns );
183 } else {
184 0 @other_columns = @columns;
185 }
186 0 foreach my $header ( sort @other_columns ) {
187 0 push @$headers, { 'header' => $header };
188 0 $subtemplate .= qq%<td><TMPL_VAR NAME="$header" ESCAPE=HTML></td>%;
189 }
190
191 0 $template_html = <<_EOF_;
192 <html>
193 <head>
194 <title>Database Administration :: Browsing <TMPL_VAR NAME="table" ESCAPE=HTML> Table</title>
195 </head>
196 <body>
197  
198 <p>
199 The <b><TMPL_VAR NAME="table" ESCAPE=HTML></b> table has the following <TMPL_VAR NAME="count" ESCAPE=HTML> records, broken up into <TMPL_VAR NAME="num_pages" ESCAPE=HTML> pages of <TMPL_VAR NAME="pagesize" ESCAPE=HTML> each:
200 </p>
201  
202 <TMPL_VAR NAME="PAGER_JAVASCRIPT">
203 <FORM METHOD="POST">
204 <TABLE BORDER=0 BGCOLOR=#000000 WIDTH=100%>
205 <TR><TD><TABLE BORDER=0 WIDTH=100%>
206 <tr BGCOLOR="#FFFFFF">
207 <TMPL_LOOP NAME="headers">
208 <th><TMPL_VAR NAME="header" ESCAPE=HTML></th>
209 </TMPL_LOOP>
210 </tr>
211 <TMPL_LOOP NAME="PAGER_DATA_LIST">
212 <TR BGCOLOR="#FFFFFF">
213 $subtemplate
214 </TR>
215 </TMPL_LOOP>
216 <TR><TD BGCOLOR=#DDDDDD COLSPAN=$colspan ALIGN=CENTER>
217 <TMPL_VAR NAME="PAGER_PREV">
218 <TMPL_VAR NAME="PAGER_JUMP">
219 <TMPL_VAR NAME="PAGER_NEXT">
220 </TD></TR>
221 </TABLE>
222 </TABLE>
223 <TMPL_VAR NAME="PAGER_HIDDEN">
224 </FORM>
225  
226 </body>
227 </html>
228 _EOF_
229
230 0 $template = HTML::Template->new( 'scalarref' => \$template_html, 'global_vars' => 1 );
231 $template->param(
232 'table' => $table,
233 'headers' => $headers,
234 'count' => $objects_iterator->count,
235 'pagesize' => $self->{'BREAD'}->{'page_size'},
236 0 'num_pages' => sprintf( "%d", ($objects_iterator->count/$self->{'BREAD'}->{'page_size'})+1 )
237 );
238
239 $get_data_sub = sub
240 0 ▶ {
Subroutine __ANON__: not called CC=4 SCAR=30.0
241 0 my ( $offset, $rows, @objects, $records );
242 0 ( $offset, $rows ) = @_;
243 0 @objects = $objects_iterator->slice( $offset, $rows+$offset-1 );
244 0 $records = [];
245
246 0 foreach my $obj ( @objects ) {
247 0 my %data = ();
248 0 foreach my $column ( @columns ) {
249 0 my $subdata = $obj->get( $column );
250 0 ▶ $subdata = ref( $subdata ) if ref( $subdata );
Branch: 1 branch
BranchTrueFalseTotal
if ref $subdata 0 0 0
251 0 $data{$column} = $subdata;
252 }
253 0 push @$records, \%data;
254 }
255  
256 0 return $records;
257 0 };
258
259 0 require HTML::Pager;
260 $pager = HTML::Pager->new(
261 'template' => $template,
262 'query' => $self->query,
263 'rows' => $objects_iterator->count,
264 0 'page_size' => $self->{'BREAD'}->{'page_size'},
265 'get_data_callback' => $get_data_sub,
266 'persist_vars' => [ 'rm' ],
267 );
268 0 return $pager->output();
269 }
270  
271 sub _read
272 0 ▶{
Subroutine _read: not called CC=1 SCAR=0.0
273 # not sure if this is needed...
274 }
275  
276 sub _edit
277 0 ▶{
Subroutine _edit: not called CC=2 SCAR=17.9
278 0 my ( $self, $runmode, $table, $html, $class, $obj, %data, $fif );
279
280 0 $self = shift;
281 0 $runmode = $self->get_current_runmode();
282 0 ( $table ) = $runmode =~ /^edit_(.+)$/;
283  
284 0 $html = _get_add_or_edit_form( $self );
285  
286 0 $class = $self->{'BREAD'}->{'tables'}->{$table};
287 0 $obj = $class->retrieve( $self->query->param( 'id' ) );
288 0 %data = ();
289 0 foreach my $column ( sort $class->columns ) {
290 0 $data{ $column } = $obj->get( $column );
291 }
292
293 0 $fif = HTML::FillInForm->new();
294 0 $fif->fill(
295 'scalarref' => \$html,
296 'fdat' => \%data
297 );
298 }
299  
300 sub _add
301 0 ▶{
Subroutine _add: not called CC=1 SCAR=6.9
302 0 my ( $self );
303
304 0 $self = shift;
305
306 0 _get_add_or_edit_form( $self );
307 }
308  
309 sub _delete
310 0 ▶{
Subroutine _delete: not called CC=1 SCAR=6.9
311 0 my ( $self, $runmode, $table, $class, $obj );
312
313 0 $self = shift;
314 0 $runmode = $self->get_current_runmode();
315 0 ( $table ) = $runmode =~ /^delete_(.+)$/;
316 0 $class = $self->{'BREAD'}->{'tables'}->{$table};
317 0 $obj = $class->retrieve( $self->query->param( 'id' ) );
318
319 0 $obj->delete;
320
321 0 $self->header_type( 'redirect' );
322 0 $self->header_props( -url => $self->{'BREAD'}->{'uri'} );
323 }
324  
325 sub _submit
326 0 ▶{
Subroutine _submit: not called CC=4 SCAR=30.0
327 0 my ( $self, $runmode, $mode, $table, $class, $form );
328  
329 0 $self = shift;
330 0 $runmode = $self->get_current_runmode();
331 0 ( $mode, $table ) = $runmode =~ /^(add|edit)_(.+)_submit$/;
332 0 $class = $self->{'BREAD'}->{'tables'}->{$table};
333
334 0 $form = $class->as_form( params => $self->query );
335 0 ▶ if ( $form->submitted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->submitted) 0 0 0
336 0 ▶ if ( $mode eq 'add' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'add') { } 0 0 0
elsif ($mode eq 'edit') { } 0 0 0
337 0 my $obj = $class->create_from_form( $form );
338 } elsif ( $mode eq 'edit' ) {
339 0 my $obj = $class->update_from_form( $form );
340 } else {
341 0 warn "CGI::Application::Plugin::BREAD::_submit(): Unknown mode ($mode) - Expecting add or edit.";
342 }
343 }
344
345 # redirecting user back to main page
346 0 $self->header_type( 'redirect' );
347 0 $self->header_props( -url => $self->{'BREAD'}->{'uri'} );
348 }
349  
350 sub _get_add_or_edit_form
351 0 ▶{
Subroutine _get_add_or_edit_form: not called CC=2 SCAR=17.9
352 0 my ( $self, $subtitle, $form, $rm_hidden, $runmode, $mode, $table, $class, $template_html, $template );
353
354 0 $self = shift;
355
356 0 ▶ if ( $self->query->param( 'id' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query->param('id')) { } 0 0 0
357 0 $subtitle = q%Editing Entry # <TMPL_VAR NAME="id" ESCAPE=HTML>%;
358 } else {
359 0 $subtitle = 'Adding an Entry';
360 }
361  
362 0 $runmode = $self->get_current_runmode();
363 0 ( $mode, $table ) = $runmode =~ /^(add|edit)_(.+)$/;
364 0 $class = $self->{'BREAD'}->{'tables'}->{$table};
365 0 $class->form_builder_defaults( { method => 'post' } );
366  
367 0 $form = $class->as_form( params => $self->query )->render;
368 0 $rm_hidden = '<input type="hidden" name="rm" value="' . $mode . '_' . $table . '_submit" />';
369 0 $form =~ s/<input/$rm_hidden<input/;
370  
371 0 $template_html = <<_EOF_;
372 <html>
373 <head>
374 <title>Database Administration :: $subtitle to the <TMPL_VAR NAME="table" ESCAPE=HTML> Table</title>
375 </head>
376 <body>
377  
378 $form
379  
380 </body>
381 </html>
382 _EOF_
383
384 0 $template = HTML::Template->new( 'scalarref' => \$template_html, 'associate' => $self->query );
385 0 $template->param(
386 'table' => $table,
387 );
388 0 $template->output;
389 }
390  
391 4 1;
392 __END__
↑ Summary