Skip to content

Commit

Permalink
Dev: refactorisation of the template configuration settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lacrioque committed Aug 16, 2017
1 parent 02c1185 commit 78cb6fa
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 300 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/templates.php
Expand Up @@ -781,7 +781,7 @@ protected function _templatesummary($templatename, $screenname, $editfile, $rela
protected function _initialise($templatename, $screenname, $editfile, $showsummary = true)
{
// LimeSurvey style
$oEditedTemplate = Template::model()->getInstance($templatename, '', true);
$oEditedTemplate = Template::model()->getInstance($templatename, null,null, true);

//App()->getClientScript()->reset();
Yii::app()->loadHelper('surveytranslator');
Expand Down
6 changes: 3 additions & 3 deletions application/models/SurveysGroups.php
Expand Up @@ -232,12 +232,12 @@ public function getButtons()
public static function getSurveyGroupsList()
{
$aSurveyList = [];
$oSurveyGroups = self::model()->findAll('parent_id=:parent_id',array('parent_id'=>0));
$oSurveyGroups = self::model()->findAll();

foreach( $oSurveyGroups as $oSurveyGroup){
$aSurveyList[$oSurveyGroup->gsid] = $oSurveyGroup->title;
}

}
return $aSurveyList;
}

Expand Down
64 changes: 1 addition & 63 deletions application/models/Template.php
Expand Up @@ -198,69 +198,7 @@ public static function getTemplateConfiguration($sTemplateName=null, $iSurveyId=

// First we try to get a confifuration row from DB
if (!$bForceXML){

if ($sTemplateName!=null){
$oTemplateConfigurationModel = TemplateConfiguration::model()->find(
'templates_name=:templates_name AND sid IS NULL AND gsid IS NULL',
array(':templates_name'=>$sTemplateName)
);
}

if($iSurveyGroupId!=null) {
$criteria = new CDbCriteria();
$sTemplateName = $sTemplateName!=null ? $sTemplateName : SurveysGroups::model()->findByPk($iSurveyGroupId)->template;
$criteria->addCondition('gsid=:gsid');
$criteria->addCondition('templates_name=:templates_name');
$criteria->params = array('gsid' => $iSurveyGroupId, 'templates_name' => $sTemplateName);

$oTemplateConfigurationModel = TemplateConfiguration::model()->find($criteria);

// No specific template configuration for this survey
if (!is_a($oTemplateConfigurationModel, 'TemplateConfiguration')){
$oTemplateConfigurationModel = TemplateConfiguration::model()->find('templates_name=:templates_name AND sid IS NULL AND gsid IS NULL', array(':templates_name'=>$sTemplateName));
}
}

if($iSurveyId!=null) {
$sTemplateName = $sTemplateName!=null ? $sTemplateName : Survey::model()->findByPk($iSurveyId)->template;
$criteria = new CDbCriteria();
$criteria->addCondition('sid=:sid');
$criteria->addCondition('templates_name=:templates_name');
$criteria->params = array('sid' => $iSurveyId, 'templates_name' => $sTemplateName);

$oTemplateConfigurationModel = TemplateConfiguration::model()->find($criteria);

// No specific template configuration for this survey
if (!is_a($oTemplateConfigurationModel, 'TemplateConfiguration')){
$sTemplateName = Survey::model()->findByPk($iSurveyId)->template;
$oTemplateConfigurationModel = TemplateConfiguration::model()->find(
'templates_name=:templates_name AND sid IS NULL AND gsid IS NULL',
array(':templates_name'=>$sTemplateName)
);
}
}

//If a survey group id is set, but there is no survey specific template configuration create one!
// TODO: move this somewhere else
if(($iSurveyGroupId != null) && !($iSurveyGroupId == $oTemplateConfigurationModel->gsid)){
$oTemplateConfigurationModel->id = null;
$oTemplateConfigurationModel->isNewRecord = true;
$oTemplateConfigurationModel->gsid = $iSurveyGroupId;
$oTemplateConfigurationModel->setToInherit();
$oTemplateConfigurationModel->save();
return $oTemplateConfigurationModel;
}

//If a survey id is set, but there is no survey specific template configuration create one!
// TODO: move this somewhere else
if(($iSurveyId != null) && !($iSurveyId == $oTemplateConfigurationModel->sid)){
$oTemplateConfigurationModel->id = null;
$oTemplateConfigurationModel->isNewRecord = true;
$oTemplateConfigurationModel->sid = $iSurveyId;
$oTemplateConfigurationModel->setToInherit();
$oTemplateConfigurationModel->save();
return $oTemplateConfigurationModel;
}
$oTemplateConfigurationModel = TemplateConfiguration::getInstance($sTemplateName, $iSurveyGroupId, $iSurveyId);
}

// If no row found, or if the template folder for this configuration row doesn't exist we load the XML config (which will load the default XML)
Expand Down
112 changes: 112 additions & 0 deletions application/models/TemplateConfiguration.php
Expand Up @@ -130,6 +130,118 @@ public function attributeLabels()
);
}

/**
* Gets an instance of a templateconfiguration by name
*
* @param [String] $sTemplateName
* @return TemplateConfiguration
*/
public static function getInstanceFromTemplateName($sTemplateName){
return self::model()->find(
'templates_name=:templates_name AND sid IS NULL AND gsid IS NULL',
array(':templates_name'=>$sTemplateName)
);
}

