diff --git a/CHANGELOG.md b/CHANGELOG.md index 997e5a1c..4878b28d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix text area fields size and alignment - Optimize container loading when there are a large number of entities - Adding a verification in refreshContainer function for obj value which can be an empty string instead of an array +- Fix GenericObject type dropdowns migration ## [1.24.0] - 2026-04-16 diff --git a/inc/container.class.php b/inc/container.class.php index c3956d33..850ac2c2 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -163,38 +163,7 @@ public static function installBaseData(Migration $migration, $version) // Get itemtypes from PluginGenericobject if ($DB->tableExists('glpi_plugin_genericobject_types')) { - // Check GenericObject version - $genericobject_info = Plugin::getInfo('genericobject'); - if (version_compare($genericobject_info['version'] ?? '0', '3.0.0', '<')) { - throw new RuntimeException( - 'GenericObject plugin cannot be migrated. Please update it to the latest version.', - ); - } - - // Check glpi_plugin_genericobject_types table - if (!$DB->fieldExists('glpi_plugin_genericobject_types', 'itemtype')) { - throw new RuntimeException( - 'Integrity error on the glpi_plugin_genericobject_types table from the GenericObject plugin.', - ); - } - - $migration_genericobject_itemtype = []; - $result = $DB->request(['FROM' => 'glpi_plugin_genericobject_types']); - foreach ($result as $type) { - $customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset'; - if (str_ends_with((string) $type['itemtype'], 'Model')) { - $customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetModel'; - } elseif (str_ends_with((string) $type['itemtype'], 'Type')) { - $customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetType'; - } - - $migration_genericobject_itemtype[$type['itemtype']] = [ - 'genericobject_itemtype' => $type['itemtype'], - 'itemtype' => $customasset_classname, - 'genericobject_name' => $type['name'], - 'name' => $type['name'] . 'Asset', - ]; - } + $migration_genericobject_itemtype = PluginFieldsMigration::getGenericObjectTypes(); // Get containers with PluginGenericobject itemtype $result = $DB->request([ diff --git a/inc/field.class.php b/inc/field.class.php index 44bf2cd8..52572425 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -153,6 +153,47 @@ public static function installBaseData(Migration $migration, $version) $migration->addConfig(['stable_search_options' => 'yes'], 'plugin:fields'); } + // Update old genericobject_itemtype dropdown fields to customasset_itemtype dropdown fields + if ($DB->tableExists('glpi_plugin_genericobject_types')) { + // Get all types from PluginGenericobject + $migration_genericobject_itemtypes = PluginFieldsMigration::getGenericObjectTypes(); + + foreach ($migration_genericobject_itemtypes as $type) { + // Check if genericobject and customasset itemtypes exist + if (!class_exists($type['genericobject_itemtype'])) { + $migration->addDebugMessage(sprintf( + 'The itemtype %s does not exist, please check if %s.class.php is present', + $type['genericobject_itemtype'], + $type['genericobject_name'], + )); + continue; + } + + $itemtype = str_replace('\\\\', '\\', $type['itemtype']); + if (!class_exists($itemtype)) { + $migration->addDebugMessage(sprintf( + 'The itemtype %s does not exist, please check if %s.class.php is present', + $itemtype, + $type['name'], + )); + continue; + } + + // If corresponding customasset_itemtype exists, update field type + $migration->addPostQuery( + $DB->buildUpdate( + self::getTable(), + [ + 'type' => 'dropdown-' . $itemtype, + ], + [ + 'type' => ['LIKE', 'dropdown-' . $type['genericobject_itemtype'] . '%'], + ], + ), + ); + } + } + return true; } diff --git a/inc/migration.class.php b/inc/migration.class.php index 0e970ee0..7206266c 100644 --- a/inc/migration.class.php +++ b/inc/migration.class.php @@ -200,4 +200,45 @@ private static function getCustomFieldsInContainerTable( fn(string $field) => !in_array($field, $basic_fields, true), ); } + + public static function getGenericObjectTypes() + { + /** @var DBmysql $DB */ + global $DB; + + // Check GenericObject version + $genericobject_info = Plugin::getInfo('genericobject'); + if (version_compare($genericobject_info['version'] ?? '0', '3.0.0', '<')) { + throw new RuntimeException( + 'GenericObject plugin cannot be migrated. Please update it to the latest version.', + ); + } + + // Check glpi_plugin_genericobject_types table + if (!$DB->fieldExists('glpi_plugin_genericobject_types', 'itemtype')) { + throw new RuntimeException( + 'Integrity error on the glpi_plugin_genericobject_types table from the GenericObject plugin.', + ); + } + + $migration_genericobject_itemtype = []; + $result = $DB->request(['FROM' => 'glpi_plugin_genericobject_types']); + foreach ($result as $type) { + $customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset'; + if (str_ends_with((string) $type['itemtype'], 'Model')) { + $customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetModel'; + } elseif (str_ends_with((string) $type['itemtype'], 'Type')) { + $customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetType'; + } + + $migration_genericobject_itemtype[$type['itemtype']] = [ + 'genericobject_itemtype' => $type['itemtype'], + 'itemtype' => $customasset_classname, + 'genericobject_name' => $type['name'], + 'name' => $type['name'] . 'Asset', + ]; + } + + return $migration_genericobject_itemtype; + } }