Skip to content

Commit

Permalink
Fixed issue #13544: Unable to use flatEllipsizeText function in (some…
Browse files Browse the repository at this point in the history
…) twig file

Dev: Add 2 function : flatString and ellipsizeString
Dev: move flatEllipsizeText to LS_Twig_Extension
Dev: set it at deprecated (4.0)
  • Loading branch information
Shnoulle committed Apr 11, 2018
1 parent 05f7078 commit 092ea04
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 4 deletions.
76 changes: 72 additions & 4 deletions application/config/internal.php
Expand Up @@ -210,10 +210,11 @@
'html' => 'CHtml'
),
'functions' => array(
'flatEllipsizeText' => 'viewHelper::flatEllipsizeText',
'getLanguageData' => 'viewHelper::getLanguageData',
'array_flip' => 'array_flip',
'array_intersect_key' => 'array_intersect_key',

/* clientScript */
'registerPublicCssFile' => 'LS_Twig_Extension::registerPublicCssFile',
'registerTemplateCssFile' => 'LS_Twig_Extension::registerTemplateCssFile',
'registerGeneralScript' => 'LS_Twig_Extension::registerGeneralScript',
Expand All @@ -227,7 +228,13 @@
'unregisterScriptForAjax' => 'LS_Twig_Extension::unregisterScriptForAjax',
'listCoreScripts' => 'LS_Twig_Extension::listCoreScripts',
'listScriptFiles' => 'LS_Twig_Extension::listScriptFiles',
/* String management */
'processString' => 'LS_Twig_Extension::processString',
'flatString' => 'LS_Twig_Extension::flatString',
'ellipsizeString' => 'LS_Twig_Extension::ellipsizeString',
'flatEllipsizeText' => 'LS_Twig_Extension::flatEllipsizeText',
'str_replace' => 'str_replace',

'getAllQuestionClasses' => 'LS_Twig_Extension::getAllQuestionClasses',
'intval' => 'intval',
'empty' => 'empty',
Expand All @@ -238,7 +245,6 @@
'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',
Expand All @@ -257,7 +263,21 @@

'sandboxConfig' => array(
'tags' => array('if', 'for', 'set', 'autoescape', 'block', 'embed', 'use', 'include', 'macro', 'import'),
'filters' => array('escape', 'raw', 't', 'merge', 'length', 'gT', 'keys', 'date', 'format','nl2br','split','trim','json_encode'),
'filters' => array(
'escape',
'raw',
't',
'merge',
'length',
'gT',
'keys',
'date',
'format',
'nl2br',
'split',
'trim',
'json_encode'
),
'methods' => array(
'ETwigViewRendererStaticClassProxy' => array("encode", "textfield", "form", "link", "emailField", "beginForm", "endForm", "dropDownList", "htmlButton", "passwordfield"),
'Survey' => array("getAllLanguages", "localizedtitle"),
Expand All @@ -272,7 +292,55 @@
'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', 'processString', 'getAllQuestionClasses', 'intval', 'count', 'empty', 'reset', 'renderCaptcha', 'getPost', 'getParam', 'getQuery', 'isset', 'str_replace', 'assetPublish', 'image', 'imageSrc', 'sprintf', 'gT', 'ngT', 'createUrl', 'json_decode', 'json_encode'),
'functions' => array(
'getLanguageData',
'array_flip',
'array_intersect_key',

'registerPublicCssFile',
'registerTemplateCssFile',
'registerGeneralScript',
'registerTemplateScript',
'registerScript',
'registerPackage',
'unregisterPackage',
'registerCssFile',
'registerScriptFile',
'unregisterScriptFile',
'unregisterScriptForAjax',
'listCoreScripts',
'listScriptFiles',
'processString',

'flatEllipsizeText',
'flatString',
'ellipsizeString',
'flatEllipsizeText',
'str_replace',

'getAllQuestionClasses',
'intval',
'empty',
'count',
'reset',
'renderCaptcha',
'getPost',
'getParam',
'getQuery',
'isset',
'assetPublish',
'image',
'imageSrc',
'sprintf',
'gT',
'ngT',
'createUrl',
'json_decode',
'json_encode',
/* Not in twigRenderer[functions] */
'include',
'dump',
),
),

),
Expand Down
62 changes: 62 additions & 0 deletions application/core/LS_Twig_Extension.php
Expand Up @@ -415,4 +415,66 @@ public static function processString($string,$static=false,$numRecursionLevels=3
}
return LimeExpressionManager::ProcessStepString($string, $aReplacement,$numRecursionLevels, $static);
}

/**
* Get html text and remove whole not clean string
* @param string $string to flatten
* @param boolean $encode html entities
* @return string
*/
public static function flatString($string,$encode=false)
{
// Remove script before removing tag, no tag : no other script (onload, on error etc …
$string = strip_tags(stripJavaScript($string));
// Remove new lines
if (version_compare(substr(PCRE_VERSION, 0, strpos(PCRE_VERSION, ' ')), '7.0') > -1) {
$string = preg_replace(array('~\R~u'), array(' '), $string);
} else {
$string = str_replace(array("\r\n", "\n", "\r"), array(' ', ' ', ' '), $string);
}
// White space to real space
$string = preg_replace('/\s+/', ' ', $string);

if($encode) {
return \CHtml::encode($string);
}
return $string;
}

/**
* get flat and ellipsize string
* @param string $string to ellipsize
* @param integer $maxlength of the final string
* @param float $position of the ellipsis in string (between 0 and 1)
* @param string $ellipsis string to shown in place of removed part
* @return string
*/
public static function ellipsizeString($string, $maxlength, $position = 1, $ellipsis = '…')
{
$string = self::flatString($string,false);
$string = ellipsize($string, $maxlength, $position, $ellipsis);// Use common_helper function
return $string;
}

/**
* flat and ellipsize text, for template compatibility
* @deprecated (4.0)
* @param string $sString :the string
* @param boolean $bFlat : flattenText or not : completely flat (not like flattenText from common_helper)
* @param integer $iAbbreviated : max string text (if true : allways flat), 0 or false : don't abbreviated
* @param string $sEllipsis if abbreviated : the char to put at end (or middle)
* @param integer $fPosition if abbreviated position to split (in % : 0 to 1)
* @return string
*/
public static function flatEllipsizeText($sString, $bFlat = true, $iAbbreviated = 0, $sEllipsis = '...', $fPosition = 1)
{
if (!$bFlat && !$iAbbreviated) {
return $sString;
}
$sString = self::flatString($sString);
if ($iAbbreviated > 0) {
$sString = ellipsize($sString, $iAbbreviated, $fPosition, $sEllipsis);
}
return $sString;
}
}

0 comments on commit 092ea04

Please sign in to comment.