#!/usr/bin/php
<?php
/**
 *
 *                       ╔═╦═╦═╦╦╦═╦╦═╦═╗  ╔═╦╦╦═╗
 *                       ║║║║║═╣╔╣║║║╔╣═╣  ║═╣╔╣║║
 *                       ║║║║║═╣║║║║║╚╣═╣╔╗║═╣║║╔╝ ╔════╗
 *                       ╚╩╩╩╩═╩╝╚╩╩╩═╩═╝╚╝╚═╩╝╚╝  ╚2016╝
 *
 *
 *
 *
 * @version mernice-erpV1.0-PHP5
 * @author Mernice Technologie <contact@mernice.ma>
 *
 * version 5 de PHP,
 * ce fichier source fait objet de la version 1.0 ,
 * date creation -- GMT --
 * date modification --
 */

echo "\n";
echo "                       ╔═╦═╦═╦╦╦═╦╦═╦═╗  ╔═╦╦╦═╗\n";
echo "                       ║║║║║═╣╔╣║║║╔╣═╣  ║═╣╔╣║║\n";
echo "                       ║║║║║═╣║║║║║╚╣═╣╔╗║═╣║║╔╝ ╔════╗\n";
echo "                       ╚╩╩╩╩═╩╝╚╩╩╩═╩═╝╚╝╚═╩╝╚╝  ╚2016╝\n";
echo " \n";
echo " Mernice Tools Version Dev.\n";
echo " Generation Code frameWork Mernice ERP\n";
echo " \n";
echo " \n";
echo " \n";

date_default_timezone_set('Africa/Casablanca');

define('LOG_SCREEN', true);
define('DOC_ROOT', "/var/www/campus/synchrone/");

include_once DOC_ROOT."lib/smarty/libs/Smarty.class.php";
include_once "function_generate.php";
include_once "Meta_PDO.php";


$CONFIG["DB"]   = require_once DOC_ROOT."/config/db.php";
$CONFIG["CORE"]  = require_once DOC_ROOT."/config/generate.php";
$CONFIG["CORE"]["modeles"] .= '/'.$CONFIG["DB"]["ENGINE"].'/';

$CONFIG["TEMPLATE"] = new Smarty();
$CONFIG["TEMPLATE"]->setTemplateDir($CONFIG["CORE"]["templatedir"]);
$CONFIG["TEMPLATE"]->setCompileDir($CONFIG["CORE"]["compiledir"]);
$CONFIG["TEMPLATE"]->setCacheDir($CONFIG["CORE"]["cachedir"]);
$CONFIG["TEMPLATE"]->setConfigDir($CONFIG["CORE"]["configdir"]);
$CONFIG["TEMPLATE"]->error_reporting = E_ALL & ~E_NOTICE;
$CONFIG["TEMPLATE"]->caching = false;


/*
 * ARGUMENT PHP CLI
 */
if( count($argv) <= 1 ) die("ARGUMENTS NOT FOUND !\n");

$commande = $argv[1];
$extract = array();
for($i=2; $i < count($argv); $i++)
{
	list($paramName, $paramValue) = explode(":", $argv[$i]);
	$extract[$paramName] = $paramValue;
}

extract($extract);

