<?php
/**************************************************************
Desciption: Backoffice ajax jqgrid editar lista de categorias
File: /bsk/categorias/registos/editar/index.php
Project: Basikus
Developers: João Vieira - 7lis.com
Start Date: 2009/09/02
**************************************************************/

//configuração principal
require_once($_SERVER['DOCUMENT_ROOT'] . "/bsk/includes/configuracao.default.php");
//funções de tratamento de textos
include($_SERVER['DOCUMENT_ROOT'] . '/bsk/includes/funcoes.default.php'); 


$localTabela    = 'categorias';

if ( !isset($_SERVER['QUERY_STRING']) ) exit();
elseif ( $_SERVER['QUERY_STRING'] != md5($pwdWord . Date('Ymd')) ) exit();

if ( !isset($_POST['id']) ) exit();   
else $_POST['id'] = (int)$_POST['id'];

$sqlStmtUp = "";

if (isset($_POST['activo'])) {
    if ( $_POST['activo'] != '0' )  $sqlStmtUp .= " activo = '" . safe($_POST['activo']) . "' ,";
    else exit();
}

$sqlStmtUp = substr($sqlStmtUp, 0, strlen($sqlStmtUp)-1);

if ( isset($_POST['oper']) ) {
    if ( $_POST['oper'] == 'addX' ) {
        $sqlStmt = "INSERT INTO " . $localTabela . " (nome) VALUES('" . safe($_POST['nome']) . "')";
    }elseif ( $_POST['oper'] == 'del' ){
        $serUsado = False;
        $sqlStmt = "SELECT conteudos_id FROM conteudoscategorias
                    WHERE " . $localTabela . "_id = '" . $_POST['id'] . "'";
        $resultDb = $connectDb->pedido($sqlStmt);
        if ( $resultDb->rowCount() > 0 ) {
            $rowResultDb = $resultDb->fetch(PDO::FETCH_ASSOC);
            $serUsado = True;
        } 
        if ( !$serUsado ) {
            $sqlStmt = "DELETE FROM " . $localTabela . " WHERE id = '" . $_POST['id'] . "'";
        }else $sqlStmt = "";
    
    }
}else {
    $sqlStmt = "UPDATE " . $localTabela . " SET " . $sqlStmtUp . " WHERE id = '" . $_POST['id'] . "'";
}
#dbg($sqlStmt, true);
$connectDb->pedido($sqlStmt);

?>