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

require_once "../obj/operation.class.php";

/**
 * Définition de la classe 'inhumation' (om_dbform).
 *
 * Surcharge de la classe 'operation'.
 */
class inhumation extends operation {

    /**
     * @var string
     */
    protected $_absolute_class_name = "inhumation";

    /**
     * @var string
     */
    var $categorie = "inhumation";

    /**
     *
     */
    function setType(&$form, $maj) {
        parent::setType($form, $maj);
        //
        if ($maj < 2) { //ajouter et modifier
            $form->setType('defunt_titre','select');
            $form->setType('defunt_nom','text');
            $form->setType('defunt_prenom','text');
            $form->setType('defunt_marital','text');
            $form->setType('defunt_datenaissance','date');
            $form->setType('defunt_datedeces','date');
            $form->setType('defunt_lieudeces','text');
            $form->setType('defunt_nature','select');
        }
        if ($maj == 3) {
            $form->setType('defunt_titre', 'selectstatic');
            $form->setType('defunt_nom', 'hiddenstatic');
            $form->setType('defunt_marital', 'hiddenstatic');
            $form->setType('defunt_prenom', 'hiddenstatic');
            $form->setType('defunt_datenaissance', 'hiddenstaticdate');
            $form->setType('defunt_datedeces', 'hiddenstaticdate');
            $form->setType('defunt_lieudeces', 'hiddenstatic');
            $form->setType('defunt_nature', 'hiddenstatic');
        }
    }

    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {
        //
        parent::setSelect($form, $maj);
        //
        $this->init_select(
            $form,
            $this->f->db,
            $maj,
            null,
            "defunt_titre",
            $this->get_var_sql_forminc__sql("defunt_titre"),
            $this->get_var_sql_forminc__sql("defunt_titre_by_id"),
            true
        );
        //
        if($maj<2){
            //nature
            $contenu=array();
            $contenu[0]=array('cercueil','zinc','urne');
            $contenu[1]=array(__('cercueil'),__("cercueil_zinc"),__("urne"));
            $form->setSelect("defunt_nature",$contenu);
        }
    }

    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();");
        $form->setOnchange('defunt_datenaissance','fdate(this)');
        $form->setOnchange('defunt_datedeces','fdate(this)');
    }

    /**
     * SETTER_FORM - setLib.
     *
     * @return void
     */
    function setLib(&$form, $maj) {
        parent::setLib($form, $maj);
        //
        $form->setLib('defunt_titre',"");
        $form->setLib('defunt_nom',__("nom de naissance"));
        $form->setLib('defunt_marital',__("nom d'usage"));
        $form->setLib('defunt_prenom',__("prenom"));
        $form->setLib('defunt_datenaissance',__("ne(e) le"));
        $form->setLib('defunt_datedeces',__("decede le"));
        $form->setLib('defunt_lieudeces',__("a"));
        $form->setLib('defunt_nature',"");
    }

    function triggerajouterapres($id, &$dnu1 = null, $val = array(), $dnu2 = null) {
        // creation du defunt
        $valF["operation_defunt"]=$this->f->db->nextId(DB_PREFIXE."operation_defunt");
        $valF['operation'] = $this->valF["operation"];
        $valF['defunt_titre'] = $val['defunt_titre'];
        if ($valF['defunt_titre'] == "") {
            $valF['defunt_titre'] = null;
        }
        $valF['defunt_nom'] = $val['defunt_nom'];
        $valF['defunt_marital'] = $val['defunt_marital'];
        $valF['defunt_prenom'] = $val['defunt_prenom'];
        //
        if ($val['defunt_datenaissance'] != "") {
            $valF['defunt_datenaissance'] = $this->dateDB($val['defunt_datenaissance']);
        } else {
            $valF['defunt_datenaissance'] = NULL;
        }
        //
        if ($val['defunt_datedeces'] != "") {
            $valF['defunt_datedeces'] = $this->dateDB($val['defunt_datedeces']);
        } else {
            $valF['defunt_datedeces'] = NULL;
        }
        $valF['defunt_lieudeces'] = $val['defunt_lieudeces'];
        $valF['defunt_nature'] = $val['defunt_nature'];
        $res= $this->f->db->autoExecute(DB_PREFIXE."operation_defunt",$valF,DB_AUTOQUERY_INSERT);
        if($this->f->isDatabaseError($res, true)){
            return false;
        }
        $this->addToMessage(__("Enregistrement ").
                   $valF["operation_defunt"]." de la table operation_defunt [".$this->f->db->affectedRows().
            " ".__("ajoute")."]") ;

        return true;
    }

    function triggermodifierapres($id, &$dnu1 = null, $val = array(), $dnu2 = null) {
        // modification du defunt
        $valF['defunt_titre'] = $val['defunt_titre'];
        if ($valF['defunt_titre'] == "") {
            $valF['defunt_titre'] = null;
        }
        $valF['defunt_nom'] = $val['defunt_nom'];
        $valF['defunt_marital'] = $val['defunt_marital'];
        $valF['defunt_prenom'] = $val['defunt_prenom'];
        //
        if ($val['defunt_datenaissance'] != "") {
            $valF['defunt_datenaissance'] = $this->dateDB($val['defunt_datenaissance']);
        } else {
            $valF['defunt_datenaissance'] = NULL;
        }
        //
        if ($val['defunt_datedeces'] != "") {
            $valF['defunt_datedeces'] = $this->dateDB($val['defunt_datedeces']);
        } else {
            $valF['defunt_datedeces'] = NULL;
        }
        $valF['defunt_lieudeces'] = $val['defunt_lieudeces'];
        $valF['defunt_nature'] = $val['defunt_nature'];
        // a voir type de cercueil
        $cle= "operation =".$this->valF["operation"];
        $res= $this->f->db->autoExecute(DB_PREFIXE."operation_defunt",$valF,DB_AUTOQUERY_UPDATE,$cle);
        if($this->f->isDatabaseError($res, true)){
            return false;
        }
        $this->addToMessage(__("Enregistrement ").
                   $id." de la table operation_defunt [".$this->f->db->affectedRows().
            " ".__("modifie")."]") ;

        return true;
    }
}
