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

require_once PATH_OPENMAIRIE."om_dbform.class.php";

class om_triggers_gen extends dbform {

    protected $_absolute_class_name = "om_triggers";

    var $table = "om_triggers";
    var $clePrimaire = "trigger_name";
    var $typeCle = "A";
    var $required_field = array(
        "trigger_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("trigger_schema");
    }

    /**
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "trigger_name",
            "trigger_schema",
            "event_manipulation",
            "event_object_table",
            "action_order",
            "action_condition",
            "action_statement",
            "action_orientation",
            "action_timing",
        );
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_event_object_table() {
        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_event_object_table_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['trigger_name'] = $val['trigger_name'];
        if ($val['trigger_schema'] == "") {
            $this->valF['trigger_schema'] = NULL;
        } else {
            $this->valF['trigger_schema'] = $val['trigger_schema'];
        }
        if ($val['event_manipulation'] == "") {
            $this->valF['event_manipulation'] = NULL;
        } else {
            $this->valF['event_manipulation'] = $val['event_manipulation'];
        }
        if ($val['event_object_table'] == "") {
            $this->valF['event_object_table'] = NULL;
        } else {
            $this->valF['event_object_table'] = $val['event_object_table'];
        }
        if (!is_numeric($val['action_order'])) {
            $this->valF['action_order'] = NULL;
        } else {
            $this->valF['action_order'] = $val['action_order'];
        }
        if ($val['action_condition'] == "") {
            $this->valF['action_condition'] = NULL;
        } else {
            $this->valF['action_condition'] = $val['action_condition'];
        }
        if ($val['action_statement'] == "") {
            $this->valF['action_statement'] = NULL;
        } else {
            $this->valF['action_statement'] = $val['action_statement'];
        }
        if ($val['action_orientation'] == "") {
            $this->valF['action_orientation'] = NULL;
        } else {
            $this->valF['action_orientation'] = $val['action_orientation'];
        }
        if ($val['action_timing'] == "") {
            $this->valF['action_timing'] = NULL;
        } else {
            $this->valF['action_timing'] = $val['action_timing'];
        }
    }

    //==========================
    // 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("trigger_name", "text");
            $form->setType("trigger_schema", "text");
            $form->setType("event_manipulation", "text");
            if ($this->is_in_context_of_foreign_key("om_tables", $this->retourformulaire)) {
                $form->setType("event_object_table", "selecthiddenstatic");
            } else {
                $form->setType("event_object_table", "select");
            }
            $form->setType("action_order", "text");
            $form->setType("action_condition", "text");
            $form->setType("action_statement", "text");
            $form->setType("action_orientation", "text");
            $form->setType("action_timing", "text");
        }

        // MDOE MODIFIER
        if ($maj == 1 || $crud == 'update') {
            $form->setType("trigger_name", "hiddenstatic");
            $form->setType("trigger_schema", "text");
            $form->setType("event_manipulation", "text");
            if ($this->is_in_context_of_foreign_key("om_tables", $this->retourformulaire)) {
                $form->setType("event_object_table", "selecthiddenstatic");
            } else {
                $form->setType("event_object_table", "select");
            }
            $form->setType("action_order", "text");
            $form->setType("action_condition", "text");
            $form->setType("action_statement", "text");
            $form->setType("action_orientation", "text");
            $form->setType("action_timing", "text");
        }

        // MODE SUPPRIMER
        if ($maj == 2 || $crud == 'delete') {
            $form->setType("trigger_name", "hiddenstatic");
            $form->setType("trigger_schema", "hiddenstatic");
            $form->setType("event_manipulation", "hiddenstatic");
            $form->setType("event_object_table", "selectstatic");
            $form->setType("action_order", "hiddenstatic");
            $form->setType("action_condition", "hiddenstatic");
            $form->setType("action_statement", "hiddenstatic");
            $form->setType("action_orientation", "hiddenstatic");
            $form->setType("action_timing", "hiddenstatic");
        }

        // MODE CONSULTER
        if ($maj == 3 || $crud == 'read') {
            $form->setType("trigger_name", "static");
            $form->setType("trigger_schema", "static");
            $form->setType("event_manipulation", "static");
            $form->setType("event_object_table", "selectstatic");
            $form->setType("action_order", "static");
            $form->setType("action_condition", "static");
            $form->setType("action_statement", "static");
            $form->setType("action_orientation", "static");
            $form->setType("action_timing", "static");
        }

    }


    function setOnchange(&$form, $maj) {
    //javascript controle client
        $form->setOnchange('action_order','VerifNum(this)');
    }
    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("trigger_name", 30);
        $form->setTaille("trigger_schema", 30);
        $form->setTaille("event_manipulation", 30);
        $form->setTaille("event_object_table", 30);
        $form->setTaille("action_order", 11);
        $form->setTaille("action_condition", 30);
        $form->setTaille("action_statement", 30);
        $form->setTaille("action_orientation", 30);
        $form->setTaille("action_timing", 30);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("trigger_name", 80);
        $form->setMax("trigger_schema", 40);
        $form->setMax("event_manipulation", 40);
        $form->setMax("event_object_table", 40);
        $form->setMax("action_order", 11);
        $form->setMax("action_condition", 80);
        $form->setMax("action_statement", 255);
        $form->setMax("action_orientation", 80);
        $form->setMax("action_timing", 80);
    }


    function setLib(&$form, $maj) {
    //libelle des champs
        $form->setLib('trigger_name', __('trigger_name'));
        $form->setLib('trigger_schema', __('trigger_schema'));
        $form->setLib('event_manipulation', __('event_manipulation'));
        $form->setLib('event_object_table', __('event_object_table'));
        $form->setLib('action_order', __('action_order'));
        $form->setLib('action_condition', __('action_condition'));
        $form->setLib('action_statement', __('action_statement'));
        $form->setLib('action_orientation', __('action_orientation'));
        $form->setLib('action_timing', __('action_timing'));
    }
    /**
     *
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {

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

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

}
