<?php
// vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker:
/**
 * Ficha para capturar casos (también utilizable para buscar).
 *
 * PHP version 5
 *
 * @category  SIVeL
 * @package   SIVeL
 * @author    Vladimir Támara <vtamara@pasosdeJesus.org>
 * @copyright 2005 Dominio público. Sin garantías.
 * @license   http://creativecommons.org/licenses/publicdomain/ Dominio Público. Sin garantías.
 * @version   $Id: captura_caso.php,v 1.57 2009/05/03 12:23:21 vtamara Exp $
 * @link      http://sivel.sf.net
 * @link http://www.21st.de/downloads/rapidprototyping.pdf
 */

/**
 * Ficha para capturar casos (también utilizada por consulta externa).
 */
include "aut/conf.php";
require_once "confv.php";

require_once 'HTML/QuickForm/Controller.php';
require_once 'HTML/QuickForm/Action/Direct.php';
require_once 'HTML/QuickForm/Action/Display.php';
require_once 'HTML/QuickForm/Action/Next.php';
require_once 'HTML/QuickForm/Action/Back.php';
require_once 'HTML/QuickForm/Action/Jump.php';
require_once 'HTML/QuickForm/header.php';
require_once 'HTML/QuickForm/date.php';
require_once 'HTML/QuickForm/text.php';
require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/Page.php';
require_once 'HTML/CSS.php';

require_once 'PagBasicos.php';
require_once 'PagLocalizacion.php';
require_once 'PagUbicacion.php';
require_once 'PagFuentesFrecuentes.php';
require_once 'PagOtrasFuentes.php';
require_once 'PagTipoViolencia.php';
require_once 'PagPResponsables.php';
require_once 'PagVictimaIndividual.php';
require_once 'PagVictimaColectiva.php';
require_once 'PagVictimaCombatiente.php';
require_once 'PagMemo.php';
require_once 'PagEvaluacion.php';
require_once 'PagPresentaRes.php';


/**
 * Presenta formulario.
 * Referencia: pear/lib/HTML/Progress/generator/default.php
 *
 * @category SIVeL
 * @package  SIVeL
 * @author   Vladimir Támara <vtamara@pasosdeJesus.org>
 * @license  http://creativecommons.org/licenses/publicdomain/ Dominio Público.
 * @link     http://sivel.sf.net/tec
*/
class PresentaFormulario extends HTML_QuickForm_Action_Display
{

