summaryrefslogtreecommitdiffstats
path: root/library/Cube/Web
diff options
context:
space:
mode:
Diffstat (limited to 'library/Cube/Web')
-rw-r--r--library/Cube/Web/ActionLink.php103
-rw-r--r--library/Cube/Web/ActionLinks.php115
-rw-r--r--library/Cube/Web/Controller.php115
-rw-r--r--library/Cube/Web/IdoController.php24
4 files changed, 357 insertions, 0 deletions
diff --git a/library/Cube/Web/ActionLink.php b/library/Cube/Web/ActionLink.php
new file mode 100644
index 0000000..c9ad87b
--- /dev/null
+++ b/library/Cube/Web/ActionLink.php
@@ -0,0 +1,103 @@
+<?php
+
+// Icinga Web 2 Cube Module | (c) 2016 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Cube\Web;
+
+use Icinga\Web\Url;
+use Icinga\Web\View;
+
+/**
+ * ActionLink
+ *
+ * ActionLinksHook implementations return instances of this class
+ *
+ * @package Icinga\Module\Cube\Web
+ */
+class ActionLink
+{
+ /** @var Url */
+ protected $url;
+
+ /** @var string */
+ protected $title;
+
+ /** @var string */
+ protected $description;
+
+ /** @var string */
+ protected $icon;
+
+ /**
+ * ActionLink constructor.
+ * @param Url $url
+ * @param string $title
+ * @param string $description
+ * @param string $icon
+ */
+ public function __construct(Url $url, $title, $description, $icon)
+ {
+ $this->url = $url;
+ $this->title = $title;
+ $this->description = $description;
+ $this->icon = $icon;
+ }
+
+ /**
+ * @return Url
+ */
+ public function getUrl()
+ {
+ return $this->url;
+ }
+
+ /**
+ * @return string
+ */
+ public function getTitle()
+ {
+ return $this->title;
+ }
+
+ /**
+ * @return string
+ */
+ public function getDescription()
+ {
+ return $this->description;
+ }
+
+ /**
+ * @return string
+ */
+ public function getIcon()
+ {
+ return $this->icon;
+ }
+
+ /**
+ * Render our icon
+ *
+ * @param View $view
+ * @return string
+ */
+ protected function renderIcon(View $view)
+ {
+ return $view->icon($this->getIcon());
+ }
+
+ /**
+ * @param View $view
+ * @return string
+ */
+ public function render(View $view)
+ {
+ return sprintf(
+ '<a href="%s">%s<span class="title">%s</span><p>%s</p></a>',
+ $this->getUrl(),
+ $this->renderIcon($view),
+ $view->escape($this->getTitle()),
+ $view->escape($this->getDescription())
+ );
+ }
+}
diff --git a/library/Cube/Web/ActionLinks.php b/library/Cube/Web/ActionLinks.php
new file mode 100644
index 0000000..0c6cba4
--- /dev/null
+++ b/library/Cube/Web/ActionLinks.php
@@ -0,0 +1,115 @@
+<?php
+
+// Icinga Web 2 Cube Module | (c) 2016 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Cube\Web;
+
+use Exception;
+use Icinga\Application\Hook;
+use Icinga\Module\Cube\Cube;
+use Icinga\Module\Cube\Hook\ActionsHook;
+use Icinga\Web\View;
+
+/**
+ * ActionLink
+ *
+ * ActionsHook implementations return instances of this class
+ *
+ * @package Icinga\Module\Cube\Web
+ */
+class ActionLinks
+{
+ /** @var ActionLink[] */
+ protected $links = array();
+
+ /**
+ * Get all links for all Hook implementations
+ *
+ * This is what the Cube calls when rendering details
+ *
+ * @param Cube $cube
+ * @param View $view
+ *
+ * @return string
+ */
+ public static function renderAll(Cube $cube, View $view)
+ {
+ $html = array();
+
+ /** @var ActionsHook $hook */
+ foreach (Hook::all('Cube/Actions') as $hook) {
+ try {
+ $hook->prepareActionLinks($cube, $view);
+ } catch (Exception $e) {
+ $html[] = static::renderErrorItem($e, $view);
+ }
+
+ foreach ($hook->getActionLinks()->getLinks() as $link) {
+ $html[] = '<li>' . $link->render($view) . '</li>';
+ }
+ }
+
+ if (empty($html)) {
+ $html[] = static::renderErrorItem(
+ $view->translate('No action links have been provided for this cube'),
+ $view
+ );
+ }
+
+ return implode("\n", $html) . "\n";
+ }
+
+ /**
+ * @param Exception|string $error
+ * @param View $view
+ * @return string
+ */
+ private static function renderErrorItem($error, View $view)
+ {
+ if ($error instanceof Exception) {
+ $error = $error->getMessage();
+ }
+ return '<li class="error">' . $view->escape($error) . '</li>';
+ }
+
+ /**
+ * Add an ActionLink to this set of actions
+ *
+ * @param ActionLink $link
+ * @return $this
+ */
+ public function add(ActionLink $link)
+ {
+ $this->links[] = $link;
+ return $this;
+ }
+
+ /**
+ * @return ActionLink[]
+ */
+ public function getLinks()
+ {
+ return $this->links;
+ }
+
+ /**
+ * @param View $view
+ *
+ * @return string
+ */
+ public function render(View $view)
+ {
+ $links = $this->getLinks();
+ if (empty($links)) {
+ return '';
+ }
+
+ $html = '<ul class="action-links">';
+ foreach ($links as $link) {
+ $html .= $link->render($view);
+ }
+ $html .= '</ul>';
+
+ return $html;
+ }
+}
diff --git a/library/Cube/Web/Controller.php b/library/Cube/Web/Controller.php
new file mode 100644
index 0000000..b7405f9
--- /dev/null
+++ b/library/Cube/Web/Controller.php
@@ -0,0 +1,115 @@
+<?php
+
+// Icinga Web 2 Cube Module | (c) 2016 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Cube\Web;
+
+use Icinga\Application\Modules\Module;
+use Icinga\Module\Cube\DimensionParams;
+use Icinga\Module\Cube\Forms\DimensionsForm;
+use Icinga\Module\Cube\Hook\IcingaDbActionsHook;
+use Icinga\Module\Cube\ProvidedHook\Icingadb\IcingadbSupport;
+use Icinga\Web\Controller as WebController;
+use Icinga\Web\View;
+
+abstract class Controller extends WebController
+{
+ /** @var View This helps IDEs to understand that this is not ZF view */
+ public $view;
+
+ /** @var \Icinga\Module\Cube\Cube */
+ protected $cube;
+
+ /**
+ * Return this controllers' cube
+ *
+ * @return \Icinga\Module\Cube\Cube
+ */
+ abstract protected function getCube();
+
+ protected function moduleInit()
+ {
+ $this->cube = $this->getCube();
+ }
+
+ public function detailsAction()
+ {
+ $this->getTabs()->add('details', [
+ 'label' => $this->translate('Cube details'),
+ 'url' => $this->getRequest()->getUrl()
+ ])->activate('details');
+
+ $this->cube->chooseFacts(array_keys($this->cube->getAvailableFactColumns()));
+ $vars = DimensionParams::fromString($this->params->shift('dimensions', ''))->getDimensions();
+ $wantNull = $this->params->shift('wantNull');
+
+ foreach ($vars as $var) {
+ $this->cube->addDimensionByName($var);
+ if ($wantNull) {
+ $this->cube->getDimension($var)->wantNull();
+ }
+ }
+
+ foreach (['renderLayout', 'showFullscreen', 'showCompact', 'view'] as $p) {
+ $this->params->shift($p);
+ }
+
+ foreach ($this->params->toArray() as $param) {
+ $this->cube->slice(rawurldecode($param[0]), rawurldecode($param[1]));
+ }
+
+ $this->view->title = $this->cube->getSlicesLabel();
+ if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
+ $this->view->links = IcingaDbActionsHook::renderAll($this->cube);
+ } else {
+ $this->view->links = ActionLinks::renderAll($this->cube, $this->view);
+ }
+
+ $this->render('cube-details', null, true);
+ }
+
+ protected function renderCube()
+ {
+ // Hint: order matters, we are shifting!
+ $showSettings = $this->params->shift('showSettings');
+
+ $this->cube->chooseFacts(array_keys($this->cube->getAvailableFactColumns()));
+ $vars = DimensionParams::fromString($this->params->shift('dimensions', ''))->getDimensions();
+ $wantNull = $this->params->shift('wantNull');
+
+ foreach ($vars as $var) {
+ $this->cube->addDimensionByName($var);
+ if ($wantNull) {
+ $this->cube->getDimension($var)->wantNull();
+ }
+ }
+
+ foreach (['renderLayout', 'showFullscreen', 'showCompact', 'view'] as $p) {
+ $this->params->shift($p);
+ }
+
+ foreach ($this->params->toArray() as $param) {
+ $this->cube->slice(rawurldecode($param[0]), rawurldecode($param[1]));
+ }
+
+ $this->view->title = sprintf(
+ $this->translate('Cube: %s'),
+ $this->cube->getPathLabel()
+ );
+
+ if (count($this->cube->listDimensions()) > 0) {
+ $this->view->cube = $this->cube;
+ } else {
+ $showSettings = true;
+ }
+
+ if ($showSettings) {
+ $this->view->form = (new DimensionsForm())->setCube($this->cube);
+ $this->view->form->handleRequest();
+ } else {
+ $this->setAutorefreshInterval(15);
+ }
+
+ $this->render('cube-index', null, true);
+ }
+}
diff --git a/library/Cube/Web/IdoController.php b/library/Cube/Web/IdoController.php
new file mode 100644
index 0000000..2a024ae
--- /dev/null
+++ b/library/Cube/Web/IdoController.php
@@ -0,0 +1,24 @@
+<?php
+
+// Icinga Web 2 Cube Module | (c) 2019 Icinga GmbH | GPLv2
+
+namespace Icinga\Module\Cube\Web;
+
+use Icinga\Web\Widget\Tabextension\DashboardAction;
+
+abstract class IdoController extends Controller
+{
+ public function createTabs()
+ {
+ return $this->getTabs()
+ ->add('cube/hosts', [
+ 'label' => $this->translate('Hosts'),
+ 'url' => 'cube/hosts' . ($this->params->toString() === '' ? '' : '?' . $this->params->toString())
+ ])
+ ->add('cube/services', [
+ 'label' => $this->translate('Services'),
+ 'url' => 'cube/services' . ($this->params->toString() === '' ? '' : '?' . $this->params->toString())
+ ])
+ ->extend(new DashboardAction());
+ }
+}