From 736426d6473215f010306300ca3f803074960d6c Mon Sep 17 00:00:00 2001 From: Giovanny Rodriguez Date: Mon, 11 May 2026 10:39:39 -0500 Subject: [PATCH] Fix category loss and undefined key warning on ticket transfer - When keep_category is enabled and the category exists in the target entity, explicitly include itilcategories_id in the update payload so GLPI core does not reset it when the entity changes. - Fix undefined variable $ticket_category in the same code path, which caused the mandatory-category check to incorrectly block the transfer. - Use !empty() on group_choice to avoid PHP warning when the key is absent from POST (group not required / not selected). Co-Authored-By: Claude Sonnet 4.6 --- src/Ticket.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Ticket.php b/src/Ticket.php index 4dee33c..a5f7804 100644 --- a/src/Ticket.php +++ b/src/Ticket.php @@ -550,7 +550,7 @@ public function launchTicketTransfer($params) 'entities_id' => $params['entity_choice'], ]; - if ($params['group_choice'] && $params['group_choice'] > 0) { + if (!empty($params['group_choice']) && $params['group_choice'] > 0) { $ticket_status = ['status' => CommonITILObject::ASSIGNED]; $ticket_update = array_merge($ticket_update, $ticket_status); } else { @@ -558,20 +558,19 @@ public function launchTicketTransfer($params) $ticket_update = array_merge($ticket_update, $ticket_status); } - // In case keep_category is at yes and category doesn't exist, reset category's ticket - if ($checkEntityRight['keep_category'] && !$checkExistingCategory) { - $ticket_category = ['itilcategories_id' => 0]; - $ticket_update = array_merge($ticket_update, $ticket_category); - } - - if (!$checkEntityRight['keep_category']) { - if ($checkEntityRight['itilcategories_id'] == null) { - $ticket_category = ['itilcategories_id' => 0]; + if ($checkEntityRight['keep_category']) { + if ($checkExistingCategory) { + // Explicitly include the current category so GLPI does not reset it on entity change + $currentTicket = new \Ticket(); + $currentTicket->getFromDB($params['id_ticket']); + $ticket_category = ['itilcategories_id' => $currentTicket->fields['itilcategories_id']]; } else { - $ticket_category = ['itilcategories_id' => $checkEntityRight['itilcategories_id']]; + $ticket_category = ['itilcategories_id' => 0]; } - $ticket_update = array_merge($ticket_update, $ticket_category); + } else { + $ticket_category = ['itilcategories_id' => $checkEntityRight['itilcategories_id'] ?? 0]; } + $ticket_update = array_merge($ticket_update, $ticket_category); // If category is mandatory with GLPIs template and category will be null if ($ticket_category['itilcategories_id'] == 0