-
Notifications
You must be signed in to change notification settings - Fork 3
Change config UI #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ class Config extends CommonDBTM | |
| { | ||
| public $dohistory = true; | ||
| public static $rightname = 'config'; | ||
| public const CONFIG_PARENT = \Entity::CONFIG_PARENT; | ||
| public static function getMenuName(): string | ||
| { | ||
| return __('More options', 'moreoptions'); | ||
|
|
@@ -105,10 +106,8 @@ public static function preItemUpdate(CommonDBTM $item): CommonDBTM | |
| } | ||
|
|
||
| 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]; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -121,7 +120,6 @@ 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', | ||
|
|
@@ -153,6 +151,12 @@ public static function getItilConfigFields(): array | |
| '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', | ||
| ]; | ||
| } | ||
|
|
||
|
|
@@ -175,24 +179,31 @@ public static function showForEntity(Entity $item): void | |
| '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, | ||
| ], | ||
| ], | ||
|
|
@@ -204,13 +215,33 @@ public static function getIcon(): string | |
| 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); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -222,7 +253,6 @@ public static function addConfig(CommonDBTM $item): void | |
| */ | ||
| public static function getConfig(?int $entityId = null, bool $useInheritance = true): self | ||
| { | ||
| // Use current entity if not specified | ||
| if ($entityId === null) { | ||
| $entityId = Session::getActiveEntity(); | ||
| } | ||
|
|
@@ -232,12 +262,15 @@ public static function getConfig(?int $entityId = null, bool $useInheritance = t | |
| '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((int) $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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -253,18 +286,16 @@ public static function install(Migration $migration): void | |
| $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', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should drop this column if table already exist
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The plugin hasn't been released yet |
||
| `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_ticket` tinyint NOT NULL DEFAULT '0', | ||
| `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', | ||
|
|
@@ -294,29 +325,40 @@ public static function install(Migration $migration): void | |
| `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`) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should drop this column if table already exist |
||
| 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) { | ||
| if ($DB->fieldExists($table, $field)) { | ||
| $migration->changeField($table, $field, $field, 'bool', ['value' => '0']); | ||
| } | ||
| } | ||
|
|
||
| $migration->executeMigration(); | ||
|
|
||
| $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 (countElementsInTable(self::getTable(), ['entities_id' => $entity_id]) > 0) { | ||
| 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, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self::getItilConfigFields already contain
The first loop at line
185 therefore generates a Yes/No badge for them (wrong — they need group-name labels), and the second explicit loop at line 188 immediately
overwrites with the correct badge.