/**
* Returns a TemplateConfiguration Object based on a surveygroup ID
* If no instance is existing, it will create one.
*
* @param [Integer] $iSurveyGroupId
* @param [String] $sTemplateName
* @return TemplateConfiguration
*/
public static function getInstanceFromSurveyGroup($iSurveyGroupId, $sTemplateName=null){

//if a template name is given also check against that
$sTemplateName = $sTemplateName != null ? $sTemplateName : SurveysGroups::model()->findByPk($iSurveyGroupId)->template;

$criteria = new CDbCriteria();
$criteria->addCondition('gsid=:gsid');
$criteria->addCondition('templates_name=:templates_name');
$criteria->params = array('gsid' => $iSurveyGroupId, 'templates_name' => $sTemplateName);
$oTemplateConfigurationModel = TemplateConfiguration::model()->find($criteria);

// No specific template configuration for this surveygroup => create one
// TODO: Move to SurveyGroup creation, right now the 'lazy loading' approach is ok.
if (!is_a($oTemplateConfigurationModel, 'TemplateConfiguration') && $sTemplateName != null){
$oTemplateConfigurationModel = TemplateConfiguration::getInstanceFromTemplateName($sTemplateName);
$oTemplateConfigurationModel->id = null;
$oTemplateConfigurationModel->isNewRecord = true;
$oTemplateConfigurationModel->gsid = $iSurveyGroupId;
$oTemplateConfigurationModel->setToInherit();
$oTemplateConfigurationModel->save();
}

return $oTemplateConfigurationModel;

}

/**
* Returns a TemplateConfiguration Object based on a surveyID
* If no instance is existing, it will create one.
*
* @param [Integer] $iSurveyId
* @param [String] $sTemplateName
* @return TemplateConfiguration
*/
public static function getInstanceFromSurveyId($iSurveyId, $sTemplateName=null){

//if a template name is given also check against that
$sTemplateName = $sTemplateName!=null ? $sTemplateName : Survey::model()->findByPk($iSurveyId)->template;

$criteria = new CDbCriteria();
$criteria->addCondition('sid=:sid');
$criteria->addCondition('templates_name=:templates_name');
$criteria->params = array('sid' => $iSurveyId, 'templates_name' => $sTemplateName);

$oTemplateConfigurationModel = TemplateConfiguration::model()->find($criteria);


// No specific template configuration for this surveygroup => create one
// TODO: Move to SurveyGroup creation, right now the 'lazy loading' approach is ok.
if (!is_a($oTemplateConfigurationModel, 'TemplateConfiguration') && $sTemplateName != null){
$oTemplateConfigurationModel = TemplateConfiguration::getInstanceFromTemplateName($sTemplateName);
$oTemplateConfigurationModel->id = null;
$oTemplateConfigurationModel->isNewRecord = true;
$oTemplateConfigurationModel->sid = $iSurveyId;
$oTemplateConfigurationModel->gsid = Survey::model()->findByPk($iSurveyId)->gsid;
$oTemplateConfigurationModel->setToInherit();
$oTemplateConfigurationModel->save();
}

return $oTemplateConfigurationModel;
}

/**
* Get an instance of a fitting TemplateConfiguration
*
* @param [String] $sTemplateName
* @param [Integer] $iSurveyGroupId
* @param [Integer] $iSurveyId
* @return TemplateConfiguration
*/
public static function getInstance($sTemplateName=null, $iSurveyGroupId=null, $iSurveyId=null){

$oTemplateConfigurationModel = new TemplateConfiguration();

if ($sTemplateName!=null){
$oTemplateConfigurationModel = @TemplateConfiguration::getInstanceFromTemplateName($sTemplateName);
}

if($iSurveyGroupId!=null && $iSurveyId==null) {
$oTemplateConfigurationModel = @TemplateConfiguration::getInstanceFromSurveyGroup($iSurveyGroupId, $sTemplateName);
}

if($iSurveyId!=null) {
$oTemplateConfigurationModel = @TemplateConfiguration::getInstanceFromSurveyId($iSurveyId, $sTemplateName);
}

return $oTemplateConfigurationModel;

}


/**
* Retrieves a list of models based on the current search/filter conditions.
*
Expand Down
9 changes: 7 additions & 2 deletions application/views/admin/survey/surveySummary_view.php
Expand Up @@ -523,8 +523,13 @@
<?php $templatename = $oSurvey->template;
if (Permission::model()->hasGlobalPermission('templates','read'))
{
$templateurl_url = $this->createAbsoluteUrl("admin/templates/sa/view/editfile/startpage.pstpl/screenname/welcome",array('templatename'=>$templatename)); ?>
<a href='<?php echo $templateurl_url?>' target='_blank'><?php echo $templatename; ?></a>
$sTemplateOptionsUrl = $this->createUrl("admin/templateoptions/sa/updatesurvey",array('surveyid'=>$oSurvey->sid, "gsid"=>$oSurvey->gsid));
$sTemplateEditorUrl = $this->createUrl("admin/templates/sa/view",array('templatename' => $oSurvey->template));
//$sTemplateEditorUrl = $this->createUrl("admin/templates/sa/view",array('editfile'=>'layout_first_page.twig', "screenname"=>'welcome', 'template' => $oSurvey->template));
?>
<?php echo $templatename; ?>
<a href='<?=$sTemplateOptionsUrl?>' title="<?php eT("Open template options"); ?>" class="btn btn-default btn-xs"><i class="fa fa-paint-brush"></i></a>
<a href='<?=$sTemplateEditorUrl?>' title="<?php eT("Open template editor in new window"); ?>" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-object-group"></i></a>
<?php
}
else
Expand Down

0 comments on commit 78cb6fa

Please sign in to comment.