Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 85 additions & 47 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
{
public $dohistory = true;
public static $rightname = 'config';
public const CONFIG_PARENT = \Entity::CONFIG_PARENT;
public static function getMenuName(): string
{
return __('More options', 'moreoptions');
Expand Down Expand Up @@ -105,10 +106,8 @@
}

foreach (self::getItilConfigFields() as $field) {
if (!isset($item->input[$field])) {
$item->input[$field] = 0;
} elseif ($item->input[$field] == 'on') {
$item->input[$field] = 1;
if (isset($item->input[$field])) {
$item->input[$field] = (int) $item->input[$field];
}
}

Expand All @@ -121,7 +120,6 @@
public static function getItilConfigFields(): array
{
return [
'use_parent_entity',
'take_item_group_ticket',
'take_item_group_change',
'take_item_group_problem',
Expand Down Expand Up @@ -153,6 +151,12 @@
'mandatory_task_duration',
'mandatory_task_user',
'mandatory_task_group',
'take_requester_group_ticket',
'take_requester_group_change',
'take_requester_group_problem',
'take_technician_group_ticket',
'take_technician_group_change',
'take_technician_group_problem',
];
}

Expand All @@ -175,24 +179,31 @@
'entities_id' => $item->getID(),
]);

// Get effective configuration to show which entity's config is actually used
$effectiveConfig = self::getConfig($item->getID(), true);
$parentEntityInfo = null;

