App-ZofCMS

 view release on metacpan or  search on metacpan

lib/App/ZofCMS/Plugin/CRUD.pm  view on Meta::CPAN

package App::ZofCMS::Plugin::CRUD;

use strict;
use warnings;

our $VERSION = '1.001008'; # VERSION

use DBI;
use Carp;
use HTML::Template;
use File::Spec;
use base 'App::ZofCMS::Plugin::Base';

sub _key { 'plug_crud' }
sub _defaults {
    return (
        table       => 'products',
        file_dir    => 'files',
        can         => 'CRUDL',
        no_time_sort => 0,
        opt         => {
            RaiseError => 1,
            AutoCommit => 1,
            mysql_enable_utf8 => 1,
        },

        # These are plug's parameters, but with no defaults
        #   dsn     => 'DBI:mysql:database=zofdb;host=localhost',
        #   user    => db_login
        #   pass    => db_pass
        #   opt     => db_opt
        #   items   => CRUD items
        #  list_sub => # sub for processing the list of output items

    );
}
sub _do {
    my ( $self, $conf, $t, $q, $config ) = @_;

    return
        unless $conf->{items} and @{ $conf->{items} || [] };

    if ( ref $conf->{can} eq 'CODE' ) {
        $conf->{can} = $conf->{can}->( $t, $q, $config );
    }

    $conf->{can} = {
        map +( $_ => 1 ),
            grep /^[crudl]$/, map lc, split //, $conf->{can}
    };

    for ( keys %{ $conf->{can} } ) {
        $t->{t}{"crud_can_$_"} = 1;
    }

    @$self{ qw/CONF  T  Q  CONFIG/ } = ( $conf, $t, $q, $config );

    croak "Cannot have backticK(`) character in `table` parameter"
        if $conf->{table} =~ /`/;

    croak "Cannot have backticK(`) character in `order_by` parameter"
        if $conf->{order_by} =~ /`/;

    $self->_prepare_items;

    if ( $q->{crud_update_save} and $conf->{can}{u} ) {
        $self->_process_UPDATE_SAVE;
    }
    elsif ( $q->{crud_update} and $conf->{can}{u} ) {
        $self->_process_UPDATE_LOAD;
    }
    elsif ( $q->{crud_delete} and $conf->{can}{d} ) {
        $self->_process_DELETE;
    }
    elsif ( $q->{crud_create} and $conf->{can}{c} ) {
        $self->_process_CREATE;
    }

    if ( $conf->{can}{c}
        and not $q->{crud_update}
        and not $q->{crud_update_save}
    ) {
        $t->{t}{crud_form} = $self->_create_CU_form('yes_is_create_form');
    }

lib/App/ZofCMS/Plugin/CRUD.pm  view on Meta::CPAN

        <table>
        <thead>
            <tr>
                <td>File</td>
                <td>Description</td>
                <td>Add Time</td>
            </tr>
        </thead>
        <tbody>
            <tmpl_loop name='crud_items'>
                <tr>
                    <td>
                    <tmpl_var name='crud_d_form'>
                    <tmpl_var name='crud_u_form'>
                    <a href="<tmpl_var escape='html' name='file'>"
                        target="_blank"><tmpl_var
                        escape='html' name='item'></a></td>
                    <td><tmpl_var name='description'></td>
                    <td><tmpl_var name='foo1'></td>
                    <td><tmpl_var name='foo2'></td>
                    <td><tmpl_var escape='html' name='time'></td>
                </tr>
            </tmpl_loop>
        </tbody>
        </table>
    <tmpl_else>
        <p>Currently there are no items in the database.</p>
    </tmpl_if>

=head1 DESCRIPTION

The plugin provides a generic "Create Read Update Delete List" functionality.
(Currently, READ is not implemented). In conjunction with this plugin,
you might find these plugins useful L<App::ZofCMS::Plugin::DBIPPT>
and L<App::ZofCMS::Plugin::FormChecker>.

=head1 ZofCMS TEMPLATE/MAIN CONFIG FILE FIRST LEVEL KEYS

The keys can be set either in ZofCMS template or in Main Config file,
if same keys are set in both, then the one in ZofCMS
template takes precedence.

=head2 C<plugins>

    plugins => [ qw/CRUD/ ],

You obviously would want to include the plugin in the
list of plugins to execute.

=head2 C<plug_crud>

    ### Mandatory fields without defaults: dsn, user, items

    plug_crud => {
        dsn             => 'DBI:mysql:database=zofdb;host=localhost',
        user            => 'db_login',
        pass            => 'db_pass',
        opt             => {
            RaiseError        => 1,
            AutoCommit        => 1,
            mysql_enable_utf8 => 1,
        },
        items       => [
            'Item',
            { Description => [ 'textarea', optional => 1 ] },
            { File        => 'file'                        },
            { Time        => sub { time(); }               },
        ],
        list_sub        => # sub for processing the list of output items
        table           => 'products',
        file_dir        => 'files/',
        can             => 'CRUDL',
        no_time_sort    => 0,
    }

B<Mandatory>. Takes either a hashref or a subref as a value. If a subref is
specified, its return value will be assigned to C<plug_crud> as if
it were already there. If the sub returns an C<undef>, then the plugin
will stop
further processing. The C<@_> of the subref will contain (in that order):
ZofCMS Template hashref, query parameters hashref, and
L<App::ZofCMS::Config> object. Possible keys/values for the hashref
are as follows:

=head3 C<dsn>

    dsn => "DBI:mysql:database=test;host=localhost",

B<Mandatory>. Specifies the "DSN" for L<DBI> module. See L<DBI>'s docs for C<connect_cached()> method for more info on this one.

=head3 C<user>

    user => 'test',

B<Mandatory>. Specifies your username for the SQL database.

=head3 C<pass>

    pass => 'test',

B<Optional>. Specifies your password for the SQL database. If not specified,
behaviour will be akin to not having a set password.

=head3 C<opt>

    opt => {
        RaiseError => 1,
        AutoCommit => 1,
        mysql_enable_utf8 => 1,
    }

B<Optional>. Takes a hashref as a value. Specifies the
additional options for L<DBI>'s C<connect_cached()> method.
See L<DBI>'s docs for C<connect_cached()> method for more info on this
one. B<Defaults to:>
C<< { RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => 1, } >>

=head3 C<table>

    table => 'products',

B<Optional>. Takes a string as a value that represents the name of the
table in which to store the data. B<Defaults to:> C<products>

=head3 C<items>

    items => [
        'Item',
        { Description => [ 'textarea', optional => 1 ] },
        { File        => 'file'                        },
        { Time        => sub { time(); }               },
    ],

B<Mandatory>. B<Takes >an arrayref as a value that must contain at least
one item in it. Specifies the list of items
that comprise a single record that the plugin will create/read/update/delete.
B<Important note:> if you're not using C<time> item
to store time, see C<no_time_sort> option below; you'll likely want it set
to a true value, to avoid the plugin from erroring out.
The items in a list can be of these types:

=head4 String

    items => [
        'Item',
        q|Employee's Performance Record #|,
    ],

A simple string will be represented as a C<< <input type="text"> >>
in the Create/Update HTML form. The string will become the
C<< <label> >> text for the form element. Then everything in it that doesn't
match C<\w> will be converted into underscores, and C<lc()> will be run,
and that new string will be used for:

=over 10

=item * SQL column name for that field

=item * HTML C<< <input> >>'s C<name=""> attribute in the Create/Update form

=item * HTML C<id=""> attribute in the Create/Update form (prefix
C<crud_> will be added)

=item * C<< <tmpl_var> >> name for the List function of the plugin.

=back

=head4 B<An example:>

We have two items in our record that we'll manipulate with this plugin:

    items => [
        'Item',
        q|Employee's Performance Record #|,
    ],



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