<?php
//$Id$ 
//gen openMairie le 23/09/2016 10:40

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

class voie_gen extends om_dbform {

    var $table = "voie";
    var $clePrimaire = "voie";
    var $typeCle = "N";
    var $required_field = array(
        "voie",
        "voielib",
        "voietype",
        "zone"
    );
    
    var $foreign_keys_extended = array(
        "voie_type" => array("voie_type", ),
        "zone" => array("zone", ),
    );



    function setvalF($val) {
        //affectation valeur formulaire
        if (!is_numeric($val['voie'])) {
            $this->valF['voie'] = ""; // -> requis
        } else {
            $this->valF['voie'] = $val['voie'];
        }
        if (!is_numeric($val['zone'])) {
            $this->valF['zone'] = ""; // -> requis
        } else {
            $this->valF['zone'] = $val['zone'];
        }
        if (!is_numeric($val['voietype'])) {
            $this->valF['voietype'] = ""; // -> requis
        } else {
            $this->valF['voietype'] = $val['voietype'];
        }
        $this->valF['voielib'] = $val['voielib'];
        if ($val['geom'] == "") {
            unset($this->valF['geom']);
        } else {
            $this->valF['geom'] = $val['geom'];
        }
    }

    //=================================================
    //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) {
    //numero automatique -> pas de controle ajout cle primaire
    }

    function verifierAjout() {
    //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("voie", "hidden");
            if ($this->is_in_context_of_foreign_key("zone", $this->retourformulaire)) {
                $form->setType("zone", "selecthiddenstatic");
            } else {
                $form->setType("zone", "select");
            }
            if ($this->is_in_context_of_foreign_key("voie_type", $this->retourformulaire)) {
                $form->setType("voietype", "selecthiddenstatic");
            } else {
                $form->setType("voietype", "select");
            }
            $form->setType("voielib", "text");
            $form->setType("geom", "geom");
        }

        // MDOE MODIFIER
        if ($maj == 1 || $crud == 'update') {
            $form->setType("voie", "hiddenstatic");
            if ($this->is_in_context_of_foreign_key("zone", $this->retourformulaire)) {
                $form->setType("zone", "selecthiddenstatic");
            } else {
                $form->setType("zone", "select");
            }
            if ($this->is_in_context_of_foreign_key("voie_type", $this->retourformulaire)) {
                $form->setType("voietype", "selecthiddenstatic");
            } else {
                $form->setType("voietype", "select");
            }
            $form->setType("voielib", "text");
            $form->setType("geom", "geom");
        }

        // MODE SUPPRIMER
        if ($maj == 2 || $crud == 'delete') {
            $form->setType("voie", "hiddenstatic");
            $form->setType("zone", "selectstatic");
            $form->setType("voietype", "selectstatic");
            $form->setType("voielib", "hiddenstatic");
            $form->setType("geom", "geom");
        }

        // MODE CONSULTER
        if ($maj == 3 || $crud == 'read') {
            $form->setType("voie", "static");
            $form->setType("zone", "selectstatic");
            $form->setType("voietype", "selectstatic");
            $form->setType("voielib", "static");
            $form->setType("geom", "geom");
        }

    }


    function setOnchange(&$form, $maj) {
    //javascript controle client
        $form->setOnchange('voie','VerifNum(this)');
        $form->setOnchange('zone','VerifNum(this)');
        $form->setOnchange('voietype','VerifNum(this)');
    }
    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("voie", 11);
        $form->setTaille("zone", 11);
        $form->setTaille("voietype", 11);
        $form->setTaille("voielib", 30);
        $form->setTaille("geom", 30);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("voie", 11);
        $form->setMax("zone", 11);
        $form->setMax("voietype", 11);
        $form->setMax("voielib", 40);
        $form->setMax("geom", 551444);
    }


    function setLib(&$form, $maj) {
    //libelle des champs
        $form->setLib('voie',_('voie'));
        $form->setLib('zone',_('zone'));
        $form->setLib('voietype',_('voietype'));
        $form->setLib('voielib',_('voielib'));
        $form->setLib('geom',_('geom'));
    }
    /**
     *
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {

        // Inclusion du fichier de requêtes
        if (file_exists("../sql/".OM_DB_PHPTYPE."/".$this->table.".form.inc.php")) {
            include "../sql/".OM_DB_PHPTYPE."/".$this->table.".form.inc.php";
        } elseif (file_exists("../sql/".OM_DB_PHPTYPE."/".$this->table.".form.inc")) {
            include "../sql/".OM_DB_PHPTYPE."/".$this->table.".form.inc";
        }

        // voietype
        $this->init_select($form, $this->f->db, $maj, null, "voietype", $sql_voietype, $sql_voietype_by_id, true);
        // zone
        $this->init_select($form, $this->f->db, $maj, null, "zone", $sql_zone, $sql_zone_by_id, false);
        // geom
        if ($maj == 1 || $maj == 3) {
            $contenu = array();
            $contenu[0] = array("voie", $this->getParameter("idx"), "0");
            $form->setSelect("geom", $contenu);
        }
    }


    //==================================
    // 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('voie_type', $this->retourformulaire))
                $form->setVal('voietype', $idxformulaire);
            if($this->is_in_context_of_foreign_key('zone', $this->retourformulaire))
                $form->setVal('zone', $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 : emplacement
        $this->rechercheTable($this->f->db, "emplacement", "voie", $id);
    }


}

?>
