From 08f9589f3d387f702d51da9846851a24732793ba Mon Sep 17 00:00:00 2001 From: Denis Chenu Date: Sat, 7 Feb 2015 16:54:18 +0100 Subject: [PATCH] Dev: move getTemplateURL and getTemplatePath to Template model Dev: using static array to validate directory only one time for a page --- application/helpers/common_helper.php | 52 +----------------- application/models/Template.php | 77 ++++++++++++++++++++------- 2 files changed, 60 insertions(+), 69 deletions(-) diff --git a/application/helpers/common_helper.php b/application/helpers/common_helper.php index 13d92b9b85a..dc17f56a662 100644 --- a/application/helpers/common_helper.php +++ b/application/helpers/common_helper.php @@ -5376,33 +5376,7 @@ function getNumericalFormat($lang = 'en', $integer = false, $negative = true) { */ function getTemplatePath($sTemplateName = false) { - if (!$sTemplateName) - { - $sTemplateName=Yii::app()->getConfig('defaulttemplate'); // if $sTemplateName is NULL or false or "" - } - if (isStandardTemplate($sTemplateName)) - { - return Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$sTemplateName; - } - else - { - if (is_dir(Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$sTemplateName)) - { - return Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$sTemplateName; - } - elseif (isStandardTemplate(Yii::app()->getConfig('defaulttemplate'))) - { - return Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$sTemplateName; - } - elseif (file_exists(Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.Yii::app()->getConfig('defaulttemplate'))) - { - return Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.Yii::app()->getConfig('defaulttemplate'); - } - else - { - return Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.'default'; - } - } + return Template::getTemplatePath($sTemplateName); } /** @@ -5412,29 +5386,7 @@ function getTemplatePath($sTemplateName = false) */ function getTemplateURL($sTemplateName) { - if (isStandardTemplate($sTemplateName)) - { - return Yii::app()->getConfig("standardtemplaterooturl").'/'.$sTemplateName; - } - else - { - if (file_exists(Yii::app()->getConfig("usertemplaterootdir").'/'.$sTemplateName)) - { - return Yii::app()->getConfig("usertemplaterooturl").'/'.$sTemplateName; - } - elseif (file_exists(Yii::app()->getConfig("usertemplaterootdir").'/'.Yii::app()->getConfig('defaulttemplate'))) - { - return Yii::app()->getConfig("usertemplaterooturl").'/'.Yii::app()->getConfig('defaulttemplate'); - } - elseif (file_exists(Yii::app()->getConfig("standardtemplaterootdir").'/'.Yii::app()->getConfig('defaulttemplate'))) - { - return Yii::app()->getConfig("standardtemplaterooturl").'/'.Yii::app()->getConfig('defaulttemplate'); - } - else - { - return Yii::app()->getConfig("standardtemplaterooturl").'/default'; - } - } + return Template::getTemplateURL($sTemplateName); } /** diff --git a/application/models/Template.php b/application/models/Template.php index ee39eb058b6..35950b4b544 100644 --- a/application/models/Template.php +++ b/application/models/Template.php @@ -17,18 +17,19 @@ class Template extends LSActiveRecord { - /** - * Returns the static model of Settings table - * - * @static - * @access public + + /** + * Returns the static model of Settings table + * + * @static + * @access public * @param string $class - * @return CActiveRecord - */ - public static function model($class = __CLASS__) - { - return parent::model($class); - } + * @return CActiveRecord + */ + public static function model($class = __CLASS__) + { + return parent::model($class); + } /** * Returns the setting's table name to be used by the model @@ -61,24 +62,62 @@ public static function templateNameFilter($sTemplateName) { $usertemplaterootdir = Yii::app()->getConfig('usertemplaterootdir'); $standardtemplaterootdir = Yii::app()->getConfig('standardtemplaterootdir'); - $sDefaultTemplate = Yii::app()->getConfig('defaulttemplate');// !empty ? - if (!empty($sTemplateName) && is_dir("$usertemplaterootdir/{$sTemplateName}/"))// Maybe better validate is_file("$usertemplaterootdir/{$sTemplateName}/startpage.pstpl") + $sTemplateName=empty($sTemplateName) ? Yii::app()->getConfig('defaulttemplate') : $sTemplateName; + + if (is_dir("{$usertemplaterootdir}/{$sTemplateName}/")) { return $sTemplateName; } - elseif (!empty($sTemplateName) && is_dir("$standardtemplaterootdir/{$sTemplateName}/")) + elseif (is_dir("{$standardtemplaterootdir}/{$sTemplateName}/")) { return $sTemplateName; } - elseif (is_dir("$standardtemplaterootdir/{$sDefaultTemplate}/")) + return 'default'; + } + + /** + * Get the template path for any template : test if template if exist + * + * @param string $sTemplateName + * @return string template path + */ + public static function getTemplatePath($sTemplateName = "") + { + static $aTemplatePath=array(); + if(isset($aTemplatePath[$sTemplateName])) + return $aTemplatePath[$sTemplateName]; + + $sFilteredTemplateName=self::templateNameFilter($sTemplateName); + if (isStandardTemplate($sFilteredTemplateName)) { - return $sDefaultTemplate; + return $aTemplatePath[$sTemplateName]=Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$sFilteredTemplateName; } - elseif (is_dir("$usertemplaterootdir/{$sDefaultTemplate}/")) + else { - return $sDefaultTemplate; + return $aTemplatePath[$sTemplateName]=Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$sFilteredTemplateName; } - return 'default'; + } + + /** + * This function returns the complete URL path to a given template name + * + * @param string $sTemplateName + * @return string template url + */ + public static function getTemplateURL($sTemplateName="") + { + static $aTemplateUrl=array(); + if(isset($aTemplateUrl[$sTemplateName])) + return $aTemplateUrl[$sTemplateName]; + $sFiteredTemplateName=self::templateNameFilter($sTemplateName); + if (isStandardTemplate($sFiteredTemplateName)) + { + return $aTemplateUrl[$sTemplateName]=Yii::app()->getConfig("standardtemplaterooturl").'/'.$sFiteredTemplateName; + } + else + { + return $aTemplateUrl[$sTemplateName]=Yii::app()->getConfig("usertemplaterooturl").'/'.$sFiteredTemplateName; + } } }