Skip to content

Commit

Permalink
Dev: added some missing function description and updated code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
ptelu committed Jun 12, 2023
1 parent 4f738ce commit ceb569c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 49 deletions.
26 changes: 17 additions & 9 deletions application/controllers/admin/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,12 @@ public function templatecopy()
}

/**
* Function responsible to delete a template.
* Function responsible to delete a template while inside the theme editor
*
* @access public
* @param string $templatename
* @return void
* @throws CDbException
* @throws CException
*/
public function delete()
{
Expand Down Expand Up @@ -828,27 +829,34 @@ public function deleteBrokenTheme()
}


/**
* Deletes a survey theme from the "Available survey themes", after it has been uninstalled TemplateConfig::uninstall.
* This will delete all local files.
* @return void
* @throws CException
* @throws CHttpException
*/
public function deleteAvailableTheme()
{
$templatename = trim(Yii::app()->request->getPost('templatename', ''));
$templatename = trim(App()->request->getPost('templatename', ''));

if (Permission::model()->hasGlobalPermission('templates', 'delete')) {
$completeFileName = realpath(Yii::app()->getConfig('userthemerootdir') . "/" . $templatename);
$completeFileName = realpath(App()->getConfig('userthemerootdir') . "/" . $templatename);
/* If retuirn false, not a dir or not inside userthemerootdir: try to hack : throw a 403 for security */
if (!is_dir($completeFileName) || substr($completeFileName, 0, strlen((string) Yii::app()->getConfig('userthemerootdir'))) !== Yii::app()->getConfig('userthemerootdir')) {
if (!is_dir($completeFileName) || !str_starts_with($completeFileName, App()->getConfig('userthemerootdir'))) {
throw new CHttpException(403, "Disable for security reasons.");
}
// CheckIfTemplateExists check if the template is installed....
Yii::import('application.helpers.SurveyThemeHelper');
if (! Template::checkIfTemplateExists($templatename) && !SurveyThemeHelper::isStandardTemplate($templatename)) {
if (rmdirr(Yii::app()->getConfig('userthemerootdir') . "/" . $templatename)) {
Yii::app()->setFlashMessage(sprintf(gT("Theme '%s' was successfully deleted."), $templatename));
if (rmdirr(App()->getConfig('userthemerootdir') . "/" . $templatename)) {
App()->setFlashMessage(sprintf(gT("Theme '%s' was successfully deleted."), $templatename));
} else {
Yii::app()->setFlashMessage(sprintf(gT("There was a problem deleting the template '%s'. Please check your directory/file permissions."), $templatename), 'error');
App()->setFlashMessage(sprintf(gT("There was a problem deleting the template '%s'. Please check your directory/file permissions."), $templatename), 'error');
}
} else {
// This should never happen... trying to submit the form via a script? so no translation
Yii::app()->setFlashMessage("You're trying to delete a theme that is installed. Please, uninstall it first", 'error');
App()->setFlashMessage("You're trying to delete a theme that is installed. Please, uninstall it first", 'error');
}
}

Expand Down
1 change: 1 addition & 0 deletions application/models/ExtensionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function isCompatible()
}

/**
* Reads xml from file and creates an instance of ExtensionConfig
* @param string $file Full file path.
* @return ExtensionConfig
*/
Expand Down
39 changes: 22 additions & 17 deletions application/models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public function primaryKey()
*
* @param string $sTemplateName
* @return string existing $sTemplateName
* @throws Exception
*/
public static function templateNameFilter($sTemplateName)
{
Expand All @@ -147,7 +148,7 @@ public static function templateNameFilter($sTemplateName)
/* Validate if template is OK in user dir, DIRECTORY_SEPARATOR not needed "/" is OK */
$oTemplate = self::model()->findByPk($sTemplateName);

if (is_object($oTemplate) && $oTemplate->checkTemplate() && (self::checkTemplateXML($oTemplate->folder))) {
if (!empty($oTemplate) && $oTemplate->checkTemplate() && (self::checkTemplateXML($oTemplate->name, $oTemplate->folder))) {
self::$aNamesFiltered[$sTemplateName] = $sTemplateName;
return self::$aNamesFiltered[$sTemplateName];
}
Expand Down Expand Up @@ -207,21 +208,25 @@ public function checkTemplate()
*/
public function checkTemplateExtends()
{
if (!empty($this->extends)) {
$oRTemplate = self::model()->findByPk($this->extends);
if (empty($oRTemplate)) {
// Why? it blocks the user at login screen....
// It should return false and show a nice warning message.

/*throw new Exception(
sprintf(
'Extended template "%s" is not installed.',
$this->extends
)
);*/
}
}
/**
* TODO: the following code needs to be rewritten, currently the only thing it does is return true, that why it is commented out
*/
return true;
// if (!empty($this->extends)) {
// $oRTemplate = self::model()->findByPk($this->extends);
// if (empty($oRTemplate)) {
// // Why? it blocks the user at login screen....
// // It should return false and show a nice warning message.
//
// /*throw new Exception(
// sprintf(
// 'Extended template "%s" is not installed.',
// $this->extends
// )
// );*/
// }
// }
// return true;
}

/**
Expand Down Expand Up @@ -711,7 +716,7 @@ public function search()
*/
public static function getDeprecatedTemplates()
{
$usertemplaterootdir = Yii::app()->getConfig("uploaddir") . DIRECTORY_SEPARATOR . "templates";
$usertemplaterootdir = App()->getConfig("uploaddir") . DIRECTORY_SEPARATOR . "templates";
$aTemplateList = array();

if ((is_dir($usertemplaterootdir)) && $usertemplaterootdir && $handle = opendir($usertemplaterootdir)) {
Expand All @@ -734,7 +739,7 @@ public static function getDeprecatedTemplates()
public static function getBrokenThemes($sFolder = null)
{
$aBrokenTemplateList = array();
$sFolder = (empty($sFolder)) ? Yii::app()->getConfig("userthemerootdir") : $sFolder;
$sFolder = (empty($sFolder)) ? App()->getConfig("userthemerootdir") : $sFolder;

if ($sFolder && $handle = opendir($sFolder)) {
while (false !== ($sFileName = readdir($handle))) {
Expand Down
9 changes: 5 additions & 4 deletions application/models/TemplateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,19 +864,20 @@ public function __toString()
}

/**
* @todo document me
* Uninstalls the selected surveytheme and deletes database entry and configuration
* @param string $templatename Name of Template
* @return bool|int
* @throws CDbException
*/
public static function uninstall($templatename)
{
if (Permission::model()->hasGlobalPermission('templates', 'delete')) {
$oTemplate = Template::model()->findByAttributes(array('name' => $templatename));
$oTemplate = Template::model()->findByAttributes(['name' => $templatename]);
if ($oTemplate) {
if ($oTemplate->delete()) {
return TemplateConfiguration::model()->deleteAll(
'template_name=:templateName',
array(':templateName' => $templatename)
[':templateName' => $templatename]
);
}
}
Expand Down Expand Up @@ -1006,7 +1007,7 @@ public function getAllDbTemplateFolders()
}

/**
* @todo document me
* Returns an array with uninstalled or incompatible survey themes
* @return array|null
*/
public function getTemplatesWithNoDb()
Expand Down
12 changes: 6 additions & 6 deletions application/models/TemplateConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public function checkTemplate()
$this->bTemplateCheckResult = true;
if (
!is_object($this->template) ||
(is_object($this->template) && !Template::checkTemplateXML($this->template->folder))
(is_object($this->template) && !Template::checkTemplateXML($this->template->name, $this->template->folder))
) {
$this->bTemplateCheckResult = false;
}
Expand Down Expand Up @@ -1035,7 +1035,6 @@ public function getOptionPage()

$oTemplate->setToInherit();
$oTemplate->setOptions();
$oTemplate->setOptionInheritance();

$oOptions = (array) $oSimpleInheritanceTemplate->oOptions;

Expand Down Expand Up @@ -1267,7 +1266,8 @@ private function setCssFramework()
}

/**
* @todo document me
* Decodes json string from the database field "options" and stores it inside $this->oOptions
* Also triggers inheritence checks
* @return void
*/
protected function setOptions()
Expand All @@ -1283,7 +1283,8 @@ protected function setOptions()
}

/**
* @todo document me
* Loop through all theme options defined, trigger check for inheritance and write the new value back to the options object
* @return void
*/
protected function setOptionInheritance()
{
Expand All @@ -1297,8 +1298,7 @@ protected function setOptionInheritance()
}

/**
* @todo document me
*
* Search through the inheritence chain and find the inherited value for theme option
* @param string $key
* @return mixed
*/
Expand Down
6 changes: 5 additions & 1 deletion application/models/TemplateManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,11 @@ public function getTemplateURL()
}


// TODO: please write documentation here, seems to be no entrypoints for this
/**
* Get buttons/actions for the "Available admin themes", not installed
* @return string
* @throws CException
*/
public function getButtons()
{
$sEditorUrl = Yii::app()->getController()->createUrl('admin/themes/sa/view', array("templatename" => $this->sTemplateName));
Expand Down
25 changes: 13 additions & 12 deletions application/views/themeOptions/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* @var bool $canImport
* @var string $importErrorMessage
* @var object $oQuestionTheme
* @var object $oSurveyTheme
* @var TemplateConfig $oSurveyTheme
* @var int $pageSize
*/

// TODO: rename to template_list.php and move to template controller
Expand Down Expand Up @@ -34,13 +35,14 @@
<div class="tab-content">
<div id="surveythemes" class="tab-pane active">
<div class="list-surveys">
<?php echo '<h3>' . gT('Installed survey themes:') . '</h3>'; ?>
<?php $this->renderPartial(
'./surveythemelist',
['oSurveyTheme' => $oSurveyTheme, 'pageSize' => $pageSize]
<h3><?= gT('Installed survey themes:') ?></h3>
<?php $this->renderPartial('./surveythemelist', [
'oSurveyTheme' => $oSurveyTheme,
'pageSize' => $pageSize
]
); ?>
<!-- Available Themes -->
<?php if (count($oSurveyTheme->templatesWithNoDb) > 0) : ?>
<?php if (count($oSurveyTheme->getTemplatesWithNoDb()) > 0) : ?>
<h3><?php eT('Available survey themes:'); ?></h3>
<div id="templates_no_db" >
<table class="items table table-hover">
Expand All @@ -57,14 +59,14 @@

<tbody>
<?php foreach ($oSurveyTheme->templatesWithNoDb as $oTemplate) : ?>
<?php // echo $oTemplate; ?>
<?php /** @var TemplateManifest $oTemplate */ ?>
<tr class="odd">
<td class="col-lg-1"><?php echo $oTemplate->preview; ?></td>
<td class="col-lg-1"><?php echo $oTemplate->getPreview(); ?></td>
<td class="col-lg-2"><?php echo $oTemplate->sTemplateName; ?></td>
<td class="col-lg-3"><?php echo $oTemplate->description; ?></td>
<td class="col-lg-3"><?php echo $oTemplate->getDescription(); ?></td>
<td class="col-lg-2"><?php eT('XML themes'); ?></td>
<td class="col-lg-1"><?php echo $oTemplate->config->metadata->extends; ?></td>
<td class="col-lg-2"><?php echo $oTemplate->buttons; ?></td>
<td class="col-lg-2"><?php echo $oTemplate->getButtons(); ?></td>
</tr>
<?php endforeach; ?>

Expand Down Expand Up @@ -150,8 +152,7 @@ class="btn btn-danger btn-sm selector--ConfirmModal">
<td class="col-lg-9"><?php echo $aDeprecatedTheme['name']; ?></td>
<td class="col-lg-2">
<div class="d-grid gap-2">
<?php if (Permission::model()->hasGlobalPermission('templates',
'export') && class_exists('ZipArchive')) : ?>
<?php if (Permission::model()->hasGlobalPermission('templates', 'export') && class_exists('ZipArchive')) : ?>
<a class="btn btn-outline-secondary btn-sm" id="button-export"
href="<?php echo $this->createUrl('admin/themes/sa/deprecatedtemplatezip/templatename/' . $aDeprecatedTheme['name']) ?>"
role="button">
Expand Down

0 comments on commit ceb569c

Please sign in to comment.