diff --git a/CHANGELOG.md b/CHANGELOG.md index ab27e6d..750494d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [UNRELEASED] -### Fixed +### Added -- Fix locales encoding +- Add Links for uninstall templates From Item Form ## [2.10.3] - 2025-11-25 diff --git a/inc/uninstall.class.php b/inc/uninstall.class.php index cac884c..d85555e 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 @@ -60,6 +59,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"; @@ -924,13 +927,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()) { + $used = array_column( + iterator_to_array( + $DB->request([ + 'SELECT' => ['id'], + 'FROM' => 'glpi_plugin_uninstall_models', + 'WHERE' => [ + 'types_id' => [2, 3], + ], + ]), + ), + '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, + ); + } - $params = ['templates_id' => '__VALUE__', - 'entity' => $item->fields["entities_id"], - 'users_id' => $_SESSION["glpiID"], - ]; + 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 => __('Keep previous location', 'uninstall'), + 0 => __s('Empty location', 'uninstall'), + ], + ]); + echo ""; + } - Ajax::updateItemOnSelectEvent( - 'dropdown_model_id' . $rand, - "show_objects", - $CFG_GLPI['root_doc'] . "/plugins/uninstall/ajax/locations.php", - $params, - ); + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + Html::closeForm(); } + } - echo "" . __s("Item's location after applying model", "uninstall") . ""; - echo "\n" . Dropdown::EMPTY_VALUE . "\n"; - echo ""; + public static function showLinksUninstallation( + $params, + ) { + /** + * @var array $UNINSTALL_TYPES + */ + global $DB, $UNINSTALL_TYPES; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - Html::closeForm(); + $right = Session::haveRight(self::$rightname, READ); + + $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', + ]; + + if (!PluginUninstallModel::canReplace()) { + $criteria['WHERE'] = ['NOT' => ['types_id' => [2, 3]]]; + } + + + $iterator = $DB->request($criteria); + foreach ($iterator as $data) { + + $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..90c529c 100644 --- a/setup.php +++ b/setup.php @@ -79,6 +79,10 @@ 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::showLinksUninstallation(...); + } + $PLUGIN_HOOKS[Hooks::STALE_AGENT_CONFIG]['uninstall'] = [ [ 'label' => __s('Apply uninstall profile'),