<?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    = 'utilizadores';

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['nome']) ) if (strlen($_REQUEST['nome'])>3) $sqlStmtTmp .= " AND " . $localTabela . ".nome LIKE '%" . safe($_REQUEST['nome']) . "%'"; 
    if ( isset($_REQUEST['email']) ) if (strlen($_REQUEST['email'])>2) $sqlStmtTmp .= " AND (" . $localTabela . ".email LIKE '%" . safe($_REQUEST['email']) . "%'  )";     
    if ( isset($_REQUEST['activo']) ) if (strlen($_REQUEST['activo'])>2) $sqlStmtTmp .= " AND " . $localTabela . ".estado = '" . ($_REQUEST['activo']) . "'";
}

$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 . "
                    WHERE (" . $localTabela . ".id > 0 " . $sqlStmtTmp . ")"; 
$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 id, " . $localTabela . ".nome AS nome, " . $localTabela . ".email AS email, " . $localTabela . ".tst AS tst, " . $localTabela . ".estado AS activo
            FROM " . $localTabela . "
            WHERE (" . $localTabela . ".id > 0  
            " . $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['id']; 
    if ( $rowResultDb['activo'] == 'NAO' ) $rowResultDb['activo'] = $bngNao;
    elseif ( $rowResultDb['activo'] == 'SIM' ) $rowResultDb['activo'] = $bngSim;
    $arrResposta->rows[$i]['cell']  = array($rowResultDb['email'],$rowResultDb['nome'],$rowResultDb['activo']);  
    $i++;
}
print json_encode($arrResposta); 
?>