try
{

	if(LOG_SCREEN) echo "Start generation \n";
	if(LOG_SCREEN) echo "---------------------------------------------------------\n";
	$metaData = require("schemaDB.php");
	$CONFIG["TEMPLATE"]->assign("metaData", $metaData);
	$CONFIG["METADATA"] = $metaData;


		if($commande == "schema")
		{
			if(isset($dbname))
				$db = trim($dbname);
			else
				$db=$CONFIG["DB"]["BASE"];

				$META_DATABASE = create_schema_db($db, $CONFIG);
				file_put_contents('schemaDB.php', "<?php \n/**\n * GENERATE $db at ".date("d-m-Y H:i:s")."\n */\n\nreturn ".var_export($META_DATABASE, TRUE).";");

		}
		/**
		 *
		 */
		if($commande == "application")
		{

			if(isset($table))
				$table = trim($table);
			else
				die("table name not found !");

				if(isset($module))
					$module = trim($module);
				else
					die("module name not found !");

				$CONFIG["TEMPLATE"]->assign("tableName", $table)
													 ->assign("module", $module)
													 ->assign("fields", $metaData["tables"][$table])
													 ->assign("date_generate", date("d-m-Y H:i:s"));

				flush_view_form_new($CONFIG, $module, $table);

				flush_view_form_update($CONFIG, $module, $table);

				flush_module_view_new($CONFIG, $module, $table);

				flush_module_view_update($CONFIG, $module, $table);

				flush_module_data_new($CONFIG, $module, $table);

				flush_module_data_update($CONFIG, $module, $table);

				flush_module_data_delete($CONFIG, $module, $table);

				flush_model_new($CONFIG, $module, $table);

				flush_model_update($CONFIG, $module, $table);

				flush_model_getbyid($CONFIG, $module, $table);

				flush_model_delete($CONFIG, $module, $table);

				flush_module_data_combo($CONFIG, $module, $table);

				flush_model_combo($CONFIG, $module, $table);

				flush_module_data_combogrid($CONFIG, $module, $table);

				flush_model_combogrid($CONFIG, $module, $table);

				flush_config_table($CONFIG, $module, $table);

				flush_module_config_table($CONFIG, $module, $table);

				flush_module_assocconfig_table($CONFIG, $module, $table);


				flush_view_datagrid($CONFIG, $module, $table);

				flush_module_view_datagrid($CONFIG, $module, $table);

				flush_model_getdatagrid($CONFIG, $module, $table);

				flush_module_data_datagrid($CONFIG, $module, $table);

				flush_module_container_new($CONFIG, $module, $table);

				flush_module_container_update($CONFIG, $module, $table);

				flush_module_container_datagrid($CONFIG, $module, $table);

				flush_view_container_new($CONFIG, $module, $table);

				flush_view_container_update($CONFIG, $module, $table);

				flush_view_container_datagrid($CONFIG, $module, $table);

				flush_module_data_assoc($CONFIG, $module, $table);

				flush_model_assoc($CONFIG, $module, $table);

				echo "\nDONE :)\n\n";


		}


		if($commande == "form")
		{

			if(isset($table))
				$table = trim($table);
			else
				die("table name not found !");

				if(isset($module))
					$module = trim($module);
				else
					die("module name not found !");

				$CONFIG["TEMPLATE"]->assign("tableName", $table)
													 ->assign("module", $module)
													 ->assign("fields", $metaData["tables"][$table])
													 ->assign("date_generate", date("d-m-Y H:i:s"));

				flush_view_form_new($CONFIG, $module, $table);

				flush_view_form_update($CONFIG, $module, $table);

				flush_module_view_new($CONFIG, $module, $table);

				flush_module_view_update($CONFIG, $module, $table);

				flush_module_data_new($CONFIG, $module, $table);

				flush_module_data_update($CONFIG, $module, $table);

				flush_module_data_delete($CONFIG, $module, $table);

				flush_model_new($CONFIG, $module, $table);

				flush_model_update($CONFIG, $module, $table);

				flush_model_getbyid($CONFIG, $module, $table);

				flush_model_delete($CONFIG, $module, $table);

				flush_module_data_combo($CONFIG, $module, $table);

				flush_model_combo($CONFIG, $module, $table);

				flush_module_data_combogrid($CONFIG, $module, $table);

				flush_model_combogrid($CONFIG, $module, $table);

				flush_config_table($CONFIG, $module, $table);

				flush_module_config_table($CONFIG, $module, $table);

				flush_module_assocconfig_table($CONFIG, $module, $table);

				echo "\nDONE :)\n\n";


		}

		if($commande == "datagrid")
		{

			if(isset($table))
				$table = trim($table);
			else
				die("table name not found !");

				if(isset($module))
					$module = trim($module);
				else
					die("module name not found !");

			$CONFIG["TEMPLATE"]->assign("tableName", $table)
												 ->assign("module", $module)
												 ->assign("fields", $metaData["tables"][$table])
												 ->assign("date_generate", date("d-m-Y H:i:s"));


			flush_view_datagrid($CONFIG, $module, $table);

			flush_module_view_datagrid($CONFIG, $module, $table);

			flush_model_getdatagrid($CONFIG, $module, $table);

			flush_module_data_datagrid($CONFIG, $module, $table);

			flush_module_container_new($CONFIG, $module, $table);

			flush_module_container_update($CONFIG, $module, $table);

			flush_module_container_datagrid($CONFIG, $module, $table);

			flush_view_container_new($CONFIG, $module, $table);

			flush_view_container_update($CONFIG, $module, $table);

			flush_view_container_datagrid($CONFIG, $module, $table);

			flush_module_data_assoc($CONFIG, $module, $table);

			flush_model_assoc($CONFIG, $module, $table);

			flush_module_data_combo($CONFIG, $module, $table);

			flush_model_combo($CONFIG, $module, $table);

			flush_module_data_combogrid($CONFIG, $module, $table);

			flush_model_combogrid($CONFIG, $module, $table);

			echo "\nDONE :)\n\n";


		}

		if($commande == "datalist")
		{

			if(isset($table))
				$table = trim($table);
			else
				die("table name not found !");

				if(isset($module))
					$module = trim($module);
				else
					die("module name not found !");

			$CONFIG["TEMPLATE"]->assign("tableName", $table)
												 ->assign("module", $module)
												 ->assign("fields", $metaData["tables"][$table])
												 ->assign("date_generate", date("d-m-Y H:i:s"));


				flush_view_datalist($CONFIG, $module, $table);

				flush_module_view_datalist($CONFIG, $module, $table);

				flush_model_getdatalist($CONFIG, $module, $table);

				flush_module_data_datalist($CONFIG, $module, $table);

				flush_module_container_datalist($CONFIG, $module, $table);

				flush_view_container_datalist($CONFIG, $module, $table);

				echo "\nDONE :)\n\n";

		}

		if($commande == "panel")
		{

			if(isset($table))
				$table = trim($table);
			else
				die("table name not found !");

			if(isset($module))
				$module = trim($module);
			else
				die("module name not found !");

			if(isset($href))
				$href = trim($href);
			else
				die("module name not found !");


			$CONFIG["TEMPLATE"]->assign("tableName", $table)
												 ->assign("module", $module)
												 ->assign("title", $title)
												 ->assign("href", $href)
												 ->assign("date_generate", date("d-m-Y H:i:s"));


			flush_view_container_panel($CONFIG, $module, $table);

			flush_module_container_panel($CONFIG, $module, $table);

			echo "\nDONE :)\n\n";

		}


		if($commande == "layout")
		{

			if(isset($module))
				$module = trim($module);
			else
				die("module name not found !");

			if(isset($name))
				$name = trim($name);
			else
				die("name not found !");

			if(isset($north))
				$north = trim($north);
			else
				$north = "";

			if(isset($south))
				$south = trim($south);
			else
				$south = "";

			if(isset($east))
				$east = trim($east);
			else
				$east = "";

			if(isset($west))
				$west = trim($west);
			else
				$west = "";

			if(isset($center))
				$center = trim($center);
			else
				$center = "";

			$CONFIG["TEMPLATE"]->assign("module",   $module)
												 ->assign("north",    $north)
												 ->assign("south",    $south)
												 ->assign("east",     $east)
												 ->assign("west",     $west)
												 ->assign("center",   $center)
											   ->assign("date_generate", date("d-m-Y H:i:s"));


			flush_view_container_layout($CONFIG, $module, $name);

			flush_module_container_layout($CONFIG, $module, $name);

			echo "\nDONE :)\n\n";

		}



	if($commande == "flush")
	{

		if(isset($module))
			$module = trim($module);
		else
			die("module name not found !");

		if(isset($module))
			$module = trim($module);
		else
			die("template name not found !");

			$CONFIG["TEMPLATE"]->assign("tableName", isset($table)?$table:"")
												 ->assign("module", $module)
												 ->assign("fields", isset($metaData["tables"][$table])?  $metaData["tables"][$table] : array())
												 ->assign("date_generate", date("d-m-Y H:i:s"));

			$function_name = 'flush_'.$template;

			if(is_callable($function_name))
				$function_name($CONFIG, $module, $table);
			else
				die("la fonction de génération de code : ".'flush_'.$template." n'est pas correcte !\n");

	}



}
catch(Exception $e)
{
	if(LOG_SCREEN)
		echo "ERROR : ".$e->getMessage()."\n";
	else
		throw new Exception($e->getMessage());
}
