Skip to content

Commit

Permalink
Dev: update template model to use templates table
Browse files Browse the repository at this point in the history
Dev: now template name can be different from template folder
  • Loading branch information
LouisGac authored and TonisOrmisson committed Jul 14, 2017
1 parent 78a95e2 commit 9f4b182
Showing 1 changed file with 183 additions and 56 deletions.
239 changes: 183 additions & 56 deletions application/models/Template.php
Expand Up @@ -34,27 +34,65 @@ class Template extends LSActiveRecord
private static $standardTemplates = array();

/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return CActiveRecord
* @return string the associated database table name
*/
public static function model($class = __CLASS__)
public function tableName()
{
return parent::model($class);
return '{{templates}}';
}

/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
* @return array validation rules for model attributes.
*/
public function tableName()
public function rules()
{
return '{{templates}}';
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name', 'required'),
array('owner_id, extends_templates_id', 'numerical', 'integerOnly'=>true),
array('name, author', 'length', 'max'=>150),
array('author_email, author_url', 'length', 'max'=>255),
array('version, path', 'length', 'max'=>45),
array('creation_date, copyright, license, description, last_update', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, name, creation_date, author, author_email, author_url, copyright, license, version, description, last_update, path, owner_id, extends_templates_id', 'safe', 'on'=>'search'),
);
}

/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'creation_date' => 'Creation Date',
'author' => 'Author',
'author_email' => 'Author Email',
'author_url' => 'Author Url',
'copyright' => 'Copyright',
'license' => 'License',
'version' => 'Version',
'description' => 'Description',
'last_update' => 'Last Update',
'path' => 'Path',
'owner_id' => 'Owner',
'extends_templates_id' => 'Extends Templates',
);
}

/**
Expand All @@ -65,7 +103,7 @@ public function tableName()
*/
public function primaryKey()
{
return 'folder';
return 'id';
}

