Drop all dashboards related legacy codes

This commit is contained in:
Yonas Habteab 2022-06-13 08:56:20 +02:00
parent e689959839
commit a726348718
18 changed files with 19 additions and 1649 deletions

View File

@ -1,35 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Forms\Navigation;
class DashletForm extends NavigationItemForm
{
/**
* {@inheritdoc}
*/
public function createElements(array $formData)
{
$this->addElement(
'text',
'pane',
array(
'required' => true,
'label' => $this->translate('Pane'),
'description' => $this->translate('The name of the dashboard pane in which to display this dashlet')
)
);
$this->addElement(
'text',
'url',
array(
'required' => true,
'label' => $this->translate('Url'),
'description' => $this->translate(
'The url to load in the dashlet. For external urls, make sure to prepend'
. ' an appropriate protocol identifier (e.g. http://example.tld)'
)
)
);
}
}

View File

@ -1,13 +0,0 @@
<div class="content">
<h1><?= $this->translate('Could not save dashboard'); ?></h1>
<p>
<?= $this->translate('Please copy the following dashboard snippet to '); ?>
<strong><?= $this->config->getConfigFile(); ?>;</strong>.
<br>
<?= $this->translate('Make sure that the webserver can write to this file.'); ?>
</p>
<pre><?= $this->config; ?></pre>
<hr>
<h2><?= $this->translate('Error details'); ?></h2>
<p><?= $this->error->getMessage(); ?></p>
</div>

View File

@ -1,26 +0,0 @@
<div class="controls">
<?php if (! $this->compact): ?>
<?= $this->tabs ?>
<?php endif ?>
</div>
<?php if ($this->dashboard): ?>
<div class="dashboard content">
<?= $this->dashboard ?>
</div>
<?php else: ?>
<div class="content">
<h1><?= $this->escape($this->translate('Welcome to Icinga Web!')) ?></h1>
<p>
<?php if (! $this->hasPermission('config/modules')) {
echo $this->escape($this->translate(
'Currently there is no dashlet available. Please contact the administrator.'
));
} else {
printf(
$this->escape($this->translate('Currently there is no dashlet available. This might change once you enabled some of the available %s.')),
$this->qlink($this->translate('modules'), 'config/modules')
);
} ?>
</p>
</div>
<?php endif ?>

View File

@ -1,6 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<?= $this->form; ?>
</div>

View File

@ -1,6 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<?= $this->form; ?>
</div>

View File

@ -1,6 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<?= $this->form; ?>
</div>

View File

@ -1,6 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<?= $this->form; ?>
</div>

View File

@ -1,91 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<h1><?= t('Dashboard Settings'); ?></h1>
<table class="avp action" data-base-target="_next">
<thead>
<tr>
<th style="width: 18em;">
<strong><?= t('Dashlet Name') ?></strong>
</th>
<th>
<strong><?= t('Url') ?></strong>
</th>
<th style="width: 1.4em;">&nbsp;</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->dashboard->getPanes() as $pane): ?>
<?php if ($pane->getDisabled()) continue; ?>
<tr>
<th colspan="2" style="text-align: left; padding: 0.5em;">
<?php if ($pane->isUserWidget()): ?>
<?= $this->qlink(
$pane->getName(),
'dashboard/rename-pane',
array('pane' => $pane->getName()),
array('title' => sprintf($this->translate('Edit pane %s'), $pane->getName()))
) ?>
<?php else: ?>
<?= $this->escape($pane->getName()) ?>
<?php endif ?>
</th>
<th>
<?= $this->qlink(
'',
'dashboard/remove-pane',
array('pane' => $pane->getName()),
array(
'icon' => 'trash',
'title' => sprintf($this->translate('Remove pane %s'), $pane->getName())
)
); ?>
</th>
</tr>
<?php $dashlets = $pane->getDashlets(); ?>
<?php if(empty($dashlets)): ?>
<tr>
<td colspan="3">
<?= $this->translate('No dashlets added to dashboard') ?>.
</td>
</tr>
<?php else: ?>
<?php foreach ($dashlets as $dashlet): ?>
<?php if ($dashlet->getDisabled()) continue; ?>
<tr>
<td>
<?= $this->qlink(
$dashlet->getTitle(),
'dashboard/update-dashlet',
array('pane' => $pane->getName(), 'dashlet' => $dashlet->getName()),
array('title' => sprintf($this->translate('Edit dashlet %s'), $dashlet->getTitle()))
); ?>
</td>
<td style="table-layout: fixed; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
<?= $this->qlink(
$dashlet->getUrl()->getRelativeUrl(),
$dashlet->getUrl()->getRelativeUrl(),
null,
array('title' => sprintf($this->translate('Show dashlet %s'), $dashlet->getTitle()))
); ?>
</td>
<td>
<?= $this->qlink(
'',
'dashboard/remove-dashlet',
array('pane' => $pane->getName(), 'dashlet' => $dashlet->getName()),
array(
'icon' => 'trash',
'title' => sprintf($this->translate('Remove dashlet %s from pane %s'), $dashlet->getTitle(), $pane->getTitle())
)
); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>

