From 8a72a0f343102d1c54e34e958ed913bc4d9a3c61 Mon Sep 17 00:00:00 2001 From: Lukas Scharmer Date: Mon, 6 Jan 2025 16:47:02 +0100 Subject: [PATCH] LegalDocuments: Fix duplicated onscreen message --- ...lass.ilLegalDocumentsAdministrationGUI.php | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/components/ILIAS/LegalDocuments/classes/class.ilLegalDocumentsAdministrationGUI.php b/components/ILIAS/LegalDocuments/classes/class.ilLegalDocumentsAdministrationGUI.php index 3bea9a6b3816..3346c0346d14 100755 --- a/components/ILIAS/LegalDocuments/classes/class.ilLegalDocumentsAdministrationGUI.php +++ b/components/ILIAS/LegalDocuments/classes/class.ilLegalDocumentsAdministrationGUI.php @@ -134,11 +134,9 @@ public function addCriterion(): void $form = $this->admin->withFormData($form, function (array $x) use ($document) { $content = new CriterionContent(...$x[0]['content']); - $this->config->legalDocuments()->document()->validateCriteriaContent($document->criteria(), $content)->map( + $this->returnWithResult($this->config->legalDocuments()->document()->validateCriteriaContent($document->criteria(), $content)->map( fn() => $this->config->legalDocuments()->document()->repository()->createCriterion($document, $content) - )->except($this->criterionInvalid(...))->value(); - - $this->returnWithMessage('doc_crit_attached', 'documents'); + ), 'doc_crit_attached', 'documents'); }); $this->admin->setContent($form); @@ -154,11 +152,9 @@ public function editCriterion(): void $form = $this->admin->withFormData($form, function (array $data) use ($document, $criterion) { $content = new CriterionContent(...$data[0]['content']); $criteria = array_filter($document->criteria(), fn(Criterion $other) => $other->id() !== $criterion->id()); - $this->config->legalDocuments()->document()->validateCriteriaContent($criteria, $content)->map( + $this->returnWithResult($this->config->legalDocuments()->document()->validateCriteriaContent($criteria, $content)->map( fn() => $this->config->legalDocuments()->document()->repository()->updateCriterionContent($criterion->id(), $content) - )->except($this->criterionInvalid(...))->value(); - - $this->returnWithMessage('doc_crit_changed', 'documents'); + ), 'doc_crit_changed', 'documents'); }); $this->container->tabs()->clearTargets(); @@ -310,19 +306,17 @@ private function deleteDocumentsConfirmation(array $documents): void /** * @param string|Exception $error */ - private function criterionInvalid($error): Result + private function criterionErrorMessage($error): string { if (!is_string($error)) { - return new Error($error); + throw $error; } - $message = match ($error) { + return match ($error) { ProvideDocument::CRITERION_ALREADY_EXISTS => $this->ui->txt('criterion_assignment_must_be_unique'), ProvideDocument::CRITERION_WOULD_NEVER_MATCH => $this->ui->txt('criterion_assignment_cannot_match'), default => $error, }; - - return new Ok($this->ui->mainTemplate()->setOnScreenMessage('failure', $message, true)); } private function returnWithMessage(string $message, string $command): void @@ -330,4 +324,15 @@ private function returnWithMessage(string $message, string $command): void $this->ui->mainTemplate()->setOnScreenMessage('success', $this->ui->txt($message), true); $this->ctrlTo('redirectByClass', $command); } + + private function returnWithResult(Result $result, string $success_message, string $target): void + { + if ($result->isOk()) { + $this->ui->mainTemplate()->setOnScreenMessage('success', $this->ui->txt($success_message), true); + } else { + $this->ui->mainTemplate()->setOnScreenMessage('failure', $this->criterionErrorMessage($result->error()), true); + } + + $this->ctrlTo('redirectByClass', $target); + } }