=head1 NAME
Dam::DamLogic
=head1 DESCRIPTION
API for session management and user access control, menu options and reports
layout.
=head1 FUNCTIONS
=cut
use strict;
use warnings;
use utf8;
package Dam::DamLogic;
use Exporter qw(import);
our @EXPORT = qw(
ALPHA
BETA
ACTION_DOWNLOAD
ACTION_PRINT
ACTION_SUBMIT
ACTION_SORT
CONTROL_CHECK
CONTROL_DATE
CONTROL_DATERANGE
CONTROL_INPUT
CONTROL_MONTH
CONTROL_MULTICHECK
CONTROL_OPTION
CONTROL_UPLOAD
CONTROL_YEAR
PACK_DATEPICKER
PACK_SELECT
PACK_TYPEAHEAD
Show__error_403
Show__error_500
Show__about
Show
is_report
is_download
Component__Header
Component__Get
Component__Set
Session__new
Session__param
Session__flush
Session__close
User__is_logged_in
User__access
User__has_access
cgiapp_param
cgiapp_cookie
cgiapp_upload
cgiapp_uploadInfo
cgiapp_header_add
cgiapp_header_props
_t
);
our @EXPORT_OK = qw(
package_config
tmpl_load
tmpl_core
);
use Cwd qw(getcwd);
use CGI::Session;
use Date::Calc qw(Now);
use Module::Load qw(load);
use Dam::Util;
use Dam::Debug;
use Dam::Var;
use constant {
ALPHA => 'alpha',
BETA => 'beta',
ACTION_DOWNLOAD => 'Dam::Components::Actions::Download',
ACTION_PRINT => 'Dam::Components::Actions::Print',
ACTION_SUBMIT => 'Dam::Components::Actions::Run',
ACTION_SORT => 'Dam::Components::Actions::Sort',
CONTROL_CHECK => 'Dam::Components::Controls::Check',
CONTROL_DATE => 'Dam::Components::Controls::Date',
CONTROL_DATERANGE => 'Dam::Components::Controls::DateRange',
CONTROL_INPUT => 'Dam::Components::Controls::Input',
CONTROL_MONTH => 'Dam::Components::Controls::Month',
CONTROL_MULTICHECK => 'Dam::Components::Controls::MultiCheck',
CONTROL_OPTION => 'Dam::Components::Controls::Option',
CONTROL_UPLOAD => 'Dam::Components::Controls::Upload',
CONTROL_YEAR => 'Dam::Components::Controls::Year',
PACK_DATEPICKER => '__DATEPICKER__',
PACK_SELECT => '__SELECT__',
PACK_TYPEAHEAD => '__TYPEAHEAD__',
DAM_TEMPLATES => strval(getcwd(), '/Dam/Components/Templates')
};
my $CURRENT_SESSION = undef;
my %EN_en = (
LANGUAGE_CODE => undef,
MONTHS => [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]
);
my $l10n = \%EN_en;
my @STYLESHEETS = ();
my @JAVASCRIPTS = ();
sub Show__error_403 {
cgiapp_header_add(
-status => '401 Forbidden'
);
return Show(
DISPLAY => 'PAGE',
TITLE => _t('Unauthorized Access'),
TEMPLATE => tmpl_core('Error403',
APP_NAME => CONFIG('APP_NAME'),
T_ATTENTION => _t('ATTENTION!'),
T_UNAUTHORIZED_ACCESS => _t('Unauthorized Access'),
T_REPORT_WITHOUT_ACCESS => _t('You are trying to run a report without sufficient access privileges.'),
T_CONTACT_ADMINISTRATOR => _t('Please contact the administrator to resolve it.')
)
);
}
sub Show__error_500 {
return Show(
DISPLAY => 'PAGE',
TITLE => _t('Unexpected Error'),
TEMPLATE => tmpl_core('Error500',
APP_NAME => CONFIG('APP_NAME'),
T_ATTENTION => _t('ATTENTION!'),
T_UNEXPECTED_ERROR => _t('Unexpected Error'),
T_ERROR_DURING_EXECUTION => _t('An unexpected error occurred during execution.'),
T_CONTACT_ADMINISTRATOR => _t('Please contact the administrator to resolve it.')
)
);
}
sub Show__about {
return Show(
DISPLAY => 'PAGE',
TITLE => _t('About <--app-->', app => CONFIG('APP_NAME')),
TEMPLATE => tmpl_core('About',
APP_NAME => CONFIG('APP_NAME'),
APP_SLOGAN => CONFIG('APP_SLOGAN'),
VERSION => CONFIG('VERSION'),
GLOBAL_WARNING => __global_messages('warning'),
T_VERSION_NEWS => _t('What\'s new'),
T_VERSION_PREV => _t('Previous version'),
CHANGELOG_LAST => __changelog(CONFIG('REF_LAST_CHANGELOG')),
CHANGELOG_PREV => __changelog(CONFIG('REF_PREV_CHANGELOG'))
)
);
}
=head2 Show($data_ref)
Returns the complete HTML code to display the report for the current package.
=head3 Arguments:
- B<$data_ref> (optional): A reference to the data structure that will be
passed to the template associated with the current package. If no reference
is indicated then only the report filter will be displayed (usually the
first time it is run).
=cut
sub Show {
my %arg = @_;
check_arguments(\%arg,
DISPLAY => [ ARG_DEFAULT, 'REPORT', 'PAGE' ],
TITLE => [ ARG_OPTIONAL ],
DATA_REF => [ ARG_OPTIONAL ],
TEMPLATE => [ ARG_OPTIONAL ]
);
my $title = $arg{TITLE};
my $content = '';
my $body_classes = '';
if (is_eq($arg{DISPLAY}, 'REPORT')) {
my $CURRENT_PACKAGE = RESERVED('REF_CURRENT_PACKAGE');
return Show__error_500() if !%$CURRENT_PACKAGE;
# Report title:
$title = strval($$CURRENT_PACKAGE{TITLE});
# Body classes:
$body_classes = strval('report', !is_empty($$CURRENT_PACKAGE{BODY_CLASSES}) ? strval(' ', $$CURRENT_PACKAGE{BODY_CLASSES}) : '');
# Filter:
$content = __tmpl_filter($title, $CURRENT_PACKAGE)->output;
# Report:
my $content_report = '';
if (defined($arg{DATA_REF})) {
my $tmpl_report = tmpl_load(strval($$CURRENT_PACKAGE{ID}, '/', $$CURRENT_PACKAGE{TEMPLATE}, '.tmpl.html'));
$tmpl_report->param($arg{DATA_REF});
$content_report = trim($tmpl_report->output);
$content_report = strval(
'
', "\n",
'', _t('NO DATA!'), ' ', _t('There is no data to apply the selection form filter.'), "\n",
'', _t('Check the filter conditions.'), '', "\n",
'
', "\n") if !is_empty($main_menu);
# User menu:
my %USER_MENU = %{CONFIG('REF_USER_MENU')};
my $submenu = __submenu(\%{$USER_MENU{OPTIONS}}, \@user_access);
if (!is_empty($submenu)) {
$main_menu = strval($main_menu,
'
', "\n", '') : ''
);
}
sub __tmpl_footer {
return tmpl_core('Footer',
DEBUG_MODE => CONFIG('DEBUG_MODE'),
FOOTER_COPYRIGHT => CONFIG('FOOTER_COPYRIGHT'),
TODAY => format_date_dmy(strval_join('-', get_today_ymd())),
CHECK_BROWSER => !User__is_logged_in(),
T_ATTENTION => _t('ATTENTION!'),
T_WARNING_MODE => _t('You are running <--app--> in <--mode-->.',
app => strval('', CONFIG('APP_NAME'), ''),
mode => strval('', CONFIG('DEBUG_MODE') == 1 ? _t('develop mode') : _t('testing mode'), '')
),
T_OLD_BROWSER => _t('This browser is out of date'),
T_UPDATE_BROWSER => _t('You must update to use <--app--> correctly.', app => strval('', CONFIG('APP_NAME'), '')),
T_UPDATE_NOW => _t('Update my browser now'),
T_CLOSE => _t('Close')
);
}
sub __global_messages {
my $type = shift;
open(my $fh, '<:encoding(UTF-8)', "error_$type.txt") or return '';
my @warnings = ();
while (my $row = <$fh>) {
chomp($row);
$row = trim($row);
push(@warnings, $row) if $row && !is_eq(substr($row, 0, 1), '#');
}
return '' if scalar(@warnings) == 0;
my $warnings = '';
my $title = is_eq($type, 'fatal') ? '
¡Atención!
' : '
Aviso
';
foreach my $warn (@warnings) { $warnings .= "
$warn
"; }
return "
$title
$warnings
";
}
sub __changelog {
my $CHANGELOG = shift;
my @user_access = User__access();
my @changelog_list = ();
my $version_block = '';
my $separe = FALSE;
foreach my $item (@$CHANGELOG) {
my ($version, $access, $log) = @$item;
my @access_to_this_new = split(',', $access) if defined($access);
if (is_eq($version, '-')) {
push(@changelog_list, { ITEM => '' }) if $separe;
$separe = FALSE;
}
elsif (match_arrays(\@user_access, \@access_to_this_new) || in_array('0', \@user_access)) {
push(@changelog_list, { ITEM => strval(!is_eq($version, $version_block) ? strval('
', $version, ' ') : '
', $log, '
') });
$version_block = $version;
$separe = TRUE;
}
}
return \@changelog_list;
}
=head2 __session_refresh()
Updates the reference to the user's current session.
=cut
sub __session_refresh {
if (!defined($CURRENT_SESSION)) {
my $sid = cgiapp_cookie('CGISESSID') || cgiapp_param('CGISESSID') || undef;
if (defined($sid)) {
$CURRENT_SESSION = new CGI::Session('driver:MySQL', $sid, { Handle => RESERVED('DBH') });
# $CURRENT_SESSION->flush();
if (defined($CURRENT_SESSION) && !is_eq($sid, $CURRENT_SESSION->id())) {
$CURRENT_SESSION->delete();
$CURRENT_SESSION = undef;
}
}
}
}
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