Skip to content

Commit

Permalink
Dev: survey summary view based on twig template panels
Browse files Browse the repository at this point in the history
  • Loading branch information
lacrioque committed Feb 4, 2018
1 parent 4aea797 commit d09bcb1
Show file tree
Hide file tree
Showing 13 changed files with 483 additions and 408 deletions.
81 changes: 43 additions & 38 deletions application/config/internal.php
Expand Up @@ -211,42 +211,47 @@
'html' => 'CHtml'
),
'functions' => array(
'flatEllipsizeText' => 'viewHelper::flatEllipsizeText',
'getLanguageData' => 'viewHelper::getLanguageData',
'array_flip' => 'array_flip',
'array_intersect_key' => 'array_intersect_key',
'registerPublicCssFile' => 'LS_Twig_Extension::registerPublicCssFile',
'registerTemplateCssFile' => 'LS_Twig_Extension::registerTemplateCssFile',
'registerGeneralScript' => 'LS_Twig_Extension::registerGeneralScript',
'registerTemplateScript' => 'LS_Twig_Extension::registerTemplateScript',
'registerScript' => 'LS_Twig_Extension::registerScript',
'registerPackage' => 'LS_Twig_Extension::registerPackage',
'unregisterPackage' => 'LS_Twig_Extension::unregisterPackage',
'registerScriptFile' => 'LS_Twig_Extension::registerScriptFile',
'registerCssFile' => 'LS_Twig_Extension::registerCssFile',
'unregisterScriptFile' => 'LS_Twig_Extension::unregisterScriptFile',
'unregisterScriptForAjax' => 'LS_Twig_Extension::unregisterScriptForAjax',
'listCoreScripts' => 'LS_Twig_Extension::listCoreScripts',
'listScriptFiles' => 'LS_Twig_Extension::listScriptFiles',
'getAllQuestionClasses' => 'LS_Twig_Extension::getAllQuestionClasses',
'intval' => 'intval',
'empty' => 'empty',
'count' => 'count',
'reset' => 'reset',
'renderCaptcha' => 'LS_Twig_Extension::renderCaptcha',
'getPost' => 'LS_Twig_Extension::getPost',
'getParam' => 'LS_Twig_Extension::getParam',
'getQuery' => 'LS_Twig_Extension::getQuery',
'isset' => 'isset',
'str_replace' => 'str_replace',
'assetPublish' => 'LS_Twig_Extension::assetPublish',
'image' => 'LS_Twig_Extension::image',
'imageSrc' => 'LS_Twig_Extension::imageSrc',
'sprintf' => 'sprintf',
'gT' => 'gT',
'ngT' => 'ngT',
'createUrl' => 'LS_Twig_Extension::createUrl',
'json_decode' => 'LS_Twig_Extension::json_decode',
'flatEllipsizeText' => 'viewHelper::flatEllipsizeText',
'flattenText' => 'flattenText',
'getLanguageData' => 'viewHelper::getLanguageData',
'array_flip' => 'array_flip',
'array_intersect_key' => 'array_intersect_key',
'registerPublicCssFile' => 'LS_Twig_Extension::registerPublicCssFile',
'registerTemplateCssFile' => 'LS_Twig_Extension::registerTemplateCssFile',
'registerGeneralScript' => 'LS_Twig_Extension::registerGeneralScript',
'registerTemplateScript' => 'LS_Twig_Extension::registerTemplateScript',
'registerScript' => 'LS_Twig_Extension::registerScript',
'registerPackage' => 'LS_Twig_Extension::registerPackage',
'unregisterPackage' => 'LS_Twig_Extension::unregisterPackage',
'registerScriptFile' => 'LS_Twig_Extension::registerScriptFile',
'registerCssFile' => 'LS_Twig_Extension::registerCssFile',
'unregisterScriptFile' => 'LS_Twig_Extension::unregisterScriptFile',
'unregisterScriptForAjax' => 'LS_Twig_Extension::unregisterScriptForAjax',
'listCoreScripts' => 'LS_Twig_Extension::listCoreScripts',
'listScriptFiles' => 'LS_Twig_Extension::listScriptFiles',
'getAllQuestionClasses' => 'LS_Twig_Extension::getAllQuestionClasses',
'getExpressionManagerOutput' => 'LS_Twig_Extension::getExpressionManagerOutput',
'checkPermission' => 'LS_Twig_Extension::checkPermission',
'intval' => 'intval',
'empty' => 'empty',
'count' => 'count',
'reset' => 'reset',
'getLanguageNameFromCode' => 'getLanguageNameFromCode',
'renderCaptcha' => 'LS_Twig_Extension::renderCaptcha',
'getPost' => 'LS_Twig_Extension::getPost',
'getParam' => 'LS_Twig_Extension::getParam',
'getQuery' => 'LS_Twig_Extension::getQuery',
'isset' => 'isset',
'str_replace' => 'str_replace',
'assetPublish' => 'LS_Twig_Extension::assetPublish',
'image' => 'LS_Twig_Extension::image',
'imageSrc' => 'LS_Twig_Extension::imageSrc',
'sprintf' => 'sprintf',
'gT' => 'gT',
'ngT' => 'ngT',
'createUrl' => 'LS_Twig_Extension::createUrl',
'createAbsoluteUrl' => 'LS_Twig_Extension::createAbsoluteUrl',
'json_decode' => 'LS_Twig_Extension::json_decode',
),
'filters' => array(
'jencode' => 'CJSON::encode',
Expand All @@ -256,7 +261,7 @@

'sandboxConfig' => array(
'tags' => array('if', 'for', 'set', 'autoescape', 'block', 'embed', 'use', 'include'),
'filters' => array('escape', 'raw', 't', 'merge', 'length', 'gT', 'keys', 'date', 'format'),
'filters' => array('escape', 'raw', 't', 'merge', 'length', 'gT', 'keys', 'date', 'format', 'round'),
'methods' => array(
'ETwigViewRendererStaticClassProxy' => array("encode", "textfield", "form", "link", "emailField", "beginForm", "endForm", "dropDownList", "htmlButton", "passwordfield"),
'Survey' => array("getAllLanguages", "localizedtitle"),
Expand All @@ -271,7 +276,7 @@
'Question' => array('qid', 'parent_qid', 'sid', 'gid', 'type', 'title', 'question', 'help', 'other', 'mandatory', 'language', 'scale_qid'),
'QuestionGroups' => array('gid', 'sid', 'group_name', 'group_order', 'description', 'language', 'randomization_group', 'grelevance')
),
'functions' => array('include', 'dump', 'flatEllipsizeText', 'getLanguageData', 'array_flip', 'array_intersect_key', 'registerPublicCssFile', 'registerTemplateCssFile', 'registerGeneralScript', 'registerTemplateScript', 'registerScript', 'registerPackage', 'unregisterPackage', 'registerCssFile', 'registerScriptFile', 'unregisterScriptFile', 'unregisterScriptForAjax', 'listCoreScripts', 'listScriptFiles', 'getAllQuestionClasses', 'intval', 'count', 'empty', 'reset', 'renderCaptcha', 'getPost', 'getParam', 'getQuery', 'isset', 'str_replace', 'assetPublish', 'image', 'imageSrc', 'sprintf', 'gT', 'ngT', 'createUrl', 'json_decode'),
'functions' => array('include', 'dump', 'flatEllipsizeText', 'flattenText', 'getLanguageData', 'array_flip', 'array_intersect_key', 'checkPermission', 'registerPublicCssFile', 'registerTemplateCssFile', 'registerGeneralScript', 'registerTemplateScript', 'registerScript', 'registerPackage', 'unregisterPackage', 'registerCssFile', 'registerScriptFile', 'unregisterScriptFile', 'unregisterScriptForAjax', 'getLanguageNameFromCode', 'listCoreScripts', 'listScriptFiles', 'getAllQuestionClasses', 'getExpressionManagerOutput', 'intval', 'count', 'empty', 'reset', 'renderCaptcha', 'getPost', 'getParam', 'getQuery', 'isset', 'str_replace', 'assetPublish', 'image', 'imageSrc', 'sprintf', 'gT', 'ngT', 'createAbsoluteUrl', 'createUrl', 'json_decode'),
),

),
Expand Down
125 changes: 79 additions & 46 deletions application/core/LSETwigViewRenderer.php
Expand Up @@ -24,52 +24,82 @@ class LSETwigViewRenderer extends ETwigViewRenderer
/**
* @var array Twig_Extension_Sandbox configuration
*/
public $sandboxConfig = array();
private $_twig;

