<?php
//$Id$ 
//gen openMairie le 12/06/2024 16:14

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

class document_type_gen extends om_dbform {

    protected $_absolute_class_name = "document_type";

    var $table = "document_type";
    var $clePrimaire = "document_type";
    var $typeCle = "N";
    var $required_field = array(
        "code",
        "document_type",
        "document_type_categorie",
        "libelle"
    );
    
    var $foreign_keys_extended = array(
        "document_type_categorie" => array("document_type_categorie", ),
    );
    
    /**
     *
     * @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(
            "document_type",
            "libelle",
            "code",
            "document_type_categorie",
            "om_validite_debut",
            "om_validite_fin",
        );
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_document_type_categorie() {
        return "SELECT document_type_categorie.document_type_categorie, document_type_categorie.libelle FROM ".DB_PREFIXE."document_type_categorie ORDER BY document_type_categorie.libelle ASC";
    }

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




    function setvalF($val = array()) {
        //affectation valeur formulaire
        if (!is_numeric($val['document_type'])) {
            $this->valF['document_type'] = ""; // -> requis
        } else {
            $this->valF['document_type'] = $val['document_type'];
        }
        $this->valF['libelle'] = $val['libelle'];
        $this->valF['code'] = $val['code'];
        if (!is_numeric($val['document_type_categorie'])) {
            $this->valF['document_type_categorie'] = ""; // -> requis
        } else {
            $this->valF['document_type_categorie'] = $val['document_type_categorie'];
        }
        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("document_type", "hidden");
            $form->setType("libelle", "text");
            $form->setType("code", "text");
            if ($this->is_in_context_of_foreign_key("document_type_categorie", $this->retourformulaire)) {
                $form->setType("document_type_categorie", "selecthiddenstatic");
            } else {
                $form->setType("document_type_categorie", "select");
            }
            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("document_type", "hiddenstatic");
            $form->setType("libelle", "text");
            $form->setType("code", "text");
            if ($this->is_in_context_of_foreign_key("document_type_categorie", $this->retourformulaire)) {
                $form->setType("document_type_categorie", "selecthiddenstatic");
            } else {
                $form->setType("document_type_categorie", "select");
            }
            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("document_type", "hiddenstatic");
            $form->setType("libelle", "hiddenstatic");
            $form->setType("code", "hiddenstatic");
            $form->setType("document_type_categorie", "selectstatic");
            $form->setType("om_validite_debut", "hiddenstatic");
            $form->setType("om_validite_fin", "hiddenstatic");
        }

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

    }


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

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


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

        // document_type_categorie
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "document_type_categorie",
            $this->get_var_sql_forminc__sql("document_type_categorie"),
            $this->get_var_sql_forminc__sql("document_type_categorie_by_id"),
            false
        );
    }


    //==================================
    // 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('document_type_categorie', $this->retourformulaire))
                $form->setVal('document_type_categorie', $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 : evenement
        $this->rechercheTable($this->f->db, "evenement", "document_type", $id);
        // Verification de la cle secondaire : instruction
        $this->rechercheTable($this->f->db, "instruction", "document_type_instruction", $id);
    }


}
