Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 14 additions & 4 deletions front/migration_status.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@
// Check if user has admin rights
Session::checkRight('config', UPDATE);

/** @var array $CFG_GLPI */
/** @var DBmysql $DB */
global $DB;
global $CFG_GLPI, $DB;

// Handle rename POST action
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['type_id'], $_POST['new_name'])) {
$type_id = (int) $_POST['type_id'];
$new_name = (string) $_POST['new_name'];
PluginGenericobjectType::renameType($type_id, $new_name);
Html::redirect($CFG_GLPI['root_doc'] . '/plugins/genericobject/front/migration_status.php');
}

// Get all GenericObject types
$genericobject_types = [];
if ($DB->tableExists(PluginGenericobjectType::getTable())) {
$query = [
'SELECT' => ['itemtype', 'name'],
'SELECT' => ['id', 'name'],
'FROM' => PluginGenericobjectType::getTable(),
];
$request = $DB->request($query);
Expand Down Expand Up @@ -70,8 +79,9 @@

// Render the template content
TemplateRenderer::getInstance()->display('@genericobject/migration_status.html.twig', [
'genericobject_types' => $genericobject_types,
'customassets' => $customassets,
'genericobject_types' => $genericobject_types,
'customassets' => $customassets,
'reserved_names' => array_map('strtolower', PluginGenericobjectType::getReservedNames()),
]);

// Display GLPI footer
Expand Down
326 changes: 265 additions & 61 deletions inc/type.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class PluginGenericobjectType extends CommonDBTM

public const CAN_OPEN_TICKET = 1024;

public const MAX_TYPE_NAME_LENGTH = 25;

public $dohistory = true;

public static $rightname = 'plugin_genericobject_types';
Expand Down Expand Up @@ -749,80 +751,240 @@ private static function normalizeNamesAndItemtypes(Migration $migration)
continue;
}

self::updateNameAndItemtype(
self::applyTypeRename(
$migration,
$type['id'],
$old_name,
$new_name,
$old_itemtype,
$new_itemtype,
);
}

$DB->update(
self::getTable(),
[
'name' => $new_name,
'itemtype' => $new_itemtype,
],
['id' => $type['id']],
);
ProfileRight::cleanAllPossibleRights();
}

