From e4c818cacb5b6c31ac26dcaaa2f44f5c3f54a70b Mon Sep 17 00:00:00 2001 From: Molkobain Date: Wed, 8 Sep 2021 21:47:05 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B04288=20-=20Portal:=20Update=20TWIG=20ext?= =?UTF-8?q?ensions=20to=20match=20those=20available=20in=20the=20backoffic?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../portal/src/Twig/AppExtension.php | 108 +++++++++++++++++- .../application/TwigBase/Twig/Extension.php | 1 - 2 files changed, 106 insertions(+), 3 deletions(-) diff --git a/datamodels/2.x/itop-portal-base/portal/src/Twig/AppExtension.php b/datamodels/2.x/itop-portal-base/portal/src/Twig/AppExtension.php index 20a33e8ac..73f9c2d68 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/Twig/AppExtension.php +++ b/datamodels/2.x/itop-portal-base/portal/src/Twig/AppExtension.php @@ -21,6 +21,8 @@ namespace Combodo\iTop\Portal\Twig; use Twig\Extension\AbstractExtension; +use AttributeDateTime; +use AttributeText; use Twig_SimpleFilter; use Twig_SimpleFunction; use utils; @@ -58,6 +60,43 @@ class AppExtension extends AbstractExtension } ); + /** + * Filter to format output + * example a DateTime is converted to user format + * Usage in twig: {{ 'String:ToFormat'|output_format }} + * + * @since 3.0.0 + */ + $filters[] = new Twig_SimpleFilter('date_format', + function ($sDate) { + try + { + if (preg_match('@^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$@', trim($sDate))) + { + return AttributeDateTime::GetFormat()->Format($sDate); + } + } + catch (Exception $e) + { + } + + return $sDate; + } + ); + + /** + * Filter to format output + * example a DateTime is converted to user format + * Usage in twig: {{ 'String:ToFormat'|output_format }} + * + * @since 3.0.0 + */ + $filters[] = new Twig_SimpleFilter('size_format', + function ($sSize) { + return utils::BytesToFriendlyFormat($sSize); + } + ); + // Filter to enable base64 encode/decode // Usage in twig: {{ 'String to encode'|base64_encode }} $filters[] = new Twig_SimpleFilter('base64_encode', 'base64_encode'); @@ -66,8 +105,31 @@ class AppExtension extends AbstractExtension // Filter to enable json decode (encode already exists) // Usage in twig: {{ aSomeArray|json_decode }} $filters[] = new Twig_SimpleFilter('json_decode', function ($sJsonString, $bAssoc = false) { - return json_decode($sJsonString, $bAssoc); - } + return json_decode($sJsonString, $bAssoc); + } + ); + + /** + * Filter to sanitize a text + * Usage in twig: {{ 'variable_name:to-sanitize'|sanitize(constant('utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME')) }} + * + * @uses \utils::Sanitize() + * @since 3.0.0 + */ + $filters[] = new Twig_SimpleFilter('sanitize', function (string $sString, string $sFilter) { + return utils::Sanitize($sString, '', $sFilter); + } + ); + + /** + * Filter to transform the wiki syntax ONLY into HTML. + * + * @uses \AttributeText::RenderWikiHtml() + * @since 3.0.0 + */ + $filters[] = new Twig_SimpleFilter('render_wiki_to_html', function ($sString) { + return AttributeText::RenderWikiHtml($sString, true /* Important, otherwise hyperlinks will be tranformed as well */); + } ); // Filter to add itopversion to an url @@ -100,6 +162,14 @@ class AppExtension extends AbstractExtension return $sUrl; }); + /** + * var_export can be used for example to transform a PHP boolean to 'true' or 'false' strings + * @see https://www.php.net/manual/fr/function.var-export.php + * + * @since 3.0.0 + */ + $filters[] = new Twig_SimpleFilter('var_export', 'var_export'); + return $filters; } @@ -125,6 +195,40 @@ class AppExtension extends AbstractExtension return $oConfig->Get($sParamName); }); + /** + * Function to get a module setting + * Usage in twig: {{ get_module_setting(, [, ]) }} + * + * @uses Config::GetModuleSetting() + * @since 3.0.0 + */ + $functions[] = new Twig_SimpleFunction('get_module_setting', + function (string $sModuleCode, string $sPropertyCode, $defaultValue = null) { + $oConfig = MetaModel::GetConfig(); + + return $oConfig->GetModuleSetting($sModuleCode, $sPropertyCode, $defaultValue); + }); + + /** + * Function to get iTop's app root absolute URL (eg. https://aaa.bbb.ccc/xxx/yyy/) + * Usage in twig: {{ get_absolute_url_app_root() }} + * + * @since 3.0.0 + */ + $functions[] = new Twig_SimpleFunction('get_absolute_url_app_root', function () { + return utils::GetAbsoluteUrlAppRoot(); + }); + + /** + * Function to get iTop's modules root absolute URL (eg. https://aaa.bbb.ccc/xxx/yyy/env-zzz/) + * Usage in twig: {{ get_absolute_url_modules_root() }} + * + * @since 3.0.0 + */ + $functions[] = new Twig_SimpleFunction('get_absolute_url_modules_root', function () { + return utils::GetAbsoluteUrlModulesRoot(); + }); + return $functions; } diff --git a/sources/application/TwigBase/Twig/Extension.php b/sources/application/TwigBase/Twig/Extension.php index 650158fa0..89b157de7 100644 --- a/sources/application/TwigBase/Twig/Extension.php +++ b/sources/application/TwigBase/Twig/Extension.php @@ -65,7 +65,6 @@ class Extension }) ); - // Filter to format output // example a DateTime is converted to user format // Usage in twig: {{ 'String:ToFormat'|output_format }}