diff --git a/application/controllers/admin/export.php b/application/controllers/admin/export.php index fd1be4005ad..7c56e24ce73 100644 --- a/application/controllers/admin/export.php +++ b/application/controllers/admin/export.php @@ -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); @@ -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); diff --git a/application/models/Survey.php b/application/models/Survey.php index be42960e6ec..220922d14a6 100755 --- a/application/models/Survey.php +++ b/application/models/Survey.php @@ -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 diff --git a/application/models/Template.php b/application/models/Template.php index abbc295eddf..e39c3400b80 100755 --- a/application/models/Template.php +++ b/application/models/Template.php @@ -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; @@ -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];