From 303358a9dda62522eb257d0276ab41173e61c988 Mon Sep 17 00:00:00 2001 From: Antoine Lemarchand Date: Tue, 13 Feb 2024 14:53:40 +0100 Subject: [PATCH] feat(twig.utils.php): entity selector on form --- inc/cartridgeitem.class.php | 4 +- inc/commonglpi.class.php | 1 - inc/computer.class.php | 2 - inc/consumableitem.class.php | 4 +- inc/contract_item.class.php | 2 +- inc/enclosure.class.php | 2 - inc/group.class.php | 4 +- inc/monitor.class.php | 2 - inc/networkequipment.class.php | 2 - inc/pdu.class.php | 1 - inc/peripheral.class.php | 2 - inc/phone.class.php | 1 - inc/printer.class.php | 2 - inc/rack.class.php | 1 - inc/software.class.php | 4 +- src/twig/twig.utils.php | 93 ++++++++++++++++++---------------- templates/macros/input.twig | 10 +++- 17 files changed, 65 insertions(+), 72 deletions(-) diff --git a/inc/cartridgeitem.class.php b/inc/cartridgeitem.class.php index bca297161d..59949bb98a 100644 --- a/inc/cartridgeitem.class.php +++ b/inc/cartridgeitem.class.php @@ -273,8 +273,6 @@ function showForm($ID, $options = []) { ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, @@ -286,7 +284,7 @@ function showForm($ID, $options = []) { ]]); $additionnalHtml = ob_get_clean(); - renderTwigForm($form, $additionnalHtml); + renderTwigForm($form, $additionnalHtml, $this->fields); return true; } diff --git a/inc/commonglpi.class.php b/inc/commonglpi.class.php index 762cffbefd..98d71b4c5c 100644 --- a/inc/commonglpi.class.php +++ b/inc/commonglpi.class.php @@ -1248,7 +1248,6 @@ function display($options = []) { if (isset($_GET["id"]) && $_GET["id"] && !$this->can($_GET["id"], READ)) { // This triggers from a profile switch. // If we don't have right, redirect instead to central page - die(dump($this->fields)); if (isset($_SESSION['_redirected_from_profile_selector']) && $_SESSION['_redirected_from_profile_selector']) { unset($_SESSION['_redirected_from_profile_selector']); diff --git a/inc/computer.class.php b/inc/computer.class.php index 1e12421f76..3b8a917ecb 100644 --- a/inc/computer.class.php +++ b/inc/computer.class.php @@ -449,8 +449,6 @@ function showForm($ID, $options = []) ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, diff --git a/inc/consumableitem.class.php b/inc/consumableitem.class.php index 455649bbe8..567f75b730 100644 --- a/inc/consumableitem.class.php +++ b/inc/consumableitem.class.php @@ -213,8 +213,6 @@ function showForm($ID, $options = []) { ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, @@ -226,7 +224,7 @@ function showForm($ID, $options = []) { ]]); $additionnalHtml = ob_get_clean(); - renderTwigForm($form, $additionnalHtml); + renderTwigForm($form, $additionnalHtml, $this->fields); return true; } diff --git a/inc/contract_item.class.php b/inc/contract_item.class.php index d78a3a0a26..208a83b790 100644 --- a/inc/contract_item.class.php +++ b/inc/contract_item.class.php @@ -307,7 +307,7 @@ static function showForItem(CommonDBTM $item, $withtemplate = 0) { if (!count($used)) { $usedCondition = []; } else { - $usedCondition = ['NOT' => ['id' => $used]]; + $usedCondition = ['NOT' => [Contract::getTable() . '.id' => $used]]; }; $form = [ 'action' => Toolbox::getItemTypeFormURL(__CLASS__), diff --git a/inc/enclosure.class.php b/inc/enclosure.class.php index ee3eab2df9..2e2005e73a 100644 --- a/inc/enclosure.class.php +++ b/inc/enclosure.class.php @@ -182,8 +182,6 @@ function showForm($ID, $options = []) { ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, diff --git a/inc/group.class.php b/inc/group.class.php index 921ba7fea7..751483fb32 100644 --- a/inc/group.class.php +++ b/inc/group.class.php @@ -239,7 +239,7 @@ function showForm($ID, $options = []) { __('As child of') => [ 'type' => 'select', 'name' => 'groups_id', - 'values' => getOptionForItems('Group', ['NOT' => ['id' => $ID]]), + 'values' => getOptionForItems('Group', ['NOT' => [Group::getTable() . '.id' => $ID]]), 'value' => $this->fields['groups_id'], 'actions' => getItemActionButtons(['info', 'add'], 'Group'), 'col_lg' => 6, @@ -316,7 +316,7 @@ function showForm($ID, $options = []) { ] ]; - renderTwigForm($form); + renderTwigForm($form, '', $this->fields); return true; } diff --git a/inc/monitor.class.php b/inc/monitor.class.php index 6795c05129..4c626c443f 100644 --- a/inc/monitor.class.php +++ b/inc/monitor.class.php @@ -332,8 +332,6 @@ function showForm($ID, $options = []) { ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, diff --git a/inc/networkequipment.class.php b/inc/networkequipment.class.php index deecfa509c..62831865b4 100644 --- a/inc/networkequipment.class.php +++ b/inc/networkequipment.class.php @@ -407,8 +407,6 @@ function showForm($ID, $options = []) { ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, diff --git a/inc/pdu.class.php b/inc/pdu.class.php index 8a29632735..e7b4722075 100644 --- a/inc/pdu.class.php +++ b/inc/pdu.class.php @@ -181,7 +181,6 @@ function showForm($ID, $options = []) { ] ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ diff --git a/inc/peripheral.class.php b/inc/peripheral.class.php index b886c37af0..9310832046 100644 --- a/inc/peripheral.class.php +++ b/inc/peripheral.class.php @@ -297,8 +297,6 @@ function showForm($ID, $options = []) { ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, diff --git a/inc/phone.class.php b/inc/phone.class.php index a2e9b6fbd8..f1dbdef83e 100644 --- a/inc/phone.class.php +++ b/inc/phone.class.php @@ -319,7 +319,6 @@ function showForm($ID, $options = []) { ], ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ diff --git a/inc/printer.class.php b/inc/printer.class.php index 55098ad512..4d45c49c56 100644 --- a/inc/printer.class.php +++ b/inc/printer.class.php @@ -436,8 +436,6 @@ function showForm($ID, $options = []) { ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, diff --git a/inc/rack.class.php b/inc/rack.class.php index 96f14ee629..a146784191 100644 --- a/inc/rack.class.php +++ b/inc/rack.class.php @@ -326,7 +326,6 @@ function addPositionOpt(val, text) { ] ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ diff --git a/inc/software.class.php b/inc/software.class.php index 809fbf3399..ad94b69d36 100644 --- a/inc/software.class.php +++ b/inc/software.class.php @@ -332,15 +332,13 @@ function showForm($ID, $options = []) { 'name' => 'softwares_id', 'type' => 'select', 'value' => $this->fields['softwares_id'], - 'values' => getOptionForItems("Software", ['entities_id' => $this->fields['entities_id']]), + 'values' => getOptionForItems("Software"), ], ] ] ] ]; - $form['content']['form_inputs_config'] = ['inputs' => getHiddenInputsForItemForm($this, $this->fields)]; - ob_start(); Plugin::doHook("post_item_form", ['item' => $this, 'options' => [ 'colspan' => 2, diff --git a/src/twig/twig.utils.php b/src/twig/twig.utils.php index d3b5115e9b..c56a945c67 100644 --- a/src/twig/twig.utils.php +++ b/src/twig/twig.utils.php @@ -8,7 +8,7 @@ function getOptionForItems($item, $conditions = [], $display_emptychoice = true, global $DB; $table = getTableForItemType($item); - $name = $isDevice ? 'designation' : 'name'; +$name = $isDevice ? 'designation' : 'name'; $iterator = $DB->request([ 'SELECT' => ['id', $name ], 'FROM' => $table, @@ -18,16 +18,16 @@ function getOptionForItems($item, $conditions = [], $display_emptychoice = true, $options = []; while ($val = $iterator->next()) { - $options[$val['id']] = $val[$name] == '' ? '(' . $val['id'] . ')' : $val[$name]; - } - if ($display_emptychoice) { + $options[$val['id']] = $val[$name] == '' ? '(' . $val['id'] . ')' : $val[$name]; + } + if ($display_emptychoice) { if (!isset($options[0])) { $options = [0 => Dropdown::EMPTY_VALUE] + $options; - } else { - $options = [-1 => Dropdown::EMPTY_VALUE] + $options; + } else { + $options = [-1 => Dropdown::EMPTY_VALUE] + $options; } } - foreach ($used as $id) { +foreach ($used as $id) { unset($options[$id]); } @@ -106,6 +106,15 @@ function renderTwigForm($form, $additionnalHtml = '', $fields = []) global $CFG_GLPI; $twig = Twig::load(GLPI_ROOT . '/templates', false); + if (isset($fields['id']) && $fields['id'] > 0) { + $form['content'][array_key_first($form['content'])]['inputs'] = array_merge([ + [ + 'type' => 'hidden', + 'name' => 'id', + 'value' => $fields['id'], + ], + ], $form['content'][array_key_first($form['content'])]['inputs']); + } if (isset($_GET['withtemplate']) && $_GET['withtemplate'] == 1) { $form['content'][array_key_first($form['content'])]['inputs'] = array_merge([ [ @@ -120,6 +129,40 @@ function renderTwigForm($form, $additionnalHtml = '', $fields = []) ] ], $form['content'][array_key_first($form['content'])]['inputs']); }; + if (count($_SESSION['glpiactiveentities']) > 1) { + $form['content'] = [Entity::getTypeName() => [ + 'visible' => true, + 'inputs' => [ + __('Entity') => [ + 'type' => 'select', + 'name' => 'entities_id', + 'values' => getOptionForItems(Entity::class), + 'value' => $fields['entities_id'] ?? Session::getActiveEntity(), + 'col_lg' => 8, + 'col_md' => 8, + ], + __('Recursive') => [ + 'type' => 'checkbox', + 'name' => 'is_recursive', + 'value' => $fields['is_recursive'] ?? Session::getIsActiveEntityRecursive(), + ], + ], + ]] + $form['content']; + } else { + $form['content'][array_key_first($form['content'])]['inputs'] = array_merge([ + [ + 'type' => 'hidden', + 'name' => 'entities_id', + 'value' => Session::getActiveEntity(), + ], + [ + 'type' => 'hidden', + 'name' => 'is_recursive', + 'value' => Session::getIsActiveEntityRecursive(), + ], + ], $form['content'][array_key_first($form['content'])]['inputs']); + + } try { echo $twig->render('form.twig', [ 'form' => $form, @@ -132,42 +175,6 @@ function renderTwigForm($form, $additionnalHtml = '', $fields = []) } } -function getHiddenInputsForItemForm($item, $options) -{ - return [ - $options['id'] != '' ? [ - 'type' => 'hidden', - 'name' => 'entities_id', - 'value' => $item->fields['entities_id'], - ] : [], - $options['id'] != '' ? [ - 'type' => 'hidden', - 'name' => 'is_recursive', - 'value' => $item->fields['is_recursive'], - ] : [], - [ - 'type' => 'hidden', - 'name' => isset($options['id']) && $options['id'] != '' ? 'update' : 'add', - 'value' => '', - ], - [ - 'type' => 'hidden', - 'name' => 'id', - 'value' => isset($options['id']) ? $options['id'] : 0, - ], - [ - 'type' => 'hidden', - 'name' => '_glpi_csrf_token', - 'value' => Session::getNewCSRFToken(), - ], - [ - 'type' => 'hidden', - 'name' => '_read_date_mod', - 'value' => (new DateTime)->format('Y-m-d H:i:s'), - ], - ]; -} - function getItemActionButtons(array $actions, string $itemType): array { $buttons = []; diff --git a/templates/macros/input.twig b/templates/macros/input.twig index 9482bb2ec3..024ec2f072 100644 --- a/templates/macros/input.twig +++ b/templates/macros/input.twig @@ -158,7 +158,15 @@ {% if attributes.searchable %}