    /**
     * Presenta formulario
     *
     * @param object &$pag Página
     *
     * @return void
     */
    function _renderForm(&$pag) 
    {
        $nomPag = $pag->getAttribute('name');
        $css = new HTML_CSS();
        $css->setStyle('body', 'background-color', '#FFFFFF');
        $css->setStyle('body', 'font-family', 'Roman');
        $css->setStyle('body', 'font-size', '10pt');
        $css->setStyle('h1', 'color', '#000FFC');
        $css->setStyle('h1', 'text-align', 'center');
        $css->setStyle('.maintable', 'width', '100%');
        $css->setStyle('.maintable', 'border-width', '0');
//        $css->setStyle('.maintable', 'border-style', 'thin dashed');
        $css->setStyle('.maintable', 'border-color', '#D0D0D0');
        $css->setStyle('.maintable', 'background-color', '#EEE');
//        $css->setStyle('.maintable', 'cellspacing', '2');
//        $css->setStyle('.maintable', 'cellpading', '3');
        $css->setStyle('th', 'text-align', 'center');
        $css->setStyle('th', 'color', '#FFC');
        $css->setStyle('th', 'background-color', '#AAA');
        $css->setStyle('th', 'white-space', 'nowrap');
        $css->setStyle('input', 'font-family', 'Roman');
        $css->setStyle('input.flat', 'font-size', '10pt');
//        $css->setStyle('input.flat', 'border-style', 'solid');
        $css->setStyle('input.flat', 'border-width', '2px 2px 0px 2px');
        $css->setStyle('input.flat', 'border-color', '#996');
        $enc = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "es">
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1">
<title>Ficha caso</title>
<style type = "text/css">
{%style%}
</style>
<script type = "text/javascript">
<!--
{%javascript%}
-->
</script>
<body>';
        $enc= str_replace('{%style%}', $css->toString(), $enc);
        $enc= str_replace('{%javascript%}', 'function envia(que){ ' .
            '    document.forms[0]._qf_default.value = que;' .
            '    document.forms[0].submit(); }', $enc);

        $renderer =& $pag->defaultRenderer();

        $renderer->setFormTemplate($enc . '<table class="maintable" ' .
            'align = "left">' .
            '<colgroup><col width = "150" style = "colprin1"/>' .
            '<col/></colgroup>' .
            '<form{attributes}>{content}</form></table>');
        $renderer->setHeaderTemplate('<tr><th colspan = "2">{header}</th></tr>');
        $renderer->setGroupTemplate('<table><tr>{content}</tr></table>', 'name');
        $renderer->setGroupElementTemplate('<td>{element}<br />' .
            '<span class="qfLabel">{label}</span></td>', 'name');
        $renderer->setElementTemplate("\n\t<tr>\n\t\t<td valign=\"top\" " .
            "align=\"left\" colspan=\"2\"><!-- BEGIN error -->" .
            "<span style=\"color: #ff0000\">{error}</span><br />" .
            "<!-- END error -->\t{element}</td>\n\t</tr>", 'tabs');
        $renderer->setElementTemplate("\n\t<tr>\n\t\t<td valign=\"top\" " .
            "align=\"center\" colspan=\"2\"><!-- BEGIN error -->" .
            "<span style=\"color: #ff0000\">{error}</span><br />" .
            "<!-- END error -->\t{element}</td>\n\t</tr>", 'buttons');
        $pag->accept($renderer);

        echo $renderer->toHtml();

        echo "</body></html>";
    }
}


/**
 * Controlador.
 * Con base en pear/lib/HTML/Progress/generator.php
 *
 * @category SIVeL
 * @package  SIVeL
 * @author   Vladimir Támara <vtamara@pasosdeJesus.org>
 * @license  http://creativecommons.org/licenses/publicdomain/ Dominio Público.
 * @link     http://sivel.sf.net/tec
*/
class CapturaCaso extends HTML_QuickForm_Controller 
{

    var $_botonAnterior  = '<< Anterior';
    var $_botonSiguiente= 'Siguiente >>';
    var $_botonElimina  = 'Elimina caso';
    var $_botonReporte  = 'Val. y Rep. Gen.';
    var $_botonMenu     = 'Menú';
    var $_botonCasoNuevo = 'Caso nuevo';
    var $_botonBusqueda = 'Buscar';
    var $_attrBoton     = array('style'=>'width:85px; padding:0; ');

    var $_tabs;
    var $opciones;

