Max YO 6 gadi atpakaļ
vecāks
revīzija
ac0c1d9c4f

+ 51 - 0
bin/karmafw.php

@@ -0,0 +1,51 @@
+<?php
+
+function help($exit=false) {
+	echo "Usage: php karmakw.php <action> [options]" . PHP_EOL;
+	echo PHP_EOL;
+	echo "  actions:" . PHP_EOL;
+	echo "     new-controller <item_name_single> <item_name_plural>" . PHP_EOL;
+	echo "     new-model <item_name_single> <item_name_plural>" . PHP_EOL;
+	echo "     new-migration <migration_name>" . PHP_EOL;
+	echo PHP_EOL;
+	echo "  example:" . PHP_EOL;
+	echo "     php karmakw.php new-controller user users" . PHP_EOL;
+	echo PHP_EOL;
+
+	if ($exit) {
+		exit();
+	}
+}
+
+function pre($var, $exit = false, $prefix = '') {
+	echo "<pre>";
+	if (!empty($prefix)) {
+		echo $prefix;
+	}
+	if (is_null($var)) {
+		echo "NULL";
+	} else if ($var === true) {
+		echo "TRUE";
+	} else if ($var === false) {
+		echo "FALSE";
+	} else if (is_string($var)) {
+		echo '"' . $var . '"';
+	} else {
+		print_r($var);
+	}
+	echo "</pre>";
+
+	if ($exit) {
+		exit;
+	}
+}
+
+
+/* ###### */
+
+if (empty($argv[1])) {
+	help(true);
+}
+
+$action = $argv[1];
+

+ 6 - 1
helpers/helpers_array.php

