Skip to content

Commit

Permalink
Fixed issue #15940: Export as printable - PHP Notice with debug mode …
Browse files Browse the repository at this point in the history
…on (#1449)

Dev: Added method/attribute on survey model for getting the effective template name (handling survey group inheritance). Dev: Used that method/attribute while exporting printable surveys.
Dev: Added some existence checkings on getTemplateUrl() and getTemplatePath()
  • Loading branch information
gabrieljenik authored and olleharstedt committed Jul 1, 2020
1 parent 0de5e51 commit f16b90e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions application/controllers/admin/export.php
Expand Up @@ -1260,8 +1260,8 @@ public function quexml($iSurveyID)
private function _exportPrintableHtmls($iSurveyID, $readFile = true)
{
$oSurvey = Survey::model()->findByPk($iSurveyID);
$assetsDir = substr(Template::getTemplateURL($oSurvey->template), 1);
$fullAssetsDir = Template::getTemplatePath($oSurvey->template);
$assetsDir = substr(Template::getTemplateURL($oSurvey->templateEffectiveName), 1);
$fullAssetsDir = Template::getTemplatePath($oSurvey->templateEffectiveName);
$aLanguages = $oSurvey->getAllLanguages();

Yii::import('application.helpers.common_helper', true);
Expand Down Expand Up @@ -1322,7 +1322,7 @@ private function _exportPrintableHtml($oSurvey, $language, $tempdir)
$file = "$tempdir/questionnaire_{$oSurvey->getPrimaryKey()}_{$language}.html";

// remove first slash to get local path for local storage for template assets
$templateDir = Template::getTemplateURL($oSurvey->template);
$templateDir = Template::getTemplateURL($oSurvey->templateEffectiveName);
$response = str_replace($templateDir, substr($templateDir, 1), $response);

file_put_contents($file, $response);
Expand Down
27 changes: 27 additions & 0 deletions application/models/Survey.php
Expand Up @@ -835,6 +835,33 @@ public function getSurveyTemplateConfiguration()
return TemplateConfiguration::getInstance(null, null, $this->sid);
}

/**
* Returns the name of the template to be used for the survey.
* It resolves inheritance from group and from default settings.
*
* @return string
*
* @todo: Cache this on a private attribute?
*/
public function getTemplateEffectiveName()
{
// Fetch template name from model
// This was already filtered on afterFind, so if the one at load time is not valid, will be replaced by default one
// If it is "inherit", means it will inherit from group, so we will replace it.
$sTemplateName = $this->template;

// if it is "inherit", get template name form group
if ($sTemplateName == 'inherit') {
if (!empty($this->oOptions->template)) {
$sTemplateName = $this->oOptions->template;
} else {
throw new CException("Unable to get a template name from group for survey {$this->sid}");
}
}

return $sTemplateName;
}

/**
* Get surveymenu configuration
* This will be made bigger in future releases, but right now it only collects the default menu-entries
Expand Down
13 changes: 13 additions & 0 deletions application/models/Template.php
Expand Up @@ -284,12 +284,20 @@ public static function checkIfTemplateExists($sTemplateName)
*/
public static function getTemplatePath($sTemplateName = "")
{
// Make sure template name is valid
if (!self::checkIfTemplateExists($sTemplateName)) {
throw new \CException("Invalid {$sTemplateName} template directory");
}

static $aTemplatePath = array();
if (isset($aTemplatePath[$sTemplateName])) {
return $aTemplatePath[$sTemplateName];
}

$oTemplate = self::model()->findByPk($sTemplateName);
if (empty($oTemplate)) {
throw new \CException("Survey theme {$sTemplateName} not found.", 1);
}

if (self::isStandardTemplate($sTemplateName)) {
return $aTemplatePath[$sTemplateName] = Yii::app()->getConfig("standardthemerootdir").DIRECTORY_SEPARATOR.$oTemplate->folder;
Expand Down Expand Up @@ -367,6 +375,11 @@ static public function getOtherFiles($filesDir)
*/
public static function getTemplateURL($sTemplateName = "")
{
// Make sure template name is valid
if (!self::checkIfTemplateExists($sTemplateName)) {
throw new \CException("Invalid {$sTemplateName} template directory");
}

static $aTemplateUrl = array();
if (isset($aTemplateUrl[$sTemplateName])) {
return $aTemplateUrl[$sTemplateName];
Expand Down

0 comments on commit f16b90e

Please sign in to comment.