This repository has been archived on 2025-06-22. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
perl-dam/Dam/Components/Controls/MultiCheck.pm

161 lines
4.9 KiB
Perl

=head1 NAME
Dam::Components::Controls::MultiCheck
=head1 SYNOPSIS
my ($check_1, $check_2, ...) = Component__Get(CONTROL_MULTICHECK, ['multicheck']);
=head1 DESCRIPTION
Control para seleccionar una o más opciones de una lista.
=head1 ARGUMENTS
(
TYPE => 'MultiCheck',
ID => 'multicheck' (default),
INFO => 1 (show info in header; default) or 0 (don't show),
LABEL => 'MultiCheck' (default),
LABEL_INFO => Same as LABEL (default),
REQUIRED => 1 (control required; default) or 0 (not required)
MULTIPLE => 1 (allows to select any number of options; default) or 0 (allows
to select only one option),
OPTIONS => { 'op1' => 'Option 1', 'op2' => 'Option 2' } (default); you can
use { ..., 'opN' => _DIVIDER_, ... } to include separators
between the options according to order,
DEFAULT => Default option(s), e.g. "'op1'" or "'op1','op3'"
)
=cut
use strict;
use warnings;
use utf8;
package Dam::Components::Controls::MultiCheck;
use Exporter qw(import);
our @EXPORT = qw(
Control__html Control__js Get
);
use Dam::Util;
use Dam::DamLogic;
my $ID_DEFAULT = 'multicheck';
sub __arguments {
my $arg_ref = shift;
$$arg_ref{LABEL} = 'MultiCheck' if is_empty($$arg_ref{LABEL});
$$arg_ref{LABEL_INFO} = $$arg_ref{LABEL} if is_empty($$arg_ref{LABEL_INFO});
check_arguments($arg_ref,
TYPE => [ ARG_REQUIRED ],
ID => [ ARG_DEFAULT, $ID_DEFAULT ],
INFO => [ ARG_DEFAULT, 1, 0 ],
LABEL => [ ARG_REQUIRED ],
LABEL_INFO => [ ARG_REQUIRED ],
REQUIRED => [ ARG_DEFAULT, 1, 0 ],
MULTIPLE => [ ARG_DEFAULT, 1, 0 ],
OPTIONS => [ ARG_DEFAULT, { 'op1' => strval(_t('Option'), ' 1'), 'op2' => strval(_t('Option'), ' 2') } ],
DEFAULT => [ ARG_OPTIONAL ]
);
}
sub Control__html {
my ($self, $arg_ref, $info_ref) = @_;
__arguments($arg_ref);
my $multicheck_value = !is_report() && !is_empty($$arg_ref{DEFAULT}) ? $$arg_ref{DEFAULT} : strval_join(',', cgiapp_multi($$arg_ref{ID}));
my $form_group = 'form-group';
my $list_opts = '';
my $info_opts = '';
my $count_opts = 0;
for my $key (sort keys(%{$$arg_ref{OPTIONS}})) {
if (is_eq(${$$arg_ref{OPTIONS}}{$key}, _DIVIDER_)) {
$list_opts .= '<option data-divider="true"></option>';
}
else {
my $checked = defined($multicheck_value) && index($multicheck_value, $key) >= 0;
my $selected = $checked ? '" selected="selected">' : '">';
$list_opts .= strval('<option value="\'', $key, '\'" title="', ${$$arg_ref{OPTIONS}}{$key}, $selected, ${$$arg_ref{OPTIONS}}{$key}, '</option>');
$form_group .= ' form-group-smaller' if $count_opts++ == 14;
$info_opts .= ' ' if $checked && !is_empty($info_opts);
$info_opts .= strval(${$$arg_ref{OPTIONS}}{$key}, ',') if $checked;
}
}
chop($info_opts) if !is_empty($info_opts);
push(@{$info_ref}, { DATA => $$arg_ref{LABEL_INFO}, VALUE => $info_opts }) if $$arg_ref{INFO} && length($info_opts);
# Required stylesheets:
Component__Header(ADD => 'CSS', RESOURCE => PACK_SELECT);
return strval('
<div class="', $form_group, ' form-group-', $ID_DEFAULT, ' form-selectpicker">
<label for="', $$arg_ref{ID}, $$arg_ref{REQUIRED} ? '" class="required">' : '">', $$arg_ref{LABEL}, '</label>
<select class="form-control input-lg selectpicker" id="', $$arg_ref{ID}, '" name="', $$arg_ref{ID}, '" multiple="multiple"', !$$arg_ref{MULTIPLE} ? ' data-max-options="1"' : '', ' data-selected-text-format="count > 2"', $$arg_ref{REQUIRED} ? ' required="required">' : '>', $list_opts, '</select>
</div>
');
}
sub Control__js {
# Required javascripts:
Component__Header(ADD => 'JS', RESOURCE => PACK_SELECT);
}
sub Get {
my ($self, $id) = @_;
$id = $ID_DEFAULT if is_empty($id);
return strval_join(',', cgiapp_multi($id));
}
1;
=head1 AUTHOR
Manuel Cillero C<< <manuel@cillero.es> >>
=head1 COPYRIGHT
The MIT License (MIT)
Copyright (c) 2004-2020 Manuel Cillero. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=cut