diff --git a/src/Config.php b/src/Config.php index 5bcddda..64ab0a5 100644 --- a/src/Config.php +++ b/src/Config.php @@ -121,6 +121,7 @@ public static function preItemUpdate(CommonDBTM $item): CommonDBTM public static function getItilConfigFields(): array { return [ + 'use_parent_entity', 'take_item_group_ticket', 'take_item_group_change', 'take_item_group_problem', @@ -169,31 +170,28 @@ public static function getSelectableActorGroup(): array public static function showForEntity(Entity $item): void { - // $parents = getAncestorsOf(Entity::getTable(), $item->getID()); - // if (!empty($parents)) { - // foreach ($parents as $parent) { - // $pconfig = new Config(); - // $pconfig->getFromDBByCrit([ - // 'entities_id' => $parent, - // ]); - // if ($pconfig->getField('is_active') == 1) { - // $pentity = $parent; - // } - // } - // $csconfig = new self(); - // $csconfig->getFromDBByCrit([ - // 'entities_id' => $pentity ?? 0, - // ]); - // } $moconfig = new self(); $moconfig->getFromDBByCrit([ '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(); + } + } + TemplateRenderer::getInstance()->display( '@moreoptions/config.html.twig', [ 'item' => $moconfig, 'dropdown_options' => self::getSelectableActorGroup(), + 'parent_entity_info' => $parentEntityInfo, 'params' => [ 'canedit' => true, ], @@ -215,12 +213,34 @@ public static function addConfig(CommonDBTM $item): void ]); } - public static function getCurrentConfig(): self + /** + * Get configuration for an entity + * + * @param int|null $entityId Entity ID (null = current active entity) + * @param bool $useInheritance Whether to follow parent entity inheritance (default: true) + * @return self + */ + public static function getConfig(?int $entityId = null, bool $useInheritance = true): self { + // Use current entity if not specified + if ($entityId === null) { + $entityId = Session::getActiveEntity(); + } + $moconfig = new self(); $moconfig->getFromDBByCrit([ - 'entities_id' => Session::getActiveEntity(), + '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) { + $entity = new Entity(); + if ($entity->getFromDB($entityId)) { + $parentId = $entity->fields['entities_id']; + return self::getConfig($parentId, true); + } + } + return $moconfig; } @@ -235,6 +255,7 @@ public static function install(Migration $migration): void `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 '0', `take_item_group_change` tinyint NOT NULL DEFAULT '0', `take_item_group_problem` tinyint NOT NULL DEFAULT '0', diff --git a/src/Controller.php b/src/Controller.php index f5ca861..fd85d38 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -84,10 +84,7 @@ public static function useConfig(CommonDBTM $item): void if ($item->fields['type'] == \CommonITILActor::OBSERVER) { return; } - $moconfig = new Config(); - $moconfig->getFromDBByCrit([ - 'entities_id' => Session::getActiveEntity(), - ]); + $moconfig = Config::getConfig(); if ($moconfig->fields['is_active'] != 1) { return; @@ -134,7 +131,7 @@ public static function useConfig(CommonDBTM $item): void public static function addItemGroups(CommonDBTM $item): void { - $conf = Config::getCurrentConfig(); + $conf = Config::getConfig(); if ($conf->fields['is_active'] != 1) { return; } @@ -307,7 +304,7 @@ public static function beforeCloseITILObject(CommonITILObject $item): void public static function preventClosure(CommonDBTM $item): void { - $conf = Config::getCurrentConfig(); + $conf = Config::getConfig(); if ($conf->fields['is_active'] != 1) { return; } @@ -344,7 +341,7 @@ public static function preventClosure(CommonDBTM $item): void public static function requireFieldsToClose(CommonITILObject $item): void { - $conf = Config::getCurrentConfig(); + $conf = Config::getConfig(); if ($conf->fields['is_active'] != 1) { return; } @@ -436,7 +433,7 @@ public static function requireFieldsToClose(CommonITILObject $item): void public static function checkTaskRequirements(CommonDBTM $item): CommonDBTM { - $conf = Config::getCurrentConfig(); + $conf = Config::getConfig(); if ($conf->fields['is_active'] != 1) { return $item; } @@ -477,7 +474,7 @@ public static function checkTaskRequirements(CommonDBTM $item): CommonDBTM public static function updateItemActors(CommonITILObject $item): CommonITILObject { - $conf = Config::getCurrentConfig(); + $conf = Config::getConfig(); if ($conf->fields['is_active'] != 1) { return $item; } diff --git a/templates/config.html.twig b/templates/config.html.twig index e251b57..7d088c6 100644 --- a/templates/config.html.twig +++ b/templates/config.html.twig @@ -59,6 +59,19 @@ __('Active on this entity', 'moreoptions') ) }} + {% if item.fields['entities_id'] != 0 %} + {{ fields.checkboxField( + 'use_parent_entity', + item.fields['use_parent_entity'], + __('Use parent entity configuration', 'moreoptions'), + {'helper': __('When enabled, this entity will inherit configuration from its parent entity', 'moreoptions')} + ) }} + + {% if parent_entity_info %} + {{ alerts.alert_info(__('Configuration is inherited from parent entity: %s', 'moreoptions')|format(parent_entity_info)) }} + {% endif %} + {% endif %} +
@@ -257,6 +270,29 @@