diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:40 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:40 +0000 |
commit | a0901c4b7f2db488cb4fb3be2dd921a0308f4659 (patch) | |
tree | fafb393cf330a60df129ff10d0059eb7b14052a7 /library/Icingadb/Web/Control/ViewModeSwitcher.php | |
parent | Initial commit. (diff) | |
download | icingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.tar.xz icingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.zip |
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Icingadb/Web/Control/ViewModeSwitcher.php')
-rw-r--r-- | library/Icingadb/Web/Control/ViewModeSwitcher.php | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/library/Icingadb/Web/Control/ViewModeSwitcher.php b/library/Icingadb/Web/Control/ViewModeSwitcher.php new file mode 100644 index 0000000..669bc32 --- /dev/null +++ b/library/Icingadb/Web/Control/ViewModeSwitcher.php @@ -0,0 +1,203 @@ +<?php + +/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */ + +namespace Icinga\Module\Icingadb\Web\Control; + +use ipl\Html\Attributes; +use ipl\Html\Form; +use ipl\Html\FormElement\HiddenElement; +use ipl\Html\FormElement\InputElement; +use ipl\Html\HtmlElement; +use ipl\Web\Common\FormUid; +use ipl\Web\Widget\IcingaIcon; + +class ViewModeSwitcher extends Form +{ + use FormUid; + + protected $defaultAttributes = [ + 'class' => 'view-mode-switcher', + 'name' => 'view-mode-switcher' + ]; + + /** @var string Default view mode */ + const DEFAULT_VIEW_MODE = 'common'; + + /** @var string Default view mode param */ + const DEFAULT_VIEW_MODE_PARAM = 'view'; + + /** @var array View mode-icon pairs */ + public static $viewModes = [ + 'minimal' => 'minimal', + 'common' => 'default', + 'detailed' => 'detailed', + 'tabular' => 'tabular' + ]; + + /** @var string */ + protected $defaultViewMode; + + /** @var string */ + protected $method = 'POST'; + + /** @var callable */ + protected $protector; + + /** @var string */ + protected $viewModeParam = self::DEFAULT_VIEW_MODE_PARAM; + + /** + * Get the default mode + * + * @return string + */ + public function getDefaultViewMode(): string + { + return $this->defaultViewMode ?: static::DEFAULT_VIEW_MODE; + } + + /** + * Set the default view mode + * + * @param string $defaultViewMode + * + * @return $this + */ + public function setDefaultViewMode(string $defaultViewMode): self + { + $this->defaultViewMode = $defaultViewMode; + + return $this; + } + + /** + * Get the view mode URL parameter + * + * @return string + */ + public function getViewModeParam(): string + { + return $this->viewModeParam; + } + + /** + * Set the view mode URL parameter + * + * @param string $viewModeParam + * + * @return $this + */ + public function setViewModeParam(string $viewModeParam): self + { + $this->viewModeParam = $viewModeParam; + + return $this; + } + + /** + * Get the view mode + * + * @return string + */ + public function getViewMode(): string + { + $viewMode = $this->getPopulatedValue($this->getViewModeParam(), $this->getDefaultViewMode()); + + if (array_key_exists($viewMode, static::$viewModes)) { + return $viewMode; + } + + return $this->getDefaultViewMode(); + } + + /** + * Set the view mode + * + * @param string $name + * + * @return $this + */ + public function setViewMode(string $name) + { + $this->populate([$this->getViewModeParam() => $name]); + + return $this; + } + + /** + * Set callback to protect ids with + * + * @param callable $protector + * + * @return $this + */ + public function setIdProtector(callable $protector): self + { + $this->protector = $protector; + + return $this; + } + + private function protectId($id) + { + if (is_callable($this->protector)) { + return call_user_func($this->protector, $id); + } + + return $id; + } + + protected function assemble() + { + $viewModeParam = $this->getViewModeParam(); + + $this->addElement($this->createUidElement()); + $this->addElement(new HiddenElement($viewModeParam)); + + foreach (static::$viewModes as $viewMode => $icon) { + if ($viewMode === 'tabular') { + continue; + } + + $protectedId = $this->protectId('view-mode-switcher-' . $icon); + $input = new InputElement($viewModeParam, [ + 'class' => 'autosubmit', + 'id' => $protectedId, + 'name' => $viewModeParam, + 'type' => 'radio', + 'value' => $viewMode + ]); + $input->getAttributes()->registerAttributeCallback('checked', function () use ($viewMode) { + return $viewMode === $this->getViewMode(); + }); + + $label = new HtmlElement( + 'label', + Attributes::create([ + 'for' => $protectedId + ]), + new IcingaIcon($icon) + ); + $label->getAttributes()->registerAttributeCallback('title', function () use ($viewMode) { + switch ($viewMode) { + case 'minimal': + $active = t('Minimal view active'); + $inactive = t('Switch to minimal view'); + break; + case 'common': + $active = t('Common view active'); + $inactive = t('Switch to common view'); + break; + case 'detailed': + $active = t('Detailed view active'); + $inactive = t('Switch to detailed view'); + } + + return $viewMode === $this->getViewMode() ? $active : $inactive; + }); + + $this->addHtml($input, $label); + } + } +} |