    /** 
     * Constructora
     *
     * @param array $opciones Opciones
     *
     * @return void
     */
    function CapturaCaso($opciones) 
    {

        $this->opciones = $opciones;
        $this->_modal = false;
        $this->_tabs = $GLOBALS['ficha_tabuladores'];
        if (isset($_SESSION['forma_modo']) &&   
            $_SESSION['forma_modo'] == 'busqueda') {
            $this->_tabs[12] = array('presentacion', 'PagPresentaRes', 
                'Forma Resultados');
        }

        if (!in_array(21, $this->opciones) && 
            (!isset($_SESSION['forma_modo']) || 
            $_SESSION['forma_modo'] != 'busqueda')) {
            die($GLOBALS['accno'] . " (3)");
        }
        $mreq = '<span style = "font-size:80%; color:#ff0000;">*</span>' .
            '<span style = "font-size:80%;"> marca un campo requerido</span>';
        foreach ($this->_tabs as $tab) {
            list($pag, $cl) = $tab;
            if ($pag == "presentacion") {
                $clpag =& new $cl($pag, $mreq, $opciones);
            } else {
                $clpag =& new $cl($pag, $mreq);
            }
            $this->addPage($clpag);
            $this->addAction($pag, new Salta());
        }

        $this->addAction('reporte', new ReporteGeneral());
        $this->addAction('menu', new Terminar());
        $this->addAction('elimina_caso', new EliminaCaso());
        $this->addAction('casonuevo', new CasoNuevo());
        $this->addAction('display', new PresentaFormulario());
        $this->addAction('next', new HTML_QuickForm_Action_Next());
        $this->addAction('back', new HTML_QuickForm_Action_Back());
        $this->addAction('jump', new HTML_QuickForm_Action_Jump());

        $this->addAction('process', new Terminar());
        
        $this->addAction('buscar', new BuscarId());

        // set ProgressBar default values on first run
        $sess = $this->container();
        $defaults = $sess['defaults'];

        if (count($sess['defaults']) == 0) {
            $this->setDefaults(array(
                'borderpainted' => false,
                'borderclass'   => 'progressBarBorder',
                'borderstyle'   => array('style' => 'solid', 
                    'width' => 0, 'color' => '#000000'),
                'cellid'        => 'progressCell%01s',
                'cellclass'     => 'cell',
                'cellvalue'     => array('min' => 0, 'max' => 100, 'inc' => 1),
                'cellsize'      => array('width' => 15, 'height' => 20, 
                    'spacing' => 2, 'count' => 10),
                'cellcolor'     => array('active' => '#006600', 
                    'inactive' => '#CCCCCC'),
                'cellfont'      => array('family' => 'Courier, Verdana', 
                    'size' => 8, 'color' => '#000000'),
                'stringpainted' => false,
                'stringid'      => 'installationProgress',
                'stringsize'    => array('width' => 50, 'height' => '', 
                    'bgcolor' => '#FFFFFF'),
                'stringvalign'  => 'right',
                'stringalign'   => 'right',
                'stringfont'    => array('family' => 
                    'Verdana, Arial, Helvetica, sans-serif', 
                    'size' => 10, 'color' => '#000000'),
                'phpcss'        => array('P'=>true)
            ));
        }
    }    


    /**  
     * Crea Tabuladores en Ficha.
     * Adaptada de Progress/generator.php 
     *
     * @param object &$page      Página
     * @param array  $attributes Atributos
     *
     * @return void
     */
    function creaTabuladores(&$page, $attributes = null)
    {
        $here = $attributes = HTML_Common::_parseAttributes($attributes);
        $here['disabled'] = 'disabled';
        $pageName = $page->getAttribute('name');
        $jump = array();

        foreach ($this->_tabs as $tab) {
            list($event, $cls) = $tab;
            $varc = get_class_vars($cls);
            $attrs = ($pageName == $event) ? $here : $attributes;
            $jump[] =& $page->createElement('submit', 
                $page->getButtonName($event), $varc['titulo'],
                HTML_Common::_getAttrString($attrs));
        }
        $page->addGroup($jump, 'tabs', '', '&nbsp;', false);
    }


    /**  
     * Agrega botones a una página.
     * Adaptada de Progress/generator.php 
     *
     * @param object &$page      HTML_QuicForm_Page página por cambiar
     * @param array  $buttons    Botones por agregar
     * @param array  $attributes Atributos
     *
     * @return void
     */
    function creaBotones(&$page, $buttons, $attributes = null)
    {
        $confirm = $attributes = HTML_Common::_parseAttributes($attributes);
        $confirm['onClick'] = "return(confirm('Are you sure ?'));";

        $prevnext = array();

        foreach ($buttons as $event => $label) {
            if ($event == 'cancel') {
                $type = 'submit';
                $attrs = $confirm;
            } elseif ($event == 'reset') {
                $type = 'reset';
                $attrs = $confirm;
            } else {
                $type = 'submit';
                $attrs = $attributes;
            }
            $prevnext[] =& $page->createElement($type, 
                $page->getButtonName($event), $label, 
                HTML_Common::_getAttrString($attrs));
        }
        $page->addGroup($prevnext, 'buttons', '', '&nbsp;', false);
    }


