Skip to content

Commit

Permalink
Fix missing form data when multi-select empty
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed May 6, 2024
1 parent f3440e8 commit 64f7c2a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 15 deletions.
2 changes: 1 addition & 1 deletion inc/db.function.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function getDateCriteria($field, $begin, $end)
/**
* Export an array to be stored in a simple field in the database
*
* @param $TAB Array to export / encode (one level depth)
* @param array|'' $TAB Array to export / encode (one level depth)
*
* @return string containing encoded array
**/
Expand Down
6 changes: 2 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,8 @@ public function prepareInputForUpdate($input)
}
}

if (isset($input['_update_devices_in_menu'])) {
$input['devices_in_menu'] = exportArrayToDB(
(isset($input['devices_in_menu']) ? $input['devices_in_menu'] : [])
);
if (isset($input['devices_in_menu'])) {
$input['devices_in_menu'] = exportArrayToDB(empty($input['devices_in_menu']) ? [] : $input['devices_in_menu']);
}

// lock mechanism update
Expand Down
2 changes: 1 addition & 1 deletion src/DbUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@ public function getDateCriteria($field, $begin, $end)
/**
* Export an array to be stored in a simple field in the database
*
* @param array $array Array to export / encode (one level depth)
* @param array|'' $array Array to export / encode (one level depth)
*
* @return string containing encoded array
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,7 @@ public static function showFromArray($name, array $elements, $options = [])
$elements = [ 0 => $param['emptylabel'] ] + $elements;
}

$original_field_name = $name;
if ($param["multiple"]) {
$field_name = $name . "[]";
} else {
Expand All @@ -2214,6 +2215,10 @@ public static function showFromArray($name, array $elements, $options = [])
}
$output .= '<span class="form-control" readonly style="width: ' . $param["width"] . '">' . implode(', ', $to_display) . '</span>';
} else {
if ($param['multiple']) {
// Fix for multiple select not sending any form data when no option is selected
$output .= "<input type='hidden' name='$original_field_name' value=''>";
}
$output .= "<select name='$field_name' id='$field_id'";

if ($param['width'] !== '') {
Expand Down
10 changes: 3 additions & 7 deletions src/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,7 @@ public function prepareInputForUpdate($input)
/** @var array $CFG_GLPI */
global $CFG_GLPI;

if (isset($input["_helpdesk_item_types"])) {
if ((!isset($input["helpdesk_item_type"])) || (!is_array($input["helpdesk_item_type"]))) {
$input["helpdesk_item_type"] = [];
}
// Linear_HIT: $input["helpdesk_item_type"] = array_keys($input["helpdesk_item_type"]
if (isset($input["helpdesk_item_type"])) {
$input["helpdesk_item_type"] = exportArrayToDB($input["helpdesk_item_type"]);
}

Expand Down Expand Up @@ -1248,7 +1244,7 @@ public function showFormTrackingHelpdesk()

echo "<tr>";
echo "<td>" . __('Associable items to tickets, changes and problems') . "</td>";
echo "<td><input type='hidden' name='_helpdesk_item_types' value='1'>";
echo "<td>";
self::dropdownHelpdeskItemtypes(['values' => $this->fields["helpdesk_item_type"]]);

echo "</td>";
Expand Down Expand Up @@ -1629,7 +1625,7 @@ public function showFormTracking($openform = true, $closeform = true)

echo "<tr>";
echo "<td>" . __('Associable items to tickets, changes and problems') . "</td>";
echo "<td><input type='hidden' name='_helpdesk_item_types' value='1'>";
echo "<td>";
self::dropdownHelpdeskItemtypes(['values' => $this->fields["helpdesk_item_type"]]);
echo "</td>";
echo "</tr>";
Expand Down
84 changes: 84 additions & 0 deletions tests/cypress/e2e/general_config.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
describe('Notifications', () => {
beforeEach(() => {
cy.login();
cy.changeProfile('Super-Admin', true);
});

it('Change devices in menu', () => {
cy.visit('/front/config.form.php');
cy.get('#tabspanel .nav-item').contains('Assets').within((nav_link) => {
cy.wrap(nav_link).click();
});

cy.get('.tab-content .tab-pane.active[id^="tab"]').within(() => {
cy.get('label').contains('Devices displayed in menu').next().within(() => {
cy.get('select').select(['Simcard items', 'Case items'], {force: true});
});
cy.get('button').contains('Save').click();
});

cy.get('#navbar-menu').within(() => {
cy.get('.nav-item dropdown').contains('Assets').click();
cy.get('.dropdown-item').contains('Simcard items').should('exist');
cy.get('.dropdown-item').contains('Case items').should('exist');
});

cy.get('.tab-content .tab-pane.active[id^="tab"]').within(() => {
cy.get('label').contains('Devices displayed in menu').next().within(() => {
cy.get('select').select(['Case items'], {force: true});
});
cy.get('button').contains('Save').click();
});

cy.get('#navbar-menu').within(() => {
cy.get('.nav-item dropdown').contains('Assets').click();
cy.get('.dropdown-item').contains('Simcard items').should('not.exist');
cy.get('.dropdown-item').contains('Case items').should('exist');
});

cy.get('.tab-content .tab-pane.active[id^="tab"]').within(() => {
cy.get('label').contains('Devices displayed in menu').next().within(() => {
cy.get('select').select([], {force: true});
});
cy.get('button').contains('Save').click();
});

cy.get('#navbar-menu').within(() => {
cy.get('.nav-item dropdown').contains('Assets').click();
cy.get('.dropdown-item').contains('Simcard items').should('not.exist');
cy.get('.dropdown-item').contains('Case items').should('not.exist');
});
});
});
4 changes: 2 additions & 2 deletions tests/functional/Glpi/Asset/AssetDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ protected function testCommonITILTabRegistrationProvider(): iterable
$itemtypes = importArrayFromDB($profile->fields["helpdesk_item_type"]);
$itemtypes[] = $class;
$this->updateItem(Profile::class, $profile->getID(), [
'helpdesk_item_type' => exportArrayToDB($itemtypes),
]);
'helpdesk_item_type' => $itemtypes,
], ['helpdesk_item_type']);
yield [
$definition,
Expand Down

0 comments on commit 64f7c2a

Please sign in to comment.