<?php
// vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker:

/**
 * Permite editar o agregar registros a una tabla
 * Referencias: http://www.21st.de/downloads/rapidprototyping.pdf
 *
 * PHP version 5
 *
 * @category  SIVeL
 * @package   SIVeL
 * @author    Vladimir Támara <vtamara@pasosdeJesus.org>
 * @copyright 2004 Dominio público. Sin garantías.
 * @license   http://creativecommons.org/licenses/publicdomain/ Dominio Público. Sin garantías.
 * @version   $Id: detalle.php,v 1.41 2009/05/03 12:23:21 vtamara Exp $
 * @link      http://sivel.sf.net
*/

/**
 * Permite editar o agregar registros a una tabla
 */
require_once "aut/aut.php";
require_once "aut/conf.php";
require_once "misc.php";
require_once "DB/DataObject/FormBuilder.php";

$aut_usuario = "";
autenticaUsuario($dsn, $accno, $aut_usuario, 11);

if (!isset($_REQUEST['tabla'])) {
    die('Por favor especificar parametro "tabla"');
}
$tabla = varEscapa($_REQUEST['tabla']);

$u = HTML_Menu_toma_url($GLOBALS['menu_tablas_basicas']);
if (!in_array($tabla, $u)) {
    die("La tabla '$tabla' no es básica");
}
$d = DB_DataObject::factory($tabla);
if (PEAR::isError($d)) {
    die($d->getMessage());
}
$db = $d->getDatabaseConnection();

if (isset($_GET['id'])) {
    $a = explode(',', varEscapa($_GET['id'], $db));
    foreach ($a as $v) {
        $r = explode('=', $v);
        $n = $r[0];
        $d->$n = $r[1];
    } 
    if ($d->find()!=1) {
        die("Se esperaba un sólo registro");
    }
    $d->fetch();
}
$d->fb_addFormHeader = true;
$d->useMutators = true;
$b =& DB_DataObject_FormBuilder::create($d);
$b->useMutators = true;
$b->createSubmit = 0;
$f = $b->getForm(htmlspecialchars($_SERVER['REQUEST_URI']));
$f->setRequiredNote($mreq);

$ed = array();
if (!isset($_GET['id'])) {
    $e =& $f->createElement('submit', 'añadir', 'Añadir');
    $ed[] =& $e;
} else {
    $e =& $f->createElement('submit', 'actualizar', 'Actualizar');
    $ed[] =& $e;
    $e =& $f->createElement('submit', 'eliminar', 'Eliminar');
    $ed[] =& $e;
    $f->addElement('hidden', 'tabla', $tabla);
}
$f->addGroup($ed, null, '', '&nbsp;', false);
$f->addElement('header', null, 
    '<div align = "right"><a href = "index.php">Menú Principal</a></div>');

if ($f->validate()) {
    if (!isset($f->_submitValues['evita_csrf']) ||
        $f->_submitValues['evita_csrf'] != $_SESSION['sin_csrf']) {
        die("Datos enviados no pasaron verificación CSRF (" .
            $_SESSION['sin_csrf'] . ", " . $f->_submitValues['evita_csrf'] . ")");
    }

    if (isset($f->_submitValues['actualizar']) || 
        isset($f->_submitValues['añadir']) ) {
            if (isset($f->_submitValues['actualizar'])) {
                $b->forceQueryType(DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEUPDATE);
            } else {
                $b->forceQueryType(DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEINSERT);
            }
        $res = $f->process(array($b, 'processForm'), false);
    } else {
        foreach ($f->_submitValues as $k => $v) {
            $d->$k = $v;
        }
        $res=& $d->delete();
    }
    if (PEAR::isError($d->_lastError)) {
        echo $d->_lastError->getMessage()."<br>";
        echo $d->_lastError->userinfo."<br>";
    } else {
        header('Location: tabla.php?tabla=' . $tabla);
    }
}

$_SESSION['sin_csrf'] = mt_rand(0, 1000);
$f->addElement('hidden', 'evita_csrf', $_SESSION['sin_csrf']);

echo $f->toHtml();

?>

