Dancer-Plugin-SimpleCRUD

 view release on metacpan or  search on metacpan

lib/Dancer/Plugin/SimpleCRUD.pm  view on Meta::CPAN


        $query .= " ORDER BY "
            . $dbh->quote_identifier($order_by_table) . "."
            . $dbh->quote_identifier($order_by_column)
            . " $order_by_direction ";
    }

    if ($args->{paginate} && $args->{paginate} =~ /^\d+$/) {
        my $page_size = $args->{paginate};

        my $qt   = uri_escape($q);
        my $sf   = uri_escape($searchfield);
        my $st   = uri_escape(params->{searchtype} || $default_searchtype);
        my $o    = uri_escape(params->{'o'}         || "");
        my $d    = uri_escape(params->{'d'}         || "");
        my $page = uri_escape(params->{'p'}         || 0);
        $page = 0 unless $page =~ /^\d+$/;

        my $offset = $page_size * $page;
        my $limit  = $page_size;

        my $url = _external_url($args->{dancer_prefix}, $args->{prefix})
            . "?o=$o&d=$d&q=$qt&searchfield=$sf&searchtype=$st";
        $html .= "<p>";
	$html .= "<table class=\"$paginate_table_class\"><tr>";

        if ($page > 0) {
            $html
                .= sprintf(
                "<td><a href=\"%s&p=%d\">&larr;&nbsp;prev.&nbsp;page</a></td>",
                $url, $page - 1)
        } else {
            $html .= "<td>&larr;&nbsp;prev.&nbsp;page&nbsp</td>";
        }
        $html .= sprintf(
            "<td>Showing page %d (records %d to %d)",
            $page + 1,
            $offset + 1,
            $offset + 1 + $limit
        );
        $html .= "</td>";
        $html .= sprintf("<td><a href=\"%s&p=%d\">next&nbsp;page&nbsp;&rarr;</a>",
            $url, $page + 1);
        $html .= "</td></tr></table>";

        $query .= " LIMIT $limit OFFSET $offset ";
    }

    debug("Running query: $query");
    my $sth = $dbh->prepare($query);
    $sth->execute(@binds)
        or die "Failed to query for records in $table_name - "
        . $dbh->errstr;

    if ($args->{downloadable} && params->{format}) {

        ##Return results as a downloaded file, instead of generating the HTML table.
        return _return_downloadable_query($args, $sth, params->{format});
    }

    my @custom_callbacks = ();
    for my $custom_col_spec (@{ $args->{custom_columns} || [] } ) {
        push @custom_callbacks, {
            column=>$custom_col_spec->{name}, 
            transform=> ($custom_col_spec->{transform} or sub { return shift;}),
        };
    }


    my $table = HTML::Table::FromDatabase->new(
        -sth       => $sth,
        -border    => 1,
        -callbacks => [
            {
                column    => 'actions',
                transform => sub {
                    my $id = shift;
                    my $action_links;
                    if ($args->{editable} && _has_permission('edit', $args)) {
                        my $edit_url
                            = _external_url(
                                $args->{dancer_prefix}, $args->{prefix}, 
                                "/edit/$id"
                            );
                        $action_links
                            .= qq[<a href="$edit_url" class="edit_link">Edit</a>];
                        if ($args->{deletable} && _has_permission('edit', $args)) {
                            my $del_url =_external_url(
                                $args->{dancer_prefix}, $args->{prefix},
                                "/delete/$id"
                            );
                            $action_links
                                .= qq[ / <a href="$del_url" class="delete_link"]
                                . qq[ onclick="delrec('$id'); return false;">]
                                . qq[Delete</a>];
                        }
                    }
                    return $action_links;
                },
            },
            @custom_callbacks,
        ],
        -rename_headers      => \%columns_sort_options,
        -html                => 'escape',
        -class               => $table_class,
    );

    # apply custom columns' column_classes as specified. Can this be done via HTML::Table::FromDatabase->new() above?
    my @all_column_names = ( (map { $_->{COLUMN_NAME} } @$columns), (map { $_->{name} } @{$args->{custom_columns}}) );
    for my $custom_col_spec (@{ $args->{custom_columns} || [] } ) {
        if (my $column_class = $custom_col_spec->{column_class}) {
            my $first_index = first_index { $_ eq $custom_col_spec->{name} } uniq @all_column_names;
            die "Cannot find index of column '$custom_col_spec->{name}'" if ($first_index == -1);
            $table->setColClass( 1 + $first_index, $column_class );
        }
    }

    my $add_link_html = "";
    if ($args->{addable} && _has_permission('edit', $args)) {
        $add_link_html = sprintf '<p><a href="%s">Add a new %s</a></p>',
            _external_url($args->{dancer_prefix}, $args->{prefix}, '/add'),
            $args->{record_title};
    }

    $html .= $add_link_html;

    $html .= $table->getTable || '';

    $html .= $add_link_html;

    if ($args->{deleteable} && _has_permission('delete', $args)) {

        # Append a little Javascript which asks for confirmation that they'd
        # like to delete the record, then makes a POST request via a hidden
        # form.  This could be made AJAXy in future.
        my $del_action = _external_url(
            $args->{dancer_prefix}, $args->{prefix}, '/delete'
        );
        $html .= <<DELETEJS;
<form name="deleteform" method="post" action="$del_action">
<input name="record_id" type="hidden">
</form>
<script language="Javascript">
function delrec(record_id) {
    if (confirm('Confirm you wish to delete this record?')) {
        document.deleteform.rowid.value = record_id;
        document.deleteform.submit();
    }
}
</script>

DELETEJS
    }

    return _apply_template($html, $args->{'template'}, $args->{'record_title'});
}

sub _apply_template {
    my ($html, $template, $title) = @_;

    if ($template) {



( run in 0.533 second using v1.01-cache-2.11-cpan-39bf76dae61 )