Skip to content

Commit

Permalink
New feature: offer core templates to install manually.
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Oct 30, 2017
1 parent 441643b commit 2b2862f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 53 deletions.
59 changes: 43 additions & 16 deletions application/models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@
*/
class Template extends LSActiveRecord
{
/** @var array $aTemplatesInUploadDir cache for the method getUploadTemplates */

/** @var array $aAllTemplatesDir cache for the method getAllTemplatesDirectories */
public static $aAllTemplatesDir = null;

/** @var array $aTemplatesInUploadDir cache for the method getTemplateInUpload */
public static $aTemplatesInUploadDir = null;

/** @var array $aTemplatesInStandardDir cache for the method getTemplateInStandard */
public static $aTemplatesInStandardDir = null;

/** @var Template - The instance of template object */
private static $instance;

Expand Down Expand Up @@ -218,7 +225,6 @@ public static function getTemplateConfiguration($sTemplateName=null, $iSurveyId=
if ( $bForceXML || !is_a($oTemplateConfigurationModel, 'TemplateConfiguration') || ! $oTemplateConfigurationModel->checkTemplate()){
$oTemplateConfigurationModel = new TemplateManifest;
$oTemplateConfigurationModel->setBasics($sTemplateName, $iSurveyId);

}

//$oTemplateConfigurationModel->prepareTemplateRendering($sTemplateName, $iSurveyId);
Expand Down Expand Up @@ -441,7 +447,7 @@ public static function forceAssets()
public static function getStandardTemplateList()
{

$standardTemplates = array('default', 'vanilla', 'material', 'no_bootstrap');
$standardTemplates = array('default', 'vanilla', 'material', 'no_bootstrap', 'monochrome');
return $standardTemplates;

/*
Expand Down Expand Up @@ -474,29 +480,50 @@ public static function hasInheritance($sTemplateName)
return self::model()->countByAttributes(array('extends' => $sTemplateName));
}

public static function getUploadTemplates()
public static function getAllTemplatesDirectories()
{
if(empty(self::$aAllTemplatesDir)){
$aTemplatesInUpload = Template::getTemplateInUpload();
$aTemplatesInCore = Template::getTemplateInStandard();
self::$aAllTemplatesDir = array_merge($aTemplatesInUpload, $aTemplatesInCore);
}
return self::$aAllTemplatesDir;
}

public static function getTemplateInUpload()
{
if(empty(self::$aTemplatesInUploadDir)){
$sUserTemplateRootDir = Yii::app()->getConfig("usertemplaterootdir");
self::$aTemplatesInUploadDir = self::getTemplateInFolder($sUserTemplateRootDir);
}

$sUserTemplateRootDir = Yii::app()->getConfig("usertemplaterootdir");
$aTemplateList = array();
return self::$aTemplatesInUploadDir;
}

if ($sUserTemplateRootDir && $handle = opendir($sUserTemplateRootDir)){
public static function getTemplateInStandard()
{
if(empty(self::$aTemplatesInStandardDir)){
$standardTemplateRootDir = Yii::app()->getConfig("standardtemplaterootdir");
self::$aTemplatesInStandardDir = self::getTemplateInFolder($standardTemplateRootDir);
}

while (false !== ($sFileName = readdir($handle))){
return self::$aTemplatesInStandardDir;
}

if (!is_file("$sUserTemplateRootDir/$sFileName") && $sFileName != "." && $sFileName != ".." && $sFileName!=".svn" && (file_exists("{$sUserTemplateRootDir}/{$sFileName}/config.xml") )){
$aTemplateList[$sFileName] = $sUserTemplateRootDir.DIRECTORY_SEPARATOR.$sFileName;
}
public static function getTemplateInFolder($sFolder)
{
$aTemplateList = array();

if ($sFolder && $handle = opendir($sFolder)){
while (false !== ($sFileName = readdir($handle))){
if (!is_file("$sFolder/$sFileName") && $sFileName != "." && $sFileName != ".." && $sFileName!=".svn" && (file_exists("{$sFolder}/{$sFileName}/config.xml") )){
$aTemplateList[$sFileName] = $sFolder.DIRECTORY_SEPARATOR.$sFileName;
}
closedir($handle);
}
ksort($aTemplateList);
self::$aTemplatesInUploadDir = $aTemplateList;
closedir($handle);
}

return self::$aTemplatesInUploadDir;
ksort($aTemplateList);
return $aTemplateList;
}


Expand Down
41 changes: 41 additions & 0 deletions application/models/TemplateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class TemplateConfig extends CActiveRecord
/** @var string $xmlFile What xml config file does it use? (config/minimal) */
protected $xmlFile;

public $allDbTemplateFolders = null;

public static $aTemplatesWithoutDB = null;

/**
* get the template API version
Expand Down Expand Up @@ -858,6 +860,45 @@ public static function importManifest($sTemplateName, $aDatas)
}
}

public function getAllDbTemplateFolders()
{
if (empty($this->allDbTemplateFolders)){

$oCriteria = new CDbCriteria;
$oCriteria->select = 'folder';
$oAllDbTemplateFolders = Template::model()->findAll($oCriteria);

$aAllDbTemplateFolders = array();
foreach ($oAllDbTemplateFolders as $oAllDbTemplateFolders){
$aAllDbTemplateFolders[] = $oAllDbTemplateFolders->folder;
}

$this->allDbTemplateFolders = array_unique($aAllDbTemplateFolders);
}

return $this->allDbTemplateFolders;
}


public function getTemplatesWithNoDb()
{
if (empty(self::$aTemplatesWithoutDB)){
$aTemplatesDirectories = Template::getAllTemplatesDirectories();
$aTemplatesInDb = $this->getAllDbTemplateFolders();
$aTemplatesWithoutDB = array();

foreach ($aTemplatesDirectories as $sName => $sPath) {
if (! in_array($sName, $aTemplatesInDb) ){
$aTemplatesWithoutDB[$sName] = Template::getTemplateConfiguration($sName, null, null, true); // Get the manifest
}
}
self::$aTemplatesWithoutDB = $aTemplatesWithoutDB;
}

return self::$aTemplatesWithoutDB;
}


// TODO: try to refactore most of those methods in TemplateConfiguration and TemplateManifest so we can define their body here.
// It will consist in adding private methods to get the values of variables... See what has been done for createTemplatePackage
// Then, the lonely differences between TemplateManifest and TemplateConfiguration should be how to retreive and format the data
Expand Down
37 changes: 1 addition & 36 deletions application/models/TemplateConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class TemplateConfiguration extends TemplateConfig

// Caches

public $allDbTemplateFolders = null;

/** @var string $sPreviewImgTag the template preview image tag for the template list*/
public $sPreviewImgTag;

Expand Down Expand Up @@ -573,7 +571,7 @@ protected function getFilesToLoad($oTemplate, $sType)
$this->aFilesToLoad[$sType] = array_merge($this->aFilesToLoad[$sType], $aFileList);
}
}

}


