Skip to content

Commit 943bafc

Browse files
cconard96cedric-anne
authored andcommitted
migrate main profile form and admin tab to twig
1 parent e919bc4 commit 943bafc

File tree

6 files changed

+216
-102
lines changed

6 files changed

+216
-102
lines changed

.phpstan-baseline.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,12 +8563,6 @@
85638563
'count' => 6,
85648564
'path' => __DIR__ . '/src/Profile.php',
85658565
];
8566-
$ignoreErrors[] = [
8567-
'message' => '#^Function json_encode is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add \'use function Safe\\\\json_encode;\' at the beginning of the file to use the variant provided by the \'thecodingmachine/safe\' library\\.$#',
8568-
'identifier' => 'theCodingMachineSafe.function',
8569-
'count' => 1,
8570-
'path' => __DIR__ . '/src/Profile.php',
8571-
];
85728566
$ignoreErrors[] = [
85738567
'message' => '#^If condition is always false\\.$#',
85748568
'identifier' => 'if.alwaysFalse',

src/Profile.php

Lines changed: 8 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -771,61 +771,12 @@ public function post_getFromDB()
771771
**/
772772
public function showForm($ID, array $options = [])
773773
{
774-
$onfocus = "";
775-
$new = false;
776-
$rowspan = 4;
777-
if ($ID > 0) {
778-
$rowspan++;
779-
$this->check($ID, READ);
780-
} else {
781-
// Create item
782-
$this->check(-1, CREATE);
783-
$onfocus = "onfocus=\"if (this.value==" . htmlescape(json_encode($this->fields["name"])) . ") this.value='';\"";
784-
$new = true;
785-
}
786-
787-
$rand = mt_rand();
788-
789-
$this->showFormHeader($options);
790-
791-
echo "<tr class='tab_bg_1'><td>" . __s('Name') . "</td>";
792-
echo "<td><input type='text' name='name' class='form-control' value=\"" . htmlescape($this->fields["name"]) . "\" $onfocus></td>";
793-
echo "<td rowspan='$rowspan' class='middle right'>" . __s('Comments') . "</td>";
794-
echo "<td class='center middle' rowspan='$rowspan'>";
795-
echo "<textarea class='form-control' rows='4' name='comment' class='form-control'>" . htmlescape($this->fields["comment"]) . "</textarea>";
796-
echo "</td></tr>";
797-
798-
echo "<tr class='tab_bg_1'><td>" . __s('Default profile') . "</td><td>";
799-
Html::showCheckbox(['name' => 'is_default',
800-
'checked' => $this->fields['is_default'],
801-
]);
802-
echo "</td></tr>";
803-
804-
echo "<tr class='tab_bg_1'><td>" . __s("Profile's interface") . "</td>";
805-
echo "<td>";
806-
Dropdown::showFromArray(
807-
'interface',
808-
self::getInterfaces(),
809-
[
810-
'value' => $this->fields["interface"],
811-
'readonly' => $this->isLastSuperAdminProfile() && $this->fields['interface'] == 'central',
812-
]
813-
);
814-
echo "</td></tr>";
815-
816-
echo "<tr class='tab_bg_1'><td>" . __s('Update own password') . "</td><td>";
817-
Html::showCheckbox(['name' => '_password_update',
818-
'checked' => $this->fields['password_update'],
774+
$this->initForm($ID, $options);
775+
TemplateRenderer::getInstance()->display('pages/admin/profile/form.html.twig', [
776+
'item' => $this,
777+
'interfaces' => self::getInterfaces(),
778+
'last_super_admin_profile' => $this->isLastSuperAdminProfile(),
819779
]);
820-
echo "</td></tr>";
821-
822-
echo "<tr class='tab_bg_1'><td>" . __s('Ticket creation form on login') . "</td><td>";
823-
Html::showCheckbox(['name' => 'create_ticket_on_login',
824-
'checked' => $this->fields['create_ticket_on_login'],
825-
]);
826-
echo "</td></tr>";
827-
828-
$this->showFormButtons($options);
829780

830781
return true;
831782
}
@@ -1968,48 +1919,9 @@ public function showFormAdmin($openform = true, $closeform = true)
19681919
return false;
19691920
}
19701921

1971-
echo "<div class='spaced'>";
1972-
1973-
if (
1974-
($canedit = Session::haveRightsOr(self::$rightname, [CREATE, UPDATE, PURGE]))
1975-
&& $openform
1976-
) {
1977-
echo "<form method='post' action='" . $this->getFormURL() . "' data-track-changes='true'>";
1978-
}
1979-
1980-
$matrix_options = [
1981-
'canedit' => $canedit,
1982-
];
1983-
1984-
$matrix_options['title'] = __('Administration');
1985-
$this->displayRightsChoiceMatrix(self::getRightsForForm('central', 'admin', 'general'), $matrix_options);
1986-
1987-
$matrix_options['title'] = __('Inventory');
1988-
$this->displayRightsChoiceMatrix(self::getRightsForForm('central', 'admin', 'inventory'), $matrix_options);
1989-
1990-
$matrix_options['title'] = _n('Rule', 'Rules', Session::getPluralNumber());
1991-
$this->displayRightsChoiceMatrix(self::getRightsForForm('central', 'admin', 'rules'), $matrix_options);
1992-
1993-
$matrix_options['title'] = __('Dropdowns dictionary');
1994-
$this->displayRightsChoiceMatrix(self::getRightsForForm('central', 'admin', 'dictionaries'), $matrix_options);
1995-
1996-
if (
1997-
$canedit
1998-
&& $closeform
1999-
) {
2000-
echo "<div class='center'>";
2001-
echo "<input type='hidden' name='id' value='" . $this->getID() . "'>";
2002-
echo Html::submit(_x('button', 'Save'), [
2003-
'class' => 'btn btn-primary mt-2',
2004-
'icon' => 'ti ti-device-floppy',
2005-
'name' => 'update',
2006-
]);
2007-
echo "</div>";
2008-
Html::closeForm();
2009-
}
2010-
echo "</div>";
2011-
2012-
$this->showLegend();
1922+
TemplateRenderer::getInstance()->display('pages/admin/profile/admin.html.twig', [
1923+
'item' => $this,
1924+
]);
20131925
}
20141926

20151927
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{#
2+
# ---------------------------------------------------------------------
3+
#
4+
# GLPI - Gestionnaire Libre de Parc Informatique
5+
#
6+
# http://glpi-project.org
7+
#
8+
# @copyright 2015-2025 Teclib' and contributors.
9+
# @licence https://www.gnu.org/licenses/gpl-3.0.html
10+
#
11+
# ---------------------------------------------------------------------
12+
#
13+
# LICENSE
14+
#
15+
# This file is part of GLPI.
16+
#
17+
# This program is free software: you can redistribute it and/or modify
18+
# it under the terms of the GNU General Public License as published by
19+
# the Free Software Foundation, either version 3 of the License, or
20+
# (at your option) any later version.
21+
#
22+
# This program is distributed in the hope that it will be useful,
23+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
# GNU General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU General Public License
28+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
#
30+
# ---------------------------------------------------------------------
31+
#}
32+
33+
{% set show_legend = true %}
34+
{% extends 'pages/admin/profile/base_tab.html.twig' %}
35+
36+
{% block content %}
37+
{{ _self.displayRightsMatrix(item, 'central', 'admin', 'general', __('Administration')) }}
38+
{{ _self.displayRightsMatrix(item, 'central', 'admin', 'inventory', __('Inventory')) }}
39+
{{ _self.displayRightsMatrix(item, 'central', 'admin', 'rules', _n('Rule', 'Rules', get_plural_number())) }}
40+
{{ _self.displayRightsMatrix(item, 'central', 'admin', 'dictionaries', __('Dropdowns dictionary')) }}
41+
{% endblock %}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{#
2+
# ---------------------------------------------------------------------
3+
#
4+
# GLPI - Gestionnaire Libre de Parc Informatique
5+
#
6+
# http://glpi-project.org
7+
#
8+
# @copyright 2015-2025 Teclib' and contributors.
9+
# @licence https://www.gnu.org/licenses/gpl-3.0.html
10+
#
11+
# ---------------------------------------------------------------------
12+
#
13+
# LICENSE
14+
#
15+
# This file is part of GLPI.
16+
#
17+
# This program is free software: you can redistribute it and/or modify
18+
# it under the terms of the GNU General Public License as published by
19+
# the Free Software Foundation, either version 3 of the License, or
20+
# (at your option) any later version.
21+
#
22+
# This program is distributed in the hope that it will be useful,
23+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
# GNU General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU General Public License
28+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
#
30+
# ---------------------------------------------------------------------
31+
#}
32+
33+
{% import 'components/form/basic_inputs_macros.html.twig' as inputs %}
34+
35+
{% set show_legend = show_legend|default(false) %}
36+
{% set can_edit = has_profile_right('profile', constant('CREATE')) or has_profile_right('profile', constant('UPDATE')) or has_profile_right('profile', constant('PURGE')) %}
37+
38+
{% macro displayRightsMatrix(item, interface, form, group, title) %}
39+
{% do item.displayRightsChoiceMatrix(
40+
call('Profile::getRightsForForm', [interface, form, group]),
41+
{
42+
canedit: has_profile_right('profile', constant('CREATE')) or has_profile_right('profile', constant('UPDATE')) or has_profile_right('profile', constant('PURGE')),
43+
title: title
44+
}
45+
) %}
46+
{% endmacro %}
47+
48+
<div class="asset">
49+
{% if can_edit %}
50+
<form method="post" action="{{ 'Profile'|itemtype_form_path }}" data-track-changes="true">
51+
<input type="hidden" name="id" value="{{ item.getID() }}">
52+
<input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}">
53+
{% endif %}
54+
<div>
55+
<div class="card-body p-0">
56+
{% block content %}{% endblock %}
57+
{% if show_legend %}
58+
{{ include('pages/admin/profile/legend.html.twig') }}
59+
{% endif %}
60+
</div>
61+
{% if can_edit %}
62+
<div class="card-body mb-n5 border-top d-flex flex-row-reverse">
63+
{{ inputs.submit('update', _x('button', 'Save'), 1, {
64+
class: 'btn btn-primary mt-2',
65+
icon: 'ti ti-device-floppy'
66+
}) }}
67+
</div>
68+
{% endif %}
69+
</div>
70+
{% if can_edit %}
71+
</form>
72+
{% endif %}
73+
</div>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{#
2+
# ---------------------------------------------------------------------
3+
#
4+
# GLPI - Gestionnaire Libre de Parc Informatique
5+
#
6+
# http://glpi-project.org
7+
#
8+
# @copyright 2015-2025 Teclib' and contributors.
9+
# @licence https://www.gnu.org/licenses/gpl-3.0.html
10+
#
11+
# ---------------------------------------------------------------------
12+
#
13+
# LICENSE
14+
#
15+
# This file is part of GLPI.
16+
#
17+
# This program is free software: you can redistribute it and/or modify
18+
# it under the terms of the GNU General Public License as published by
19+
# the Free Software Foundation, either version 3 of the License, or
20+
# (at your option) any later version.
21+
#
22+
# This program is distributed in the hope that it will be useful,
23+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
# GNU General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU General Public License
28+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
#
30+
# ---------------------------------------------------------------------
31+
#}
32+
33+
{% extends "generic_show_form.html.twig" %}
34+
{% import 'components/form/fields_macros.html.twig' as fields %}
35+
36+
{% block form_fields %}
37+
{{ fields.textField('name', item.fields['name'], __('Name')) }}
38+
{{ fields.textareaField('comment', item.fields['comment'], _n('Comment', 'Comments', get_plural_number())) }}
39+
{% block more_fields %}
40+
{{ fields.checkboxField('is_default', item.fields['is_default'], __('Default profile')) }}
41+
{{ fields.nullField() }}
42+
{{ fields.dropdownArrayField('interface', item.fields['interface'], interfaces, __("Profile's interface"), {
43+
readonly: last_super_admin_profile and item.fields['interface'] == 'central'
44+
}) }}
45+
{{ fields.nullField() }}
46+
{{ fields.checkboxField('_password_update', item.fields['password_update'], __('Update own password')) }}
47+
{{ fields.nullField() }}
48+
{{ fields.checkboxField('create_ticket_on_login', item.fields['create_ticket_on_login'], __('Ticket creation form on login')) }}
49+
{% endblock %}
50+
{% endblock %}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{#
2+
# ---------------------------------------------------------------------
3+
#
4+
# GLPI - Gestionnaire Libre de Parc Informatique
5+
#
6+
# http://glpi-project.org
7+
#
8+
# @copyright 2015-2025 Teclib' and contributors.
9+
# @licence https://www.gnu.org/licenses/gpl-3.0.html
10+
#
11+
# ---------------------------------------------------------------------
12+
#
13+
# LICENSE
14+
#
15+
# This file is part of GLPI.
16+
#
17+
# This program is free software: you can redistribute it and/or modify
18+
# it under the terms of the GNU General Public License as published by
19+
# the Free Software Foundation, either version 3 of the License, or
20+
# (at your option) any later version.
21+
#
22+
# This program is distributed in the hope that it will be useful,
23+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
# GNU General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU General Public License
28+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
#
30+
# ---------------------------------------------------------------------
31+
#}
32+
33+
<table class="table table-borderless table-sm w-auto">
34+
<tr>
35+
<td class="text-decoration-underline fw-bold px-2">{{ __('Caption') }}</td>
36+
<td class="table-secondary border-1" style="width: 15px"></td>
37+
<td class="fw-bold px-2">{{ __('Global right') }}</td>
38+
</tr>
39+
<tr>
40+
<td></td>
41+
<td class="border-1" style="width:15px;"></td>
42+
<td class="fw-bold px-2">{{ __('Entity right') }}</td>
43+
</tr>
44+
</table>

0 commit comments

Comments
 (0)