Catalyst-Plugin-AutoCRUD
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/AutoCRUD/Model/StorageEngine/DBIC/CRUD.pm view on Meta::CPAN
else {
# proxy cols must be called as accessors, but normally we'd
# prefer to use get_column, so try both, otherwise empty str
my $evalue = eval{$row->get_column($col)};
if ($@) { $evalue = eval{$row->$col} }
if ($@) { $evalue = '' }
$data->{$col} = (defined $evalue ? $evalue : '');
}
}
#if ($ENV{AUTOCRUD_DEBUG} and $c->debug) {
# $c->log->debug( Dumper ['item:', $data] );
#}
# these are used for delete and update to overcome ExtJS single col PK
$data->{cpac__id} = _create_ID($row);
$data->{cpac__display_name} = _stringify($row);
push @{$response->{rows}}, $data;
}
# sort col which cannot be passed to the DB
if (exists $delay_page_sort{$sort}) {
@{$response->{rows}} = sort {
$dir eq 'ASC' ? ($a->{$sort} cmp $b->{$sort})
: ($b->{$sort} cmp $a->{$sort})
} @{$response->{rows}};
}
$response->{total} =
eval {$rs->pager->total_entries} || scalar @{$response->{rows}};
# user filtered by FK so do the paging now (will be S-L-O-W)
if ($page =~ m/^\d+$/ and $limit =~ m/^\d+$/ and scalar keys %delay_page_sort) {
my $pg = Data::Page->new;
$pg->total_entries(scalar @{$response->{rows}});
$pg->entries_per_page($limit);
$pg->current_page($page);
$response->{rows} = [ $pg->splice($response->{rows}) ];
$response->{total} = $pg->total_entries;
}
if ($ENV{AUTOCRUD_DEBUG} and $c->debug) {
$c->log->debug( Dumper $response );
$c->model($meta->extra('model'))->result_source->storage->debug(0);
}
return $self;
}
sub create {
my ($self, $c) = @_;
return &_create_update_txn($c, sub {
my $c = shift;
my $meta = $c->stash->{cpac}->{tm};
my $rs = $c->model( $meta->extra('model') );
return $rs->new({});
});
}
sub update {
my ($self, $c) = @_;
return &_create_update_txn($c, sub {
my $c = shift;
my $params = $c->req->params;
my $meta = $c->stash->{cpac}->{tm};
my $rs = $c->model( $meta->extra('model') );
return $rs->find(_extract_ID($params->{'cpac__id'} || ''), {key => 'primary'});
});
}
sub _create_update_txn {
my ($c, $mk_self_row) = @_;
my $meta = $c->stash->{cpac}->{tm};
my $response = $c->stash->{json_data} = {};
if ($ENV{AUTOCRUD_DEBUG} and $c->debug) {
$c->model($meta->extra('model'))->result_source->storage->debug(1);
}
my $success =
eval{ $c->model($meta->extra('model'))
->result_source->storage->txn_do(\&_create_update_core, $c, $mk_self_row) };
$response->{'success'} = (($success && !$@) ? 1 : 0);
$c->log->debug($@) if $@ and $c->debug;
if ($ENV{AUTOCRUD_DEBUG} and $c->debug) {
$c->model($meta->extra('model'))->result_source->storage->debug(0);
}
}
sub _create_update_core {
my ($c, $mk_self_row) = @_;
my $meta = $c->stash->{cpac}->{tm};
my $params = $c->req->params;
if ($ENV{AUTOCRUD_DEBUG} and $c->debug) {
use Data::Dumper;
$c->log->debug( Dumper $params );
}
my $self_row = $mk_self_row->($c);
my $proxy_updates = {};
my $update = {};
COL: foreach my $col (@{$meta->extra('fields')}) {
my $ci = $meta->f->{$col};
next COL if $ci->extra('is_reverse') or $ci->extra('masked_by');
if (not $ci->is_foreign_key) {
# fix for HTML standard which excludes checkboxes
$params->{$col} ||= 'false'
if $ci->data_type and $ci->data_type eq 'boolean';
# skip auto-inc cols unless they contain data
next COL unless exists $params->{$col}
and ($params->{$col} or not $ci->is_auto_increment);
# only works if user doesn't change the FK val
if ($ci->extra('is_proxy')) {
$proxy_updates->{$ci->extra('proxy_field')}
( run in 0.929 second using v1.01-cache-2.11-cpan-39bf76dae61 )