<?php
//$Id$ 
//gen openMairie le 30/04/2024 22:08

require_once PATH_OPENMAIRIE."om_dbform.class.php";

class om_forms_gen extends dbform {

    protected $_absolute_class_name = "om_forms";

    var $table = "om_forms";
    var $clePrimaire = "column_name";
    var $typeCle = "A";
    var $required_field = array(
        "column_name"
    );
    
    var $foreign_keys_extended = array(
        "om_tables" => array("om_tables", ),
    );
    
    /**
     *
     * @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(
            "column_name",
            "table_name",
            "table_schema",
            "data_type",
            "character_maximum_length",
            "is_nullable",
            "column_default",
            "comment",
            "parametres",
            "libelle",
            "libelle_court",
            "type",
            "bloc",
            "position",
            "calcul",
            "liste",
            "position_liste",
        );
    }

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

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_table_name_by_id() {
        return "SELECT om_tables.table_name, om_tables.libelle FROM ".DB_PREFIXE."om_tables WHERE table_name = '<idx>'";
    }




    function setvalF($val = array()) {
        //affectation valeur formulaire
        $this->valF['column_name'] = $val['column_name'];
        if ($val['table_name'] == "") {
            $this->valF['table_name'] = NULL;
        } else {
            $this->valF['table_name'] = $val['table_name'];
        }
        if ($val['table_schema'] == "") {
            $this->valF['table_schema'] = NULL;
        } else {
            $this->valF['table_schema'] = $val['table_schema'];
        }
        if ($val['data_type'] == "") {
            $this->valF['data_type'] = NULL;
        } else {
            $this->valF['data_type'] = $val['data_type'];
        }
        if (!is_numeric($val['character_maximum_length'])) {
            $this->valF['character_maximum_length'] = NULL;
        } else {
            $this->valF['character_maximum_length'] = $val['character_maximum_length'];
        }
        if ($val['is_nullable'] == "") {
            $this->valF['is_nullable'] = NULL;
        } else {
            $this->valF['is_nullable'] = $val['is_nullable'];
        }
        if ($val['column_default'] == "") {
            $this->valF['column_default'] = NULL;
        } else {
            $this->valF['column_default'] = $val['column_default'];
        }
            $this->valF['comment'] = $val['comment'];
            $this->valF['parametres'] = $val['parametres'];
        if ($val['libelle'] == "") {
            $this->valF['libelle'] = NULL;
        } else {
            $this->valF['libelle'] = $val['libelle'];
        }
        if ($val['libelle_court'] == "") {
            $this->valF['libelle_court'] = NULL;
        } else {
            $this->valF['libelle_court'] = $val['libelle_court'];
        }
        if ($val['type'] == "") {
            $this->valF['type'] = NULL;
        } else {
            $this->valF['type'] = $val['type'];
        }
        if ($val['bloc'] == "") {
            $this->valF['bloc'] = NULL;
        } else {
            $this->valF['bloc'] = $val['bloc'];
        }
        if (!is_numeric($val['position'])) {
            $this->valF['position'] = NULL;
        } else {
            $this->valF['position'] = $val['position'];
        }
        if ($val['calcul'] == "") {
            $this->valF['calcul'] = NULL;
        } else {
            $this->valF['calcul'] = $val['calcul'];
        }
        if (!is_numeric($val['liste'])) {
            $this->valF['liste'] = NULL;
        } else {
            $this->valF['liste'] = $val['liste'];
        }
        if (!is_numeric($val['position_liste'])) {
            $this->valF['position_liste'] = NULL;
        } else {
            $this->valF['position_liste'] = $val['position_liste'];
        }
    }

    //==========================
    // 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("column_name", "text");
            if ($this->is_in_context_of_foreign_key("om_tables", $this->retourformulaire)) {
                $form->setType("table_name", "selecthiddenstatic");
            } else {
                $form->setType("table_name", "select");
            }
            $form->setType("table_schema", "text");
            $form->setType("data_type", "text");
            $form->setType("character_maximum_length", "text");
            $form->setType("is_nullable", "text");
            $form->setType("column_default", "text");
            $form->setType("comment", "textarea");
            $form->setType("parametres", "textarea");
            $form->setType("libelle", "text");
            $form->setType("libelle_court", "text");
            $form->setType("type", "text");
            $form->setType("bloc", "text");
            $form->setType("position", "text");
            $form->setType("calcul", "text");
            $form->setType("liste", "text");
            $form->setType("position_liste", "text");
        }

        // MDOE MODIFIER
        if ($maj == 1 || $crud == 'update') {
            $form->setType("column_name", "hiddenstatic");
            if ($this->is_in_context_of_foreign_key("om_tables", $this->retourformulaire)) {
                $form->setType("table_name", "selecthiddenstatic");
            } else {
                $form->setType("table_name", "select");
            }
            $form->setType("table_schema", "text");
            $form->setType("data_type", "text");
            $form->setType("character_maximum_length", "text");
            $form->setType("is_nullable", "text");
            $form->setType("column_default", "text");
            $form->setType("comment", "textarea");
            $form->setType("parametres", "textarea");
            $form->setType("libelle", "text");
            $form->setType("libelle_court", "text");
            $form->setType("type", "text");
            $form->setType("bloc", "text");
            $form->setType("position", "text");
            $form->setType("calcul", "text");
            $form->setType("liste", "text");
            $form->setType("position_liste", "text");
        }

        // MODE SUPPRIMER
        if ($maj == 2 || $crud == 'delete') {
            $form->setType("column_name", "hiddenstatic");
            $form->setType("table_name", "selectstatic");
            $form->setType("table_schema", "hiddenstatic");
            $form->setType("data_type", "hiddenstatic");
            $form->setType("character_maximum_length", "hiddenstatic");
            $form->setType("is_nullable", "hiddenstatic");
            $form->setType("column_default", "hiddenstatic");
            $form->setType("comment", "hiddenstatic");
            $form->setType("parametres", "hiddenstatic");
            $form->setType("libelle", "hiddenstatic");
            $form->setType("libelle_court", "hiddenstatic");
            $form->setType("type", "hiddenstatic");
            $form->setType("bloc", "hiddenstatic");
            $form->setType("position", "hiddenstatic");
            $form->setType("calcul", "hiddenstatic");
            $form->setType("liste", "hiddenstatic");
            $form->setType("position_liste", "hiddenstatic");
        }

        // MODE CONSULTER
        if ($maj == 3 || $crud == 'read') {
            $form->setType("column_name", "static");
            $form->setType("table_name", "selectstatic");
            $form->setType("table_schema", "static");
            $form->setType("data_type", "static");
            $form->setType("character_maximum_length", "static");
            $form->setType("is_nullable", "static");
            $form->setType("column_default", "static");
            $form->setType("comment", "textareastatic");
            $form->setType("parametres", "textareastatic");
            $form->setType("libelle", "static");
            $form->setType("libelle_court", "static");
            $form->setType("type", "static");
            $form->setType("bloc", "static");
            $form->setType("position", "static");
            $form->setType("calcul", "static");
            $form->setType("liste", "static");
            $form->setType("position_liste", "static");
        }

    }


    function setOnchange(&$form, $maj) {
    //javascript controle client
        $form->setOnchange('character_maximum_length','VerifNum(this)');
        $form->setOnchange('position','VerifNum(this)');
        $form->setOnchange('liste','VerifNum(this)');
        $form->setOnchange('position_liste','VerifNum(this)');
    }
    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("column_name", 30);
        $form->setTaille("table_name", 30);
        $form->setTaille("table_schema", 30);
        $form->setTaille("data_type", 30);
        $form->setTaille("character_maximum_length", 11);
        $form->setTaille("is_nullable", 10);
        $form->setTaille("column_default", 30);
        $form->setTaille("comment", 80);
        $form->setTaille("parametres", 80);
        $form->setTaille("libelle", 30);
        $form->setTaille("libelle_court", 30);
        $form->setTaille("type", 30);
        $form->setTaille("bloc", 10);
        $form->setTaille("position", 11);
        $form->setTaille("calcul", 30);
        $form->setTaille("liste", 11);
        $form->setTaille("position_liste", 11);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("column_name", 120);
        $form->setMax("table_name", 40);
        $form->setMax("table_schema", 40);
        $form->setMax("data_type", 40);
        $form->setMax("character_maximum_length", 11);
        $form->setMax("is_nullable", 5);
        $form->setMax("column_default", 80);
        $form->setMax("comment", 6);
        $form->setMax("parametres", 6);
        $form->setMax("libelle", 80);
        $form->setMax("libelle_court", 40);
        $form->setMax("type", 40);
        $form->setMax("bloc", 5);
        $form->setMax("position", 11);
        $form->setMax("calcul", 255);
        $form->setMax("liste", 11);
        $form->setMax("position_liste", 11);
    }


    function setLib(&$form, $maj) {
    //libelle des champs
        $form->setLib('column_name', __('column_name'));
        $form->setLib('table_name', __('table_name'));
        $form->setLib('table_schema', __('table_schema'));
        $form->setLib('data_type', __('data_type'));
        $form->setLib('character_maximum_length', __('character_maximum_length'));
        $form->setLib('is_nullable', __('is_nullable'));
        $form->setLib('column_default', __('column_default'));
        $form->setLib('comment', __('comment'));
        $form->setLib('parametres', __('parametres'));
        $form->setLib('libelle', __('libelle'));
        $form->setLib('libelle_court', __('libelle_court'));
        $form->setLib('type', __('type'));
        $form->setLib('bloc', __('bloc'));
        $form->setLib('position', __('position'));
        $form->setLib('calcul', __('calcul'));
        $form->setLib('liste', __('liste'));
        $form->setLib('position_liste', __('position_liste'));
    }
    /**
     *
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {

        // table_name
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "table_name",
            $this->get_var_sql_forminc__sql("table_name"),
            $this->get_var_sql_forminc__sql("table_name_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('om_tables', $this->retourformulaire))
                $form->setVal('table_name', $idxformulaire);
        }// fin validation
        $this->set_form_default_values($form, $maj, $validation);
    }// fin setValsousformulaire

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

}
