diff --git a/CHANGELOG.md b/CHANGELOG.md index 89321bc..04fd772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed + +- 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 + ### Added - Add links to apply the uninstall models from the item form footer (when the status dropdown is not replaced) diff --git a/inc/preference.class.php b/inc/preference.class.php index beb9319..a7a1038 100644 --- a/inc/preference.class.php +++ b/inc/preference.class.php @@ -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( @@ -78,21 +81,38 @@ public function showFormUserPreferences() echo "" . __s("Item's location after uninstall", "uninstall") . ""; echo ""; - 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) { + // 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 "" . $name . ""; echo ""; - $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; } @@ -321,6 +341,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 . "` ( @@ -330,7 +353,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); } diff --git a/setup.php b/setup.php index 03d8105..b66d474 100644 --- a/setup.php +++ b/setup.php @@ -53,8 +53,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];