<?php
//$Id$ 
//gen openMairie le 28/03/2023 16:36

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

class nature_travaux_gen extends om_dbform {

    protected $_absolute_class_name = "nature_travaux";

    var $table = "nature_travaux";
    var $clePrimaire = "nature_travaux";
    var $typeCle = "N";
    var $required_field = array(
        "famille_travaux",
        "libelle",
        "nature_travaux"
    );
    
    var $foreign_keys_extended = array(
        "famille_travaux" => array("famille_travaux", ),
    );
    
    /**
     *
     * @return string
     */
    function get_default_libelle() {
        return $this->getVal($this->clePrimaire)."&nbsp;".$this->getVal("libelle");
    }

    /**
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "nature_travaux",
            "libelle",
            "code",
            "famille_travaux",
            "description",
            "om_validite_debut",
            "om_validite_fin",
        );
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_famille_travaux() {
        return "SELECT famille_travaux.famille_travaux, famille_travaux.libelle FROM ".DB_PREFIXE."famille_travaux WHERE ((famille_travaux.om_validite_debut IS NULL AND (famille_travaux.om_validite_fin IS NULL OR famille_travaux.om_validite_fin > CURRENT_DATE)) OR (famille_travaux.om_validite_debut <= CURRENT_DATE AND (famille_travaux.om_validite_fin IS NULL OR famille_travaux.om_validite_fin > CURRENT_DATE))) ORDER BY famille_travaux.libelle ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_famille_travaux_by_id() {
        return "SELECT famille_travaux.famille_travaux, famille_travaux.libelle FROM ".DB_PREFIXE."famille_travaux WHERE famille_travaux = <idx>";
    }




    function setvalF($val = array()) {
        //affectation valeur formulaire
        if (!is_numeric($val['nature_travaux'])) {
            $this->valF['nature_travaux'] = ""; // -> requis
        } else {
            $this->valF['nature_travaux'] = $val['nature_travaux'];
        }
        $this->valF['libelle'] = $val['libelle'];
        if ($val['code'] == "") {
            $this->valF['code'] = NULL;
        } else {
            $this->valF['code'] = $val['code'];
        }
        if (!is_numeric($val['famille_travaux'])) {
            $this->valF['famille_travaux'] = ""; // -> requis
        } else {
            $this->valF['famille_travaux'] = $val['famille_travaux'];
        }
            $this->valF['description'] = $val['description'];
        if ($val['om_validite_debut'] != "") {
            $this->valF['om_validite_debut'] = $this->dateDB($val['om_validite_debut']);
        } else {
            $this->valF['om_validite_debut'] = NULL;
        }
        if ($val['om_validite_fin'] != "") {
            $this->valF['om_validite_fin'] = $this->dateDB($val['om_validite_fin']);
        } else {
            $this->valF['om_validite_fin'] = NULL;
        }
    }

    //=================================================
    //cle primaire automatique [automatic primary key]
    //==================================================

    function setId(&$dnu1 = null) {
    //numero automatique
        $this->valF[$this->clePrimaire] = $this->f->db->nextId(DB_PREFIXE.$this->table);
    }

    function setValFAjout($val = array()) {
    //numero automatique -> pas de controle ajout cle primaire
    }

    function verifierAjout($val = array(), &$dnu1 = null) {
    //numero automatique -> pas de verfication de cle primaire
    }
    /**
     * Methode verifier
     */
    function verifier($val = array(), &$dnu1 = null, $dnu2 = null) {
        // On appelle la methode de la classe parent
        parent::verifier($val, $this->f->db, null);

        // gestion des dates de validites
        $date_debut = $this->valF['om_validite_debut'];
        $date_fin = $this->valF['om_validite_fin'];

        if ($date_debut != '' and $date_fin != '') {
        
            $date_debut = explode('-', $this->valF['om_validite_debut']);
            $date_fin = explode('-', $this->valF['om_validite_fin']);

            $time_debut = mktime(0, 0, 0, $date_debut[1], $date_debut[2],
                                 $date_debut[0]);
            $time_fin = mktime(0, 0, 0, $date_fin[1], $date_fin[2],
                                 $date_fin[0]);

            if ($time_debut > $time_fin or $time_debut == $time_fin) {
                $this->correct = false;
                $this->addToMessage(__('La date de fin de validite doit etre future a la de debut de validite.'));
            }
        }
    }


