Files
dojo/lib/Dojo/Support.pm

108 lines
2.3 KiB
Perl
Raw Permalink Normal View History

2018-07-13 19:06:08 -05:00
package Dojo::Support;
use strict;
use warnings;
use Exporter 'import';
2020-01-22 18:22:31 -06:00
our @EXPORT = qw/send_mail mtxt commify month_names month_num2txt log dmph merge_hash load_module get_names /;
2018-07-13 19:06:08 -05:00
use Mojo::Base 'Mojolicious';
use File::Basename;
use Mojo::File 'path';
use Path::Class;
use Encode qw{ decode_utf8 };
use Hash::Merge;
use Dojo::Conf;
2018-07-19 03:45:46 -05:00
use Mojo::Log;
2018-07-13 19:06:08 -05:00
2018-07-19 03:45:46 -05:00
sub log{
my $log = Mojo::Log->new;
2018-07-20 05:16:47 -05:00
$log->debug("============ ".shift." ===========");
2018-07-19 03:45:46 -05:00
}
2020-01-20 14:49:59 -06:00
2020-01-22 18:22:31 -06:00
sub _log{
my $log = Mojo::Log->new;
$log->debug("============ ".shift." ===========");
}
2018-07-13 19:06:08 -05:00
sub get_names{
my $dir = shift;
my @file_name;
opendir(DIR, $dir) or die "error opening dir: $dir \n$!";
while (my $file = readdir(DIR)) {
next unless ( $file =~m/^[\w\d]/);
next unless (-f "$dir/$file");
push(@file_name,$file);
}
closedir(DIR);
return sort @file_name;
};
sub merge_hash{
2018-07-19 03:45:46 -05:00
#my ($v,$w)=@_;
my $h={};
2018-07-13 19:06:08 -05:00
my $merger = Hash::Merge->new('LEFT_PRECEDENT');
2018-07-19 03:45:46 -05:00
foreach (@_){
$h= $merger->merge($h,$_);
2020-01-20 14:49:59 -06:00
}
return $h;
2018-07-13 19:06:08 -05:00
}
2020-01-20 14:49:59 -06:00
2020-01-15 23:42:58 -06:00
sub month_num2txt{
return ("enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre" =~ m/\w+/g)[shift (@_) -1];
}
2020-01-22 18:22:31 -06:00
sub month_names{
return qw /enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre/;
}
2020-01-20 14:49:59 -06:00
sub mtxt {
my ($di, $mi, $df, $mf)=@_; # dini,mini,dfin,mfin
my $txt;
if ($mi == $mf){
if ($di == $df){ $txt = "$di de ".month_num2txt($mi);}
else{ $txt = "Del $di al $df de ".month_num2txt($mi);}
}
else{
$txt = "Del $di de ".month_num2txt($mi)." al $df de ".month_num2txt($mf);
}
return $txt;
}
2020-01-15 23:42:58 -06:00
#put comas on price numbers
sub commify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
2018-07-13 19:06:08 -05:00
sub load_module{
2018-07-18 04:28:26 -05:00
my $dir=shift;
2018-07-13 19:06:08 -05:00
my $lpath= Dojo::Conf::MOD."/$dir";
2018-07-18 04:28:26 -05:00
my $h;
2018-07-13 19:06:08 -05:00
$h->{layout} = "default";
my $q;
my @s= get_names($lpath);
foreach (@s) {
push(@{$h->{$1}}, "/$dir/$_") if /.*\.(css|js)$/;
push(@{$q->{$1}}, "/$dir/$_") if /.*\.(q|json|txt|md)$/;
}
return ($h,$q);
};
sub dmph{
my $x = shift;
my $y=" ";
foreach (sort(keys %$x)) {
$y= "$y $_ = $x->{$_} \n";
}
return $y;
}
2020-01-22 18:22:31 -06:00
sub send_mail{ #msg header mail
system( 'echo "'.shift.'" | mutt -s '.shift.' '.shift.' &');
_log("mail sent");
return 0;
}
2018-07-13 19:06:08 -05:00
1;