#!/usr/bin/perl -w use strict; use lib "../perl-libs/"; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use EXO::Com; use EXO::EsqCom; my $idp = int(param("idp")) || 0; my $post = ""; if($idp){ $post = $dbh->selectrow_hashref("SELECT title, subtitle, contents, img FROM posts WHERE id_post=?",{},$idp) || EXO::Com::http_redirect("index.pl"); print EXO::EsqCom::print(single(),{title=>$post->{title}, menu=>'posts', description => $post->{title} . " " . $post->{subtitle}, keywords => $post->{title} . " " . $post->{subtitle}}); }else{ print EXO::EsqCom::print(list(),{title=>"Noticias", menu=>'posts', description=>"", keywords => ""}); } EXO::Com::app_end(); sub single { my $HTML = ""; my $template = Template->new(); my $vars = { msg => msg_print(), post => $post, }; $template->process("tmpl/posts-single.html", $vars,\$HTML) or $HTML = $template->error(); return $HTML; } sub list { my $HTML = ""; my $template = Template->new(); my $adjacents = 2; my $limit = 6; my $page = param("page") || 0; my $script = "posts.pl"; my $totalrows = $dbh->selectrow_array("SELECT COUNT(*) FROM posts WHERE active=1",{}); my $extra = ""; my $current = ($page * $limit) - $limit; $current = 0 if($current < 0); my $posts = $dbh->selectall_arrayref("SELECT id_post, title, subtitle, contents, img FROM posts WHERE active=1 ORDER BY created_at DESC LIMIT $current,$limit",{Slice=>{}}); foreach my $p(@$posts){ $p->{contents} =~ s/<.+?>//g; $p->{contents} = substr($p->{contents}, 0, 150) . "[...] "; } my $vars = { msg => msg_print(), posts => $posts, pagination => paginate($adjacents,$limit,$page,$script,$totalrows,$extra), }; $template->process("tmpl/posts.html", $vars,\$HTML) or $HTML = $template->error(); return $HTML; } sub paginate { my $adjacents = shift || 2; my $limit = shift || 6; my $page = shift || 0; my $script = shift || ""; my $totalrows = shift || 0; my $extra = shift || ""; #ej. ¶m=val¶m2=val2 $page = 1 if($page == 0 or !$page); my $prev = $page - 1; my $next = $page + 1; my $lastpage = int(($totalrows/$limit)+0.99);#we need to ceil the number to the next maximum int to integrate all records my $lpm1 = $lastpage - 1;#ultima pagina menos 1 my $pagination = ""; my $counter = 0; if($lastpage > 1){ $pagination .= "