<?php

require './common.inc.php';
if (empty($tpl_id) && !empty($_REQUEST['aid'])) {
    $tpl_id = (int) $_REQUEST['aid'];
}
$blog_id = trim(preg_replace('(\\.(.*))', '', $_REQUEST['idblog']), "/\t\n \r\v");
# Comentarios, 1 hr
$pg = (int) $_REQUEST['pg'] ? (int) $_REQUEST['pg'] : 1;
$smarty->cache_lifetime = 3600;
$tpl = AB_TPL_DIR . 'comments.tpl';
$pattern = "blogs|{$blog_id}|{$tpl_id}|comentarios|{$pg}";
if (!$smarty->is_cached($tpl, $pattern)) {
    $comentario = new ab_commentTable();
    $comentario->order = 'sentdate DESC';
    $comentario->limit = defined('AB_COMMENT_PAGE') && AB_COMMENT_PAGE > 0 ? AB_COMMENT_PAGE : 10;
    $comentario->offset = ($pg - 1) * $comentario->limit;
    $comentario->filter = '';
    $_REQUEST['idab_entry'] = $tpl_id;
    $comentario->readEnv();
    if ($comentario->request['identrada']) {
        $w = $comentario->name . ".public IS TRUE AND " . $comentario->name . ".idab_entry = " . $comentario->request['idab_entry'];
        $rows = $comentario->readDataFilter($w);
        if (!empty($rows)) {
            $num_comentarios = $comentario->getVar("SELECT count(" . $comentario->key . ") FROM " . $comentario->name . " WHERE {$w}");
            $pgs = ceil($num_comentarios / $comentario->limit);
            $smarty->assign('pg', $pg);
            $smarty->assign('pgs', $pgs);
            $smarty->assign('prev_num', $num_comentarios - ($pg - 1) * $comentario->limit);
            $smarty->assign('comentarios', $rows);
            $smarty->assign('num_comentarios', $num_comentarios);
     # Paginas
     $ab_page = new ab_pageTable();
     $rows = $ab_page->readDataSQL("SELECT idab_page,ab_page FROM ab_page ORDER BY orden");
     $smarty->assign('ab_pages', $rows);
     unset($rows);
     # Entradas
     $entrada = new ab_entryTable();
     $entrada->readEnv();
     list($row) = $entrada->readDataFilter("idab_entry = " . $entrada->request['idab_entry'] . " AND public IS TRUE");
     if (count($row)) {
         $row['seo_title'] = generate_seo_link($row['ab_entry']);
         $row['body'] = replace_code($row['body']);
         $smarty->assign('entrada', $row);
         $smarty->assign('title', $row['ab_entry']);
     }
     $comentarioent = new ab_commentTable();
     $comentarioent->order = 'sentdate DESC';
     $comentarioent->filter = '';
     if ($row) {
         $rows = $comentarioent->readDataFilter("ab_comment.public IS TRUE AND ab_comment.idab_entry = " . $row['idab_entry']);
         $pgs = ceil($comentarioent->getVar("SELECT count(idab_comment) FROM ab_comment WHERE ab_comment.public IS TRUE AND ab_comment.idab_entry = " . $row['idab_entry']) / AB_COMMENT_PAGE);
         $smarty->assign('pg', 1);
         $smarty->assign('prev_num', 1);
         $smarty->assign('pgs', $pgs);
         $smarty->assign('comentarios', $rows);
     }
     unset($row);
     unset($comentarios);
 }
 $smarty->display($tpl, $pattern);
 # Comments