The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

App::ZofCMS::Plugin::SplitPriceSelect - plugin for generating a <select> for "price range" out of arbitrary range of prices.

SYNOPSIS

In your Main Config File or ZofCMS Template:

    plugins => [ qw/SplitPriceSelect/ ],

    plug_split_price_select => {
        prices => [ 200, 300, 1000, 4000, 5000 ],
    },

In your HTML::Template file:

    <form...
        <label for="plug_split_price_select">Price range: </label>
        <tmpl_var name='plug_split_price_select'>
    .../form>

DESCRIPTION

The module is a plugin for App::ZofCMS that allows you to give several prices and plugin will create a <select> HTML element with its <option>s containing ranges of prices. The idea is that you'd specify how many options you would want to have and plugin will figure out how to split the prices to generate that many ranges.

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

MAIN CONFIG FILE AND ZofCMS TEMPLATE FIRST-LEVEL KEYS

plugins

    plugins => [ qw/SplitPriceSelect/ ],

You need to add the plugin in the list of plugins to execute.

plug_split_price_select

    plug_split_price_select => {
        prices      => [ qw/foo bar baz/ ],
        t_name      => 'plug_split_price_select',
        options     => 3,
        name        => 'plug_split_price_select',
        id          => 'plug_split_price_select',
        dollar_sign => 1,
    }

    plug_split_price_select => sub {
        my ( $t, $q, $config ) = @_;
        return {
            prices      => [ qw/foo bar baz/ ],
            t_name      => 'plug_split_price_select',
            options     => 3,
            name        => 'plug_split_price_select',
            id          => 'plug_split_price_select',
            dollar_sign => 1,
        };
    }

The plug_split_price_select first-level key takes a hashref or a subref as a value. If subref is specified, its return value will be assigned to plug_split_price_select as if it was already there. If sub returns an undef, then plugin will stop further processing. The @_ of the subref will contain (in that order): ZofCMS Tempalate hashref, query parameters hashref and App::ZofCMS::Config object. If a certain key in that hashref is specified in both, Main Config File and ZofCMS Template, then the value given in ZofCMS Template will take precedence. Plugin will not run if plug_split_price_select is not specified (or if prices key's arrayref is empty). Possible keys/value of plug_split_price_select hashref are as follows:

prices

    prices => [ qw/foo bar baz/ ],

Mandatory. Takes an arrayref as a value, plugin will not run if prices arrayref is empty or prices is set to undef. The arrayref's elements represent the prices for which you wish to generate ranges. All elements must be numeric.

options

    options => 3,

Optional. Takes a positive integer as a value. Specifies how many price ranges (i.e. <option>s) you want to have. Note: if there are not enough prices in the prices argument, expect to have ranges with the same price on both sides; with evel smaller dataset, expect to have less than options <option>s generated. Defaults to: 3

t_name

    t_name => 'plug_split_price_select',

Optional. Plugin will put generated <select> into {t} ZofCMS Template special key, the t_name parameter specifies the name of that key. Defaults to: plug_split_price_select

name

    name => 'plug_split_price_select',

Optional. Specifies the value of the name="" attribute on the generated <select> element. Defaults to: plug_split_price_select

id

    id => 'plug_split_price_select',

Optional. Specifies the value of the id="" attribute on the generated <select> element. Defaults to: plug_split_price_select

dollar_sign

    dollar_sign => 1,

Optional. Takes either true or false values. When set to a true value, the <option>s will contain a dollar sign in front of prices when displayed in the browser (the value=""s will still not contain the dollar sign, see PARSING QUERY section below). Defaults to: 1

PARSING QUERY

    plug_split_price_select=500-14000

Now, the price ranges are generated and you completed your gorgeous form... how to parse those ranges is the question. The value="" attribute of each of generated <option> element will contain the starting price in the range followed by a - (dash, rather minus sign) followed by the ending price in the range. Note: the price on each end of the range may be the same if there are not enough prices available. Thus you can do something along the lines of:

    my ( $start_price, $end_price ) = split /-/, $query->{plug_split_price_select};
    my @products_in_which_the_user_is_interested = grep {
        $_->{price} >= $start_price and $_->{price} <= $end_price
    } @all_of_the_products;

GENERATED HTML CODE

This is what the HTML code generated by the plugin looks like (providing all the optional arguments are left at their default values):

    <select id="plug_split_price_select" name="plug_split_price_select">
        <option value="200-1000">$200 - $1000</option>
        <option value="4000-6000">$4000 - $6000</option>
        <option value="7000-7000">$7000 - $7000</option>
    </select>

REPOSITORY

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

BUGS

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

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

AUTHOR

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

LICENSE

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