<?php
//$Id$ 
//gen openMairie le 07/06/2013 19:36

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

class travaux_nature_gen extends om_dbform {
    var $table="travaux_nature";
    var $clePrimaire="travaux_nature";
    var $typeCle="N";
    var $required_field=array(
        "code",
        "libelle",
        "travaux_nature"
    );
    var $retourformulaire;

    function setvalF($val) {
        //affectation valeur formulaire
        if (!is_numeric($val['travaux_nature'])) {
            $this->valF['travaux_nature'] = ""; // -> requis
        } else {
            $this->valF['travaux_nature'] = $val['travaux_nature'];
        }
        $this->valF['code'] = $val['code'];
        $this->valF['libelle'] = $val['libelle'];
        if ($val['description'] == "") {
            $this->valF['description'] = NULL;
        } else {
            $this->valF['description'] = $val['description'];
        }
        if ($val['om_validite_debut'] != "") {
            $this->valF['om_validite_debut'] = $this->dateDB($val['om_validite_debut']);
        } else {
            $this->valF['om_validite_debut'] = NULL;
        }
        if ($val['om_validite_fin'] != "") {
            $this->valF['om_validite_fin'] = $this->dateDB($val['om_validite_fin']);
        } else {
            $this->valF['om_validite_fin'] = NULL;
        }
    }

    //=================================================
    //cle primaire automatique [automatic primary key]
    //==================================================

    function setId(&$db) {
    //numero automatique
        $this->valF[$this->clePrimaire] = $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
    }
    /**
     * Methode verifier
     */
    function verifier($val = array(), &$db = NULL, $DEBUG = false) {
        // On appelle la methode de la classe parent
        parent::verifier($val, $db, $DEBUG);

        // gestion des dates de validites
        $date_debut = $this->valF['om_validite_debut'];
        $date_fin = $this->valF['om_validite_fin'];

        if ($date_debut != '' and $date_fin != '') {
        
            $date_debut = explode('-', $this->valF['om_validite_debut']);
            $date_fin = explode('-', $this->valF['om_validite_fin']);

            $time_debut = mktime(0, 0, 0, $date_debut[1], $date_debut[2],
                                 $date_debut[0]);
            $time_fin = mktime(0, 0, 0, $date_fin[1], $date_fin[2],
                                 $date_fin[0]);

            if ($time_debut > $time_fin or $time_debut == $time_fin) {
                $this->correct = false;
                $this->addToMessage(_('La date de fin de validite doit etre future a la de debut de validite.'));
            }
        }
    }


    //==========================
    // Formulaire  [form]
    //==========================

    function setType(&$form,$maj) {
        //type
        if ($maj==0){ //ajout
            $form->setType('travaux_nature','hidden');// cle automatique
            $form->setType('code','text');
            $form->setType('libelle','text');
            $form->setType('description','text');
            if ($this->f->isAccredited(array($this->table.'_modifier_validite',
                                             $this->table))) {
                if($this->retourformulaire=='')
                    $form->setType('om_validite_debut','date');
                else
                    $form->setType('om_validite_debut','date2');
            } else {
                $form->setType('om_validite_debut','hiddenstaticdate');
            }
            if ($this->f->isAccredited(array($this->table.'_modifier_validite',
                                             $this->table))) {
                if($this->retourformulaire=='')
                    $form->setType('om_validite_fin','date');
                else
                    $form->setType('om_validite_fin','date2');
            } else {
                $form->setType('om_validite_fin','hiddenstaticdate');
            }
        }// fin ajout
        if ($maj==1){ //modifier
            $form->setType('travaux_nature','hiddenstatic');
            $form->setType('code','text');
            $form->setType('libelle','text');
            $form->setType('description','text');
            if ($this->f->isAccredited(array($this->table.'_modifier_validite',
                                             $this->table))) {
                if($this->retourformulaire=='')
                    $form->setType('om_validite_debut','date');
                else
                    $form->setType('om_validite_debut','date2');
            } else {
                $form->setType('om_validite_debut','hiddenstaticdate');
            }
            if ($this->f->isAccredited(array($this->table.'_modifier_validite',
                                             $this->table))) {
                if($this->retourformulaire=='')
                    $form->setType('om_validite_fin','date');
                else
                    $form->setType('om_validite_fin','date2');
            } else {
                $form->setType('om_validite_fin','hiddenstaticdate');
            }
        }// fin modifier
        if ($maj==2){ //supprimer
            $form->setType('travaux_nature','hiddenstatic');
            $form->setType('code','hiddenstatic');
            $form->setType('libelle','hiddenstatic');
            $form->setType('description','hiddenstatic');
            $form->setType('om_validite_debut','hiddenstatic');
            $form->setType('om_validite_fin','hiddenstatic');
        }//fin supprimer
        if ($maj==3){ //consulter
            $form->setType('travaux_nature','static');
            $form->setType('code','static');
            $form->setType('libelle','static');
            $form->setType('description','static');
            $form->setType('om_validite_debut','datestatic');
            $form->setType('om_validite_fin','datestatic');
        }//fin consulter
    }

    function setOnchange(&$form,$maj) {
    //javascript controle client
        $form->setOnchange('travaux_nature','VerifNum(this)');
        $form->setOnchange('om_validite_debut','fdate(this)');
        $form->setOnchange('om_validite_fin','fdate(this)');
    }
    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("travaux_nature", 11);
        $form->setTaille("code", 10);
        $form->setTaille("libelle", 30);
        $form->setTaille("description", 30);
        $form->setTaille("om_validite_debut", 12);
        $form->setTaille("om_validite_fin", 12);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("travaux_nature", 11);
        $form->setMax("code", 10);
        $form->setMax("libelle", 100);
        $form->setMax("description", 200);
        $form->setMax("om_validite_debut", 12);
        $form->setMax("om_validite_fin", 12);
    }


    function setLib(&$form,$maj) {
    //libelle des champs
        $form->setLib('travaux_nature',_('travaux_nature'));
        $form->setLib('code',_('code'));
        $form->setLib('libelle',_('libelle'));
        $form->setLib('description',_('description'));
        $form->setLib('om_validite_debut',_('om_validite_debut'));
        $form->setLib('om_validite_fin',_('om_validite_fin'));
    }

    function setSelect(&$form, $maj,&$db,$debug) {
        if(file_exists ("../sql/".$db->phptype."/".$this->table.".form.inc.php"))
            include ("../sql/".$db->phptype."/".$this->table.".form.inc.php");
        elseif(file_exists ("../sql/".$db->phptype."/".$this->table.".form.inc"))
            include ("../sql/".$db->phptype."/".$this->table.".form.inc");
    }// fin select

    //==================================
    // sous Formulaire  [subform]
    //==================================

    function setValsousformulaire(&$form,$maj,$validation,$idxformulaire,$retourformulaire,$typeformulaire,&$db,$DEBUG=null){
        $this->retourformulaire = $retourformulaire;
    }// fin setValsousformulaire

    //==================================
    // cle secondaire  [secondary key]
    //==================================
    /**
     * Methode clesecondaire
     */
    function cleSecondaire($id, &$db = NULL, $val = array(), $DEBUG = false) {
        // On appelle la methode de la classe parent
        parent::cleSecondaire($id, $db, $val, $DEBUG);
        // Verification de la cle secondaire : travaux
        $this->rechercheTable($db, "travaux", "naturetravaux", $id);
        // Verification de la cle secondaire : travaux_archive
        $this->rechercheTable($db, "travaux_archive", "naturetravaux", $id);
    }


}// fin classe
?>