From 4548122a18627ff701e0848d983f01c4c70d14d0 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 11 Mar 2022 16:39:19 +0100 Subject: [PATCH] Introduce `DashletListItem` class --- .../Dashboard/ItemList/DashletListItem.php | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 library/Icinga/Web/Dashboard/ItemList/DashletListItem.php diff --git a/library/Icinga/Web/Dashboard/ItemList/DashletListItem.php b/library/Icinga/Web/Dashboard/ItemList/DashletListItem.php new file mode 100644 index 000000000..a010016bf --- /dev/null +++ b/library/Icinga/Web/Dashboard/ItemList/DashletListItem.php @@ -0,0 +1,89 @@ + 'dashlet-list-item']; + + protected $tag = 'li'; + + /** @var Dashlet */ + protected $dashlet; + + protected $renderEditButton; + + public function __construct(Dashlet $dashlet = null, $renderEditButton = false) + { + $this->dashlet = $dashlet; + $this->renderEditButton = $renderEditButton; + } + + /** + * Set whether to render an edit button for this dashlet + * + * @param bool $value + * + * @return $this + */ + protected function setDetailUrl(bool $value) + { + $this->renderEditButton = $value; + + return $this; + } + + protected function assembleTitle() + { + $title = HtmlElement::create('h1', ['class' => 'dashlet-header']); + + if (! $this->dashlet) { + $title->add(t('Custom Url')); + } else { + $title->add($this->dashlet->getTitle()); + + if ($this->renderEditButton) { + $pane = $this->dashlet->getPane(); + $url = Url::fromPath(Dashboard::BASE_ROUTE . '/edit-dashlet'); + $url->setParams([ + 'home' => $pane->getHome()->getName(), + 'pane' => $pane->getName(), + 'dashlet' => $this->dashlet->getName() + ]); + + $title->addHtml(new Link(t('Edit'), $url, [ + 'data-icinga-modal' => true, + 'data-no-icinga-ajax' => true + ])); + } + } + + return $title; + } + + protected function assembleSummary() + { + $section = HtmlElement::create('section', ['class' => 'caption']); + + if (! $this->dashlet) { + $section->add(t('Create a dashlet with custom url and filter')); + } else { + $section->add($this->dashlet->getDescription() ?: $this->dashlet->getTitle()); + } + + return $section; + } + + protected function assemble() + { + $this->addHtml($this->assembleTitle()); + $this->addHtml($this->assembleSummary()); + } +}