$DB->update(
self::getTable(),
[
'linked_itemtypes' => new QueryExpression(
'REPLACE('
. $DB->quoteName('linked_itemtypes')
. ','
. $DB->quoteValue('"' . $old_itemtype . '"') // itemtype is surrounded by quotes
. ','
. $DB->quoteValue('"' . $new_itemtype . '"') // itemtype is surrounded by quotes
. ')',
),
],
['linked_itemtypes' => ['LIKE', '%"' . $old_itemtype . '"%']],
);
/**
* Apply a full rename for a single type: update files, database tables,
* foreign keys, relation tables, linked_itemtypes, and related dropdowns.
*
* @param Migration $migration
* @param int $type_id ID of the type in glpi_plugin_genericobject_types
* @param string $old_name Current type name
* @param string $new_name New type name
* @param string $old_itemtype Current itemtype class name
* @param string $new_itemtype New itemtype class name
*/
private static function applyTypeRename(
Migration $migration,
int $type_id,
string $old_name,
string $new_name,
string $old_itemtype,
string $new_itemtype,
): void {
/** @var DBmysql $DB */
global $DB;

// Handle dropdowns related to itemtype
$table = getTableForItemType($new_itemtype);
$fields = $DB->listFields($table);
foreach ($fields as $field => $options) {
if (preg_match("/s_id$/", $field)) {
$dropdown_old_table = getTableNameForForeignKeyField($field);

if (!preg_match('/^glpi_plugin_genericobject_/', $dropdown_old_table)) {
continue;
}

$dropdown_old_name = getSingular(
str_replace(
"glpi_plugin_genericobject_",
"",
$dropdown_old_table,
),
);
$dropdown_old_itemtype = 'PluginGenericobject' . ucfirst($dropdown_old_name);
$dropdown_new_name = self::filterInput($dropdown_old_name);
$dropdown_new_itemtype = self::getClassByName($dropdown_new_name);

if (
$dropdown_old_name == $dropdown_new_name
&& $dropdown_old_itemtype == $dropdown_new_itemtype
) {
continue;
}

self::updateNameAndItemtype(
$migration,
$dropdown_old_name,
$dropdown_new_name,
$dropdown_old_itemtype,
$dropdown_new_itemtype,
);
self::updateNameAndItemtype(
$migration,
$old_name,
$new_name,
$old_itemtype,
$new_itemtype,
);

$DB->update(
self::getTable(),
[
'name' => $new_name,
'itemtype' => $new_itemtype,
],
['id' => $type_id],
);

$DB->update(
self::getTable(),
[
'linked_itemtypes' => new QueryExpression(
'REPLACE('
. $DB->quoteName('linked_itemtypes')
. ','
. $DB->quoteValue('"' . $old_itemtype . '"') // itemtype is surrounded by quotes
. ','
. $DB->quoteValue('"' . $new_itemtype . '"') // itemtype is surrounded by quotes
. ')',
),
],
['linked_itemtypes' => ['LIKE', '%"' . $old_itemtype . '"%']],
);

// Handle dropdowns related to itemtype
$table = getTableForItemType($new_itemtype);
$fields = $DB->listFields($table);
foreach ($fields as $field => $options) {
if (preg_match("/s_id$/", $field)) {
$dropdown_old_table = getTableNameForForeignKeyField($field);

if (!preg_match('/^glpi_plugin_genericobject_/', $dropdown_old_table)) {
continue;
}

$dropdown_old_name = getSingular(
str_replace(
"glpi_plugin_genericobject_",
"",
$dropdown_old_table,
),
);
$dropdown_old_itemtype = 'PluginGenericobject' . ucfirst($dropdown_old_name);
$dropdown_new_name = self::filterInput($dropdown_old_name);
$dropdown_new_itemtype = self::getClassByName($dropdown_new_name);

if (
$dropdown_old_name == $dropdown_new_name
&& $dropdown_old_itemtype == $dropdown_new_itemtype
) {
continue;
}

self::updateNameAndItemtype(
$migration,
$dropdown_old_name,
$dropdown_new_name,
$dropdown_old_itemtype,
$dropdown_new_itemtype,
);
}
}

ProfileRight::cleanAllPossibleRights(); // Clean all possible rights are their name may have change
self::applyPluginsRename($migration, $old_itemtype, $new_itemtype);
}

/**
* Rename a genericobject type.
*
* Updates the type name, itemtype, all generated files, database tables,
* foreign keys, and all relation tables where the itemtype is referenced.
*
* @param int $type_id ID of the type in glpi_plugin_genericobject_types
* @param string $new_name New name for the type (will be filtered)
*
* @return bool True on success, false on failure
*/
public static function renameType(int $type_id, string $new_name): bool
{
/** @var DBmysql $DB */
global $DB;

$type = new self();
if (!$type->getFromDB($type_id)) {
Session::addMessageAfterRedirect(
__s('Type not found.', 'genericobject'),
false,
ERROR,
);
return false;
}

$old_name = $type->fields['name'];
$new_name = self::filterInput($new_name);

if ($new_name === '') {
Session::addMessageAfterRedirect(
__s('The new name cannot be empty.', 'genericobject'),
false,
ERROR,
);
return false;
}

if ($new_name === $old_name) {
return true;
}

$existing = $DB->request([
'FROM' => self::getTable(),
'WHERE' => [
'name' => $new_name,
'NOT' => ['id' => $type_id],
],
]);
if ($existing->numrows() > 0) {
Session::addMessageAfterRedirect(
__s('A type with this name already exists.', 'genericobject'),
false,
ERROR,
);
return false;
}

$manager = \Glpi\Asset\AssetDefinitionManager::getInstance();
$reserved_pattern = $manager->getReservedSystemNamesPattern();
if (preg_match($reserved_pattern, $new_name) === 1) {
Session::addMessageAfterRedirect(
__s('This name is reserved by a native GLPI asset type.', 'genericobject'),
false,
ERROR,
);
return false;
}

$new_system_name = self::getSystemName($new_name);
if (strlen($new_system_name) > self::MAX_TYPE_NAME_LENGTH) {
Session::addMessageAfterRedirect(
sprintf(
__s('The name "%s" is too long. The maximum allowed length is %d characters.', 'genericobject'),
$new_system_name,
self::MAX_TYPE_NAME_LENGTH,
),
false,
ERROR,
);
return false;
}

$old_itemtype = $type->fields['itemtype'];
$new_itemtype = self::getClassByName($new_name);

$migration = new Migration(PLUGIN_GENERICOBJECT_VERSION);
self::applyTypeRename(
$migration,
$type_id,
$old_name,
$new_name,
$old_itemtype,
$new_itemtype,
);
ProfileRight::cleanAllPossibleRights();
$migration->executeMigration();

Session::addMessageAfterRedirect(
sprintf(
__s('Type "%s" has been renamed to "%s".', 'genericobject'),
$old_name,
$new_name,
),
false,
INFO,
);

return true;
}

/**
* Get the list of reserved GLPI core asset names.
*
* @return string[]
*/
public static function getReservedNames(): array
{
$manager = \Glpi\Asset\AssetDefinitionManager::getInstance();
$pattern = $manager->getReservedSystemNamesPattern();
if (preg_match('/\(([^)]+)\)/', $pattern, $matches)) {
return explode('|', $matches[1]);
}
return [];
}

/**
Expand Down Expand Up @@ -963,4 +1125,46 @@ private static function updateNameAndItemtype(
);
}
}

/**
* Update all plugin data when a genericobject type is renamed.
*
* @param Migration $migration
* @param string $old_itemtype Old itemtype class name
* @param string $new_itemtype New itemtype class name
*/
private static function applyPluginsRename(
Migration $migration,
string $old_itemtype,
string $new_itemtype,
): void {
/** @var DBmysql $DB */
global $DB;

$columns = $DB->request([
'SELECT' => ['TABLE_NAME'],
'FROM' => 'information_schema.COLUMNS',
'WHERE' => [
'TABLE_SCHEMA' => $DB->dbdefault,
'TABLE_NAME' => ['LIKE', 'glpi\_plugin\_%'],
'COLUMN_NAME' => 'itemtypes',
],
]);

foreach ($columns as $row) {
$DB->update(
$row['TABLE_NAME'],
[
'itemtypes' => new QueryExpression(
'REPLACE('
. $DB->quoteName('itemtypes')
. ', ' . $DB->quoteValue(json_encode($old_itemtype))
. ', ' . $DB->quoteValue(json_encode($new_itemtype))
. ')',
),
],
['itemtypes' => ['LIKE', '%' . $old_itemtype . '%']],
);
}
}
}
Loading