if (($moconfig->fields['use_parent_entity'] ?? false) && ($effectiveConfig->fields['entities_id'] != $item->getID())) {
$parentEntity = new Entity();
if ($parentEntity->getFromDB($effectiveConfig->fields['entities_id'])) {
$parentEntityInfo = $parentEntity->getName();
$inheritance_labels = [];
if ($item->getID() > 0) {
$parentConfig = self::getConfig($item->fields['entities_id'], true);
foreach (self::getItilConfigFields() as $field) {
$inheritance_labels[$field] = self::getInheritedValueBadge($parentConfig->fields[$field] ?? 0);
}
foreach ([
'take_requester_group_ticket',
'take_requester_group_change',
'take_requester_group_problem',
'take_technician_group_ticket',
'take_technician_group_change',
'take_technician_group_problem',
] as $field) {
$inheritance_labels[$field] = self::getInheritedValueBadgeForActorGroup($parentConfig->fields[$field] ?? 0);
}
}

TemplateRenderer::getInstance()->display(
'@moreoptions/config.html.twig',
[
'item' => $moconfig,
'dropdown_options' => self::getSelectableActorGroup(),
'parent_entity_info' => $parentEntityInfo,
'params' => [
'item' => $moconfig,
'dropdown_options' => self::getSelectableActorGroup(),
'inheritance_labels' => $inheritance_labels,
'params' => [
'canedit' => true,
],
],
Expand All @@ -204,13 +215,33 @@
return "ti ti-send";
}

private static function getInheritedValueBadge(mixed $value): string
{
$text = match ((int) $value) {
1 => __('Yes'),
default => __('No'),
};
return Entity::inheritedValue(htmlescape($text), false, false);
}

private static function getInheritedValueBadgeForActorGroup(mixed $value): string
{
$options = self::getSelectableActorGroup();
$text = $options[(int) $value] ?? __('No');
return Entity::inheritedValue(htmlescape($text), false, false);
}

public static function addConfig(CommonDBTM $item): void
{
$moconfig = new self();
$moconfig->add([
'is_active' => 0,
'entities_id' => $item->getID(),
]);
$entity_id = $item->getID();
$data = ['entities_id' => $entity_id];
if ($entity_id > 0) {
foreach (self::getItilConfigFields() as $field) {
$data[$field] = self::CONFIG_PARENT;
}
}
$moconfig->add($data);
}

/**
Expand All @@ -222,7 +253,6 @@
*/
public static function getConfig(?int $entityId = null, bool $useInheritance = true): self
{
// Use current entity if not specified
if ($entityId === null) {
$entityId = Session::getActiveEntity();
}
Expand All @@ -232,12 +262,15 @@
'entities_id' => $entityId,
]);

// If inheritance is enabled, use_parent_entity is set, and we're not at root entity
if ($useInheritance && ($moconfig->fields['use_parent_entity'] ?? false) && $entityId > 0) {
if ($useInheritance && $entityId > 0) {
$entity = new Entity();
if ($entity->getFromDB($entityId)) {
$parentId = $entity->fields['entities_id'];
return self::getConfig($parentId, true);
$parentConfig = self::getConfig($entity->fields['entities_id'], true);
foreach (self::getItilConfigFields() as $field) {
if (($moconfig->fields[$field] ?? 0) == self::CONFIG_PARENT) {
$moconfig->fields[$field] = $parentConfig->fields[$field] ?? 0;
}
}
}
}

Expand All @@ -253,18 +286,16 @@
$migration->displayMessage("Installing $table");
$query = "CREATE TABLE IF NOT EXISTS `$table` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`is_active` tinyint NOT NULL DEFAULT '1',
`entities_id` int unsigned NOT NULL DEFAULT '0',
`use_parent_entity` tinyint NOT NULL DEFAULT '0',
`take_item_group_ticket` tinyint NOT NULL DEFAULT '-2',
`take_item_group_change` tinyint NOT NULL DEFAULT '0',
`take_item_group_problem` tinyint NOT NULL DEFAULT '0',
`take_requester_group_ticket` int unsigned NOT NULL DEFAULT '0',
`take_requester_group_change` int unsigned NOT NULL DEFAULT '0',
`take_requester_group_problem` int unsigned NOT NULL DEFAULT '0',
`take_technician_group_ticket` int unsigned NOT NULL DEFAULT '0',
`take_technician_group_change` int unsigned NOT NULL DEFAULT '0',
`take_technician_group_problem` int unsigned NOT NULL DEFAULT '0',
`take_requester_group_ticket` tinyint NOT NULL DEFAULT '0',
`take_requester_group_change` tinyint NOT NULL DEFAULT '0',
`take_requester_group_problem` tinyint NOT NULL DEFAULT '0',
`take_technician_group_ticket` tinyint NOT NULL DEFAULT '0',
`take_technician_group_change` tinyint NOT NULL DEFAULT '0',
`take_technician_group_problem` tinyint NOT NULL DEFAULT '0',
`prevent_closure_ticket` tinyint NOT NULL DEFAULT '0',
`prevent_closure_change` tinyint NOT NULL DEFAULT '0',
`prevent_closure_problem` tinyint NOT NULL DEFAULT '0',
Expand Down Expand Up @@ -294,29 +325,36 @@
`mandatory_task_user` tinyint NOT NULL DEFAULT '0',
`mandatory_task_group` tinyint NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `entities_id` (`entities_id`),
KEY `is_active` (`is_active`)
KEY `entities_id` (`entities_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
";
$DB->doQuery($query);
}

// Migration: Add use_parent_entity column if it doesn't exist
if (!$DB->fieldExists($table, 'use_parent_entity')) {
$migration->displayMessage("Adding use_parent_entity field to $table");
$migration->addField($table, 'use_parent_entity', 'tinyint', [
'after' => 'entities_id',
'value' => 0,
'nodefault' => false,
]);
foreach ([
'take_requester_group_ticket',
'take_requester_group_change',
'take_requester_group_problem',
'take_technician_group_ticket',
'take_technician_group_change',
'take_technician_group_problem',
] as $field) {
$migration->changeField($table, $field, $field, 'tinyint', ['value' => 0]);
}

$entities = new Entity();
foreach ($entities->find() as $entity) {
if (is_array($entity) && isset($entity['id'])) {
$data = [
'entities_id' => $entity['id'],
];
$entity_id = (int) $entity['id'];
if ($DB->numrows($DB->request(['FROM' => self::getTable(), 'WHERE' => ['entities_id' => $entity_id]])) > 0) {

Check failure on line 349 in src/Config.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.5 - mariadb:11.8 / Continuous integration

Parameter #1 $result of method DBmysql::numrows() expects mysqli_result, DBmysqlIterator given.

Check failure on line 349 in src/Config.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.2 - mariadb:10.6 / Continuous integration

Parameter #1 $result of method DBmysql::numrows() expects mysqli_result, DBmysqlIterator given.
continue;
}
$data = ['entities_id' => $entity_id];
if ($entity_id > 0) {
foreach (self::getItilConfigFields() as $field) {
$data[$field] = self::CONFIG_PARENT;
}
}
$DB->insert(
self::getTable(),
$data,
Expand Down
22 changes: 0 additions & 22 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ public static function useConfig(CommonDBTM $item): void
}
$moconfig = Config::getConfig();

if ($moconfig->fields['is_active'] != 1) {
return;
}

switch ($item) {
case $item instanceof Ticket_User:
if ($item->fields['type'] == \CommonITILActor::REQUESTER) {
Expand Down Expand Up @@ -132,9 +128,6 @@ public static function useConfig(CommonDBTM $item): void
public static function addItemGroups(CommonDBTM $item): void
{
$conf = Config::getConfig();
if ($conf->fields['is_active'] != 1) {
return;
}

// Mapping of item types to their configuration fields and group classes
$itemMappings = [
Expand Down Expand Up @@ -311,9 +304,6 @@ public static function beforeCloseITILObject(CommonDBTM $item): void
public static function preventClosure(CommonDBTM $item): bool
{
$conf = Config::getConfig();
if ($conf->fields['is_active'] != 1) {
return true;
}

$tasks = [];

Expand Down Expand Up @@ -349,9 +339,6 @@ public static function preventClosure(CommonDBTM $item): bool
public static function requireFieldsToClose(CommonDBTM $item, bool $is_solution = false): bool
{
$conf = Config::getConfig();
if ($conf->fields['is_active'] != 1) {
return true;
}

$message = '';
$itemtype = get_class($item);
Expand Down Expand Up @@ -447,9 +434,6 @@ public static function requireFieldsToClose(CommonDBTM $item, bool $is_solution
public static function checkTaskRequirements(CommonDBTM $item): CommonDBTM
{
$conf = Config::getConfig();
if ($conf->fields['is_active'] != 1) {
return $item;
}

$message = '';
if ($conf->fields['mandatory_task_category'] == 1) {
Expand Down Expand Up @@ -488,9 +472,6 @@ public static function checkTaskRequirements(CommonDBTM $item): CommonDBTM
public static function updateItemActors(CommonITILObject $item): CommonITILObject
{
$conf = Config::getConfig();
if ($conf->fields['is_active'] != 1) {
return $item;
}

switch (get_class($item)) {
case 'Ticket':
Expand Down Expand Up @@ -557,9 +538,6 @@ public static function updateItemActors(CommonITILObject $item): CommonITILObjec
public static function assignTechnicianFromTask(\CommonITILTask $item): void
{
$conf = Config::getConfig(Session::getActiveEntity());
if ($conf->fields['is_active'] != 1) {
return;
}

// Check if a technician is assigned to the task
if (empty($item->fields['users_id_tech'])) {
Expand Down
Loading
Loading