Skip to content

Commit

Permalink
Create HtmlExtension and make all Html calls from Twig more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Jan 3, 2025
1 parent 5ed9f50 commit a58189a
Show file tree
Hide file tree
Showing 24 changed files with 172 additions and 95 deletions.
72 changes: 72 additions & 0 deletions src/Glpi/Application/View/Extension/HtmlExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Application\View\Extension;

use Html;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class HtmlExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('html_clean_id', Html::cleanId(...)),
new TwigFunction('html_display_title', Html::displayTitle(...)),
new TwigFunction('html_file', Html::file(...)),
new TwigFunction('html_init_editor_system', Html::initEditorSystem(...)),
new TwigFunction('html_js_ajax_dropdown', Html::jsAjaxDropdown(...)),
new TwigFunction('html_parse_attributes', Html::parseAttributes(...)),
new TwigFunction('html_print_ajax_pager', Html::printAjaxPager(...)),
new TwigFunction('html_progress', Html::progress(...)),

new TwigFunction('html_header', Html::header(...)),
new TwigFunction('html_help_header', Html::helpHeader(...)),
new TwigFunction('html_pop_header', Html::popHeader(...)),
new TwigFunction('html_simple_header', Html::simpleHeader(...)),
new TwigFunction('html_null_header', Html::nullHeader(...)),

new TwigFunction('html_footer', Html::footer(...)),
new TwigFunction('html_pop_footer', Html::popFooter(...)),

