From 153f834e8dbbba143c0fe91d1961571cdbcbaf7a Mon Sep 17 00:00:00 2001 From: LouisGac Date: Mon, 9 Jul 2018 12:16:35 +0200 Subject: [PATCH] Dev: added some array functions to Twig --- application/config/internal.php | 7 ++++++ application/core/LS_Twig_Extension.php | 30 ++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/application/config/internal.php b/application/config/internal.php index 957ab1404ca..5ffcddf1928 100644 --- a/application/config/internal.php +++ b/application/config/internal.php @@ -241,6 +241,9 @@ 'empty' => 'empty', 'count' => 'count', 'reset' => 'reset', + 'in_array' => 'in_array', + 'in_multiarray' => 'LS_Twig_Extension::in_multiarray', + 'array_search' => 'array_search', 'renderCaptcha' => 'LS_Twig_Extension::renderCaptcha', 'getPost' => 'LS_Twig_Extension::getPost', 'getParam' => 'LS_Twig_Extension::getParam', @@ -287,6 +290,7 @@ 'json_encode', 'replace', 'last', + 'first', 'capitalize', 'lower', 'upper', @@ -337,6 +341,9 @@ 'empty', 'count', 'reset', + 'in_array', + 'array_search', + 'in_multiarray', 'renderCaptcha', 'getPost', 'getParam', diff --git a/application/core/LS_Twig_Extension.php b/application/core/LS_Twig_Extension.php index 0b8f5b4ff5a..08e9a41f41a 100644 --- a/application/core/LS_Twig_Extension.php +++ b/application/core/LS_Twig_Extension.php @@ -257,7 +257,7 @@ public static function image($sImagePath, $alt = '', $htmlOptions = array( )) if (@is_array(getimagesize(Yii::app()->getConfig('rootdir').'/'.$sImagePath))) { $sUrlImgAsset = self::assetPublish(Yii::app()->getConfig('rootdir').'/'.$sImagePath); } - + return CHtml::image($sUrlImgAsset, $alt, $htmlOptions); } @@ -273,11 +273,11 @@ public static function imageSrc($sImagePath, $default = './files/pattern.png') // Reccurence on templates to find the file $oTemplate = self::getTemplateForRessource($sImagePath); $sUrlImgAsset = $sImagePath; - - + + if ($oTemplate) { $sUrlImgAsset = self::assetPublish($oTemplate->path.$sImagePath); - } + } if (@is_array(getimagesize(Yii::app()->getConfig('rootdir').'/'.$sImagePath))) { $sUrlImgAsset = self::assetPublish(Yii::app()->getConfig('rootdir').'/'.$sImagePath); @@ -493,12 +493,28 @@ public static function darkencss($cssColor, $grade=10, $alpha=1){ } return 'rgba('.join(', ', $return).','.$alpha.')'; - + } + /** + * Check if a needle is in a multidimensional array + * @param mixed $needle The searched value. + * @param array $haystack The array. + * @param bool $strict If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack. + */ + function in_multiarray($needle, $haystack, $strict = false) { + + foreach ($haystack as $item) { + if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { + return true; + } + } + return false; } - - public static function lightencss($cssColor, $grade=10, $alpha=1){ + + + public static function lightencss($cssColor, $grade=10, $alpha=1) + { $aColors = str_split(substr($cssColor,1), 2); $return = []; foreach ($aColors as $color) {