View File

@ -1,6 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
</div>
<div class="content">
<?= $this->form; ?>
</div>

View File

@ -238,28 +238,7 @@ class Web extends EmbeddedWeb
*/
public function getSharedNavigation($type)
{
$config = Config::navigation($type === 'dashboard-pane' ? 'dashlet' : $type);
if ($type === 'dashboard-pane') {
$panes = array();
foreach ($config as $dashletName => $dashletConfig) {
if ($this->hasAccessToSharedNavigationItem($dashletConfig)) {
// TODO: Throw ConfigurationError if pane or url is missing
$panes[$dashletConfig->pane][$dashletName] = $dashletConfig->url;
}
}
$navigation = new Navigation();
foreach ($panes as $paneName => $dashlets) {
$navigation->addItem(
$paneName,
array(
'type' => 'dashboard-pane',
'dashlets' => $dashlets
)
);
}
} else {
$config = Config::navigation($type);
$items = array();
foreach ($config as $name => $typeConfig) {
if (isset($this->accessibleMenuItems[$name])) {
@ -276,10 +255,7 @@ class Web extends EmbeddedWeb
}
}
$navigation = Navigation::fromConfig($items);
}
return $navigation;
return Navigation::fromConfig($items);
}
/**

View File

@ -1,137 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Legacy;
use Icinga\Application\Config;
use Icinga\User;
use Icinga\Web\Navigation\DashboardPane;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Navigation\NavigationItem;
/**
* Legacy dashboard config class for case insensitive interpretation of dashboard config files
*
* Before 2.2, the username part in dashboard config files was not lowered.
*
* @deprecated(el): Remove. TBD.
*/
class DashboardConfig extends Config
{
/**
* User
*
* @var User
*/
protected $user;
/**
* Get the user
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set the user
*
* @param User $user
*
* @return $this
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* List all dashboard configuration files that match the given user
*
* @param User $user
*
* @return string[]
*/
public static function listConfigFilesForUser(User $user)
{
$files = array();
$dashboards = static::resolvePath('dashboards');
if ($handle = @opendir($dashboards)) {
while (false !== ($entry = readdir($handle))) {
if ($entry[0] === '.' || ! is_dir($dashboards . '/' . $entry)) {
continue;
}
if (strtolower($entry) === strtolower($user->getUsername())) {
$files[] = $dashboards . '/' . $entry . '/dashboard.ini';
}
}
closedir($handle);
}
return $files;
}
/**
* {@inheritdoc}
*/
public function saveIni($filePath = null, $fileMode = 0660)
{
// Preprocessing start, ensures that the non-translated names are used to save module dashboard changes
// TODO: This MUST NOT survive the new dashboard implementation (yes, it's still a thing..)
$dashboardNavigation = new Navigation();
$dashboardNavigation->load('dashboard-pane');
$getDashboardPane = function ($label) use ($dashboardNavigation) {
foreach ($dashboardNavigation as $dashboardPane) {
/** @var DashboardPane $dashboardPane */
if ($dashboardPane->getLabel() === $label) {
return $dashboardPane;
}
foreach ($dashboardPane->getChildren() as $dashlet) {
/** @var NavigationItem $dashlet */
if ($dashlet->getLabel() === $label) {
return $dashlet;
}
}
}
};
foreach (clone $this->config as $name => $options) {
if (strpos($name, '.') !== false) {
list($dashboardLabel, $dashletLabel) = explode('.', $name, 2);
} else {
$dashboardLabel = $name;
$dashletLabel = null;
}
$dashboardPane = $getDashboardPane($dashboardLabel);
if ($dashboardPane !== null) {
$dashboardLabel = $dashboardPane->getName();
}
if ($dashletLabel !== null) {
$dashletItem = $getDashboardPane($dashletLabel);
if ($dashletItem !== null) {
$dashletLabel = $dashletItem->getName();
}
}
unset($this->config[$name]);
$this->config[$dashboardLabel . ($dashletLabel ? '.' . $dashletLabel : '')] = $options;
}
// Preprocessing end
parent::saveIni($filePath, $fileMode);
if ($filePath === null) {
$filePath = $this->configFile;
}
foreach (static::listConfigFilesForUser($this->user) as $file) {
if ($file !== $filePath) {
@unlink($file);
}
}
}
}