Expand Down Expand Up @@ -833,21 +831,6 @@ public function getParentConfiguration(){
}


public function getTemplatesWithNoDb()
{
$aTemplatesInUpload = Template::getUploadTemplates();
$aTemplatesInDb = $this->getAllDbTemplateFolders();
$aTemplatesWithoutDB = array();

foreach ($aTemplatesInUpload as $sName => $sPath) {
if (! in_array($sName, $aTemplatesInDb) ){
$aTemplatesWithoutDB[$sName] = Template::getTemplateConfiguration($sName, null, null, true); // Get the manifest
}
}

return $aTemplatesWithoutDB;
}

/**
* Change the template name inside the configuration entries (called from template editor)
* NOTE: all tests (like template exist, etc) are done from template controller.
Expand All @@ -860,24 +843,6 @@ public static function rename($sOldName,$sNewName)
self::model()->updateAll(array( 'template_name' => $sNewName ), "template_name = :oldname", array(':oldname'=>$sOldName));
}

public function getAllDbTemplateFolders()
{
if (empty($this->allDbTemplateFolders)){

$oCriteria = new CDbCriteria;
$oCriteria->select = 'folder';
$oAllDbTemplateFolders = Template::model()->findAll($oCriteria);

$aAllDbTemplateFolders = array();
foreach ($oAllDbTemplateFolders as $oAllDbTemplateFolders){
$aAllDbTemplateFolders[] = $oAllDbTemplateFolders->folder;
}

$this->allDbTemplateFolders = array_unique($aAllDbTemplateFolders);
}

return $this->allDbTemplateFolders;
}

/**
* Proxy for the AR method to manage the inheritance
Expand Down
3 changes: 2 additions & 1 deletion application/models/TemplateManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ private function setPath()
*/
private function setTemplateName($sTemplateName='', $iSurveyId='')
{

// If it is called from the template editor, a template name will be provided.
// If it is called for survey taking, a survey id will be provided
if ($sTemplateName == '' && $iSurveyId == '') {
Expand All @@ -555,7 +556,7 @@ private function setTemplateName($sTemplateName='', $iSurveyId='')
} else {
$this->sTemplateName = Template::templateNameFilter(App()->getConfig('defaulttemplate','default'));
}
}
}
}


Expand Down

0 comments on commit 2b2862f

Please sign in to comment.