@@ -96,7 +96,12 @@ if (! function_exists('get_csv')) {
 			foreach ($arr as $row) {
 				$line = array();
 				foreach ($fields as $field) {
-					$line[] = $row[$field];
+					$val = $row[$field];
+					if (is_numeric($val) && substr($val."", 0, 1) === "0" && strlen($val."") > 1) {
+						// pour exporter correctement dans Excel les numeros de telephone commencant par 0
+						$val = '="' . $val . '"';
+					}
+					$line[] = $val;
 				}
 				//$str .= implode($sep, $line) . PHP_EOL;
 				//$str .= '"' . implode('"' . $sep . '"', str_replace('"', '\\"', $line)) . '"' . PHP_EOL;

+ 9 - 0
helpers/helpers_default.php

@@ -161,3 +161,12 @@ if (! function_exists('getRouteUrl')) {
 		return Router::getRouteUrl($route_name, $urls_args);
 	}
 }
+
+
+if (! function_exists('dateUstoFr')) {
+	function dateUstoFr($date_us) {
+	    $date_us = substr($date_us, 0, 10);
+	    $parts = explode('-', $date_us);
+	    return implode('/', array_reverse($parts));
+	}
+}

+ 5 - 3
src/App.php

@@ -29,7 +29,9 @@ class App
 		HooksManager::applyHook('app_boot__before', []);
 
 		// start session
-		session_start();
+		if (empty(session_id())) {
+			session_start();
+		}
 
 		// move fw_helpers at the end of the list (to be loaded the last one)
 		if (count(self::$helpers_dirs) > 1) {
@@ -137,9 +139,9 @@ class App
 	}
 
 
-	public static function createTemplate($tpl_dir=null, $default_vars=[])
+	public static function createTemplate($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
 	{
-		return new PhpTemplate($tpl_dir, $default_vars);
+		return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
 	}
 
 

+ 4 - 1
src/Database/Sql/Drivers/Mysqli/MysqliResultset.php

@@ -27,7 +27,10 @@ class MysqliResultset extends SqlResultset implements SqlResultsetInterface
 
 	public function fetchOne()
 	{
-		if (empty($this->rs)) {
+		if (is_bool($this->rs)) {
+			//debug_print_backtrace();
+		}
+		if (empty($this->rs) || $this->rs === true) {
 			return [];
 		}
 		return mysqli_fetch_assoc($this->rs);

+ 8 - 3
src/Database/Sql/SqlDb.php

@@ -143,17 +143,22 @@ class SqlDb
 		return $this->createQuery()->executeSelectOne($sql, $params);
 	}
 
-	public function executeInsert($query, $params=[])
+	public function executeSelectValue($sql, $column_name, $params=[])
+	{
+		return $this->createQuery()->executeSelectValue($sql, $column_name, $params);
+	}
+
+	public function executeInsert($sql, $params=[])
 	{
 		return $this->createQuery()->executeInsert($sql, $params);
 	}
 
-	public function executeUpdate($query, $params=[])
+	public function executeUpdate($sql, $params=[])
 	{
 		return $this->createQuery()->executeUpdate($sql, $params);
 	}
 
-	public function executeDelete($query, $params=[])
+	public function executeDelete($sql, $params=[])
 	{
 		return $this->createQuery()->executeDelete($sql, $params);
 	}

+ 15 - 2
src/Database/Sql/SqlQuery.php

@@ -85,7 +85,7 @@ class SqlQuery
 	}
 
 
-	public function execute($query=null, $params=[])
+	public function execute($query=null, $params=[], $nb_tries=1)
 	{
 		if (! $this->db->isConnected()) {
 			$this->db->connect();
@@ -137,6 +137,14 @@ class SqlQuery
 			$this->error = $error_msg;
 			$this->status = 'error';
 
+			if ($error_code == 2006) {
+				// MySQL server has gone away
+				if ($nb_tries > 1) {
+					sleep(1);
+					return $this->execute($query, $params, $nb_tries-1);
+				}
+			}
+
 			if ($this->db->throwOnSqlError) {
 				throw new \Exception('SqlQuery::execute() => DB error [' . $error_code . '] : ' . $error_msg . PHP_EOL . '[QUERY: ' . preg_replace('/\s+/', ' ', $query) . ' ]');
 			}
@@ -166,6 +174,11 @@ class SqlQuery
 		return $this->execute($query, $params)->fetchOne();
 	}
 
+	public function executeSelectValue($query, $column_name, $params=[])
+	{
+		return $this->execute($query, $params)->fetchColumn($column_name);
+	}
+
 	public function executeSelectAll($query, $params=[])
 	{
 		return $this->execute($query, $params)->fetchAll();
@@ -175,7 +188,7 @@ class SqlQuery
 	public function executeInsert($query, $params=[])
 	{
 		$this->execute($query, $params);
-		return $this->insert_id();
+		return $this->db->getInsertId();
 	}
 	
 

+ 12 - 1
src/Database/Sql/SqlTable.php

@@ -73,7 +73,12 @@ class SqlTable
 
 		$inserts_sql = implode(', ', $values_array);
 
-		$query = "insert into " . $this->table_name . " (" . $fields_sql . ") values " . $inserts_sql;
+		$ignore_sql = empty($options['ignore']) ? '' : 'ignore';
+
+		$on_duplicate_key_updates_sql = empty($options['on_duplicate_key_updates']) ? "" : ("on duplicate key update " . $options['on_duplicate_key_updates']);
+		// TODO: gerer on_duplicate_key_updates comme le where, dans un tableau et non dans un string
+
+		$query = "insert " . $ignore_sql . " into " . $this->table_name . " (" . $fields_sql . ") values " . $inserts_sql . " " . $on_duplicate_key_updates_sql;
 		return $this->db->createQuery()->executeInsertAll($query);
 	}
 
@@ -157,8 +162,13 @@ class SqlTable
 			// alias "order by" to "order_by"
 			$options['order_by'] = $options['order by'];
 		}
+		if (empty($options['group_by']) && ! empty($options['group by'])) {
+			// alias "group by" to "group_by"
+			$options['group_by'] = $options['group by'];
+		}
 
 		$limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
+		$group_by_sql = isset($options['group_by']) ? ("group by " . $options['group_by']) : "";
 		$order_by_sql = isset($options['order_by']) ? ("order by " . $options['order_by']) : "";
 		$having_sql = isset($options['having']) ? ("having " . $options['having']) : "";
 		$table_name = isset($options['from']) ? $options['from'] : $this->table_name;
@@ -184,6 +194,7 @@ class SqlTable
 					" . $joins_sql . "
 					where " . $this->db->buildSqlWhere($where) . "
 					" . $having_sql . "
+					" . $group_by_sql . "
 					" . $order_by_sql . "
 					" . $limit_sql;
 

+ 101 - 46
src/Templates/PhpTemplate.php

@@ -5,69 +5,107 @@ namespace KarmaFW\Templates;
 
 class PhpTemplate
 {
-	public $tpl_dir = APP_DIR . '/templates';
-	protected $vars = [];
-	protected $plugins = [];
+	protected $tpl_path = null;
+	protected $variables = [];
 	protected $layout = null;
+	protected $plugins = [];
+	public $templates_dirs = APP_DIR . '/templates';
+
+
+	public static function createTemplate($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
+	{
+		return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
+	}
+
 
-	function __construct($tpl_dir=null, $default_vars=[])
+	function __construct($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
 	{
-		if (is_null($tpl_dir) && defined('TPL_DIR')) {
-			$tpl_dir = TPL_DIR;
+		if (is_null($templates_dirs) && defined('TPL_DIR')) {
+			$templates_dirs = explode(':', TPL_DIR);
+
+		} else if (is_string($templates_dirs)) {
+			$templates_dirs = explode(':', $templates_dirs);
+
+		} else if (is_array($templates_dirs)) {
+
+		} else {
+			$templates_dirs = null;
 		}
 		
-		if (! is_null($tpl_dir)) {
-			$this->tpl_dir = $tpl_dir;
-		}
+		$this->tpl_path = $tpl_path;
+		$this->variables = is_array($variables) ? $variables : [];
+		$this->layout = $layout;
+		$this->templates_dirs = $templates_dirs;
+
 
-		$this->assign($default_vars);
+		// PLUGINS
 
 		$template = $this;
 		$this->addPlugin('layout', function ($param) use ($template) {
+			// {layout my_layout_template.tpl.php}
 			$template->layout = $param;
 			return '';
 		});
 		$this->addPlugin('\/\/', function ($param) {
+			// {// this is a comment}
+			return '';
+		});
+		$this->addPlugin('#', function ($param) {
+			// {# this is a comment}
 			return '';
 		});
 		$this->addPlugin('tr', function ($param) {
+			// {tr my text in english} ==> mon texte en francais
 			return gettext($param);
 		});
 		$this->addPlugin('include', function ($param) use ($template) {
-			$template = new PhpTemplate($template->tpl_dir, $template->vars);
+			// {include my_template.tpl.php}
+			$template = new PhpTemplate($template->templates_dirs, $template->variables);
 			$templatechild_content = $template->fetch($param);
 			return $templatechild_content;
 		});
-		$this->addPlugin('routeUrl', function ($param) use ($template) {
+		$this->addPlugin('routeUrl', function ($param) {
+			// {routeUrl login_page} ===> /login
 			$params = explode(' ', $param);
 			$route_name = array_shift($params);
 			$url_args = $params;
 			$url = getRouteUrl($route_name, $url_args);
 			return $url;
 		});
-	}
 
-	public static function createTemplate($tpl_dir=null, $default_vars=[])
-	{
-		return new PhpTemplate($tpl_dir, $default_vars);
 	}
 
-	public function fetch($tpl, $layout=null, $extra_vars=[])
+
+	public function fetch($tpl, $extra_vars=[], $layout=null, $options=[])
 	{
 		$tpl_dirs = [];
 
-		if (! is_null($this->tpl_dir) && is_dir($this->tpl_dir)) {
-			$tpl_dirs[] = $this->tpl_dir; // user templates
+		// user templates
+		if (! empty($this->templates_dirs)) {
+			foreach ($this->templates_dirs as $templates_dir) {
+				if (is_dir($templates_dir)) {
+					$tpl_dirs[] = $templates_dir;
+				}
+			}
 		}
 
+		// framework templates
 		if (is_dir(FW_DIR . '/templates')) {
-			$tpl_dirs[] = FW_DIR . '/templates'; // framework templates
+			$tpl_dirs[] = FW_DIR . '/templates';
 		}
 
 		if (empty($tpl_dirs)) {
 			throw new \Exception("No Templates dir. Please define TPL_DIR with a valid directory path.", 1);
 		}
 
+		if (empty($tpl)) {
+			$tpl = $this->tpl_path;
+		}
+		if (empty($tpl)) {
+			//throw new Exception("no template specified", 1);
+			return '';
+		}
+
 		$tpl_path = false;
 		foreach ($tpl_dirs as $tpl_dir) {
 			$tpl_path = $tpl_dir . '/' . $tpl;
@@ -83,8 +121,12 @@ class PhpTemplate
 			throw new \Exception("Template not found : " . $tpl, 1);
 		}
 		
-		$tpl_vars = array_merge($this->vars, $extra_vars);
-		extract($tpl_vars);
+		//$tpl_vars = array_merge($this->variables, $extra_vars);
+		//extract($tpl_vars);
+		if (! empty($extra_vars) && is_array($extra_vars)) {
+			$this->variables = array_merge($this->variables, $extra_vars);
+		}
+		extract($this->variables);
 		
 		if ($tpl_path) {
 			ob_start();
@@ -98,29 +140,31 @@ class PhpTemplate
 
 
 		// plugins. ex: {tr English text} ==> "Texte francais"
-		if (! empty($this->plugins)) {
-			foreach ($this->plugins as $prefix => $callback) {
-				//preg_match_all('/{' . $prefix . ':([^}]+)}/', $content, $regs, PREG_SET_ORDER);
-				preg_match_all('/{' . $prefix . ' ([^}]+)}/', $content, $regs, PREG_SET_ORDER);
-				foreach($regs as $reg) {
-					$replaced = $callback($reg[1]);
-					$content = str_replace($reg[0], $replaced, $content);
-				}
+		if (empty($options['no_plugins'])) {
+			if (! empty($this->plugins)) {
+				foreach ($this->plugins as $prefix => $callback) {
+					//preg_match_all('/{' . $prefix . ':([^}]+)}/', $content, $regs, PREG_SET_ORDER);
+					preg_match_all('/{' . $prefix . ' ([^}]+)}/', $content, $regs, PREG_SET_ORDER);
+					foreach($regs as $reg) {
+						$replaced = $callback($reg[1]);
+						$content = str_replace($reg[0], $replaced, $content);
+					}
 
+				}
 			}
-		}
 
-		// variables. ex: {$user_name} ==> John
-		if (true) {
-			preg_match_all('/{\$([a-zA-Z0-9_\[\]\']+)}/', $content, $regs, PREG_SET_ORDER);
-			foreach($regs as $reg) {
-				$var = $reg[1];
-				
-				if (isset(${$var})) {
-					$replaced = ${$var};
-					$content = str_replace($reg[0], $replaced, $content);
-				} else {
-					//$content = str_replace($reg[0], '', $content);
+			// variables. ex: {$user_name} ==> John
+			if (true) {
+				preg_match_all('/{\$([a-zA-Z0-9_\[\]\']+)}/', $content, $regs, PREG_SET_ORDER);
+				foreach($regs as $reg) {
+					$var = $reg[1];
+					
+					if (isset(${$var})) {
+						$replaced = ${$var};
+						$content = str_replace($reg[0], $replaced, $content);
+					} else {
+						//$content = str_replace($reg[0], '', $content);
+					}
 				}
 			}
 		}
@@ -137,14 +181,25 @@ class PhpTemplate
 		} else {
 			$extra_vars['child_content'] = $content;
 			//$extra_vars['child_content'] = '{CONTENT OF ' . $tpl . '}';
-			$content_layout = $this->fetch($layout, null, $extra_vars);
+			$content_layout = $this->fetch($layout, $extra_vars, null, $options);
 			return $content_layout;
 		}
 	}
 
-	public function display($tpl, $layout=null, $extra_vars=[])
+	public function display($tpl, $extra_vars=[], $layout=null, $options=[])
+	{
+		echo $this->fetch($tpl, $extra_vars, $layout, $options);
+	}
+
+
+	public function setAllVariables($variables=[])
+	{
+		$this->variables = $variables;
+	}
+
+	public function setVariable($var_name, $var_value)
 	{
-		echo $this->fetch($tpl, $layout, $extra_vars);
+		$this->variables[$var_name] = $var_value;
 	}
 
 	public function assign($var_name, $var_value=null)
@@ -156,7 +211,7 @@ class PhpTemplate
 			return $this;
 		}
 
-		$this->vars[$var_name] = $var_value;
+		$this->variables[$var_name] = $var_value;
 
 		return $this;
 	}

+ 75 - 0
src/Templates/PhpTemplater.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace KarmaFW\Templates;
+
+
+class PhpTemplater
+{
+	protected $variables = [];
+	protected $layout = null;
+	protected $plugins = [];
+	public $templates_dirs = [];
+
+
+	public function __construct($templates_dirs=null, $variables=[], $layout=null)
+	{
+		$this->setTemplateDirs($templates_dirs);
+
+		$this->variables = $variables;
+		$this->layout = $layout;
+	}
+
+
+	public function setVariables($variables=[])
+	{
+		$this->variables = $variables;
+	}
+
+	public function setAllVariables($var_name, $var_value)
+	{
+		$this->variables[$var_name] = $var_value;
+	}
+
+	public function setTemplateDirs($templates_dirs=[])
+	{
+		if (empty($templates_dirs) && defined('TPL_DIR')) {
+			$templates_dirs = explode(':', TPL_DIR);
+
+		} else if (is_string($templates_dirs)) {
+			$templates_dirs = explode(':', $templates_dirs);
+
+		} else if (is_array($templates_dirs)) {
+
+		} else {
+			$templates_dirs = [];
+		}
+
+		$this->templates_dirs = $templates_dirs;
+	}
+
+
+	public function assign($var_name, $var_value=null)
+	{
+		if (is_array($var_name)) {
+			foreach ($var_name as $k => $v) {
+				$this->assign($k, $v);
+			}
+			return $this;
+		}
+
+		$this->variables[$var_name] = $var_value;
+
+		return $this;
+	}
+
+
+
+	function tpl($tpl_path=null, $variables=[], $layout=null, $templates_dirs=[])
+	{
+		$variables = array_merge($this->variables, (is_array($variables) ? $variables : []) );
+		$templates_dirs = array_merge($this->templates_dirs, (is_array($templates_dirs) ? $templates_dirs : []) );
+
+		return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
+	}
+
+}