diff --git a/src/Ticket.php b/src/Ticket.php index dae70ea..4dbff8e 100644 --- a/src/Ticket.php +++ b/src/Ticket.php @@ -321,8 +321,7 @@ public static function checkExistingCategory($params) global $DB; $id_ticket = $params['id_ticket']; - $targetEntity = $params['entity_choice']; - + $targetEntity = (int) $params['entity_choice']; $result = $DB->request([ 'SELECT' => 'itilcategories_id', @@ -330,10 +329,13 @@ public static function checkExistingCategory($params) 'WHERE' => ['id' => $id_ticket] ]); - $getTicketCategory = ''; - + $ticketCategoryId = 0; foreach ($result as $data) { - $getTicketCategory = $data['itilcategories_id']; + $ticketCategoryId = (int) $data['itilcategories_id']; + } + + if ($ticketCategoryId === 0) { + return false; } $result = $DB->request([ @@ -341,43 +343,38 @@ public static function checkExistingCategory($params) 'WHERE' => ['id' => $targetEntity] ]); - $ancestorsEntities = []; - + $visibleEntities = []; foreach ($result as $data) { - if ($data['ancestors_cache']) { - $ancestorsEntities = $data['ancestors_cache']; - $ancestorsEntities = json_decode($ancestorsEntities, true); - array_push($ancestorsEntities, $targetEntity); - } else { - array_push($ancestorsEntities, 0); + if (!empty($data['ancestors_cache'])) { + $decoded = json_decode($data['ancestors_cache'], true); + if (is_array($decoded)) { + $visibleEntities = array_map('intval', array_values($decoded)); + } } } + $visibleEntities[] = $targetEntity; $result = $DB->request([ 'FROM' => 'glpi_itilcategories', - 'WHERE' => ['id' => $getTicketCategory] + 'WHERE' => ['id' => $ticketCategoryId] ]); - - $getEntitiesFromCategoryTicket = ''; - $isRecursiveCategory = ''; - + $categoryEntity = null; + $isRecursive = false; foreach ($result as $data) { - $getEntitiesFromCategoryTicket = $data['entities_id']; - $isRecursiveCategory = $data['is_recursive']; + $categoryEntity = (int) $data['entities_id']; + $isRecursive = (bool) $data['is_recursive']; } - if (!$isRecursiveCategory) { - if ($getEntitiesFromCategoryTicket == $targetEntity) { - $isRecursiveCategory = true; - } + if ($categoryEntity === null) { + return false; } - if (in_array($getEntitiesFromCategoryTicket, $ancestorsEntities) && $isRecursiveCategory) { + if ($categoryEntity === $targetEntity) { return true; - } else { - return false; } + + return $isRecursive && in_array($categoryEntity, $visibleEntities, true); }