Skip to content

Commit

Permalink
Dev: move getTemplateURL and getTemplatePath to Template model
Browse files Browse the repository at this point in the history
Dev: using static array to validate directory only one time for a page
  • Loading branch information
Shnoulle committed Feb 7, 2015
1 parent 4bba2c4 commit 08f9589
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 69 deletions.
52 changes: 2 additions & 50 deletions application/helpers/common_helper.php
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
77 changes: 58 additions & 19 deletions application/models/Template.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 08f9589

Please sign in to comment.