From d6d0d2190a88afe8539b48ecc975c8f36db32e22 Mon Sep 17 00:00:00 2001 From: Bastian Lederer Date: Thu, 23 Apr 2026 09:32:57 +0200 Subject: [PATCH] Add `TimestampToggle` control Add a control to allow users to switch between absolute and relatvie timestamps. --- .../Icingadb/Web/Control/TimestampToggle.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 library/Icingadb/Web/Control/TimestampToggle.php diff --git a/library/Icingadb/Web/Control/TimestampToggle.php b/library/Icingadb/Web/Control/TimestampToggle.php new file mode 100644 index 00000000..72d5b6b8 --- /dev/null +++ b/library/Icingadb/Web/Control/TimestampToggle.php @@ -0,0 +1,93 @@ + +// SPDX-License-Identifier: GPL-3.0-or-later + +namespace Icinga\Module\Icingadb\Web\Control; + +use ipl\Html\Form; +use ipl\I18n\Translation; +use ipl\Web\Common\FormUid; + +class TimestampToggle extends Form +{ + use FormUid; + use Translation; + + /** @var string Default timestamp mode param */ + public const DEFAULT_TIMESTAMP_MODE_PARAM = 'timestamps'; + + /** @var bool Whether relative or absolute timestamps are to be used */ + protected bool $enabled; + + protected $defaultAttributes = [ + 'name' => 'timestamp-toggle', + 'class' => ['icinga-form', 'icinga-controls', 'inline'] + ]; + + /** + * Create a toggle to switch between absolute and relative timestamps + * + * When enabled, timestamps are displayed as relative, absolute otherwise + * + * @param bool $enabled True to use relative timestamps, false for absolute + */ + public function __construct(bool $enabled = false) + { + $this->enabled = $enabled; + $this->addElementDecoratorLoaderPaths([['ipl\\Web\\Compat\\FormDecorator', 'Decorator']]); + } + + /** + * Get the current value of the timestamp-toggle element + * + * @return string `absolute` or `relative` + */ + public function getToggleValue(): string + { + return $this->getValue('timestamp-toggle'); + } + + /** + * Get whether the toggle is enabled + * + * When enabled, timestamps are displayed as relative, absolute otherwise + * + * @return bool + */ + public function isEnabled(): bool + { + return $this->getElement('timestamp-toggle')->isChecked(); + } + + protected function assemble(): void + { + $this->addElement('checkbox', 'timestamp-toggle', [ + 'class' => ['icingadb-timestamp-toggle', 'autosubmit'], + 'label' => $this->translate('Use relative timestamps'), + 'value' => $this->enabled, + 'checkedValue' => 'relative', + 'uncheckedValue' => 'absolute', + 'decorators' => [ + 'Label', + 'LabelGroup' => [ + 'name' => 'HtmlTag', + 'options' => [ + 'tag' => 'div', + 'class' => 'control-label-group', + ] + ], + 'Checkbox', + 'RenderElement', + 'ControlGroup' => [ + 'name' => 'HtmlTag', + 'options' => [ + 'tag' => 'div', + 'class' => 'control-group' + ] + ] + ] + ]); + $this->addElement($this->createUidElement()); + } +}