App-ZofCMS

 view release on metacpan or  search on metacpan

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

        },
        grep exists $available_items{ $_ }, @saved_order_names;

    my %disabled_items =
        map +(  (/^\[:\[(.+)]:]$/)[0] => 1 ),
            grep /^\[:\[.+]:]$/, @saved_order_names;

    %disabled_items = ()
        unless $conf->{has_disabled};

    my %saved_items = map +( $_->{name} => 1 ), @saved_items;

    my @disabled_items;
    for ( sort keys %available_items ) {
        if ( exists $disabled_items{$_} ) {
            push @disabled_items, {
                name  => $_,
                value => $available_items{$_},
            };
        }
        else {
            push @saved_items, {
                name  => $_,
                value => $available_items{$_},
            } unless exists $saved_items{$_};
        }
    }

    return ( \@saved_items, \@disabled_items );
}

sub has_value {
    my $v = shift;

    return 1
        if defined $v
            and length $v;

    return 0;
}

sub dbh {
    my $self = shift;

    return $self->{DBH}
        if $self->{DBH};

    $self->{DBH} = DBI->connect_cached(
        @{ $self->{CONF} }{ qw/dsn user pass opt/ },
    );

    return $self->{DBH};
}

sub list_html_template {
    return <<'END_HTML';
<tmpl_if name='is_disabled'>
    <tmpl_if name='has_disabled_items'>
        <ul class="plug_list_html_template_disabled">
            <tmpl_loop name='items'>
                <li id="ppof_order_list_disabled_item_<tmpl_var escape='html'
                    name='name'>"><tmpl_var name='value'></li>
            </tmpl_loop>
        </ul>
    </tmpl_if>
<tmpl_else>
    <tmpl_if name='has_enabled_items'>
        <ul class="plug_list_html_template">
            <tmpl_loop name='items'>
                <li id="ppof_order_list_item_<tmpl_var escape='html'
                    name='name'>"><tmpl_var name='value'></li>
            </tmpl_loop>
        </ul>
    </tmpl_if>
</tmpl_if>
END_HTML
}

sub form_html_template {
    return <<'END_HTML';
<form action="" method="POST" id="plug_preferential_order_form">
<div>
    <input type="hidden" name="page" value="<tmpl_var escape='html' name='q_page'>">
    <input type="hidden" name="ppof_save_order" value="1">

    <tmpl_if name='is_saved'>
        <p class="success-message">Successfully saved</p>
    </tmpl_if>

    <div id="ppof_enabled_container">
        <tmpl_if name='has_disabled'>
            <tmpl_var name='enabled_label'>
        </tmpl_if>
        <ul id="ppof_order" class="ppof_list">
            <tmpl_loop name='items'>
                <li id="ppof_order_item_<tmpl_var escape='html' name='name'>"><tmpl_var name='form_name'></li>
            </tmpl_loop>
        </ul>
    </div>

    <tmpl_if name='has_disabled'>
        <div id="ppof_enabled_container">
            <tmpl_var name='disabled_label'>
            <ul id="ppof_order_disabled" class="ppof_list">
                <tmpl_loop name='items_disabled'>
                    <li id="ppof_order_item_<tmpl_var escape='html' name='name'>"><tmpl_var name='form_name'></li>
                </tmpl_loop>
            </ul>
        </div>
    </tmpl_if>

    <tmpl_var name='submit_button'>
</div>
</form>
END_HTML
}

1;
__END__

=encoding utf8

=head1 NAME

App::ZofCMS::Plugin::PreferentialOrder - Display HTML snippets in user-controllable, savable order

=head1 EXTRA RESOURCES (BEYOND PERL)

This plugin was designed to be used in conjunction with JavaScript (JS)
code that controls the order of items on the page and submits that
information to the server.

If you wish to use a different, your own front-end, please study JS code
provided at the end of this documentation to understand what is required.

=head1 SYNOPSIS

In your L<HTML::Template> template:

    <tmpl_var name='plug_pref_order_form'>
    <tmpl_var name='plug_pref_order_list'>
    <tmpl_var name='plug_pref_order_disabled_list'>

