<?php
//$Id$ 
//gen openMairie le 25/04/2024 13:51


require_once "../gen/obj/om_vues.class.php";


class om_vues extends om_vues_gen {

   function setType(&$form,$maj) {
        parent::setType($form, $maj);       
        if($maj==0){
            $form->setType("table_schema", "hiddenstatic"); 
        } 
        if($maj==1){
            $form->setType("table_schema", "hiddenstatic"); 
		}	
    }

    function ajouter($val = array(), &$dnu1 = null, $dnu2 = null) {		
        // Begin
        $this->begin_treatment(__METHOD__);
        // Mutateur de valF
        $this->setValF($val);
        // verifier
        // si le nom de la table n'existe pas
        $this->verifier($val, $this->f->db, null);
        //$this->setValFAjout($val); -> ne marche pas et pas de remontée de messages -> mis en verifier()
        if ($this->correct) {
			// creation de la table + clé primaire			
			$sql  = "create or replace view ".DB_PREFIXE.$this->valF['table_name']." as ";
			$sql .= $this->valF['view_definition']." ";			 
			$res = $this->f->db->query($sql);			
			if ($this->f->isDatabaseError($res, true)) {
				// Appel de la methode de recuperation des erreurs
				$this->addToMessage($this->valF['table_name']." ".__("vue non créé")." ".$sql);
				$this->addToMessage($res->getDebugInfo());
				$this->correct = false;
				// Return
				return $this->end_treatment(__METHOD__, false);
			}else{
				$this->addToMessage("creation table ".$this->valF['table_name']);
			}
		}else {
            $this->addToMessage("<br/>".__("vue non créé")."<br/>");
            $this->addToLog(__METHOD__."(): ".$message, VERBOSE_MODE);
            // Return
            return $this->end_treatment(__METHOD__, false);
        }	
        return $this->end_treatment(__METHOD__, true);
	}

    function modifier($val = array(), &$dnu1 = null, $dnu2 = null) {		
        // Begin
        $this->begin_treatment(__METHOD__);
        // Mutateur de valF
        $this->setValF($val);
        // verifier
        // si le nom de la table n'existe pas
        $this->verifier($val, $this->f->db, null);
        //$this->setValFAjout($val); -> ne marche pas et pas de remontée de messages -> mis en verifier()
        if ($this->correct) {
			// creation de la table + clé primaire			
			$sql  = "create or replace view ".DB_PREFIXE.$this->valF['table_name']." as ";
			$sql .= $this->valF['view_definition']." ";			 
			$res = $this->f->db->query($sql);			
			if ($this->f->isDatabaseError($res, true)) {
				// Appel de la methode de recuperation des erreurs
				$this->addToMessage($this->valF['table_name']." ".__("vue non créé")." ".$sql);
				$this->addToMessage($res->getDebugInfo());
				$this->correct = false;
				// Return
				return $this->end_treatment(__METHOD__, false);
			}else{
				$this->addToMessage("modification vue ".$this->valF['table_name']);
			}
		}else {
            $this->addToMessage("<br/>".__("vue non modifiée")."<br/>");
            $this->addToLog(__METHOD__."(): ".$message, VERBOSE_MODE);
            // Return
            return $this->end_treatment(__METHOD__, false);
        }	
        return $this->end_treatment(__METHOD__, true);
	}

    function supprimer($val = array(), &$dnu1 = null, $dnu2 = null) {		
        // Begin
        $this->begin_treatment(__METHOD__);
        // Mutateur de valF
        $this->setValF($val);
        if ($this->correct) {
			// creation de la table + clé primaire			
			$sql  = "drop view ".DB_PREFIXE.$this->valF['table_name']."  ";		 
			$res = $this->f->db->query($sql);			
			if ($this->f->isDatabaseError($res, true)) {
				// Appel de la methode de recuperation des erreurs
				$this->addToMessage($this->valF['table_name']." ".__("vue non supprimmée")." ".$sql);
				$this->addToMessage($res->getDebugInfo());
				$this->correct = false;
				// Return
				return $this->end_treatment(__METHOD__, false);
			}else{
				$this->addToMessage("suppression  ".$this->valF['table_name']);
			}
		}else {
            $this->addToMessage("<br/>".__("vue non supprimée")."<br/>");
            $this->addToLog(__METHOD__."(): ".$message, VERBOSE_MODE);
            // Return
            return $this->end_treatment(__METHOD__, false);
        }	
        return $this->end_treatment(__METHOD__, true);
	}




}
