and
443
|
|
|
|
|
|
|
C. |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
=item C |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
An arrayref of hashrefs to specify custom columns to appear in the list view |
448
|
|
|
|
|
|
|
of an entity. (Previously, this was just a hashref of column names and specs, |
449
|
|
|
|
|
|
|
and this style is still supported for backwards compatibility, but is deprecated |
450
|
|
|
|
|
|
|
because it leaves the order of the columns unpredictable.) |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
The keys of each hash are C, the name to use for this custom column, |
453
|
|
|
|
|
|
|
C indicating a column from the table that should be selected to |
454
|
|
|
|
|
|
|
build the custom column from, C, a subref to be used as a |
455
|
|
|
|
|
|
|
HTML::Table::FromDatabase callback on the resulting column, and C, |
456
|
|
|
|
|
|
|
to specify a CSS class for the the column. C is optional, and if |
457
|
|
|
|
|
|
|
no C is provided, sub { return shift; } will be used. |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
If your custom column has the same name as an existing column, your customizations |
460
|
|
|
|
|
|
|
will be used in-place to override the display of the content in that column. |
461
|
|
|
|
|
|
|
If sorting is enabled, the column will be sorted by the |
462
|
|
|
|
|
|
|
underlying database content for that row, and not by the output of your transform function. |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
For a somewhat spurious example: |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
... |
467
|
|
|
|
|
|
|
custom_columns => [ |
468
|
|
|
|
|
|
|
{ |
469
|
|
|
|
|
|
|
name => 'email_provider', |
470
|
|
|
|
|
|
|
raw_column => 'email', |
471
|
|
|
|
|
|
|
transform => sub { |
472
|
|
|
|
|
|
|
my $value = shift; |
473
|
|
|
|
|
|
|
return (split /@/, 1)[1]; |
474
|
|
|
|
|
|
|
}, |
475
|
|
|
|
|
|
|
column_class => 'column-class', |
476
|
|
|
|
|
|
|
}, |
477
|
|
|
|
|
|
|
], |
478
|
|
|
|
|
|
|
... |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
The C code ref is passed to L as a |
482
|
|
|
|
|
|
|
callback for that column, so it can do anything a |
483
|
|
|
|
|
|
|
L |
484
|
|
|
|
|
|
|
can do. In particular, the coderef will receive the value of the |
485
|
|
|
|
|
|
|
column as the first parameter, but also a reference to the whole row hashref |
486
|
|
|
|
|
|
|
as the second parameter, so you can do a variety of cunning things. |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
An example of a custom column whose C coderef uses the row |
489
|
|
|
|
|
|
|
hashref to get other values for the same row could be: |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
... |
492
|
|
|
|
|
|
|
custom_columns => [ |
493
|
|
|
|
|
|
|
{ |
494
|
|
|
|
|
|
|
name => 'salutation', |
495
|
|
|
|
|
|
|
raw_column => 'name', |
496
|
|
|
|
|
|
|
transform => sub { |
497
|
|
|
|
|
|
|
my ($name_value, $row) = @_; |
498
|
|
|
|
|
|
|
return "Hi, $row->{title} $name_value!"; |
499
|
|
|
|
|
|
|
}, |
500
|
|
|
|
|
|
|
} |
501
|
|
|
|
|
|
|
], |
502
|
|
|
|
|
|
|
... |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
=item C |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
You can require that users be authenticated to view/edit records using the C |
507
|
|
|
|
|
|
|
option to enable authentication powered by L. |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
You can set different requirements for viewing and editing, for example: |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
auth => { |
512
|
|
|
|
|
|
|
view => { |
513
|
|
|
|
|
|
|
require_login => 1, |
514
|
|
|
|
|
|
|
}, |
515
|
|
|
|
|
|
|
edit => { |
516
|
|
|
|
|
|
|
require_role => 'Admin', |
517
|
|
|
|
|
|
|
}, |
518
|
|
|
|
|
|
|
}, |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
The example above means that any logged in user can view records, but only users |
521
|
|
|
|
|
|
|
with the 'Admin' role are able to create/edit/delete records. |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
Or, to just require login for anything (same requirements for both viewing and |
524
|
|
|
|
|
|
|
editing), you can use the shorthand: |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
auth => { |
527
|
|
|
|
|
|
|
require_login => 1, |
528
|
|
|
|
|
|
|
}, |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
=item C |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
This provides a CSS class for the tables. |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=item C |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
This provides a CSS class for the tables paginate buttons. |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=cut |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
sub simple_crud { |
542
|
8
|
|
|
8
|
|
18367
|
my (%args) = @_; |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
# Get a database connection to verify that the table name is OK, etc. |
545
|
8
|
|
|
|
|
20
|
my $dbh = _database(\%args); |
546
|
|
|
|
|
|
|
|
547
|
8
|
50
|
|
|
|
618
|
if (!$dbh) { |
548
|
0
|
|
|
|
|
0
|
warn "No database handle"; |
549
|
0
|
|
|
|
|
0
|
return; |
550
|
|
|
|
|
|
|
} |
551
|
|
|
|
|
|
|
|
552
|
8
|
50
|
|
|
|
21
|
if (!$args{prefix}) { die "Need prefix to create routes!"; } |
|
0
|
|
|
|
|
0
|
|
553
|
8
|
50
|
|
|
|
91
|
if ($args{prefix} !~ m{^/}) { |
554
|
0
|
|
|
|
|
0
|
$args{prefix} = '/' . $args{prefix}; |
555
|
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
# If there's a Dancer prefix in use, as well as a prefix we're told about, |
558
|
|
|
|
|
|
|
# then _construct_url() will need to be told about that later so it can |
559
|
|
|
|
|
|
|
# construct URLs. It can't just call Dancer::App->current->prefix itself, |
560
|
|
|
|
|
|
|
# though, as the prefix may have changed by the time the code is actually |
561
|
|
|
|
|
|
|
# running. (See RT #73620.) So, we need to grab it here and add it to |
562
|
|
|
|
|
|
|
# %args, so it can see it later. |
563
|
8
|
|
50
|
|
|
36
|
$args{dancer_prefix} = Dancer::App->current->prefix || ''; |
564
|
|
|
|
|
|
|
|
565
|
8
|
50
|
|
|
|
267
|
if (!$args{db_table}) { die "Need table name!"; } |
|
0
|
|
|
|
|
0
|
|
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
# Accept deleteable as a synonym for deletable |
568
|
|
|
|
|
|
|
$args{deletable} = delete $args{deleteable} |
569
|
8
|
50
|
33
|
|
|
34
|
if !exists $args{deletable} && exists $args{deleteable}; |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
# Sane default values: |
572
|
8
|
|
50
|
|
|
37
|
$args{key_column} ||= 'id'; |
573
|
8
|
|
50
|
|
|
20
|
$args{record_title} ||= 'record'; |
574
|
8
|
100
|
|
|
|
17
|
$args{editable} = 1 unless exists $args{editable}; |
575
|
8
|
100
|
|
|
|
21
|
$args{addable} = $args{editable} unless exists $args{addable}; |
576
|
8
|
50
|
|
|
|
20
|
$args{query_auto_focus} = 1 unless exists $args{query_auto_focus}; |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
# Sanitise things we'll have to interpolate into queries (yes, that makes me |
579
|
|
|
|
|
|
|
# feel bad, but you can't use params for field/table names): |
580
|
8
|
|
|
|
|
13
|
my $table_name = $args{db_table}; |
581
|
8
|
|
|
|
|
14
|
my $key_column = $args{key_column}; |
582
|
8
|
|
|
|
|
19
|
for ($table_name, $key_column) { |
583
|
16
|
50
|
|
|
|
39
|
die "Invalid table name/key column - SQL injection attempt?" |
584
|
|
|
|
|
|
|
if /--/; |
585
|
16
|
|
|
|
|
39
|
s/[^a-zA-Z0-9_-]//g; |
586
|
|
|
|
|
|
|
} |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
# OK, create a route handler to deal with adding/editing: |
589
|
|
|
|
|
|
|
my $handler |
590
|
8
|
|
|
6
|
|
35
|
= sub { _create_add_edit_route(\%args, $table_name, $key_column); }; |
|
6
|
|
|
|
|
64820
|
|
591
|
|
|
|
|
|
|
|
592
|
8
|
100
|
|
|
|
22
|
if ($args{editable}) { |
593
|
3
|
|
|
|
|
14
|
for ('/edit/:id') { |
594
|
3
|
|
|
|
|
11
|
my $url = _construct_url($args{prefix}, $_); |
595
|
3
|
|
|
|
|
20
|
Dancer::Logger::debug("Setting up route for $url"); |
596
|
3
|
|
|
|
|
184
|
any ['get', 'post'] => $url => _ensure_auth( |
597
|
|
|
|
|
|
|
'edit', $handler, \%args, |
598
|
|
|
|
|
|
|
); |
599
|
|
|
|
|
|
|
} |
600
|
|
|
|
|
|
|
} |
601
|
8
|
100
|
|
|
|
2066
|
if ($args{addable}) { |
602
|
2
|
|
|
|
|
5
|
for ('/add') { |
603
|
2
|
|
|
|
|
7
|
my $url = _construct_url($args{prefix}, $_); |
604
|
2
|
|
|
|
|
11
|
Dancer::Logger::debug("Setting up route for $url"); |
605
|
2
|
|
|
|
|
98
|
any ['get', 'post'] => $url => _ensure_auth( |
606
|
|
|
|
|
|
|
'edit', $handler, \%args |
607
|
|
|
|
|
|
|
); |
608
|
|
|
|
|
|
|
} |
609
|
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
# And a route to list records already in the table: |
612
|
|
|
|
|
|
|
my $list_handler |
613
|
|
|
|
|
|
|
= _ensure_auth( |
614
|
|
|
|
|
|
|
'view', |
615
|
14
|
|
|
14
|
|
247493
|
sub { _create_list_handler(\%args, $table_name, $key_column); }, |
616
|
8
|
|
|
|
|
1359
|
\%args, |
617
|
|
|
|
|
|
|
); |
618
|
|
|
|
|
|
|
get _construct_url( |
619
|
|
|
|
|
|
|
$args{prefix}, |
620
|
8
|
|
|
|
|
28
|
) => $list_handler; |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
# If we should allow deletion of records, set up routes to handle that, |
623
|
|
|
|
|
|
|
# too. |
624
|
8
|
50
|
66
|
|
|
3535
|
if ($args{editable} && $args{deletable}) { |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
# A route for GET requests, to present a "Do you want to delete this" |
627
|
|
|
|
|
|
|
# message with a form to submit (this is only for browsers which didn't |
628
|
|
|
|
|
|
|
# support Javascript, otherwise the list page will have POSTed the ID |
629
|
|
|
|
|
|
|
# to us) (or they just came here directly for some reason) |
630
|
|
|
|
|
|
|
get _construct_url( |
631
|
|
|
|
|
|
|
$args{prefix}, "/delete/:id" |
632
|
|
|
|
|
|
|
) => sub { |
633
|
0
|
|
|
0
|
|
0
|
return _apply_template(<
|
634
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
Do you really wish to delete this record? |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
CONFIRMDELETE |
643
|
|
|
|
|
|
|
|
644
|
0
|
|
|
|
|
0
|
}; |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
# A route for POST requests, to actually delete the record |
647
|
|
|
|
|
|
|
my $del_url_stub = _construct_url( |
648
|
0
|
|
|
|
|
0
|
$args{prefix}, '/delete' |
649
|
|
|
|
|
|
|
); |
650
|
|
|
|
|
|
|
my $delete_handler = sub { |
651
|
0
|
|
0
|
0
|
|
0
|
my ($id) = params->{record_id} || splat; |
652
|
0
|
|
|
|
|
0
|
my $dbh = _database(\%args); |
653
|
0
|
|
|
|
|
0
|
my $where = _get_where_filter_from_args(\%args); |
654
|
0
|
|
|
|
|
0
|
$where->{$key_column} = $id; |
655
|
|
|
|
|
|
|
|
656
|
0
|
|
|
|
|
0
|
my %params = params; |
657
|
0
|
|
|
|
|
0
|
my $meta_for_hook = { |
658
|
|
|
|
|
|
|
args => \%args, |
659
|
|
|
|
|
|
|
params => \%params, |
660
|
|
|
|
|
|
|
table_name => $table_name, |
661
|
|
|
|
|
|
|
key_column => $key_column, |
662
|
|
|
|
|
|
|
}; |
663
|
|
|
|
|
|
|
# fire the pre-delete hook, in case user wants to perform trickery before the delete |
664
|
0
|
|
|
|
|
0
|
execute_hook('delete_row_pre_delete', $meta_for_hook ); |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
my $rows_deleted = $dbh->quick_delete($table_name, $where) |
667
|
|
|
|
|
|
|
or return _apply_template(" Failed to delete! ", |
668
|
0
|
0
|
|
|
|
0
|
$args{'template'}); |
669
|
|
|
|
|
|
|
|
670
|
0
|
|
|
|
|
0
|
$meta_for_hook->{success} = $rows_deleted; |
671
|
0
|
0
|
|
|
|
0
|
if ($rows_deleted) { |
672
|
|
|
|
|
|
|
# post-delete hook, in case user wants to do cunning things after the delete |
673
|
0
|
|
|
|
|
0
|
execute_hook('delete_row_post_delete', $meta_for_hook ); |
674
|
|
|
|
|
|
|
} |
675
|
|
|
|
|
|
|
|
676
|
0
|
|
|
|
|
0
|
redirect _external_url($args{dancer_prefix}, $args{prefix}); |
677
|
0
|
|
|
|
|
0
|
}; |
678
|
0
|
|
|
|
|
0
|
post qr[$del_url_stub/?(.+)?$] => _ensure_auth( |
679
|
|
|
|
|
|
|
'edit', $delete_handler, \%args |
680
|
|
|
|
|
|
|
); |
681
|
|
|
|
|
|
|
} |
682
|
|
|
|
|
|
|
my $view_url_stub = _construct_url( |
683
|
8
|
|
|
|
|
20
|
$args{prefix}, '/view' |
684
|
|
|
|
|
|
|
); |
685
|
|
|
|
|
|
|
my $view_handler = _ensure_auth( |
686
|
|
|
|
|
|
|
'view', |
687
|
2
|
|
|
2
|
|
7254
|
sub { _create_view_handler(\%args, $table_name, $key_column); }, |
688
|
8
|
|
|
|
|
40
|
\%args, |
689
|
|
|
|
|
|
|
); |
690
|
|
|
|
|
|
|
|
691
|
8
|
|
|
|
|
48
|
get $view_url_stub.'/:id' => $view_handler; |
692
|
|
|
|
|
|
|
} |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
sub _create_view_handler { |
695
|
2
|
|
|
2
|
|
6
|
my ($args, $table_name, $key_column) = @_; |
696
|
2
|
|
|
|
|
7
|
my $params = params; |
697
|
2
|
50
|
|
|
|
332
|
my $id = $params->{id} or return _apply_template(" Need id to view! ", $args->{'template'}); |
698
|
|
|
|
|
|
|
|
699
|
2
|
|
|
|
|
7
|
my $dbh = _database($args); |
700
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
# a hash containing the current values in the database. Take where_filter |
702
|
|
|
|
|
|
|
# into account, so we can't fetch a row if it doesn't match the filter |
703
|
|
|
|
|
|
|
# (otherwise people could load any record they wished just by changing the |
704
|
|
|
|
|
|
|
# ID in the URL, which would be considered a Bad Thing) |
705
|
2
|
|
|
|
|
153
|
my $where = _get_where_filter_from_args($args); |
706
|
2
|
|
|
|
|
8
|
$where->{$key_column} = $id; |
707
|
2
|
|
|
|
|
9
|
my $values_from_database = $dbh->quick_select($table_name, $where); |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
# Find out about table columns: |
710
|
2
|
|
|
|
|
609
|
my $all_table_columns = _find_columns($dbh, $args->{db_table}); |
711
|
2
|
|
|
|
|
15
|
my @rows = (['Column Name', 'Value']); |
712
|
2
|
|
|
|
|
16
|
my $table = HTML::Table->new( -border=>1 ); |
713
|
2
|
|
|
|
|
139
|
$table->addSectionRow('thead', 0, 'Column Name', 'Value'); |
714
|
2
|
|
|
|
|
69
|
$table->setSectionCellHead('thead', 0, 1, 1, 1); |
715
|
2
|
|
|
|
|
57
|
$table->setSectionCellHead('thead', 0, 1, 2, 1); |
716
|
2
|
|
|
|
|
40
|
foreach my $col (@$all_table_columns) { |
717
|
6
|
|
|
|
|
96
|
$table->addSectionRow('tbody', 0, $col->{COLUMN_NAME}, $values_from_database->{$col->{COLUMN_NAME}}); |
718
|
|
|
|
|
|
|
} |
719
|
2
|
|
50
|
|
|
46
|
my $html = $table->getTable || ''; |
720
|
2
|
|
|
|
|
843
|
return _apply_template($html, $args->{'template'}); |
721
|
|
|
|
|
|
|
} |
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
register simple_crud => \&simple_crud; |
724
|
|
|
|
|
|
|
register_hook(qw( |
725
|
|
|
|
|
|
|
add_edit_row |
726
|
|
|
|
|
|
|
add_edit_row_pre_save |
727
|
|
|
|
|
|
|
add_edit_row_post_save |
728
|
|
|
|
|
|
|
delete_row_pre_delete |
729
|
|
|
|
|
|
|
delete_row_post_delete |
730
|
|
|
|
|
|
|
)); |
731
|
|
|
|
|
|
|
register_plugin; |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
sub _database { |
735
|
30
|
|
|
30
|
|
73
|
my $args = shift; |
736
|
30
|
|
50
|
|
|
98
|
my $provider = $args->{db_connection_provider} || "Database"; |
737
|
30
|
50
|
|
|
|
78
|
if ($provider eq "Database") { |
738
|
30
|
|
|
|
|
166
|
return Dancer::Plugin::Database::database($args->{db_connection_name}); # D:P:Database already loaded |
739
|
|
|
|
|
|
|
} |
740
|
0
|
0
|
|
|
|
0
|
if ($provider eq "DBIC") { |
741
|
0
|
|
|
|
|
0
|
require Dancer::Plugin::DBIC; |
742
|
0
|
|
|
|
|
0
|
my $dbh = Dancer::Plugin::DBIC::schema($args->{db_connection_name})->storage->dbh; |
743
|
0
|
|
|
|
|
0
|
return bless $dbh => 'Dancer::Plugin::Database::Core::Handle'; # so we can use ->quick_update/_insert |
744
|
|
|
|
|
|
|
} |
745
|
0
|
|
|
|
|
0
|
die "db_connection_provider can be 'Database' or 'DBIC'. Don't understand '$provider'"; |
746
|
|
|
|
|
|
|
} |
747
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
sub _create_add_edit_route { |
749
|
6
|
|
|
6
|
|
21
|
my ($args, $table_name, $key_column) = @_; |
750
|
6
|
|
|
|
|
21
|
my $params = params; |
751
|
6
|
|
|
|
|
1119
|
my $id = $params->{id}; |
752
|
|
|
|
|
|
|
|
753
|
6
|
|
|
|
|
23
|
my $dbh = _database($args); |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
# a hash containing the current values in the database |
756
|
6
|
|
|
|
|
483
|
my $values_from_database; |
757
|
6
|
100
|
|
|
|
24
|
if (defined $id) { |
758
|
5
|
|
|
|
|
14
|
my $where = _get_where_filter_from_args($args); |
759
|
5
|
|
|
|
|
15
|
$where->{$key_column} = $id; |
760
|
5
|
|
|
|
|
32
|
$values_from_database |
761
|
|
|
|
|
|
|
= $dbh->quick_select($table_name, $where); |
762
|
5
|
100
|
|
|
|
1756
|
if (!$values_from_database) { |
763
|
3
|
|
|
|
|
22
|
send_error "$args->{record_title} $id not found", 404; |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
} |
766
|
|
|
|
|
|
|
} |
767
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
# Find out about table columns: |
769
|
3
|
|
|
|
|
14
|
my $all_table_columns = _find_columns($dbh, $args->{db_table}); |
770
|
3
|
|
|
|
|
9
|
my @editable_columns; |
771
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
# Now, find out which ones we can edit. |
773
|
3
|
50
|
|
|
|
13
|
if ($args->{editable_columns}) { |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
# We were given an explicit list of fields we can edit, so this is |
776
|
|
|
|
|
|
|
# easy: |
777
|
0
|
|
|
|
|
0
|
@editable_columns = @{ $args->{editable_columns} }; |
|
0
|
|
|
|
|
0
|
|
778
|
|
|
|
|
|
|
} else { |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
# OK, take all the columns from the table, except the key field: |
781
|
9
|
|
|
|
|
22
|
@editable_columns = grep { $_ ne $key_column } |
782
|
3
|
|
|
|
|
10
|
map { $_->{COLUMN_NAME} } @$all_table_columns; |
|
9
|
|
|
|
|
23
|
|
783
|
|
|
|
|
|
|
} |
784
|
|
|
|
|
|
|
|
785
|
3
|
50
|
|
|
|
11
|
if ($args->{not_editable_columns}) { |
786
|
0
|
|
|
|
|
0
|
for my $col (@{ $args->{not_editable_columns} }) { |
|
0
|
|
|
|
|
0
|
|
787
|
0
|
|
|
|
|
0
|
@editable_columns = grep { $_ ne $col } @editable_columns; |
|
0
|
|
|
|
|
0
|
|
788
|
|
|
|
|
|
|
} |
789
|
|
|
|
|
|
|
} |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
# Some DWIMery: if we don't have a validation rule specified for a |
792
|
|
|
|
|
|
|
# field, and it's pretty clear what it is supposed to be, just do it: |
793
|
3
|
|
100
|
|
|
16
|
my $validation = $args->{validation} || {}; |
794
|
3
|
|
|
|
|
8
|
for my $field (grep { $_ ne $key_column } @editable_columns) { |
|
6
|
|
|
|
|
16
|
|
795
|
6
|
50
|
|
|
|
13
|
next if $validation->{$field}; |
796
|
6
|
50
|
|
|
|
21
|
if ($field =~ /email/) { |
797
|
0
|
|
|
|
|
0
|
$validation->{$field} = 'EMAIL'; |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
} |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
# More DWIMmery: if the user hasn't supplied a list of required fields, |
802
|
|
|
|
|
|
|
# work out what fields are required by whether they're nullable in the |
803
|
|
|
|
|
|
|
# DB: |
804
|
3
|
|
|
|
|
6
|
my %required_fields; |
805
|
3
|
50
|
|
|
|
9
|
if (exists $args->{required}) { |
806
|
0
|
|
|
|
|
0
|
$required_fields{$_}++ for @{ $args->{required} }; |
|
0
|
|
|
|
|
0
|
|
807
|
|
|
|
|
|
|
} else { |
808
|
|
|
|
|
|
|
$_->{NULLABLE} || $required_fields{ $_->{COLUMN_NAME} }++ |
809
|
3
|
|
33
|
|
|
16
|
for @$all_table_columns; |
810
|
|
|
|
|
|
|
} |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
# If the user didn't supply a list of acceptable values for a field, but |
813
|
|
|
|
|
|
|
# it's an ENUM column, use the possible values declared in the ENUM. |
814
|
|
|
|
|
|
|
# Also remember field types for easy reference later |
815
|
3
|
|
|
|
|
10
|
my %constrain_values; |
816
|
|
|
|
|
|
|
my %field_type; |
817
|
3
|
|
|
|
|
8
|
for my $field (@$all_table_columns) { |
818
|
9
|
|
|
|
|
17
|
my $name = $field->{COLUMN_NAME}; |
819
|
9
|
|
|
|
|
20
|
$field_type{$name} = $field->{TYPE_NAME}; |
820
|
9
|
50
|
33
|
|
|
52
|
if (my $values_specified = $args->{acceptable_values}->{$name}) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
# It may have been given to us as a coderef; if so, execute it to |
823
|
|
|
|
|
|
|
# get the results |
824
|
0
|
0
|
|
|
|
0
|
if (ref $values_specified eq 'CODE') { |
825
|
0
|
|
|
|
|
0
|
$values_specified = $values_specified->(); |
826
|
|
|
|
|
|
|
} |
827
|
0
|
|
|
|
|
0
|
$constrain_values{$name} = $values_specified; |
828
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
} elsif (my $foreign_key = $args->{foreign_keys}{$name}) { |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
# Find out the possible values for this column from the other table: |
832
|
0
|
|
|
|
|
0
|
my %possible_values; |
833
|
0
|
|
|
|
|
0
|
debug "Looking for rows for foreign relation: " => $foreign_key; |
834
|
0
|
|
|
|
|
0
|
for my $row ($dbh->quick_select($foreign_key->{table}, {})) { |
835
|
0
|
|
|
|
|
0
|
debug "Row from foreign relation: " => $row; |
836
|
|
|
|
|
|
|
$possible_values{ $row->{ $foreign_key->{key_column} } } |
837
|
0
|
|
|
|
|
0
|
= $row->{ $foreign_key->{label_column} }; |
838
|
|
|
|
|
|
|
} |
839
|
0
|
|
|
|
|
0
|
$constrain_values{$name} = \%possible_values; |
840
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
} elsif (my $values_from_db = $field->{mysql_values} |
842
|
|
|
|
|
|
|
|| $field->{mariadb_values}) { |
843
|
0
|
|
|
|
|
0
|
$constrain_values{$name} = $values_from_db; |
844
|
|
|
|
|
|
|
} |
845
|
|
|
|
|
|
|
} |
846
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
# Only give CGI::FormBuilder our fake CGI object if the form has been |
848
|
|
|
|
|
|
|
# POSTed to us already; otherwise, it will ignore default values from |
849
|
|
|
|
|
|
|
# the DB, it seems. |
850
|
|
|
|
|
|
|
my $paramsobj |
851
|
3
|
50
|
|
|
|
16
|
= request->{method} eq 'POST' |
852
|
|
|
|
|
|
|
? Dancer::Plugin::SimpleCRUD::ParamsObject->new({ params() }) |
853
|
|
|
|
|
|
|
: undef; |
854
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
my $form = CGI::FormBuilder->new( |
856
|
|
|
|
|
|
|
fields => \@editable_columns, |
857
|
|
|
|
|
|
|
params => $paramsobj, |
858
|
|
|
|
|
|
|
values => $values_from_database, |
859
|
|
|
|
|
|
|
validate => $validation, |
860
|
|
|
|
|
|
|
method => 'post', |
861
|
|
|
|
|
|
|
action => _external_url( |
862
|
|
|
|
|
|
|
$args->{dancer_prefix}, |
863
|
|
|
|
|
|
|
$args->{prefix}, |
864
|
|
|
|
|
|
|
( |
865
|
|
|
|
|
|
|
params->{id} |
866
|
|
|
|
|
|
|
? '/edit/' . params->{id} |
867
|
3
|
100
|
|
|
|
40
|
: '/add' |
868
|
|
|
|
|
|
|
) |
869
|
|
|
|
|
|
|
), |
870
|
|
|
|
|
|
|
); |
871
|
3
|
|
|
|
|
41677
|
for my $field (@editable_columns) { |
872
|
|
|
|
|
|
|
# first check if there's data from the database for this field, |
873
|
|
|
|
|
|
|
# then if there's a value in params() for this field, |
874
|
|
|
|
|
|
|
# then if args->{default_value} was set for this field via the |
875
|
|
|
|
|
|
|
# 'default_value' hash when the route was created. |
876
|
|
|
|
|
|
|
my $default = |
877
|
|
|
|
|
|
|
exists $values_from_database->{$field} |
878
|
|
|
|
|
|
|
? $values_from_database->{$field} |
879
|
|
|
|
|
|
|
: exists params->{$field} |
880
|
|
|
|
|
|
|
? uri_unescape(params->{$field}) |
881
|
|
|
|
|
|
|
: exists $args->{default_value}->{$field} |
882
|
6
|
50
|
|
|
|
595
|
? $args->{default_value}->{$field} |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
883
|
|
|
|
|
|
|
: ''; |
884
|
6
|
|
|
|
|
67
|
my %field_params = ( |
885
|
|
|
|
|
|
|
name => $field, |
886
|
|
|
|
|
|
|
value => $default, |
887
|
|
|
|
|
|
|
); |
888
|
|
|
|
|
|
|
|
889
|
6
|
|
|
|
|
15
|
$field_params{required} = $required_fields{$field}; |
890
|
|
|
|
|
|
|
|
891
|
6
|
50
|
|
|
|
71
|
if ($constrain_values{$field}) { |
892
|
0
|
|
|
|
|
0
|
$field_params{options} = $constrain_values{$field}; |
893
|
|
|
|
|
|
|
} |
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
# Certain options in $args simply cause that value to be added to the |
896
|
|
|
|
|
|
|
# params for this field we'll pass to $form->field: |
897
|
6
|
|
|
|
|
30
|
my %option_map = ( |
898
|
|
|
|
|
|
|
labels => 'label', |
899
|
|
|
|
|
|
|
validation => 'validate', |
900
|
|
|
|
|
|
|
message => 'message', |
901
|
|
|
|
|
|
|
jsmessage => 'jsmessage', |
902
|
|
|
|
|
|
|
sort_options => 'sortopts', |
903
|
|
|
|
|
|
|
); |
904
|
6
|
|
|
|
|
23
|
while (my ($arg_name, $field_param_name) = each(%option_map)) { |
905
|
30
|
50
|
|
|
|
96
|
if (my $val = $args->{$arg_name}{$field}) { |
906
|
0
|
|
|
|
|
0
|
$field_params{$field_param_name} = $val; |
907
|
|
|
|
|
|
|
} |
908
|
|
|
|
|
|
|
} |
909
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
# Normally, CGI::FormBuilder can guess the type of field perfectly, |
911
|
|
|
|
|
|
|
# but give it some extra DWIMmy help: |
912
|
6
|
100
|
|
|
|
43
|
if ($field =~ /pass(?:wd|word)?$/i) { |
913
|
3
|
|
|
|
|
9
|
$field_params{type} = 'password'; |
914
|
|
|
|
|
|
|
} |
915
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
# use a |
917
|
6
|
50
|
|
|
|
19
|
if ($field_type{$field} eq 'TEXT') { |
918
|
0
|
|
|
|
|
0
|
$field_params{type} = 'textarea'; |
919
|
|
|
|
|
|
|
} |
920
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
# ... unless the user specified a type for this field, in which case, |
922
|
|
|
|
|
|
|
# use what they said |
923
|
6
|
50
|
|
|
|
16
|
if (my $override_type = $args->{input_types}{$field}) { |
924
|
0
|
|
|
|
|
0
|
$field_params{type} = $override_type; |
925
|
|
|
|
|
|
|
} |
926
|
|
|
|
|
|
|
|
927
|
|
|
|
|
|
|
# if the constraint on this is an array of arrays, |
928
|
|
|
|
|
|
|
# and there are three elements in the first array in that list, |
929
|
|
|
|
|
|
|
# (which will be intepreted as: value, label, category) |
930
|
|
|
|
|
|
|
# we are going to assume you want optgroups, with the |
931
|
|
|
|
|
|
|
# third element in each being the category. |
932
|
|
|
|
|
|
|
# |
933
|
|
|
|
|
|
|
# (See the optgroups option in CGI::FormBuilder) |
934
|
6
|
50
|
|
|
|
16
|
if (ref($field_params{options}) eq 'ARRAY') { |
935
|
0
|
0
|
|
|
|
0
|
if (ref( $field_params{options}->[0] ) eq 'ARRAY') { |
936
|
0
|
0
|
|
|
|
0
|
if (@{ $field_params{options}->[0] } == 3) { |
|
0
|
|
|
|
|
0
|
|
937
|
0
|
|
|
|
|
0
|
$field_params{optgroups} = 1; |
938
|
|
|
|
|
|
|
} |
939
|
|
|
|
|
|
|
} |
940
|
|
|
|
|
|
|
} |
941
|
|
|
|
|
|
|
|
942
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
# OK, add the field to the form: |
944
|
6
|
|
|
|
|
28
|
$form->field(%field_params); |
945
|
|
|
|
|
|
|
} |
946
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
# Now, if all is OK, go ahead and process: |
948
|
3
|
50
|
33
|
|
|
537
|
if (request->{method} eq 'POST' && $form->submitted && $form->validate) { |
|
|
|
33
|
|
|
|
|
949
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
# Assemble a hash of only fields from the DB (if other fields were |
951
|
|
|
|
|
|
|
# submitted with the form which don't belong in the DB, ignore them) |
952
|
0
|
|
|
|
|
0
|
my %params; |
953
|
0
|
|
|
|
|
0
|
$params{$_} = params('body')->{$_} for @editable_columns; |
954
|
|
|
|
|
|
|
|
955
|
0
|
|
|
|
|
0
|
my $meta_for_hook = { |
956
|
|
|
|
|
|
|
args => $args, |
957
|
|
|
|
|
|
|
params => \%params, |
958
|
|
|
|
|
|
|
table_name => $table_name, |
959
|
|
|
|
|
|
|
key_column => $key_column, |
960
|
|
|
|
|
|
|
}; |
961
|
|
|
|
|
|
|
# Fire a hook so the user can manipulate the data in a whole range of |
962
|
|
|
|
|
|
|
# cunning ways, if they wish |
963
|
0
|
|
|
|
|
0
|
execute_hook('add_edit_row', \%params); |
964
|
0
|
|
|
|
|
0
|
execute_hook('add_edit_row_pre_save', $meta_for_hook); |
965
|
|
|
|
|
|
|
|
966
|
0
|
|
|
|
|
0
|
my $verb; |
967
|
|
|
|
|
|
|
my $success; |
968
|
0
|
0
|
|
|
|
0
|
if (exists params('route')->{id}) { |
969
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
# We're editing an existing record - make sure the WHERE clause |
971
|
|
|
|
|
|
|
# hashref incorporates where_filter, if in use, so that users can't |
972
|
|
|
|
|
|
|
# edit stuff they shouldn't be able to |
973
|
0
|
|
|
|
|
0
|
my $where = _get_where_filter_from_args($args); |
974
|
0
|
|
|
|
|
0
|
$where->{$key_column} = params('route')->{id}; |
975
|
0
|
|
|
|
|
0
|
$success = $dbh->quick_update($table_name, $where, \%params); |
976
|
0
|
|
|
|
|
0
|
$verb = 'update'; |
977
|
|
|
|
|
|
|
} else { |
978
|
0
|
|
|
|
|
0
|
$success = $dbh->quick_insert($table_name, \%params); |
979
|
|
|
|
|
|
|
# pass them *this* dbh instance so that they can call last_insert_id() |
980
|
|
|
|
|
|
|
# against it if they need to. last_insert_id in some instances requires |
981
|
|
|
|
|
|
|
# catalog, schema, etc args, so we can't just call it and save the result. |
982
|
|
|
|
|
|
|
# important that we don't do any more database operations that would change |
983
|
|
|
|
|
|
|
# last_insert_id between here and the hook, or this won't work. |
984
|
0
|
|
|
|
|
0
|
$meta_for_hook->{dbh} = $dbh; |
985
|
0
|
|
|
|
|
0
|
$verb = 'create new'; |
986
|
|
|
|
|
|
|
} |
987
|
|
|
|
|
|
|
|
988
|
0
|
|
|
|
|
0
|
$meta_for_hook->{success} = $success; |
989
|
0
|
|
|
|
|
0
|
$meta_for_hook->{verb} = $verb; |
990
|
0
|
0
|
|
|
|
0
|
if ($success) { |
991
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
# Redirect to the list page |
993
|
|
|
|
|
|
|
# TODO: pass a param to cause it to show a message? |
994
|
0
|
|
|
|
|
0
|
execute_hook('add_edit_row_post_save', $meta_for_hook); |
995
|
0
|
|
|
|
|
0
|
redirect _external_url($args->{dancer_prefix}, $args->{prefix}); |
996
|
0
|
|
|
|
|
0
|
return; |
997
|
|
|
|
|
|
|
} else { |
998
|
0
|
|
|
|
|
0
|
execute_hook('add_edit_row_post_save', $meta_for_hook); |
999
|
|
|
|
|
|
|
# TODO: better error handling - options to provide error templates |
1000
|
|
|
|
|
|
|
# etc |
1001
|
|
|
|
|
|
|
# (below is one approach to that TODO--this, or perhaps the hook could return a hash |
1002
|
|
|
|
|
|
|
# that would specify these overrides? Probably best to come up with a complete mechanism |
1003
|
|
|
|
|
|
|
# consistent across hooks before we implement.) |
1004
|
|
|
|
|
|
|
# return _apply_template( |
1005
|
|
|
|
|
|
|
# $meta_for_hook->{return}{error_message} || " Unable to $verb $args->{record_title} ", |
1006
|
|
|
|
|
|
|
# $meta_for_hook->{return}{error_template} || $args->{error_template} || $args->{'template'} |
1007
|
|
|
|
|
|
|
#); |
1008
|
|
|
|
|
|
|
return _apply_template( |
1009
|
|
|
|
|
|
|
" Unable to $verb $args->{record_title} ", |
1010
|
0
|
|
|
|
|
0
|
$args->{'template'}, $args->{'record_title'}); |
1011
|
|
|
|
|
|
|
} |
1012
|
|
|
|
|
|
|
|
1013
|
|
|
|
|
|
|
} else { |
1014
|
3
|
|
|
|
|
41
|
return _apply_template($form->render, $args->{'template'}, $args->{'record_title'}); |
1015
|
|
|
|
|
|
|
} |
1016
|
|
|
|
|
|
|
} |
1017
|
|
|
|
|
|
|
|
1018
|
|
|
|
|
|
|
sub _create_list_handler { |
1019
|
14
|
|
|
14
|
|
41
|
my ($args, $table_name, $key_column) = @_; |
1020
|
|
|
|
|
|
|
|
1021
|
14
|
|
|
|
|
35
|
my $dbh = _database($args); |
1022
|
14
|
|
|
|
|
1179
|
my $columns = _find_columns($dbh, $table_name); |
1023
|
|
|
|
|
|
|
|
1024
|
14
|
|
|
|
|
49
|
my $display_columns = $args->{'display_columns'}; |
1025
|
|
|
|
|
|
|
|
1026
|
14
|
|
50
|
|
|
71
|
my $table_class = $args->{'table_class'} || ''; |
1027
|
14
|
|
50
|
|
|
58
|
my $paginate_table_class = $args->{'paginate_table_class'} || ''; |
1028
|
|
|
|
|
|
|
|
1029
|
|
|
|
|
|
|
# If display_columns argument was passed, filter the column list to only |
1030
|
|
|
|
|
|
|
# have the ones we asked for. |
1031
|
14
|
50
|
|
|
|
45
|
if (ref $display_columns eq 'ARRAY') { |
1032
|
0
|
|
|
|
|
0
|
my @filtered_columns; |
1033
|
|
|
|
|
|
|
|
1034
|
0
|
|
|
|
|
0
|
foreach my $col (@$columns) { |
1035
|
0
|
0
|
|
|
|
0
|
if (grep { $_ eq $col->{'COLUMN_NAME'} } @$display_columns) { |
|
0
|
|
|
|
|
0
|
|
1036
|
0
|
|
|
|
|
0
|
push @filtered_columns, $col; |
1037
|
|
|
|
|
|
|
} |
1038
|
|
|
|
|
|
|
} |
1039
|
|
|
|
|
|
|
|
1040
|
0
|
0
|
|
|
|
0
|
if (@filtered_columns) { |
1041
|
0
|
|
|
|
|
0
|
$columns = \@filtered_columns; |
1042
|
|
|
|
|
|
|
} |
1043
|
|
|
|
|
|
|
} |
1044
|
|
|
|
|
|
|
|
1045
|
14
|
|
66
|
|
|
62
|
my $searchfield = params->{searchfield} || $key_column; |
1046
|
|
|
|
|
|
|
my $searchfield_options = join( |
1047
|
|
|
|
|
|
|
"\n", |
1048
|
|
|
|
|
|
|
map { |
1049
|
14
|
|
|
|
|
2054
|
my $friendly_name = $_->{COLUMN_NAME}; |
|
42
|
|
|
|
|
78
|
|
1050
|
42
|
50
|
|
|
|
102
|
if ($args->{labels}{$_->{COLUMN_NAME}}) { |
1051
|
0
|
|
|
|
|
0
|
$friendly_name = $args->{labels}{$_->{COLUMN_NAME}}; |
1052
|
|
|
|
|
|
|
} |
1053
|
|
|
|
|
|
|
my $sel |
1054
|
|
|
|
|
|
|
= ($searchfield eq $_->{COLUMN_NAME}) |
1055
|
42
|
100
|
|
|
|
100
|
? "selected" |
1056
|
|
|
|
|
|
|
: ""; |
1057
|
42
|
|
|
|
|
167
|
"" |
1058
|
|
|
|
|
|
|
} @$columns |
1059
|
|
|
|
|
|
|
); |
1060
|
14
|
|
|
|
|
30
|
my $default_searchtype = "e"; |
1061
|
14
|
|
|
|
|
191
|
my @searchtypes = ( |
1062
|
|
|
|
|
|
|
[ e => { name => "Equals", cmp => "=" } ], |
1063
|
|
|
|
|
|
|
[ c => { name => "Contains", cmp => "like" } ], |
1064
|
|
|
|
|
|
|
[ b => { name => "Begins With", cmp => "like" } ], |
1065
|
|
|
|
|
|
|
[ ne => { name => "Does Not Equal", cmp => "!=" } ], |
1066
|
|
|
|
|
|
|
[ nc => { name => "Does Not Contain", cmp => "not like" } ], |
1067
|
|
|
|
|
|
|
|
1068
|
|
|
|
|
|
|
[ lt => { name => "Less Than", cmp => "<" } ], |
1069
|
|
|
|
|
|
|
[ lte => { name => "Less Than or Equal To", cmp => "<=" } ], |
1070
|
|
|
|
|
|
|
[ gt => { name => "Greater Than", cmp => ">" } ], |
1071
|
|
|
|
|
|
|
[ gte => { name => "Greater Than or Equal To", cmp => ">=" } ], |
1072
|
|
|
|
|
|
|
|
1073
|
|
|
|
|
|
|
[ like => { name => "Like", cmp => "LIKE" } ], |
1074
|
|
|
|
|
|
|
); |
1075
|
|
|
|
|
|
|
my $searchtype_options = join( "\n", |
1076
|
|
|
|
|
|
|
map { |
1077
|
14
|
|
|
|
|
37
|
my ($search_code, $hashref) = @$_; |
|
140
|
|
|
|
|
281
|
|
1078
|
140
|
|
|
|
|
216
|
my $name = $hashref->{name}; |
1079
|
140
|
|
66
|
|
|
273
|
my $sel = (params->{searchtype} || $default_searchtype) eq $search_code; |
1080
|
140
|
100
|
|
|
|
2323
|
sprintf("", $search_code, $sel ? " selected" : "", $name); |
1081
|
|
|
|
|
|
|
} @searchtypes |
1082
|
|
|
|
|
|
|
); |
1083
|
|
|
|
|
|
|
|
1084
|
14
|
|
50
|
|
|
64
|
my $order_by_param = params->{'o'} || ""; |
1085
|
14
|
|
50
|
|
|
219
|
my $order_by_direction = params->{'d'} || ""; |
1086
|
14
|
|
|
|
|
222
|
my $q = _defined_or_empty(params->{'q'}); |
1087
|
14
|
|
|
|
|
61
|
my $display_q = encode_entities( $q ); |
1088
|
14
|
|
|
|
|
284
|
my $html = <<"SEARCHFORM"; |
1089
|
|
|
|
|
|
|
|
1090
|
|
|
|
|
|
|
Field: |
1091
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
|
1093
|
|
|
|
|
|
|
|
1094
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
|
1097
|
|
|
|
|
|
|
SEARCHFORM |
1098
|
|
|
|
|
|
|
|
1099
|
14
|
50
|
|
|
|
42
|
if ($args->{query_auto_focus}) { |
1100
|
14
|
|
|
|
|
26
|
$html |
1101
|
|
|
|
|
|
|
.= ""; |
1102
|
|
|
|
|
|
|
} |
1103
|
|
|
|
|
|
|
|
1104
|
|
|
|
|
|
|
# Explicitly select the columns we are displaying. (May have been filtered |
1105
|
|
|
|
|
|
|
# by display_columns above.) |
1106
|
|
|
|
|
|
|
|
1107
|
14
|
|
|
|
|
35
|
my @select_cols = map { $_->{COLUMN_NAME} } @$columns; |
|
42
|
|
|
|
|
90
|
|
1108
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
# If we have some columns declared as foreign keys, though, we don't want to |
1110
|
|
|
|
|
|
|
# see the raw values in the result; we'll add JOIN clauses to fetch the info |
1111
|
|
|
|
|
|
|
# from the related table, so for now just select the defined label column |
1112
|
|
|
|
|
|
|
# from the related table instead of the raw ID value. |
1113
|
|
|
|
|
|
|
|
1114
|
|
|
|
|
|
|
# This _as_simplecrud_fk_ mechanism is clearly a bit of a hack. At some point we |
1115
|
|
|
|
|
|
|
# might want to pull in an existing solution for this--this is simple and |
1116
|
|
|
|
|
|
|
# may have pitfalls that have already been solved in Catalyst/DBIC code. |
1117
|
|
|
|
|
|
|
# For now, we're going with simple. git show 14cec4ea647 to see the |
1118
|
|
|
|
|
|
|
# basic change (that's previous to the add of LEFT to the JOIN, though), if you want |
1119
|
|
|
|
|
|
|
# to know exactly what to pull out when replacing this |
1120
|
|
|
|
|
|
|
|
1121
|
14
|
|
|
|
|
29
|
my @foreign_cols; |
1122
|
|
|
|
|
|
|
my %fk_alias; # foreign key aliases for cases where we might have collisions |
1123
|
14
|
100
|
|
|
|
41
|
if ($args->{foreign_keys}) { |
1124
|
1
|
|
|
|
|
10
|
my $seen_table = {$table_name=>1}; |
1125
|
1
|
|
|
|
|
3
|
while (my ($col, $foreign_key) = each(%{ $args->{foreign_keys} })) { |
|
1
|
|
|
|
|
6
|
|
1126
|
0
|
|
|
|
|
0
|
@select_cols = grep { $_ ne $col } @select_cols; |
|
0
|
|
|
|
|
0
|
|
1127
|
0
|
|
|
|
|
0
|
my $raw_ftable = $foreign_key->{table}; |
1128
|
0
|
|
|
|
|
0
|
my $ftable_alias; |
1129
|
0
|
0
|
|
|
|
0
|
if ($seen_table->{$raw_ftable}++) { |
1130
|
0
|
|
|
|
|
0
|
$ftable_alias = $fk_alias{ $col } = $dbh->quote_identifier($raw_ftable. "_as_simplecrud_fk_$seen_table->{$raw_ftable}"); |
1131
|
|
|
|
|
|
|
} |
1132
|
0
|
|
|
|
|
0
|
my $ftable = $dbh->quote_identifier($raw_ftable); |
1133
|
|
|
|
|
|
|
my $fcol |
1134
|
0
|
|
|
|
|
0
|
= $dbh->quote_identifier($foreign_key->{label_column}); |
1135
|
|
|
|
|
|
|
my $lcol |
1136
|
0
|
|
0
|
|
|
0
|
= $dbh->quote_identifier($args->{labels}{$col} || $col); |
1137
|
|
|
|
|
|
|
|
1138
|
0
|
|
0
|
|
|
0
|
my $table_or_alias = $fk_alias{ $col } || $ftable; |
1139
|
0
|
|
|
|
|
0
|
push @foreign_cols, "$table_or_alias.$fcol AS $lcol"; |
1140
|
|
|
|
|
|
|
} |
1141
|
|
|
|
|
|
|
} |
1142
|
|
|
|
|
|
|
|
1143
|
14
|
|
|
|
|
23
|
my @custom_cols; |
1144
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
# For backwards compatibility, understand custom_columns being a hashref, |
1146
|
|
|
|
|
|
|
# and translate it |
1147
|
14
|
50
|
|
|
|
43
|
if (ref $args->{custom_columns} eq 'HASH') { |
1148
|
0
|
|
|
|
|
0
|
my @custom_cols_list; |
1149
|
0
|
|
|
|
|
0
|
for my $column_alias (keys %{ $args->{custom_columns} }) { |
|
0
|
|
|
|
|
0
|
|
1150
|
|
|
|
|
|
|
push @custom_cols_list, { |
1151
|
|
|
|
|
|
|
name => $column_alias, |
1152
|
0
|
|
|
|
|
0
|
%{ $args->{custom_columns}{$column_alias} } |
|
0
|
|
|
|
|
0
|
|
1153
|
|
|
|
|
|
|
}; |
1154
|
|
|
|
|
|
|
} |
1155
|
0
|
|
|
|
|
0
|
$args->{custom_columns} = \@custom_cols_list; |
1156
|
|
|
|
|
|
|
} |
1157
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
# If we're not overriding a column with the same name, then add custom column |
1159
|
14
|
100
|
|
|
|
30
|
for my $custom_col_spec (@{ $args->{custom_columns} || [] }) { |
|
14
|
|
|
|
|
56
|
|
1160
|
11
|
|
|
|
|
181
|
my $column_alias = $custom_col_spec->{name}; |
1161
|
11
|
100
|
|
|
|
21
|
if( ! grep { $column_alias eq $_ } @select_cols) { |
|
33
|
|
|
|
|
81
|
|
1162
|
|
|
|
|
|
|
my $raw_column = $custom_col_spec->{raw_column} |
1163
|
4
|
50
|
|
|
|
13
|
or die "you must specify a raw_column that " |
1164
|
|
|
|
|
|
|
. "$column_alias will be built using"; |
1165
|
4
|
50
|
|
|
|
26
|
if ($raw_column =~ /^[\w_]+$/) { |
1166
|
4
|
|
|
|
|
30
|
push @custom_cols, "$table_name." |
1167
|
|
|
|
|
|
|
. $dbh->quote_identifier($raw_column) |
1168
|
|
|
|
|
|
|
. " AS ". $dbh->quote_identifier($column_alias); |
1169
|
|
|
|
|
|
|
} else { |
1170
|
0
|
|
|
|
|
0
|
push @custom_cols, "$raw_column AS $column_alias"; |
1171
|
|
|
|
|
|
|
} |
1172
|
|
|
|
|
|
|
} |
1173
|
|
|
|
|
|
|
} |
1174
|
|
|
|
|
|
|
|
1175
|
|
|
|
|
|
|
my $col_list = join( |
1176
|
|
|
|
|
|
|
',', |
1177
|
|
|
|
|
|
|
map( |
1178
|
14
|
|
|
|
|
82
|
{ $table_name . "." . $dbh->quote_identifier($_) } |
|
42
|
|
|
|
|
829
|
|
1179
|
|
|
|
|
|
|
@select_cols |
1180
|
|
|
|
|
|
|
), |
1181
|
|
|
|
|
|
|
@foreign_cols, # already assembled from quoted identifiers |
1182
|
|
|
|
|
|
|
@custom_cols, |
1183
|
|
|
|
|
|
|
); |
1184
|
|
|
|
|
|
|
my $add_actions |
1185
|
|
|
|
|
|
|
= $args->{editable} |
1186
|
14
|
100
|
|
|
|
367
|
? ", $table_name.$key_column AS actions" |
1187
|
|
|
|
|
|
|
: ''; |
1188
|
14
|
|
|
|
|
38
|
my $query = "SELECT $col_list $add_actions FROM $table_name"; |
1189
|
14
|
|
|
|
|
23
|
my @binds; |
1190
|
|
|
|
|
|
|
|
1191
|
|
|
|
|
|
|
# If we have foreign key relationship info, we need to join on those tables: |
1192
|
14
|
100
|
|
|
|
38
|
if ($args->{foreign_keys}) { |
1193
|
1
|
|
|
|
|
3
|
while (my ($col, $foreign_key) = each %{ $args->{foreign_keys} }) { |
|
1
|
|
|
|
|
5
|
|
1194
|
0
|
|
|
|
|
0
|
my $ftable = $dbh->quote_identifier($foreign_key->{table}); |
1195
|
0
|
|
|
|
|
0
|
my $lkey = $dbh->quote_identifier($col); |
1196
|
0
|
|
|
|
|
0
|
my $rkey = $dbh->quote_identifier($foreign_key->{key_column}); |
1197
|
|
|
|
|
|
|
|
1198
|
|
|
|
|
|
|
# Identifiers quoted above, and $table_name quoted further up, so |
1199
|
|
|
|
|
|
|
# all safe to interpolate |
1200
|
0
|
|
|
|
|
0
|
my $what_to_join = $ftable; |
1201
|
0
|
|
|
|
|
0
|
my $join_reference = $ftable; |
1202
|
0
|
0
|
|
|
|
0
|
if (my $alias = $fk_alias{$col}) { |
1203
|
0
|
|
|
|
|
0
|
$what_to_join = " $ftable AS $alias "; |
1204
|
0
|
|
|
|
|
0
|
$join_reference = $alias; |
1205
|
|
|
|
|
|
|
} |
1206
|
|
|
|
|
|
|
# If this join is not a left join, the list view only shows rows where the |
1207
|
|
|
|
|
|
|
# foreign key is defined and matching a row |
1208
|
0
|
|
|
|
|
0
|
$query .= " LEFT JOIN $what_to_join ON $table_name.$lkey = $join_reference.$rkey "; |
1209
|
|
|
|
|
|
|
} |
1210
|
|
|
|
|
|
|
} |
1211
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
# If we have a query or a where_filter, we need to assemble a WHERE clause... |
1213
|
14
|
|
|
|
|
47
|
my $where_filter = _get_where_filter_from_args($args); |
1214
|
14
|
100
|
66
|
|
|
62
|
if (length $q || $where_filter) { |
1215
|
|
|
|
|
|
|
|
1216
|
|
|
|
|
|
|
# Turn the $where_filter hashref into some SQL clauses and bind params, |
1217
|
|
|
|
|
|
|
# which we'll add to with the user's search params shortly |
1218
|
4
|
|
|
|
|
18
|
my ($where_filter_sql, @where_filter_binds) |
1219
|
|
|
|
|
|
|
= $dbh->generate_where_clauses($where_filter); |
1220
|
|
|
|
|
|
|
|
1221
|
4
|
|
|
|
|
47
|
my (@search_wheres, @search_binds); |
1222
|
4
|
50
|
|
|
|
15
|
if (length $q) { # this nested code is all for queries in $q |
1223
|
|
|
|
|
|
|
my ($column_data) |
1224
|
12
|
|
|
|
|
34
|
= grep { lc $_->{COLUMN_NAME} eq lc $searchfield } |
1225
|
4
|
|
|
|
|
7
|
@{$columns}; |
|
4
|
|
|
|
|
9
|
|
1226
|
4
|
|
|
|
|
24
|
debug( |
1227
|
|
|
|
|
|
|
"Searching on $column_data->{COLUMN_NAME} which is a " |
1228
|
|
|
|
|
|
|
. "$column_data->{TYPE_NAME}" |
1229
|
|
|
|
|
|
|
); |
1230
|
4
|
|
66
|
|
|
231
|
my $st = params->{searchtype} || $default_searchtype; |
1231
|
|
|
|
|
|
|
|
1232
|
4
|
50
|
|
|
|
80
|
if ($column_data) { |
1233
|
4
|
|
|
|
|
9
|
my $search_value = $q; |
1234
|
4
|
50
|
33
|
|
|
28
|
if ($st eq 'c' || $st eq 'nc') { # contains or does not contain |
|
|
50
|
|
|
|
|
|
1235
|
0
|
|
|
|
|
0
|
$search_value = '%' . $search_value . '%'; |
1236
|
|
|
|
|
|
|
} elsif ($st eq 'b') { # begins with |
1237
|
0
|
|
|
|
|
0
|
$search_value = $search_value . '%'; |
1238
|
|
|
|
|
|
|
} |
1239
|
|
|
|
|
|
|
|
1240
|
4
|
|
|
|
|
10
|
my ($searchtype_row) = grep { $_->[0] eq $st } @searchtypes; |
|
40
|
|
|
|
|
75
|
|
1241
|
4
|
|
50
|
|
|
13
|
my $cmp = $searchtype_row->[1]->{cmp} || '='; |
1242
|
4
|
|
|
|
|
22
|
push(@search_wheres, |
1243
|
|
|
|
|
|
|
"$table_name." |
1244
|
|
|
|
|
|
|
. $dbh->quote_identifier($searchfield) |
1245
|
|
|
|
|
|
|
. " $cmp ?" ); |
1246
|
4
|
|
|
|
|
111
|
push(@search_binds, $search_value); |
1247
|
|
|
|
|
|
|
|
1248
|
4
|
|
50
|
|
|
15
|
my $matchtype = lc($searchtype_row->[1]->{name} || "equals"); |
1249
|
4
|
|
|
|
|
15
|
$html |
1250
|
|
|
|
|
|
|
.= sprintf( |
1251
|
|
|
|
|
|
|
" Showing results from searching for '%s' %s '%s'", |
1252
|
|
|
|
|
|
|
encode_entities($searchfield), $matchtype, encode_entities($q) |
1253
|
|
|
|
|
|
|
); |
1254
|
|
|
|
|
|
|
$html .= sprintf '—Reset search', |
1255
|
4
|
|
|
|
|
126
|
_external_url($args->{dancer_prefix}, $args->{prefix}); |
1256
|
|
|
|
|
|
|
} |
1257
|
|
|
|
|
|
|
} |
1258
|
|
|
|
|
|
|
# add the 'where' clauses to $query and the binds to @binds |
1259
|
4
|
|
|
|
|
1875
|
$query .= " where " . join( " AND ", grep { length $_ } ($where_filter_sql, @search_wheres)); |
|
8
|
|
|
|
|
28
|
|
1260
|
4
|
|
|
|
|
15
|
push(@binds, @where_filter_binds, @search_binds); |
1261
|
|
|
|
|
|
|
} |
1262
|
|
|
|
|
|
|
|
1263
|
14
|
50
|
|
|
|
37
|
if ($args->{downloadable}) { |
1264
|
0
|
|
|
|
|
0
|
my $qt = uri_escape($q); |
1265
|
0
|
|
|
|
|
0
|
my $sf = uri_escape($searchfield); |
1266
|
0
|
|
0
|
|
|
0
|
my $st = uri_escape(params->{searchtype} || $default_searchtype); |
1267
|
0
|
|
0
|
|
|
0
|
my $o = uri_escape(params->{'o'} || ""); |
1268
|
0
|
|
0
|
|
|
0
|
my $d = uri_escape(params->{'d'} || ""); |
1269
|
0
|
|
0
|
|
|
0
|
my $page = uri_escape(params->{'p'} || 0); |
1270
|
|
|
|
|
|
|
|
1271
|
0
|
|
|
|
|
0
|
my @formats = qw/csv tabular json xml/; |
1272
|
|
|
|
|
|
|
|
1273
|
|
|
|
|
|
|
my $url = _external_url($args->{dancer_prefix}, $args->{prefix}) |
1274
|
0
|
|
|
|
|
0
|
. "?o=$o&d=$d&q=$qt&searchfield=$sf&searchtype=$st&p=$page"; |
1275
|
|
|
|
|
|
|
|
1276
|
|
|
|
|
|
|
$html |
1277
|
|
|
|
|
|
|
.= " Download as: " |
1278
|
0
|
|
|
|
|
0
|
. join(", ", map { "$_" } @formats) |
|
0
|
|
|
|
|
0
|
|
1279
|
|
|
|
|
|
|
. " "; |
1280
|
|
|
|
|
|
|
} |
1281
|
|
|
|
|
|
|
|
1282
|
14
|
|
|
|
|
36
|
my %columns_sort_options; |
1283
|
14
|
100
|
|
|
|
36
|
if ($args->{sortable}) { |
1284
|
4
|
|
|
|
|
17
|
my $qt = uri_escape($q); |
1285
|
4
|
|
|
|
|
54
|
my $sf = uri_escape($searchfield); |
1286
|
4
|
|
33
|
|
|
49
|
my $st = uri_escape(params->{searchtype} || $default_searchtype); |
1287
|
4
|
|
33
|
|
|
139
|
my $order_by_column = uri_escape(params->{'o'}) || $key_column; |
1288
|
|
|
|
|
|
|
|
1289
|
|
|
|
|
|
|
# Invalid column name ? discard it |
1290
|
4
|
|
|
|
|
88
|
my $valid = grep { $_->{COLUMN_NAME} eq $order_by_column } @$columns; |
|
12
|
|
|
|
|
31
|
|
1291
|
4
|
50
|
|
|
|
10
|
$order_by_column = $key_column unless $valid; |
1292
|
4
|
|
|
|
|
8
|
my $order_by_table = $table_name; |
1293
|
|
|
|
|
|
|
|
1294
|
|
|
|
|
|
|
my $order_by_direction |
1295
|
4
|
50
|
33
|
|
|
11
|
= (exists params->{'d'} && params->{'d'} eq "desc") |
1296
|
|
|
|
|
|
|
? "desc" |
1297
|
|
|
|
|
|
|
: "asc"; |
1298
|
4
|
50
|
|
|
|
68
|
my $opposite_order_by_direction |
1299
|
|
|
|
|
|
|
= ($order_by_direction eq "asc") ? "desc" : "asc"; |
1300
|
|
|
|
|
|
|
|
1301
|
|
|
|
|
|
|
# Get a list of all columns (normal, and custom_columns), then assemble |
1302
|
|
|
|
|
|
|
# the names and labels to pass to HTML::Table::FromDatabase |
1303
|
4
|
|
|
|
|
8
|
my @all_cols = map { $_->{COLUMN_NAME} } @$columns; |
|
12
|
|
|
|
|
27
|
|
1304
|
|
|
|
|
|
|
%columns_sort_options = map { |
1305
|
4
|
|
|
|
|
8
|
my $col_name = $_; |
|
12
|
|
|
|
|
26
|
|
1306
|
12
|
|
|
|
|
22
|
my $direction = $order_by_direction; |
1307
|
12
|
|
|
|
|
18
|
my $direction_char = ""; |
1308
|
12
|
|
|
|
|
21
|
my $friendly_name = $col_name; |
1309
|
12
|
50
|
|
|
|
32
|
if ($args->{labels}{$col_name}) { |
1310
|
0
|
|
|
|
|
0
|
$friendly_name = $args->{labels}{$col_name}; |
1311
|
|
|
|
|
|
|
} else { |
1312
|
12
|
|
|
|
|
25
|
$friendly_name = _prettify_column_name($friendly_name); |
1313
|
|
|
|
|
|
|
} |
1314
|
12
|
100
|
|
|
|
32
|
if ($col_name eq $order_by_column) { |
1315
|
4
|
|
|
|
|
8
|
$direction = $opposite_order_by_direction; |
1316
|
4
|
50
|
|
|
|
10
|
$direction_char = ($direction eq "asc") ? "↑" : "↓"; |
1317
|
|
|
|
|
|
|
} |
1318
|
|
|
|
|
|
|
my $url = _external_url($args->{dancer_prefix}, $args->{prefix}) |
1319
|
12
|
|
|
|
|
50
|
. "?o=$col_name&d=$direction&q=$q&searchfield=$sf&searchtype=$st"; |
1320
|
12
|
|
|
|
|
5721
|
$col_name => |
1321
|
|
|
|
|
|
|
"$friendly_name $direction_char"; |
1322
|
|
|
|
|
|
|
} @all_cols; |
1323
|
|
|
|
|
|
|
|
1324
|
|
|
|
|
|
|
# And for custom columns, do the prettification, but don't include a |
1325
|
|
|
|
|
|
|
# link for sorting unless we're overriding the display of an already |
1326
|
|
|
|
|
|
|
# sortable column. We can't sort non-overridden custom columns (sorting is |
1327
|
|
|
|
|
|
|
# done by SQL, and the custom column values are calculated after we get |
1328
|
|
|
|
|
|
|
# the results from the SQL query, so to support sorting by them we'd |
1329
|
|
|
|
|
|
|
# have to stop getting the database to sort the data and sort it |
1330
|
|
|
|
|
|
|
# ourselves afterwards). |
1331
|
4
|
50
|
|
|
|
18
|
if (exists $args->{custom_columns}) { |
1332
|
4
|
|
|
|
|
10
|
for my $custom_column_name ( |
1333
|
7
|
|
|
|
|
19
|
map { $_->{name} } @{ $args->{custom_columns} } |
|
4
|
|
|
|
|
11
|
|
1334
|
|
|
|
|
|
|
) { |
1335
|
7
|
100
|
|
|
|
13
|
if ( !grep { $_ eq $custom_column_name } @all_cols) { |
|
21
|
|
|
|
|
62
|
|
1336
|
2
|
|
|
|
|
6
|
$columns_sort_options{$custom_column_name} |
1337
|
|
|
|
|
|
|
= _prettify_column_name($custom_column_name); |
1338
|
|
|
|
|
|
|
} |
1339
|
|
|
|
|
|
|
} |
1340
|
|
|
|
|
|
|
} |
1341
|
|
|
|
|
|
|
|
1342
|
4
|
0
|
33
|
|
|
16
|
if (exists $args->{foreign_keys} and exists $args->{foreign_keys}{$order_by_column}) { |
1343
|
0
|
|
|
|
|
0
|
my $fk = $args->{foreign_keys}{$order_by_column}; |
1344
|
0
|
|
|
|
|
0
|
$order_by_column = $fk->{label_column}; |
1345
|
0
|
|
|
|
|
0
|
$order_by_table = $fk->{table}; |
1346
|
|
|
|
|
|
|
} |
1347
|
|
|
|
|
|
|
|
1348
|
4
|
|
|
|
|
23
|
$query .= " ORDER BY " |
1349
|
|
|
|
|
|
|
. $dbh->quote_identifier($order_by_table) . "." |
1350
|
|
|
|
|
|
|
. $dbh->quote_identifier($order_by_column) |
1351
|
|
|
|
|
|
|
. " $order_by_direction "; |
1352
|
|
|
|
|
|
|
} |
1353
|
|
|
|
|
|
|
|
1354
|
14
|
50
|
33
|
|
|
233
|
if ($args->{paginate} && $args->{paginate} =~ /^\d+$/) { |
1355
|
0
|
|
|
|
|
0
|
my $page_size = $args->{paginate}; |
1356
|
|
|
|
|
|
|
|
1357
|
0
|
|
|
|
|
0
|
my $qt = uri_escape($q); |
1358
|
0
|
|
|
|
|
0
|
my $sf = uri_escape($searchfield); |
1359
|
0
|
|
0
|
|
|
0
|
my $st = uri_escape(params->{searchtype} || $default_searchtype); |
1360
|
0
|
|
0
|
|
|
0
|
my $o = uri_escape(params->{'o'} || ""); |
1361
|
0
|
|
0
|
|
|
0
|
my $d = uri_escape(params->{'d'} || ""); |
1362
|
0
|
|
0
|
|
|
0
|
my $page = uri_escape(params->{'p'} || 0); |
1363
|
0
|
0
|
|
|
|
0
|
$page = 0 unless $page =~ /^\d+$/; |
1364
|
|
|
|
|
|
|
|
1365
|
0
|
|
|
|
|
0
|
my $offset = $page_size * $page; |
1366
|
0
|
|
|
|
|
0
|
my $limit = $page_size; |
1367
|
|
|
|
|
|
|
|
1368
|
|
|
|
|
|
|
my $url = _external_url($args->{dancer_prefix}, $args->{prefix}) |
1369
|
0
|
|
|
|
|
0
|
. "?o=$o&d=$d&q=$qt&searchfield=$sf&searchtype=$st"; |
1370
|
0
|
|
|
|
|
0
|
$html .= " "; |
1371
|
0
|
|
|
|
|
0
|
$html .= ""; |
1391
|
|
|
|
|
|
|
|
1392
|
0
|
|
|
|
|
0
|
$query .= " LIMIT $limit OFFSET $offset "; |
1393
|
|
|
|
|
|
|
} |
1394
|
|
|
|
|
|
|
|
1395
|
14
|
|
|
|
|
62
|
debug("Running query: $query"); |
1396
|
14
|
|
|
|
|
899
|
my $sth = $dbh->prepare($query); |
1397
|
14
|
50
|
|
|
|
1238
|
$sth->execute(@binds) |
1398
|
|
|
|
|
|
|
or die "Failed to query for records in $table_name - " |
1399
|
|
|
|
|
|
|
. $dbh->errstr; |
1400
|
|
|
|
|
|
|
|
1401
|
14
|
0
|
33
|
|
|
65
|
if ($args->{downloadable} && params->{format}) { |
1402
|
|
|
|
|
|
|
|
1403
|
|
|
|
|
|
|
##Return results as a downloaded file, instead of generating the HTML table. |
1404
|
0
|
|
|
|
|
0
|
return _return_downloadable_query($args, $sth, params->{format}); |
1405
|
|
|
|
|
|
|
} |
1406
|
|
|
|
|
|
|
|
1407
|
14
|
|
|
|
|
33
|
my @custom_callbacks = (); |
1408
|
14
|
100
|
|
|
|
24
|
for my $custom_col_spec (@{ $args->{custom_columns} || [] } ) { |
|
14
|
|
|
|
|
71
|
|
1409
|
|
|
|
|
|
|
push @custom_callbacks, { |
1410
|
|
|
|
|
|
|
column=>$custom_col_spec->{name}, |
1411
|
11
|
|
50
|
0
|
|
57
|
transform=> ($custom_col_spec->{transform} or sub { return shift;}), |
|
0
|
|
|
|
|
0
|
|
1412
|
|
|
|
|
|
|
}; |
1413
|
|
|
|
|
|
|
} |
1414
|
|
|
|
|
|
|
|
1415
|
|
|
|
|
|
|
|
1416
|
|
|
|
|
|
|
my $table = HTML::Table::FromDatabase->new( |
1417
|
|
|
|
|
|
|
-sth => $sth, |
1418
|
|
|
|
|
|
|
-border => 1, |
1419
|
|
|
|
|
|
|
-callbacks => [ |
1420
|
|
|
|
|
|
|
{ |
1421
|
|
|
|
|
|
|
column => 'actions', |
1422
|
|
|
|
|
|
|
transform => sub { |
1423
|
16
|
|
|
16
|
|
3774
|
my $id = shift; |
1424
|
16
|
|
|
|
|
42
|
my $action_links; |
1425
|
16
|
50
|
33
|
|
|
60
|
if ($args->{editable} && _has_permission('edit', $args)) { |
1426
|
|
|
|
|
|
|
my $edit_url |
1427
|
|
|
|
|
|
|
= _external_url( |
1428
|
|
|
|
|
|
|
$args->{dancer_prefix}, $args->{prefix}, |
1429
|
16
|
|
|
|
|
52
|
"/edit/$id" |
1430
|
|
|
|
|
|
|
); |
1431
|
16
|
|
|
|
|
7239
|
$action_links |
1432
|
|
|
|
|
|
|
.= qq[Edit]; |
1433
|
16
|
50
|
33
|
|
|
150
|
if ($args->{deletable} && _has_permission('edit', $args)) { |
1434
|
|
|
|
|
|
|
my $del_url =_external_url( |
1435
|
|
|
|
|
|
|
$args->{dancer_prefix}, $args->{prefix}, |
1436
|
0
|
|
|
|
|
0
|
"/delete/$id" |
1437
|
|
|
|
|
|
|
); |
1438
|
0
|
|
|
|
|
0
|
$action_links |
1439
|
|
|
|
|
|
|
.= qq[ /
|
1440
|
|
|
|
|
|
|
. qq[ onclick="delrec('$id'); return false;">] |
1441
|
|
|
|
|
|
|
. qq[Delete]; |
1442
|
|
|
|
|
|
|
} |
1443
|
|
|
|
|
|
|
} |
1444
|
16
|
|
|
|
|
62
|
return $action_links; |
1445
|
|
|
|
|
|
|
}, |
1446
|
|
|
|
|
|
|
}, |
1447
|
14
|
|
|
|
|
207
|
@custom_callbacks, |
1448
|
|
|
|
|
|
|
], |
1449
|
|
|
|
|
|
|
-rename_headers => \%columns_sort_options, |
1450
|
|
|
|
|
|
|
-html => 'escape', |
1451
|
|
|
|
|
|
|
-class => $table_class, |
1452
|
|
|
|
|
|
|
); |
1453
|
|
|
|
|
|
|
|
1454
|
|
|
|
|
|
|
# apply custom columns' column_classes as specified. Can this be done via HTML::Table::FromDatabase->new() above? |
1455
|
14
|
|
|
|
|
19189
|
my @all_column_names = ( (map { $_->{COLUMN_NAME} } @$columns), (map { $_->{name} } @{$args->{custom_columns}}) ); |
|
42
|
|
|
|
|
106
|
|
|
11
|
|
|
|
|
31
|
|
|
14
|
|
|
|
|
40
|
|
1456
|
14
|
50
|
|
|
|
30
|
for my $custom_col_spec (@{ $args->{custom_columns} || [] } ) { |
|
14
|
|
|
|
|
61
|
|
1457
|
11
|
50
|
|
|
|
1008
|
if (my $column_class = $custom_col_spec->{column_class}) { |
1458
|
11
|
|
|
27
|
|
96
|
my $first_index = first_index { $_ eq $custom_col_spec->{name} } uniq @all_column_names; |
|
27
|
|
|
|
|
52
|
|
1459
|
11
|
50
|
|
|
|
47
|
die "Cannot find index of column '$custom_col_spec->{name}'" if ($first_index == -1); |
1460
|
11
|
|
|
|
|
44
|
$table->setColClass( 1 + $first_index, $column_class ); |
1461
|
|
|
|
|
|
|
} |
1462
|
|
|
|
|
|
|
} |
1463
|
|
|
|
|
|
|
|
1464
|
14
|
|
|
|
|
1100
|
my $add_link_html = ""; |
1465
|
14
|
100
|
66
|
|
|
71
|
if ($args->{addable} && _has_permission('edit', $args)) { |
1466
|
|
|
|
|
|
|
$add_link_html = sprintf ' Add a new %s ', |
1467
|
|
|
|
|
|
|
_external_url($args->{dancer_prefix}, $args->{prefix}, '/add'), |
1468
|
1
|
|
|
|
|
7
|
$args->{record_title}; |
1469
|
|
|
|
|
|
|
} |
1470
|
|
|
|
|
|
|
|
1471
|
14
|
|
|
|
|
475
|
$html .= $add_link_html; |
1472
|
|
|
|
|
|
|
|
1473
|
14
|
|
100
|
|
|
51
|
$html .= $table->getTable || ''; |
1474
|
|
|
|
|
|
|
|
1475
|
14
|
|
|
|
|
12779
|
$html .= $add_link_html; |
1476
|
|
|
|
|
|
|
|
1477
|
14
|
50
|
33
|
|
|
45
|
if ($args->{deleteable} && _has_permission('delete', $args)) { |
1478
|
|
|
|
|
|
|
|
1479
|
|
|
|
|
|
|
# Append a little Javascript which asks for confirmation that they'd |
1480
|
|
|
|
|
|
|
# like to delete the record, then makes a POST request via a hidden |
1481
|
|
|
|
|
|
|
# form. This could be made AJAXy in future. |
1482
|
|
|
|
|
|
|
my $del_action = _external_url( |
1483
|
0
|
|
|
|
|
0
|
$args->{dancer_prefix}, $args->{prefix}, '/delete' |
1484
|
|
|
|
|
|
|
); |
1485
|
0
|
|
|
|
|
0
|
$html .= <
|
1486
|
|
|
|
|
|
|
|
1487
|
|
|
|
|
|
|
|
1488
|
|
|
|
|
|
|
|
1489
|
|
|
|
|
|
|
|
1497
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
DELETEJS |
1499
|
|
|
|
|
|
|
} |
1500
|
|
|
|
|
|
|
|
1501
|
14
|
|
|
|
|
64
|
return _apply_template($html, $args->{'template'}, $args->{'record_title'}); |
1502
|
|
|
|
|
|
|
} |
1503
|
|
|
|
|
|
|
|
1504
|
|
|
|
|
|
|
sub _apply_template { |
1505
|
19
|
|
|
19
|
|
37577
|
my ($html, $template, $title) = @_; |
1506
|
|
|
|
|
|
|
|
1507
|
19
|
50
|
|
|
|
59
|
if ($template) { |
1508
|
0
|
|
|
|
|
0
|
return template $template, { simple_crud => $html, record_title => $title }; |
1509
|
|
|
|
|
|
|
} else { |
1510
|
19
|
|
|
|
|
72
|
return engine('template')->apply_layout($html); |
1511
|
|
|
|
|
|
|
} |
1512
|
|
|
|
|
|
|
} |
1513
|
|
|
|
|
|
|
|
1514
|
|
|
|
|
|
|
sub _return_downloadable_query { |
1515
|
0
|
|
|
0
|
|
0
|
my ($args, $sth, $format) = @_; |
1516
|
|
|
|
|
|
|
|
1517
|
0
|
|
|
|
|
0
|
my $output; |
1518
|
|
|
|
|
|
|
|
1519
|
|
|
|
|
|
|
## Generate an informative filename |
1520
|
0
|
|
|
|
|
0
|
my $filename = $args->{db_table}; |
1521
|
0
|
0
|
|
|
|
0
|
if (params->{'o'}) { |
1522
|
0
|
|
|
|
|
0
|
my $order = params->{'o'}; |
1523
|
0
|
|
|
|
|
0
|
$order =~ s/[^\w\.\-]+/_/g; |
1524
|
0
|
|
|
|
|
0
|
$filename .= "__sorted_by_" . $order; |
1525
|
|
|
|
|
|
|
} |
1526
|
0
|
|
|
|
|
0
|
my $q = _defined_or_empty(params->{'q'}); |
1527
|
0
|
0
|
|
|
|
0
|
if (length($q)) { |
1528
|
0
|
|
|
|
|
0
|
my $query = $q; |
1529
|
0
|
|
|
|
|
0
|
$query =~ s/[^\w\.\-]+/_/g; |
1530
|
0
|
|
|
|
|
0
|
$filename .= "__query_" . $query; |
1531
|
|
|
|
|
|
|
} |
1532
|
0
|
0
|
|
|
|
0
|
if (params->{'p'}) { |
1533
|
0
|
|
|
|
|
0
|
my $page = params->{'p'}; |
1534
|
0
|
|
|
|
|
0
|
$page =~ s/[^0-9]+/_/g; |
1535
|
0
|
|
|
|
|
0
|
$filename .= "__page_" . $page; |
1536
|
|
|
|
|
|
|
} |
1537
|
|
|
|
|
|
|
|
1538
|
|
|
|
|
|
|
## Generate data in the requested format |
1539
|
0
|
0
|
|
|
|
0
|
if ($format eq "tabular") { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1540
|
0
|
|
|
|
|
0
|
header('Content-Type' => 'text/tab-separated-values'); |
1541
|
0
|
|
|
|
|
0
|
header('Content-Disposition' => |
1542
|
|
|
|
|
|
|
"attachment; filename=\"$filename.txt\""); |
1543
|
0
|
|
|
|
|
0
|
my $aref = $sth->{NAME}; |
1544
|
0
|
|
|
|
|
0
|
$output = join("\t", @$aref) . "\r\n"; |
1545
|
0
|
|
|
|
|
0
|
while ($aref = $sth->fetchrow_arrayref) { |
1546
|
0
|
|
|
|
|
0
|
$output .= join("\t", @{$aref}) . "\r\n"; |
|
0
|
|
|
|
|
0
|
|
1547
|
|
|
|
|
|
|
} |
1548
|
|
|
|
|
|
|
} elsif ($format eq "csv") { |
1549
|
0
|
|
|
|
|
0
|
eval { require Text::CSV }; |
|
0
|
|
|
|
|
0
|
|
1550
|
|
|
|
|
|
|
return |
1551
|
0
|
0
|
|
|
|
0
|
"Error: required module Text::CSV not installed. Can't generate CSV file." |
1552
|
|
|
|
|
|
|
if $@; |
1553
|
|
|
|
|
|
|
|
1554
|
0
|
|
|
|
|
0
|
header('Content-Type' => 'text/comma-separated-values'); |
1555
|
0
|
|
|
|
|
0
|
header('Content-Disposition' => |
1556
|
|
|
|
|
|
|
"attachment; filename=\"$filename.csv\""); |
1557
|
|
|
|
|
|
|
|
1558
|
0
|
|
|
|
|
0
|
my $csv = Text::CSV->new(); |
1559
|
0
|
|
|
|
|
0
|
my $aref = $sth->{NAME}; |
1560
|
0
|
|
|
|
|
0
|
$csv->combine(@{$aref}); |
|
0
|
|
|
|
|
0
|
|
1561
|
0
|
|
|
|
|
0
|
$output = $csv->string() . "\r\n"; |
1562
|
0
|
|
|
|
|
0
|
while ($aref = $sth->fetchrow_arrayref) { |
1563
|
0
|
|
|
|
|
0
|
$csv->combine(@{$aref}); |
|
0
|
|
|
|
|
0
|
|
1564
|
0
|
|
|
|
|
0
|
$output .= $csv->string() . "\r\n"; |
1565
|
|
|
|
|
|
|
} |
1566
|
|
|
|
|
|
|
} elsif ($format eq "json") { |
1567
|
0
|
|
|
|
|
0
|
header('Content-Type' => 'text/json'); |
1568
|
0
|
|
|
|
|
0
|
header('Content-Disposition' => |
1569
|
|
|
|
|
|
|
"attachment; filename=\"$filename.json\""); |
1570
|
0
|
|
|
|
|
0
|
$output = to_json($sth->fetchall_arrayref({})); |
1571
|
|
|
|
|
|
|
} elsif ($format eq "xml") { |
1572
|
0
|
|
|
|
|
0
|
header('Content-Type' => 'text/xml'); |
1573
|
0
|
|
|
|
|
0
|
header('Content-Disposition' => |
1574
|
|
|
|
|
|
|
"attachment; filename=\"$filename.xml\""); |
1575
|
0
|
|
|
|
|
0
|
$output = to_xml($sth->fetchall_arrayref({})); |
1576
|
|
|
|
|
|
|
} else { |
1577
|
0
|
|
|
|
|
0
|
$output = "Error: unknown format $format"; |
1578
|
|
|
|
|
|
|
} |
1579
|
|
|
|
|
|
|
|
1580
|
0
|
|
|
|
|
0
|
return $output; |
1581
|
|
|
|
|
|
|
} |
1582
|
|
|
|
|
|
|
|
1583
|
|
|
|
|
|
|
# Given a table name, return an arrayref of hashrefs describing each column in |
1584
|
|
|
|
|
|
|
# the table. |
1585
|
|
|
|
|
|
|
# Expect to see the following keys: |
1586
|
|
|
|
|
|
|
# COLUMN_NAME |
1587
|
|
|
|
|
|
|
# COLUMN_SIZE |
1588
|
|
|
|
|
|
|
# NULLABLE |
1589
|
|
|
|
|
|
|
# DATETIME ? |
1590
|
|
|
|
|
|
|
# TYPE_NAME (e.g. INT, VARCHAR, ENUM) |
1591
|
|
|
|
|
|
|
# MySQL/MariaDB-specific stuff includes: |
1592
|
|
|
|
|
|
|
# {mysql,mariadb}_type_name (e.g. "enum('One', 'Two', 'Three')" |
1593
|
|
|
|
|
|
|
# {mysql,mariadb}_is_pri_key |
1594
|
|
|
|
|
|
|
# {mysql,mariadb}_values (for an enum, ["One", "Two", "Three"] |
1595
|
|
|
|
|
|
|
sub _find_columns { |
1596
|
19
|
|
|
19
|
|
45
|
my ($dbh, $table_name) = @_; |
1597
|
19
|
50
|
|
|
|
163
|
my $sth = $dbh->column_info(undef, undef, $table_name, undef) |
1598
|
|
|
|
|
|
|
or die "Failed to get column info for $table_name - " . $dbh->errstr; |
1599
|
19
|
|
|
|
|
18590
|
my @columns; |
1600
|
19
|
|
|
|
|
189
|
while (my $col = $sth->fetchrow_hashref) { |
1601
|
|
|
|
|
|
|
|
1602
|
|
|
|
|
|
|
# Push a copy of the hashref, as I think DBI re-uses them |
1603
|
57
|
|
|
|
|
1930
|
push @columns, {%$col}; |
1604
|
|
|
|
|
|
|
} |
1605
|
|
|
|
|
|
|
|
1606
|
19
|
50
|
|
|
|
368
|
die "no columns for table [$table_name]--are you sure this table exists in the database [$dbh->{Driver}->{Name}:$dbh->{Name}]?" unless @columns; |
1607
|
|
|
|
|
|
|
|
1608
|
|
|
|
|
|
|
# Return the columns, sorted by their position in the table: |
1609
|
19
|
|
|
|
|
92
|
return [sort { $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION} } |
|
57
|
|
|
|
|
741
|
|
1610
|
|
|
|
|
|
|
@columns]; |
1611
|
|
|
|
|
|
|
} |
1612
|
|
|
|
|
|
|
|
1613
|
|
|
|
|
|
|
# Given parts of an URL, assemble them together, prepending the current prefix |
1614
|
|
|
|
|
|
|
# setting if needed, and taking care to get slashes right. |
1615
|
|
|
|
|
|
|
# e.g. for the following example: |
1616
|
|
|
|
|
|
|
# prefix '/foo'; |
1617
|
|
|
|
|
|
|
# simple_crud( prefix => '/bar', ....); |
1618
|
|
|
|
|
|
|
# calling: _construct_url($args{prefix}, '/baz') |
1619
|
|
|
|
|
|
|
# would return: /foo/bar/baz |
1620
|
|
|
|
|
|
|
sub _construct_url { |
1621
|
57
|
|
|
57
|
|
147
|
my @url_parts = @_; |
1622
|
|
|
|
|
|
|
|
1623
|
|
|
|
|
|
|
# Just concatenate all parts together, then deal with multiple slashes. |
1624
|
|
|
|
|
|
|
# This could be problematic if any URL was ever supposed to contain multiple |
1625
|
|
|
|
|
|
|
# slashes, but that shouldn't be an issue here. |
1626
|
57
|
|
|
|
|
191
|
my $url = '/' . join '/', @url_parts; |
1627
|
57
|
|
|
|
|
354
|
$url =~ s{/{2,}}{/}g; |
1628
|
57
|
|
|
|
|
214
|
return $url; |
1629
|
|
|
|
|
|
|
} |
1630
|
|
|
|
|
|
|
|
1631
|
|
|
|
|
|
|
sub _external_url { |
1632
|
36
|
50
|
|
36
|
|
179
|
if ( plugin_setting()->{use_old_url_scheme} ) { |
1633
|
0
|
|
|
|
|
0
|
return _construct_url(@_); |
1634
|
|
|
|
|
|
|
} |
1635
|
|
|
|
|
|
|
else { |
1636
|
36
|
|
|
|
|
690
|
return uri_for(_construct_url(@_)); |
1637
|
|
|
|
|
|
|
} |
1638
|
|
|
|
|
|
|
} |
1639
|
|
|
|
|
|
|
|
1640
|
|
|
|
|
|
|
# Given a mode ("view" or "edit", a handler coderef, and an args coderef, works |
1641
|
|
|
|
|
|
|
# out if we need to wrap the handler coderef via |
1642
|
|
|
|
|
|
|
# Dancer::Plugin::Auth::Extensible to ensure authorisation, and if so, does so. |
1643
|
|
|
|
|
|
|
sub _ensure_auth { |
1644
|
21
|
|
|
21
|
|
47
|
my ($mode, $handler, $args) = @_; |
1645
|
|
|
|
|
|
|
|
1646
|
21
|
|
50
|
|
|
92
|
my $auth_settings = $args->{auth}{$mode} || $args->{auth} || {}; |
1647
|
|
|
|
|
|
|
|
1648
|
21
|
100
|
|
|
|
51
|
if (keys %$auth_settings) { |
1649
|
4
|
50
|
|
|
|
19
|
Dancer::ModuleLoader->load('Dancer::Plugin::Auth::Extensible') |
1650
|
|
|
|
|
|
|
or die "Can't use auth settings without" |
1651
|
|
|
|
|
|
|
. " Dancer::Plugin::Auth::Extensible!"; |
1652
|
|
|
|
|
|
|
} else { |
1653
|
|
|
|
|
|
|
# I think this can just be 'return;' given the way it is |
1654
|
|
|
|
|
|
|
# used currently, but the other branch returns a $handler, |
1655
|
|
|
|
|
|
|
# so this is more consistent |
1656
|
17
|
|
|
|
|
41
|
return $handler; |
1657
|
|
|
|
|
|
|
} |
1658
|
|
|
|
|
|
|
|
1659
|
4
|
50
|
|
|
|
8952
|
if ($auth_settings->{require_login}) { |
1660
|
4
|
|
|
|
|
13
|
return $handler = |
1661
|
|
|
|
|
|
|
Dancer::Plugin::Auth::Extensible::require_login($handler); |
1662
|
|
|
|
|
|
|
} else { |
1663
|
0
|
|
|
|
|
0
|
for my $keyword (qw(require_role require_any_role require_all_roles)) { |
1664
|
0
|
0
|
|
|
|
0
|
if (my $val = $auth_settings->{$keyword}) { |
1665
|
0
|
|
|
|
|
0
|
return $handler = Dancer::Plugin::Auth::Extensible->can($keyword)->( |
1666
|
|
|
|
|
|
|
$val, $handler |
1667
|
|
|
|
|
|
|
); |
1668
|
|
|
|
|
|
|
} |
1669
|
|
|
|
|
|
|
} |
1670
|
|
|
|
|
|
|
} |
1671
|
|
|
|
|
|
|
} |
1672
|
|
|
|
|
|
|
|
1673
|
|
|
|
|
|
|
# Given an action (view/edit) and an args coderef, returns whether the user has |
1674
|
|
|
|
|
|
|
# permission to perform that action (e.g. if require_login is set, checks the |
1675
|
|
|
|
|
|
|
# user is logged in; if require_role is set, checks they have that role, etc) |
1676
|
|
|
|
|
|
|
sub _has_permission { |
1677
|
17
|
|
|
17
|
|
36
|
my ($mode, $args) = @_; |
1678
|
|
|
|
|
|
|
|
1679
|
17
|
|
50
|
|
|
104
|
my $auth_settings = $args->{auth}{$mode} || $args->{auth} || {}; |
1680
|
17
|
50
|
|
|
|
44
|
if (keys %$auth_settings) { |
1681
|
0
|
0
|
|
|
|
0
|
Dancer::ModuleLoader->load('Dancer::Plugin::Auth::Extensible') |
1682
|
|
|
|
|
|
|
or die "Can't use auth settings without" |
1683
|
|
|
|
|
|
|
. " Dancer::Plugin::Auth::Extensible!"; |
1684
|
|
|
|
|
|
|
} else { |
1685
|
|
|
|
|
|
|
# If no auth settings provided, they can do what they like |
1686
|
17
|
|
|
|
|
56
|
return 1; |
1687
|
|
|
|
|
|
|
} |
1688
|
|
|
|
|
|
|
|
1689
|
0
|
0
|
|
|
|
0
|
if ($auth_settings->{require_login}) { |
1690
|
0
|
0
|
|
|
|
0
|
return Dancer::Plugin::Auth::Extensible::logged_in_user() ? 1 : 0; |
1691
|
|
|
|
|
|
|
} |
1692
|
|
|
|
|
|
|
|
1693
|
0
|
0
|
|
|
|
0
|
if (my $need_role = $auth_settings->{require_role}) { |
1694
|
0
|
|
|
|
|
0
|
return Dancer::Plugin::Auth::Extensible::user_has_role($need_role); |
1695
|
|
|
|
|
|
|
} |
1696
|
|
|
|
|
|
|
|
1697
|
|
|
|
|
|
|
# TODO: handle require_any_role / require_all_roles here |
1698
|
0
|
|
|
|
|
0
|
warn "TODO: handle require_any_role / requires_all_roles"; |
1699
|
0
|
|
|
|
|
0
|
return 0; |
1700
|
|
|
|
|
|
|
} |
1701
|
|
|
|
|
|
|
|
1702
|
|
|
|
|
|
|
sub _defined_or_empty { |
1703
|
14
|
|
|
14
|
|
235
|
my $v = shift; |
1704
|
14
|
100
|
|
|
|
45
|
return defined($v) ? $v : ""; |
1705
|
|
|
|
|
|
|
} |
1706
|
|
|
|
|
|
|
|
1707
|
|
|
|
|
|
|
# where_filter "if it's a coderef, call it and check it gave us a hashref to |
1708
|
|
|
|
|
|
|
# use, otherwise expect it to be a hashref" logic as we need this in several |
1709
|
|
|
|
|
|
|
# places. Returns a hashref, ready for us to add other stuff to in most cases |
1710
|
|
|
|
|
|
|
# (e.g. usually we'd call this, then add $key_column => ... to it) |
1711
|
|
|
|
|
|
|
sub _get_where_filter_from_args { |
1712
|
21
|
|
|
21
|
|
38
|
my $args = shift; |
1713
|
21
|
50
|
|
|
|
65
|
return unless $args->{where_filter}; |
1714
|
|
|
|
|
|
|
|
1715
|
0
|
0
|
|
|
|
0
|
if (ref $args->{where_filter} eq 'HASH') { |
|
|
0
|
|
|
|
|
|
1716
|
0
|
|
|
|
|
0
|
return $args->{where_filter}; |
1717
|
|
|
|
|
|
|
} elsif (ref $args->{where_filter} eq 'CODE') { |
1718
|
0
|
|
|
|
|
0
|
my $result = $args->{where_filter}->(); |
1719
|
0
|
0
|
|
|
|
0
|
if (ref $result eq 'HASH') { |
1720
|
0
|
|
|
|
|
0
|
return $result; |
1721
|
|
|
|
|
|
|
} else { |
1722
|
|
|
|
|
|
|
# TODO: better error reporting, so we know which one caused |
1723
|
|
|
|
|
|
|
# the problem |
1724
|
0
|
|
|
|
|
0
|
die "where_filter coderef didn't return a hashref!"; |
1725
|
|
|
|
|
|
|
} |
1726
|
|
|
|
|
|
|
} else { |
1727
|
0
|
|
|
|
|
0
|
die "Invalid where_filter"; |
1728
|
|
|
|
|
|
|
} |
1729
|
|
|
|
|
|
|
} |
1730
|
|
|
|
|
|
|
|
1731
|
|
|
|
|
|
|
sub _prettify_column_name { |
1732
|
14
|
|
|
14
|
|
25
|
my $name = shift; |
1733
|
14
|
|
|
|
|
30
|
for ($name) { |
1734
|
14
|
|
|
|
|
24
|
$_ = lc; |
1735
|
14
|
|
|
|
|
28
|
s{_}{ }g; |
1736
|
14
|
|
|
|
|
108
|
s{\b(\w)}{\u$1}g; |
1737
|
|
|
|
|
|
|
} |
1738
|
14
|
|
|
|
|
47
|
return $name; |
1739
|
|
|
|
|
|
|
} |
1740
|
|
|
|
|
|
|
|
1741
|
|
|
|
|
|
|
=back |
1742
|
|
|
|
|
|
|
|
1743
|
|
|
|
|
|
|
=head1 DWIMmery |
1744
|
|
|
|
|
|
|
|
1745
|
|
|
|
|
|
|
This module tries to do what you'd expect it to do, so you can rock up your web |
1746
|
|
|
|
|
|
|
app with as little code and effort as possible, whilst still giving you control |
1747
|
|
|
|
|
|
|
to override its decisions wherever you need to. |
1748
|
|
|
|
|
|
|
|
1749
|
|
|
|
|
|
|
=head2 Field types |
1750
|
|
|
|
|
|
|
|
1751
|
|
|
|
|
|
|
CGI::FormBuilder is excellent at working out what kind of field to use by |
1752
|
|
|
|
|
|
|
itself, but we give it a little help where needed. For instance, if a field |
1753
|
|
|
|
|
|
|
looks like it's supposed to contain a password, we'll have it rendered as a |
1754
|
|
|
|
|
|
|
password entry box, rather than a standard text box. |
1755
|
|
|
|
|
|
|
|
1756
|
|
|
|
|
|
|
If the column in the database is an ENUM, we'll limit the choices available for |
1757
|
|
|
|
|
|
|
this field to the choices defined by the ENUM list. (Unless you've provided a |
1758
|
|
|
|
|
|
|
set of acceptable values for this field using the C option to |
1759
|
|
|
|
|
|
|
C, in which case what you say goes.) |
1760
|
|
|
|
|
|
|
|
1761
|
|
|
|
|
|
|
=head1 Hooks |
1762
|
|
|
|
|
|
|
|
1763
|
|
|
|
|
|
|
Hooks are provided, which can be used in the normal Dancer way, using the |
1764
|
|
|
|
|
|
|
C keyword. |
1765
|
|
|
|
|
|
|
|
1766
|
|
|
|
|
|
|
=head2 add_edit_row (deprecated, use add_edit_row_pre_save) |
1767
|
|
|
|
|
|
|
|
1768
|
|
|
|
|
|
|
You can use the same code from your add_edit_row hook in an add_edit_row_pre_save |
1769
|
|
|
|
|
|
|
hook. The only modification is that the new hook passes the editable params |
1770
|
|
|
|
|
|
|
as a key of the first argument (called C), rather than as the first |
1771
|
|
|
|
|
|
|
argument itself. So, if your hook had C, it could just |
1772
|
|
|
|
|
|
|
use C<< my $args = shift->{params}; >> and it should work the same way. |
1773
|
|
|
|
|
|
|
|
1774
|
|
|
|
|
|
|
=head2 add_edit_row_pre_save, add_edit_row_post_save |
1775
|
|
|
|
|
|
|
|
1776
|
|
|
|
|
|
|
These fire right before and after a row is added/edited; a hashref is |
1777
|
|
|
|
|
|
|
passed with metadata such as the name of the table (in C), the |
1778
|
|
|
|
|
|
|
args from the original route setup (C), the table's key column |
1779
|
|
|
|
|
|
|
(C), and the values of the editable params (C). |
1780
|
|
|
|
|
|
|
|
1781
|
|
|
|
|
|
|
In the post-save hook, you are also sent C (the return value of |
1782
|
|
|
|
|
|
|
quick_insert or quick_update) telling you if the save was successful |
1783
|
|
|
|
|
|
|
(which is a little redundant because your post-save hook won't be called unless |
1784
|
|
|
|
|
|
|
the insert or update was successful). You'll also get |
1785
|
|
|
|
|
|
|
C giving you the instance of the handle used to save the entity |
1786
|
|
|
|
|
|
|
(so you can access last_insert_id()), and C (currently either |
1787
|
|
|
|
|
|
|
'create new' or 'update'). |
1788
|
|
|
|
|
|
|
|
1789
|
|
|
|
|
|
|
For instance, if you were dealing with a users table, you could use the |
1790
|
|
|
|
|
|
|
pre_save hook to hash the password before storing it - assuming for the sake |
1791
|
|
|
|
|
|
|
of example that you have a C function to return a hashed password: |
1792
|
|
|
|
|
|
|
|
1793
|
|
|
|
|
|
|
hook add_edit_row_pre_save => sub { |
1794
|
|
|
|
|
|
|
my $args = shift; |
1795
|
|
|
|
|
|
|
if ($args->{table_name} eq 'user') { |
1796
|
|
|
|
|
|
|
$args->{params}{password} = hash_pw($args->{params}{password}); |
1797
|
|
|
|
|
|
|
} |
1798
|
|
|
|
|
|
|
}; |
1799
|
|
|
|
|
|
|
|
1800
|
|
|
|
|
|
|
=head2 delete_row_pre_delete, delete_row_post_delete |
1801
|
|
|
|
|
|
|
|
1802
|
|
|
|
|
|
|
These fire right before and after a row is deleted. As with the |
1803
|
|
|
|
|
|
|
add_edit_row_pre_save and add_edit_row_post_save hooks, these are |
1804
|
|
|
|
|
|
|
passed a hashref with metadata such as the name of the table |
1805
|
|
|
|
|
|
|
(in C), the args from the original route setup (C), |
1806
|
|
|
|
|
|
|
the table's key column (C), and the values of the |
1807
|
|
|
|
|
|
|
editable params (C). As with the post-save hook, delete_row_post_delete hook won't be |
1808
|
|
|
|
|
|
|
called if we weren't able to delete the row. |
1809
|
|
|
|
|
|
|
|
1810
|
|
|
|
|
|
|
You could use these to clean up ancillary data associated with a |
1811
|
|
|
|
|
|
|
database row when it was deleted, for example. |
1812
|
|
|
|
|
|
|
|
1813
|
|
|
|
|
|
|
=head1 AUTHOR |
1814
|
|
|
|
|
|
|
|
1815
|
|
|
|
|
|
|
David Precious, C<< >> |
1816
|
|
|
|
|
|
|
|
1817
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
1818
|
|
|
|
|
|
|
|
1819
|
|
|
|
|
|
|
Alberto Simões (ambs) |
1820
|
|
|
|
|
|
|
|
1821
|
|
|
|
|
|
|
WK |
1822
|
|
|
|
|
|
|
|
1823
|
|
|
|
|
|
|
Johnathan Barber |
1824
|
|
|
|
|
|
|
|
1825
|
|
|
|
|
|
|
saberworks |
1826
|
|
|
|
|
|
|
|
1827
|
|
|
|
|
|
|
jasonjayr |
1828
|
|
|
|
|
|
|
|
1829
|
|
|
|
|
|
|
Paul Johnson (pjcj) |
1830
|
|
|
|
|
|
|
|
1831
|
|
|
|
|
|
|
Rahul Kotamaraju |
1832
|
|
|
|
|
|
|
|
1833
|
|
|
|
|
|
|
Michael J South (msouth) |
1834
|
|
|
|
|
|
|
|
1835
|
|
|
|
|
|
|
Martijn Lievaart |
1836
|
|
|
|
|
|
|
|
1837
|
|
|
|
|
|
|
Josh Rabinowitz |
1838
|
|
|
|
|
|
|
|
1839
|
|
|
|
|
|
|
Phil Carmody (thefatphil) |
1840
|
|
|
|
|
|
|
|
1841
|
|
|
|
|
|
|
=head1 BUGS |
1842
|
|
|
|
|
|
|
|
1843
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
1844
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
1845
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
1846
|
|
|
|
|
|
|
|
1847
|
|
|
|
|
|
|
|
1848
|
|
|
|
|
|
|
=head1 CONTRIBUTING |
1849
|
|
|
|
|
|
|
|
1850
|
|
|
|
|
|
|
This module is developed on Github: |
1851
|
|
|
|
|
|
|
|
1852
|
|
|
|
|
|
|
http://github.com/bigpresh/Dancer-Plugin-SimpleCRUD |
1853
|
|
|
|
|
|
|
|
1854
|
|
|
|
|
|
|
Bug reports, ideas, suggestions, patches/pull requests all welcome. |
1855
|
|
|
|
|
|
|
|
1856
|
|
|
|
|
|
|
Even just a quick "Hey, this is great, thanks" or "This is no good to me |
1857
|
|
|
|
|
|
|
because..." is greatly appreciated. It's always good to know if people are |
1858
|
|
|
|
|
|
|
using your code, and what they think. |
1859
|
|
|
|
|
|
|
|
1860
|
|
|
|
|
|
|
|
1861
|
|
|
|
|
|
|
=head1 SUPPORT |
1862
|
|
|
|
|
|
|
|
1863
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
1864
|
|
|
|
|
|
|
|
1865
|
|
|
|
|
|
|
perldoc Dancer::Plugin::SimpleCRUD |
1866
|
|
|
|
|
|
|
|
1867
|
|
|
|
|
|
|
You may find help with this module on the main Dancer IRC channel or mailing |
1868
|
|
|
|
|
|
|
list - see http://www.perldancer.org/ |
1869
|
|
|
|
|
|
|
|
1870
|
|
|
|
|
|
|
|
1871
|
|
|
|
|
|
|
You can also look for information at: |
1872
|
|
|
|
|
|
|
|
1873
|
|
|
|
|
|
|
=over 4 |
1874
|
|
|
|
|
|
|
|
1875
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
1876
|
|
|
|
|
|
|
|
1877
|
|
|
|
|
|
|
L |
1878
|
|
|
|
|
|
|
|
1879
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
1880
|
|
|
|
|
|
|
|
1881
|
|
|
|
|
|
|
L |
1882
|
|
|
|
|
|
|
|
1883
|
|
|
|
|
|
|
=item * CPAN Ratings |
1884
|
|
|
|
|
|
|
|
1885
|
|
|
|
|
|
|
L |
1886
|
|
|
|
|
|
|
|
1887
|
|
|
|
|
|
|
=item * Search CPAN |
1888
|
|
|
|
|
|
|
|
1889
|
|
|
|
|
|
|
L |
1890
|
|
|
|
|
|
|
|
1891
|
|
|
|
|
|
|
=back |
1892
|
|
|
|
|
|
|
|
1893
|
|
|
|
|
|
|
|
1894
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
1895
|
|
|
|
|
|
|
|
1896
|
|
|
|
|
|
|
Copyright 2010-18 David Precious. |
1897
|
|
|
|
|
|
|
|
1898
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
1899
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
1900
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
1901
|
|
|
|
|
|
|
|
1902
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
1903
|
|
|
|
|
|
|
|
1904
|
|
|
|
|
|
|
|
1905
|
|
|
|
|
|
|
=cut |
1906
|
|
|
|
|
|
|
|
1907
|
|
|
|
|
|
|
1; # End of Dancer::Plugin::SimpleCRUD |