=head1 NAME Dam::Components::Controls::DateRange =head1 SYNOPSIS my ($ini, $end) = Component__Get(CONTROL_DATERANGE, ['range']); =head1 DESCRIPTION Control para obtener un rango de fechas. =head1 ARGUMENTS ( TYPE => 'DateRange', ID => 'range' (default), INFO => 1 (show info in header; default) or 0 (don't show), LABEL => 'Date range' (default), LABEL_INFO => Same as LABEL (default), REQUIRED => 1 (control required; default) or 0 (not required) MAXDAYS => 1095 (default) or maximum number of days for range ) =cut use strict; use warnings; use utf8; package Dam::Components::Controls::DateRange; use Exporter qw(import); our @EXPORT = qw( Control__html Control__js Get ); use Date::Calc qw(Today Add_Delta_YM Days_in_Month); use Dam::Util; use Dam::DamLogic; use Dam::Var; my $ID_DEFAULT = 'range'; sub __arguments { my $arg_ref = shift; $$arg_ref{LABEL} = _t('Date range') 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 ], MAXDAYS => [ ARG_DEFAULT, 1095 ] ); } sub Control__html { my ($self, $arg_ref, $info_ref) = @_; __arguments($arg_ref); my $ini_value = cgiapp_param($$arg_ref{ID}); my $end_value = cgiapp_param(strval($$arg_ref{ID}, '_end')); if (!is_report()) { my @previous = Add_Delta_YM(Today(), 0, -1); $ini_value = strval('01/', sprintf("%02d", $previous[1]), '/', $previous[0]); $end_value = strval(Days_in_Month($previous[0], $previous[1]), '/', sprintf("%02d", $previous[1]), '/', $previous[0]); } my @ini = split(/\//, $ini_value); my @end = split(/\//, $end_value); my $range = strval($ini_value, ' al ', $end_value); if (($ini[0] == 1) && ($ini[2] == $end[2])) { if (($end[0] == Days_in_Month($ini[2],$ini[1])) && ($ini[1] == $end[1])) { $range = strval($range, ' (', uc(_t('MONTHS', $ini[1])), ' ', $ini[2], ')'); } elsif (($ini[1] == 1) && ($end[0] == 31) && ($end[1] == 12)) { $range = strval($range, ' (', _t('YEAR'), ' ', $ini[2], ')'); } elsif (($ini[1] == 1) && ($end[0] == 30) && ($end[1] == 6)) { $range = strval($range, ' (', _t('FIRST SEMESTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 7) && ($end[0] == 31) && ($end[1] == 12)) { $range = strval($range, ' (', _t('SECOND SEMESTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 1) && ($end[0] == 30) && ($end[1] == 4)) { $range = strval($range, ' (', _t('FIRST QUARTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 5) && ($end[0] == 31) && ($end[1] == 8)) { $range = strval($range, ' (', _t('SECOND QUARTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 9) && ($end[0] == 31) && ($end[1] == 12)) { $range = strval($range, ' (', _t('THIRD QUARTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 1) && ($end[0] == 31) && ($end[1] == 3)) { $range = strval($range, ' (', _t('FIRST TRIMESTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 4) && ($end[0] == 30) && ($end[1] == 6)) { $range = strval($range, ' (', _t('SECOND TRIMESTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 7) && ($end[0] == 30) && ($end[1] == 9)) { $range = strval($range, ' (', _t('THIRD TRIMESTER'), ' ', $ini[2], ')'); } elsif (($ini[1] == 10) && ($end[0] == 31) && ($end[1] == 12)) { $range = strval($range, ' (', _t('FOURTH TRIMESTER'), ' ', $ini[2], ')'); } } push(@{$info_ref}, { DATA => $$arg_ref{LABEL_INFO}, VALUE => $range }) if $$arg_ref{INFO}; # Required stylesheets: Component__Header(ADD => 'CSS', RESOURCE => PACK_DATEPICKER); return strval('
al
'); } sub Control__js { my ($self, $arg_ref) = @_; __arguments($arg_ref); # Required javascripts: Component__Header(ADD => 'JS', RESOURCE => PACK_DATEPICKER); return strval(' $(function(){ $(".input-group.input-daterange").datepicker({ language: "', _t('LANGUAGE_CODE'), '", autoclose: true, todayHighlight: true, disableTouchKeyboard: true, // endDate: "0d", startDate: "01/01/1900" // https://github.com/uxsolutions/bootstrap-datepicker/issues/721#issuecomment-86275874 (workaround) }); $("#filter").on("submit", function(e) { if ($("#filter").valid()) { var range = ', $$arg_ref{MAXDAYS}, '; if (Math.round(($("#', $$arg_ref{ID}, '_end").datepicker("getDate") - $("#', $$arg_ref{ID}, '").datepicker("getDate")) / (1000 * 60 * 60 * 24)) > range) { $("#nv").val(1); $("#filter-message").text("', _t('Date ranges greater than <--max--> are not allowed.', max => $$arg_ref{MAXDAYS} % 365 ? strval($$arg_ref{MAXDAYS}, ' ', _t('day(s)')) : strval($$arg_ref{MAXDAYS} / 365, ' ', _t('year(s)'))), '"); $("#filter-error").modal(); e.preventDefault(); return false; } } }); }); '); } sub Get { my ($self, $id) = @_; $id = $ID_DEFAULT if is_empty($id); my @ini_value = split('/', cgiapp_param($id)); my $ini_value = "$ini_value[2]-$ini_value[1]-$ini_value[0]"; my @end_value = split('/', cgiapp_param(strval($id, '_end'))); my $end_value = "$end_value[2]-$end_value[1]-$end_value[0]"; return ($ini_value, $end_value); } 1; =head1 AUTHOR Manuel Cillero C<< >> =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