Skip to content

Commit

Permalink
Fix missing self service ticket form defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Sep 2, 2022
1 parent 9a7113c commit 6f6e885
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
39 changes: 27 additions & 12 deletions src/CommonITILObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,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 @@ -372,7 +374,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 @@ -383,6 +385,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 @@ -416,7 +431,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 @@ -3895,11 +3895,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 @@ -3949,6 +3945,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 @@ -4139,23 +4136,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 @@ -4300,7 +4281,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,
'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

0 comments on commit 6f6e885

Please sign in to comment.