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


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


class om_actions extends om_actions_gen {


    function init_class_actions() {
        parent::init_class_actions();
		// modifier
        //$this->class_actions[1] = array(); // pas de méthode modifier
		
		// generer
		$this->class_actions[4] = array(
			"identifier" => "generer_etat",
			"portlet" => array(
				"type" => "action-direct-with-confirmation",
				"libelle" => _("générer un état "),
				"order" => 40,
				"class" => "copy-16",
				),
            "method" => "generer_etat",
            "permission_suffix" => "om_actions",
            //"condition" => array(
            //    "is_valider")
			); 
	}




   function setType(&$form,$maj) {
        parent::setType($form, $maj);       
        if($maj==0){
            $form->setType("parametres", "textarea"); 
			$form->setType("module", "select"); 
        } 
        if($maj==1){
            $form->setType("parametres", "textarea"); 
			$form->setType("module", "select"); 
		}	
    }

	function setSelect(&$form, $maj, &$dnu1 = NULL, $dnu2 = NULL) {
        parent :: setSelect($form, $maj, $dnu1, $dnu2);
        if($maj<2){
            // module
            $contenu=array();
            $contenu[0]=array('','edition','ajouter',
								'modifier','supprimer');
            $contenu[1]=array(_('choisir un module'),_('edition'),_('ajouter'),
								_('modifier'),_('supprimer'));
            $form->setSelect("module",$contenu);
		}
	}

	function generer_etat(){
		// initialisation
		$idx=$this->getParameter('idx');
		$obj = $this->getParameter('idxformulaire');
		$variable = '&';
		// recherche des champs
        $sql = "select column_name "; 
        $sql .= " from ".DB_PREFIXE."om_forms ";
        $sql .= " where table_name ='".$obj."'";
        $res = $this->f->db->query($sql);
        $this->f->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);
        $this->f->isDatabaseError($res2);
		// sql
		$t_etat='';
		$t_requete="";
		while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) {
			$t=explode(".",$row['column_name']);
			$t_requete .= $row['column_name']." AS ".$t[1]." ,";
			$t_etat .= "[".$t[1]."]".chr(13).chr(10);
		}
		$t_requete  = substr($t_requete, 0, strlen($t_requete)-1);
		$t_etat  = substr($t_etat, 0, strlen($t_etat)-1);
		// om_requete
		$this->f->db->autoCommit(false);
		require_once "../core/obj/om_requete.class.php";
        $om_requete = new om_requete_core("]", $this->f->db, $DEBUG);

		//
		$val = array(
			"om_requete" => "",
			"code" => $obj."_".$idx,
			"libelle" => __("Requete")." '".$obj."_".$idx."'",
			"description" => "créé le ".date('d/m/Y'),
			"type" => "sql",
			"requete" => "select ".$obj.".".$obj.", ".$t_requete.
						 " from &DB_PREFIXE".$obj.
						 " where ".$obj.".".$obj."='".$variable."idx'",
			"classe" => "",
			"methode" => "",
			"merge_fields" => "",
		);
		//
		$ret = $om_requete->ajouter($val, $this->f->db, null);
		if ($ret !== true) {
			//
			$this->addToMessage(__("Erreur lors de l'ajout de la requête. Contactez votre administrateur."));
			return "";
		}
		$this->addToMessage(__("création de requête")." ".$om_requete->valF[$om_requete->clePrimaire]);
		// etat
		require_once "../core/obj/om_etat.class.php";
        $om_etat = new om_etat_core("]", $this->f->db, $DEBUG);	
        //	
        $etat = $om_etat->get_default_values();
		$etat['om_sql'] = $om_requete->valF[$om_requete->clePrimaire];
		$etat['titre_om_htmletat'] = "le ".$variable."aujourdhui";
		$etat['corps_om_htmletatex'] = $t_etat;
		// id
		$etat['id'] = $obj."_".$idx;
		$etat['libelle'] = $obj."_".$idx." no code le ".date('d/m/Y');
		$etat['actif'] = false;
		// om_collectivite
		$etat['om_collectivite'] = $_SESSION['collectivite'];
		$etat['om_etat'] = "";
		//
		$ret = $om_etat->ajouter($etat, $this->f->db, null);
		if ($ret !== true) {
			//
			$this->f->addToLog(__METHOD__."(): ".$om_etat->msg);
			$this->f->displayMessage("error", __("Erreur lors de l'ajout de l'état. Contactez votre administrateur."));
			return "";
		}
		$this->addToMessage(__("création d'état")." ".$om_etat->valF[$om_etat->clePrimaire]);
		$this->f->db->commit();		
	}





}
