App-ZofCMS
view release on metacpan or search on metacpan
lib/App/ZofCMS/Plugin/SplitPriceSelect.pm view on Meta::CPAN
use HTML::Template;
sub _key { 'plug_split_price_select' }
sub _defaults {
return (
t_name => 'plug_split_price_select',
# prices => [ qw/foo bar baz/ ],
options => 3,
name => 'plug_split_price_select',
id => 'plug_split_price_select',
dollar_sign => 1,
);
}
sub _do {
my ( $self, $conf, $template, $query, $config ) = @_;
return
unless @{ $conf->{prices} || [] };
my @prices = sort { $a <=> $b } @{ $conf->{prices} };
@prices = _split_n( $conf->{options}, @prices );
my $t = HTML::Template->new_scalar_ref( \ _html_template() );
$t->param(
name => $conf->{name},
id => $conf->{id},
options => [
map +{
value => join('-', @{ $prices[ $_ ] }[0,-1]),
name => join ' - ',
map { $conf->{dollar_sign} ? "\$$_" : $_ }
@{ $prices[ $_ ] }[0,-1],
}, 0 .. $#prices,
],
);
$template->{t}{ $conf->{t_name} } = $t->output;
}
sub _split_n {
my $n = shift;
$n = @_ if @_ < $n;
my $l = 1 + $#_/$n;
my @opt = map [splice @_, 0, $l], 1 .. $n;
for ( reverse 0..$#opt ) {
unless( @{ $opt[$_] } ) {
push @{ $opt[$_] }, pop @{ $opt[ $_-1 ] }
}
}
return @opt
}
sub _html_template {
return <<'END_TEMPLATE';
<select id="<tmpl_var escape='html' name='id'>" name="<tmpl_var escape='html' name='name'>"><tmpl_loop name="options">
<option value="<tmpl_var escape='html' name='value'>"><tmpl_var escape='html' name='name'></option></tmpl_loop>
</select>
END_TEMPLATE
}
1;
__END__
=encoding utf8
=head1 NAME
App::ZofCMS::Plugin::SplitPriceSelect - plugin for generating a <select> for "price range" out of arbitrary range of prices.
=head1 SYNOPSIS
In your Main Config File or ZofCMS Template:
plugins => [ qw/SplitPriceSelect/ ],
plug_split_price_select => {
prices => [ 200, 300, 1000, 4000, 5000 ],
},
In your L<HTML::Template> file:
<form...
<label for="plug_split_price_select">Price range: </label>
<tmpl_var name='plug_split_price_select'>
.../form>
=head1 DESCRIPTION
The module is a plugin for L<App::ZofCMS> that allows you to give several prices and plugin
will create a C<< <select> >> HTML element with its C<< <option> >>s containing I<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 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 => [ qw/SplitPriceSelect/ ],
You need to add the plugin in the list of plugins to execute.
=head2 C<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,
}
( run in 1.062 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )