<?php
/*********************************************
Desciption: Listagem de registos para grid
File: /bsk/artigos/registos/lista/index.php
Project: Basikus.Net
Developers: João Vieira
Start Date: 2009/05/21
*********************************************/

//Included main configuration file
require_once($_SERVER['DOCUMENT_ROOT'] . "/bsk/includes/configuracao.default.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/bsk/includes/funcoes.default.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/bsk/includes/classe.categorias.php");

$localTabela    = 'conteudos';

if (!isset($_GET['page'])) $_GET['page']        = '1';
if (!isset($_GET['rows'])) $_GET['rows']        = '10';
if (!isset($_GET['sidx'])) $_GET['sidx']        = '0';
if (!isset($_GET['sord'])) $_GET['sord']        = '';
if (!isset($_REQUEST['_search'])) $_REQUEST['_search'] = '';

$sqlStmtTmp     = ""; 
$searchOn       = safe($_REQUEST['_search']); 

if( $searchOn == 'true' ) {
    if ( isset($_REQUEST['eddata']) ) if (strlen($_REQUEST['eddata'])>1) $sqlStmtTmp .= " AND DATE_FORMAT(" . $localTabela . ".dataentrada, '%Y%m%d') LIKE DATE_FORMAT('" . safe($_REQUEST['eddata']) . "', '%Y%m%d')";
    if ( isset($_REQUEST['ednome']) ) if (strlen($_REQUEST['ednome'])>3) $sqlStmtTmp .= " AND " . $localTabela . ".titulo LIKE '%" . safe($_REQUEST['ednome']) . "%'";
    if ( isset($_REQUEST['edcategoria']) ) if (strlen($_REQUEST['edcategoria'])>2) $sqlStmtTmp .= " AND (categorias.nome LIKE '%" . safe($_REQUEST['edcategoria']) . "%'  )";     
    if ( isset($_REQUEST['edactivo']) ) if (strlen($_REQUEST['edactivo'])>2) $sqlStmtTmp .= " AND " . $localTabela . ".estado = '" . safe($_REQUEST['edactivo']) . "'";
    if ( isset($_REQUEST['eddestaque']) ) if (strlen($_REQUEST['eddestaque'])>2) $sqlStmtTmp .= " AND " . $localTabela . ".destaque = '" . safe($_REQUEST['eddestaque']) . "'";
}

if (isset($_GET['xuta'])) {
    $camRes = new BD_Categorias($connectDb);
    $camRes->CaminhoSubs((int)$_GET['xuta']);
    $strIds = implode(",", $camRes->resSubs);
    $sqlStmtTmp .= " AND (categorias.id IN (" . $strIds . ")  )";
}


$page = $_GET['page']; 
$limit = $_GET['rows']; 
$sidx = $_GET['sidx']; 
$sord = $_GET['sord']; 

if(!$sidx) $sidx =1; 

// calculate the number of rows for the query. We need this for paging the result 
$sqlStmt        = "SELECT COUNT(*) AS count 
                    FROM " . $localTabela . ",categorias
                    WHERE (" . $localTabela . ".categorias_id = categorias.id " . $sqlStmtTmp . ")"; 
#dbg($sqlStmt);
$resultDb       = $connectDb->pedido($sqlStmt);
$rowResultDb    = $resultDb->fetch(PDO::FETCH_ASSOC);
$count = $rowResultDb['count']; 

// calculate the total pages for the query 
if( $count > 0 ) { 
              $total_pages = ceil($count/$limit); 
} else { 
              $total_pages = 0; 
} 

if ($page > $total_pages) $page=$total_pages;

$start = $limit*$page - $limit;

if($start <0) $start = 0; 

$sqlStmt = "SELECT " . $localTabela . ".id AS edid, " . $localTabela . ".titulo AS ednome, " . $localTabela . ".tst AS edtst,
            DATE_FORMAT(" . $localTabela . ".dataentrada, '%Y-%m-%d') AS eddata, " . $localTabela . ".estado AS edactivo, " . $localTabela . ".destaque AS eddestaque, categorias.nome AS edcategoria
            FROM " . $localTabela . ",categorias
            WHERE (" . $localTabela . ".categorias_id = categorias.id  
            " . $sqlStmtTmp . ")
            ORDER BY " . $sidx . " " . $sord . " 
            LIMIT " . $start . " , " . $limit . ""; 

$resultDb = $connectDb->pedido($sqlStmt);

#em JSON
$arrResposta->page = $page; 
$arrResposta->total = $total_pages; 
$arrResposta->records = $count; 
$i=0; 
foreach ($resultDb as $rowResultDb) {
    $arrResposta->rows[$i]['id']    = $rowResultDb['edid']; 
    if ( $rowResultDb['edactivo'] == 'NAO' ) $rowResultDb['edactivo'] = $bngNao;
    elseif ( $rowResultDb['edactivo'] == 'SIM' ) $rowResultDb['edactivo'] = $bngSim;
    if ( $rowResultDb['eddestaque'] == 'NAO' ) $rowResultDb['eddestaque'] = $bngNao;
    elseif ( $rowResultDb['eddestaque'] == 'SIM' ) $rowResultDb['eddestaque'] = $bngSim;
    #$arrResposta->rows[$i]['cell']  = array($rowResultDb['edreferencia'],trd($rowResultDb['ednome'], $usrLng), $rowResultDb['edactivo'], $rowResultDb['eddestaque']);
    $arrResposta->rows[$i]['cell']  = array($rowResultDb['eddata'],trd(($rowResultDb['ednome']), $usrLng),trd(($rowResultDb['edcategoria']),$usrLng),$rowResultDb['edactivo'], $rowResultDb['eddestaque']);  
    $i++;
}
print json_encode($arrResposta); 
?>