N°4288 - Portal: Update TWIG extensions to match those available in the backoffice

This commit is contained in:
Molkobain
2021-09-08 21:47:05 +02:00
parent 5b6b07af48
commit e4c818cacb
2 changed files with 106 additions and 3 deletions

View File

@@ -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(<MODULE_CODE>, <PROPERTY_CODE> [, <DEFAULT_VALUE>]) }}
*
* @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;
}

View File

@@ -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 }}