package App::ZofCMS::Plugin::ConfigToTemplate;

use warnings;
use strict;

our $VERSION = '0.0102';

use base 'App::ZofCMS::Plugin::Base';

sub _key { 'plug_config_to_template' }
sub _defaults {
    return (
        cell         => 'd',
        key          => 'public_config',
        config_cell  => 'public_config',
        config_keys  => undef,
        noop         => 0,
    );
}
sub _do {
    my ( $self, $conf, $t, $q, $config ) = @_;

    return
        if $conf->{noop};

    my $zcms_config = $config->conf;
    my ( $cell, $key, $ccell, $ckeys ) = @$conf{ qw/cell key config_cell config_keys/ };

    my @keys;
    if ( defined $ckeys ) {
        @keys = @$ckeys;
    }
    else {
        @keys = keys %{ $zcms_config->{ $ccell } || {} };
    }

    for ( @keys ) {
        if ( defined $key  ) {
            $t->{ $cell }{ $key }{ $_ } = $zcms_config->{ $ccell }{ $_ };
        }
        else {
            $t->{ $cell }{ $_ } = $zcms_config->{ $ccell }{ $_ };
        }
    }
}

=head1 NAME

App::ZofCMS::Plugin::ConfigToTemplate - plugin to dynamically stuff Main Config File keys into ZofCMS Template

=head1 SYNOPSIS

In Main Config File:

    public_config => {
        name => 'test',
        value => 'plug_test',
    },

In ZofCMS Template:

    plugins => [
        { ConfigToTemplate => 2000 },
    ],

    plug_config_to_template => {
        key     => undef,
        cell    => 't',
    },

Now we can use `name` and `value` variables in HTML::Template template...

=head1 DESCRIPTION

The module is a plugin for L<App::ZofCMS> that technically expands functionality of
Main Config File's C<template_defaults> special key.

Using this plugin you can dynamically (and more "on demand") stuff keys from Main Config File
to ZofCMS Template hashref without messing around with other plugins and poking with
->conf method

This documentation assumes you've read L<App::ZofCMS>, L<App::ZofCMS::Config> and L<App::ZofCMS::Template>

=head1 Main Config File and ZofCMS Template First Level Keys

=head2 C<plugins>

    plugins => [
        { ConfigToTemplate => 2000 },
    ],

You need to include the plugin in the list of plugins to run.

=head2 C<plug_config_to_template>

    # these are the default values
    plug_config_to_template => {
        cell         => 'd',
        key          => 'public_config',
        config_cell  => 'public_config',
        config_keys  => undef,
        noop         => 0,
    }

The C<plug_config_to_template> must be present in order for the plugin to run. It takes a
hashref as a value. Keys of this hashref can be set in either (or both) Main Config File and
ZofCMS Template - they will be merged together if set in both files; if the same key is set in
both files, the value set in ZofCMS Template will take precedence. All keys are optional, to
run the plugins with all the defaults use an empty hashref. Possible keys/values
are as follows:

=head3 C<cell>

    cell => 'd',

B<Optional>. Specifies the cell (first-level key) in ZofCMS Template hashref where to put
config file data. B<Defaults to:> C<d>

=head3 C<key>

    key => 'public_config',

    key => undef,

B<Optional>. Specifies the key in the cell (i.e. the second-level key inside the first-level
key) of where to put config file data. B<Can be set to> C<undef> in which case data will be
stuffed right into the cell. B<Defaults to:> C<public_config>

=head3 C<config_cell>

    config_cell  => 'public_config',

B<Optional>. Specifies the cell (first-level key) in Main Config File from where to take the
data. Note that C<config_cell> must point to a hashref. B<Defaults to:> C<public_config>

=head3 C<config_keys>

    config_keys  => undef,
    config_keys  => [
        qw/foo bar baz/,
    ],

B<Optional>. Takes either C<undef> or an arrayref. Specifies the keys in the cell (i.e.
the second-level key inside the first-level key) in Main Config File from where to take the
data. When set to an arrayref, the elements of the arrayref represent the names of the keys.
When set to C<undef> all keys will be taken. Note that C<config_cell> must point to
a hashref. B<Defaults to:> C<undef>

=head3 C<noop>

    noop => 0,

B<Optional>. Pneumonic: B<No> B<Op>eration. Takes either true or false values. When set to
a true value, the plugin will not run. B<Defaults to:> C<0>

=head1 EXAMPLES

=head2 EXAMPLE 1

    Config File:
    plug_config_to_template => {}, # all defaults

    public_config => {
        name => 'test',
        value => 'plug_test',
    },


    Relevant dump of ZofCMS Template hashref:

    $VAR1 = {
        'd' => {
            'public_config' => {
                'value' => 'plug_test',
                'name' => 'test'
            }
        },
    };

=head2 EXAMPLE 2

    Config File:
    plug_config_to_template => {
        key     => undef,
        cell    => 't',
    },

    public_config => {
        name => 'test',
        value => 'plug_test',
    },


    Relevant dump of ZofCMS Template hashref:

    $VAR1 = {
        't' => {
            'value' => 'plug_test',
            'name' => 'test'
        }
    };

=head1 AUTHOR

Zoffix, C<< <'zoffix at cpan.org'> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-app-zofcms-plugin-configtotemplate at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-ZofCMS-Plugin-ConfigToTemplate>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc App::ZofCMS::Plugin::ConfigToTemplate

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-ZofCMS-Plugin-ConfigToTemplate>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/App-ZofCMS-Plugin-ConfigToTemplate>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/App-ZofCMS-Plugin-ConfigToTemplate>

=item * Search CPAN

L<http://search.cpan.org/dist/App-ZofCMS-Plugin-ConfigToTemplate/>

=back

=head1 COPYRIGHT & LICENSE

Copyright 2009 Zoffix, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


=cut
