<?php
//$Id$ 
//gen openMairie le 25/01/2024 10:10

require_once PATH_OPENMAIRIE."om_dbform.class.php";

class om_contraintes_gen extends dbform {

    protected $_absolute_class_name = "om_contraintes";

    var $table = "om_contraintes";
    var $clePrimaire = "constraint_name";
    var $typeCle = "A";
    var $required_field = array(
        "constraint_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("table_name");
    }

    /**
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "constraint_name",
            "table_name",
            "column_name",
            "foreign_table_name",
            "foreign_column_name",
        );
    }

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

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




    function setvalF($val = array()) {
        //affectation valeur formulaire
        $this->valF['constraint_name'] = $val['constraint_name'];
        if ($val['table_name'] == "") {
            $this->valF['table_name'] = NULL;
        } else {
            $this->valF['table_name'] = $val['table_name'];
        }
        if ($val['column_name'] == "") {
            $this->valF['column_name'] = NULL;
        } else {
            $this->valF['column_name'] = $val['column_name'];
        }
        if ($val['foreign_table_name'] == "") {
            $this->valF['foreign_table_name'] = NULL;
        } else {
            $this->valF['foreign_table_name'] = $val['foreign_table_name'];
        }
        if ($val['foreign_column_name'] == "") {
            $this->valF['foreign_column_name'] = NULL;
        } else {
            $this->valF['foreign_column_name'] = $val['foreign_column_name'];
        }
    }

    //==========================
    // 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("constraint_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("column_name", "text");
            $form->setType("foreign_table_name", "text");
            $form->setType("foreign_column_name", "text");
        }

        // MDOE MODIFIER
        if ($maj == 1 || $crud == 'update') {
            $form->setType("constraint_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("column_name", "text");
            $form->setType("foreign_table_name", "text");
            $form->setType("foreign_column_name", "text");
        }

        // MODE SUPPRIMER
        if ($maj == 2 || $crud == 'delete') {
            $form->setType("constraint_name", "hiddenstatic");
            $form->setType("table_name", "selectstatic");
            $form->setType("column_name", "hiddenstatic");
            $form->setType("foreign_table_name", "hiddenstatic");
            $form->setType("foreign_column_name", "hiddenstatic");
        }

        // MODE CONSULTER
        if ($maj == 3 || $crud == 'read') {
            $form->setType("constraint_name", "static");
            $form->setType("table_name", "selectstatic");
            $form->setType("column_name", "static");
            $form->setType("foreign_table_name", "static");
            $form->setType("foreign_column_name", "static");
        }

    }

    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("constraint_name", 30);
        $form->setTaille("table_name", 30);
        $form->setTaille("column_name", 30);
        $form->setTaille("foreign_table_name", 30);
        $form->setTaille("foreign_column_name", 30);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("constraint_name", 80);
        $form->setMax("table_name", 40);
        $form->setMax("column_name", 40);
        $form->setMax("foreign_table_name", 40);
        $form->setMax("foreign_column_name", 40);
    }


    function setLib(&$form, $maj) {
    //libelle des champs
        $form->setLib('constraint_name', __('constraint_name'));
        $form->setLib('table_name', __('table_name'));
        $form->setLib('column_name', __('column_name'));
        $form->setLib('foreign_table_name', __('foreign_table_name'));
        $form->setLib('foreign_column_name', __('foreign_column_name'));
    }
    /**
     *
     */
    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
    //==================================
    

}
