<?php
//$Id$ 
//gen openMairie le 03/01/2025 08:11

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

class courrier_gen extends om_dbform {

    protected $_absolute_class_name = "courrier";

    var $table = "courrier";
    var $clePrimaire = "courrier";
    var $typeCle = "N";
    var $required_field = array(
        "courrier"
    );
    
    var $foreign_keys_extended = array(
        "contrat" => array("contrat", ),
        "autorisation" => array("autorisation", ),
        "emplacement" => array("emplacement", "colombarium", "concession", "depositoire", "enfeu", "ossuaire", "terraincommunal", ),
        "reprise_abandon" => array("reprise_abandon", ),
        "travaux" => array("travaux", ),
    );
    
    /**
     *
     * @return string
     */
    function get_default_libelle() {
        return $this->getVal($this->clePrimaire)."&nbsp;".$this->getVal("destinataire");
    }

    /**
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "courrier",
            "destinataire",
            "datecourrier",
            "lettretype",
            "complement",
            "emplacement",
            "contrat",
            "contexte",
            "reprise_abandon",
            "objet",
            "finalise",
            "fichier",
            "travaux",
        );
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_contrat() {
        return "SELECT contrat.contrat, contrat.emplacement FROM ".DB_PREFIXE."contrat ORDER BY contrat.emplacement ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_contrat_by_id() {
        return "SELECT contrat.contrat, contrat.emplacement FROM ".DB_PREFIXE."contrat WHERE contrat = <idx>";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_destinataire() {
        return "SELECT autorisation.autorisation, autorisation.emplacement FROM ".DB_PREFIXE."autorisation ORDER BY autorisation.emplacement ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_destinataire_by_id() {
        return "SELECT autorisation.autorisation, autorisation.emplacement FROM ".DB_PREFIXE."autorisation WHERE autorisation = <idx>";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_emplacement() {
        return "SELECT emplacement.emplacement, emplacement.nature FROM ".DB_PREFIXE."emplacement ORDER BY emplacement.nature ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_emplacement_by_id() {
        return "SELECT emplacement.emplacement, emplacement.nature FROM ".DB_PREFIXE."emplacement WHERE emplacement = <idx>";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_reprise_abandon() {
        return "SELECT reprise_abandon.reprise_abandon, reprise_abandon.emplacement FROM ".DB_PREFIXE."reprise_abandon ORDER BY reprise_abandon.emplacement ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_reprise_abandon_by_id() {
        return "SELECT reprise_abandon.reprise_abandon, reprise_abandon.emplacement FROM ".DB_PREFIXE."reprise_abandon WHERE reprise_abandon = <idx>";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_travaux() {
        return "SELECT travaux.travaux, travaux.entreprise FROM ".DB_PREFIXE."travaux ORDER BY travaux.entreprise ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_travaux_by_id() {
        return "SELECT travaux.travaux, travaux.entreprise FROM ".DB_PREFIXE."travaux WHERE travaux = <idx>";
    }




    function setvalF($val = array()) {
        //affectation valeur formulaire
        if (!is_numeric($val['courrier'])) {
            $this->valF['courrier'] = ""; // -> requis
        } else {
            $this->valF['courrier'] = $val['courrier'];
        }
        if (!is_numeric($val['destinataire'])) {
            $this->valF['destinataire'] = NULL;
        } else {
            $this->valF['destinataire'] = $val['destinataire'];
        }
        if ($val['datecourrier'] != "") {
            $this->valF['datecourrier'] = $this->dateDB($val['datecourrier']);
        } else {
            $this->valF['datecourrier'] = NULL;
        }
        if ($val['lettretype'] == "") {
            $this->valF['lettretype'] = NULL;
        } else {
            $this->valF['lettretype'] = $val['lettretype'];
        }
            $this->valF['complement'] = $val['complement'];
        if (!is_numeric($val['emplacement'])) {
            $this->valF['emplacement'] = NULL;
        } else {
            $this->valF['emplacement'] = $val['emplacement'];
        }
        if (!is_numeric($val['contrat'])) {
            $this->valF['contrat'] = NULL;
        } else {
            $this->valF['contrat'] = $val['contrat'];
        }
        if ($val['contexte'] == "") {
            $this->valF['contexte'] = NULL;
        } else {
            $this->valF['contexte'] = $val['contexte'];
        }
        if (!is_numeric($val['reprise_abandon'])) {
            $this->valF['reprise_abandon'] = NULL;
        } else {
            $this->valF['reprise_abandon'] = $val['reprise_abandon'];
        }
        if ($val['objet'] == "") {
            $this->valF['objet'] = NULL;
        } else {
            $this->valF['objet'] = $val['objet'];
        }
        if ($val['finalise'] == 1 || $val['finalise'] == "t" || $val['finalise'] == "Oui") {
            $this->valF['finalise'] = true;
        } else {
            $this->valF['finalise'] = false;
        }
        if ($val['fichier'] == "") {
            $this->valF['fichier'] = NULL;
        } else {
            $this->valF['fichier'] = $val['fichier'];
        }
        if (!is_numeric($val['travaux'])) {
            $this->valF['travaux'] = NULL;
        } else {
            $this->valF['travaux'] = $val['travaux'];
        }
    }

    //=================================================
    //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
    }

    //==========================
    // 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("courrier", "hidden");
            if ($this->is_in_context_of_foreign_key("autorisation", $this->retourformulaire)) {
                $form->setType("destinataire", "selecthiddenstatic");
            } else {
                $form->setType("destinataire", "select");
            }
            $form->setType("datecourrier", "date");
            $form->setType("lettretype", "text");
            $form->setType("complement", "textarea");
            if ($this->is_in_context_of_foreign_key("emplacement", $this->retourformulaire)) {
                $form->setType("emplacement", "selecthiddenstatic");
            } else {
                $form->setType("emplacement", "select");
            }
            if ($this->is_in_context_of_foreign_key("contrat", $this->retourformulaire)) {
                $form->setType("contrat", "selecthiddenstatic");
            } else {
                $form->setType("contrat", "select");
            }
            $form->setType("contexte", "text");
            if ($this->is_in_context_of_foreign_key("reprise_abandon", $this->retourformulaire)) {
                $form->setType("reprise_abandon", "selecthiddenstatic");
            } else {
                $form->setType("reprise_abandon", "select");
            }
            $form->setType("objet", "text");
            $form->setType("finalise", "checkbox");
            $form->setType("fichier", "text");
            if ($this->is_in_context_of_foreign_key("travaux", $this->retourformulaire)) {
                $form->setType("travaux", "selecthiddenstatic");
            } else {
                $form->setType("travaux", "select");
            }
        }

        // MDOE MODIFIER
        if ($maj == 1 || $crud == 'update') {
            $form->setType("courrier", "hiddenstatic");
            if ($this->is_in_context_of_foreign_key("autorisation", $this->retourformulaire)) {
                $form->setType("destinataire", "selecthiddenstatic");
            } else {
                $form->setType("destinataire", "select");
            }
            $form->setType("datecourrier", "date");
            $form->setType("lettretype", "text");
            $form->setType("complement", "textarea");
            if ($this->is_in_context_of_foreign_key("emplacement", $this->retourformulaire)) {
                $form->setType("emplacement", "selecthiddenstatic");
            } else {
                $form->setType("emplacement", "select");
            }
            if ($this->is_in_context_of_foreign_key("contrat", $this->retourformulaire)) {
                $form->setType("contrat", "selecthiddenstatic");
            } else {
                $form->setType("contrat", "select");
            }
            $form->setType("contexte", "text");
            if ($this->is_in_context_of_foreign_key("reprise_abandon", $this->retourformulaire)) {
                $form->setType("reprise_abandon", "selecthiddenstatic");
            } else {
                $form->setType("reprise_abandon", "select");
            }
            $form->setType("objet", "text");
            $form->setType("finalise", "checkbox");
            $form->setType("fichier", "text");
            if ($this->is_in_context_of_foreign_key("travaux", $this->retourformulaire)) {
                $form->setType("travaux", "selecthiddenstatic");
            } else {
                $form->setType("travaux", "select");
            }
        }

        // MODE SUPPRIMER
        if ($maj == 2 || $crud == 'delete') {
            $form->setType("courrier", "hiddenstatic");
            $form->setType("destinataire", "selectstatic");
            $form->setType("datecourrier", "hiddenstatic");
            $form->setType("lettretype", "hiddenstatic");
            $form->setType("complement", "hiddenstatic");
            $form->setType("emplacement", "selectstatic");
            $form->setType("contrat", "selectstatic");
            $form->setType("contexte", "hiddenstatic");
            $form->setType("reprise_abandon", "selectstatic");
            $form->setType("objet", "hiddenstatic");
            $form->setType("finalise", "hiddenstatic");
            $form->setType("fichier", "hiddenstatic");
            $form->setType("travaux", "selectstatic");
        }

        // MODE CONSULTER
        if ($maj == 3 || $crud == 'read') {
            $form->setType("courrier", "static");
            $form->setType("destinataire", "selectstatic");
            $form->setType("datecourrier", "datestatic");
            $form->setType("lettretype", "static");
            $form->setType("complement", "textareastatic");
            $form->setType("emplacement", "selectstatic");
            $form->setType("contrat", "selectstatic");
            $form->setType("contexte", "static");
            $form->setType("reprise_abandon", "selectstatic");
            $form->setType("objet", "static");
            $form->setType("finalise", "checkboxstatic");
            $form->setType("fichier", "static");
            $form->setType("travaux", "selectstatic");
        }

    }


    function setOnchange(&$form, $maj) {
    //javascript controle client
        $form->setOnchange('courrier','VerifNum(this)');
        $form->setOnchange('destinataire','VerifNum(this)');
        $form->setOnchange('datecourrier','fdate(this)');
        $form->setOnchange('emplacement','VerifNum(this)');
        $form->setOnchange('contrat','VerifNum(this)');
        $form->setOnchange('reprise_abandon','VerifNum(this)');
        $form->setOnchange('travaux','VerifNum(this)');
    }
    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("courrier", 11);
        $form->setTaille("destinataire", 11);
        $form->setTaille("datecourrier", 12);
        $form->setTaille("lettretype", 30);
        $form->setTaille("complement", 80);
        $form->setTaille("emplacement", 11);
        $form->setTaille("contrat", 11);
        $form->setTaille("contexte", 30);
        $form->setTaille("reprise_abandon", 11);
        $form->setTaille("objet", 30);
        $form->setTaille("finalise", 1);
        $form->setTaille("fichier", 30);
        $form->setTaille("travaux", 11);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("courrier", 11);
        $form->setMax("destinataire", 11);
        $form->setMax("datecourrier", 12);
        $form->setMax("lettretype", 255);
        $form->setMax("complement", 6);
        $form->setMax("emplacement", 11);
        $form->setMax("contrat", 11);
        $form->setMax("contexte", 255);
        $form->setMax("reprise_abandon", 11);
        $form->setMax("objet", 250);
        $form->setMax("finalise", 1);
        $form->setMax("fichier", 50);
        $form->setMax("travaux", 11);
    }


    function setLib(&$form, $maj) {
    //libelle des champs
        $form->setLib('courrier', __('courrier'));
        $form->setLib('destinataire', __('destinataire'));
        $form->setLib('datecourrier', __('datecourrier'));
        $form->setLib('lettretype', __('lettretype'));
        $form->setLib('complement', __('complement'));
        $form->setLib('emplacement', __('emplacement'));
        $form->setLib('contrat', __('contrat'));
        $form->setLib('contexte', __('contexte'));
        $form->setLib('reprise_abandon', __('reprise_abandon'));
        $form->setLib('objet', __('objet'));
        $form->setLib('finalise', __('finalise'));
        $form->setLib('fichier', __('fichier'));
        $form->setLib('travaux', __('travaux'));
    }
    /**
     *
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {

        // contrat
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "contrat",
            $this->get_var_sql_forminc__sql("contrat"),
            $this->get_var_sql_forminc__sql("contrat_by_id"),
            false
        );
        // destinataire
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "destinataire",
            $this->get_var_sql_forminc__sql("destinataire"),
            $this->get_var_sql_forminc__sql("destinataire_by_id"),
            false
        );
        // emplacement
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "emplacement",
            $this->get_var_sql_forminc__sql("emplacement"),
            $this->get_var_sql_forminc__sql("emplacement_by_id"),
            false
        );
        // reprise_abandon
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "reprise_abandon",
            $this->get_var_sql_forminc__sql("reprise_abandon"),
            $this->get_var_sql_forminc__sql("reprise_abandon_by_id"),
            false
        );
        // travaux
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "travaux",
            $this->get_var_sql_forminc__sql("travaux"),
            $this->get_var_sql_forminc__sql("travaux_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('contrat', $this->retourformulaire))
                $form->setVal('contrat', $idxformulaire);
            if($this->is_in_context_of_foreign_key('autorisation', $this->retourformulaire))
                $form->setVal('destinataire', $idxformulaire);
            if($this->is_in_context_of_foreign_key('emplacement', $this->retourformulaire))
                $form->setVal('emplacement', $idxformulaire);
            if($this->is_in_context_of_foreign_key('reprise_abandon', $this->retourformulaire))
                $form->setVal('reprise_abandon', $idxformulaire);
            if($this->is_in_context_of_foreign_key('travaux', $this->retourformulaire))
                $form->setVal('travaux', $idxformulaire);
        }// fin validation
        $this->set_form_default_values($form, $maj, $validation);
    }// fin setValsousformulaire

    //==================================
    // cle secondaire
    //==================================
    

}
