From 1000b4661f70f65986a1798805f232dc089ed041 Mon Sep 17 00:00:00 2001
From: Marius Hein
Date: Tue, 11 Nov 2014 11:51:18 +0100
Subject: [PATCH 01/31] AddToDashboard: Remove controller logic
refs #4537
---
.../controllers/DashboardController.php | 24 +------------------
1 file changed, 1 insertion(+), 23 deletions(-)
diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php
index d357231ca..fb8da0f5a 100644
--- a/application/controllers/DashboardController.php
+++ b/application/controllers/DashboardController.php
@@ -80,30 +80,8 @@ class DashboardController extends ActionController
'url' => Url::fromRequest()
)
)->activate('addurl');
-
$form = new AddUrlForm();
- $request = $this->getRequest();
- if ($request->isPost()) {
- if ($form->isValid($request->getPost()) && $form->isSubmitted()) {
- $dashboard = $this->getDashboard();
- $dashboard->setComponentUrl(
- $form->getValue('pane'),
- $form->getValue('component'),
- ltrim($form->getValue('url'), '/')
- );
-
- $configFile = Config::app('dashboard/dashboard')->getConfigFile();
- if ($this->writeConfiguration(new Config($dashboard->toArray()), $configFile)) {
- $this->redirectNow(Url::fromPath('dashboard', array('pane' => $form->getValue('pane'))));
- } else {
- $this->render('showConfiguration');
- return;
- }
- }
- } else {
- $form->create()->setDefault('url', htmlspecialchars_decode($request->getParam('url', '')));
- }
-
+ $form->handleRequest();
$this->view->form = $form;
}
From 5f9d394bbef0fd74fcf50148ff1c9dd0b33edae7 Mon Sep 17 00:00:00 2001
From: Marius Hein
Date: Tue, 11 Nov 2014 11:51:58 +0100
Subject: [PATCH 02/31] AddToDashboard: Form layout and template
refa #4537
---
application/forms/Dashboard/AddUrlForm.php | 108 +++++++++++++-----
.../views/scripts/dashboard/addurl.phtml | 8 +-
2 files changed, 82 insertions(+), 34 deletions(-)
diff --git a/application/forms/Dashboard/AddUrlForm.php b/application/forms/Dashboard/AddUrlForm.php
index 7f5a217d1..6ca5c23dd 100644
--- a/application/forms/Dashboard/AddUrlForm.php
+++ b/application/forms/Dashboard/AddUrlForm.php
@@ -7,6 +7,7 @@ namespace Icinga\Forms\Dashboard;
use Icinga\Application\Config;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form;
+use Icinga\Web\Request;
/**
* Form to add an url a dashboard pane
@@ -23,35 +24,49 @@ class AddUrlForm extends Form
}
/**
+ * Build AddUrl form elements
+ *
* @see Form::createElements()
*/
public function createElements(array $formData)
{
+ $paneSelectionValues = $this->getDashboardPaneSelectionValues();
+ $groupElements = array();
+
$this->addElement(
'text',
'url',
array(
- 'required' => true,
- 'label' => t('Url'),
- 'helptext' => t('The url being loaded in the dashlet')
+ 'required' => true,
+ 'label' => t('Url'),
+ 'description' =>
+ t('Enter url being loaded in the dashlet. You can paste the full URL, including filters.')
+ )
+ );
+ $this->addElement(
+ 'text',
+ 'component',
+ array(
+ 'required' => true,
+ 'label' => t('Dashlet Title'),
+ 'description' => t('Enter a title for the dashlet.')
)
);
-
- $paneSelectionValues = $this->getDashboardPaneSelectionValues();
if (empty($paneSelectionValues) ||
((isset($formData['create_new_pane']) && $formData['create_new_pane'] != false) &&
(false === isset($formData['use_existing_dashboard']) || $formData['use_existing_dashboard'] != true))
) {
- $this->addElement(
+ $groupElements[] = $this->createElement(
'text',
'pane',
array(
- 'required' => true,
- 'label' => t("The New Pane's Title"),
- 'style' => 'display: inline-block'
+ 'required' => true,
+ 'label' => t("New Pane Title"),
+ 'description' =>
+ t('Enter a title for the new pane.')
)
);
- $this->addElement( // Prevent the button from being displayed again on validation errors
+ $groupElements[] = $this->createElement( // Prevent the button from being displayed again on validation errors
'hidden',
'create_new_pane',
array(
@@ -59,45 +74,62 @@ class AddUrlForm extends Form
)
);
if (false === empty($paneSelectionValues)) {
- $this->addElement(
+ $buttonExistingPane = $this->createElement(
'submit',
'use_existing_dashboard',
array(
- 'ignore' => true,
- 'label' => t('Use An Existing Pane'),
- 'style' => 'display: inline-block'
+ 'ignore' => true,
+ 'label' => t('Use An Existing Pane'),
+ 'description' =>
+ t('Click on the button to add the dashlet to an existing pane on your dashboard.')
)
);
+ $buttonExistingPane->removeDecorator('Label');
+ $groupElements[] = $buttonExistingPane;
}
} else {
- $this->addElement(
+ $groupElements[] = $this->createElement(
'select',
'pane',
array(
'required' => true,
'label' => t('Pane'),
- 'style' => 'display: inline-block;',
- 'multiOptions' => $paneSelectionValues
+ 'multiOptions' => $paneSelectionValues,
+ 'description' =>
+ t('Select a pane you want to add the dashlet.')
)
);
- $this->addElement(
+ $buttonNewPane = $this->createElement(
'submit',
'create_new_pane',
array(
- 'ignore' => true,
- 'label' => t('Create A New Pane'),
- 'style' => 'display: inline-block'
+ 'ignore' => true,
+ 'label' => t('Create A New Pane'),
+ 'description' =>
+ t('Click on the button if you want to add the dashlet to a new pane on the dashboard.')
)
);
+ $buttonNewPane->removeDecorator('Label');
+ $groupElements[] = $buttonNewPane;
}
-
- $this->addElement(
- 'text',
- 'component',
+ $this->addDisplayGroup(
+ $groupElements,
+ 'pane_group',
array(
- 'required' => true,
- 'label' => t('Title'),
- 'helptext' => t('The title for the dashlet')
+ 'legend' => t('Pane'),
+ 'description' => t(
+ 'Decide if you want add the dashlet to an existing pane'
+ . ' or create a new pane. Have a look on the button below.'
+ ),
+ 'decorators' => array(
+ 'FormElements',
+ array('HtmlTag', array('tag' => 'div', 'class' => 'control-group')),
+ array(
+ 'Description',
+ array('tag' => 'span', 'class' => 'description', 'placement' => 'prepend')
+ ),
+ 'Fieldset'
+ )
)
);
}
@@ -113,4 +145,24 @@ class AddUrlForm extends Form
$dashboard->readConfig(Config::app('dashboard/dashboard'));
return $dashboard->getPaneKeyTitleArray();
}
+
+ /**
+ * Adjust preferences and persist them
+ *
+ * @see Form::onSuccess()
+ */
+ public function onSuccess(Request $request)
+ {
+ return false;
+ }
+
+ /**
+ * Populate data if any
+ *
+ * @see Form::onRequest()
+ */
+ public function onRequest(Request $request)
+ {
+
+ }
}
diff --git a/application/views/scripts/dashboard/addurl.phtml b/application/views/scripts/dashboard/addurl.phtml
index c2d3f7e6c..9c41a7713 100644
--- a/application/views/scripts/dashboard/addurl.phtml
+++ b/application/views/scripts/dashboard/addurl.phtml
@@ -3,10 +3,6 @@
-
= $this->escape($this->translate('This feature is deactivated at the moment.')); ?>
-
- =
- $this->escape($this->translate('Please have a little patience, we are hard working on it, take a look at icingaweb2 issues.'));
- ?>
-
+
= t('Add URL to Dashboard'); ?>
+ = $this->form; ?>
\ No newline at end of file
From af799d42dc28bbce5989fbbaf134c2142d31bf42 Mon Sep 17 00:00:00 2001
From: Marius Hein
Date: Tue, 11 Nov 2014 14:44:38 +0100
Subject: [PATCH 03/31] Widget/Dashboard: Cleanup object and remove unused
stuff
refs #4537
---
.../controllers/DashboardController.php | 79 +------
application/forms/Dashboard/AddUrlForm.php | 63 +++++-
.../dashboard/show-configuration.phtml | 28 ---
library/Icinga/Web/Widget/Dashboard.php | 199 +++++++-----------
4 files changed, 133 insertions(+), 236 deletions(-)
delete mode 100644 application/views/scripts/dashboard/show-configuration.phtml
diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php
index fb8da0f5a..bf94e26af 100644
--- a/application/controllers/DashboardController.php
+++ b/application/controllers/DashboardController.php
@@ -20,54 +20,6 @@ use Icinga\Web\Widget\Dashboard;
*/
class DashboardController extends ActionController
{
- /**
- * Default configuration
- */
- const DEFAULT_CONFIG = 'dashboard/dashboard';
-
- /**
- * Retrieve a dashboard from the provided config
- *
- * @param string $config The config to read the dashboard from, or 'dashboard/dashboard' if none is given
- *
- * @return \Icinga\Web\Widget\Dashboard
- */
- private function getDashboard($config = self::DEFAULT_CONFIG)
- {
- $dashboard = new Dashboard();
- try {
- $dashboardConfig = Config::app($config);
- if (count($dashboardConfig) === 0) {
- return null;
- }
- $dashboard->readConfig($dashboardConfig);
- } catch (NotReadableError $e) {
- Logger::error(new IcingaException('Cannot load dashboard configuration. An exception was thrown:', $e));
- return null;
- }
- return $dashboard;
- }
-
- /**
- * Remove a component from the pane identified by the 'pane' parameter
- */
- public function removecomponentAction()
- {
- $pane = $this->_getParam('pane');
- $dashboard = $this->getDashboard();
- try {
- $dashboard->removeComponent(
- $pane,
- $this->_getParam('component')
- )->store();
- $this->redirectNow(Url::fromPath('dashboard', array('pane' => $pane)));
- } catch (ConfigurationError $exc ) {
- $this->_helper->viewRenderer('show_configuration');
- $this->view->exceptionMessage = $exc->getMessage();
- $this->view->iniConfigurationString = $dashboard->toIni();
- }
- }
-
/**
* Display the form for adding new components or add the new component if submitted
*/
@@ -93,7 +45,9 @@ class DashboardController extends ActionController
*/
public function indexAction()
{
- $dashboard = Dashboard::load();
+ $dashboard = new Dashboard();
+ $dashboard->setUser($this->getRequest()->getUser());
+ $dashboard->load();
if (! $dashboard->hasPanes()) {
$this->view->title = 'Dashboard';
@@ -103,8 +57,6 @@ class DashboardController extends ActionController
$dashboard->activate($pane);
}
- $this->view->configPath = Config::resolvePath(self::DEFAULT_CONFIG);
-
if ($dashboard === null) {
$this->view->title = 'Dashboard';
} else {
@@ -125,29 +77,4 @@ class DashboardController extends ActionController
}
}
}
-
- /**
- * Store the given configuration as INI file
- *
- * @param Config $config The configuration to store
- * @param string $target The path where to store the configuration
- *
- * @return bool Whether the configuartion has been successfully stored
- */
- protected function writeConfiguration(Config $config, $target)
- {
- $writer = new IniWriter(array('config' => $config, 'filename' => $target));
-
- try {
- $writer->write();
- } catch (Exception $e) {
- Logger::error(new ConfiguationError("Cannot write dashboard to $target", 0, $e));
- $this->view->configString = $writer->render();
- $this->view->errorMessage = $e->getMessage();
- $this->view->filePath = $target;
- return false;
- }
-
- return true;
- }
}
diff --git a/application/forms/Dashboard/AddUrlForm.php b/application/forms/Dashboard/AddUrlForm.php
index 6ca5c23dd..b881ad237 100644
--- a/application/forms/Dashboard/AddUrlForm.php
+++ b/application/forms/Dashboard/AddUrlForm.php
@@ -5,6 +5,7 @@
namespace Icinga\Forms\Dashboard;
use Icinga\Application\Config;
+use Icinga\File\Ini\IniWriter;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form;
use Icinga\Web\Request;
@@ -14,6 +15,13 @@ use Icinga\Web\Request;
*/
class AddUrlForm extends Form
{
+ /**
+ * Config file name
+ *
+ * @var string
+ */
+ private $configFile = 'dashboard/dashboard';
+
/**
* Initialize this form
*/
@@ -134,16 +142,42 @@ class AddUrlForm extends Form
);
}
+ /**
+ * Create a dashboard object
+ *
+ * @return Dashboard
+ */
+ private function createDashboard()
+ {
+ $dashboard = new Dashboard();
+ $dashboard->readConfig(Config::app($this->getConfigFile()));
+ return $dashboard;
+ }
+
/**
* Return the names and titles of the available dashboard panes as key-value array
*
* @return array
*/
- protected function getDashboardPaneSelectionValues()
+ private function getDashboardPaneSelectionValues()
{
- $dashboard = new Dashboard();
- $dashboard->readConfig(Config::app('dashboard/dashboard'));
- return $dashboard->getPaneKeyTitleArray();
+ return $this->createDashboard()->getPaneKeyTitleArray();
+ }
+
+ /**
+ * @param string $configFile
+ */
+ public function setConfigFile($configFile)
+ {
+ $this->configFile = $configFile;
+ }
+
+ /**
+ * @return string
+ */
+ public function getConfigFile()
+ {
+ return $this->configFile;
}
/**
@@ -153,6 +187,22 @@ class AddUrlForm extends Form
*/
public function onSuccess(Request $request)
{
+ $dashboard = $this->createDashboard();
+ $dashboard->setComponentUrl(
+ $this->getValue('pane'),
+ $this->getValue('component'),
+ ltrim($this->getValue('url'), '/')
+ );
+ /*
+ $writer = new IniWriter(
+ array(
+ 'config' => new Config($dashboard->toArray()),
+ 'filename' => $dashboard->getConfig()->getConfigFile()
+ )
+ );
+
+ $writer->write();
+ */
return false;
}
@@ -163,6 +213,9 @@ class AddUrlForm extends Form
*/
public function onRequest(Request $request)
{
-
+ $data = array(
+ 'url' => $request->getParam('url')
+ );
+ $this->populate($data);
}
}
diff --git a/application/views/scripts/dashboard/show-configuration.phtml b/application/views/scripts/dashboard/show-configuration.phtml
deleted file mode 100644
index 7cdce496c..000000000
--- a/application/views/scripts/dashboard/show-configuration.phtml
+++ /dev/null
@@ -1,28 +0,0 @@
-
-
-
{{WARNING_ICON}}Saving Dashboard Failed
-
-
- Your dashboard couldn't be stored (error: "= $this->exceptionMessage; ?>"). This could have one or more
- of the following reasons:
-
-
-
You don't have permissions to write to the dashboard file
-
Something went wrong while writing the file
-
There's an application error preventing you from persisting the configuration
-
-
-
-
- Details can be seen in your application log (if you don't have access to this file, call your administrator in this case).
-
- In case you can access the configuration file (config/dashboard/dashboard.ini) by yourself, you can open it and
- insert the config manually:
-
@@ -179,7 +179,6 @@ EOD;
'{URL}',
'{IFRAME_URL}',
'{FULL_URL}',
- '{REMOVE_BTN}',
'{TITLE}',
'{REMOVE}'
);
@@ -188,9 +187,8 @@ EOD;
$url,
$iframeUrl,
$url->getUrlWithout(array('view', 'limit')),
- $this->getRemoveForm($view),
$view->escape($this->getTitle()),
- $this->getRemoveForm()
+ $this->getRemoveLink()
);
return str_replace($searchTokens, $replaceTokens, $this->template);
@@ -201,32 +199,13 @@ EOD;
*
* @return string The html representation of the form
*/
- protected function getRemoveForm()
+ protected function getRemoveLink()
{
- // TODO: temporarily disabled, should point to a form asking for confirmal
- return '';
- $removeUrl = Url::fromPath(
- '/dashboard/removecomponent',
- array(
- 'pane' => $this->pane->getName(),
- 'component' => $this->getTitle()
- )
+ return sprintf(
+ '%s',
+ Url::fromRequest(array('remove' => $this->getTitle())),
+ t('Remove')
);
- $form = new Form();
- $form->setMethod('POST');
- $form->setAttrib('class', 'inline');
- $form->setAction($removeUrl);
- $form->addElement(
- new Zend_Form_Element_Button(
- 'remove_pane_btn',
- array(
- 'class'=> 'link-like pull-right',
- 'type' => 'submit',
- 'label' => 'x'
- )
- )
- );
- return $form;
}
/**
diff --git a/library/Icinga/Web/Widget/Dashboard/Pane.php b/library/Icinga/Web/Widget/Dashboard/Pane.php
index 8119eaf66..26cd46e44 100644
--- a/library/Icinga/Web/Widget/Dashboard/Pane.php
+++ b/library/Icinga/Web/Widget/Dashboard/Pane.php
@@ -130,7 +130,13 @@ class Pane extends UserWidget
public function removeComponent($title)
{
if ($this->hasComponent($title)) {
- unset($this->components[$title]);
+ $component = $this->getComponent($title);
+ if ($component->isUserWidget() === true) {
+ unset($this->components[$title]);
+ } else {
+ $component->setUserWidget();
+ $component->setDisabled(true);
+ }
}
return $this;
}
From bec0085683558b569316218a8a49b764b2fdabb3 Mon Sep 17 00:00:00 2001
From: Marius Hein
Date: Wed, 12 Nov 2014 09:22:39 +0100
Subject: [PATCH 08/31] Dashboard: Add urls to any dashboard (form)
refs #4537
---
.../controllers/DashboardController.php | 7 +
application/forms/Dashboard/AddUrlForm.php | 120 ++++++++++--------
library/Icinga/Web/Widget/Dashboard.php | 14 ++
3 files changed, 87 insertions(+), 54 deletions(-)
diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php
index fd06e62ea..6e4dd8cc2 100644
--- a/application/controllers/DashboardController.php
+++ b/application/controllers/DashboardController.php
@@ -32,7 +32,14 @@ class DashboardController extends ActionController
'url' => Url::fromRequest()
)
)->activate('addurl');
+
$form = new AddUrlForm();
+
+ $dashboard = new Dashboard();
+ $dashboard->setUser($this->getRequest()->getUser());
+ $dashboard->load();
+ $form->setDashboard($dashboard);
+ $form->setRedirectUrl($this->getParam('url'));
$form->handleRequest();
$this->view->form = $form;
}
diff --git a/application/forms/Dashboard/AddUrlForm.php b/application/forms/Dashboard/AddUrlForm.php
index b881ad237..f67c92a42 100644
--- a/application/forms/Dashboard/AddUrlForm.php
+++ b/application/forms/Dashboard/AddUrlForm.php
@@ -5,7 +5,9 @@
namespace Icinga\Forms\Dashboard;
use Icinga\Application\Config;
+use Icinga\Exception\ProgrammingError;
use Icinga\File\Ini\IniWriter;
+use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form;
use Icinga\Web\Request;
@@ -22,6 +24,11 @@ class AddUrlForm extends Form
*/
private $configFile = 'dashboard/dashboard';
+ /**
+ * @var Dashboard
+ */
+ private $dashboard;
+
/**
* Initialize this form
*/
@@ -38,7 +45,12 @@ class AddUrlForm extends Form
*/
public function createElements(array $formData)
{
- $paneSelectionValues = $this->getDashboardPaneSelectionValues();
+ $paneSelectionValues = array();
+
+ if ($this->dashboard !== null) {
+ $paneSelectionValues = $this->dashboard->getPaneKeyTitleArray();
+ }
+
$groupElements = array();
$this->addElement(
@@ -142,44 +154,6 @@ class AddUrlForm extends Form
);
}
- /**
- * Create a dashboard object
- *
- * @return Dashboard
- */
- private function createDashboard()
- {
- $dashboard = new Dashboard();
- $dashboard->readConfig(Config::app($this->getConfigFile()));
- return $dashboard;
- }
-
- /**
- * Return the names and titles of the available dashboard panes as key-value array
- *
- * @return array
- */
- private function getDashboardPaneSelectionValues()
- {
- return $this->createDashboard()->getPaneKeyTitleArray();
- }
-
- /**
- * @param string $configFile
- */
- public function setConfigFile($configFile)
- {
- $this->configFile = $configFile;
- }
-
- /**
- * @return string
- */
- public function getConfigFile()
- {
- return $this->configFile;
- }
-
/**
* Adjust preferences and persist them
*
@@ -187,23 +161,34 @@ class AddUrlForm extends Form
*/
public function onSuccess(Request $request)
{
- $dashboard = $this->createDashboard();
- $dashboard->setComponentUrl(
- $this->getValue('pane'),
+ $pane = null;
+
+ if ($this->dashboard === null) {
+ throw new ProgrammingError('Dashboard is not set, can not write values');
+ }
+
+ try {
+ $pane = $this->dashboard->getPane($this->getValue('pane'));
+ } catch (ProgrammingError $e) {
+ $pane = new Dashboard\Pane($this->getValue('pane'));
+ $pane->setUserWidget();
+ $this->dashboard->addPane($pane);
+ }
+
+ $component = new Dashboard\Component(
$this->getValue('component'),
- ltrim($this->getValue('url'), '/')
- );
- /*
- $writer = new IniWriter(
- array(
- 'config' => new Config($dashboard->toArray()),
- 'filename' => $dashboard->getConfig()->getConfigFile()
- )
+ $this->getValue('url'),
+ $pane
);
- $writer->write();
- */
- return false;
+ $component->setUserWidget();
+
+ $pane->addComponent($component);
+
+ $this->dashboard->write();
+
+
+ return true;
}
/**
@@ -213,9 +198,36 @@ class AddUrlForm extends Form
*/
public function onRequest(Request $request)
{
+ // TODO(mh): Im not sure if this is the right place for that
+ $url = $this->getValue('url');
+ if (! $url) {
+ $url = $request->getParam('url');
+ }
+
+ if (! $url) {
+ return;
+ }
+
$data = array(
- 'url' => $request->getParam('url')
+ 'url' => urldecode(Url::fromPath($url)->getPath())
);
+
$this->populate($data);
}
+
+ /**
+ * @param Dashboard $dashboard
+ */
+ public function setDashboard(Dashboard $dashboard)
+ {
+ $this->dashboard = $dashboard;
+ }
+
+ /**
+ * @return Dashboard
+ */
+ public function getDashboard()
+ {
+ return $this->dashboard;
+ }
}
diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php
index f8cc54625..1a049ef59 100644
--- a/library/Icinga/Web/Widget/Dashboard.php
+++ b/library/Icinga/Web/Widget/Dashboard.php
@@ -275,6 +275,20 @@ class Dashboard extends AbstractWidget
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
*/
From 89b36f79b2e5ab48761ce3f6fc5a292538405465 Mon Sep 17 00:00:00 2001
From: Marius Hein
Date: Wed, 12 Nov 2014 12:02:05 +0100
Subject: [PATCH 09/31] Dashboard: Reenable generic add link in tab bar
refs #4537
---
application/controllers/DashboardController.php | 6 ------
1 file changed, 6 deletions(-)
diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php
index 6e4dd8cc2..2c2b7d2fd 100644
--- a/application/controllers/DashboardController.php
+++ b/application/controllers/DashboardController.php
@@ -63,20 +63,16 @@ class DashboardController extends ActionController
$pane = $this->_getParam('pane');
$dashboard->activate($pane);
}
-
if ($dashboard === null) {
$this->view->title = 'Dashboard';
} else {
$this->view->title = $dashboard->getActivePane()->getTitle() . ' :: Dashboard';
$this->view->tabs = $dashboard->getTabs();
-
if ($this->hasParam('remove')) {
$dashboard->getActivePane()->removeComponent($this->getParam('remove'));
$dashboard->write();
$this->redirectNow(URL::fromRequest()->remove('remove'));
}
-
- /* Temporarily removed
$this->view->tabs->add(
'Add',
array(
@@ -84,8 +80,6 @@ class DashboardController extends ActionController
'url' => Url::fromPath('dashboard/addurl')
)
);
- */
-
$this->view->dashboard = $dashboard;
}
}
From 47414f352839b2cfcd34042fbdb0e885da692694 Mon Sep 17 00:00:00 2001
From: Eric Lippmann
Date: Tue, 4 Nov 2014 16:15:06 +0100
Subject: [PATCH 10/31] Introduce Form::setOnSuccess() in favor of overriding
the constructor
Zend_Form uses setters for options if a respective setter method exists.
It is not necessary to override the constructor for introducing new options.
---
library/Icinga/Web/Form.php | 140 ++++++++++++------------------------
1 file changed, 47 insertions(+), 93 deletions(-)
diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php
index 20484c33e..787e8dc27 100644
--- a/library/Icinga/Web/Form.php
+++ b/library/Icinga/Web/Form.php
@@ -11,7 +11,6 @@ use Zend_View_Interface;
use Icinga\Application\Icinga;
use Icinga\Web\Form\Decorator\NoScriptApply;
use Icinga\Web\Form\Element\CsrfCounterMeasure;
-use Icinga\Web\Form\FormElement;
/**
* Base class for forms providing CSRF protection, confirmation logic and auto submission
@@ -37,13 +36,6 @@ class Form extends Zend_Form
*/
protected $created = false;
- /**
- * The request associated with this form
- *
- * @var Request
- */
- protected $request;
-
/**
* The callback to call instead of Form::onSuccess()
*
@@ -108,38 +100,31 @@ class Form extends Zend_Form
* @var array
*/
public static $defaultElementDecorators = array(
- array('ViewHelper', array('separator' => '')),
- array('Errors', array('separator' => '')),
- array('Description', array('tag' => 'span', 'class' => 'description', 'separator' => '')),
- array('Label', array('separator' => '')),
- array('HtmlTag', array('tag' => 'div', 'class' => 'element'))
+ 'ViewHelper',
+ 'Errors',
+ array('Description', array('tag' => 'span', 'class' => 'description')),
+ 'Label',
+ array('HtmlTag', array('tag' => 'div'))
);
/**
- * Create a new form
+ * Set a callback that is called instead of this form's onSuccess method
*
- * Accepts an additional option `onSuccess' which is a callback that is called instead of this
- * form's method. It is called using the following signature: (Form $form).
+ * It is called using the following signature: (Request $request, Form $form).
*
- * @see Zend_Form::__construct()
+ * @param callable $onSuccess Callback
*
- * @throws LogicException In case `onSuccess' is not callable
+ * @return $this
+ *
+ * @throws LogicException If the callback is not callable
*/
- public function __construct($options = null)
+ public function setOnSuccess($onSuccess)
{
- if (is_array($options) && isset($options['onSuccess'])) {
- $this->onSuccess = $options['onSuccess'];
- unset($options['onSuccess']);
- } elseif (isset($options->onSuccess)) {
- $this->onSuccess = $options->onSuccess;
- unset($options->onSuccess);
- }
-
- if ($this->onSuccess !== null && false === is_callable($this->onSuccess)) {
+ if (! is_callable($onSuccess)) {
throw new LogicException('The option `onSuccess\' is not callable');
}
-
- parent::__construct($options);
+ $this->onSuccess = $onSuccess;
+ return $this;
}
/**
@@ -363,9 +348,11 @@ class Form extends Zend_Form
*
* Intended to be implemented by concrete form classes. The base implementation returns always FALSE.
*
+ * @param Request $request The valid request used to process this form
+ *
* @return null|bool Return FALSE in case no redirect should take place
*/
- public function onSuccess()
+ public function onSuccess(Request $request)
{
return false;
}
@@ -374,8 +361,10 @@ class Form extends Zend_Form
* Perform actions when no form dependent data was sent
*
* Intended to be implemented by concrete form classes.
+ *
+ * @param Request $request The current request
*/
- public function onRequest()
+ public function onRequest(Request $request)
{
}
@@ -441,8 +430,8 @@ class Form extends Zend_Form
* `disableLoadDefaultDecorators' option to any other value than `true'. For loading custom element decorators use
* the 'decorators' option.
*
- * @param string $type The type of the element
- * @param string $name The name of the element
+ * @param string $type String element type
+ * @param string $name The name of the element to add
* @param mixed $options The options for the element
*
* @return Zend_Form_Element
@@ -464,20 +453,10 @@ class Form extends Zend_Form
$options = array('decorators' => static::$defaultElementDecorators);
}
- if (($el = $this->createIcingaFormElement($type, $name, $options)) === null) {
- $el = parent::createElement($type, $name, $options);
- }
+ $el = parent::createElement($type, $name, $options);
if ($el && $el->getAttrib('autosubmit')) {
- $noScript = new NoScriptApply(); // Non-JS environments
- $decorators = $el->getDecorators();
- $pos = array_search('Zend_Form_Decorator_ViewHelper', array_keys($decorators)) + 1;
- $el->setDecorators(
- array_slice($decorators, 0, $pos, true)
- + array(get_class($noScript) => $noScript)
- + array_slice($decorators, $pos, count($decorators) - $pos, true)
- );
-
+ $el->addDecorator(new NoScriptApply()); // Non-JS environments
$class = $el->getAttrib('class');
if (is_array($class)) {
$class[] = 'autosubmit';
@@ -487,7 +466,6 @@ class Form extends Zend_Form
$class .= ' autosubmit';
}
$el->setAttrib('class', $class); // JS environments
-
unset($el->autosubmit);
}
@@ -555,17 +533,15 @@ class Form extends Zend_Form
{
if ($request === null) {
$request = $this->getRequest();
- } else {
- $this->request = $request;
}
- $formData = $this->getRequestData();
+ $formData = $this->getRequestData($request);
if ($this->getUidDisabled() || $this->wasSent($formData)) {
$this->populate($formData); // Necessary to get isSubmitted() to work
if (! $this->getSubmitLabel() || $this->isSubmitted()) {
if ($this->isValid($formData)
- && (($this->onSuccess !== null && false !== call_user_func($this->onSuccess, $this))
- || ($this->onSuccess === null && false !== $this->onSuccess()))) {
+ && (($this->onSuccess !== null && false !== call_user_func($this->onSuccess, $request, $this))
+ || ($this->onSuccess === null && false !== $this->onSuccess($request)))) {
$this->getResponse()->redirectAndExit($this->getRedirectUrl());
}
} else {
@@ -573,7 +549,7 @@ class Form extends Zend_Form
$this->isValidPartial($formData);
}
} else {
- $this->onRequest();
+ $this->onRequest($request);
}
return $request;
@@ -699,19 +675,29 @@ class Form extends Zend_Form
}
/**
- * Return the request associated with this form
+ * Return the request data based on this form's request method
*
- * Returns the global request if none has been set for this form yet.
+ * @param Request $request The request to fetch the data from
+ *
+ * @return array
+ */
+ public function getRequestData(Request $request)
+ {
+ if (strtolower($request->getMethod()) === $this->getMethod()) {
+ return $request->{'get' . ($request->isPost() ? 'Post' : 'Query')}();
+ }
+
+ return array();
+ }
+
+ /**
+ * Return the current request
*
* @return Request
*/
public function getRequest()
{
- if ($this->request === null) {
- $this->request = Icinga::app()->getFrontController()->getRequest();
- }
-
- return $this->request;
+ return Icinga::app()->getFrontController()->getRequest();
}
/**
@@ -724,39 +710,6 @@ class Form extends Zend_Form
return Icinga::app()->getFrontController()->getResponse();
}
- /**
- * Return the request data based on this form's request method
- *
- * @return array
- */
- protected function getRequestData()
- {
- if (strtolower($this->request->getMethod()) === $this->getMethod()) {
- return $this->request->{'get' . ($this->request->isPost() ? 'Post' : 'Query')}();
- }
-
- return array();
- }
-
- /**
- * Create a new element located in the Icinga Web 2 library
- *
- * @param string $type The type of the element
- * @param string $name The name of the element
- * @param mixed $options The options for the element
- *
- * @return NULL|FormElement NULL in case the element is not found in the Icinga Web 2 library
- *
- * @see Form::$defaultElementDecorators For Icinga Web 2's default element decorators.
- */
- protected function createIcingaFormElement($type, $name, $options = null)
- {
- $className = 'Icinga\\Web\\Form\\Element\\' . ucfirst($type);
- if (class_exists($className)) {
- return new $className($name, $options);
- }
- }
-
/**
* Render this form
*
@@ -770,3 +723,4 @@ class Form extends Zend_Form
return parent::render($view);
}
}
+
From cbcd276b44f437c24df69c36298fcea46743cf9f Mon Sep 17 00:00:00 2001
From: Marius Hein
Date: Tue, 18 Nov 2014 09:50:10 +0100
Subject: [PATCH 11/31] Dashboard: Rewrite forms and controller [WIP]
refs #4537
---
.../controllers/DashboardController.php | 143 ++++++++++++++----
.../{AddUrlForm.php => ComponentForm.php} | 104 ++++++-------
.../scripts/dashboard/new-component.phtml | 8 +
.../scripts/dashboard/remove-component.phtml | 8 +
.../views/scripts/dashboard/settings.phtml | 47 ++++++
.../scripts/dashboard/update-component.phtml | 8 +
library/Icinga/Web/Widget/Dashboard.php | 3 +-
.../Icinga/Web/Widget/Dashboard/Component.php | 24 +++
.../Widget/Tabextension/DashboardSettings.php | 40 +++++
9 files changed, 294 insertions(+), 91 deletions(-)
rename application/forms/Dashboard/{AddUrlForm.php => ComponentForm.php} (75%)
create mode 100644 application/views/scripts/dashboard/new-component.phtml
create mode 100644 application/views/scripts/dashboard/remove-component.phtml
create mode 100644 application/views/scripts/dashboard/settings.phtml
create mode 100644 application/views/scripts/dashboard/update-component.phtml
create mode 100644 library/Icinga/Web/Widget/Tabextension/DashboardSettings.php
diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php
index 2c2b7d2fd..14252ce22 100644
--- a/application/controllers/DashboardController.php
+++ b/application/controllers/DashboardController.php
@@ -2,16 +2,13 @@
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
-use Icinga\Application\Config;
use Icinga\Application\Logger;
-use Icinga\Exception\ConfigurationError;
-use Icinga\Exception\IcingaException;
-use Icinga\Exception\NotReadableError;
-use Icinga\File\Ini\IniWriter;
-use Icinga\Forms\Dashboard\AddUrlForm;
+use Icinga\Form\Dashboard\ComponentForm;
+use Icinga\Web\Notification;
use Icinga\Web\Controller\ActionController;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
+use Icinga\Web\Widget\Tabextension\DashboardSettings;
/**
* Handle creation, removal and displaying of dashboards, panes and components
@@ -20,26 +17,100 @@ use Icinga\Web\Widget\Dashboard;
*/
class DashboardController extends ActionController
{
+ /**
+ * @var Dashboard;
+ */
+ private $dashboard;
+
+ public function init()
+ {
+ $this->dashboard = new Dashboard();
+ $this->dashboard->setUser($this->getRequest()->getUser());
+ $this->dashboard->load();
+ }
+
+ public function newComponentAction()
+ {
+ $form = new ComponentForm();
+ $this->createTabs();
+ $dashboard = new Dashboard();
+ $dashboard->setUser($this->getRequest()->getUser());
+ $dashboard->load();
+ $form->setDashboard($dashboard);
+ $form->handleRequest();
+ $this->view->form = $form;
+ }
+
+ public function updateComponentAction()
+ {
+ $this->createTabs();
+ $dashboard = $this->dashboard;
+ $form = new ComponentForm();
+ $form->setDashboard($dashboard);
+ $form->setSubmitLabel(t('Update Component'));
+ if (! $this->_request->getParam('pane')) {
+ throw new Zend_Controller_Action_Exception(
+ 'Missing parameter "pane"',
+ 400
+ );
+ }
+ if (! $this->_request->getParam('component')) {
+ throw new Zend_Controller_Action_Exception(
+ 'Missing parameter "component"',
+ 400
+ );
+ }
+ $form->setOnSuccess(function (\Icinga\Web\Request $request, \Icinga\Web\Form $form) use ($dashboard) {
+ $pane = $dashboard->getPane($form->getValue('pane'));
+ try {
+ $component = $pane->getComponent($form->getValue('component'));
+ $component->setUrl($form->getValue('url'));
+ } catch (\Icinga\Exception\ProgrammingError $e) {
+ $component = new Dashboard\Component($form->getValue('component'), $form->getValue('url'), $pane);
+ $pane->addComponent($component);
+ }
+ $component->setUserWidget();
+ // Rename component
+ if ($form->getValue('org_component') && $form->getValue('org_component') !== $component->getTitle()) {
+ $pane->removeComponent($form->getValue('org_component'));
+ }
+ $dashboard->write();
+ Notification::success(t('Component updated'));
+ return true;
+ });
+ $form->setRedirectUrl('dashboard/settings');
+ $form->handleRequest();
+ $pane = $dashboard->getPane($this->getParam('pane'));
+ $component = $pane->getComponent($this->getParam('component'));
+ $form->load($component);
+
+ $this->view->form = $form;
+ }
+
+ public function deleteComponentAction()
+ {
+ $form = new ComponentForm();
+
+ $this->createTabs();
+ $dashboard = new Dashboard();
+ $dashboard->setUser($this->getRequest()->getUser());
+ $dashboard->load();
+ $form->setDashboard($dashboard);
+ $form->handleRequest();
+ $this->view->form = $form;
+ }
+
/**
* Display the form for adding new components or add the new component if submitted
*/
public function addurlAction()
{
- $this->getTabs()->add(
- 'addurl',
- array(
- 'title' => 'Add Dashboard URL',
- 'url' => Url::fromRequest()
- )
- )->activate('addurl');
-
$form = new AddUrlForm();
$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
$form->setDashboard($dashboard);
- $form->setRedirectUrl($this->getParam('url'));
$form->handleRequest();
$this->view->form = $form;
}
@@ -52,36 +123,50 @@ class DashboardController extends ActionController
*/
public function indexAction()
{
- $dashboard = new Dashboard();
- $dashboard->setUser($this->getRequest()->getUser());
- $dashboard->load();
-
- if (! $dashboard->hasPanes()) {
+ $this->createTabs();
+ if (! $this->dashboard->hasPanes()) {
$this->view->title = 'Dashboard';
} else {
if ($this->_getParam('pane')) {
$pane = $this->_getParam('pane');
- $dashboard->activate($pane);
+ $this->dashboard->activate($pane);
}
- if ($dashboard === null) {
+ if ($this->dashboard === null) {
$this->view->title = 'Dashboard';
} else {
- $this->view->title = $dashboard->getActivePane()->getTitle() . ' :: Dashboard';
- $this->view->tabs = $dashboard->getTabs();
+ $this->view->title = $this->dashboard->getActivePane()->getTitle() . ' :: Dashboard';
if ($this->hasParam('remove')) {
- $dashboard->getActivePane()->removeComponent($this->getParam('remove'));
- $dashboard->write();
+ $this->dashboard->getActivePane()->removeComponent($this->getParam('remove'));
+ $this->dashboard->write();
$this->redirectNow(URL::fromRequest()->remove('remove'));
}
- $this->view->tabs->add(
+
+ /* $this->view->tabs->add(
'Add',
array(
'title' => '+',
'url' => Url::fromPath('dashboard/addurl')
)
- );
- $this->view->dashboard = $dashboard;
+ ); */
+ $this->view->dashboard = $this->dashboard;
}
}
}
+
+ /**
+ * Setting dialog
+ */
+ public function settingsAction()
+ {
+ $this->createTabs();
+ $this->view->dashboard = $this->dashboard;
+ }
+
+ /**
+ * Create tab aggregation
+ */
+ private function createTabs()
+ {
+ $this->view->tabs = $this->dashboard->getTabs()->extend(new DashboardSettings());
+ }
}
diff --git a/application/forms/Dashboard/AddUrlForm.php b/application/forms/Dashboard/ComponentForm.php
similarity index 75%
rename from application/forms/Dashboard/AddUrlForm.php
rename to application/forms/Dashboard/ComponentForm.php
index f67c92a42..d3bcb22f2 100644
--- a/application/forms/Dashboard/AddUrlForm.php
+++ b/application/forms/Dashboard/ComponentForm.php
@@ -11,19 +11,13 @@ use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form;
use Icinga\Web\Request;
+use Icinga\Web\Widget\Dashboard\Component;
/**
* Form to add an url a dashboard pane
*/
-class AddUrlForm extends Form
+class ComponentForm extends Form
{
- /**
- * Config file name
- *
- * @var string
- */
- private $configFile = 'dashboard/dashboard';
-
/**
* @var Dashboard
*/
@@ -35,7 +29,9 @@ class AddUrlForm extends Form
public function init()
{
$this->setName('form_dashboard_addurl');
- $this->setSubmitLabel(t('Add To Dashboard'));
+ if (! $this->getSubmitLabel()) {
+ $this->setSubmitLabel(t('Add To Dashboard'));
+ }
}
/**
@@ -45,13 +41,28 @@ class AddUrlForm extends Form
*/
public function createElements(array $formData)
{
- $paneSelectionValues = array();
+ $groupElements = array();
+ $panes = array();
- if ($this->dashboard !== null) {
- $paneSelectionValues = $this->dashboard->getPaneKeyTitleArray();
+ if ($this->dashboard) {
+ $panes = $this->dashboard->getPaneKeyTitleArray();
}
- $groupElements = array();
+ $this->addElement(
+ 'hidden',
+ 'org_pane',
+ array(
+ 'required' => false
+ )
+ );
+
+ $this->addElement(
+ 'hidden',
+ 'org_component',
+ array(
+ 'required' => false
+ )
+ );
$this->addElement(
'text',
@@ -72,7 +83,7 @@ class AddUrlForm extends Form
'description' => t('Enter a title for the dashlet.')
)
);
- if (empty($paneSelectionValues) ||
+ if (empty($panes) ||
((isset($formData['create_new_pane']) && $formData['create_new_pane'] != false) &&
(false === isset($formData['use_existing_dashboard']) || $formData['use_existing_dashboard'] != true))
) {
@@ -93,7 +104,7 @@ class AddUrlForm extends Form
'value' => 1
)
);
- if (false === empty($paneSelectionValues)) {
+ if (false === empty($panes)) {
$buttonExistingPane = $this->createElement(
'submit',
'use_existing_dashboard',
@@ -114,7 +125,7 @@ class AddUrlForm extends Form
array(
'required' => true,
'label' => t('Pane'),
- 'multiOptions' => $paneSelectionValues,
+ 'multiOptions' => $panes,
'description' =>
t('Select a pane you want to add the dashlet.')
)
@@ -161,33 +172,6 @@ class AddUrlForm extends Form
*/
public function onSuccess(Request $request)
{
- $pane = null;
-
- if ($this->dashboard === null) {
- throw new ProgrammingError('Dashboard is not set, can not write values');
- }
-
- try {
- $pane = $this->dashboard->getPane($this->getValue('pane'));
- } catch (ProgrammingError $e) {
- $pane = new Dashboard\Pane($this->getValue('pane'));
- $pane->setUserWidget();
- $this->dashboard->addPane($pane);
- }
-
- $component = new Dashboard\Component(
- $this->getValue('component'),
- $this->getValue('url'),
- $pane
- );
-
- $component->setUserWidget();
-
- $pane->addComponent($component);
-
- $this->dashboard->write();
-
-
return true;
}
@@ -198,25 +182,11 @@ class AddUrlForm extends Form
*/
public function onRequest(Request $request)
{
- // TODO(mh): Im not sure if this is the right place for that
- $url = $this->getValue('url');
- if (! $url) {
- $url = $request->getParam('url');
- }
-
- if (! $url) {
- return;
- }
-
- $data = array(
- 'url' => urldecode(Url::fromPath($url)->getPath())
- );
-
- $this->populate($data);
+ return true;
}
/**
- * @param Dashboard $dashboard
+ * @param \Icinga\Web\Widget\Dashboard $dashboard
*/
public function setDashboard(Dashboard $dashboard)
{
@@ -224,10 +194,24 @@ class AddUrlForm extends Form
}
/**
- * @return Dashboard
+ * @return \Icinga\Web\Widget\Dashboard
*/
public function getDashboard()
{
return $this->dashboard;
}
+
+ /**
+ * @param Component $component
+ */
+ public function load(Component $component)
+ {
+ $this->populate(array(
+ 'pane' => $component->getPane()->getName(),
+ 'org_pane' => $component->getPane()->getName(),
+ 'component' => $component->getTitle(),
+ 'org_component' => $component->getTitle(),
+ 'url' => $component->getUrl()
+ ));
+ }
}
diff --git a/application/views/scripts/dashboard/new-component.phtml b/application/views/scripts/dashboard/new-component.phtml
new file mode 100644
index 000000000..456d14a65
--- /dev/null
+++ b/application/views/scripts/dashboard/new-component.phtml
@@ -0,0 +1,8 @@
+
+ = $this->tabs ?>
+
+
+
+
= t('Add Component To Dashboard'); ?>
+ = $this->form; ?>
+
\ No newline at end of file
diff --git a/application/views/scripts/dashboard/remove-component.phtml b/application/views/scripts/dashboard/remove-component.phtml
new file mode 100644
index 000000000..9138b95b1
--- /dev/null
+++ b/application/views/scripts/dashboard/remove-component.phtml
@@ -0,0 +1,8 @@
+
+ = $this->tabs ?>
+
+
+
+
= t('Remove Component From Dashboard'); ?>
+ = $this->form; ?>
+
\ No newline at end of file
diff --git a/application/views/scripts/dashboard/settings.phtml b/application/views/scripts/dashboard/settings.phtml
new file mode 100644
index 000000000..7a2e8f390
--- /dev/null
+++ b/application/views/scripts/dashboard/settings.phtml
@@ -0,0 +1,47 @@
+
+
\ No newline at end of file
diff --git a/application/views/scripts/dashboard/update-component.phtml b/application/views/scripts/dashboard/update-component.phtml
new file mode 100644
index 000000000..0493f5613
--- /dev/null
+++ b/application/views/scripts/dashboard/update-component.phtml
@@ -0,0 +1,8 @@
+
+ = $this->tabs ?>
+
+
+
+
= t('Edit Component'); ?>
+ = $this->form; ?>
+
\ No newline at end of file
diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php
index 1a049ef59..1764e5cea 100644
--- a/library/Icinga/Web/Widget/Dashboard.php
+++ b/library/Icinga/Web/Widget/Dashboard.php
@@ -187,7 +187,7 @@ class Dashboard extends AbstractWidget
*/
public function getTabs()
{
- $url = Url::fromRequest()->getUrlWithout($this->tabParam);
+ $url = Url::fromPath('dashboard')->getUrlWithout($this->tabParam);
if ($this->tabs === null) {
$this->tabs = new Tabs();
@@ -212,7 +212,6 @@ class Dashboard extends AbstractWidget
*/
public function getPanes()
{
- return '';
return $this->panes;
}
diff --git a/library/Icinga/Web/Widget/Dashboard/Component.php b/library/Icinga/Web/Widget/Dashboard/Component.php
index 693f60bfe..0ab210057 100644
--- a/library/Icinga/Web/Widget/Dashboard/Component.php
+++ b/library/Icinga/Web/Widget/Dashboard/Component.php
@@ -93,6 +93,14 @@ EOD;
return $this->title;
}
+ /**
+ * @param string $title
+ */
+ public function setTitle($title)
+ {
+ $this->title = $title;
+ }
+
/**
* Retrieve the components url
*
@@ -228,4 +236,20 @@ EOD;
$cmp = new Component($title, Url::fromPath($url, $parameters), $pane);
return $cmp;
}
+
+ /**
+ * @param \Icinga\Web\Widget\Dashboard\Pane $pane
+ */
+ public function setPane(Panel $pane)
+ {
+ $this->pane = $pane;
+ }
+
+ /**
+ * @return \Icinga\Web\Widget\Dashboard\Pane
+ */
+ public function getPane()
+ {
+ return $this->pane;
+ }
}
diff --git a/library/Icinga/Web/Widget/Tabextension/DashboardSettings.php b/library/Icinga/Web/Widget/Tabextension/DashboardSettings.php
new file mode 100644
index 000000000..4242f864d
--- /dev/null
+++ b/library/Icinga/Web/Widget/Tabextension/DashboardSettings.php
@@ -0,0 +1,40 @@
+addAsDropdown(
+ 'dashboard_add',
+ array(
+ 'icon' => 'img/icons/dashboard.png',
+ 'title' => t('Add To Dashboard'),
+ 'url' => Url::fromPath('dashboard/addurl')
+ )
+ );
+
+ $tabs->addAsDropdown(
+ 'dashboard_settings',
+ array(
+ 'icon' => 'img/icons/dashboard.png',
+ 'title' => t('Settings'),
+ 'url' => Url::fromPath('dashboard/settings')
+ )
+ );
+ }
+}
\ No newline at end of file
From 3e58ec253030838dac328e674bd631b6d5d9fb05 Mon Sep 17 00:00:00 2001
From: Marius Hein
Date: Tue, 18 Nov 2014 12:51:28 +0100
Subject: [PATCH 12/31] Dashboard: Use new controller actions
refs #4537
---
.../controllers/DashboardController.php | 98 +++++++++++++------
application/forms/Dashboard/ComponentForm.php | 4 -
.../scripts/dashboard/remove-component.phtml | 6 ++
.../views/scripts/dashboard/settings.phtml | 37 ++++---
.../Icinga/Web/Widget/Dashboard/Component.php | 5 +-
.../Widget/Tabextension/DashboardAction.php | 2 +-
.../Widget/Tabextension/DashboardSettings.php | 2 +-
7 files changed, 101 insertions(+), 53 deletions(-)
diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php
index 14252ce22..a28e1485a 100644
--- a/application/controllers/DashboardController.php
+++ b/application/controllers/DashboardController.php
@@ -3,9 +3,13 @@
// {{{ICINGA_LICENSE_HEADER}}}
use Icinga\Application\Logger;
-use Icinga\Form\Dashboard\ComponentForm;
+use Icinga\Exception\ProgrammingError;
+use Icinga\Forms\ConfirmRemovalForm;
+use Icinga\Forms\Dashboard\ComponentForm;
+use Icinga\Web\Form;
use Icinga\Web\Notification;
use Icinga\Web\Controller\ActionController;
+use Icinga\Web\Request;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Widget\Tabextension\DashboardSettings;
@@ -33,10 +37,29 @@ class DashboardController extends ActionController
{
$form = new ComponentForm();
$this->createTabs();
- $dashboard = new Dashboard();
- $dashboard->setUser($this->getRequest()->getUser());
- $dashboard->load();
+ $dashboard = $this->dashboard;
$form->setDashboard($dashboard);
+ if ($this->_request->getParam('url')) {
+ $params = $this->_request->getParams();
+ $params['url'] = rawurldecode($this->_request->getParam('url'));
+ $form->populate($params);
+ }
+ $form->setOnSuccess(function (Request $request, Form $form) use ($dashboard) {
+ try {
+ $pane = $dashboard->getPane($form->getValue('pane'));
+ } catch (ProgrammingError $e) {
+ $pane = new Dashboard\Pane($form->getValue('pane'));
+ $pane->setUserWidget();
+ $dashboard->addPane($pane);
+ }
+ $component = new Dashboard\Component($form->getValue('component'), $form->getValue('url'), $pane);
+ $component->setUserWidget();
+ $pane->addComponent($component);
+ $dashboard->write();
+ Notification::success(t('Component created'));
+ return true;
+ });
+ $form->setRedirectUrl('dashboard');
$form->handleRequest();
$this->view->form = $form;
}
@@ -60,12 +83,12 @@ class DashboardController extends ActionController
400
);
}
- $form->setOnSuccess(function (\Icinga\Web\Request $request, \Icinga\Web\Form $form) use ($dashboard) {
+ $form->setOnSuccess(function (Request $request, Form $form) use ($dashboard) {
$pane = $dashboard->getPane($form->getValue('pane'));
try {
$component = $pane->getComponent($form->getValue('component'));
$component->setUrl($form->getValue('url'));
- } catch (\Icinga\Exception\ProgrammingError $e) {
+ } catch (ProgrammingError $e) {
$component = new Dashboard\Component($form->getValue('component'), $form->getValue('url'), $pane);
$pane->addComponent($component);
}
@@ -75,6 +98,7 @@ class DashboardController extends ActionController
$pane->removeComponent($form->getValue('org_component'));
}
$dashboard->write();
+ $dashboard->write();
Notification::success(t('Component updated'));
return true;
});
@@ -87,31 +111,42 @@ class DashboardController extends ActionController
$this->view->form = $form;
}
- public function deleteComponentAction()
+ public function removeComponentAction()
{
- $form = new ComponentForm();
-
+ $form = new ConfirmRemovalForm();
$this->createTabs();
- $dashboard = new Dashboard();
- $dashboard->setUser($this->getRequest()->getUser());
- $dashboard->load();
- $form->setDashboard($dashboard);
- $form->handleRequest();
- $this->view->form = $form;
- }
-
- /**
- * Display the form for adding new components or add the new component if submitted
- */
- public function addurlAction()
- {
- $form = new AddUrlForm();
-
- $dashboard = new Dashboard();
- $dashboard->setUser($this->getRequest()->getUser());
- $dashboard->load();
- $form->setDashboard($dashboard);
+ $dashboard = $this->dashboard;
+ if (! $this->_request->getParam('pane')) {
+ throw new Zend_Controller_Action_Exception(
+ 'Missing parameter "pane"',
+ 400
+ );
+ }
+ if (! $this->_request->getParam('component')) {
+ throw new Zend_Controller_Action_Exception(
+ 'Missing parameter "component"',
+ 400
+ );
+ }
+ $pane = $this->_request->getParam('pane');
+ $component = $this->_request->getParam('component');
+ $form->setOnSuccess(function (Request $request, Form $form) use ($dashboard, $component, $pane) {
+ try {
+ $pane = $dashboard->getPane($pane);
+ $pane->removeComponent($component);
+ $dashboard->write();
+ Notification::success(t('Component has been removed from') . ' ' . $pane->getTitle());
+ return true;
+ } catch (ProgrammingError $e) {
+ Notification::error($e->getMessage());
+ return false;
+ }
+ return false;
+ });
+ $form->setRedirectUrl('dashboard/settings');
$form->handleRequest();
+ $this->view->pane = $pane;
+ $this->view->component = $component;
$this->view->form = $form;
}
@@ -140,14 +175,13 @@ class DashboardController extends ActionController
$this->dashboard->write();
$this->redirectNow(URL::fromRequest()->remove('remove'));
}
-
- /* $this->view->tabs->add(
+ $this->view->tabs->add(
'Add',
array(
'title' => '+',
- 'url' => Url::fromPath('dashboard/addurl')
+ 'url' => Url::fromPath('dashboard/new-component')
)
- ); */
+ );
$this->view->dashboard = $this->dashboard;
}
}
diff --git a/application/forms/Dashboard/ComponentForm.php b/application/forms/Dashboard/ComponentForm.php
index d3bcb22f2..c62bf82fe 100644
--- a/application/forms/Dashboard/ComponentForm.php
+++ b/application/forms/Dashboard/ComponentForm.php
@@ -4,10 +4,6 @@
namespace Icinga\Forms\Dashboard;
-use Icinga\Application\Config;
-use Icinga\Exception\ProgrammingError;
-use Icinga\File\Ini\IniWriter;
-use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form;
use Icinga\Web\Request;
diff --git a/application/views/scripts/dashboard/remove-component.phtml b/application/views/scripts/dashboard/remove-component.phtml
index 9138b95b1..c39472771 100644
--- a/application/views/scripts/dashboard/remove-component.phtml
+++ b/application/views/scripts/dashboard/remove-component.phtml
@@ -4,5 +4,11 @@
= $this->escape($this->translate('Welcome to Icinga Web!')) ?>
-
= sprintf(
- $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')
- ) ?>
+
+ = sprintf(
+ $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')
+ ) ?>
+
\ No newline at end of file
diff --git a/application/views/scripts/dashboard/remove-pane.phtml b/application/views/scripts/dashboard/remove-pane.phtml
new file mode 100644
index 000000000..637774cab
--- /dev/null
+++ b/application/views/scripts/dashboard/remove-pane.phtml
@@ -0,0 +1,14 @@
+
\ No newline at end of file
diff --git a/application/views/scripts/dashboard/settings.phtml b/application/views/scripts/dashboard/settings.phtml
index 76a66a0d2..ebe0188c4 100644
--- a/application/views/scripts/dashboard/settings.phtml
+++ b/application/views/scripts/dashboard/settings.phtml
@@ -23,6 +23,11 @@