line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Browse; |
2
|
2
|
|
|
2
|
|
57055
|
use Class::Std; |
|
2
|
|
|
|
|
26715
|
|
|
2
|
|
|
|
|
14
|
|
3
|
2
|
|
|
2
|
|
2046
|
use Class::Std::Utils; |
|
2
|
|
|
|
|
7625
|
|
|
2
|
|
|
|
|
14
|
|
4
|
2
|
|
|
2
|
|
2431
|
use DBIx::MySperqlOO; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use warnings; |
7
|
|
|
|
|
|
|
use strict; |
8
|
|
|
|
|
|
|
use Carp; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use version; our $VERSION = qv('1.0.0'); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
{ |
13
|
|
|
|
|
|
|
my %dbh_of :ATTR( :get :set ); |
14
|
|
|
|
|
|
|
my %connect_of :ATTR( :get :set :default<{}> :init_arg ); |
15
|
|
|
|
|
|
|
my %fields_of :ATTR( :get :set :default<'20'> :init_arg ); |
16
|
|
|
|
|
|
|
my %sql_of :ATTR( :get :set :default<''> :init_arg ); |
17
|
|
|
|
|
|
|
my %urls_of :ATTR( :get :set :default<{}> :init_arg ); |
18
|
|
|
|
|
|
|
my %sort_of :ATTR( :get :set :default<''> :init_arg ); |
19
|
|
|
|
|
|
|
my %sort_vec_of :ATTR( :get :set :default<'asc'> :init_arg ); |
20
|
|
|
|
|
|
|
my %window_of :ATTR( :get :set :default<'20'> :init_arg ); |
21
|
|
|
|
|
|
|
my %index_of :ATTR( :get :set :default<'0'> :init_arg ); |
22
|
|
|
|
|
|
|
my %count_of :ATTR( :get :set :default<'0'> ); |
23
|
|
|
|
|
|
|
my %features_of :ATTR( :get :set :default<{}> :init_arg ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my %classes_of :ATTR( :get :set :default<['browseRowA','browseRowB']> :init_arg ); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub BUILD { |
28
|
|
|
|
|
|
|
my ( $self, $ident, $arg_ref ) = @_; |
29
|
|
|
|
|
|
|
return; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub START { |
33
|
|
|
|
|
|
|
my ( $self, $ident, $arg_ref ) = @_; |
34
|
|
|
|
|
|
|
if (! defined $arg_ref->{no_dbh} ) { |
35
|
|
|
|
|
|
|
$self->set_dbh( DBIx::MySperqlOO->new($self->get_connect()) ); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
return; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub dbh { my ( $self ) = @_; return $self->get_dbh(); } |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub delete_enabled { |
43
|
|
|
|
|
|
|
my ( $self ) = @_; |
44
|
|
|
|
|
|
|
my $delete = ''; |
45
|
|
|
|
|
|
|
if ( defined $self->get_features()->{delete} ) { $delete = $self->get_features()->{delete}; } |
46
|
|
|
|
|
|
|
return $delete; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub args_to_attributes { |
50
|
|
|
|
|
|
|
my ($self, $arg_ref) = @_; |
51
|
|
|
|
|
|
|
if ( defined $arg_ref->{index} ) { $self->set_index( $arg_ref->{index} ); } |
52
|
|
|
|
|
|
|
if ( defined $arg_ref->{window} ) { $self->set_window( $arg_ref->{window} ); } |
53
|
|
|
|
|
|
|
if ( defined $arg_ref->{sort} ) { $self->set_sort( $arg_ref->{sort} ); } |
54
|
|
|
|
|
|
|
if ( defined $arg_ref->{sort_vec} ) { $self->set_sort_vec( $arg_ref->{sort_vec} ); } |
55
|
|
|
|
|
|
|
if ( defined $arg_ref->{features} ) { $self->set_features( $arg_ref->{features} ); } |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub build { |
59
|
|
|
|
|
|
|
my ($self, $arg_ref) = @_; |
60
|
|
|
|
|
|
|
my $build = {}; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$self->args_to_attributes( $arg_ref ); |
63
|
|
|
|
|
|
|
$self->set_count( $self->_get_row_count() ); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$build->{browse_styles} = $self->_build_styles(); |
66
|
|
|
|
|
|
|
$build->{browse_script} = $self->_build_javascript(); |
67
|
|
|
|
|
|
|
$build->{browse_action} = $self->get_urls()->{root} . $self->get_urls()->{browse}; |
68
|
|
|
|
|
|
|
$build->{browse_table} = $self->_build_table(); |
69
|
|
|
|
|
|
|
$build->{browse_sorted} = $self->_build_sorted(); |
70
|
|
|
|
|
|
|
$build->{browse_start} = $self->_build_start(); |
71
|
|
|
|
|
|
|
$build->{browse_prevnext} = $self->_build_prev_next(); |
72
|
|
|
|
|
|
|
$build->{browse_show} = $self->_build_show(); |
73
|
|
|
|
|
|
|
$build->{browse_goto} = $self->_build_goto(); |
74
|
|
|
|
|
|
|
$build->{browse_control} = $self->_build_control(); |
75
|
|
|
|
|
|
|
$build->{browse_delete} = $self->_build_delete(); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
if (defined $self->get_features->{default_html} ) { # Defined And |
78
|
|
|
|
|
|
|
if ( $self->get_features->{default_html} ) { # Also True |
79
|
|
|
|
|
|
|
return $self->_build_default_html( $build ); |
80
|
|
|
|
|
|
|
} else { return $build; } # Default HTML False |
81
|
|
|
|
|
|
|
} else { |
82
|
|
|
|
|
|
|
return $build; # Default HTML Undefined |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub build_sql { |
87
|
|
|
|
|
|
|
my ( $self, $arg_ref ) = @_; |
88
|
|
|
|
|
|
|
my $sql = $self->get_sql(); |
89
|
|
|
|
|
|
|
my $sort = $self->get_sort(); |
90
|
|
|
|
|
|
|
my $sort_vec = $self->get_sort_vec(); |
91
|
|
|
|
|
|
|
my $index = $self->get_index(); |
92
|
|
|
|
|
|
|
my $window = $self->get_window(); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Add sort |
95
|
|
|
|
|
|
|
if ( $sort ) { $sql .= " order by $sort $sort_vec"; } |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Add limit |
98
|
|
|
|
|
|
|
$sql .= " limit $index, $window"; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return $sql; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _build_javascript { |
104
|
|
|
|
|
|
|
my ( $self ) = @_; |
105
|
|
|
|
|
|
|
my $url = $self->get_urls()->{root} . $self->get_urls()->{browse}; |
106
|
|
|
|
|
|
|
my $html = '' . "\n"; |
156
|
|
|
|
|
|
|
return $html; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub _build_styles { |
160
|
|
|
|
|
|
|
my $styles = <
|
161
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
STYLES_END |
189
|
|
|
|
|
|
|
return $styles; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub _build_table { |
193
|
|
|
|
|
|
|
my ($self, $arg_ref) = @_; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
my $fields = $self->get_fields(); |
196
|
|
|
|
|
|
|
my @classes = @{ $self->get_classes() }; |
197
|
|
|
|
|
|
|
my $class_count = scalar(@classes); |
198
|
|
|
|
|
|
|
my $class_index = 0; |
199
|
|
|
|
|
|
|
my $rows = $self->_get_rows( $arg_ref ); |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my $html = '' . "\n"; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
return $html; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub _build_header { |
240
|
|
|
|
|
|
|
my ( $self ) = @_; |
241
|
|
|
|
|
|
|
my $fields = $self->get_fields(); |
242
|
|
|
|
|
|
|
my $html .= ' |
' . "\n";
243
|
|
|
|
|
|
|
if ( $self->delete_enabled() ) { |
244
|
|
|
|
|
|
|
$html .= ' | | ' . "\n";
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
foreach my $field ( @$fields ) { |
247
|
|
|
|
|
|
|
if (! $field->{hide} ) { |
248
|
|
|
|
|
|
|
if ( $field->{sort} ) { |
249
|
|
|
|
|
|
|
$html .= ' | ' . $field->{label} . ' | ' . "\n";
250
|
|
|
|
|
|
|
} else { |
251
|
|
|
|
|
|
|
$html .= ' | ' . $field->{label} . ' | ' . "\n";
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
$html .= ' |
' . "\n";
256
|
|
|
|
|
|
|
return $html; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
sub _build_sorted { |
260
|
|
|
|
|
|
|
my ( $self ) = @_; |
261
|
|
|
|
|
|
|
my $sort = $self->get_sort(); |
262
|
|
|
|
|
|
|
$sort =~ s/_/ /g; |
263
|
|
|
|
|
|
|
$sort = $sort ? $sort : 'default'; |
264
|
|
|
|
|
|
|
return ' Sorted by ' . $sort . '.'; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub _build_start { |
268
|
|
|
|
|
|
|
my ( $self ) = @_; |
269
|
|
|
|
|
|
|
my $count = $self->get_count(); |
270
|
|
|
|
|
|
|
my $index = $self->get_index(); |
271
|
|
|
|
|
|
|
return ' Starting row ' . $self->commify($index + 1) . ' / ' . $self->commify($count) . '.'; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
sub _build_prev_next { |
275
|
|
|
|
|
|
|
my ( $self ) = @_; |
276
|
|
|
|
|
|
|
my $window = $self->get_window(); |
277
|
|
|
|
|
|
|
my $index = $self->get_index(); |
278
|
|
|
|
|
|
|
my $count = $self->get_count(); |
279
|
|
|
|
|
|
|
my ( $pindex, $nindex, $html ); |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
if ($index > 0) { |
282
|
|
|
|
|
|
|
if ($index - $window > 0) { $pindex = $index - $window; } else { $pindex = 0; } |
283
|
|
|
|
|
|
|
$html .= ' < Prev | '; |
284
|
|
|
|
|
|
|
} else { |
285
|
|
|
|
|
|
|
$html .= ' < Prev | '; |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
# Next rows |
289
|
|
|
|
|
|
|
if ($index + $window < $count) { |
290
|
|
|
|
|
|
|
$nindex = $index + $window; |
291
|
|
|
|
|
|
|
$html .= 'Next > '; |
292
|
|
|
|
|
|
|
} else { |
293
|
|
|
|
|
|
|
$html .= 'Next >'; |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
return $html; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
sub _build_show { |
299
|
|
|
|
|
|
|
my ( $self ) = @_; |
300
|
|
|
|
|
|
|
my $window = $self->get_window(); |
301
|
|
|
|
|
|
|
my $html = 'Show rows. '; |
302
|
|
|
|
|
|
|
return $html; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
sub _build_goto { |
306
|
|
|
|
|
|
|
my ( $self ) = @_; |
307
|
|
|
|
|
|
|
my $window = $self->get_window(); |
308
|
|
|
|
|
|
|
my $count = $self->get_count(); |
309
|
|
|
|
|
|
|
my $index = 0; |
310
|
|
|
|
|
|
|
my $page = 0; |
311
|
|
|
|
|
|
|
my $html = 'Go to page |
312
|
|
|
|
|
|
|
while ( $index < $count ) { |
313
|
|
|
|
|
|
|
$html .= ' |
314
|
|
|
|
|
|
|
$index += $window; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
$html .= '. '; |
317
|
|
|
|
|
|
|
return $html; |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
sub _build_control { |
321
|
|
|
|
|
|
|
my ( $self ) = @_; |
322
|
|
|
|
|
|
|
my $index = $self->get_index(); |
323
|
|
|
|
|
|
|
my $sort = $self->get_sort(); |
324
|
|
|
|
|
|
|
my $sort_vec = $self->get_sort_vec(); |
325
|
|
|
|
|
|
|
my $html = ''; |
326
|
|
|
|
|
|
|
$html .= ''; |
327
|
|
|
|
|
|
|
$html .= ''; |
328
|
|
|
|
|
|
|
if ( $self->delete_enabled() ) { |
329
|
|
|
|
|
|
|
$html .= ''; |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
return $html; |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
sub _build_delete { |
335
|
|
|
|
|
|
|
my ( $self ) = @_; |
336
|
|
|
|
|
|
|
my $html; |
337
|
|
|
|
|
|
|
if ( $self->delete_enabled() eq 'multi' ) { |
338
|
|
|
|
|
|
|
my $url = $self->get_urls()->{root} . $self->get_urls()->{delete}; |
339
|
|
|
|
|
|
|
$html = ''; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
return $html; |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
sub _build_default_html { |
345
|
|
|
|
|
|
|
my ( $self, $build ) = @_; |
346
|
|
|
|
|
|
|
my $html = $build->{browse_script}; |
347
|
|
|
|
|
|
|
$html .= ' |
348
|
|
|
|
|
|
|
$html .= '' . "\n";
349
|
|
|
|
|
|
|
$html .= ' | ' . "\n";
350
|
|
|
|
|
|
|
$html .= ' | ' . $build->{browse_sorted} . ' | ' . "\n";
351
|
|
|
|
|
|
|
$html .= ' | ' . $build->{browse_start} . ' | ' . "\n";
352
|
|
|
|
|
|
|
$html .= ' | ' . $build->{browse_prevnext} . ' | ' . "\n";
353
|
|
|
|
|
|
|
$html .= ' | ' . "\n";
354
|
|
|
|
|
|
|
$html .= ' | ' . "\n"; |
355
|
|
|
|
|
|
|
$html .= $build->{browse_table}; |
356
|
|
|
|
|
|
|
$html .= ' ' . "\n"; |
357
|
|
|
|
|
|
|
$html .= '' . "\n";
358
|
|
|
|
|
|
|
$html .= ' | ' . "\n";
359
|
|
|
|
|
|
|
$html .= ' | ' . $build->{browse_show} . ' | ' . "\n";
360
|
|
|
|
|
|
|
$html .= ' | ' . $build->{browse_delete} . ' | ' . "\n";
361
|
|
|
|
|
|
|
$html .= ' | ' . $build->{browse_goto} . ' | ' . "\n";
362
|
|
|
|
|
|
|
$html .= ' | ' . "\n";
363
|
|
|
|
|
|
|
$html .= ' | ' . "\n";
364
|
|
|
|
|
|
|
$html .= ' | ' . $build->{browse_control} . ' | ' . "\n";
365
|
|
|
|
|
|
|
$html .= ' | ' . "\n";
366
|
|
|
|
|
|
|
$html .= ' | ' . "\n"; |
367
|
|
|
|
|
|
|
$html .= '' . "\n"; |
368
|
|
|
|
|
|
|
return $html; |
369
|
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
sub _get_rows { |
372
|
|
|
|
|
|
|
my ( $self, $arg_ref ) = @_; |
373
|
|
|
|
|
|
|
my $sql = $self->build_sql( $arg_ref ); |
374
|
|
|
|
|
|
|
return $self->dbh()->sqlexec( $sql, '\@@' ); |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
sub _get_row_count { |
378
|
|
|
|
|
|
|
my ( $self, $arg_ref ) = @_; |
379
|
|
|
|
|
|
|
my $sql = $self->build_sql( $arg_ref ); |
380
|
|
|
|
|
|
|
$sql =~ s/select (.*) from/select count(*) from/g; |
381
|
|
|
|
|
|
|
$sql =~ s/limit.*$//g; |
382
|
|
|
|
|
|
|
my ( $row_count ) = $self->dbh()->sqlexec( $sql, '@' ); |
383
|
|
|
|
|
|
|
return $row_count; |
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
sub commify { |
387
|
|
|
|
|
|
|
my ( $self, $value ) = @_; |
388
|
|
|
|
|
|
|
my $text = reverse $value; |
389
|
|
|
|
|
|
|
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; |
390
|
|
|
|
|
|
|
return scalar reverse $text; |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
sub flip_sort_vec { |
394
|
|
|
|
|
|
|
my ( $self ) = @_; |
395
|
|
|
|
|
|
|
my $ident = ident $self; |
396
|
|
|
|
|
|
|
my $sort_vec = $sort_vec_of{$ident}; |
397
|
|
|
|
|
|
|
if ( $sort_vec eq 'asc' ) { $sort_vec = 'desc'; } else { $sort_vec = 'asc'; } |
398
|
|
|
|
|
|
|
$self->set_sort_vec( $sort_vec ); |
399
|
|
|
|
|
|
|
return $sort_vec; |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
404
|
|
|
|
|
|
|
__END__ |