Skip to content

Commit

Permalink
feat(dropdownfield,glpiselectfield): choose if subtree root is select…
Browse files Browse the repository at this point in the history
…able

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Jun 11, 2021
1 parent 6773e29 commit 164e752
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ajax/commontree.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
}

// Load parameters
$itemtype = $_GET['itemtype'];
$root = $_GET['root'];
$depth = $_GET['maxDepth'];
$itemtype = $_GET['itemtype'];
$root = $_GET['root'];
$depth = $_GET['maxDepth'];
$selectableRoot = $_GET['selectableRoot'];

// This should only be used for dropdowns
if (!is_a($itemtype, CommonTreeDropdown::class, true)) {
Expand All @@ -53,6 +54,10 @@
$additions .= '<label for="dropdown_show_tree_root'.$rand.'" id="label_show_tree_root">';
$additions .= __('Subtree root', 'formcreator');
$additions .= '</label>';
$additions .= '<br>';
$additions .= '<label for="dropdown_selectable_tree_root'.$rand.'" id="label_selectable_tree_root">';
$additions .= __('Selectable', 'formcreator');
$additions .= '</label>';
$additions .= '</td>';
$additions .= '<td>';
$additions .= Dropdown::show($itemtype, [
Expand All @@ -61,6 +66,8 @@
'rand' => $rand,
'display' => false,
]);
$additions .= '<br>';
$additions .= Dropdown::showYesNo('selectable_tree_root', $selectableRoot, -1, ['display' => false]);
$additions .= '</td>';
$additions .= '<td>';
$additions .= '<label for="dropdown_show_tree_depth'.$rand.'" id="label_show_tree_depth">';
Expand Down
9 changes: 9 additions & 0 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function getDesignSpecializationField(): array {

$root = $decodedValues['show_tree_root'] ?? Dropdown::EMPTY_VALUE;
$maxDepth = $decodedValues['show_tree_depth'] ?? Dropdown::EMPTY_VALUE;
$selectableRoot = $decodedValues['selectable_tree_root'] ?? '0';

$optgroup = Dropdown::getStandardDropdownItemTypes();

Expand Down Expand Up @@ -136,6 +137,7 @@ public function getDesignSpecializationField(): array {
$additions .= '<td>';
$additions .= "<input id='commonTreeDropdownRoot' type='hidden' value='$root'>";
$additions .= "<input id='commonTreeDropdownMaxDepth' type='hidden' value='$maxDepth'>";
$additions .= "<input id='commonTreeDropdownSelectableRoot' type='hidden' value='$selectableRoot'>";
$additions .= '</td>';
$additions .= '<td>';
$additions .= '</td>';
Expand Down Expand Up @@ -308,11 +310,16 @@ public function buildParams($rand = null) {
$itemtype::getTable(),
$decodedValues['show_ticket_categories_root']
);
if (!isset($decodedValues['selectable_tree_root']) || $decodedValues['selectable_tree_root'] == '0') {
unset($sons[$decodedValues['show_tree_root']]);
}

$dparams_cond_crit[$itemtype::getTable() . '.id'] = $sons;
$rootItem = new $itemtype();
if ($rootItem->getFromDB($decodedValues['show_ticket_categories_root'])) {
$baseLevel = $rootItem->fields['level'];
}

}

// Apply max depth if defined (CommonTreeDropdown)
Expand Down Expand Up @@ -518,6 +525,7 @@ public function prepareQuestionInputForSave($input) {
// Set default for depth setting
$input['values']['show_tree_depth'] = (string) (int) ($input['show_tree_depth'] ?? '-1');
$input['values']['show_tree_root'] = ($input['show_tree_root'] ?? '');
$input['values']['selectable_tree_root'] = ($input['selectable_tree_root'] ?? '0');
} else if ($input['dropdown_values'] == SLA::getType()
|| $input['dropdown_values'] == OLA::getType()
) {
Expand All @@ -532,6 +540,7 @@ public function prepareQuestionInputForSave($input) {
unset($input['show_ticket_categories']);
unset($input['show_tree_depth']);
unset($input['show_tree_root']);
unset($input['selectable_tree_root']);
unset($input['dropdown_values']);

return $input;
Expand Down
4 changes: 4 additions & 0 deletions inc/field/glpiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function getDesignSpecializationField(): array {

$root = $decodedValues['show_tree_root'] ?? '0';
$maxDepth = $decodedValues['show_tree_depth'] ?? Dropdown::EMPTY_VALUE;
$selectableRoot = $decodedValues['selectable_tree_root'] ?? '0';

$additions = '<tr class="plugin_formcreator_question_specific">';
$additions .= '<td>';
Expand All @@ -109,6 +110,7 @@ public function getDesignSpecializationField(): array {
$additions .= '<td>';
$additions .= "<input id='commonTreeDropdownRoot' type='hidden' value='$root'>";
$additions .= "<input id='commonTreeDropdownMaxDepth' type='hidden' value='$maxDepth'>";
$additions .= "<input id='commonTreeDropdownSelectableRoot' type='hidden' value='$selectableRoot'>";
$additions .= '</td>';
$additions .= '<td>';
$additions .= '</td>';
Expand Down Expand Up @@ -162,13 +164,15 @@ public function prepareQuestionInputForSave($input) {
// Set default for depth setting
$input['values']['show_tree_depth'] = (int) ($input['show_tree_depth'] ?? '-1');
$input['values']['show_tree_root'] = ($input['show_tree_root'] ?? '');
$input['values']['selectable_tree_root'] = ($input['selectable_tree_root'] ?? '0');
}

$input['values'] = json_encode($input['values']);

unset($input['dropdown_default_value']);
unset($input['show_tree_root']);
unset($input['show_tree_depth']);
unset($input['selectable_tree_root']);

return $input;
}
Expand Down
2 changes: 2 additions & 0 deletions js/scripts.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ function plugin_formcreator_changeDropdownItemtype(rand) {
itemtype: dropdown_type,
root: $("#commonTreeDropdownRoot").val(),
maxDepth: $("#commonTreeDropdownMaxDepth").val(),
selectableRoot: $("#commonTreeDropdownSelectableRoot").val(),
},
}).done(function(response) {
$('.plugin_formcreator_dropdown').html(response);
Expand Down Expand Up @@ -1500,6 +1501,7 @@ function plugin_formcreator_changeGlpiObjectItemType() {
itemtype: glpi_object,
root: $("#commonTreeDropdownRoot").val(),
maxDepth: $("#commonTreeDropdownMaxDepth").val(),
selectableRoot: $("#commonTreeDropdownSelectableRoot").val(),
},
}).done(function(response) {
$('.plugin_formcreator_dropdown').html(response);
Expand Down

0 comments on commit 164e752

Please sign in to comment.