Skip to content

Commit b4427ca

Browse files
committed
Apply suggestions from code review
1 parent 3f7e61b commit b4427ca

File tree

10 files changed

+151
-83
lines changed

10 files changed

+151
-83
lines changed

js/modules/Forms/RendererController.js

+6-19
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ export class GlpiFormRendererController
124124
});
125125

126126
// Handle delegation form update
127-
this.#initDelegationEventHandlers();
127+
$(this.#target).on(
128+
'change',
129+
'[data-glpi-form-renderer-delegation-container] select[name="delegation_users_id"]',
130+
(e) => this.#renderDelegation(e)
131+
);
128132
}
129133

130134
/**
@@ -452,15 +456,6 @@ export class GlpiFormRendererController
452456
;
453457
}
454458

455-
#initDelegationEventHandlers()
456-
{
457-
$(this.#target)
458-
.find('[data-glpi-form-renderer-delegation-container]')
459-
.find('select[name="delegation_users_id"]')
460-
.on('change', (e) => this.#renderDelegation(e))
461-
;
462-
}
463-
464459
async #renderDelegation()
465460
{
466461
const selected_user_id = $(this.#target)
@@ -472,17 +467,9 @@ export class GlpiFormRendererController
472467
'selected_user_id': selected_user_id,
473468
});
474469

475-
// Create a temporary element to parse the AJAX response
476-
const $temp = $('<div>').html(response);
477-
// Extract the inner content of the delegation container from the response
478-
const innerContent = $temp.find('[data-glpi-form-renderer-delegation-container]').html();
479-
480470
// Replace only the inner content of the delegation container
481471
$(this.#target)
482472
.find('[data-glpi-form-renderer-delegation-container]')
483-
.html(innerContent);
484-
485-
// Re-init event handlers
486-
this.#initDelegationEventHandlers();
473+
.html(response);
487474
}
488475
}

src/Glpi/Controller/Form/DelegationController.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* http://glpi-project.org
99
*
1010
* @copyright 2015-2025 Teclib' and contributors.
11-
* @copyright 2003-2014 by the INDEPNET Development Team.
1211
* @licence https://www.gnu.org/licenses/gpl-3.0.html
1312
*
1413
* ---------------------------------------------------------------------

src/Glpi/Controller/Form/SubmitAnswerController.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Glpi\Exception\Http\NotFoundHttpException;
4242
use Glpi\Form\AnswersHandler\AnswersHandler;
4343
use Glpi\Form\AnswersSet;
44+
use Glpi\Form\DelegationData;
4445
use Glpi\Form\EndUserInputNameProvider;
4546
use Glpi\Form\Form;
4647
use Glpi\Http\Firewall;
@@ -96,11 +97,11 @@ private function saveSubmittedAnswers(
9697
$post = $request->request->all();
9798
$provider = new EndUserInputNameProvider();
9899

99-
$delegation = [
100-
'users_id' => $post['delegation_users_id'] ?? null,
101-
'use_notification' => $post['delegation_use_notification'] ?? null,
102-
'alternative_email' => $post['delegation_alternative_email'] ?? null,
103-
];
100+
$delegation = new DelegationData(
101+
$request->request->getInt('delegation_users_id', 0) ?: null,
102+
$request->request->getBoolean('delegation_use_notification', false) ?: null,
103+
$request->request->getString('delegation_alternative_email', '') ?: null
104+
);
104105
$answers = $provider->getAnswers($post);
105106
$files = $provider->getFiles($post, $answers);
106107
if (empty($answers)) {
@@ -112,8 +113,8 @@ private function saveSubmittedAnswers(
112113
$form,
113114
$answers,
114115
Session::getLoginUserID(),
115-
$files,
116-
$delegation
116+
$delegation,
117+
$files
117118
);
118119

119120
return $answers;

src/Glpi/Form/AnswersHandler/AnswersHandler.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Glpi\Form\AnswersSet;
4141
use Glpi\Form\Condition\Engine;
4242
use Glpi\Form\Condition\EngineInput;
43+
use Glpi\Form\DelegationData;
4344
use Glpi\Form\Destination\AnswersSet_FormDestinationItem;
4445
use Glpi\Form\Destination\FormDestination;
4546
use Glpi\Form\Form;
@@ -92,21 +93,21 @@ public function saveAnswers(
9293
Form $form,
9394
array $answers,
9495
int $users_id,
96+
DelegationData $delegation,
9597
array $files = [],
96-
array $delegation = []
9798
): AnswersSet {
9899
/** @var \DBmysql $DB */
99100
global $DB;
100101