View File

@ -621,29 +621,8 @@ class User
*/
public function getNavigation($type)
{
$config = Config::navigation($type === 'dashboard-pane' ? 'dashlet' : $type, $this->getUsername());
$config = Config::navigation($type, $this->getUsername());
if ($type === 'dashboard-pane') {
$panes = array();
foreach ($config as $dashletName => $dashletConfig) {
// TODO: Throw ConfigurationError if pane or url is missing
$panes[$dashletConfig->pane][$dashletName] = $dashletConfig->url;
}
$navigation = new Navigation();
foreach ($panes as $paneName => $dashlets) {
$navigation->addItem(
$paneName,
array(
'type' => 'dashboard-pane',
'dashlets' => $dashlets
)
);
}
} else {
$navigation = Navigation::fromConfig($config);
}
return $navigation;
return Navigation::fromConfig($config);
}
}

View File

@ -1,84 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Navigation;
use Icinga\Web\Url;
/**
* A dashboard pane
*/
class DashboardPane extends NavigationItem
{
/**
* This pane's dashlets
*
* @var array
*/
protected $dashlets;
protected $disabled;
/**
* Set this pane's dashlets
*
* @param array $dashlets
*
* @return $this
*/
public function setDashlets(array $dashlets)
{
$this->dashlets = $dashlets;
return $this;
}
/**
* Return this pane's dashlets
*
* @param bool $ordered Whether to order the dashlets first
*
* @return array
*/
public function getDashlets($ordered = true)
{
if ($this->dashlets === null) {
return array();
}
if ($ordered) {
$dashlets = $this->dashlets;
ksort($dashlets);
return $dashlets;
}
return $this->dashlets;
}
/**
* {@inheritdoc}
*/
public function init()
{
$this->setUrl(Url::fromPath('dashboard', array('pane' => $this->getName())));
}
/**
* Set disabled state for pane
*
* @param bool $disabled
*/
public function setDisabled($disabled = true)
{
$this->disabled = (bool) $disabled;
}
/**
* Get disabled state for pane
*
* @return bool
*/
public function getDisabled()
{
return $this->disabled;
}
}

View File

@ -414,13 +414,11 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
public function load($type)
{
$user = Auth::getInstance()->getUser();
if ($type !== 'dashboard-pane') {
// Shareables
$this->merge(Icinga::app()->getSharedNavigation($type));
// User Preferences
$this->merge($user->getNavigation($type));
}
// Modules
$moduleManager = Icinga::app()->getModuleManager();
@ -428,8 +426,6 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
if ($user->can($moduleManager::MODULE_PERMISSION_NS . $module->getName())) {
if ($type === 'menu-item') {
$this->merge($module->getMenu());
} elseif ($type === 'dashboard-pane') {
$this->merge($module->getDashboard());
}
}
}
@ -448,11 +444,7 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
'menu-item' => array(
'label' => t('Menu Entry'),
'config' => 'menu'
)/*, // Disabled, until it is able to fully replace the old implementation
'dashlet' => array(
'label' => 'Dashlet',
'config' => 'dashboard'
)*/
)
);
$moduleItemTypes = array();

View File

