Max YO před 6 roky
rodič
revize
f38f3b7c6a

+ 11 - 1
helpers/helpers_default.php

@@ -9,7 +9,17 @@ if (! function_exists('pre')) {
 		if (!empty($prefix)) {
 			echo $prefix;
 		}
-		print_r($var);
+		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) {

+ 8 - 2
src/App.php

@@ -31,6 +31,12 @@ class App
 		// start session
 		session_start();
 
+		// move fw_helpers at the end of the list (to be loaded the last one)
+		if (count(self::$helpers_dirs) > 1) {
+		$fw_helpers = array_shift(self::$helpers_dirs);
+		self::$helpers_dirs[] = $fw_helpers;
+		}
+
 		// include helpers
 		foreach (self::$helpers_dirs as $helpers_dir) {
 			self::loadHelpers($helpers_dir);
@@ -131,9 +137,9 @@ class App
 	}
 
 
-	public static function createTemplate()
+	public static function createTemplate($tpl_dir=null, $default_vars=[])
 	{
-		return new PhpTemplate();
+		return new PhpTemplate($tpl_dir, $default_vars);
 	}
 
 

+ 29 - 0
src/Database/Sql/SqlIn.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace KarmaFW\Database\Sql;
+
+use \KarmaFW\App;
+
+
+class SqlIn
+{
+	protected $expr = null;
+
+    public function __construct($expr)
+    {
+    	if (is_array($expr)) {
+    		$db = App::getDb();
+
+    		$arr = $expr;
+    		$arr = array_map(function ($p) use ($db) {return $db->escape($p);}, $arr);
+    		$expr = implode(', ', $arr);
+    	}
+        $this->expr = $expr;
+    }
+
+    public function __toString()
+    {
+        return $this->expr;
+    }
+
+}

+ 10 - 0
src/Database/Sql/SqlTable.php

@@ -129,6 +129,13 @@ class SqlTable
 	}
 
 
+	public function selectCount($where=null, $options=[])
+	{
+		$options['select'] = 'count(*) as nb';
+		$row = $this->getOne($where, $options);
+		return empty($row['nb']) ? 0 : $row['nb'];
+	}
+
 	public function selectOne($where=null, $options=[])
 	{
 		// Alias of getOne
@@ -153,6 +160,7 @@ class SqlTable
 
 		$limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
 		$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;
 
 		$select_sql = '*';
@@ -170,10 +178,12 @@ class SqlTable
 			$joins_sql = implode(' ', $options['join']);
 		}
 
+
 		$query = "select " . $select_sql . "
 					from " . $this->table_name . "
 					" . $joins_sql . "
 					where " . $this->db->buildSqlWhere($where) . "
+					" . $having_sql . "
 					" . $order_by_sql . "
 					" . $limit_sql;
 

+ 8 - 0
src/Database/Sql/SqlTools.php

@@ -132,6 +132,14 @@ class SqlTools
                 }else if ($value instanceof SqlLike) {
                     $where_sql[] = $key . ' like ' . (string) $this->escape($value);
                     
+                }else if ($value instanceof SqlIn) {
+                    $value = (string) $value;
+                    if (empty($value)) {
+                        $where_sql[] = '0';
+                    } else {
+                        $where_sql[] = $key . ' in (' . $value . ')';
+                    }
+                    
                 }else if ($value instanceof SqlExpr) {
                     $where_sql[] = $key . ' = ' . (string) $value;