    /**
     * Crea botones de la parte inferior de la ficha.
     *
     * @param object &$page HTML_QuickForm_Page Página con ficha.
     *
     * @return void
     */
    function creaBotonesEstandar(&$page)
    {
        $page->addElement('header', null, '&nbsp; ');
        $botones = array('anterior'   => $this->_botonAnterior,
        'reporte'  => $this->_botonReporte,
        'busqueda'=> $this->_botonBusqueda,
        'menu'=> $this->_botonMenu,
        'elimina_caso' => $this->_botonElimina,
        'casonuevo'=> $this->_botonCasoNuevo,
        'siguiente'   => $this->_botonSiguiente
        );
        $this->creaBotones($page, $botones, 
            $this->_attrBoton);
        if (!isset($_SESSION['forma_modo'])
            || $_SESSION['forma_modo'] != 'busqueda') {
                $this->deshabilitaBotones($page, array('busqueda'));
        } else {
                $this->deshabilitaBotones($page, array('reporte'));
                $this->deshabilitaBotones($page, array('elimina_caso'));
                $this->deshabilitaBotones($page, array('casonuevo'));
        }
    }



    /**  
     * Habilita botones estándar de la página.
     * Adaptada de Progress/generator.php 
     *
     * @param object &$page  HTML_QuickForm_Page Página con ficha.
     * @param array  $events Eventos
     *
     * @return void
     */  
    function habilitaBotones(&$page, $events = array()) 
    {
        if (!is_a($page, 'HTML_QuickForm_Page')) {
            die("page no es HTML_QuickForm_page");
        } elseif (!is_array($events)) {
            die("events no es arreglo");
        }
        static $all;
        if (!isset($all)) {
            $all = array('back','next','cancel','reset','apply','help');
        }
        $buttons = (count($events) == 0) ? $all : $events;

        foreach ($buttons as $event) {
            $group    =& $page->getElement('buttons');
            $elements =& $group->getElements();
            foreach (array_keys($elements) as $key) {
                if ($group->getElementName($key) == $page->getButtonName($event)) {
                    $elements[$key]->updateAttributes(array('disabled'=>'false'));
                }
            }
        }
    }


    /**  
     * Deshabilita los botones estándar de una página.
     * Adaptada de Progress/generator.php 
     *
     * @param object &$page  HTML_QuickForm_Page Página con ficha.
     * @param array  $events Eventos
     *
     * @return void
     */  
    function deshabilitaBotones(&$page, $events = array()) 
    {
        if (!is_a($page, 'HTML_QuickForm_Page')) {
            die("page no es HTML_QuickForm_page");
        } elseif (!is_array($events)) {
            die("events no es arreglo");
        }
        static $all;
        if (!isset($all)) {
            $all = array('back','next','cancel','reset','apply','help');
        }
        $buttons = (count($events) == 0) ? $all : $events;

        foreach ($buttons as $event) {
            $group    =& $page->getElement('buttons');
            $elements =& $group->getElements();
            foreach (array_keys($elements) as $key) {
                if ($group->getElementName($key) == 
                    $page->getButtonName($event)) {
                    $elements[$key]->updateAttributes(array('disabled'=>'true')); 
                } 
            }
        }
    }


