From a0901c4b7f2db488cb4fb3be2dd921a0308f4659 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:36:40 +0200 Subject: Adding upstream version 1.0.2. Signed-off-by: Daniel Baumann --- library/Icingadb/Hook/TabHook/HookActions.php | 148 ++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 library/Icingadb/Hook/TabHook/HookActions.php (limited to 'library/Icingadb/Hook/TabHook/HookActions.php') diff --git a/library/Icingadb/Hook/TabHook/HookActions.php b/library/Icingadb/Hook/TabHook/HookActions.php new file mode 100644 index 0000000..d2801a5 --- /dev/null +++ b/library/Icingadb/Hook/TabHook/HookActions.php @@ -0,0 +1,148 @@ +loadTabHooks(); + if (isset($hooks[$hookName])) { + $this->showTabHook($hooks[$hookName]); + return; + } + } + + parent::__call($methodName, $args); + } + + /** + * Register the object for which to load additional tabs + * + * @param Model $object + * + * @return void + */ + protected function loadTabsForObject(Model $object) + { + $this->objectToLoadTabsFor = $object; + } + + /** + * Load tab hooks + * + * @return array + */ + protected function loadTabHooks(): array + { + if ($this->objectToLoadTabsFor === null) { + return []; + } elseif ($this->tabHooks !== null) { + return $this->tabHooks; + } + + $this->tabHooks = []; + foreach (Hook::all('Icingadb\\Tab') as $hook) { + /** @var TabHook $hook */ + try { + if ($hook->shouldBeShown($this->objectToLoadTabsFor)) { + $this->tabHooks[Str::camel($hook->getName())] = $hook; + } + } catch (Exception $e) { + Logger::error("Failed to load tab hook: %s\n%s", $e, $e->getTraceAsString()); + } + } + + return $this->tabHooks; + } + + /** + * Load additional tabs + * + * @return Generator + */ + protected function loadAdditionalTabs(): Generator + { + foreach ($this->loadTabHooks() as $hook) { + yield $hook->getName() => [ + 'label' => $hook->getLabel(), + 'url' => 'icingadb/' . $this->getRequest()->getControllerName() . '/' . $hook->getName() + ]; + } + } + + /** + * Render the given tab hook + * + * @param TabHook $hook + * + * @return void + */ + protected function showTabHook(TabHook $hook) + { + $moduleName = $hook->getModule()->getName(); + + foreach ($hook->getControls($this->objectToLoadTabsFor) as $control) { + $this->addControl($control); + } + + if (! empty($this->controls->getContent())) { + $this->controls->addAttributes([ + 'class' => ['icinga-module', 'module-' . $moduleName], + 'data-icinga-module' => $moduleName + ]); + } else { + foreach ($this->getDefaultTabControls() as $control) { + $this->addControl($control); + } + } + + foreach ($hook->getContent($this->objectToLoadTabsFor) as $content) { + $this->addContent($content); + } + + $this->content->addAttributes([ + 'class' => ['icinga-module', 'module-' . $moduleName], + 'data-icinga-module' => $moduleName + ]); + + foreach ($hook->getFooter($this->objectToLoadTabsFor) as $footer) { + $this->addFooter($footer); + } + + $this->footer->addAttributes([ + 'class' => ['icinga-module', 'module-' . $moduleName], + 'data-icinga-module' => $moduleName + ]); + } +} -- cgit v1.2.3