Catalyst::Example::Controller::InstantCRUD - Catalyst CRUD example Controller


Catalyst-Example-InstantCRUD documentation Contained in the Catalyst-Example-InstantCRUD distribution.

Index


Code Index:

NAME

Top

Catalyst::Example::Controller::InstantCRUD - Catalyst CRUD example Controller

VERSION

Top

This document describes Catalyst::Example::Controller::InstantCRUD version 0.0.14

SYNOPSIS

Top

    use base Catalyst::Example::Controller::InstantCRUD;

  


DESCRIPTION

Top

INTERFACE

Top

METHODS

load_interface_config Returns the config hash for input forms (widgets) and other interface elements
get_resultset Returns the resultset appriopriate for the page parameters.
model_resultset Returns a resultset from the model.
model_item Returns an item from the model.
source_name Class method for finding name of corresponding database table.
add Method for displaying form for adding new records
auto Adds Controller name as additional directory to search for templates
index Forwards to list
destroy Deleting records.
do_add Method for adding new records
do_edit Method for editin existing records
edit Method for displaying form for editing a record.
list Method for displaying pages of records
view Method for diplaying one record

DIAGNOSTICS

Top

Error message here, perhaps with %s placeholders

[Description of error here]

Another error message here

[Description of error here]

[Et cetera, et cetera]

CONFIGURATION AND ENVIRONMENT

Top

Catalyst::Example::Controller::InstantCRUD requires no configuration files or environment variables.

DEPENDENCIES

Top

None.

INCOMPATIBILITIES

Top

None reported.

BUGS AND LIMITATIONS

Top

No bugs have been reported.

Please report any bugs or feature requests to bug-catalyst-example-controller-instantcrud@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Top

<Zbigniew Lukasiak> <<zz bb yy @ gmail.com>> <Jonas Alves> <<jonas.alves at gmail.com>> <Lars Balker Rasmussen>

LICENCE AND COPYRIGHT

Top

DISCLAIMER OF WARRANTY

Top

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


Catalyst-Example-InstantCRUD documentation Contained in the Catalyst-Example-InstantCRUD distribution.

package Catalyst::Example::Controller::InstantCRUD;

use Moose;
BEGIN {
       extends 'Catalyst::Controller';
}

with 'Catalyst::Component::ContextClosure';

use Carp;
use Data::Dumper;
use Path::Class;

our $VERSION = '0.016';

has source_name => ( isa => 'Str', is => 'rw', lazy => 1, builder => 'build_source_name' );
sub build_source_name {
    my $self  = shift;
    my $class = ref $self;
    $class =~ /([^:]*)$/;
    return $1;
}

has form_class => ( isa => 'Str', is => 'rw', lazy => 1, builder => 'build_form_class' );
sub build_form_class {
    my $self  = shift;
    return ref( $self ) . '::' . $self->source_name . 'Form';
}

sub auto : Action {
    my ( $self, $c ) = @_;
    $c->stash->{additional_template_paths} = [ dir( $c->config->{root}, lc $self->source_name) . '', $c->config->{root} . ''];
}

sub model_item {
    my ( $self, $c, @pks ) = @_;
    my $rs = $self->model_resultset($c);
    my @pk_columns = $rs->result_source->primary_columns;
    my $data = {
        map { $pk_columns[$_] => $pks[$_] } 0..$#pk_columns
    };
    my $item = $rs->find($data, { key => 'primary' });
    return $item;
}

sub model_resultset {
    my ( $self, $c ) = @_;
    my $source     = $self->source_name;
    return $self->model_schema($c)->resultset($source);
}

sub model_schema {
    my ( $self, $c ) = @_;
    my $model_name = $c->config->{InstantCRUD}{model_name};
    return $c->model($model_name);
}

sub index : Private {
    my ( $self, $c ) = @_;
    $c->stash->{template} = lc( $self->source_name ) . '/list.tt';
    $c->forward('list');
}

sub destroy : Action {
    my ( $self, $c, @pks ) = @_;
    if ( $c->req->method eq 'POST' ) {
        $self->model_item( $c, @pks )->delete;
        $c->res->redirect(
            $c->uri_for( $self->action_for('list') )
        );
    }
    else {
        my $action_uri = $c->uri_for( $self->action_for('destroy'), @pks);
        $c->stash->{destroywidget} = <<END;
<form action="$action_uri" id="widget" method="post">
<fieldset class="widget_fieldset">
<input class="submit" id="widget_ok" name="ok" type="submit" value="Delete ?" />
</fieldset>
</form>
END
    }
}

sub build_form {
    my ( $self, $c, @pks ) = @_;
    my @ids;
    @ids = ( item_id => [ @pks ] ) if @pks;
    return $self->form_class->new( 
        schema => $self->model_schema($c), 
        params => $c->req->params, 
        @ids,
    );
}

sub edit : Action {
    my ( $self, $c, @pks ) = @_; 
    my $form = $self->build_form($c, @pks);
    my $item = $form->item;
    if( $c->req->method eq 'POST' && $form->process() ){
        $item = $form->item;
        $c->res->redirect( $c->uri_for( $self->action_for('view'), $item->id ) );
    }
    if( @pks ){
        $form->field( 'submit' )->value( 'Update' );
    }
    else{
        $form->field( 'submit' )->value( 'Create' );
    }

    $c->stash(
        form => $form,
        item => $item,
    );
}

sub view : Action {
    my ( $self, $c, @pks ) = @_;
    die "You need to pass an id" unless @pks;
    my $item = $self->model_item( $c, @pks );
    $c->stash->{item} = $item;
}

sub get_resultset {
    my ( $self, $c ) = @_;
    my $params = $c->request->params;
    my $order = $params->{'order'};
    $order .= ' DESC' if $params->{'o2'};
    my $join;
    if ( $order && $order =~ m/(\w+)\.\w+/ ) {
        $join = $1;
    }
    my $maxrows = $c->config->{InstantCRUD}{maxrows} || 10;
    my $page = $params->{'page'} || 1;
    return $self->model_resultset($c)->search(
        {},
        {
            page     => $page,
            $join ? ( join => $join ) : (),
            order_by => $order,
            rows     => $maxrows,
        }
    );
}

sub create_col_link {
    my ( $self, $c, $source ) = @_;
    my $origparams = $c->request->params;
    return $self->make_context_closure(sub {
        my ( $ctx, $column, $label ) = @_;
        my $addr;
        no warnings 'uninitialized';
        if ( $origparams->{'order'} eq $column && !$origparams->{'o2'} ) {
            $addr = $ctx->request->uri_with({ page => 1, order =>  $column, o2 => 'desc' });
        }else{
            $addr = $ctx->request->uri_with({ page => 1, order =>  $column, o2 => undef });
        }
        my $result = qq{<a href="$addr">$label</a>};
        if ( $origparams->{'order'} && $column eq $origparams->{'order'} ) {
            $result .= $origparams->{'o2'} ? "&darr;" : "&uarr;";
        }
        return $result;
    }, $c);
}

sub list : Action {
    my ( $self, $c ) = @_;
    my $result = $self->get_resultset($c);
    $c->stash->{pager}     = $result->pager;
    my $source  = $result->result_source;
    ($c->stash->{pri}) = $source->primary_columns;
    $c->stash->{order_by_column_link} = $self->create_col_link($c, $source);
    $c->stash->{result} = $result;
}



1;

__END__