Skip to content

Commit

Permalink
Replace usage of negative values in models.groups_id
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Feb 4, 2022
1 parent 0f3f89b commit ccf700a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 116 deletions.
52 changes: 0 additions & 52 deletions ajax/groups.php

This file was deleted.

3 changes: 3 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ function plugin_uninstall_install() {
PluginUninstallModel::install($migration);
PluginUninstallPreference::install($migration);
PluginUninstallConfig::install($migration);

$migration->executeMigration();

return true;
}

Expand Down
67 changes: 40 additions & 27 deletions inc/model.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ static function getMenuContent() {
return $menu;
}

function prepareInputForAdd($input) {

if (isset($input['_groups_id_action'])
&& ($input['_groups_id_action'] == 'old')) {
$input['groups_id'] = -1;
}
return $input;
}


function prepareInputForUpdate($input) {
return $this->prepareInputForAdd($input);
Expand Down Expand Up @@ -244,15 +235,19 @@ function showForm($ID, $options = []) {
echo "</td></tr>";

echo "<tr class='tab_bg_1'>";
if ($ID == -1) {
$this->fields['groups_id'] = -1;
}

if ($this->fields["types_id"] != self::TYPE_MODEL_REPLACEMENT) {
echo "<td>" . __('Action on group', 'uninstall') . "</td><td>";
$uninst = new PluginUninstallUninstall();
$action = $uninst->dropdownFieldAction("groups_id", $this->fields['entities_id'],
$entity_sons, $this->fields["groups_id"]);
Dropdown::showFromArray(
"groups_action",
[
'old' => __('Keep in the current group', 'uninstall'),
'set' => __('Affect to a new group', 'uninstall')
],
[
'on_change' => '$("#show_groups").toggle($(this).val() === "set");',
'value' => $this->fields["groups_action"],
]
);
echo "</td>";
} else {
echo "<td colspan='2'></td>";
Expand All @@ -262,15 +257,11 @@ function showForm($ID, $options = []) {
echo "<tr class='tab_bg_1'>";
if ($this->fields["types_id"] != self::TYPE_MODEL_REPLACEMENT) {
echo "<td>" . __('New group', 'uninstall') . "</td><td>";
echo "<span id='show_groups' name='show_groups'>";
if ($this->fields['groups_id'] != -1) {
Group::dropdown(['value' => $this->fields["groups_id"],
'entity' => $this->fields["entities_id"],
'entity_sons' => $entities,
'emptylabel' => __('None')]);
} else {
echo Dropdown::EMPTY_VALUE;
}
echo "<span id='show_groups' " . ($this->fields["groups_action"] === 'old' ? 'style="display:none;"' : '') . ">";
Group::dropdown(['value' => $this->fields["groups_id"],
'entity' => $this->fields["entities_id"],
'entity_sons' => $entities,
'emptylabel' => __('None')]);
echo "</span></td>";
} else {
echo "<td colspan='2'></td>";
Expand Down Expand Up @@ -841,6 +832,7 @@ function rawSearchOptions() {
'name' => __('Action on group', 'uninstall'),
'linkfield' => '',
'datatype' => 'specific',
'additionalfields' => ['groups_action'],
'searchtype' => 'equals',
];

Expand Down Expand Up @@ -991,9 +983,9 @@ static function getSpecificValueToDisplay($field, $values, array $options = [])
break;

case 'groups_id' :
if ($values['groups_id'] < 0) {
if ($values['groups_action'] === 'old') {
return __('Keep in the current group', 'uninstall');
} else if (!$values['groups_id']) {
} else if ($values['groups_id'] === 0) {
return __('None');
}
return Dropdown::getDropdownName('glpi_groups', $values['groups_id']);
Expand Down Expand Up @@ -1175,6 +1167,25 @@ static function install($migration) {
$migration->addField($table, 'raz_antivirus', "bool");
}

// 2.7.2
if (!$DB->fieldExists($table, 'groups_action')) {
$migration->addField($table, 'groups_action', "varchar(10) NOT NULL DEFAULT 'set'");
$migration->addPostQuery(
$DB->buildUpdate(
$table,
['groups_action' => 'set'],
['NOT' => ['groups_id' => '-1']]
)
);
$migration->addPostQuery(
$DB->buildUpdate(
$table,
['groups_action' => 'old', 'groups_id' => '0'],
['groups_id' => '-1']
)
);
}

$migration->migrationOneTable($table);

} else {
Expand All @@ -1200,6 +1211,7 @@ static function install($migration) {
`raz_user` int(1) NOT NULL DEFAULT '1',
`raz_ocs_registrykeys` int(1) NOT NULL DEFAULT '1',
`comment` text COLLATE utf8_unicode_ci NOT NULL,
`groups_action` varchar(10) NOT NULL DEFAULT 'set',
`groups_id` int(11) NOT NULL DEFAULT '0',
`remove_from_ocs` int(1) NOT NULL DEFAULT '0',
`delete_ocs_link` int(1) NOT NULL DEFAULT '0',
Expand Down Expand Up @@ -1281,6 +1293,7 @@ static function createTransferModel($name = 'Uninstall') {
$tmp['raz_fusioninventory'] = 1;
$tmp['raz_plugin_fields'] = 1;
$tmp['comment'] = '';
$tmp['groups_action'] = 'set';
$tmp['groups_id'] = 0;
$tmp['remove_from_ocs'] = 0;
$tmp['delete_ocs_link'] = 0;
Expand Down
38 changes: 1 addition & 37 deletions inc/uninstall.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static function uninstall($type, $model_id, $tab_ids, $location) {
if ($item->isField('groups_id')) {
$nbgroup = countElementsInTableForEntity("glpi_groups", $entity,
['id' => $item->fields['groups_id']]);
if (($model->fields["groups_id"] > -1)
if (($model->fields["groups_action"] === 'set')
&& ($nbgroup == 1)) {
// If a new group is defined and if the group is accessible in the object's entity
$fields["groups_id"] = $model->fields["groups_id"];
Expand Down Expand Up @@ -745,42 +745,6 @@ static function dropdownUninstallModels($name, $user, $entity) {
}


/**
* @param $name
* @param $entity (default 0)
* @param $entity_sons array
* @param $value (default -1)
*/
static function dropdownFieldAction($name, $entity = 0, $entity_sons = [], $value = -1) {
global $CFG_GLPI;

if ($value == -1) {
$action = 'old';
} else {
$action = 'set';
}

$tabactions = ['old' => __('Keep in the current group', 'uninstall'), // Keep the current value
'set' => __('Affect to a new group', 'uninstall')
]; // Affect a new value

preg_match('/(.*)_id/', $name, $results);
$ajax_page = $results[1];
$rand = Dropdown::showFromArray("_" . $name . "_action", $tabactions,
['value' => $action]);

$params = ['id' => '__VALUE__',
$name => $value,
'entities_id' => $entity,
'entity_sons' => $entity_sons];

Ajax::updateItemOnSelectEvent("dropdown__" . $name . "_action".$rand, "show_".$ajax_page,
Plugin::getWebDir('uninstall')."/ajax/$ajax_page.php",
$params);
return $action;
}


/**
* @param $entity
* @param $add_entity (false by default)
Expand Down

0 comments on commit ccf700a

Please sign in to comment.