From 2f8f98642bfb14f27dc3f323f849b0cdb2fd16d1 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 2 Oct 2023 14:02:00 +0900 Subject: [PATCH] Update Logging\ErrorMsg to add a Jump Target list for direct jumps So we can return a list for css element ids we want to jump directly to --- .../CoreLibsLoggingErrorMessagesTest.php | 64 +++++++++++++++++++ www/admin/class_test.error_msg.php | 7 ++ www/lib/CoreLibs/Logging/ErrorMessage.php | 50 +++++++++++++++ 3 files changed, 121 insertions(+) diff --git a/4dev/tests/Logging/CoreLibsLoggingErrorMessagesTest.php b/4dev/tests/Logging/CoreLibsLoggingErrorMessagesTest.php index 3393415d..8f543cb9 100644 --- a/4dev/tests/Logging/CoreLibsLoggingErrorMessagesTest.php +++ b/4dev/tests/Logging/CoreLibsLoggingErrorMessagesTest.php @@ -421,6 +421,70 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase ); } } + + /** + * Undocumented function + * + * @testdox Test jump target set and reporting + * + * @return void + */ + public function testJumpTarget(): void + { + $log = new \CoreLibs\Logging\Logging([ + 'log_file_id' => 'testErrorMessagesLogDebug', + 'log_folder' => self::LOG_FOLDER, + 'log_level' => Level::Debug, + 'log_per_run' => true + ]); + $em = new \CoreLibs\Logging\ErrorMessage($log); + $em->setJumpTarget( + 'target-f', + 'Target text' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text' + ], + $em->getJumpTarget() + ); + // set same target, keep as before + $em->setJumpTarget( + 'target-f', + 'Other text' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text' + ], + $em->getJumpTarget() + ); + // add new now two messages + $em->setJumpTarget( + 'target-s', + 'More text' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text', + 'target-s' => 'More text' + ], + $em->getJumpTarget() + ); + // add empty info + $em->setJumpTarget( + 'target-e', + '' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text', + 'target-s' => 'More text', + 'target-e' => 'Jump to: target-e' + ], + $em->getJumpTarget() + ); + } } // __END__ diff --git a/www/admin/class_test.error_msg.php b/www/admin/class_test.error_msg.php index 03d8e5cf..e2aa5901 100644 --- a/www/admin/class_test.error_msg.php +++ b/www/admin/class_test.error_msg.php @@ -48,9 +48,16 @@ $em->setErrorMsg('123', 'error', 'msg this is bad, never logged', log_error:fals $em->setErrorMsg('1000', 'info', 'This is good'); $em->setErrorMsg('9999', 'abort', 'BAD: This is critical (abort)'); $em->setErrorMsg('10-1000', 'wrong', 'Wrong level: This is emergency'); +// set some jump targets too +$em->setErrorMsg('100-1', 'error', 'Input wring', jump_target:['target' => 'foo-123', 'info' => 'Jump Target 123']); +$em->setErrorMsg('100-2', 'error', 'Input wring', jump_target:['target' => 'foo-123', 'info' => 'Jump Target 456']); +$em->setMessage('error', 'I have no id set', jump_target:['target' => 'bar-123', 'info' => 'Jump Bar']); +$em->setMessage('error', 'Jump empty', jump_target:['target' => 'bar-empty']); + print "ErrorsLast:
" . $log->prAr($em->getLastErrorMsg()) . "
"; print "ErrorsIds:
" . $log->prAr($em->getErrorIds()) . "
"; print "Errors:
" . $log->prAr($em->getErrorMsg()) . "
"; +print "JumpTargets:
" . $log->prAr($em->getJumpTarget()) . "
"; print ""; diff --git a/www/lib/CoreLibs/Logging/ErrorMessage.php b/www/lib/CoreLibs/Logging/ErrorMessage.php index acd6cf8e..332cd01b 100644 --- a/www/lib/CoreLibs/Logging/ErrorMessage.php +++ b/www/lib/CoreLibs/Logging/ErrorMessage.php @@ -17,6 +17,8 @@ class ErrorMessage { /** @var array */ private array $error_str = []; + /** @var array */ + private array $jump_targets; /** @var \CoreLibs\Logging\Logging $log */ public \CoreLibs\Logging\Logging $log; @@ -61,6 +63,8 @@ class ErrorMessage * highlight is a list of other target points to highlight * for highlight targets css names are $level without a prefix and should be * nested in the target element "input .error { ... }" + * jump_target: a target id for to jump and message, is stored in separate jump array + * where the target is unique, first one set is used for info message * target_style: if not set uses 'error-' $level as css style. applies to targets or main only * * @param string $error_id Any internal error ID for this error @@ -70,6 +74,9 @@ class ErrorMessage * @param string $target_style Alternate color style for the error message * @param array $highlight Any additional error data as error OR * highlight points for field highlights + * @param array{}|array{target:string,info?:string} $jump_target with "target" for where to jump and + * "info" for string to show in jump list + * target must be set, if info not set, default message used * @param string|null $message If abort/crash, non localized $str * @param array $context Additionl info for abort/crash messages * @param bool|null $log_error [=null] log level 'error' to error, if null use global, @@ -82,6 +89,7 @@ class ErrorMessage string $target = '', string $target_style = '', array $highlight = [], + array $jump_target = [], ?string $message = null, array $context = [], ?bool $log_error = null, @@ -103,6 +111,8 @@ class ErrorMessage 'target_style' => $target_style, 'highlight' => $highlight, ]; + // set a jump target + $this->setJumpTarget($jump_target['target'] ?? null, $jump_target['info'] ?? null); // write to log for abort/crash switch ($level) { case 'notice': @@ -152,6 +162,9 @@ class ErrorMessage * @param string $target_style Alternate color style for the error message * @param array $highlight Any additional error data as error OR * highlight points for field highlights + * @param array{}|array{target:string,info?:string} $jump_target with "target" for where to jump and + * "info" for string to show in jump list + * target must be set, if info not set, default message used * @param string|null $message If abort/crash, non localized $str * @param array $context Additionl info for abort/crash messages * @param bool|null $log_error [=null] log level 'error' to error, if null use global, @@ -164,6 +177,7 @@ class ErrorMessage string $target = '', string $target_style = '', array $highlight = [], + array $jump_target = [], ?string $message = null, array $context = [], ?bool $log_error = null, @@ -175,12 +189,38 @@ class ErrorMessage $target, $target_style, $highlight, + $jump_target, $message, $context, $log_error ); } + /** + * Set a jump target. This can be used to jump directly a frontend html block + * with the target id set + * + * @param string|null $target + * @param string|null $info + * @return void + */ + public function setJumpTarget( + ?string $target, + ?string $info, + ): void { + if ( + empty($target) || + !empty($this->jump_targets[$target]) + // also check if this is an alphanumeric string? css id compatible? + ) { + return; + } + if (empty($info)) { + $info = 'Jump to: ' . $target; + } + $this->jump_targets[$target] = $info; + } + // ********************************************************************* // GETTERS // ********************************************************************* @@ -223,6 +263,16 @@ class ErrorMessage ]; } + /** + * Return the jump target list + * + * @return array{}|array{string,string} List of jump targets with info text, or empty array if not set + */ + public function getJumpTarget(): array + { + return $this->jump_targets ?? []; + } + // ********************************************************************* // FLAG SETTERS // *********************************************************************