summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php')
-rw-r--r--library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php83
1 files changed, 83 insertions, 0 deletions
diff --git a/library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php b/library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php
new file mode 100644
index 0000000..2f8df33
--- /dev/null
+++ b/library/Icingadb/Hook/ActionsHook/ObjectActionsHook.php
@@ -0,0 +1,83 @@
+<?php
+
+/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */
+
+namespace Icinga\Module\Icingadb\Hook\ActionsHook;
+
+use Exception;
+use Icinga\Application\Hook;
+use Icinga\Application\Logger;
+use Icinga\Exception\IcingaException;
+use Icinga\Module\Icingadb\Hook\Common\HookUtils;
+use Icinga\Module\Icingadb\Hook\HostActionsHook;
+use Icinga\Module\Icingadb\Hook\ServiceActionsHook;
+use Icinga\Module\Icingadb\Model\Host;
+use Icinga\Module\Icingadb\Model\Service;
+use InvalidArgumentException;
+use ipl\Html\Attributes;
+use ipl\Html\HtmlElement;
+use ipl\Html\HtmlString;
+use ipl\Html\Text;
+use ipl\Orm\Model;
+use ipl\Web\Widget\Link;
+
+use function ipl\Stdlib\get_php_type;
+
+abstract class ObjectActionsHook
+{
+ use HookUtils;
+
+ /**
+ * Load all actions for the given object
+ *
+ * @param Host|Service $object
+ *
+ * @return HtmlElement
+ *
+ * @throws InvalidArgumentException If the given model is not supported
+ */
+ final public static function loadActions(Model $object): HtmlElement
+ {
+ switch (true) {
+ case $object instanceof Host:
+ /** @var HostActionsHook $hook */
+ $hookName = 'Icingadb\\HostActions';
+ break;
+ case $object instanceof Service:
+ /** @var ServiceActionsHook $hook */
+ $hookName = 'Icingadb\\ServiceActions';
+ break;
+ default:
+ throw new InvalidArgumentException(
+ sprintf('%s is not a supported object type', get_php_type($object))
+ );
+ }
+
+ $list = new HtmlElement('ul', Attributes::create(['class' => 'object-detail-actions']));
+ foreach (Hook::all($hookName) as $hook) {
+ try {
+ foreach ($hook->getActionsForObject($object) as $link) {
+ if (! $link instanceof Link) {
+ continue;
+ }
+
+ // It may be ValidHtml, but modules shouldn't be able to break our views.
+ // That's why it needs to be rendered instantly, as any error will then
+ // be caught here.
+ $renderedLink = (string) $link;
+ $moduleName = $hook->getModule()->getName();
+
+ $list->addHtml(new HtmlElement('li', Attributes::create([
+ 'class' => 'icinga-module module-' . $moduleName,
+ 'data-icinga-module' => $moduleName
+ ]), HtmlString::create($renderedLink)));
+ }
+ } catch (Exception $e) {
+ Logger::error("Failed to load object actions: %s\n%s", $e, $e->getTraceAsString());
+ $list->addHtml(new HtmlElement('li', null, Text::create(IcingaException::describe($e))));
+ }
+ }
+
+ return $list;
+ }
+}