App-AutoCRUD
view release on metacpan or search on metacpan
lib/App/AutoCRUD/Controller/Table.pm view on Meta::CPAN
my $db_table = $datasource->schema->db_table($table);
my @pk = $db_table->insert($req_data);
# redirect to a list to display the results
my $message = "1 record was inserted";
my $query_string = $self->_query_string(-message => $message);
$self->redirect(join("/", "id", @pk) . "?$query_string");
}
else {
# display the insert form
my $data = $self->descr($table);
$data->{init_form} = $self->_encode_json($req_data);
return $data;
}
}
sub count_where { # used in Ajax mode by update and delete forms
my ($self, $table) = @_;
my $context = $self->context;
my $req_data = $context->req_data;
my $datasource = $context->datasource;
my $n_records = -1;
if (my $where = $req_data->{where}) {
my $criteria = $datasource->query_parser->parse($where);
if ($criteria and keys %$criteria) {
my $db_table = $datasource->schema->db_table($table);
my $result = $db_table->select(
-columns => 'COUNT(*)',
-where => $criteria,
-result_as => 'flat_arrayref',
);
$n_records = $result->[0];
}
}
return {n_records => $n_records};
}
#----------------------------------------------------------------------
# auxiliary methods
#----------------------------------------------------------------------
sub _query_string {
my ($self, %params) = @_;
my @fragments;
KEY:
foreach my $key (sort keys %params) {
my $val = $params{$key};
length $val or next KEY;
# cheap URI escape (for chars '=', '&', ';' and '+')
s/=/%3D/g, s/&/%26/g, s/;/%3B/g, s/\+/%2B/g for $key, $val;
push @fragments, "$key=$val";
}
return join "&", @fragments;
}
sub _encode_json {
my ($self, $data) = @_;
# utf8-encoding is done in the view, so here we turn it off
my $json_maker = JSON::MaybeXS->new(allow_blessed => 1,
convert_blessed => 1,
utf8 => 0);
return $json_maker->encode($data);
}
sub _mark_multicols_keys {
my ($self, $data) = @_;
if (my $sep = $self->datasource->schema->sql_abstract->multicols_sep) {
# in case of multi-columns keys, the form needs to add special fields
# and to ignore regular fields for those columns
my $where = $self->context->req_data->{where} || {};
my @multi_cols_keys = grep m[$sep], keys %$where;
$data->{multi_cols_keys} = \@multi_cols_keys;
$data->{ignore_col}{$_} = 1 foreach map {split m[$sep]} @multi_cols_keys;
}
}
1;
__END__
=head1 NAME
App::AutoCRUD::Controller::Table - Table controller
=head1 DESCRIPTION
This controller provides methods for searching and describing
a given table within some datasource.
=head1 METHODS
=head2 serve
Entry point to the controller; from the URL, it extracts the table
name and the name of the method to dispatch to (the URL is expected
to be of shape C<< table/{table_name}/{$method_name}?{arguments} >>).
It also sets the default template to C<< table/{method_name}.tt >>.
=head2 descr
Returns a hashref describing the table, with keys C<descr>
(description information from the config), C<table> (table name),
( run in 2.308 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )