package Dojo::Support; use strict; use warnings; use Exporter 'import'; our @EXPORT = qw/send_mail mtxt commify month_names month_num2txt log dmph merge_hash load_module get_names /; 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; use Mojo::Log; sub log{ my $log = Mojo::Log->new; $log->debug("============ ".shift." ==========="); } sub _log{ my $log = Mojo::Log->new; $log->debug("============ ".shift." ==========="); } 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{ #my ($v,$w)=@_; my $h={}; my $merger = Hash::Merge->new('LEFT_PRECEDENT'); foreach (@_){ $h= $merger->merge($h,$_); } return $h; } sub month_num2txt{ return ("enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre" =~ m/\w+/g)[shift (@_) -1]; } sub month_names{ return qw /enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre/; } 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; } #put comas on price numbers sub commify { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; } sub load_module{ my $dir=shift; my $lpath= Dojo::Conf::MOD."/$dir"; my $h; $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; } sub send_mail{ #msg header mail system( 'echo "'.shift.'" | mutt -s '.shift.' '.shift.' &'); _log("mail sent"); return 0; } 1;