@ -1,475 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Widget;
use Icinga\Application\Config;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotReadableError;
use Icinga\Exception\ProgrammingError;
use Icinga\Legacy\DashboardConfig;
use Icinga\User;
use Icinga\Web\Navigation\DashboardPane;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard\Dashlet as DashboardDashlet;
use Icinga\Web\Widget\Dashboard\Pane;
/**
* Dashboards display multiple views on a single page
*
* The terminology is as follows:
* - Dashlet: A single view showing a specific url
* - Pane: Aggregates one or more dashlets on one page, displays its title as a tab
* - Dashboard: Shows all panes
*
*/
class Dashboard extends AbstractWidget
{
/**
* An array containing all panes of this dashboard
*
* @var array
*/
private $panes = array();
/**
* The @see Icinga\Web\Widget\Tabs object for displaying displayable panes
*
* @var Tabs
*/
protected $tabs;
/**
* The parameter that will be added to identify panes
*
* @var string
*/
private $tabParam = 'pane';
/**
* @var User
*/
private $user;
/**
* Set the given tab name as active.
*
* @param string $name The tab name to activate
*
*/
public function activate($name)
{
$this->getTabs()->activate($name);
}
/**
* Load Pane items provided by all enabled modules
*
* @return $this
*/
public function load()
{
$navigation = new Navigation();
$navigation->load('dashboard-pane');
$panes = array();
foreach ($navigation as $dashboardPane) {
/** @var DashboardPane $dashboardPane */
$pane = new Pane($dashboardPane->getLabel());
foreach ($dashboardPane->getChildren() as $dashlet) {
$pane->addDashlet($dashlet->getLabel(), $dashlet->getUrl());
}
$panes[] = $pane;
}
$this->mergePanes($panes);
$this->loadUserDashboards($navigation);
return $this;
}
/**
* Create and return a Config object for this dashboard
*
* @return Config
*/
public function getConfig()
{
$output = array();
foreach ($this->panes as $pane) {
if ($pane->isUserWidget()) {
$output[$pane->getName()] = $pane->toArray();
}
foreach ($pane->getDashlets() as $dashlet) {
if ($dashlet->isUserWidget()) {
$output[$pane->getName() . '.' . $dashlet->getName()] = $dashlet->toArray();
}
}
}
return DashboardConfig::fromArray($output)->setConfigFile($this->getConfigFile())->setUser($this->user);
}
/**
* Load user dashboards from all config files that match the username
*/
protected function loadUserDashboards(Navigation $navigation)
{
foreach (DashboardConfig::listConfigFilesForUser($this->user) as $file) {
$this->loadUserDashboardsFromFile($file, $navigation);
}
}
/**
* Load user dashboards from the given config file
*
* @param string $file
*
* @return bool
*/
protected function loadUserDashboardsFromFile($file, Navigation $dashboardNavigation)
{
try {
$config = Config::fromIni($file);
} catch (NotReadableError $e) {
return false;
}
if (! count($config)) {
return false;
}
$panes = array();
$dashlets = array();
foreach ($config as $key => $part) {
if (strpos($key, '.') === false) {
$dashboardPane = $dashboardNavigation->getItem($key);
if ($dashboardPane !== null) {
$key = $dashboardPane->getLabel();
}
if ($this->hasPane($key)) {
$panes[$key] = $this->getPane($key);
} else {
$panes[$key] = new Pane($key);
$panes[$key]->setTitle($part->title);
}
$panes[$key]->setUserWidget();
if ((bool) $part->get('disabled', false) === true) {
$panes[$key]->setDisabled();
}
} else {
list($paneName, $dashletName) = explode('.', $key, 2);
$dashboardPane = $dashboardNavigation->getItem($paneName);
if ($dashboardPane !== null) {
$paneName = $dashboardPane->getLabel();
$dashletItem = $dashboardPane->getChildren()->getItem($dashletName);
if ($dashletItem !== null) {
$dashletName = $dashletItem->getLabel();
}
}
$part->pane = $paneName;
$part->dashlet = $dashletName;
$dashlets[] = $part;
}
}
foreach ($dashlets as $dashletData) {
$pane = null;
if (array_key_exists($dashletData->pane, $panes) === true) {
$pane = $panes[$dashletData->pane];
} elseif (array_key_exists($dashletData->pane, $this->panes) === true) {
$pane = $this->panes[$dashletData->pane];
} else {
continue;
}
$dashlet = new DashboardDashlet(
$dashletData->title,
$dashletData->url,
$pane
);
$dashlet->setName($dashletData->dashlet);
if ((bool) $dashletData->get('disabled', false) === true) {
$dashlet->setDisabled(true);
}
$dashlet->setUserWidget();
$pane->addDashlet($dashlet);
}
$this->mergePanes($panes);
return true;
}
/**
* Merge panes with existing panes
*
* @param array $panes
*
* @return $this
*/
public function mergePanes(array $panes)
{
/** @var $pane Pane */
foreach ($panes as $pane) {
if ($this->hasPane($pane->getName()) === true) {
/** @var $current Pane */
$current = $this->panes[$pane->getName()];
$current->addDashlets($pane->getDashlets());
} else {
$this->panes[$pane->getName()] = $pane;
}
}
return $this;
}
/**
* Return the tab object used to navigate through this dashboard
*
* @return Tabs
*/
public function getTabs()
{
$url = Url::fromPath('dashboard')->getUrlWithout($this->tabParam);
if ($this->tabs === null) {
$this->tabs = new Tabs();
foreach ($this->panes as $key => $pane) {
if ($pane->getDisabled()) {
continue;
}
$this->tabs->add(
$key,
array(
'title' => sprintf(
t('Show %s', 'dashboard.pane.tooltip'),
$pane->getTitle()
),
'label' => $pane->getTitle(),
'url' => clone($url),
'urlParams' => array($this->tabParam => $key)
)
);
}
}
return $this->tabs;
}
/**
* Return all panes of this dashboard
*
* @return array
*/
public function getPanes()
{
return $this->panes;
}
/**
* Creates a new empty pane with the given title
*
* @param string $title
*
* @return $this
*/
public function createPane($title)
{
$pane = new Pane($title);
$pane->setTitle($title);
$this->addPane($pane);
return $this;
}
/**
* Checks if the current dashboard has any panes
*
* @return bool
*/
public function hasPanes()
{
return ! empty($this->panes);
}
/**
* Check if a panel exist
*
* @param string $pane
* @return bool
*/
public function hasPane($pane)
{
return $pane && array_key_exists($pane, $this->panes);
}
/**
* Add a pane object to this dashboard
*
* @param Pane $pane The pane to add
*
* @return $this
*/
public function addPane(Pane $pane)
{
$this->panes[$pane->getName()] = $pane;
return $this;
}
public function removePane($title)
{
if ($this->hasPane($title) === true) {
$pane = $this->getPane($title);
if ($pane->isUserWidget() === true) {
unset($this->panes[$pane->getName()]);
} else {
$pane->setDisabled();
$pane->setUserWidget();
}
} else {
throw new ProgrammingError('Pane not found: ' . $title);
}
}
/**
* Return the pane with the provided name
*
* @param string $name The name of the pane to return
*
* @return Pane The pane or null if no pane with the given name exists
* @throws ProgrammingError
*/
public function getPane($name)
{
if (! array_key_exists($name, $this->panes)) {
throw new ProgrammingError(
'Trying to retrieve invalid dashboard pane "%s"',
$name
);
}
return $this->panes[$name];
}
/**
* Return an array with pane name=>title format used for comboboxes
*
* @return array
*/
public function getPaneKeyTitleArray()
{
$list = array();
foreach ($this->panes as $name => $pane) {
$list[$name] = $pane->getTitle();
}
return $list;
}
/**
* @see Icinga\Web\Widget::render
*/
public function render()
{
if (empty($this->panes)) {
return '';
}
return $this->determineActivePane()->render();
}
/**
* Activates the default pane of this dashboard and returns its name
*
* @return mixed
*/
private function setDefaultPane()
{
$active = null;
foreach ($this->panes as $key => $pane) {
if ($pane->getDisabled() === false) {
$active = $key;
break;
}
}
if ($active !== null) {
$this->activate($active);
}
return $active;
}
/**
* @see determineActivePane()
*/
public function getActivePane()
{
return $this->determineActivePane();
}
/**
* Determine the active pane either by the selected tab or the current request
*
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\ProgrammingError
*
* @return Pane The currently active pane
*/
public function determineActivePane()
{
$active = $this->getTabs()->getActiveName();
if (! $active) {
if ($active = Url::fromRequest()->getParam($this->tabParam)) {
if ($this->hasPane($active)) {
$this->activate($active);
} else {
throw new ProgrammingError(
'Try to get an inexistent pane.'
);
}
} else {
$active = $this->setDefaultPane();
}
}
if (isset($this->panes[$active])) {
return $this->panes[$active];
}
throw new ConfigurationError('Could not determine active pane');
}
/**
* Setter for user object
*
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Getter for user object
*
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* Get config file
*
* @return string
*/
public function getConfigFile()
{
if ($this->user === null) {
throw new ProgrammingError('Can\'t load dashboards. User is not set');
}
return Config::resolvePath('dashboards/' . strtolower($this->user->getUsername()) . '/dashboard.ini');
}
}