new TwigFunction('html_show_checkbox', Html::showCheckbox(...)),
new TwigFunction('html_show_date_time_field', Html::showDateTimeField(...)),
new TwigFunction('html_show_massive_action_checkbox', Html::showMassiveActionCheckBox(...)),
new TwigFunction('html_show_massive_actions', Html::showMassiveActions(...)),
new TwigFunction('html_show_simple_form', Html::showSimpleForm(...)),
new TwigFunction('html_show_tooltip', Html::showToolTip(...)),
];
}
}
2 changes: 2 additions & 0 deletions src/Glpi/Application/View/TemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use Glpi\Application\View\Extension\SecurityExtension;
use Glpi\Application\View\Extension\SessionExtension;
use Glpi\Application\View\Extension\TeamExtension;
use Glpi\Application\View\Extension\HtmlExtension;
use Glpi\Debug\Profiler;
use Plugin;
use Session;
Expand Down Expand Up @@ -118,6 +119,7 @@ public function __construct(string $rootdir = GLPI_ROOT, string $cachedir = GLPI
$this->environment->addExtension(new SearchExtension());
$this->environment->addExtension(new SessionExtension());
$this->environment->addExtension(new TeamExtension());
$this->environment->addExtension(new HtmlExtension());

// add superglobals
$this->environment->addGlobal('_post', $_POST);
Expand Down
30 changes: 15 additions & 15 deletions templates/components/checkbox_matrix.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
<tr>
<th>{{ param['first_cell']|raw }}</th>
{% for col_name, column in columns %}
{% set col_id = call('Html::cleanId', ['col_label_' ~ col_name ~ '_' ~ param['rand']]) %}
{% set col_id = html_clean_id('col_label_' ~ col_name ~ '_' ~ param['rand']) %}
<th id="{{ col_id }}">
{% if column is not iterable %}
{{ column }}
{% else %}
{% if column['short'] is defined and column['long'] is defined %}
{{ column['short'] }}
{% do call('Html::showToolTip', [column['long'], {'applyto': col_id}]) %}
{% do html_show_tooltip(column['long'], {'applyto': col_id}) %}
{% else %}
{{ column['label'] }}
{% endif %}
Expand All @@ -60,7 +60,7 @@
{% endfor %}

{% if param['row_check_all'] %}
{% set col_id = call('Html::cleanId', ['col_of_table_' ~ param['rand']]) %}
{% set col_id = html_clean_id('col_of_table_' ~ param['rand']) %}
<th id="{{ col_id }}">
{{ __('Select/unselect all') }}
</th>
Expand All @@ -73,7 +73,7 @@
{% if row is not iterable %}
<td colspan="{{ number_columns }}">{{ row }}</td>
{% else %}
{% set row_id = call('Html::cleanId', ['row_label_' ~ row_name ~ '_' ~ param['rand']]) %}
{% set row_id = html_clean_id('row_label_' ~ row_name ~ '_' ~ param['rand']) %}
<td class="{{ row['class'] }}" id="{{ row_id }}">
{{ row['label'] ?? '&nbsp;' }}
</td>
Expand All @@ -87,7 +87,7 @@
{% endif %}
{% set content = content|merge({
'name': row_name ~ '[' ~ col_name ~ ']',
'id': call('Html::cleanId', ['cb_' ~ row_name ~ '_' ~ col_name ~ '_' ~ param['rand']])
'id': html_clean_id('cb_' ~ row_name ~ '_' ~ col_name ~ '_' ~ param['rand'])
}) %}
{% set massive_tags = [] %}
{% if param['row_check_all'] %}
Expand All @@ -108,7 +108,7 @@
{% set content = content|merge({
'massive_tags': massive_tags
}) %}
{% do call('Html::showCheckbox', [content]) %}
{% do html_show_checkbox(content) %}

{% else %}
{% if content is not iterable %}
Expand All @@ -120,15 +120,15 @@

{% if param['row_check_all'] %}
<td>
{% do call('Html::showCheckbox', [{
{% do html_show_checkbox({
'title': __('Check/uncheck all'),
'criterion': {
'tag_for_massive': 'row_' ~ row_name ~ '_' ~ param['rand']
},
'massive_tags': 'table_' ~ param['rand'],
'id': call('Html::cleanId', ['cb_checkall_row_' ~ row_name ~ '_' ~ param['rand']]),
'id': html_clean_id('cb_checkall_row_' ~ row_name ~ '_' ~ param['rand']),
'checked': (nb_cb_per_row[row_name]['checked'] >= (nb_cb_per_row[row_name]['total'])),
}]) %}
}) %}
</td>
{% endif %}
{% endif %}
Expand All @@ -141,29 +141,29 @@
{% for col_name, column in columns %}
<td>
{% if nb_cb_per_col[col_name]['total'] >= 2 %}
{% do call('Html::showCheckbox', [{
{% do html_show_checkbox({
'title': __('Check/uncheck all'),
'criterion': {
'tag_for_massive': 'col_' ~ col_name ~ '_' ~ param['rand']
},
'massive_tags': 'table_' ~ param['rand'],
'id': call('Html::cleanId', ['cb_checkall_col_' ~ col_name ~ param['rand']]),
'id': html_clean_id('cb_checkall_col_' ~ col_name ~ param['rand']),
'checked': (nb_cb_per_col[col_name]['checked'] >= (nb_cb_per_col[col_name]['total'])),
}]) %}
}) %}
{% endif %}
</td>
{% endfor %}

{% if param['row_check_all'] %}
<td>
{% do call('Html::showCheckbox', [{
{% do html_show_checkbox({
'title': __('Check/uncheck all'),
'criterion': {
'tag_for_massive': 'table_' ~ param['rand']
},
'massive_tags': '',
'id': call('Html::cleanId', ['cb_checkall_table_' ~ param['rand']]),
}]) %}
'id': html_clean_id('cb_checkall_table_' ~ param['rand']),
}) %}
</td>
{% endif %}
</tr>
Expand Down
16 changes: 8 additions & 8 deletions templates/components/datatable.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<div class="table-responsive" {% if showmassiveactions %} id="{{ massiveactionparams['container'] }}" {% endif %}>
{% if showmassiveactions %}
<div class="mb-2">
{% do call('Html::showMassiveActions', [massiveactionparams|default([])]) %}
{% do html_show_massive_actions(massiveactionparams|default([])) %}
</div>
{% endif %}
<table id="{{ datatable_id }}" class="table {{ table_class_style|default('table-hover') }}">
Expand Down Expand Up @@ -159,21 +159,21 @@
{% endfor %}
</select>
{% elseif formatter == "datetime" %}
{{ call("Html::showDateTimeField", [
{{ html_show_date_time_field(
"filters[" ~ colkey ~ "]",
{
'value': filters[colkey],
'display': false
}
])|raw }}
)|raw }}
{% elseif formatter == "date" %}
{{ call("Html::showDateField", [
{{ html_show_date_time_field(
"filters[" ~ colkey ~ "]",
{
'value': filters[colkey],
'display': false
}
])|raw }}
)|raw }}
{% elseif formatter starts with "progress" %}
<input type="range" class="form-range"
name="filters[{{ colkey }}]"
Expand Down Expand Up @@ -237,11 +237,11 @@
{{ entry[colkey] }}
</span>
{% elseif formatter starts with "progress" %}
{{ call("Html::progress", [100, entry[colkey]])|raw }}
{{ html_progress(100, entry[colkey])|raw }}
{% elseif formatter == "date" %}
{{ call("Html::convDate", [entry[colkey]])|raw }}
{{ entry[colkey]|formatted_date|raw }}
{% elseif formatter == "datetime" %}
{{ call("Html::convDateTime", [entry[colkey]])|raw }}
{{ entry[colkey]|formatted_datetime|raw }}
{% elseif formatter == "duration" %}
{{ entry[colkey]|formatted_duration }}
{% elseif formatter == "bytesize" %}
Expand Down
12 changes: 4 additions & 8 deletions templates/components/form/basic_inputs_macros.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@

{% if options.step != 'any' and options.step|round(0, 'floor') != options.step %}
{# Only format number if not a whole number #}
{% set value = call('Html::formatNumber', [value, true]) %}
{% set value = value|formatted_number(true) %}
{% endif %}

{{ _self.input(name, value, options|merge({'type': 'number'})) }}
Expand Down Expand Up @@ -194,11 +194,7 @@
{% if options.simple %}
{{ _self.input(name, value, options|merge({'type': 'file'})) }}
{% else %}
{% do call('Html::file', [
options|merge({
'name': name,
})
]) %}
{% do html_file(options|merge({'name': name})) %}
{% endif %}
{% endmacro %}

Expand Down Expand Up @@ -338,7 +334,7 @@
{{ options.required ? 'required' : '' }}>{{ options.enable_richtext ? value|safe_html|escape : value }}</textarea>

{% if options.enable_richtext %}
{% do call('Html::initEditorSystem', [
{% do html_init_editor_system(
options.id,
options.rand,
true,
Expand All @@ -352,7 +348,7 @@
options.toolbar,
options.statusbar,
options.content_style,
]) %}
) %}
{% endif %}

{% if options.enable_form_tags %}
Expand Down
2 changes: 1 addition & 1 deletion templates/components/form/buttons.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
{% if canedit and params['addbuttons']|length > 0 %}
{% for key, val in params['addbuttons'] %}
{% if val.add_attribs is not empty %}
{% set extra_attribs = call('Html::parseAttributes', {options: val.add_attribs}) %}
{% set extra_attribs = html_parse_attributes(val.add_attribs) %}
{% else %}
{% set extra_attribs = '' %}
{% endif %}
Expand Down
20 changes: 10 additions & 10 deletions templates/components/form/fields_macros.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,22 @@
{% set add_html = '' %}
{% if not options.readonly and options.enable_fileupload %}
{% set add_html %}
{% do call('Html::file', [{
{% do html_file({
'editor_id': options.id,
'multiple': true,
'uploads': options.uploads,
'required': options.fields_template.isMandatoryField('_documents_id')
}]) %}
}) %}
{% endset %}
{% elseif not options.readonly and not options.enable_fileupload and options.enable_richtext and options.enable_images %}
{% set add_html %}
{% do call('Html::file', [{
{% do html_file({
'editor_id': options.id,
'name': name,
'only_uploaded_files': true,
'uploads': options.uploads,
'required': options.fields_template.isMandatoryField('_documents_id')
}]) %}
}) %}
{% endset %}
{% endif %}

Expand Down Expand Up @@ -348,9 +348,9 @@
{% set url = options['url'] ?? null %}
{% set options = options|filter((v, k) => k != 'url' and k != 'clearable') %}
{% if url is not empty %}
<a href="{{ url }}" {{ call('Html::parseAttributes', [link_options])|raw }}>
<a href="{{ url }}" {{ html_parse_attributes(link_options)|raw }}>
{% endif %}
<img src="{{ value }}" {{ call('Html::parseAttributes', [options])|raw }} />
<img src="{{ value }}" {{ html_parse_attributes(options)|raw }} />
{% if url is not empty %}
</a>
{% endif %}
Expand Down Expand Up @@ -732,7 +732,7 @@
}) %}
{% set field %}
{% set ajax_opts = options|filter((v, k) => k in ['templateResult', 'templateSelection', 'rand']) %}
{{ call('Html::jsAjaxDropdown', [name, options.id, url, ajax_opts])|raw }}
{{ html_js_ajax_dropdown(name, options.id, url, ajax_opts)|raw }}
{% endset %}

