Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing self service ticket form defaults #12473

Merged
merged 4 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/CommonITILObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,20 @@ public function getActorsForType(int $actortype = 1, array $params = []): array
return $actors;
}


/**
* @param $ID
* @param $options array
**/
public function showForm($ID, array $options = [])
* Restores input, restores saved values, then sets the default options for any that are missing.
* @param integer $ID The item ID
* @param array $options ITIL Object options array passed to showFormXXXX functions. This is passed by reference and will be modified by this function.
* @param ?array $overriden_defaults If specified, these values will be used as the defaults instead of the ones from the {@link getDefaultValues()} function.
* @param bool $force_set_defaults If true, the defaults are set for missing options even if the item is not new.
* @return void
* @see getDefaultOptions()
* @see restoreInput()
* @see restoreSavedValues()
*/
protected function restoreInputAndDefaults($ID, array &$options, ?array $overriden_defaults = null, bool $force_set_defaults = false): void
{
if (!static::canView()) {
return false;
}

$default_values = static::getDefaultValues();
$default_values = $overriden_defaults ?? static::getDefaultValues();

// Restore saved value or override with page parameter
$options['_saved'] = $this->restoreInput();
Expand All @@ -370,7 +372,7 @@ public function showForm($ID, array $options = [])
$this->restoreSavedValues($options['_saved']);

// Set default options
if (!$ID) {
if ($force_set_defaults || static::isNewID($ID)) {
foreach ($default_values as $key => $val) {
if (!isset($options[$key])) {
if (isset($options['_saved'][$key])) {
Expand All @@ -381,6 +383,19 @@ public function showForm($ID, array $options = [])
}
}
}
}

/**
* @param $ID
* @param $options array
**/
public function showForm($ID, array $options = [])
{
if (!static::canView()) {
return false;
}

$this->restoreInputAndDefaults($ID, $options);

$this->initForm($ID, $options);

Expand Down Expand Up @@ -414,7 +429,7 @@ public function showForm($ID, array $options = [])
($ID ? $this->fields['entities_id'] : $options['entities_id'])
);

$predefined_fields = $this->setPredefinedFields($tt, $options, $default_values);
$predefined_fields = $this->setPredefinedFields($tt, $options, self::getDefaultValues());

TemplateRenderer::getInstance()->display('components/itilobject/layout.html.twig', [
'item' => $this,
Expand Down
27 changes: 4 additions & 23 deletions src/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -3898,11 +3898,7 @@ public function showFormHelpdesk($ID, $ticket_template = false)
$options['name'] = str_replace($order, $replace, $options['name']);
}

// Restore saved value or override with page parameter
$options['_saved'] = $this->restoreInput();

// Restore saved values and override $this->fields
$this->restoreSavedValues($options['_saved']);
$this->restoreInputAndDefaults($ID, $options, $default_values, true);

// Check category / type validity
if ($options['itilcategories_id']) {
Expand Down Expand Up @@ -3952,6 +3948,7 @@ public function showFormHelpdesk($ID, $ticket_template = false)
'selfservice' => true,
'item' => $this,
'params' => $options,
'entities_id' => $options['entities_id'],
'itiltemplate_key' => self::getTemplateFormFieldName(),
'itiltemplate' => $tt,
'delegating' => $delegating,
Expand Down Expand Up @@ -4142,23 +4139,7 @@ public function showForm($ID, array $options = [])
$options['entities_id'] = $item->fields['entities_id'];
}

$default_values = self::getDefaultValues();

// Restore saved value or override with page parameter
$options['_saved'] = $this->restoreInput();

// Restore saved values and override $this->fields
$this->restoreSavedValues($options['_saved']);

foreach ($default_values as $name => $value) {
if (!isset($options[$name])) {
if (isset($options['_saved'][$name])) {
$options[$name] = $options['_saved'][$name];
} else {
$options[$name] = $value;
}
}
}
$this->restoreInputAndDefaults($ID, $options, null, true);

if (isset($options['content'])) {
$order = ["\\'", '\\"', "\\\\"];
Expand Down Expand Up @@ -4303,7 +4284,7 @@ public function showForm($ID, array $options = [])
);

// override current fields in options with template fields and return the array of these predefined fields
$predefined_fields = $this->setPredefinedFields($tt, $options, $default_values);
$predefined_fields = $this->setPredefinedFields($tt, $options, self::getDefaultValues());

// check right used for this ticket
$canupdate = !$ID
Expand Down
4 changes: 2 additions & 2 deletions templates/components/itilobject/selfservice.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@

{{ include('components/itilobject/actors/main.html.twig', {
'item': item,
'entities_id': params['entities_id'],
'entities_id': entities_id,
cedric-anne marked this conversation as resolved.
Show resolved Hide resolved
'itiltemplate': itiltemplate,
'field_options': right_field_options,
'canupdate': true,
Expand Down Expand Up @@ -247,7 +247,7 @@
{{ call_plugin_hook(constant('Glpi\\Plugin\\Hooks::POST_ITEM_FORM'), {'item': item, 'options': params}) }}

<div class="card-footer text-center">
<input type="hidden" name="entities_id" value="{{ params['entities_id'] }}" />
<input type="hidden" name="entities_id" value="{{ entities_id }}" />
<button type="submit" class="btn btn-primary" name="add">
<i class="fas fa-plus"></i>
<span>{{ __('Submit message') }}</span>
Expand Down