Puzzle/Template.pm untested

filter: Statementstmt 0.0% 0 / 217 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 361 CC 29 SCAR 24.7
CC
29
Cov
0%
_tmplLang
42.0
_getTextFile
30.0
_split_date
20.0
CRAP 11.8
1 package Puzzle::Template;
2  
3 our $VERSION = '0.18';
4  
5 use base qw(Class::Container HTML::Template::Pro::Extension);
6  
7 use File::Spec;
8 use Params::Validate qw(:types);
9  
10 use Puzzle::DBIx::ClassConverter;
11  
12  
13 __PACKAGE__->valid_params(
14 dcc => { isa => 'Puzzle::DBIx::ClassConverter'} ,
15 );
16  
17 __PACKAGE__->contained_objects (
18 dcc => 'Puzzle::DBIx::ClassConverter',
19 );
20  
21 use HTML::Mason::MethodMaker(
22 read_only => [ qw(dcc) ],
23 );
24  
25 *print_template = \&print;
26  
27 # definition of localizated error string for unexistent template
28 my $err_tmpl_notfound_string = {
29 'it' => q|
30 <h2>Modello per il componente <b>%comp_name%</b>
31 non trovato.</h2>
32 Il modello mancante dovrebbe essere posto
33 nel percorso <br><b><pre>%tmpl_file_path%</pre></b>
34 <p>Contattate il webmaster
35 |,
36 'en' => q|
37 <h2>Unable to find template for <b>%comp_name%</b>
38 .</h2>
39 The missing template should be located in the path
40 <br><b><pre>%tmpl_file_path%</pre></b>
41 <p>Please contact webmaster
42 |,
43 'fr' => q|
44 <h2>Unable to find template for <b>%comp_name%</b>
45 .</h2>
46 The missing template should be located in the path
47 <br><b><pre>%tmpl_file_path%</pre></b>
48 <p>Please contact webmaster
49 <p>Please traslate it in french language
50 |,
51 };
52  
53 my %fields =
54 (
55 plugins => ['SLASH_VAR','HEAD_BODY'],
56 );
57  
58  
59 sub new {
60 my $class = shift;
61 my %opt = @_;
62 # first call the Class::Container::new
63 my $htmpl = $class->SUPER::new();
64 # then create an initial HTML::Template::Pro::Extension object
65 my $htmpl1 = new HTML::Template::Pro::Extension(
66 %fields, functions => {
67 date2human => \&_ext_date2human,
68 datetime2human => \&_ext_datetime2human,
69 isgid => \&_ext_isgid,
70 s => \&_ext_s
71 }
72 );
73 # then merge the two objects and bless them to this class
74 $htmpl = bless {%$htmpl,%$htmpl1}, $class;
75 $htmpl->file($opt{file}) if (exists($opt{file}));
76 $htmpl->cache($opt{cache}) if (exists($opt{cache}));
77 return $htmpl;
78 }
79  
80 sub cache {defined $_[1] ? $_[0]->{cache} = $_[1] : $_[0]->{cache};}
81  
82 sub print {
83 my $self = shift;
84 print $self->sprint(@_);
85 }
86  
87 sub sprint {
88 my $self = shift;
89 my $args = shift || {};
90 my $tmpl_file_path = shift || $self->container->_mason->current_comp->name;
91 $tmpl_file_path = $self->_convFileName($tmpl_file_path);
92 # merging $c_args and items
93 my $html = $self->html($args,$tmpl_file_path);
94 return $html if (defined $html);
95 }
96  
97  
98  
99 sub html {
100 # overraide defalt html method to support base_root_dir
101 # and return html error string if selected template doesn't exist.
102 my $self = shift;
103 my $args = shift;
104 my $file = shift;
105  
106 my $as = { %{$self->container->dbg->all_mason_args} ,%$args};
107  
108 # define lang bypass cache
109 #if (exists($as->{lang})) {
110 # $self->{default_language} = $as->{lang};
111 #}
112 if (defined $file) {
113 my $file = $self->_tmplFilePath($file);
114 # _tmplFilePath could change all_mason_args
115 $as = { %{$self->container->dbg->all_mason_args} ,%$args};
116 if (-e $file) {
117 $self->SUPER::tmplfile($self->_getTextFile($file));
118 return $self->SUPER::html($as,undef);
119 } else {
120 # template file don't exists...print error to client
121 $self->_throw_error_tmpl_notfound($file,$mason);
122 # undef to stop print above.
123 return undef;
124 }
125 } else {
126 return $self->SUPER::html($as);
127 }
128 }
129  
130 sub mhtml {
131 my $self = shift;
132 my $args = shift || {};
133 my $file = shift;
134  
135 my $as = { %{$self->container->dbg->all_mason_args} ,%$args};
136 return $self->html($as,$file);
137 }
138  
139 use Switch;
140  
141 use Data::Dumper;
142 use JSON::Any;
143 use XML::Simple;
144 use Text::CSV::Slurp;
145  
146 sub printct {
147 print $_[0]->sprintct($_[1], $_[2]);
148 }
149  
150 sub sprintct {
151 my $self = shift;
152 my $pl = shift;
153 # printa una struttura perl, di solito un hashref
154 # coerentemente con il tipo di content-type di uscita
155 # supportato per ora, text, html, xml, json
156 #
157  
158 my $ct = shift || $self->container->_mason->apache_req->content_type;
159  
160 my $pl2html = sub { return Data::Dumper::Dumper($_[0]) };
161 my $pl2text = sub { return Data::Dumper::Dumper($_[0]) };
162 my $pl2json = sub { my $obj = JSON::Any->new; return $obj->objToJson($_[0]) };
163 my $pl2xml = sub { return XMLout($_[0]) };
164 my $pl2csv = sub { return Text::CSV::Slurp->create(input => $_[0],sep_char => ';') };
165 my $pl2else = sub { return Data::Dumper::Dumper($_[0]) };
166  
167 my $fc;
168
169 switch ($ct) {
170 case /json/ { $fc = $pl2json}
171 case /xml/ { $fc = $pl2xml}
172 case 'text/plain' { $fc = $pl2text}
173 case 'text/html' { $fc = $pl2html}
174 case /excel/ { $fc = $pl2csv}
175 else { $fc = $pl2else}
176 }
177  
178 return &$fc($pl);
179 }
180  
181  
182 sub _getTextFile {
183 my $self = shift;
184 my $filename = shift;
185 my $ret;
186 $ret = $self->container->_mason->cache(namespace=>__PACKAGE__)->get($filename,busy_lock=>'30 sec')
187 if ($self->cache);
188 if (!defined($ret) || !$self->cache) {
189 confess(__PACKAGE__ . " : Cannot open included file $filename : $!")
190 unless defined(open(TEMPLATE, $filename));
191 while (read(TEMPLATE, $ret, 10240, length($ret))) {}
192 close(TEMPLATE);
193 $self->container->_mason->cache(namespace=>__PACKAGE__)->set($filename, $ret, '5h')
194 if ($self->cache);
195 }
196 return \$ret;
197 }
198  
199  
200 sub _tmplFilePath {
201 # convert the file path based on absolute/relative path
202 # and to base_dir and language
203 my $self = shift;
204 my $mason = $self->container->_mason;
205 my $comp_name = shift || $mason->current_comp->name;
206 my $abs_path;
207 # built absolute path
208 my $base_root = $mason->interp->comp_root;
209 my $tbp = $self->{template_base_path} eq 'undef' ? '' :
210 $self->{template_base_path};
211 if (File::Spec->file_name_is_absolute($comp_name)) {
212 $abs_path = File::Spec->catfile($base_root,$tbp,$comp_name);
213 } else {
214 my $comp_dir= $mason->current_comp->path;
215 (undef,$comp_dir,undef) = File::Spec->splitpath($comp_dir);
216 $abs_path = File::Spec->catfile($base_root,$tbp,$comp_dir,$comp_name);
217 }
218 return $self->_tmplLang($abs_path);
219 }
220  
221 use YAML qw(LoadFile);
222  
223 sub _tmplLang {
224 # try to see if exists file for language selected
225 my $self = shift;
226 my $abs_path = shift;
227 #my $lang = $self->{default_language};
228 my $lang = $self->container->lang_manager->lang;
229 my ($volume,$dirs,$file) = File::Spec->splitpath( $abs_path );
230 my ($fn,$ext) = split(/\./,$file);
231  
232 # check and load Yaml language file into page
233 my $yaml_path = &_existsPath($volume,$dirs,$fn . '.yaml');
234 if ($yaml_path) {
235 $self->container->args->set($self->yamlArgs($yaml_path,$lang));
236 }
237  
238 my $mobile = $self->_isMobile ? '.mobile' : '';
239  
240 my $rfile;
241 $rfile = &_existsPath($volume,$dirs,$fn.$mobile.'.'.$lang.'.'.$ext) unless ($yaml_path);
242 $rfile = &_existsPath($volume,$dirs,$fn.$mobile.'.'.$ext) unless ($rfile);
243 $rfile = &_existsPath($volume,$dirs,$fn.'.'.$ext) unless ($rfile);
244  
245 return $rfile;
246 }
247  
248 sub yamlArgs {
249 my $self = shift;
250 my $yaml_path = shift;
251 my $lang = shift;
252 my $ts = LoadFile($yaml_path);
253 &_recursiveStructRemoveLang(\$ts,$lang);
254 return $ts;
255 }
256  
257 sub _recursiveStructRemoveLang {
258 my $struct = shift;
259 my $lang = shift;
260  
261 foreach my $key (keys %$$struct) {
262 if (ref($$struct->{$key}) eq 'HASH') {
263 _recursiveStructRemoveLang(\($$struct->{$key}),$lang);
264 } elsif ($key eq $lang) {
265 $$struct = $$struct->{$lang};
266 return;
267 }
268 }
269 }
270  
271 sub _existsPath {
272 my ($volume,$dirs,$file) = @_;
273 my $path = File::Spec->canonpath(File::Spec->catpath($volume,$dirs,$file));
274 return -e $path ? $path : undef;
275 }
276  
277 use HTTP::BrowserDetect;
278  
279 sub _isMobile {
280 # detect if browser is mobile
281 my $self = shift;
282 my $ua_string = $ENV{'HTTP_USER_AGENT'};
283 my $bdetect = new HTTP::BrowserDetect($ua_string);
284 return $bdetect->mobile;
285 }
286  
287 sub _convFileName {
288 # convert component name in template file subst extention with ".htt"
289 my $self = shift;
290 my $abs_path = shift;
291 my ($volume,$dirs,$file) = File::Spec->splitpath( $abs_path );
292 my ($fn,$ext) = split(/\./,$file);
293 return $abs_path if ($ext !~ /^m(pl|htm|html)$/);
294 $file = "$fn.htt";
295 return File::Spec->canonpath(File::Spec->catpath($volume,$dirs,$file));
296 }
297  
298 sub _print_html() {
299 my $self = shift;
300 return "<HTML>\n<HEAD>\n</HEAD>\n<BODY>\n" . shift() . "\n</BODY>\n</HTML>";
301 }
302  
303 sub _throw_error_tmpl_notfound {
304 my $self = shift;
305 my $tmpl_file_path = shift;
306 my $mason = shift;
307 #my $comp_name = $mason->callers(0)->path;
308 my $comp_name = $mason->current_comp->path;
309 my $htmlerr = $self->_print_html($self->_err_tmpl_notfound);
310 $self->tmplfile(\$htmlerr);
311 print $self->html({ comp_name => $comp_name ,
312 tmpl_file_path => $tmpl_file_path} );
313 }
314  
315 sub _err_tmpl_notfound {
316 # return localized error string for unexistent template
317 # see err_tmpl_notfound_string hash in the header of this package
318 return exists($err_tmpl_notfound_string->{$self->{default_language}}) ?
319 $err_tmpl_notfound_string->{$self->{default_language}} :
320 $err_tmpl_notfound_string->{en};
321 }
322  
323 sub _ext_date2human {
324 my @split_date = _split_date(shift);
325 if ($#split_date == 0) {
326 # not a date
327 return $split_date[0]
328 } else {
329 # return %d-%m-%Y
330 return "$split_date[2]-$split_date[1]-$split_date[0]";
331 }
332 }
333  
334 sub _ext_datetime2human {
335 my @split_date = _split_date(shift);
336 if ($#split_date == 0) {
337 # not a date
338 return $split_date[0]
339 } else {
340 # return %d-%m-%Y
341 return "$split_date[2]-$split_date[1]-$split_date[0] $split_date[3]:$split_date[4]";
342 }
343 }
344  
345 sub _ext_isgid {
346 return Puzzle->instance->session->user->isGid(shift);
347 }
348  
349  
350 sub _ext_s {
351 return Puzzle->instance->lang->s(shift);
352 }
353  
354 sub _split_date {
355 my $mysql_date = shift;
356 my @dcomp = split('-',$mysql_date);
357 if ($#dcomp == 2) {
358 my @tcomp = split(' ',$dcomp[2]);
359 if ($#tcomp == 1) {
360 # have time
361 $dcomp[2] = $tcomp[0];
362 @tcomp = split(':',$tcomp[1]);
363 @ret = (@dcomp,@tcomp);
364 } else {
365 @ret = (@dcomp,'00','00','00');
366 }
367 } else {
368 # not a date
369 return ($mysql_date);
370 }
371 @ret = map(length($_) == 1 ? "0$_" : $_,@ret);
372 return @ret;
373 }
374  
375 sub combo_selected {
376 my $s = shift;
377 my $arraylist = shift;
378 my $keyname = shift;
379 my $vselected = shift;
380  
381 LOOP: foreach (@$arraylist) {
382 if ($_->{$keyname} eq $vselected) {
383 $_->{selected} = 'selected';
384 last LOOP;
385 }
386 }
387 }
388  
389 1;
390  
391 # vim: set ts=2: