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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [UNRELEASED]

### Fixed
### Added

- Add Links for uninstall templates From Item Form
- Fix SQL errors when uninstalling or replacing peripheral assets
- Fix locales encoding

Expand Down
4 changes: 2 additions & 2 deletions ajax/dropdownReplaceFindDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
throw new AccessDeniedHttpException(__s("You don't have permission to perform this action."));
}

if (class_exists($_REQUEST['itemtype']) && is_a($_REQUEST['itemtype'], CommonDBTM::class, true)) {
$item = getItemForItemtype($_REQUEST['itemtype']);
if ($item instanceof CommonDBTM) {
$itemtypeisplugin = isPluginItemType($_REQUEST['itemtype']);
$item = new $_REQUEST['itemtype']();
$table = getTableForItemType($_REQUEST['itemtype']);
$options = [];
$count = 0;
Expand Down
2 changes: 2 additions & 0 deletions front/config.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* -------------------------------------------------------------------------
*/

declare(strict_types=1);

Session::checkRight('config', UPDATE);

/** @var array $CFG_GLPI */
Expand Down
41 changes: 10 additions & 31 deletions inc/state.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
* -------------------------------------------------------------------------
*/

use function Safe\json_encode;
use function Safe\ob_end_clean;
use function Safe\ob_start;
use Glpi\Application\View\TemplateRenderer;

class PluginUninstallState
{
Expand Down Expand Up @@ -58,36 +56,17 @@ public static function replaceState($params = [])
'complete' => true,
]);

// get form for uninstall actions
ob_start();
PluginUninstallUninstall::showFormUninstallation($items_id, $item, $users_id);
$html_modal = ob_get_contents();
ob_end_clean();
// Get the uninstall actions form as a string (no output buffering).
$html_modal = PluginUninstallUninstall::showFormUninstallation($items_id, $item, $users_id, 0, false);

// we json encore to pass it to js (auto-escaping)
$html = json_encode("
{$states_name}
<a href='#' id='uninstall_actions_open' class='vsubmit'>"
. __s("Update")
. "</a>");
$modal_body = json_encode($html_modal);

$JS = <<<JAVASCRIPT
$(function() {
// replace status select
var state_span = $("#page select[name=states_id]").parent();
state_span.html({$html});

$("#uninstall_actions_open").on("click", function(event) {
event.preventDefault();
// The state select is swapped at runtime by scripts/uninstall.js, which
// also wires the modal opener; nothing is echoed inline here.
TemplateRenderer::getInstance()->display('@uninstall/state_replace.html.twig', [
'rand' => mt_rand(),
'states_name' => $states_name,
'modal' => $html_modal,
]);

glpi_html_dialog({
body: {$modal_body}
})
});
});
JAVASCRIPT;
echo Html::scriptBlock($JS);
return null;
}
}
198 changes: 153 additions & 45 deletions inc/uninstall.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
*/

use Glpi\Asset\Asset_PeripheralAsset;

use function Safe\preg_grep;
use Glpi\Application\View\TemplateRenderer;

/**
* -------------------------------------------------------------------------
Expand Down Expand Up @@ -60,6 +59,8 @@
* -------------------------------------------------------------------------
*/

use function Safe\preg_grep;