101102
if ($DB->inTransaction()) {
102-
return $this->doSaveAnswers($form, $answers, $users_id, $files, $delegation);
103+
return $this->doSaveAnswers($form, $answers, $users_id, $delegation, $files);
103104
} else {
104105
// We do not want to commit the answers unless everything was processed
105106
// correctly
106107
$DB->beginTransaction();
107108

108109
try {
109-
$answers_set = $this->doSaveAnswers($form, $answers, $users_id, $files, $delegation);
110+
$answers_set = $this->doSaveAnswers($form, $answers, $users_id, $delegation, $files);
110111
$DB->commit();
111112
return $answers_set;
112113
} catch (\Throwable $e) {
@@ -141,8 +142,8 @@ protected function doSaveAnswers(
141142
Form $form,
142143
array $answers,
143144
int $users_id,
145+
DelegationData $delegation,
144146
array $files = [],
145-
array $delegation = []
146147
): AnswersSet {
147148
// Save answers
148149
$answers_set = $this->createAnswserSet(

src/Glpi/Form/AnswersSet.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ final class AnswersSet extends CommonDBChild
5252

5353
public array $files = [];
5454

55-
/** @var array<string, ?string> $delegation */
56-
public array $delegation = [];
55+
public DelegationData $delegation;
5756

5857
#[Override]
5958
public static function getTypeName($nb = 0)
@@ -271,19 +270,19 @@ public function setSubmittedFiles(array $files): void
271270
/**
272271
* Get delegation data
273272
*
274-
* @return array<string, ?string>
273+
* @return DelegationData
275274
*/
276-
public function getDelegation(): array
275+
public function getDelegation(): DelegationData
277276
{
278277
return $this->delegation;
279278
}
280279

281280
/**
282281
* Set delegation data
283282
*
284-
* @param array<string, ?string> $delegation
283+
* @param DelegationData $delegation
285284
*/
286-
public function setDelegation(array $delegation): void
285+
public function setDelegation(DelegationData $delegation): void
287286
{
288287
$this->delegation = $delegation;
289288
}

src/Glpi/Form/DelegationData.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2025 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
*
13+
* ---------------------------------------------------------------------
14+
*
15+
* LICENSE
16+
*
17+
* This file is part of GLPI.
18+
*
19+
* This program is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
*
32+
* ---------------------------------------------------------------------
33+
*/
34+
35+
namespace Glpi\Form;
36+
37+
final class DelegationData
38+
{
39+
public function __construct(
40+
public readonly ?int $users_id,
41+
public readonly ?bool $use_notification,
42+
public readonly ?string $alternative_email
43+
) {
44+
}
45+
}

src/Glpi/Form/Destination/CommonITILField/ITILActorFieldStrategy.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ private function getActorsFromCurrentUser(AnswersSet $answers_set): ?array
123123
}
124124

125125
$delegation = $answers_set->getDelegation();
126-
if (Ticket::canDelegateeCreateTicket($delegation['users_id'] ?? $user_id)) {
126+
if (Ticket::canDelegateeCreateTicket($delegation->users_id ?? $user_id)) {
127127
return [
128128
[
129129
'itemtype' => User::class,
130-
'items_id' => $delegation['users_id'] ?? $user_id,
131-
'use_notification' => $delegation['use_notification'] ?? 0,
132-
'alternative_email' => $delegation['alternative_email'] ?? '',
130+
'items_id' => $delegation->users_id ?? $user_id,
131+
'use_notification' => $delegation->use_notification ?? 0,
132+
'alternative_email' => $delegation->alternative_email ?? '',
133133
]
134134
];
135135
} else {
@@ -144,7 +144,7 @@ private function getActorsFromCurrentUser(AnswersSet $answers_set): ?array
144144

145145
private function getActorsFromSupervisorOfCurrentUser(AnswersSet $answers_set): ?array
146146
{
147-
$users_id = $answers_set->getDelegation()['users_id'] ?? Session::getLoginUserID();
147+
$users_id = $answers_set->getDelegation()->users_id ?? Session::getLoginUserID();
148148
if (!is_numeric($users_id)) {
149149
return null;
150150
}

src/User.php

+30
Original file line numberDiff line numberDiff line change
@@ -3697,6 +3697,36 @@ public static function getDelegateGroupsForUser($entities_id = '')
36973697
return $groups;
36983698
}
36993699

3700+
/**
3701+
* Get all users from groups where the current user have delegating, plus the current user.
3702+
*
3703+
* @param integer|string $entities_id ID of the entity to restrict
3704+
*
3705+
* @return array<int, string> Array of user IDs mapped to their friendly names, sorted alphabetically, with "Myself" first.
3706+
*/
3707+
public static function getUsersFromDelegatedGroups($entities_id = ''): array
3708+
{
3709+
$groups_ids = self::getDelegateGroupsForUser($entities_id);
3710+
$users_data = [];
3711+
foreach ($groups_ids as $groups_id) {
3712+
$users_data = array_merge($users_data, Group_User::getGroupUsers($groups_id));
3713+
}
3714+
3715+
// Get unique user IDs from the collected data
3716+
$user_ids = array_unique(array_column($users_data, 'id'));
3717+
3718+
$formatted_users = [];
3719+
foreach ($user_ids as $user_id) {
3720+
// Avoid adding the current user if they are in the delegated groups, will be added later
3721+
if ($user_id !== Session::getLoginUserID()) {
3722+
$formatted_users[$user_id] = User::getFriendlyNameById($user_id);
3723+
}
3724+
}
3725+
3726+
uasort($formatted_users, 'strcasecmp');
3727+
3728+
return [Session::getLoginUserID() => __('Myself')] + $formatted_users;
3729+
}
37003730

37013731
/**
37023732
* Execute the query to select box with all glpi users where select key = name

templates/components/helpdesk_forms/delegation_alert.html.twig

+44-40
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,26 @@
3838
{% set selected_user = get_current_user() %}
3939
{% endif %}
4040

41-
{% set users = get_current_user().getDelegateGroupsForUser()|map(
42-
groups_id => call('Group_User::getGroupUsers', [groups_id])
43-
)|reduce(
44-
(carry, group_users) => carry|merge(group_users), []
45-
) %}
41+
{% set users = get_current_user().getUsersFromDelegatedGroups() %}
42+
{% set user_dropdown_html %}
43+
{{ fields.dropdownArrayField(
44+
'delegation_users_id',
45+
selected_user.getID(),
46+
users,
47+
'',
48+
{
49+
'no_label' : true,
50+
'width' : 'auto',
51+
'field_class' : '',
52+
'mb' : '',
53+
'aria_label' : __('Select the user to delegate'),
54+
'wrapper_class' : 'd-inline-block',
55+
}
56+
) }}
57+
{% endset %}
4658

47-
<div
48-
class="alert d-flex align-items-center justify-content-start"
49-
data-glpi-form-renderer-delegation-container
50-
>
51-
{% if users is not empty %}
52-
<span class="text-nowrap text-muted mx-2">This ticket is for</span>
53-
{{ fields.dropdownArrayField(
54-
'delegation_users_id',
55-
selected_user.getID(),
56-
{
57-
(get_current_user().getID()): __('Myself'),
58-
} + users|reduce((carry, user) => carry + {(user.id): call('User::getFriendlyNameById', [user.id])}, {}),
59-
'',
60-
{
61-
'no_label' : true,
62-
'width' : 'auto',
63-
'field_class': '',
64-
'mb' : '',
65-
'aria_label' : __('Select the user to delegate'),
66-
}
67-
) }}
68-
<span class="text-nowrap text-muted mx-2">and</span>
69-
{% endif %}
70-
<span class="me-2 input-group d-inline-flex w-auto">
59+
{% set notification_part_html %}
60+
<span class="input-group d-inline-flex w-auto">
7161
{% if selected_user.getID() == get_current_user().getID() %}
7262
{{ fields.dropdownArrayField(
7363
'delegation_use_notification',
@@ -78,11 +68,12 @@
7868
},
7969
'',
8070
{
81-
'no_label' : true,
82-
'width' : 'auto',
83-
'field_class': '',
84-
'mb' : '',
85-
'aria_label' : __('Do you want to be notified of future events of this ticket'),
71+
'no_label' : true,
72+
'width' : 'auto',
73+
'field_class' : '',
74+
'mb' : '',
75+
'aria_label' : __('Do you want to be notified of future events of this ticket'),
76+
'wrapper_class' : 'd-inline-block',
8677
}
8778
) }}
8879
{% else %}
@@ -95,11 +86,12 @@
9586
},
9687
'',
9788
{
98-
'no_label' : true,
99-
'width' : 'auto',
100-
'field_class': '',
101-
'mb' : '',
102-
'aria_label' : __('Do you want to be notified of future events of this ticket'),
89+
'no_label' : true,
90+
'width' : 'auto',
91+
'field_class' : '',
92+
'mb' : '',
93+
'aria_label' : __('Do you want to be notified of future events of this ticket'),
94+
'wrapper_class' : 'd-inline-block',
10395
}
10496
) }}
10597
{% endif %}
@@ -126,6 +118,18 @@
126118
) }}
127119
</div>
128120
</span>
129-
<span class="text-nowrap text-muted ms-1">to be notified of future events of this ticket</span><span></span>
121+
{% endset %}
122+
123+
<div class="alert d-flex align-items-center justify-content-start flex-wrap">
124+
{% if users|length > 1 %}
125+
{{ __('This ticket is for %1$s and %2$s to be notified of future events of this ticket.')|format(
126+
'<span class="mx-2">' ~ user_dropdown_html|raw ~ '</span>',
127+
'<span class="mx-2">' ~ notification_part_html|raw ~ '</span>'
128+
)|raw }}
129+
{% else %}
130+
{{ __('%1$s to be notified of future events of this ticket.')|format(
131+
'<span class="mx-2">' ~ notification_part_html|raw ~ '</span>'
132+
)|raw }}
133+
{% endif %}
130134
</div>
131135
{% endif %}

0 commit comments

Comments
 (0)