App-AutoCRUD
view release on metacpan or search on metacpan
lib/App/AutoCRUD/Controller/Table.pm view on Meta::CPAN
$data->{offset} = ($page_index - 1) * $page_size + 1;
$data->{similar_query} = $self->_query_string(%$req_data,
-page_index => 1);
$data->{next_page} = $self->_query_string(%$req_data,
-page_index => $page_index+1)
unless @{$data->{rows}} < $page_size;
$data->{prev_page} = $self->_query_string(%$req_data,
-page_index => $page_index-1)
unless $page_index <= 1;
}
sub id {
my ($self, $table) = @_;
my $data = $self->descr($table);
my $pk = $data->{primary_key};
my @vals = $self->context->extract_path_segments(scalar(@$pk));
my %criteria = mesh @$pk, @vals;
# get row from database
my $row = $self->datasource->schema->db_table($table)->fetch(@vals);
# assemble results
$data->{row} = $row;
$data->{pk_val} = join "/", @vals;
# links
my %where_pk = map { ("where_pk.$_" => $criteria{$_}) } keys %criteria;
$data->{where_pk} = $self->_query_string(%where_pk);
return $data;
}
sub search {
my ($self, $table) = @_;
my $context = $self->context;
my $req_data = $context->req_data;
if ($context->req->method eq 'POST') {
my $output = delete $req_data->{-output} || "";
my $cols = [keys %{delete $req_data->{col} || {}}];
$req_data->{-columns} = join ",", @$cols;
$self->redirect("list$output?" . $self->_query_string(%$req_data));
}
else {
# display the search form
my @cols = split /,/, (delete $req_data->{-columns} || "");
$req_data->{"col.$_"} = 1 foreach @cols;
my $data = $self->descr($table);
$data->{init_form} = $self->_encode_json($req_data);
return $data;
}
}
sub update {
my ($self, $table) = @_;
$self->_check_canmodify;
if ($self->context->req->method eq 'POST') {
$self->_do_update_data($table);
}
else {
$self->_display_update_form($table);
}
}
sub _check_canmodify {
my ($self) = @_;
if ($self->context->app->readonly) {
die 'readonly mode';
}
}
sub _do_update_data {
my ($self, $table) = @_;
$self->_check_canmodify;
my $context = $self->context;
my $req_data = $context->req_data;
my $datasource = $context->datasource;
# columns to update
my $to_set = $req_data->{set} || {};
foreach my $key (keys %$to_set) {
my $val = $to_set->{$key};
delete $to_set->{$key} if ! length $val;
$to_set->{$key} = undef if $val eq 'Null';
}
keys %$to_set or die "nothing to update";
# build filtering criteria
my $where = $req_data->{where} or die "update without any '-where' clause";
my $criteria = $datasource->query_parser->parse($where);
$criteria and keys %$criteria or die "update without any '-where' criteria";
# perform the update
my $db_table = $datasource->schema->db_table($table);
my $n_updates = $db_table->update(-set => $to_set, -where => $criteria);
# redirect to a list to display the results
my $message = ($n_updates == 1) ? "1 record was updated"
: "$n_updates records were updated";
# TODO: $message could repeat the $to_set pairs
my $query_string = $self->_query_string(%$where, -message => $message);
$self->redirect("list?$query_string");
}
sub _display_update_form {
my ($self, $table) = @_;
$self->_check_canmodify;
( run in 0.940 second using v1.01-cache-2.11-cpan-bbb979687b5 )