In your ZofCMS template:

    plugins => [ qw/PreferentialOrder/, ],

    # except for the mandatory argument `items`, the default values are shown
    plug_preferential_order => {
        items => [ # four value type variations shown here
            forum3  => '<a href="#">Forum3</a>',
            forum4  => [ 'Last forum ":)"',   \'forum-template.tmpl', ],
            forum   => [ 'First forum ":)"',  '<a href="#">Forum</a>',  ],
            forum2  => [
                'Second forum ":)"',
                sub {
                    my ( $t, $q, $config ) = @_;
                    return '$value_for_the_second_element_in_the_arrayref';
                },
            ],
        ],
        dsn            => "DBI:mysql:database=test;host=localhost",
        user           => '',
        pass           => undef,
        opt            => { RaiseError => 1, AutoCommit => 1 },
        users_table    => 'users',

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

    <!-- Double list (has_disabled is set to a true value) -->
    <form action="" method="POST" id="plug_preferential_order_form">
    <div>
        <input type="hidden" name="page" value="/index">
        <input type="hidden" name="ppof_save_order" value="1">

        <div id="ppof_enabled_container">
            <p class="ppof_label">Enabled items</p>
            <ul id="ppof_order" class="ppof_list">
                <li id="ppof_order_item_forum4">Last forum ":)"</li>
                <li id="ppof_order_item_forum">First forum ":)"</li>
            </ul>
        </div>

        <div id="ppof_enabled_container">
            <p class="ppof_label">Disabled items</p>
            <ul id="ppof_order_disabled" class="ppof_list">
                <li id="ppof_order_item_forum2">Second forum ":)"</li>
                <li id="ppof_order_item_forum3">forum3</li>
            </ul>
        </div>

        <input type="submit" class="input_submit" value="Save">
    </div>
    </form>

    <!-- Single list (has_disabled is set to a false value) -->
    <form action="" method="POST" id="plug_preferential_order_form">
    <div>
        <input type="hidden" name="page" value="/index">
        <input type="hidden" name="ppof_save_order" value="1">

        <div id="ppof_enabled_container">
            <ul id="ppof_order" class="ppof_list">
                <li id="ppof_order_item_forum4">Last forum ":)"</li>
                <li id="ppof_order_item_forum">First forum ":)"</li>
                <li id="ppof_order_item_forum2">Second forum ":)"</li>
                <li id="ppof_order_item_forum3">forum3</li>
            </ul>
        </div>

        <input type="submit" class="input_submit" value="Save">
    </div>
    </form>

This form shows the default arguments for C<enabled_label>,
C<disabled_label> and C<submit_button>. Note that C<id=""> attributes on
the list items are partially made out of the "keys" set in C<items>
argument. The value for C<page> hidden C<input> is derived by the
plugin automagically.

=head2 "Enabled" Sorted List

    <ul class="plug_list_html_template">
        <li id="ppof_order_list_item_forum4">Foo:</li>
        <li id="ppof_order_list_item_forum"><a href="#">Forum</a></li>
    </ul>

The end parts of C<id=""> attributes on the list items are derived from
the "keys" in C<items> arrayref. Note that HTML in the values are
not escaped.

=head2 "Disabled" Sorted List

    <ul class="plug_list_html_template_disabled">
        <li id="ppof_order_list_disabled_item_forum2">Bar</li>
        <li id="ppof_order_list_disabled_item_forum3">Meow</li>
    </ul>

The end parts of C<id=""> attributes on the list items are derived from
the "keys" in C<items> arrayref. HTML in the values (innards of
C<< <li> >>s) are not escaped.

=head1 REQUIRED MODULES

This plugins lives on these modules:

    App::ZofCMS::Plugin::Base => 0.0106,
    DBI                       => 1.607,
    HTML::Template            => 2.9,

=head1 REPOSITORY

Fork this module on GitHub:
L<https://github.com/zoffixznet/App-ZofCMS>

=head1 BUGS

To report bugs or request features, please use
L<https://github.com/zoffixznet/App-ZofCMS/issues>

If you can't access GitHub, you can email your request
to C<bug-App-ZofCMS at rt.cpan.org>

=head1 AUTHOR

Zoffix Znet <zoffix at cpan.org>
(L<http://zoffix.com/>, L<http://haslayout.net/>)

=head1 LICENSE

You can use and distribute this module under the same terms as Perl itself.
See the C<LICENSE> file included in this distribution for complete
details.

=cut



( run in 1.172 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )