From 24f3e361d1f5e5110f4e0f893a8e1427f5ee2aa1 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Tue, 24 Feb 2026 13:13:12 +0100 Subject: [PATCH 1/5] Add Links for uninstall templates From Item Form --- inc/uninstall.class.php | 162 +++++++++++++++++++++++++++++++++------- setup.php | 6 ++ 2 files changed, 139 insertions(+), 29 deletions(-) diff --git a/inc/uninstall.class.php b/inc/uninstall.class.php index c376539..561c060 100644 --- a/inc/uninstall.class.php +++ b/inc/uninstall.class.php @@ -924,13 +924,13 @@ public static function getInfocomPresentForDevice($type, $ID) * @param $ID * @param $item * @param $user_id - **/ - public static function showFormUninstallation($ID, $item, $user_id) + **/ + public static function showFormUninstallation($ID, $item, $user_id, $templates_id = 0) { /** * @var array $CFG_GLPI */ - global $CFG_GLPI; + global $CFG_GLPI, $DB; $type = $item->getType(); echo "
" . __s("Model") . ""; if (class_exists($type) && is_a($type, CommonDBTM::class, true)) { - $item = new $type(); $item->getFromDB($ID); - $rand = self::dropdownUninstallModels( - "model_id", - $_SESSION["glpiID"], - $item->fields["entities_id"], - ); + + if ($templates_id == 0) { + $rand = self::dropdownUninstallModels( + "model_id", + $_SESSION["glpiID"], + $item->fields["entities_id"], + ); + } else { + $used = []; + if (!PluginUninstallModel::canReplace()) { + foreach ( + $DB->request([ + "FROM" => 'glpi_plugin_uninstall_models', + "WHERE" => [ + 'types_id' => [2, 3], + ], + ]) as $data + ) { + $used[] = $data['id']; + } + } + + $rand = PluginUninstallModel::dropdown([ + 'name' => "model_id", + 'value' => $templates_id, + 'entity' => $item->fields["entities_id"], + 'used' => $used, + ]); + } + + + echo ""; + if ($templates_id == 0) { + $params = [ + 'templates_id' => '__VALUE__', + 'entity' => $item->fields["entities_id"], + 'users_id' => $_SESSION["glpiID"], + ]; + + Ajax::updateItemOnSelectEvent( + 'dropdown_model_id' . $rand, + "show_objects", + $CFG_GLPI['root_doc'] . "/plugins/uninstall/ajax/locations.php", + $params, + ); + } + + echo "" . __s("Item's location after applying model", "uninstall") . ""; + if ($templates_id == 0) { + echo "\n" . Dropdown::EMPTY_VALUE . "\n"; + } else { + echo ""; + $location = PluginUninstallPreference::getLocationByUserByEntity( + $user_id, + $templates_id, + $item->fields["entities_id"], + ); + Location::dropdown([ + 'value' => ($location == '' ? 0 : $location), + 'comments' => 1, + 'entity' => $item->fields["entities_id"], + 'toadd' => [ + -1 => __s('Keep previous location', 'uninstall'), + 0 => __s('Empty location', 'uninstall'), + ], + ]); + echo ""; + } + echo ""; + + echo ""; + echo ""; + echo ""; echo ""; + echo ""; + Html::closeForm(); + } + } + + public static function showLinksUninstallation( + $params + ) { + global $DB, $UNINSTALL_TYPES; + + $right = true; - $params = ['templates_id' => '__VALUE__', - 'entity' => $item->fields["entities_id"], - 'users_id' => $_SESSION["glpiID"], + $users_id = Session::getLoginUserID(); + $item = $params['item']; + if ($right + && in_array($item->getType(), $UNINSTALL_TYPES) && $item->getID() > 0) { + echo "
"; + + $criteria = [ + "FROM" => 'glpi_plugin_uninstall_models', ]; - Ajax::updateItemOnSelectEvent( - 'dropdown_model_id' . $rand, - "show_objects", - $CFG_GLPI['root_doc'] . "/plugins/uninstall/ajax/locations.php", - $params, - ); + if (!PluginUninstallModel::canReplace()) { + $criteria['WHERE'] = ['NOT' => ['types_id' => [2, 3]]]; + } - } - echo "" . __s("Item's location after applying model", "uninstall") . ""; - echo "\n" . Dropdown::EMPTY_VALUE . "\n"; - echo ""; + $iterator = $DB->request($criteria); + foreach ($iterator as $data) { - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - Html::closeForm(); + $templates_id = $data['id']; + echo "" + . $data['name'] + . ""; + + // get form for uninstall actions + ob_start(); + self::showFormUninstallation($item->getID(), $item, $users_id, $templates_id); + $html_modal = ob_get_contents(); + ob_end_clean(); + + // we json encore to pass it to js (auto-escaping) + + $modal_body = json_encode($html_modal); + + $JS = <<"; + } + + return null; } diff --git a/setup.php b/setup.php index 2b5c90d..95a2dd5 100644 --- a/setup.php +++ b/setup.php @@ -79,6 +79,12 @@ function plugin_init_uninstall() $PLUGIN_HOOKS[Hooks::PRE_ITEM_ADD]['uninstall'] = [Config::class => PluginUninstallConfig::preConfigSet(...)]; $PLUGIN_HOOKS[Hooks::PRE_ITEM_UPDATE]['uninstall'] = [Config::class => PluginUninstallConfig::preConfigSet(...)]; + if (!$uninstallconfig['replace_status_dropdown']) { + $PLUGIN_HOOKS[Hooks::POST_ITEM_FORM]['uninstall'] = [ + PluginUninstallUninstall::class, + 'showLinksUninstallation' + ]; + } $PLUGIN_HOOKS[Hooks::STALE_AGENT_CONFIG]['uninstall'] = [ [ 'label' => __s('Apply uninstall profile'), From 154fc1d9628b990b8bb1a1bd2608d12b9a7572a7 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Tue, 24 Feb 2026 13:27:16 +0100 Subject: [PATCH 2/5] Try fix CI --- inc/uninstall.class.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/inc/uninstall.class.php b/inc/uninstall.class.php index 561c060..542d628 100644 --- a/inc/uninstall.class.php +++ b/inc/uninstall.class.php @@ -60,6 +60,10 @@ * ------------------------------------------------------------------------- */ +use function Safe\json_encode; +use function Safe\ob_end_clean; +use function Safe\ob_start; + class PluginUninstallUninstall extends CommonDBTM { public const PLUGIN_UNINSTALL_TRANSFER_NAME = "plugin_uninstall"; @@ -1026,9 +1030,12 @@ class='submit'>"; public static function showLinksUninstallation( $params ) { + /** + * @var array $UNINSTALL_TYPES + */ global $DB, $UNINSTALL_TYPES; - $right = true; + $right = Session::haveRight(self::$rightname, READ); $users_id = Session::getLoginUserID(); $item = $params['item']; From f825c3afd30476ef4556669b1862e72a5e3dbbc9 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Tue, 24 Feb 2026 13:35:02 +0100 Subject: [PATCH 3/5] Fix Rector --- ajax/dropdownReplaceFindDevice.php | 6 +++--- inc/replace.class.php | 2 +- inc/uninstall.class.php | 2 +- setup.php | 5 +---- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/ajax/dropdownReplaceFindDevice.php b/ajax/dropdownReplaceFindDevice.php index 054baf3..85cbf49 100644 --- a/ajax/dropdownReplaceFindDevice.php +++ b/ajax/dropdownReplaceFindDevice.php @@ -83,7 +83,7 @@ if ( isset($_REQUEST['searchText']) - && strlen($_REQUEST['searchText']) > 0 + && (string) $_REQUEST['searchText'] !== '' && $_REQUEST['searchText'] != $CFG_GLPI["ajax_wildcard"] ) { // isset already makes sure the search value isn't null @@ -143,7 +143,7 @@ ); } - if ((strlen((string) $withoutput) > 0) && ($withoutput != ' ')) { + if (((string) $withoutput !== '') && ($withoutput != ' ')) { $outputval = sprintf(__s('%1$s - %2$s'), $outputval, $withoutput); } } @@ -160,7 +160,7 @@ if ( $_SESSION["glpiis_ids_visible"] - || (strlen((string) $outputval) == 0) + || ((string) $outputval === '') ) { $outputval = sprintf(__s('%1$s (%2$s)'), $outputval, $ID); } diff --git a/inc/replace.class.php b/inc/replace.class.php index b6aaf87..6c533ee 100644 --- a/inc/replace.class.php +++ b/inc/replace.class.php @@ -464,7 +464,7 @@ public static function replace($type, $model_id, $tab_ids, $location) } // Location - if ((int) $location != 0 && $olditem->isField('locations_id')) { + if ((int) $location !== 0 && $olditem->isField('locations_id')) { $olditem->getFromDB($olditem_id); switch ($location) { case -1: diff --git a/inc/uninstall.class.php b/inc/uninstall.class.php index 542d628..958c4e6 100644 --- a/inc/uninstall.class.php +++ b/inc/uninstall.class.php @@ -1072,7 +1072,7 @@ public static function showLinksUninstallation( $JS = << PluginUninstallConfig::preConfigSet(...)]; if (!$uninstallconfig['replace_status_dropdown']) { - $PLUGIN_HOOKS[Hooks::POST_ITEM_FORM]['uninstall'] = [ - PluginUninstallUninstall::class, - 'showLinksUninstallation' - ]; + $PLUGIN_HOOKS[Hooks::POST_ITEM_FORM]['uninstall'] = PluginUninstallUninstall::showLinksUninstallation(...); } $PLUGIN_HOOKS[Hooks::STALE_AGENT_CONFIG]['uninstall'] = [ [ From 6a157f06f5aae9434bc4099419e8ef0fe4b4e9f0 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Tue, 24 Feb 2026 13:43:13 +0100 Subject: [PATCH 4/5] Fix CS --- inc/uninstall.class.php | 2 ++ setup.php | 1 + 2 files changed, 3 insertions(+) diff --git a/inc/uninstall.class.php b/inc/uninstall.class.php index 958c4e6..cf07bc0 100644 --- a/inc/uninstall.class.php +++ b/inc/uninstall.class.php @@ -1015,6 +1015,7 @@ public static function showFormUninstallation($ID, $item, $user_id, $templates_i ]); echo ""; } + echo ""; echo ""; @@ -1083,6 +1084,7 @@ public static function showLinksUninstallation( JAVASCRIPT; echo Html::scriptBlock($JS); } + echo "
"; } diff --git a/setup.php b/setup.php index b8ec9ae..90c529c 100644 --- a/setup.php +++ b/setup.php @@ -82,6 +82,7 @@ function plugin_init_uninstall() if (!$uninstallconfig['replace_status_dropdown']) { $PLUGIN_HOOKS[Hooks::POST_ITEM_FORM]['uninstall'] = PluginUninstallUninstall::showLinksUninstallation(...); } + $PLUGIN_HOOKS[Hooks::STALE_AGENT_CONFIG]['uninstall'] = [ [ 'label' => __s('Apply uninstall profile'), From e9c3970b1c46ab21fd660a3007735ca9157e646a Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Tue, 24 Feb 2026 16:17:37 +0100 Subject: [PATCH 5/5] CS Fixer --- inc/uninstall.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inc/uninstall.class.php b/inc/uninstall.class.php index cf07bc0..be78ad7 100644 --- a/inc/uninstall.class.php +++ b/inc/uninstall.class.php @@ -31,7 +31,6 @@ use Glpi\Asset\Asset_PeripheralAsset; use function Safe\preg_grep; - /** * ------------------------------------------------------------------------- * Uninstall plugin for GLPI @@ -1029,7 +1028,7 @@ class='submit'>"; } public static function showLinksUninstallation( - $params + $params, ) { /** * @var array $UNINSTALL_TYPES