/**
Expand All @@ -76,24 +114,28 @@ public function primaryKey()
*/
public static function templateNameFilter($sTemplateName)
{
$sDefaultTemplate = Yii::app()->getConfig('defaulttemplate','default');
$sDefaultTemplate = Yii::app()->getConfig('defaulttemplate','Default template');
$sTemplateName = empty($sTemplateName) ? $sDefaultTemplate : $sTemplateName;

/* Standard Template return it without testing */
if(self::isStandardTemplate($sTemplateName)) {
return $sTemplateName;
}

/* Validate if template is OK in user dir, DIRECTORY_SEPARATOR not needed "/" is OK */
if(is_file(Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$sTemplateName.DIRECTORY_SEPARATOR.'config.xml')) {
$oTemplate = self::model()->find('name=:name', array(':name'=>$sTemplateName));

if(is_object($oTemplate) && is_file(Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$oTemplate->path.DIRECTORY_SEPARATOR.'config.xml')) {
return $sTemplateName;
}

/* Then try with the global default template */
if($sTemplateName!=$sDefaultTemplate) {
return self::templateNameFilter($sDefaultTemplate);
}

/* Last solution : default */
return 'default';
return 'Default template';
}


Expand All @@ -118,17 +160,18 @@ public static function checkIfTemplateExists($sTemplateName)
*/
public static function getTemplatePath($sTemplateName = "")
{
static $aTemplatePath=array();
static $aTemplatePath = array();
if(isset($aTemplatePath[$sTemplateName])) {
return $aTemplatePath[$sTemplateName];
}

$sFilteredTemplateName=self::templateNameFilter($sTemplateName);
if (self::isStandardTemplate($sFilteredTemplateName)) {
return $aTemplatePath[$sTemplateName]=Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$sFilteredTemplateName;
$oTemplate = self::model()->find('name=:name', array(':name'=>$sTemplateName));

if (self::isStandardTemplate($sTemplateName)) {
return $aTemplatePath[$sTemplateName] = Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$oTemplate->path;
}
else {
return $aTemplatePath[$sTemplateName]=Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$sFilteredTemplateName;
return $aTemplatePath[$sTemplateName] = Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$oTemplate->path;
}
}

Expand Down Expand Up @@ -172,7 +215,6 @@ static public function getOtherFiles($filesDir)
return $otherFiles;
}


/**
* This function returns the complete URL path to a given template name
*
Expand All @@ -186,40 +228,58 @@ public static function getTemplateURL($sTemplateName="")
return $aTemplateUrl[$sTemplateName];
}

$sFiteredTemplateName=self::templateNameFilter($sTemplateName);
if (self::isStandardTemplate($sFiteredTemplateName)) {
return $aTemplateUrl[$sTemplateName]=Yii::app()->getConfig("standardtemplaterooturl").'/'.$sFiteredTemplateName.'/';
}
else {
return $aTemplateUrl[$sTemplateName]=Yii::app()->getConfig("usertemplaterooturl").'/'.$sFiteredTemplateName.'/';
$oTemplate = self::model()->find('name=:name', array(':name'=>$sTemplateName));

if (is_object($oTemplate)){
if (self::isStandardTemplate($sTemplateName)) {
return $aTemplateUrl[$sTemplateName]=Yii::app()->getConfig("standardtemplaterooturl").'/'.$oTemplate->path.'/';
}
else {
return $aTemplateUrl[$sTemplateName]=Yii::app()->getConfig("usertemplaterooturl").'/'.$oTemplate->path.'/';
}
}else{
return '';
}

}


/**
* Returns an array of all available template names - does a basic check if the template might be valid
*
* TODO: replace the calls to that function by a data provider based on search
*
* @return array
*/
public static function getTemplateList()
{
$sUserTemplateRootDir=Yii::app()->getConfig("usertemplaterootdir");
$standardTemplateRootDir=Yii::app()->getConfig("standardtemplaterootdir");
$sUserTemplateRootDir = Yii::app()->getConfig("usertemplaterootdir");
$standardTemplateRootDir = Yii::app()->getConfig("standardtemplaterootdir");

$aTemplateList=array();
$aStandardTemplates = self::getStandardTemplateList();

foreach ($aStandardTemplates as $sTemplateName){
$oTemplate = self::model()->find('name=:name', array(':name'=>$sTemplateName));

$aStandardTemplates=self::getStandardTemplateList();
foreach($aStandardTemplates as $templateName){
$aTemplateList[$templateName] = $standardTemplateRootDir.DIRECTORY_SEPARATOR.$templateName;
if (is_object($oTemplate)){
$aTemplateList[$sTemplateName] = $standardTemplateRootDir.DIRECTORY_SEPARATOR.$oTemplate->path;
}
}

if ($sUserTemplateRootDir && $handle = opendir($sUserTemplateRootDir)) {
while (false !== ($sFileName = readdir($handle))) {
while (false !== ($sTemplatePath = readdir($handle))) {
// Maybe $file[0] != "." to hide Linux hidden directory
if (!is_file("$sUserTemplateRootDir/$sFileName")
&& $sFileName != "."
&& $sFileName != ".." && $sFileName!=".svn"
&& (file_exists("{$sUserTemplateRootDir}/{$sFileName}/config.xml"))) {
if (!is_file("$sUserTemplateRootDir/$sTemplatePath")
&& $sTemplatePath != "."
&& $sTemplatePath != ".." && $sTemplatePath!=".svn"
&& (file_exists("{$sUserTemplateRootDir}/{$sTemplatePath}/config.xml"))) {

$aTemplateList[$sFileName] = $sUserTemplateRootDir.DIRECTORY_SEPARATOR.$sFileName;
$oTemplate = self::model()->find('path=:path', array(':path'=>$sTemplatePath));

if (is_object($oTemplate)){
$aTemplateList[$oTemplate->name] = $sUserTemplateRootDir.DIRECTORY_SEPARATOR.$sTemplatePath;
}
}
}
closedir($handle);
Expand All @@ -231,26 +291,39 @@ public static function getTemplateList()

/**
* @return array
* TODO: replace the calls to that function by a data provider based on search
*/
public static function getTemplateListWithPreviews()
{
$usertemplaterootdir=Yii::app()->getConfig("usertemplaterootdir");
$standardtemplaterootdir=Yii::app()->getConfig("standardtemplaterootdir");
$usertemplaterooturl = Yii::app()->getConfig("usertemplaterooturl");
$standardtemplaterooturl=Yii::app()->getConfig("standardtemplaterooturl");
$usertemplaterootdir = Yii::app()->getConfig("usertemplaterootdir");
$standardtemplaterootdir = Yii::app()->getConfig("standardtemplaterootdir");
$usertemplaterooturl = Yii::app()->getConfig("usertemplaterooturl");
$standardtemplaterooturl = Yii::app()->getConfig("standardtemplaterooturl");

$aTemplateList=array();
$aStandardTemplates=self::getStandardTemplateList();
foreach($aStandardTemplates as $templateName){
$aTemplateList[$templateName]['directory'] = $standardtemplaterootdir.DIRECTORY_SEPARATOR.$templateName;
$aTemplateList[$templateName]['preview'] = $standardtemplaterooturl.'/'.$templateName.'/preview.png';
$aTemplateList = array();
$aStandardTemplates = self::getStandardTemplateList();

foreach ($aStandardTemplates as $sTemplateName){
$oTemplate = self::model()->find('name=:name', array(':name'=>$sTemplateName));

if (is_object($oTemplate)) {
$aTemplateList[$sTemplateName]['directory'] = $standardtemplaterootdir.DIRECTORY_SEPARATOR.$oTemplate->path;
$aTemplateList[$sTemplateName]['preview'] = $standardtemplaterooturl.'/'.$oTemplate->path.'/preview.png';
}
}

if ($usertemplaterootdir && $handle = opendir($usertemplaterootdir)) {
while (false !== ($file = readdir($handle))) {
while (false !== ($sTemplatePath = readdir($handle))) {
// Maybe $file[0] != "." to hide Linux hidden directory
if (!is_file("$usertemplaterootdir/$file") && $file != "." && $file != ".." && $file!=".svn") {
$aTemplateList[$file]['directory'] = $usertemplaterootdir.DIRECTORY_SEPARATOR.$file;
$aTemplateList[$file]['preview'] = $usertemplaterooturl.'/'.$file.'/'.'preview.png';
if (!is_file("$usertemplaterootdir/$sTemplatePath") && $sTemplatePath != "." && $sTemplatePath != ".." && $sTemplatePath!=".svn") {


$oTemplate = self::model()->find('path=:path', array(':path'=>$sTemplatePath));

if (is_object($oTemplate)){
$aTemplateList[$oTemplate->name]['directory'] = $sUserTemplateRootDir.DIRECTORY_SEPARATOR.$sTemplatePath;
$aTemplateList[$oTemplate->name]['preview'] = $sUserTemplateRootDir.DIRECTORY_SEPARATOR.$sTemplatePath.'/'.'preview.png';
}
}
}
closedir($handle);
Expand Down Expand Up @@ -299,7 +372,7 @@ public static function forceAssets()
if (App()->getAssetManager()->linkAssets) return;

$standardTemplatesPath = Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR;
$Resource = opendir($standardTemplatesPath);
$Resource = opendir($standardTemplatesPath);
while ($Item = readdir($Resource)) {
if (is_dir($standardTemplatesPath . $Item) && $Item != "." && $Item != "..") {
touch($standardTemplatesPath . $Item);
Expand All @@ -314,6 +387,11 @@ public static function forceAssets()
*/
public static function getStandardTemplateList()
{

$standardTemplates = array('default');
return $standardTemplates;

/*
$standardTemplates=self::$standardTemplates;
if(empty($standardTemplates)){
$standardTemplates = array();
Expand All @@ -333,7 +411,56 @@ public static function getStandardTemplateList()
}
self::$standardTemplates = $standardTemplates;
}

*/
return self::$standardTemplates;
}

/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('creation_date',$this->creation_date,true);
$criteria->compare('author',$this->author,true);
$criteria->compare('author_email',$this->author_email,true);
$criteria->compare('author_url',$this->author_url,true);
$criteria->compare('copyright',$this->copyright,true);
$criteria->compare('license',$this->license,true);
$criteria->compare('version',$this->version,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('last_update',$this->last_update,true);
$criteria->compare('path',$this->path,true);
$criteria->compare('owner_id',$this->owner_id);
$criteria->compare('extends_templates_id',$this->extends_templates_id);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Templates the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}

0 comments on commit 9f4b182

Please sign in to comment.