<?php
/**
     * MiniClick
     * Gestionnaire de tableau collaboratif
     * Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
	     *
	     * This program is free software; you can redistribute it and/or modify
	     * it under the terms of the GNU General Public License as published by
	     * the Free Software Foundation; either version 2 of the License, or
	     * any later version.
	     *
	     * This program is distributed in the hope that it will be useful,
	     * but WITHOUT ANY WARRANTY; without even the implied warranty of
	     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	     * GNU General Public License for more details.
	     *
	     * You should have received a copy of the GNU General Public License
	     * along with this program; if not, write to the Free Software
	     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
	     *
	     * @package miniclick
	     * @version 3.1.0
*/
function create_default_config($table){
                $creates = mysql_query("SELECT * FROM CONFIG WHERE Table_Id='$table' ORDER BY Position");
                if(!$create = mysql_fetch_object($creates)){
                        $fields = mysql_query("DESCRIBE $table");
                        $pos = 0;
                        $key = "";
                        while($field = mysql_fetch_object($fields)){
                                $pos++;
                                if($field->Key == "PRI"){
                                        $key = "true";
                                }else{
                                        $key = "false";
                                }
                                mysql_query("INSERT INTO `CONFIG` (`Config_Id`, `Table_Id`, `Colonne_Id`, `Label`, `Position`, `IsKey`, `Type`, `Value`, `Size`, `Total`, `Wf`, `Defaut`, `Afficher`)  VALUES('','$table','".$field->Field."','".$field->Field."','$pos','$key','INPUT','','10','false','false','','true')");
				echo "Creation de la config table ".$nom_table."<br>";
				echo "INSERT INTO `CONFIG` (`Config_Id`, `Table_Id`, `Colonne_Id`, `Label`, `Position`, `IsKey`, `Type`, `Value`, `Size`, `Total`, `Wf`, `Defaut`, `Afficher`)  VALUES('','$table','".$field->Field."','".$field->Field."','$pos','$key','INPUT','','10','false','false','','true')<br>";
			

                        }
                }
}
function create_default_droit($table){
        $creates = mysql_query("SELECT * FROM DROIT WHERE Table_Id='$table'");
        if(!$create = mysql_fetch_object($creates)){
		$fields = mysql_query("DESCRIBE $table");
		while($field = mysql_fetch_object($fields)){
			mysql_query("INSERT INTO `DROIT` (`Droit_Id`, `Table_Id`, `Colonne_Id`, `Role_Id`, `Read_Perm`, `Write_Perm`, `Workflow_Perm`) VALUES('','$table','".$field->Field."','ADMIN','on','on','on')");
			echo "Creation des droits table ".$nom_table."<br>";
			echo "INSERT INTO `DROIT` (`Droit_Id`, `Table_Id`, `Colonne_Id`, `Role_Id`, `Read_Perm`, `Write_Perm`, `Workflow_Perm`)  VALUES('','$table','".$field->Field."','ADMIN','on','on','on')<br>";
		}
	}
}
function create_default_vue($table){
        $creates = mysql_query("SELECT * FROM VUE WHERE Table_Id='$table' ");
        if(!$create = mysql_fetch_object($creates)){
		mysql_query("INSERT INTO `VUE` (`Vue_Id`, `Nom`, `Table_Id`, `Rows`, `Condition`, `Defaut`, `Ordre`) VALUES ('', 'Tout', '".$table."', '*', '', 1, 1)");
		echo "Creation de la vue de la table ".$nom_table."<br>";
		echo "INSERT INTO `VUE` (`Vue_Id`, `Nom`, `Table_Id`, `Rows`, `Condition`, `Defaut`, `Ordre`) VALUES ('', 'Tout', '".$table."', '*', '', 1, 1)<br>";

	}
}

//Nouveau Module
include (MINICLICK_INSTALL_PATH."includes/connection.php");
include(MINICLICK_INSTALL_PATH."engine/miniclic/miniclick.class.php");
//ho $_GET["newmodule"]." ".$_GET["App_Id"]." ".$_GET["Access_Id"];
if ((isset($_GET["newmodule"])) && (isset($_GET["App_Id"]))&& (isset($_GET["Access_Id"]))){
	//récupération des données de la ligne (si données vide -> erreur)
	$sql_query="SELECT Nom, Host, Db, Role, User_Id FROM MINICLICK.APP WHERE App_Id=".$_GET['App_Id'];
	echo $sql_query;
	$rq=mysql_query($sql_query);
	$app=mysql_fetch_object($rq);

	//connexion a la base de données de l'app
	$_SESSION['current_db']=$app->Db;
	$_SESSION['database']=$app->Db;
	$base=mysql_select_db($app->Db);
	
	if (($app->Nom!="") && ($app->Host!="") && ($app->Db!="") && ($app->Role!="") && ($app->User_Id!="")){
		$sql_module="SELECT Lien, Module FROM ".$app->Db.".ACCESS WHERE Access_Id=".$_GET["Access_Id"];
		echo $sql_module." ".$module->Lien." ".$module->Module; 
		$rq_module=mysql_query($sql_module);
		$module=mysql_fetch_object($rq_module);
		$dossier_module=explode('/',$module->Lien);
		if ($module->Module{strlen($module->Module)-1}=="s"){
			$nom_table=substr($module->Module, 0, strlen($module->Module)-1);
		}else{
			$nom_table=$module->Module;
		}	
		if (($module->Lien!="") && ($module->Module!="")){
			//verification existence du dossier (si existence -> avertissement)
			if (!is_dir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/modules/".$dossier_module[0])){
				echo "Création d'un dossier "."apps/".$app->Nom."/modules/".$dossier_module[0]."<br>";
				mkdir(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/modules/".$dossier_module[0]);
			}
			if (!is_file(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/modules/".$module->Lien)){
				$defaut = fopen(MINICLICK_INSTALL_PATH."apps/".$app->Nom."/modules/".$module->Lien,"w+");
				fputs($defaut,'<?php
				include("engine/miniclic/miniclick.class.php");
				$mini = new miniclick("'.strtoupper($nom_table).'");
				?>');
				fclose($defaut);
				echo "Creation et ecriture du fichier  "."apps/".$app->Nom."/modules/".$module->Lien."<br>";
				chmod (MINICLICK_INSTALL_PATH."apps/".$app->Nom."/modules/".$module->Lien, 0755);
			}

			//création de la table
			mysql_query("CREATE TABLE `".strtoupper($nom_table)."` (`".ucfirst($nom_table)."_Id` INT NOT NULL AUTO_INCREMENT ,PRIMARY KEY ( `".ucfirst($nom_table)."_Id` )) TYPE = MYISAM ;");
			echo "Creation de la table ".strtoupper($nom_table)."<br>";

			//config par défaut pour la table
			create_default_config(strtoupper($nom_table));
			create_default_droit(strtoupper($nom_table));
			create_default_vue(strtoupper($nom_table));

			//ajout de l'admin sur ce module
			$sql_acc="INSERT INTO ACCESS ( `Access_Id` , `App_Id` , `Module` , `Lien` ,`Position` , `User_Id` ) VALUES ('', '".$app->Nom."', '".$module->Module."', '".$Module->Lien."', '1', '1')";
			mysql_query($sql_acc);
			echo $sql_acc."<br>";
			

		}else{
			echo "Informations incompletes sur le module";
		}		
	}else{
		echo "Informations sur l'application incomplètes ";
	}
}

?>
