Catalyst-Plugin-AutoCRUD

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/AutoCRUD/Controller/Root.pm  view on Meta::CPAN

package Catalyst::Plugin::AutoCRUD::Controller::Root;
{
  $Catalyst::Plugin::AutoCRUD::Controller::Root::VERSION = '2.143070';
}

use strict;
use warnings;

use base 'Catalyst::Controller';
use Catalyst::Utils;
use SQL::Translator::AutoCRUD::Quick;
use File::Basename;
use Scalar::Util 'weaken';
use List::Util 'first';

__PACKAGE__->mk_classdata(_site_conf_cache => {});

# the templates are squirreled away in ../templates
(my $pkg_path = __PACKAGE__) =~ s{::}{/}g;
my (undef, $directory, undef) = fileparse(
    $INC{ $pkg_path .'.pm' }
);

sub base : Chained PathPart('autocrud') CaptureArgs(0) {
    my ($self, $c) = @_;

    $c->stash->{cpac} = {};
    $c->stash->{template} = 'list.tt';
    $c->stash->{current_view} = 'AutoCRUD::TT';
    $c->stash->{cpac}->{g}->{version} = 'CPAC v'
        . $Catalyst::Plugin::AutoCRUD::VERSION;
    $c->stash->{cpac}->{g}->{site} = 'default';

    # load enough metadata to display schema and sources
    if (!exists $self->_site_conf_cache->{dispatch}) {
        my $dispatch = {};
        foreach my $backend ($self->_enumerate_backends($c)) {
            my $new_dispatch = $c->forward($backend, 'dispatch_table') || {};
            for (keys %$new_dispatch) {$new_dispatch->{$_}->{backend} = $backend}
            $dispatch = merge_hashes($dispatch, $new_dispatch);
        }
        $self->_site_conf_cache->{dispatch} = $dispatch;
        $c->log->debug("autocrud: generated global dispatch table") if $c->debug;
    }

    # param becomes a list when js grid filter is added to url query string.
    # suppress that back to single item, and also set up filter_params for ease
    foreach my $k (%{ $c->req->params }) {
        next unless $k =~ m/^cpac_filter\./;
        $c->stash->{cpac}->{g}->{filter_params}->{$k}
            = ref $c->req->params->{$k} eq ref [] ? pop @{ $c->req->params->{$k} }
                                                  : $c->req->params->{$k};
        $c->req->params->{$k} = $c->stash->{cpac}->{g}->{filter_params}->{$k};
    }

    # cpac.c.<schema>.t.<source>.<property>
    $c->stash->{cpac}->{c} = $self->_site_conf_cache->{dispatch};
}

sub _enumerate_backends {
    my ($self, $c) = @_;

    my @backends = @{ $c->config->{'Plugin::AutoCRUD'}->{backends} };
    $c->log->debug('autocrud: backends are '. join ',', @backends) if $c->debug;
    return @backends;
}

sub merge_hashes { return Catalyst::Utils::merge_hashes(@_) }

# =====================================================================

# old back-compat /<schema>/<source> which uses default site
# also good for friendly URLs which use default site

lib/Catalyst/Plugin/AutoCRUD/Controller/Root.pm  view on Meta::CPAN


    my $site = $c->stash->{cpac}->{g}->{site};
    my $db = $c->stash->{cpac}->{g}->{db};
    my $table = $c->stash->{cpac}->{g}->{table};

    $c->detach('err_message') if !exists $c->stash->{cpac}->{c}->{$db}
        or !exists $c->stash->{cpac}->{c}->{$db}->{t}->{$table};

    # it's the whole schema, because related table data is also required.
    if (!exists $self->_site_conf_cache->{meta}->{$db}) {
        $self->_site_conf_cache->{meta}->{$db} = SQL::Translator::AutoCRUD::Quick->new(
            $c->forward($c->stash->{cpac}->{c}->{$db}->{backend}, 'schema_metadata'));
        $c->log->debug("autocrud: generated schema metadata for [$db]") if $c->debug;
    }

    $c->stash->{cpac}->{m} = $self->_site_conf_cache->{meta}->{$db};
    $c->log->debug("autocrud: retrieved cached schema metadata for [$db]") if $c->debug;

    foreach my $so (keys %{ $c->stash->{cpac}->{c}->{$db}->{t} }) {
        my $user = $c->config->{'Plugin::AutoCRUD'}->{sites}->{$site}->{$db}->{$so} || {};
        my $conf = $c->stash->{cpac}->{c}->{$db}->{t}->{$so};
        my $meta = $c->stash->{cpac}->{m}->t->{$so};
        my $visible = {};

        # columns from the user conf can be loaded (for current db only - lazy)
        if ((ref $user->{columns} eq ref []) and scalar @{$user->{columns}}) {
            foreach my $c (@{$user->{columns}}) {
                next unless exists $meta->f->{$c};
                push @{$conf->{cols}}, $c;
                ++$visible->{$c};
            }
            foreach my $c (@{$meta->extra('fields')}) {
                next if exists $visible->{$c};
                push @{$conf->{cols}}, $c;
                $conf->{hidden_cols}->{$c} = 1;
            }
        }
        # set a default list of cols according to some sane rules
        else {
            $conf->{cols} = [@{$meta->extra('fields')}];
            $conf->{hidden_cols}->{$_} = 1 for grep {
                $meta->f->{$_}->extra('masked_by') or
                ($meta->f->{$_}->extra('ref_table')
                    and $meta->f->{$_}->extra('rel_type') eq 'has_many'
                    and not $c->stash->{cpac}->{m}->t->{$meta->f->{$_}->extra('ref_table')}->is_data)
            } @{$meta->extra('fields')};
        }

        # headings from the user conf can be loaded (for current db only - lazy)
        foreach my $f (@{$meta->extra('fields')}) {
            $conf->{headings}->{$f} =
                $user->{headings}->{$f} || $meta->f->{$f}->extra('display_name');
        }
    }

    # set up helper variables for templates
    $c->stash->{cpac_db} = $db;
    $c->stash->{cpac_table} = $table;
    $c->stash->{cpac}->{tm} = $c->stash->{cpac}->{m}->t->{$table};
    $c->stash->{cpac}->{tc} = $c->stash->{cpac}->{c}->{$db}->{t}->{$table};
    weaken $c->stash->{cpac}->{tm};
    weaken $c->stash->{cpac}->{tc};
}

sub helloworld : Chained('base') Args(0) {
    my ($self, $c) = @_;
    $c->forward('build_site_config');
    $c->stash->{cpac}->{g}->{title} = 'Hello World';
    $c->stash->{template} = 'helloworld.tt';
}

sub end : ActionClass('RenderView') {
    my ($self, $c) = @_;
    my $frontend = $c->stash->{cpac}->{g}->{frontend} || 'extjs2';

    $c->stash->{cpac}->{g} = merge_hashes(
        $c->stash->{cpac}->{g},
        _one_level_of($c->config->{'Plugin::AutoCRUD'}));

    my $tt_path = $c->config->{'Plugin::AutoCRUD'}->{tt_path};
    $tt_path = (defined $tt_path ? (ref $tt_path eq '' ? [$tt_path] : $tt_path ) : [] );

    push @$tt_path, "$directory../templates/$frontend";
    $c->stash->{additional_template_paths} = $tt_path;
}

1;
__END__



( run in 1.911 second using v1.01-cache-2.11-cpan-d8267643d1d )