<?php
/**
 * Ce script définit la classe 'operation_defunt'.
 *
 * @package opencimetiere
 * @version SVN : $Id$
 */

require_once "../gen/obj/operation_defunt.class.php";

/**
 * Définition de la classe 'operation_defunt' (om_dbform).
 */
class operation_defunt extends operation_defunt_gen {

    var $idxformulaire;
    var $validation;
    var $maj;

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_defunt() {
        return "SELECT defunt, nom, prenom FROM ".DB_PREFIXE."defunt";
    }

    function setvalF($val = array()) {
        //
        parent::setvalF($val);
        //
        if($this->maj==0){
            unset($this->valF['defunt_titre']);
            unset($this->valF['defunt_nom']);
            unset($this->valF['defunt_marital']);
            unset($this->valF['defunt_prenom']);
            unset($this->valF['defunt_datenaissance']);
            unset($this->valF['defunt_datedeces']);
            unset($this->valF['defunt_lieudeces']);
        }

    }

    function verifier($val = array(), &$dnu1 = null, $dnu2 = null) {
        //
        parent::verifier($val);
        // On verifie si le champ n'est pas vide
        if ($this->valF['defunt'] == "") {
            $this->correct = false;
            $this->addToMessage(__("Le champ")." ".__("defunt")." ".__("est obligatoire"));
        }
    }

    function setType(&$form, $maj) {
        //
        parent::setType($form, $maj);
        //
        if ($maj < 2) { //ajouter et modifier
            if($maj==0){
                echo $maj;
                $form->setType('defunt', 'select');
                $form->setType('defunt_titre','hidden');
                $form->setType('defunt_nom','hidden');
                $form->setType('defunt_marital','hidden');
                $form->setType('defunt_prenom','hidden');
                $form->setType('defunt_datenaissance','hidden');
                $form->setType('defunt_datedeces','hidden');
                $form->setType('defunt_lieudeces','hidden');
                $form->setType('defunt_nature','hidden');
            }else{
                $form->setType('defunt', 'hidden');
            }
            $form->setType('operation', 'hiddenstatic');
        }
         if($maj==2){
             $form->setType('defunt', 'hiddenstatic');
         }
    }

    function setOnchange(&$form, $maj) {
        //
        parent::setOnchange($form, $maj);
        //
        $form->setOnchange("defunt_nom","this.value=this.value.toUpperCase();");
        $form->setOnchange("defunt_prenom","this.value=this.value.toUpperCase();");
        $form->setOnchange("defunt_lieudeces","this.value=this.value.toUpperCase();");
        $form->setOnchange("defunt_marital","this.value=this.value.toUpperCase();");
    }


    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {
        //
        parent::setSelect($form, $maj);
        //
        if($maj<2){
             // defunt
            $k=1;
            $contenu="";
            $contenu[0][0]="";
            $contenu[1][0]=__("choix_defunt");
            // construction de la requete suivant le type d operation
            $sql_defunt = $this->get_var_sql_forminc__sql("defunt");
            if($this->retourformulaire =='reduction' and $this->validation == '0'){     
                $temp = "select emplacement from ".DB_PREFIXE."operation where operation =".$this->idxformulaire;
                $emplacement = $this->f->db->getOne($temp);
                $sql_defunt.=" where emplacement =".$emplacement;
                $sql_defunt.= " and reduction = 'Non'";
                $sql_defunt.= " and exhumation = 'Non'";
                $sql_defunt.= " and verrou = 'Non'";
                $sql_defunt.= " and nature != 'urne'";
            }
            if($this->retourformulaire =='transfert' and $this->validation == '0'){
                $temp = "select emplacement from ".DB_PREFIXE."operation where operation =".$this->idxformulaire;
                $emplacement = $this->f->db->getOne($temp);
                $sql_defunt.=" where emplacement =".$emplacement;
                $sql_defunt.= " and exhumation = 'Non'";
                $sql_defunt.= " and verrou = 'Non'";            
            }
            $res = $this->f->db->query($sql_defunt);
            if (DB :: isError($res)){
                 die($res->getMessage().$sql_defunt);
            }else{              
                 //$k=1;
                 while ($row=& $res->fetchRow()){
                     $contenu[0][$k]=$row[0];
                     $contenu[1][$k]=$row[1]." ".$row[2];
                     $k++;
                 }
                 $form->setSelect("defunt",$contenu);
            }
        }  
    }

    function setValsousformulaire(&$form, $maj, $validation, $idxformulaire, $retourformulaire, $typeformulaire, &$dnu1 = null, $dnu2 = null) {
        //
        parent::setValsousformulaire($form, $maj, $validation, $idxformulaire, $retourformulaire, $typeformulaire);
        //
        if($validation==0 and isset($retourformulaire) and $retourformulaire != '') {
            $form->setVal('operation', $idxformulaire);
        }
        $this->validation=$validation;
        $this->idxformulaire=$idxformulaire;
        $this->retourformulaire=$retourformulaire;
        $this->maj=$maj;
    }

    function triggerajouter($id, &$dnu1 = null, $val = array(), $dnu2 = null) {
        // creation du defunt
        $sql="select * from ".DB_PREFIXE."defunt where defunt=".$this->valF['defunt'];
        $res= $this->f->db->query($sql);
        // a voir type de cercueil
        if($this->f->isDatabaseError($res, true)){
            return false;
        }
        while ($row=& $res->fetchRow(DB_FETCHMODE_ASSOC)){
            $this->valF['defunt_nom'] = $row['nom'];
            $this->valF['defunt_prenom'] = $row['prenom'];
            $this->valF['defunt_marital'] = $row['marital'];
            if($row['titre']!='')
                $this->valF['defunt_titre'] = $row['titre'];
            $this->valF['defunt_nature'] = $row['nature'];
            if($row['datenaissance']!='')
                $this->valF['defunt_datenaissance'] = $row['datenaissance'];
            if($row['datedeces'])
                $this->valF['defunt_datedeces'] = $row['datedeces'];
            $this->valF['defunt_lieudeces'] = $row['lieudeces'];
        }
        // controle de date : 5 ans inhumation
        // envoi d un message

        return true;
    }

    function triggerajouterapres($id, &$dnu1 = null, $val = array(), $dnu2 = null) {
        //verrou defunt
        $sql="update ".DB_PREFIXE."defunt set verrou ='Oui' where defunt=".$this->valF['defunt'];
        $res= $this->f->db->query($sql);
        if($this->f->isDatabaseError($res, true)){
            return false;
        }

        return true;
    }

    function triggersupprimer($id, &$dnu1 = null, $val = array(), $dnu2 = null) {
        //verrou defunt
        $sql="update ".DB_PREFIXE."defunt set verrou ='Non' where defunt=".$val['defunt'];
        $res= $this->f->db->query($sql);
        if($this->f->isDatabaseError($res, true)){
            $message = __("impossible de supprimer le verrou")."&nbsp;]";
            $this->addToLog($message, VERBOSE_MODE);
            return false;
        }
        $message = __("Enregistrement")."&nbsp;".$val['defunt']."&nbsp;";
        $message .= __("de la table")."&nbsp;\"defunt\"&nbsp;";
        $message .= "[&nbsp;".$this->f->db->affectedRows()."&nbsp;";
        $message .= __("enregistrement modifie")."&nbsp;]";
        $this->addToLog($message, VERBOSE_MODE);

        return true;
    }
}
