Skip to content

Commit

Permalink
feat(twig.utils.php): entity selector on form
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLemarchand committed Feb 13, 2024
1 parent 209a3b9 commit 303358a
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 72 deletions.
4 changes: 1 addition & 3 deletions inc/cartridgeitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -286,7 +284,7 @@ function showForm($ID, $options = []) {
]]);
$additionnalHtml = ob_get_clean();

renderTwigForm($form, $additionnalHtml);
renderTwigForm($form, $additionnalHtml, $this->fields);
return true;
}

Expand Down
1 change: 0 additions & 1 deletion inc/commonglpi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
2 changes: 0 additions & 2 deletions inc/computer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions inc/consumableitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -226,7 +224,7 @@ function showForm($ID, $options = []) {
]]);
$additionnalHtml = ob_get_clean();

renderTwigForm($form, $additionnalHtml);
renderTwigForm($form, $additionnalHtml, $this->fields);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion inc/contract_item.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__),
Expand Down
2 changes: 0 additions & 2 deletions inc/enclosure.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions inc/group.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -316,7 +316,7 @@ function showForm($ID, $options = []) {
]

];
renderTwigForm($form);
renderTwigForm($form, '', $this->fields);
return true;
}

Expand Down
2 changes: 0 additions & 2 deletions inc/monitor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions inc/networkequipment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion inc/pdu.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
2 changes: 0 additions & 2 deletions inc/peripheral.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion inc/phone.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
2 changes: 0 additions & 2 deletions inc/printer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion inc/rack.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
4 changes: 1 addition & 3 deletions inc/software.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
93 changes: 50 additions & 43 deletions src/twig/twig.utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]);
}

Expand Down Expand Up @@ -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([
[
Expand All @@ -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,
Expand All @@ -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 = [];
Expand Down
10 changes: 9 additions & 1 deletion templates/macros/input.twig
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@
<select class='form-select {{attributes.class}}'
{% for name, value in attributes %} {% if name not in ['values', 'class']%}{{name}}="{{value}}"{% endif %} {% endfor %}>
{% for key, item in attributes.values %}
<option value="{{key}}" {% if attributes.value == key %}selected="selected"{% endif %}>{{item}}</option>
{% if item is iterable %}
<optgroup label="{{key}}">
{% for key, item in item %}
<option value="{{key}}" {% if attributes.value == key %}selected="selected"{% endif %}>{{item}}</option>
{% endfor %}
</optgroup>
{% else %}
<option value="{{key}}" {% if attributes.value == key %}selected="selected"{% endif %}>{{item}}</option>
{% endif %}
{% endfor %}
</select>
{% if attributes.searchable %}
Expand Down

0 comments on commit 303358a

Please sign in to comment.