diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:46:47 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:46:47 +0000 |
commit | 4ada86876033fa171e2896d7e3d3c5645d8062db (patch) | |
tree | f0d1fee61877df200ccfb1c0af58a39cd551fb46 /library/Reporting/Web/Widget | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-reporting-4ada86876033fa171e2896d7e3d3c5645d8062db.tar.xz icingaweb2-module-reporting-4ada86876033fa171e2896d7e3d3c5645d8062db.zip |
Adding upstream version 0.10.0.upstream/0.10.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Reporting/Web/Widget')
-rw-r--r-- | library/Reporting/Web/Widget/CompatDropdown.php | 22 | ||||
-rw-r--r-- | library/Reporting/Web/Widget/CoverPage.php | 181 | ||||
-rw-r--r-- | library/Reporting/Web/Widget/HeaderOrFooter.php | 95 | ||||
-rw-r--r-- | library/Reporting/Web/Widget/Template.php | 183 |
4 files changed, 481 insertions, 0 deletions
diff --git a/library/Reporting/Web/Widget/CompatDropdown.php b/library/Reporting/Web/Widget/CompatDropdown.php new file mode 100644 index 0000000..cdd7b40 --- /dev/null +++ b/library/Reporting/Web/Widget/CompatDropdown.php @@ -0,0 +1,22 @@ +<?php +// Icinga Reporting | (c) 2021 Icinga GmbH | GPLv2 + +namespace Icinga\Module\Reporting\Web\Widget; + +use ipl\Web\Widget\ActionLink; +use ipl\Web\Widget\Dropdown; + +class CompatDropdown extends Dropdown +{ + public function addLink($content, $url, $icon = null, array $attributes = null) + { + $link = new ActionLink($content, $url, $icon, ['class' => 'dropdown-item']); + if (! empty($attributes)) { + $link->addAttributes($attributes); + } + + $this->links[] = $link; + + return $this; + } +} diff --git a/library/Reporting/Web/Widget/CoverPage.php b/library/Reporting/Web/Widget/CoverPage.php new file mode 100644 index 0000000..545ef6a --- /dev/null +++ b/library/Reporting/Web/Widget/CoverPage.php @@ -0,0 +1,181 @@ +<?php + +namespace Icinga\Module\Reporting\Web\Widget; + +use Icinga\Module\Reporting\Common\Macros; +use ipl\Html\BaseHtmlElement; +use ipl\Html\Html; + +class CoverPage extends BaseHtmlElement +{ + use Macros; + + /** @var array */ + protected $backgroundImage; + + /** @var string */ + protected $color; + + /** @var array */ + protected $logo; + + /** @var string */ + protected $title; + + protected $tag = 'div'; + + protected $defaultAttributes = ['class' => 'cover-page page']; + + /** + * @return bool + */ + public function hasBackgroundImage() + { + return $this->backgroundImage !== null; + } + + /** + * @return array + */ + public function getBackgroundImage() + { + return $this->backgroundImage; + } + + /** + * @param array $backgroundImage + * + * @return $this + */ + public function setBackgroundImage($backgroundImage) + { + $this->backgroundImage = $backgroundImage; + + return $this; + } + + /** + * @return bool + */ + public function hasColor() + { + return $this->color !== null; + } + + /** + * @return string + */ + public function getColor() + { + return $this->color; + } + + /** + * @param string $color + * + * @return $this + */ + public function setColor($color) + { + $this->color = $color; + + return $this; + } + + /** + * @return bool + */ + public function hasLogo() + { + return $this->logo !== null; + } + + /** + * @return array + */ + public function getLogo() + { + return $this->logo; + } + + /** + * @param array $logo + * + * @return $this + */ + public function setLogo($logo) + { + $this->logo = $logo; + + return $this; + } + + public function hasTitle() + { + return $this->title !== null; + } + + /** + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param string $title + * + * @return $this + */ + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + protected function assemble() + { + if ($this->hasBackgroundImage()) { + $this + ->getAttributes() + ->add('style', "background-image: url('" . Template::getDataUrl($this->getBackgroundImage()) . "');"); + } + + $content = Html::tag('div', ['class' => 'cover-page-content']); + + if ($this->hasColor()) { + $content->getAttributes()->add('style', "color: {$this->getColor()};"); + } + + if ($this->hasLogo()) { + $content->add(Html::tag( + 'img', + [ + 'class' => 'logo', + 'src' => Template::getDataUrl($this->getLogo()) + ] + )); + } + + if ($this->hasTitle()) { + $title = array_map(function ($part) { + $part = trim($part); + + if (! $part) { + return Html::tag('br'); + } else { + return Html::tag('div', null, $part); + } + }, explode("\n", $this->resolveMacros($this->getTitle()))); + + $content->add(Html::tag( + 'h2', + $title + )); + } + + $this->add($content); + } +} diff --git a/library/Reporting/Web/Widget/HeaderOrFooter.php b/library/Reporting/Web/Widget/HeaderOrFooter.php new file mode 100644 index 0000000..dcb37e7 --- /dev/null +++ b/library/Reporting/Web/Widget/HeaderOrFooter.php @@ -0,0 +1,95 @@ +<?php + +namespace Icinga\Module\Reporting\Web\Widget; + +use Icinga\Module\Reporting\Common\Macros; +use ipl\Html\Html; +use ipl\Html\HtmlDocument; + +class HeaderOrFooter extends HtmlDocument +{ + use Macros; + + const HEADER = 'header'; + + const FOOTER = 'footer'; + + protected $type; + + protected $data; + + protected $tag = 'div'; + + public function __construct($type, array $data) + { + $this->type = $type; + $this->data = $data; + } + + protected function resolveVariable($variable) + { + switch ($variable) { + case 'report_title': + $resolved = Html::tag('span', ['class' => 'title']); + break; + case 'time_frame': + $resolved = Html::tag('p', $this->getMacro('time_frame')); + break; + case 'time_frame_absolute': + $resolved = Html::tag('p', $this->getMacro('time_frame_absolute')); + break; + case 'page_number': + $resolved = Html::tag('span', ['class' => 'pageNumber']); + break; + case 'total_number_of_pages': + $resolved = Html::tag('span', ['class' => 'totalPages']); + break; + case 'page_of': + $resolved = Html::tag('p', Html::sprintf( + '%s / %s', + Html::tag('span', ['class' => 'pageNumber']), + Html::tag('span', ['class' => 'totalPages']) + )); + break; + case 'date': + $resolved = Html::tag('span', ['class' => 'date']); + break; + default: + $resolved = $variable; + break; + } + + return $resolved; + } + + protected function createColumn(array $data, $key) + { + $typeKey = "${key}_type"; + $valueKey = "${key}_value"; + $type = isset($data[$typeKey]) ? $data[$typeKey] : null; + + switch ($type) { + case 'text': + $column = Html::tag('p', $data[$valueKey]); + break; + case 'image': + $column = Html::tag('img', ['height' => 13, 'src' => Template::getDataUrl($data[$valueKey])]); + break; + case 'variable': + $column = $this->resolveVariable($data[$valueKey]); + break; + default: + $column = Html::tag('div'); + break; + } + + return $column; + } + + protected function assemble() + { + for ($i = 1; $i <= 3; ++$i) { + $this->add($this->createColumn($this->data, "{$this->type}_column{$i}")); + } + } +} diff --git a/library/Reporting/Web/Widget/Template.php b/library/Reporting/Web/Widget/Template.php new file mode 100644 index 0000000..e780a3d --- /dev/null +++ b/library/Reporting/Web/Widget/Template.php @@ -0,0 +1,183 @@ +<?php +// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 + +namespace Icinga\Module\Reporting\Web\Widget; + +use Icinga\Module\Reporting\Common\Macros; +use Icinga\Module\Reporting\Database; +use ipl\Html\BaseHtmlElement; +use ipl\Html\Html; +use ipl\Sql\Select; + +class Template extends BaseHtmlElement +{ + use Database; + use Macros; + + protected $tag = 'div'; + + protected $defaultAttributes = ['class' => 'template']; + + /** @var CoverPage */ + protected $coverPage; + + /** @var HeaderOrFooter */ + protected $header; + + /** @var HeaderOrFooter */ + protected $footer; + + protected $preview; + + public static function getDataUrl(array $image = null) + { + if (empty($image)) { + return 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='; + } + + return sprintf('data:%s;base64,%s', $image['mime_type'], $image['content']); + } + + public static function fromDb($id) + { + $template = new static(); + + $select = (new Select()) + ->from('template') + ->columns('*') + ->where(['id = ?' => $id]); + + $row = $template->getDb()->select($select)->fetch(); + + if ($row === false) { + return null; + } + + $row->settings = json_decode($row->settings, true); + + $coverPage = (new CoverPage()) + ->setColor($row->settings['color']) + ->setTitle($row->settings['title']); + + if (isset($row->settings['cover_page_background_image'])) { + $coverPage->setBackgroundImage($row->settings['cover_page_background_image']); + } + + if (isset($row->settings['cover_page_logo'])) { + $coverPage->setLogo($row->settings['cover_page_logo']); + } + + $template + ->setCoverPage($coverPage) + ->setHeader(new HeaderOrFooter(HeaderOrFooter::HEADER, $row->settings)) + ->setFooter(new HeaderOrFooter(HeaderOrFooter::FOOTER, $row->settings)); + + return $template; + } + + /** + * @return CoverPage + */ + public function getCoverPage() + { + return $this->coverPage; + } + + /** + * @param CoverPage $coverPage + * + * @return $this + */ + public function setCoverPage(CoverPage $coverPage) + { + $this->coverPage = $coverPage; + + return $this; + } + + /** + * @return HeaderOrFooter + */ + public function getHeader() + { + return $this->header; + } + + /** + * @param HeaderOrFooter $header + * + * @return $this + */ + public function setHeader($header) + { + $this->header = $header; + + return $this; + } + + /** + * @return HeaderOrFooter + */ + public function getFooter() + { + return $this->footer; + } + + /** + * @param HeaderOrFooter $footer + * + * @return $this + */ + public function setFooter($footer) + { + $this->footer = $footer; + + return $this; + } + + /** + * @return mixed + */ + public function getPreview() + { + return $this->preview; + } + + /** + * @param mixed $preview + * + * @return $this + */ + public function setPreview($preview) + { + $this->preview = $preview; + + return $this; + } + + protected function assemble() + { + if ($this->preview) { + $this->getAttributes()->add('class', 'preview'); + } + + $this->add($this->getCoverPage()->setMacros($this->macros)); + +// $page = Html::tag( +// 'div', +// ['class' => 'main'], +// Html::tag('div', ['class' => 'page-content'], [ +// $this->header->setMacros($this->macros), +// Html::tag( +// 'div', +// [ +// 'class' => 'main' +// ] +// ), +// $this->footer->setMacros($this->macros) +// ]) +// ); +// +// $this->add($page); + } +} |