    /** 
     * Validaciones globales de un caso 
     *
     * @param integer $idcaso Identificación de caso por validar.
     *
     * @return bool Validado
     */
    function validaCaso($idcaso) 
    {
        $valr = true;
        $dcaso = DB_DataObject::factory('caso');
        if (PEAR::isError($dcaso)) {
            die($dcaso->getMessage());
        }
        $db =& $dcaso->getDatabaseConnection();
        // Completo: localización, fuentes, clasif., pr. resp,
        // victima excepto en ciertas bélicas, memo.
        $q = "SELECT COUNT(*) FROM clase_caso WHERE id_caso='" .
            $idcaso . "';";
        $nloc = (int)$db->getOne($q);
        $q = "SELECT COUNT(*) FROM municipio_caso WHERE id_caso='" .
            $idcaso . "';";
        $nloc+=(int)$db->getOne($q);
        $q = "SELECT COUNT(*) FROM departamento_caso " .
            "WHERE id_caso='" . $idcaso . "';";
        $nloc+=(int)$db->getOne($q);
        if ($nloc <= 0) {
            errorValida('Falta localización.<br>', array());
            $valr = false;
        }
        $q = "SELECT COUNT(*) FROM escrito_caso WHERE id_caso='"
            .$idcaso . "';";
        $nfue = (int)$db->getOne($q);
        $q = "SELECT COUNT(*) FROM fuente_directa_caso " .
            "WHERE id_caso='" . $idcaso . "';";
        $nfue+=(int)$db->getOne($q);
        if ($nfue <= 0) {
            errorValida('Falta fuente.<br>', array());
            $valr = false;
        }
        $q = "SELECT COUNT(*) FROM categoria_caso " .
            "WHERE id_caso='" . $idcaso . "';";
        $ncat = (int)$db->getOne($q);
        if ($ncat <= 0) {
            errorValida('Falta tipo de violencia.<br>', array());
            $valr = false;
        }
        $q = "SELECT COUNT(*) FROM presuntos_responsables_caso " .
            "WHERE id_caso='" . $idcaso . "';";
        $npresp = (int)$db->getOne($q);
        if ($npresp <= 0) {
            errorValida('Falta presunto responsable.<br>', array());
            $valr = false;
        }
        $q = "SELECT COUNT(*) FROM victima " .
            "WHERE id_caso='" . $idcaso . "';";
        $nvic = (int)$db->getOne($q);
        $q = "SELECT COUNT(*) FROM victima_colectiva_caso " .
            "WHERE id_caso='" . $idcaso . "';";
        $nvic+=(int)$db->getOne($q);
        $q = "SELECT COUNT(*) FROM combatiente " .
            "WHERE id_caso='" . $idcaso . "';";
        $nvic+=(int)$db->getOne($q);
        if ($nvic <= 0) {
            errorValida('Falta victima.<br>', array());
            $valr = false;
        }
        $dcaso->get($idcaso);
        if (trim($dcaso->memo)=='') {
            errorValida('Falta memo.<br>', array());
            $valr = false;
        }
        if (isset($GLOBALS['ISPELL']) && $GLOBALS['ISPELL'] != '') {
            $cmd = "";
            if (isset($GLOBALS['DICCIONARIO'])) {
                // http://mintaka.sdsu.edu/GF/bibliog/ispell/multi.html
                $cmd = "echo \"".escapeshellcmd($dcaso->memo)."\" | " .
                    $GLOBALS['ISPELL'] . " -d spanish -Tlatin1 -p" .
                    $GLOBALS['DICCIONARIO'] . " -l";
            }
            $r=`$cmd`;
            if ($r != "") {
                errorValida("Errores ortográficos en memo: $r<br>" .
                    str_replace('%l', $GLOBALS['CHROOTDIR'] . getcwd() . "/" . 
                    $GLOBALS['DICCIONARIO'], $GLOBALS['MENS_ORTOGRAFIA']),
                    array());
            }
        }
        if (!$valr) {
            echo "<hr>";
        }
        return $valr;
    }
}

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

if (isset($_GET['limpia']) && $_GET['limpia'] == 1) {
    unset_var_session();                
}

//ambiente();
//die("abc");

$opciones = array();

$nv = "_auth_".nomSesion();
if (isset($_SESSION[$nv]['username']) || $opciones == array()) {
    $d = DB_DataObject::factory('caso');
    if (PEAR::isError($d)) {
        die($d->getMessage());
    }
    $db =& $d->getDatabaseConnection();
    $rol = "";
    sacaOpciones($_SESSION[$nv]['username'], $db, $opciones, $rol);
}

$captura=& new CapturaCaso($opciones);

if (isset($_REQUEST['modo'])) {
    $_SESSION['forma_modo'] = 'consulta';
}

$GLOBALS['ya_captura_caso'] =& $captura;

$captura->run();
?>
