Browse Source

dev 2021-04

Max F 4 years ago
parent
commit
bbd867ca42

+ 6 - 0
src/App/Middlewares/ErrorHandler.php

@@ -75,6 +75,12 @@ class ErrorHandler
                 $response_content = '<title>' . $title . '</title><h1>' . $title . '</h1><p>' . $message . '</p>';
             }
 
+			if (App::isCli()) {
+				echo $title . PHP_EOL . PHP_EOL;
+				echo $message . PHP_EOL . PHP_EOL;
+				exit;
+			}
+
             $response->html($response_content, $http_code);
         }
 

+ 6 - 0
src/App/Middlewares/UrlRouter.php

@@ -145,6 +145,12 @@ class UrlRouter
                 $response_content = '<title>' . $title . '</title><h1>' . $title . '</h1><p>' . $message . '</p>';
             }
 
+			if (App::isCli()) {
+				echo $title . PHP_EOL . PHP_EOL;
+				echo $message . PHP_EOL . PHP_EOL;
+				exit;
+			}
+
 
 			// else => error 500
 			$response->html($response_content, 500);

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

@@ -379,6 +379,7 @@ class SqlTable
 		}
 
 		$limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
+		$offset_sql = isset($options['offset']) ? ("offset " . $options['offset']) : "";
 		$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']) : "";
@@ -416,7 +417,8 @@ class SqlTable
 					" . $group_by_sql . "
 					" . $having_sql . "
 					" . $order_by_sql . "
-					" . $limit_sql;
+					" . $limit_sql . "
+					" . $offset_sql;
 
 		if (! empty($options['debug'])) {
 			echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";

+ 41 - 5
src/Database/Sql/SqlTableEntity.php

@@ -17,29 +17,66 @@ class SqlTableEntity /* extends \ArrayObject */
 	protected $data = [];
 
 
-    public function __construct($primary_key_values=null)
+    public function __construct($primary_key_values=null, array $where=[], array $options=[])
     {
         if ($primary_key_values !== null) {
+			if (! is_array($primary_key_values)) {
+				if (! empty(static::$primary_keys) && count(static::$primary_keys) == 1) {
+					$primary_key = static::$primary_keys[0];
+					$primary_key_values = [
+						$primary_key => $primary_key_values,
+					];
+
+				} else {
+					throw new \Exception('Unknown primary key ' . $primary_key_values);
+				}
+			}
+			
             $this->primary_key_values = $primary_key_values;
-            $this->load();
+            $this->load($where, $options);
 
         } else {
+            //throw new \Exception('Unknown entity');
             $this->data = (static::$model)::getEmpty();
         }
     }
 
 
-    public function load()
+    public function load(array $where=[], array $options=[])
     {
         if (empty($this->primary_key_values)) {
             throw new \Exception('missing primary_key_values');
         }
 
-        $this->data = (static::$model)::load($this->primary_key_values);
+		$entity = (static::$model)::load($this->primary_key_values, $where, $options);
+
+		if (!$entity) {
+            //throw new \Exception('Unknown entity');
+			$entity = (static::$model)::getEmpty();
+		}
+
+        $this->data = $entity;
         return $this;
     }
 
 
+	public function fromArray($data)
+	{
+		$this->primary_key_values = [];
+		$this->data = array_slice($data, 0);
+
+		if (! empty($this->primary_keys)) {
+			foreach ($this->primary_keys as $column_name) {
+				if (isset($this->data[$column_name])) {
+					$this->primary_key_values[$column_name] = $this->data[$column_name];
+				}
+			}
+		}
+
+		return $this;
+	}
+
+
     public function save()
     {
         if (empty($this->primary_key_values)) {
@@ -103,7 +140,6 @@ class SqlTableEntity /* extends \ArrayObject */
 		unset($this->data[$name]);
 	}
 
-	
 
 	public function asArray()
 	{

+ 5 - 2
src/helpers/helpers_array.php

@@ -210,7 +210,7 @@ if (! function_exists('import_csv')) {
 
 
 if (! function_exists('get_csv')) {
-	function get_csv($arr, $fields=array(), $sep=";") {
+	function get_csv($arr, $fields=array(), $sep=";", $display_headers=true) {
 		$str = '';
 		if (! empty($arr)) {
 
@@ -227,7 +227,10 @@ if (! function_exists('get_csv')) {
 					$line[] = $v;
 				}
 			}
-			$str .= implode($sep, $line) . PHP_EOL;
+
+			if ($display_headers) {
+				$str .= implode($sep, $line) . PHP_EOL;
+			}
 
 			foreach ($arr as $row) {
 				$line = array();