瀏覽代碼

dev 2019-12

Max YO 5 年之前
父節點
當前提交
23907e9525
共有 4 個文件被更改,包括 136 次插入72 次删除
  1. 72 64
      helpers/helpers_default.php
  2. 19 1
      src/Database/Sql/SqlTable.php
  3. 5 0
      src/Routing/Router.php
  4. 40 7
      src/Templates/PhpTemplate.php

+ 72 - 64
helpers/helpers_default.php

@@ -82,65 +82,73 @@ if (! function_exists('cookie')) {
 
 if (! function_exists('slugify')) {
 	function slugify($text, $max_length=null) {
-	    // https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
+		// https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
+		// https://stackoverflow.com/questions/3371697/replacing-accented-characters-php
 
-	    // replace non letter or digits by -
-	    $text = preg_replace('~[^\pL\d]+~u', '-', $text);
+		$unwanted_array = array(    'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
+									'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
+									'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
+									'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
+									'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
+		$text = strtr( $text, $unwanted_array );
 
-	    // transliterate
-	    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
+		// replace non letter or digits by -
+		$text = preg_replace('~[^\pL\d]+~u', '-', $text);
 
-	    // remove unwanted characters
-	    $text = preg_replace('~[^-\w]+~', '', $text);
+		// transliterate
+		$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
 
-	    // trim
-	    $text = trim($text, '-');
+		// remove unwanted characters
+		$text = preg_replace('~[^-\w]+~', '', $text);
 
-	    // remove duplicate -
-	    $text = preg_replace('~-+~', '-', $text);
+		// trim
+		$text = trim($text, '-');
 
-	    // lowercase
-	    $text = strtolower($text);
+		// remove duplicate -
+		$text = preg_replace('~-+~', '-', $text);
 
-	    if (empty($text)) {
-	    	return 'n-a';
-	    }
+		// lowercase
+		$text = strtolower($text);
 
-	    if (! empty($max_length) && strlen($text) > $max_length) {
-	    	$text = substr(0, $max_length);
-	    }
+		if (empty($text)) {
+			return 'n-a';
+		}
+
+		if (! empty($max_length) && strlen($text) > $max_length) {
+			$text = substr(0, $max_length);
+		}
 
-	    return $text;
+		return $text;
 	}
 }
 
 
 if (! function_exists('generate_uid')) {
 	function generate_uid() {
-	    
-	    if (function_exists('com_create_guid')) {
-	        return trim(com_create_guid(), '{}');
-	    }
-
-	    if (function_exists('openssl_random_pseudo_bytes') === true) {
-	        $data = openssl_random_pseudo_bytes(16);
-	        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
-	        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
-	        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
-	    }
-
-	    mt_srand((double)microtime()*10000);
-	    
-	    $charid = strtoupper(md5(uniqid(rand(), true)));
-	    $uuid = sprintf(
-	                "%s-%s-%s-%s-%s",
-	                substr($charid, 0, 8),
-	                substr($charid, 8, 4),
-	                substr($charid,12, 4),
-	                substr($charid,16, 4),
-	                substr($charid,20,12)
-	             );
-	    return strtolower($uuid);
+		
+		if (function_exists('com_create_guid')) {
+			return trim(com_create_guid(), '{}');
+		}
+
+		if (function_exists('openssl_random_pseudo_bytes') === true) {
+			$data = openssl_random_pseudo_bytes(16);
+			$data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
+			$data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
+			return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
+		}
+
+		mt_srand((double)microtime()*10000);
+		
+		$charid = strtoupper(md5(uniqid(rand(), true)));
+		$uuid = sprintf(
+					"%s-%s-%s-%s-%s",
+					substr($charid, 0, 8),
+					substr($charid, 8, 4),
+					substr($charid,12, 4),
+					substr($charid,16, 4),
+					substr($charid,20,12)
+				 );
+		return strtolower($uuid);
 	}
 }
 
@@ -148,10 +156,10 @@ if (! function_exists('generate_uid')) {
 
 if (! function_exists('generate_password')) {
 	function generate_password($nb_chars = 8) {
-	    $ref = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 62 caractères au total
-	    $ref = $ref . $ref . $ref; // permet d'avoir jusqu'à 3 fois le meme caractere dans le mot de passe
-	    $ref = str_shuffle($ref);
-	    return substr($ref, 0, $nb_chars);
+		$ref = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 62 caractères au total
+		$ref = $ref . $ref . $ref; // permet d'avoir jusqu'à 3 fois le meme caractere dans le mot de passe
+		$ref = str_shuffle($ref);
+		return substr($ref, 0, $nb_chars);
 	}
 }
 
@@ -165,36 +173,36 @@ if (! function_exists('getRouteUrl')) {
 
 if (! function_exists('date_us_to_fr')) {
 	function date_us_to_fr($date_us) {
-	    $date_us = substr($date_us, 0, 10);
-	    $parts = explode('-', $date_us);
-	    return implode('/', array_reverse($parts));
+		$date_us = substr($date_us, 0, 10);
+		$parts = explode('-', $date_us);
+		return implode('/', array_reverse($parts));
 	}
 }
 
 
 if (! function_exists('truncate_str')) {
 	function truncate_str($str, $max_length) {
-	    if (strlen($str) > $max_length) {
-	        $str = substr($str, 0, $max_length-1) . '…';
-	    }
-	    return $str;
+		if (strlen($str) > $max_length) {
+			$str = substr($str, 0, $max_length-1) . '…';
+		}
+		return $str;
 	}
 }
 
 
 if (! function_exists('get_url_path')) {
 	function get_url_path($url, $with_querystring=true, $with_url_hash=false) {
-	    $url_parts = parse_url($url);
-	    $url = $url_parts['path'];
+		$url_parts = parse_url($url);
+		$url = $url_parts['path'];
 
-	    if ($with_querystring && ! empty($url_parts['query'])) {
-	        $url .= '?' . $url_parts['query'];
-	    }
-	    if ($with_url_hash && ! empty($url_parts['fragment'])) {
-	        $url .= '#' . $url_parts['fragment'];
-	    }
+		if ($with_querystring && ! empty($url_parts['query'])) {
+			$url .= '?' . $url_parts['query'];
+		}
+		if ($with_url_hash && ! empty($url_parts['fragment'])) {
+			$url .= '#' . $url_parts['fragment'];
+		}
 
-	    return $url;
+		return $url;
 	}
 }
 

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

@@ -87,7 +87,17 @@ class SqlTable
 	{
 		$limit_sql = (isset($options['limit']) && ! is_null($options['limit'])) ? ("limit " . $options['limit']) : "";
 
+		$join_sql = isset($options['join']) ? implode(" ", $options['join']) : "";
+
+		/*
+		if (isset($options['group by']) && empty($options['group_by'])) {
+			$options['group_by'] = $options['group by'];
+		}
+		$group_by_sql = isset($options['group_by']) ? ("group by " . $options['group_by']) : "";
+		*/
+
 		$query = "update " . $this->table_name . "
+					" . $join_sql . "
 					set " . $this->db->buildSqlUpdateValues($updates) . "
 					where " . $this->db->buildSqlWhere($where) . "
 					" . $limit_sql;
@@ -106,6 +116,12 @@ class SqlTable
 	}
 
 
+	public function all($where=null, $options=[])
+	{
+		// Alias of getAll
+		return $this->getAll($where, $options);
+	}
+
 	public function select($where=null, $options=[])
 	{
 		// Alias of getAll
@@ -189,6 +205,8 @@ class SqlTable
 			}
 		}
 
+		$table_alias = empty($options['alias']) ? "" : $options['alias'];
+
 		$joins_sql = '';
 		if (! empty($options['join'])) {
 			$options['join'] = is_array($options['join']) ? $options['join'] : [$options['join']];
@@ -197,7 +215,7 @@ class SqlTable
 
 
 		$query = "select " . $select_sql . "
-					from " . $this->table_name . "
+					from " . $this->table_name . " " . $table_alias . "
 					" . $joins_sql . "
 					where " . $this->db->buildSqlWhere($where) . "
 					" . $having_sql . "

+ 5 - 0
src/Routing/Router.php

@@ -136,6 +136,11 @@ class Router
 		$link = rtrim($link, '$');
 		$link = ltrim($link, '^');
 
+		$link = str_replace('\\.', '.', $link);
+		$link = str_replace('\\?', '?', $link);
+		$link = str_replace('\\+', '+', $link);
+		$link = str_replace('\\-', '-', $link);
+
 		if (! empty($urls_args)) {
 			foreach ($urls_args as $arg_value) {
 				$pos1 = strpos($link, '(');

+ 40 - 7
src/Templates/PhpTemplate.php

@@ -60,7 +60,7 @@ class PhpTemplate
 		});
 		$this->addPlugin('include', function ($param) use ($template) {
 			// {include my_template.tpl.php}
-			$template = new PhpTemplate($template->templates_dirs, $template->variables);
+			$template = new PhpTemplate($template->templates_dirs, $template->variables, null, $template->templates_dirs);
 			$templatechild_content = $template->fetch($param);
 			return $templatechild_content;
 		});
@@ -73,6 +73,23 @@ class PhpTemplate
 			return $url;
 		});
 
+		$this->addPlugin('block', function ($param, $matched_expr, $begin_block_offset_start, &$content) use ($template) {
+			// {block block_name}my html content{/block}  ==> assign variable $block_name with block content
+			$begin_block_offset_end = $begin_block_offset_start + strlen($matched_expr);
+
+			$end_block_offset_start = strpos($content, '{/block}', $begin_block_offset_end);
+
+			if ($end_block_offset_start) {
+				$block = substr($content, $begin_block_offset_end, $end_block_offset_start - $begin_block_offset_end);
+				$template->assign($param, $block);
+
+				$end_block_offset_end = $end_block_offset_start + strlen("{/block}");
+				$content = substr($content, 0, $begin_block_offset_start) . substr($content, $end_block_offset_end);
+			}
+
+
+			return '';
+		});
 	}
 
 
@@ -143,13 +160,28 @@ class PhpTemplate
 		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);
+
+					if ($prefix != 'block') {
+						preg_match_all('/{' . $prefix . ' ([^}]+)}/', $content, $regs, PREG_SET_ORDER);
+						foreach($regs as $reg) {
+							$replaced = $callback($reg[1], $reg);
+							$content = str_replace($reg[0], $replaced, $content);
+						}
+					} else {
+
+						$nb_iterations = 10;
+						while ($nb_iterations--) {
+							preg_match('/{' . $prefix . ' ([^}]+)}/', $content, $regs, PREG_OFFSET_CAPTURE);
+							if (! $regs) {
+								break;
+							}
+
+							$replaced = $callback($regs[1][0], $regs[0][0], $regs[0][1], $content);
+							$content = str_replace($regs[0][0], $replaced, $content);
+						}
 					}
 
+
 				}
 			}
 
@@ -163,7 +195,8 @@ class PhpTemplate
 						$replaced = ${$var};
 						$content = str_replace($reg[0], $replaced, $content);
 					} else {
-						//$content = str_replace($reg[0], '', $content);
+						// if variable not exists, replace with empty string
+						$content = str_replace($reg[0], '', $content);
 					}
 				}
 			}