{% if field|trim is not empty %}
Expand Down Expand Up @@ -834,7 +834,7 @@
{% set class = class ~ ' ' ~ options.add_field_class %}

{% if options.add_field_attribs is not empty %}
{% set extra_attribs = call('Html::parseAttributes', {options: options.add_field_attribs}) %}
{% set extra_attribs = html_parse_attributes(options.add_field_attribs) %}
{% else %}
{% set extra_attribs = '' %}
{% endif %}
Expand Down Expand Up @@ -886,7 +886,7 @@
{% endif %}

{% if options.add_field_attribs is not empty %}
{% set extra_attribs = call('Html::parseAttributes', {options: options.add_field_attribs}) %}
{% set extra_attribs = html_parse_attributes(options.add_field_attribs) %}
{% else %}
{% set extra_attribs = '' %}
{% endif %}
Expand Down Expand Up @@ -927,7 +927,7 @@
{% endif %}

{% if options.add_field_attribs is not empty %}
{% set extra_attribs = call('Html::parseAttributes', {options: options.add_field_attribs}) %}
{% set extra_attribs = html_parse_attributes(options.add_field_attribs) %}
{% else %}
{% set extra_attribs = '' %}
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

{% if linknewitil %}
<div class='mb-3'>
{{ call('Html::showSimpleForm', [itemtype_1|itemtype_form_path, '_add_fromitem', __('New %s for this item')|format(itemtype_1|itemtype_name(1)), {'itemtype': item.getType(), 'items_id': item.getID()}]) }}
{{ html_show_simple_form(itemtype_1|itemtype_form_path, '_add_fromitem', __('New %s for this item')|format(itemtype_1|itemtype_name(1)), {'itemtype': item.getType(), 'items_id': item.getID()}) }}
</div>
{% endif %}
<div class="mb-3">
Expand Down
4 changes: 2 additions & 2 deletions templates/components/form/rulematchedlogs.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#}

{% block more_fields %}
{% do call('Html::printAjaxPager', ['RuleMatchedLog'|itemtype_name, start, count]) %}
{% do html_print_ajax_pager('RuleMatchedLog'|itemtype_name, start, count) %}

<table class="tab_cadre_fixe">
<tr>
Expand Down Expand Up @@ -96,5 +96,5 @@
{% endfor %}
</table>

{% do call('Html::printAjaxPager', ['RuleMatchedLog'|itemtype_name, start, count]) %}
{% do html_print_ajax_pager('RuleMatchedLog'|itemtype_name, start, count) %}
{% endblock %}
Loading

0 comments on commit a58189a

Please sign in to comment.