Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- CI: fix Psalm cache directory, drop the stray `glpi-project/tools` composer dependency, declare a unique composer autoloader suffix
- Speed up the Lifecycle tab of the user preferences and the location lookup when applying a model, and remove the SQL queries the plugin ran on every page to check its own activation

## [2.10.6] - 2026-09-18

Expand Down
42 changes: 34 additions & 8 deletions inc/preference.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function handleLocationPref(array $input): array

public function showFormUserPreferences()
{
/** @var DBmysql $DB */
global $DB;

$entity = $_SESSION['glpiactive_entity'];
$userID = Session::getLoginUserID();
$templates = PluginUninstallUninstall::getAllTemplatesByEntity(
Expand All @@ -78,21 +81,40 @@ public function showFormUserPreferences()
echo "<th>" . __s("Item's location after uninstall", "uninstall") . "</th>";
echo "</tr>";

foreach ($templates as $ID => $name) {
$pref_ID = self::checkIfPreferenceExistsByEntity($userID, $ID, $entity);
// One query for all the user preferences in this entity instead of two per model
$prefs = [];
foreach (
$DB->request([
'SELECT' => ['id', 'templates_id', 'locations_action', 'locations_id'],
'FROM' => self::getTable(),
'WHERE' => [
'users_id' => $userID,
'entities_id' => $entity,
],
]) as $row
) {
$prefs[$row['templates_id']] ??= $row;
}

if (!$pref_ID) {
$pref_ID = self::addDefaultPreference($userID, $ID, $entity);
foreach ($templates as $ID => $name) {
if (!isset($prefs[$ID])) {
// Default preference: keep the previous location (see handleLocationPref())
$prefs[$ID] = [
'id' => self::addDefaultPreference($userID, $ID, $entity),
'locations_action' => 'old',
'locations_id' => 0,
];
}

$this->getFromDB($pref_ID);
$pref = $prefs[$ID];
$pref_ID = $pref['id'];

echo "<tr class='tab_bg_1'><td>" . $name . "</td>";
echo "<td>";
$value = ($this->fields["locations_id"] ?? 0);
$value = ($pref["locations_id"] ?? 0);

// Handle "old" special value, dropdown expect "-1" in this case
if ($this->fields['locations_action'] == "old" && $value == 0) {
if ($pref['locations_action'] == "old" && $value == 0) {
$value = -1;
}

Expand Down Expand Up @@ -321,6 +343,9 @@ public static function install(Migration $migration)
),
);
}

// Preferences are always searched by user, entity and model
$migration->addKey($table, ['users_id', 'entities_id', 'templates_id'], 'users_entities_templates');
} else {
// plugin nevers installed
$query = "CREATE TABLE `" . $table . "` (
Expand All @@ -330,7 +355,8 @@ public static function install(Migration $migration)
`templates_id` int {$default_key_sign} DEFAULT '0',
`locations_action` varchar(10) NOT NULL DEFAULT 'set',
`locations_id` int {$default_key_sign} DEFAULT '0',
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
KEY `users_entities_templates` (`users_id`, `entities_id`, `templates_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
$DB->doQuery($query);
}
Expand Down
5 changes: 3 additions & 2 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ function plugin_init_uninstall()
Plugin::registerClass(PluginUninstallPreference::class, ['addtabon' => [Preference::class]]);
Plugin::registerClass(PluginUninstallProfile::class, ['addtabon' => [Profile::class]]);

$plugin = new Plugin();
if ($plugin->isActivated('uninstall')) {
// Core also runs this init while installing or activating the plugin: keep the guard,
// but read the list of active plugins filled at boot instead of querying glpi_plugins.
if (in_array('uninstall', Plugin::getPlugins(), true)) {
$UNINSTALL_TYPES = [Computer::class, Monitor::class, NetworkEquipment::class, Peripheral::class, Phone::class, Printer::class];
$UNINSTALL_DIRECT_CONNECTIONS_TYPE = [Monitor::class, Peripheral::class, Phone::class, Printer::class];

Expand Down
Loading