    //==========================
    // Formulaire  [form]
    //==========================
    /**
     *
     */
    function setType(&$form, $maj) {
        // Récupération du mode de l'action
        $crud = $this->get_action_crud($maj);

        // MODE AJOUTER
        if ($maj == 0 || $crud == 'create') {
            $form->setType("nature_travaux", "hidden");
            $form->setType("libelle", "text");
            $form->setType("code", "text");
            if ($this->is_in_context_of_foreign_key("famille_travaux", $this->retourformulaire)) {
                $form->setType("famille_travaux", "selecthiddenstatic");
            } else {
                $form->setType("famille_travaux", "select");
            }
            $form->setType("description", "textarea");
            if ($this->f->isAccredited(array($this->table."_modifier_validite", $this->table, ))) {
                $form->setType("om_validite_debut", "date");
            } else {
                $form->setType("om_validite_debut", "hiddenstaticdate");
            }
            if ($this->f->isAccredited(array($this->table."_modifier_validite", $this->table, ))) {
                $form->setType("om_validite_fin", "date");
            } else {
                $form->setType("om_validite_fin", "hiddenstaticdate");
            }
        }

        // MDOE MODIFIER
        if ($maj == 1 || $crud == 'update') {
            $form->setType("nature_travaux", "hiddenstatic");
            $form->setType("libelle", "text");
            $form->setType("code", "text");
            if ($this->is_in_context_of_foreign_key("famille_travaux", $this->retourformulaire)) {
                $form->setType("famille_travaux", "selecthiddenstatic");
            } else {
                $form->setType("famille_travaux", "select");
            }
            $form->setType("description", "textarea");
            if ($this->f->isAccredited(array($this->table."_modifier_validite", $this->table, ))) {
                $form->setType("om_validite_debut", "date");
            } else {
                $form->setType("om_validite_debut", "hiddenstaticdate");
            }
            if ($this->f->isAccredited(array($this->table."_modifier_validite", $this->table, ))) {
                $form->setType("om_validite_fin", "date");
            } else {
                $form->setType("om_validite_fin", "hiddenstaticdate");
            }
        }

        // MODE SUPPRIMER
        if ($maj == 2 || $crud == 'delete') {
            $form->setType("nature_travaux", "hiddenstatic");
            $form->setType("libelle", "hiddenstatic");
            $form->setType("code", "hiddenstatic");
            $form->setType("famille_travaux", "selectstatic");
            $form->setType("description", "hiddenstatic");
            $form->setType("om_validite_debut", "hiddenstatic");
            $form->setType("om_validite_fin", "hiddenstatic");
        }

        // MODE CONSULTER
        if ($maj == 3 || $crud == 'read') {
            $form->setType("nature_travaux", "static");
            $form->setType("libelle", "static");
            $form->setType("code", "static");
            $form->setType("famille_travaux", "selectstatic");
            $form->setType("description", "textareastatic");
            $form->setType("om_validite_debut", "datestatic");
            $form->setType("om_validite_fin", "datestatic");
        }

    }


    function setOnchange(&$form, $maj) {
    //javascript controle client
        $form->setOnchange('nature_travaux','VerifNum(this)');
        $form->setOnchange('famille_travaux','VerifNum(this)');
        $form->setOnchange('om_validite_debut','fdate(this)');
        $form->setOnchange('om_validite_fin','fdate(this)');
    }
    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("nature_travaux", 11);
        $form->setTaille("libelle", 30);
        $form->setTaille("code", 30);
        $form->setTaille("famille_travaux", 11);
        $form->setTaille("description", 80);
        $form->setTaille("om_validite_debut", 12);
        $form->setTaille("om_validite_fin", 12);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("nature_travaux", 11);
        $form->setMax("libelle", 255);
        $form->setMax("code", 50);
        $form->setMax("famille_travaux", 11);
        $form->setMax("description", 6);
        $form->setMax("om_validite_debut", 12);
        $form->setMax("om_validite_fin", 12);
    }


    function setLib(&$form, $maj) {
    //libelle des champs
        $form->setLib('nature_travaux', __('nature_travaux'));
        $form->setLib('libelle', __('libelle'));
        $form->setLib('code', __('code'));
        $form->setLib('famille_travaux', __('famille_travaux'));
        $form->setLib('description', __('description'));
        $form->setLib('om_validite_debut', __('om_validite_debut'));
        $form->setLib('om_validite_fin', __('om_validite_fin'));
    }
    /**
     *
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {

        // famille_travaux
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "famille_travaux",
            $this->get_var_sql_forminc__sql("famille_travaux"),
            $this->get_var_sql_forminc__sql("famille_travaux_by_id"),
            true
        );
    }


    //==================================
    // sous Formulaire
    //==================================
    

    function setValsousformulaire(&$form, $maj, $validation, $idxformulaire, $retourformulaire, $typeformulaire, &$dnu1 = null, $dnu2 = null) {
        $this->retourformulaire = $retourformulaire;
        if($validation == 0) {
            if($this->is_in_context_of_foreign_key('famille_travaux', $this->retourformulaire))
                $form->setVal('famille_travaux', $idxformulaire);
        }// fin validation
        $this->set_form_default_values($form, $maj, $validation);
    }// fin setValsousformulaire

    //==================================
    // cle secondaire
    //==================================
    
    /**
     * Methode clesecondaire
     */
    function cleSecondaire($id, &$dnu1 = null, $val = array(), $dnu2 = null) {
        // On appelle la methode de la classe parent
        parent::cleSecondaire($id);
        // Verification de la cle secondaire : lien_dit_nature_travaux
        $this->rechercheTable($this->f->db, "lien_dit_nature_travaux", "nature_travaux", $id);
        // Verification de la cle secondaire : lien_dossier_nature_travaux
        $this->rechercheTable($this->f->db, "lien_dossier_nature_travaux", "nature_travaux", $id);
    }


}
