Skip to content

Commit

Permalink
New feature: Dynamic theme options page rendering
Browse files Browse the repository at this point in the history
Dev: Theme options page is now dynamically generated according to corresponding config.xml file
Dev: new xml attribute engine-->optionspage:
Dev: "core" - use core options view file(design layout can't be customized); "custom" - use customized options.twig view file(design layout can be customized)
Dev: options now use many attributes to define visual appearance
Dev: possible to define new options "on-the-fly"
  • Loading branch information
dominikvitt committed Mar 5, 2019
1 parent cd7635b commit 5b974d2
Show file tree
Hide file tree
Showing 10 changed files with 575 additions and 90 deletions.
14 changes: 13 additions & 1 deletion application/controllers/admin/themeoptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,26 @@ private function _updateCommon(TemplateConfiguration $model, $sid = null,$gsid =

$oModelWithInheritReplacement = TemplateConfiguration::model()->findByPk($model->id);
$templateOptionPage = $oModelWithInheritReplacement->optionPage;
$aOptionAttributes = TemplateManifest::getOptionAttributes($oModelWithInheritReplacement->path);
$aTemplateConfiguration = $oModelWithInheritReplacement->getOptionPageAttributes();
Yii::app()->clientScript->registerPackage('bootstrap-switch', LSYii_ClientScript::POS_BEGIN);

$oSimpleInheritance = Template::getInstance($oModelWithInheritReplacement->sTemplateName, $sid, $gsid, null, true);
$oSimpleInheritance->options = 'inherit';
$oSimpleInheritanceTemplate = $oSimpleInheritance->prepareTemplateRendering($oModelWithInheritReplacement->sTemplateName);
$oParentOptions = (array) $oSimpleInheritanceTemplate->oOptions;
$oParentOptions = TemplateConfiguration::translateOptionLabels($oParentOptions);

$aData = array(
'model'=>$model,
'templateOptionPage' => $templateOptionPage,
'optionInheritedValues' => $oModelWithInheritReplacement->oOptions,
'optionCssFiles' => $oModelWithInheritReplacement->files_css,
'optionCssFramework' => $oModelWithInheritReplacement->cssframework_css,
'sid' => $sid
'aTemplateConfiguration' => $aTemplateConfiguration,
'aOptionAttributes' => $aOptionAttributes,
'sid' => $sid,
'oParentOptions' => $oParentOptions
);

if ($sid !== null) {
Expand Down
3 changes: 3 additions & 0 deletions application/models/TemplateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class TemplateConfig extends CActiveRecord

/** @var array $oOptions The template options */
public $oOptions;
public $oOptionAttributes;


/** @var string[] $depends List of all dependencies (could be more that just the config.xml packages) */
Expand Down Expand Up @@ -90,6 +91,8 @@ class TemplateConfig extends CActiveRecord

public static $aTemplatesWithoutDB = null;

public $options_page = 'core';

/**
* get the template API version
* @return integer
Expand Down
27 changes: 16 additions & 11 deletions application/models/TemplateConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ private function _getImageInfo($file)
return ['preview' => $previewFilePath, 'filepath' => $filePath, 'filepathOptions' => $filePath ,'filename'=>basename($file)];
}

protected function getOptionPageAttributes()
public function getOptionPageAttributes()
{
$aData = $this->attributes;
$fileList = array_merge(Template::getOtherFiles($this->filesPath), Template::getOtherFiles($this->generalFilesPath));
Expand Down Expand Up @@ -865,16 +865,7 @@ public function getOptionPage()
$oTemplate->setOptionInheritance();

$oOptions = (array) $oSimpleInheritanceTemplate->oOptions;
// translation of database values, to match labels on the page
foreach ($oOptions as $key=>$value){
if ($key == 'showpopups'){
$oOptions[$key] = str_replace(array('1', '0', '-1'), array(gT("Popup"), gT("On page"), gT("No")), $value);
} elseif ($key == 'notables'){
$oOptions[$key] = str_replace(array('2', '1', '0'), array(gT("Always on"), gT("Small screens"), gT("Off")), $value);
} else {
$oOptions[$key] = str_replace(array('on', 'off', 'top', 'bottom'), array(gT("Yes"), gT("No"), gT("Top"), gT("Bottom")), $value);
}
}
$oOptions = TemplateConfiguration::translateOptionLabels($oOptions);

//We add some extra values to the option page
//This is just a dirty hack, and somewhere in the future we will correct it
Expand Down Expand Up @@ -1355,4 +1346,18 @@ public function setOptionKeysToInherit(){
$this->options = json_encode($aOptions);
}
}

public static function translateOptionLabels($oOptions){
// translation of database values, to match labels on the page
foreach ($oOptions as $key=>$value){
if ($key == 'showpopups'){
$oOptions[$key] = str_replace(array('1', '0', '-1'), array(gT("Popup"), gT("On page"), gT("No")), $value);
} elseif ($key == 'notables'){
$oOptions[$key] = str_replace(array('2', '1', '0'), array(gT("Always on"), gT("Small screens"), gT("Off")), $value);
} else {
$oOptions[$key] = str_replace(array('on', 'off', 'top', 'bottom'), array(gT("Yes"), gT("No"), gT("Top"), gT("Bottom")), $value);

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Jul 1, 2020

Collaborator

Since there are optionlabels : why we need this ?

}
}
return $oOptions;
}
}
32 changes: 32 additions & 0 deletions application/models/TemplateManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,36 @@ public function getTemplateAndMotherNames()

return $sTemplateNames;
}

/**
* Get options_page value from template configuration
*/
public static function getOptionAttributes($path){
libxml_disable_entity_loader(false);
$file = realpath($path."config.xml");
if (file_exists($file)) {
$sXMLConfigFile = file_get_contents($file);
$oXMLConfig = simplexml_load_string($sXMLConfigFile);
$aOptions['categories'] = array();

foreach($oXMLConfig->options->children() as $key => $option){
$aOptions['optionAttributes'][$key]['type'] = !empty($option['type']) ? (string)$option['type'] : '';
$aOptions['optionAttributes'][$key]['title'] = !empty($option['title']) ? (string)$option['title'] : '';
$aOptions['optionAttributes'][$key]['category'] = !empty($option['category']) ? (string)$option['category'] : gT('Simple options');
$aOptions['optionAttributes'][$key]['width'] = !empty($option['width']) ? (string)$option['width'] : '2';
$aOptions['optionAttributes'][$key]['options'] = !empty($option['options']) ? (string)$option['options'] : '';
$aOptions['optionAttributes'][$key]['optionlabels'] = !empty($option['optionlabels']) ? (string)$option['optionlabels'] : '';
$aOptions['optionAttributes'][$key]['parent'] = !empty($option['parent']) ? (string)$option['parent'] : '';

if (!in_array($aOptions['optionAttributes'][$key]['category'], $aOptions['categories'])){
$aOptions['categories'][] = $aOptions['optionAttributes'][$key]['category'];
}
}

$aOptions['optionsPage'] = !empty((array)$oXMLConfig->engine->optionspage) ? ((array)$oXMLConfig->engine->optionspage)[0] : false;

return $aOptions;
}
return false;
}
}
Loading

2 comments on commit 5b974d2

@maziminke
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great new feature. Please make sure this is documented well, otherwise no one will use it.

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Translation broke other system : please review.

https://bugs.limesurvey.org/view.php?id=16442

My opinon : must be restricted ONLY to specific choose by template option AND must restric to on no %on%

Please sign in to comment.