/**
* Main method to render a survey.
* @param string $sLayout the name of the layout to render
* @param array $aDatas the datas needed to fill the layout
* @param boolean $bReturn if true, it will return the html string without rendering the whole page. Usefull for debuging, and used for Print Answers
*/
public function renderTemplateFromFile($sLayout, $aDatas, $bReturn)
{
$oTemplate = Template::getInstance();
$oLayoutTemplate = $this->getTemplateForView($sLayout, $oTemplate);
if ($oLayoutTemplate) {
$line = file_get_contents($oLayoutTemplate->viewPath.$sLayout);
$sHtml = $this->convertTwigToHtml($line, $aDatas, $oTemplate);

if ($bReturn) {
return $sHtml;
} else {
$this->renderHtmlPage($sHtml, $oTemplate);
}
public $sandboxConfig = array();
private $_twig;

/**
* Main method to render a survey.
* @param string $sLayout the name of the layout to render
* @param array $aDatas the datas needed to fill the layout
* @param boolean $bReturn if true, it will return the html string without rendering the whole page. Usefull for debuging, and used for Print Answers
*/
public function renderTemplateFromFile($sLayout, $aDatas, $bReturn)
{
$oTemplate = Template::getInstance();
$oLayoutTemplate = $this->getTemplateForView($sLayout, $oTemplate);
if ($oLayoutTemplate) {
$line = file_get_contents($oLayoutTemplate->viewPath.$sLayout);
$sHtml = $this->convertTwigToHtml($line, $aDatas, $oTemplate);

if ($bReturn) {
return $sHtml;
} else {
$templateDbConf = Template::getTemplateConfiguration($oTemplate->template_name, null, null, true);
// A possible solution to this error is to re-install the template.
if ($templateDbConf->config->metadata->version != $oTemplate->template->version) {
throw new WrongTemplateVersionException(
sprintf(
gT("Can't render layout %s for template %s. Template version in database is %s, but in config.xml it's %s. Please re-install the template."),
$sLayout,
$oTemplate->template_name,
$oTemplate->template->version,
$templateDbConf->config->metadata->version
)
);
}
// TODO: Panic or default to something else?
throw new CException(
$this->renderHtmlPage($sHtml, $oTemplate);
}
} else {
$templateDbConf = Template::getTemplateConfiguration($oTemplate->template_name, null, null, true);
// A possible solution to this error is to re-install the template.
if ($templateDbConf->config->metadata->version != $oTemplate->template->version) {
throw new WrongTemplateVersionException(
sprintf(
gT("Can't render layout %s for template %s. Please try to re-install the template."),
gT("Can't render layout %s for template %s. Template version in database is %s, but in config.xml it's %s. Please re-install the template."),
$sLayout,
$oTemplate->template_name
$oTemplate->template_name,
$oTemplate->template->version,
$templateDbConf->config->metadata->version
)
);
}
// TODO: Panic or default to something else?
throw new CException(
sprintf(
gT("Can't render layout %s for template %s. Please try to re-install the template."),
$sLayout,
$oTemplate->template_name
)
);
}
}


/**
* Main method to render a admin page or block.
* Extendable to use admin templates in the future currently running on pathes, like the yii render methods go.
* @param string $sLayout the name of the layout to render
* @param array $aDatas the datas needed to fill the layout
* @param boolean $bReturn if true, it will return the html string without rendering the whole page. Usefull for debuging, and used for Print Answers
*/
public function renderViewFromFile($sLayoutFilePath, $aDatas, $bReturn=false)
{
$viewFile = Yii::app()->getConfig('rootdir').$sLayoutFilePath;
if (file_exists($viewFile)) {
$line = file_get_contents($viewFile);
$sHtml = $this->convertTwigToHtml($line, $aDatas);

if ($bReturn) {
return $sHtml;
} else {
$this->renderHtmlPage($sHtml, $oTemplate);
}
} else {
throw new CException(
sprintf(
gT("Can't render layout %s. Please check the view exists or contact your admin."),
$sLayoutFilePath
)
);
}
}

/**
* This method is called from qanda helper to render a question view file.
Expand Down Expand Up @@ -213,19 +243,22 @@ private function renderHtmlPage($sHtml, $oTemplate)
* @param TemplateConfiguration $oTemplate
* @return string
*/
public function convertTwigToHtml($sString, $aDatas, $oTemplate)
public function convertTwigToHtml($sString, $aDatas, $oTemplate = null)
{
// Twig init
$this->_twig = $twig = parent::getTwig();

// Get the additional infos for the view, such as language, direction, etc
$aDatas = $this->getAdditionalInfos($aDatas, $oTemplate);
//Run theme related things only if a theme is provided!
if($oTemplate !== null ) {
// Get the additional infos for the view, such as language, direction, etc
$aDatas = $this->getAdditionalInfos($aDatas, $oTemplate);

// Add to the loader the path of the template and its parents.
$this->addRecursiveTemplatesPath($oTemplate);
// Add to the loader the path of the template and its parents.
$this->addRecursiveTemplatesPath($oTemplate);

// Plugin for blocks replacement
list($sString, $aDatas) = $this->getPluginsData($sString, $aDatas);
// Plugin for blocks replacement
list($sString, $aDatas) = $this->getPluginsData($sString, $aDatas);
}

// Twig rendering
$oTwigTemplate = $twig->createTemplate($sString);
Expand Down
35 changes: 35 additions & 0 deletions application/core/LS_Twig_Extension.php
Expand Up @@ -229,6 +229,11 @@ public static function createUrl($url, $params = array())
return App()->getController()->createUrl($url, $params);
}

public static function createAbsoluteUrl($url, $params = array())
{
return App()->getController()->createAbsoluteUrl($url, $params);
}

/**
* @param string $sRessource
*/
Expand Down Expand Up @@ -279,6 +284,36 @@ public static function imageSrc($sImagePath, $default = './files/pattern.png')
return $sUrlImgAsset;
}


/**
* Get the parsed output of the expression manger for a specific string
*
* @param String $sInString
* @return String
*/
public static function getExpressionManagerOutput($sInString){
templatereplace(flattenText($sInString));
return LimeExpressionManager::GetLastPrettyPrintExpression();
}

/**
* Checks for a permission on render
*
* @param String $permission
* @param String $permissionGrade
* @param Integer|NULL $iSurveyId (default null)
*
* @return Boolean
*/
public static function checkPermission($permission, $permissionGrade, $iSurveyId = null) {

if($iSurveyId === null){
return Permission::model()->hasGlobalPermission($permission,$permissionGrade);
}
return Permission::model()->hasSurveyPermission($iSurveyId,$permission,$permissionGrade);

}

/**
* @param string $sRessource
*/
Expand Down

3 comments on commit d09bcb1

@TonisOrmisson
Copy link
Collaborator

Choose a reason for hiding this comment

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

Deleting the upload/themes/survey/index.html and upload/surveys/index.html will break the tests and a system wit h no uploaded themes. This issue has been around for multiple times now :)

@lacrioque
Copy link
Contributor Author

@lacrioque lacrioque commented on d09bcb1 Feb 4, 2018 via email

Choose a reason for hiding this comment

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

@TonisOrmisson
Copy link
Collaborator

Choose a reason for hiding this comment

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

I also seem to have some funny business with git ignoring those files for some reason.

Please sign in to comment.