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