Dashboard: Add urls to any dashboard (form)

refs #4537
This commit is contained in:
Marius Hein 2014-11-12 09:22:39 +01:00
parent 769e8f2636
commit bec0085683
3 changed files with 87 additions and 54 deletions

View File

@ -32,7 +32,14 @@ class DashboardController extends ActionController
'url' => Url::fromRequest() 'url' => Url::fromRequest()
) )
)->activate('addurl'); )->activate('addurl');
$form = new AddUrlForm(); $form = new AddUrlForm();
$dashboard = new Dashboard();
$dashboard->setUser($this->getRequest()->getUser());
$dashboard->load();
$form->setDashboard($dashboard);
$form->setRedirectUrl($this->getParam('url'));
$form->handleRequest(); $form->handleRequest();
$this->view->form = $form; $this->view->form = $form;
} }

View File

@ -5,7 +5,9 @@
namespace Icinga\Forms\Dashboard; namespace Icinga\Forms\Dashboard;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Exception\ProgrammingError;
use Icinga\File\Ini\IniWriter; use Icinga\File\Ini\IniWriter;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard; use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\Request; use Icinga\Web\Request;
@ -22,6 +24,11 @@ class AddUrlForm extends Form
*/ */
private $configFile = 'dashboard/dashboard'; private $configFile = 'dashboard/dashboard';
/**
* @var Dashboard
*/
private $dashboard;
/** /**
* Initialize this form * Initialize this form
*/ */
@ -38,7 +45,12 @@ class AddUrlForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
$paneSelectionValues = $this->getDashboardPaneSelectionValues(); $paneSelectionValues = array();
if ($this->dashboard !== null) {
$paneSelectionValues = $this->dashboard->getPaneKeyTitleArray();
}
$groupElements = array(); $groupElements = array();
$this->addElement( $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 * Adjust preferences and persist them
* *
@ -187,23 +161,34 @@ class AddUrlForm extends Form
*/ */
public function onSuccess(Request $request) public function onSuccess(Request $request)
{ {
$dashboard = $this->createDashboard(); $pane = null;
$dashboard->setComponentUrl(
$this->getValue('pane'), 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('component'),
ltrim($this->getValue('url'), '/') $this->getValue('url'),
); $pane
/*
$writer = new IniWriter(
array(
'config' => new Config($dashboard->toArray()),
'filename' => $dashboard->getConfig()->getConfigFile()
)
); );
$writer->write(); $component->setUserWidget();
*/
return false; $pane->addComponent($component);
$this->dashboard->write();
return true;
} }
/** /**
@ -213,9 +198,36 @@ class AddUrlForm extends Form
*/ */
public function onRequest(Request $request) 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( $data = array(
'url' => $request->getParam('url') 'url' => urldecode(Url::fromPath($url)->getPath())
); );
$this->populate($data); $this->populate($data);
} }
/**
* @param Dashboard $dashboard
*/
public function setDashboard(Dashboard $dashboard)
{
$this->dashboard = $dashboard;
}
/**
* @return Dashboard
*/
public function getDashboard()
{
return $this->dashboard;
}
} }

View File

@ -275,6 +275,20 @@ class Dashboard extends AbstractWidget
return $this->panes[$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 * @see Icinga\Web\Widget::render
*/ */