Skip to content

Commit

Permalink
fix enrichment
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Apr 27, 2023
1 parent 5039e68 commit 142a3bd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
45 changes: 35 additions & 10 deletions src/FormElement/TermInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Web\FormElement;

use ipl\Html\Attributes;
use ipl\Html\Form;
use ipl\Html\FormElement\FieldsetElement;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
Expand Down Expand Up @@ -41,6 +42,9 @@ class TermInput extends FieldsetElement
/** @var Term[] */
protected $terms = [];

/** @var bool */
protected $hasBeenAutoSubmitted = false;

/**
* Set the suggestion url
*
Expand Down Expand Up @@ -101,12 +105,14 @@ public function getValue($name = null, $default = null)

public function setValue($value)
{
$recipients = $value;
if (is_array($value)) {
return parent::setValue($value);
$recipients = $value['value'] ?? '';
parent::setValue($value);
}

$terms = [];
foreach ($this->parseValue($value) as $term) {
foreach ($this->parseValue($recipients) as $term) {
$terms[] = new Term($term);
}

Expand Down Expand Up @@ -142,14 +148,31 @@ protected function parseValue(string $value): array

public function prepareMultipartUpdate(RequestInterface $request): array
{
$mainInputId = $this->getElement('value')->getAttributes()->get('id')->getValue();
if (! empty($this->changes) || in_array($mainInputId, $request->getHeader('X-Icinga-Autosubmittedby'), true)) {
return [[HtmlString::create(json_encode(['#' . $mainInputId, $this->changes])), 'Behavior:InputEnrichment']];
if (! empty($this->changes) || $this->hasBeenAutoSubmitted()) {
return [[
HtmlString::create(json_encode(['#' . $this->getName() . '-search-input', $this->changes])),
'Behavior:InputEnrichment'
]];
}

return [];
}

private function hasBeenAutoSubmitted(): bool
{
return $this->hasBeenAutoSubmitted;
}

public function onRegistered(Form $form)
{
$termContainerId = $this->getName() . '-terms';
$mainInputId = $this->getName() . '-search-input';
$autoSubmittedBy = $form->getRequest()->getHeader('X-Icinga-Autosubmittedby');

$this->hasBeenAutoSubmitted = in_array($mainInputId, $autoSubmittedBy, true)
|| in_array($termContainerId, $autoSubmittedBy, true);
}

private function validateTerms(string $type, array $terms, array &$changes): bool
{
$validatedTerms = [];
Expand Down Expand Up @@ -225,9 +248,11 @@ protected function assemble()

$changes = [];
$invalid = $this->validateTerms($data['type'], $data['terms'] ?? [], $changes);
if (! empty($changes)) {
$this->changes = $changes;
$termContainer->applyChanges($changes);
$this->changes = $changes;

$terms = $this->getTerms();
foreach ($this->changes as $index => $change) {
$terms[$index]->applyChanges($change);
}

return ! $invalid;
Expand All @@ -252,7 +277,7 @@ protected function assemble()
'data-term-container' => '#' . $termContainerId,
'data-term-suggestions' => '#' . $suggestionsId,
'validators' => ['callback' => function ($value) use (&$mainInput) {
$this->setValue($value);
//$this->setValue($value);
$mainInput->getAttributes()
->registerAttributeCallback('value', function () {
return '';
Expand Down Expand Up @@ -280,7 +305,7 @@ protected function assemble()

$this->addHtml($suggestions);

if (! $dataInput->hasValue()) {
if (! $this->hasBeenAutoSubmitted()) {
$this->emit(self::ON_ENRICH, [$this->getTerms()]);
}
}
Expand Down
21 changes: 20 additions & 1 deletion src/FormElement/TermInput/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getPattern(): ?string
return null;
}

return $this->pattern ?? ValidatedTerm::DEFAULT_PATTERN;
return $this->pattern;
}

public function render(string $separator): string
Expand All @@ -89,4 +89,23 @@ public function render(string $separator): string

return $this->value;
}

public function applyChanges(array $changes): void
{
if (isset($changes['search'])) {
$this->value = $changes['search'];
}

if (isset($changes['label'])) {
$this->setLabel($changes['label']);
}

if (isset($changes['invalidMsg'])) {
$this->setMessage($changes['invalidMsg']);
}

if (isset($changes['pattern'])) {
$this->setPattern($changes['pattern']);
}
}
}
14 changes: 3 additions & 11 deletions src/FormElement/TermInput/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,11 @@ class Terms extends BaseHtmlElement
/** @var string[] */
protected $terms = [];

/** @var array */
protected $changes = [];

public function __construct(TermInput $input)
{
$this->input = $input;
}

public function applyChanges(array $changes): self
{
$this->changes = $changes;

return $this;
}

protected function assemble()
{
foreach ($this->input->getTerms() as $i => $term) {
Expand All @@ -51,7 +41,9 @@ protected function assemble()
'input',
Attributes::create([
'type' => 'text',
'value' => $label
'value' => $label,
'pattern' => $term->getPattern(),
'data-invalid-msg' => $term->getMessage()
])
)
));
Expand Down

0 comments on commit 142a3bd

Please sign in to comment.