summaryrefslogtreecommitdiffstats
path: root/library/Cube/Hook
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--library/Cube/Hook/ActionsHook.php99
-rw-r--r--library/Cube/Hook/IcingaDbActionsHook.php125
2 files changed, 224 insertions, 0 deletions
diff --git a/library/Cube/Hook/ActionsHook.php b/library/Cube/Hook/ActionsHook.php
new file mode 100644
index 0000000..8ba8a7c
--- /dev/null
+++ b/library/Cube/Hook/ActionsHook.php
@@ -0,0 +1,99 @@
+<?php
+
+// Icinga Web 2 Cube Module | (c) 2016 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Cube\Hook;
+
+use Icinga\Module\Cube\Cube;
+use Icinga\Module\Cube\Web\ActionLink;
+use Icinga\Module\Cube\Web\ActionLinks;
+use Icinga\Web\Url;
+use Icinga\Web\View;
+
+/**
+ * ActionsHook
+ *
+ * Implement this hook in case your module wants to add links to the detail
+ * page shown for a slice.
+ *
+ * @package Icinga\Module\Cube\Hook
+ */
+abstract class ActionsHook
+{
+ /** @var ActionLinks */
+ private $actionLinks;
+
+ /**
+ * Your implementation should extend this method
+ *
+ * Then use the addActionLink() method, eventually combined with the
+ * createUrl() helper like this:
+ *
+ * <code>
+ * $this->addActionLink(
+ * $this->makeUrl('mymodule/controller/action', array('some' => 'param')),
+ * 'A shown title',
+ * 'A longer description text, should fit into the available square field',
+ * 'icon-name'
+ * );
+ * </code>
+ *
+ * For a list of available icon names please enable the Icinga Web 2 'doc'
+ * module and go to "Documentation" -> "Developer - Style" -> "Icons"
+ *
+ * @param Cube $cube
+ * @param View $view
+ *
+ * @return void
+ */
+ abstract public function prepareActionLinks(Cube $cube, View $view);
+
+ /**
+ * Lazy access to an ActionLinks object
+ *
+ * @return ActionLinks
+ */
+ public function getActionLinks()
+ {
+ if ($this->actionLinks === null) {
+ $this->actionLinks = new ActionLinks();
+ }
+ return $this->actionLinks;
+ }
+
+ /**
+ * Helper method instantiating an ActionLink object
+ *
+ * @param Url $url
+ * @param string $title
+ * @param string $description
+ * @param string $icon
+ *
+ * @return $this
+ */
+ public function addActionLink(Url $url, $title, $description, $icon)
+ {
+ $this->getActionLinks()->add(
+ new ActionLink($url, $title, $description, $icon)
+ );
+
+ return $this;
+ }
+
+ /**
+ * Helper method instantiating an Url object
+ *
+ * @param string $path
+ * @param array $params
+ * @return Url
+ */
+ public function makeUrl($path, $params = null)
+ {
+ $url = Url::fromPath($path);
+ if ($params !== null) {
+ $url->getParams()->mergeValues($params);
+ }
+
+ return $url;
+ }
+}
diff --git a/library/Cube/Hook/IcingaDbActionsHook.php b/library/Cube/Hook/IcingaDbActionsHook.php
new file mode 100644
index 0000000..63c24fe
--- /dev/null
+++ b/library/Cube/Hook/IcingaDbActionsHook.php
@@ -0,0 +1,125 @@
+<?php
+
+// Icinga Web 2 Cube Module | (c) 2016 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Cube\Hook;
+
+use Exception;
+use Icinga\Application\Hook;
+use Icinga\Module\Cube\Cube;
+use Icinga\Module\Cube\IcingaDb\IcingaDbCube;
+use ipl\Web\Url;
+use ipl\Html\HtmlDocument;
+use ipl\Html\HtmlElement;
+use ipl\Web\Widget\Icon;
+use ipl\Web\Widget\Link;
+
+/**
+ * ActionsHook
+ *
+ * Implement this hook in case your module wants to add links to the detail
+ * page shown for a slice.
+ *
+ * @package Icinga\Module\Cube\Hook
+ */
+abstract class IcingaDbActionsHook
+{
+ /** @var Link[] */
+ private $actionLinks = [];
+
+ /**
+ * Create additional action links for the given cube
+ *
+ * @param IcingaDbCube $cube
+ * @return void
+ */
+ abstract public function createActionLinks(IcingaDbCube $cube);
+
+ /**
+ * Return the action links for the cube
+ *
+ * @return Link[]
+ */
+ final protected function getActionLinks(): array
+ {
+ return $this->actionLinks;
+ }
+
+ /**
+ * Helper method to populate action links array
+ *
+ * @param Url $url
+ * @param string $title
+ * @param string $description
+ * @param string $icon
+ *
+ * @return $this
+ */
+ final protected function addActionLink(Url $url, string $title, string $description, string $icon): self
+ {
+ $linkContent = (new HtmlDocument());
+ $linkContent->addHtml(new Icon($icon));
+ $linkContent->addHtml(HtmlElement::create('span', ['class' => 'title'], $title));
+ $linkContent->addHtml(HtmlElement::create('p', null, $description));
+
+ $this->actionLinks[] = new Link($linkContent, $url);
+
+ return $this;
+ }
+
+ /**
+ * Helper method instantiating an Url object
+ *
+ * @param string $path
+ * @param array $params
+ * @return Url
+ */
+ final protected function makeUrl(string $path, array $params = null): Url
+ {
+ $url = Url::fromPath($path);
+ if ($params !== null) {
+ $url->getParams()->mergeValues($params);
+ }
+
+ return $url;
+ }
+
+ /**
+ * Render all links for all Hook implementations
+ *
+ * This is what the Cube calls when rendering details
+ *
+ * @param IcingaDbCube $cube
+ *
+ * @return string
+ */
+ public static function renderAll(Cube $cube)
+ {
+ $html = new HtmlDocument();
+
+ /** @var IcingaDbActionsHook $hook */
+ foreach (Hook::all('Cube/IcingaDbActions') as $hook) {
+ try {
+ $hook->createActionLinks($cube);
+ } catch (Exception $e) {
+ $html->addHtml(HtmlElement::create('li', ['class' => 'error'], $e->getMessage()));
+ }
+
+ foreach ($hook->getActionLinks() as $link) {
+ $html->addHtml(HtmlElement::create('li', null, $link));
+ }
+ }
+
+ if ($html->isEmpty()) {
+ $html->addHtml(
+ HtmlElement::create(
+ 'li',
+ ['class' => 'error'],
+ t('No action links have been provided for this cube')
+ )
+ );
+ }
+
+ return $html->render();
+ }
+}