class PluginUninstallUninstall extends CommonDBTM
{
public const PLUGIN_UNINSTALL_TRANSFER_NAME = "plugin_uninstall";
Expand Down Expand Up @@ -921,61 +922,165 @@ public static function getInfocomPresentForDevice($type, $ID)


/**
* @param $ID
* @param $item
* @param $user_id
**/
public static function showFormUninstallation($ID, $item, $user_id)
* Render the "apply uninstall/replace model" form.
*
* @param int|string $ID id of the item the form applies to
* @param CommonGLPI $item item the form applies to
* @param int $user_id current user id (for stored location preference)
* @param int $templates_id 0 for a fresh model selector, otherwise the model to apply
* @param bool $display true echoes the form, false returns it as a string
*
* @return string|null the HTML when $display is false
**/
public static function showFormUninstallation($ID, $item, $user_id, $templates_id = 0, $display = true)
{
/**
* @var array $CFG_GLPI
*/
global $CFG_GLPI;
global $CFG_GLPI, $DB;

$type = $item->getType();
echo "<form action='" . $CFG_GLPI['root_doc'] . "/plugins/uninstall/front/action.php'
method='post'>";
echo Html::hidden('device_type', ['value' => $type]);
echo "<table class='tab_cadre_fixe' cellpadding='5'>";
echo "<tr><th colspan='3'>" . __s("Apply model", 'uninstall') . "</th></tr>";

echo "<tr class='tab_bg_1'><td>" . __s("Model") . "</td><td>";
if (class_exists($type) && is_a($type, CommonDBTM::class, true)) {
$item = getItemForItemtype($type);
$supported = $item instanceof CommonDBTM;

$item = new $type();
$model_dropdown = '';
$location_html = '';
$onselect_js = '';

if ($supported) {
$item->getFromDB($ID);
$rand = self::dropdownUninstallModels(
"model_id",
$_SESSION["glpiID"],
$item->fields["entities_id"],
);
echo "</td></tr>";
$entities_id = $item->fields["entities_id"];

if ($templates_id == 0) {
$rand = mt_rand();
$model_dropdown = self::dropdownUninstallModels(
"model_id",
$_SESSION["glpiID"],
$entities_id,
['display' => false, 'rand' => $rand],
);

$params = ['templates_id' => '__VALUE__',
'entity' => $item->fields["entities_id"],
'users_id' => $_SESSION["glpiID"],
];
$params = [
'templates_id' => '__VALUE__',
'entity' => $entities_id,
'users_id' => $_SESSION["glpiID"],
];

$onselect_js = Ajax::updateItemOnSelectEvent(
'dropdown_model_id' . $rand,
"show_objects",
$CFG_GLPI['root_doc'] . "/plugins/uninstall/ajax/locations.php",
$params,
false,
);

Ajax::updateItemOnSelectEvent(
'dropdown_model_id' . $rand,
"show_objects",
$CFG_GLPI['root_doc'] . "/plugins/uninstall/ajax/locations.php",
$params,
);
$location_html = "<span id='show_objects'>" . Dropdown::EMPTY_VALUE . "</span>";
} 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',
);
}

$model_dropdown = PluginUninstallModel::dropdown([
'name' => "model_id",
'value' => $templates_id,
'entity' => $entities_id,
'used' => $used,
'display' => false,
]);

$location = PluginUninstallPreference::getLocationByUserByEntity(
$user_id,
$templates_id,
$entities_id,
);
$location_html = Location::dropdown([
'value' => ($location == '' ? 0 : $location),
'comments' => 1,
'entity' => $entities_id,
'toadd' => [
-1 => __('Keep previous location', 'uninstall'),
0 => __('Empty location', 'uninstall'),
],
'display' => false,
]);
}
}

$html = TemplateRenderer::getInstance()->render('@uninstall/form_uninstallation.html.twig', [
'supported' => $supported,
'action_url' => $CFG_GLPI['root_doc'] . "/plugins/uninstall/front/action.php",
'device_type' => $type,
'item_id' => $ID,
'model_dropdown' => $model_dropdown,
'location_html' => $location_html,
'onselect_js' => $onselect_js,
]);

if ($display) {
echo $html;
return null;
}

echo "<tr class='tab_bg_1'><td>" . __s("Item's location after applying model", "uninstall") . "</td>";
echo "<td><span id='show_objects'>\n" . Dropdown::EMPTY_VALUE . "</span></td>\n";
echo "</tr>";
return $html;
}

echo "<tr class='tab_bg_1 center'><td colspan='3'>";
echo "<input type='submit' name='uninstall' value=\"" . _sx('button', 'Post') . "\"
class='submit'>";
echo "<input type='hidden' name='id' value='" . $ID . "'>";
echo "</td></tr>";
echo "</table>";
Html::closeForm();
public static function showLinksUninstallation(
$params,
) {
/**
* @var array $UNINSTALL_TYPES
*/
global $DB, $UNINSTALL_TYPES;

$right = Session::haveRight(self::$rightname, READ);

$users_id = Session::getLoginUserID();
$item = $params['item'];
if (
!$right
|| !in_array($item->getType(), $UNINSTALL_TYPES)
|| $item->getID() <= 0
) {
return null;
}

$criteria = [
"FROM" => 'glpi_plugin_uninstall_models',
];

if (!PluginUninstallModel::canReplace()) {
$criteria['WHERE'] = ['NOT' => ['types_id' => [2, 3]]];
}

$links = [];
foreach ($DB->request($criteria) as $data) {
$links[] = [
'id' => (int) $data['id'],
'name' => $data['name'],
// The modal body is captured as a string (no output buffering)
// and rendered inside an inert <template> element by Twig.
'modal' => self::showFormUninstallation($item->getID(), $item, $users_id, $data['id'], false),
];
}

TemplateRenderer::getInstance()->display('@uninstall/links_uninstallation.html.twig', [
'links' => $links,
]);

return null;
}


Expand Down Expand Up @@ -1019,8 +1124,10 @@ public static function razPortInfos($type, $items_id)
* @param $name
* @param $user
* @param $entity
* @param array $options extra options forwarded to PluginUninstallModel::dropdown
* (e.g. 'display' => false to get the HTML back as a string)
**/
public static function dropdownUninstallModels($name, $user, $entity)
public static function dropdownUninstallModels($name, $user, $entity, $options = [])
{
/** @var DBmysql $DB */
global $DB;
Expand All @@ -1038,11 +1145,12 @@ public static function dropdownUninstallModels($name, $user, $entity)
}
}

return PluginUninstallModel::dropdown(['name' => $name,
return PluginUninstallModel::dropdown(array_merge([
'name' => $name,
'value' => 0,
'entity' => $entity,
'used' => $used,
]);
], $options));
}


Expand Down
Loading