View File

@ -1,315 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Widget\Dashboard;
use Icinga\Web\Url;
use Icinga\Data\ConfigObject;
use Icinga\Exception\IcingaException;
/**
* A dashboard pane dashlet
*
* This is the element displaying a specific view in icinga2web
*
*/
class Dashlet extends UserWidget
{
/**
* The url of this Dashlet
*
* @var Url|null
*/
private $url;
private $name;
/**
* The title being displayed on top of the dashlet
* @var
*/
private $title;
/**
* The pane containing this dashlet, needed for the 'remove button'
* @var Pane
*/
private $pane;
/**
* The disabled option is used to "delete" default dashlets provided by modules
*
* @var bool
*/
private $disabled = false;
/**
* The progress label being used
*
* @var string
*/
private $progressLabel;
/**
* The template string used for rendering this widget
*
* @var string
*/
private $template =<<<'EOD'
<div class="container" data-icinga-url="{URL}">
<h1><a href="{FULL_URL}" aria-label="{TOOLTIP}" title="{TOOLTIP}" data-base-target="col1">{TITLE}</a></h1>
<p class="progress-label">{PROGRESS_LABEL}<span>.</span><span>.</span><span>.</span></p>
<noscript>
<div class="iframe-container">
<iframe
src="{IFRAME_URL}"
frameborder="no"
title="{TITLE_PREFIX}{TITLE}">
</iframe>
</div>
</noscript>
</div>
EOD;
/**
* The template string used for rendering this widget in case of an error
*
* @var string
*/
private $errorTemplate = <<<'EOD'
<div class="container">
<h1 title="{TOOLTIP}">{TITLE}</h1>
<p class="error-message">{ERROR_MESSAGE}</p>
</div>
EOD;
/**
* Create a new dashlet displaying the given url in the provided pane
*
* @param string $title The title to use for this dashlet
* @param Url|string $url The url this dashlet uses for displaying information
* @param Pane $pane The pane this Dashlet will be added to
*/
public function __construct($title, $url, Pane $pane)
{
$this->name = $title;
$this->title = $title;
$this->pane = $pane;
$this->url = $url;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
/**
* Retrieve the dashlets title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Retrieve the dashlets url
*
* @return Url|null
*/
public function getUrl()
{
if ($this->url !== null && ! $this->url instanceof Url) {
$this->url = Url::fromPath($this->url);
}
return $this->url;
}
/**
* Set the dashlets URL
*
* @param string|Url $url The url to use, either as an Url object or as a path
*
* @return $this
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Set the disabled property
*
* @param boolean $disabled
*/
public function setDisabled($disabled)
{
$this->disabled = $disabled;
}
/**
* Get the disabled property
*
* @return boolean
*/
public function getDisabled()
{
return $this->disabled;
}
/**
* Set the progress label to use
*
* @param string $label
*
* @return $this
*/
public function setProgressLabel($label)
{
$this->progressLabel = $label;
return $this;
}
/**
* Return the progress label to use
*
* @return string
*/
public function getProgressLabe()
{
if ($this->progressLabel === null) {
return $this->view()->translate('Loading');
}
return $this->progressLabel;
}
/**
* Return this dashlet's structure as array
*
* @return array
*/
public function toArray()
{
$array = array(
'url' => $this->getUrl()->getRelativeUrl(),
'title' => $this->getTitle()
);
if ($this->getDisabled() === true) {
$array['disabled'] = 1;
}
return $array;
}
/**
* @see Widget::render()
*/
public function render()
{
if ($this->disabled === true) {
return '';
}
$view = $this->view();
if (! $this->url) {
$searchTokens = array(
'{TOOLTIP}',
'{TITLE}',
'{ERROR_MESSAGE}'
);
$replaceTokens = array(
sprintf($view->translate('Show %s', 'dashboard.dashlet.tooltip'), $view->escape($this->getTitle())),
$view->escape($this->getTitle()),
$view->escape(
sprintf($view->translate('Cannot create dashboard dashlet "%s" without valid URL'), $this->title)
)
);
return str_replace($searchTokens, $replaceTokens, $this->errorTemplate);
}
$url = $this->getUrl();
$url->setParam('showCompact', true);
$iframeUrl = clone $url;
$iframeUrl->setParam('isIframe');
$searchTokens = array(
'{URL}',
'{IFRAME_URL}',
'{FULL_URL}',
'{TOOLTIP}',
'{TITLE}',
'{TITLE_PREFIX}',
'{PROGRESS_LABEL}'
);
$replaceTokens = array(
$url,
$iframeUrl,
$url->getUrlWithout(['showCompact', 'limit', 'view']),
sprintf($view->translate('Show %s', 'dashboard.dashlet.tooltip'), $view->escape($this->getTitle())),
$view->escape($this->getTitle()),
$view->translate('Dashlet') . ': ',
$this->getProgressLabe()
);
return str_replace($searchTokens, $replaceTokens, $this->template);
}
/**
* Create a @see Dashlet instance from the given Zend config, using the provided title
*
* @param $title The title for this dashlet
* @param ConfigObject $config The configuration defining url, parameters, height, width, etc.
* @param Pane $pane The pane this dashlet belongs to
*
* @return Dashlet A newly created Dashlet for use in the Dashboard
*/
public static function fromIni($title, ConfigObject $config, Pane $pane)
{
$height = null;
$width = null;
$url = $config->get('url');
$parameters = $config->toArray();
unset($parameters['url']); // otherwise there's an url = parameter in the Url
$cmp = new Dashlet($title, Url::fromPath($url, $parameters), $pane);
return $cmp;
}
/**
* @param \Icinga\Web\Widget\Dashboard\Pane $pane
*/
public function setPane(Pane $pane)
{
$this->pane = $pane;
}
/**
* @return \Icinga\Web\Widget\Dashboard\Pane
*/
public function getPane()
{
return $this->pane;
}
}

View File

@ -1,335 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Widget\Dashboard;
use Icinga\Data\ConfigObject;
use Icinga\Web\Widget\AbstractWidget;
use Icinga\Exception\ProgrammingError;
use Icinga\Exception\ConfigurationError;
/**
* A pane, displaying different Dashboard dashlets
*/
class Pane extends UserWidget
{
/**
* The name of this pane, as defined in the ini file
*
* @var string
*/
private $name;
/**
* The title of this pane, as displayed in the dashboard tabs
*
* @var string
*/
private $title;
/**
* An array of @see Dashlets that are displayed in this pane
*
* @var array
*/
private $dashlets = array();
/**
* Disabled flag of a pane
*
* @var bool
*/
private $disabled = false;
/**
* Create a new pane
*
* @param string $name The pane to create
*/
public function __construct($name)
{
$this->name = $name;
$this->title = $name;
}
/**
* Set the name of this pane
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Returns the name of this pane
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns the title of this pane
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Overwrite the title of this pane
*
* @param string $title The new title to use for this pane
*
* @return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Return true if a dashlet with the given title exists in this pane
*
* @param string $title The title of the dashlet to check for existence
*
* @return bool
*/
public function hasDashlet($title)
{
return array_key_exists($title, $this->dashlets);
}
/**
* Checks if the current pane has any dashlets
*
* @return bool
*/
public function hasDashlets()
{
return ! empty($this->dashlets);
}
/**
* Return a dashlet with the given name if existing
*
* @param string $title The title of the dashlet to return
*
* @return Dashlet The dashlet with the given title
* @throws ProgrammingError If the dashlet doesn't exist
*/
public function getDashlet($title)
{
if ($this->hasDashlet($title)) {
return $this->dashlets[$title];
}
throw new ProgrammingError(
'Trying to access invalid dashlet: %s',
$title
);
}
/**
* Removes the dashlet with the given title if it exists in this pane
*
* @param string $title The pane
* @return Pane $this
*/
public function removeDashlet($title)
{
if ($this->hasDashlet($title)) {
$dashlet = $this->getDashlet($title);
if ($dashlet->isUserWidget() === true) {
unset($this->dashlets[$title]);
} else {
$dashlet->setDisabled(true);
$dashlet->setUserWidget();
}
} else {
throw new ProgrammingError('Dashlet does not exist: ' . $title);
}
return $this;
}
/**
* Removes all or a given list of dashlets from this pane
*
* @param array $dashlets Optional list of dashlet titles
* @return Pane $this
*/
public function removeDashlets(array $dashlets = null)
{
if ($dashlets === null) {
$this->dashlets = array();
} else {
foreach ($dashlets as $dashlet) {
$this->removeDashlet($dashlet);
}
}
return $this;
}
/**
* Return all dashlets added at this pane
*
* @return array
*/
public function getDashlets()
{
return $this->dashlets;
}
/**
* @see Widget::render
*/
public function render()
{
$dashlets = array_filter(
$this->dashlets,
function ($e) {
return ! $e->getDisabled();
}
);
return implode("\n", $dashlets) . "\n";
}
/**
* Create, add and return a new dashlet
*
* @param string $title
* @param string $url
*
* @return Dashlet
*/
public function createDashlet($title, $url = null)
{
$dashlet = new Dashlet($title, $url, $this);
$this->addDashlet($dashlet);
return $dashlet;
}
/**
* Add a dashlet to this pane, optionally creating it if $dashlet is a string
*
* @param string|Dashlet $dashlet The dashlet object or title
* (if a new dashlet will be created)
* @param string|null $url An Url to be used when dashlet is a string
*
* @return $this
* @throws \Icinga\Exception\ConfigurationError
*/
public function addDashlet($dashlet, $url = null)
{
if ($dashlet instanceof Dashlet) {
$this->dashlets[$dashlet->getName()] = $dashlet;
} elseif (is_string($dashlet) && $url !== null) {
$this->createDashlet($dashlet, $url);
} else {
throw new ConfigurationError('Invalid dashlet added: %s', $dashlet);
}
return $this;
}
/**
* Add new dashlets to existing dashlets
*
* @param array $dashlets
* @return $this
*/
public function addDashlets(array $dashlets)
{
/* @var $dashlet Dashlet */
foreach ($dashlets as $dashlet) {
if (array_key_exists($dashlet->getName(), $this->dashlets)) {
if (preg_match('/_(\d+)$/', $dashlet->getName(), $m)) {
$name = preg_replace('/_\d+$/', $m[1]++, $dashlet->getName());
} else {
$name = $dashlet->getName() . '_2';
}
$this->dashlets[$name] = $dashlet;
} else {
$this->dashlets[$dashlet->getName()] = $dashlet;
}
}
return $this;
}
/**
* Add a dashlet to the current pane
*
* @param $title
* @param $url
* @return Dashlet
*
* @see addDashlet()
*/
public function add($title, $url = null)
{
$this->addDashlet($title, $url);
return $this->dashlets[$title];
}
/**
* Return the this pane's structure as array
*
* @return array
*/
public function toArray()
{
$pane = array(
'title' => $this->getTitle(),
);
if ($this->getDisabled() === true) {
$pane['disabled'] = 1;
}
return $pane;
}
/**
* Create a new pane with the title $title from the given configuration
*
* @param $title The title for this pane
* @param ConfigObject $config The configuration to use for setup
*
* @return Pane
*/
public static function fromIni($title, ConfigObject $config)
{
$pane = new Pane($title);
if ($config->get('title', false)) {
$pane->setTitle($config->get('title'));
}
return $pane;
}
/**
* Setter for disabled
*
* @param boolean $disabled
*/
public function setDisabled($disabled = true)
{
$this->disabled = (bool) $disabled;
}
/**
* Getter for disabled
*
* @return boolean
*/
public function getDisabled()
{
return $this->disabled;
}
}

View File

@ -1,36 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Widget\Dashboard;
use Icinga\Web\Widget\AbstractWidget;
abstract class UserWidget extends AbstractWidget
{
/**
* Flag if widget is created by an user
*
* @var bool
*/
protected $userWidget = false;
/**
* Set the user widget flag
*
* @param boolean $userWidget
*/
public function setUserWidget($userWidget = true)
{
$this->userWidget = (bool) $userWidget;
}
/**
* Getter for user widget flag
*
* @return boolean
*/
public function isUserWidget()
{
return $this->userWidget;
}
}