Datalists: completely refactor forms and tables

fixes #11401
This commit is contained in:
Thomas Gelf 2016-03-22 23:47:54 +01:00
parent 324e15e2b5
commit 478ee6131a
7 changed files with 199 additions and 98 deletions

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Objects\DirectorDatalist;
use Icinga\Module\Director\Web\Controller\ActionController;
class DataController extends ActionController
@ -10,7 +11,7 @@ class DataController extends ActionController
{
$this->view->addLink = $this->view->qlink(
$this->translate('Add list'),
'director/datalist/add',
'director/data/list',
null,
array('class' => 'icon-plus')
);
@ -20,6 +21,98 @@ class DataController extends ActionController
$this->prepareAndRenderTable('datalist');
}
public function listAction()
{
$this->view->stayHere = true;
$form = $this->view->form = $this->loadForm('directorDatalist')
->setSuccessUrl('director/data/lists')
->setDb($this->db());
if ($id = $this->getRequest()->getUrl()->shift('id')) {
$form->loadObject($id);
$this->view->title = sprintf(
$this->translate('Data list: %s'),
$form->getObject()->list_name
);
$this->view->addLink = $this->view->qlink(
$this->translate('back'),
'director/data/list',
null,
array('class' => 'icon-left-big')
);
$this->view->addLink .= $this->view->qlink(
$this->translate('Entries'),
'director/data/listentry',
array('list_id' => $id),
array(
'class' => 'icon-doc-text',
'data-base-target' => '_next'
)
);
$this->getTabs()->add('editlist', array(
'url' => 'director/data/list' . '?id=' . $id,
'label' => $this->translate('Edit list'),
))->add('entries', array(
'url' => 'director/data/listentry' . '?list_id=' . $id,
'label' => $this->translate('List entries'),
))->activate('editlist');
} else {
$this->view->title = $this->translate('Add data list');
$this->getTabs()->add('addlist', array(
'url' => 'director/data/list',
'label' => $this->view->title,
))->activate('addlist');
}
$form->handleRequest();
$this->setViewScript('object/form');
}
public function indexAction()
{
$edit = false;
if ($id = $this->params->get('id')) {
$edit = true;
}
if ($edit) {
$this->view->title = $this->translate('Edit list');
$this->getTabs()->add('editlist', array(
'url' => 'director/datalist/edit' . '?id=' . $id,
'label' => $this->view->title,
))->add('entries', array(
'url' => 'director/data/listentry' . '?list_id=' . $id,
'label' => $this->translate('List entries'),
))->activate('editlist');
} else {
$this->view->title = $this->translate('Add list');
$this->getTabs()->add('addlist', array(
'url' => 'director/datalist/add',
'label' => $this->view->title,
))->activate('addlist');
}
$form = $this->view->form = $this->loadForm('directorDatalist')
->setSuccessUrl('director/data/lists')
->setDb($this->db());
if ($edit) {
$form->loadObject($id);
}
$form->handleRequest();
$this->render('object/form', null, true);
}
public function fieldsAction()
{
$this->view->addLink = $this->view->qlink(
@ -36,26 +129,46 @@ class DataController extends ActionController
public function listentryAction()
{
$listId = $this->params->get('list_id');
$this->view->lastId = $listId;
$this->view->stayHere = true;
$this->view->addLink = $this->view->qlink(
$this->translate('Add entry'),
'director/datalistentry/add' . '?list_id=' . $listId,
null,
array('class' => 'icon-plus')
);
$url = $this->getRequest()->getUrl();
$entryName = $url->shift('entry_name');
$list = DirectorDatalist::load($url->shift('list_id'), $this->db());
$listId = $list->id;
$this->view->title = $this->translate('List entries');
$form = $this->view->form = $this->loadForm('directorDatalistentry')
->setSuccessUrl('director/data/listentry')
->setList($list)
->setDb($this->db());
if ($entryName) {
$form->loadObject(array(
'list_id' => $listId,
'entry_name' => $entryName
));
$this->view->addLink = $this->view->qlink(
$this->translate('back'),
'director/data/listentry' . '?list_id=' . $listId,
null,
array('class' => 'icon-left-big')
);
}
$form->handleRequest();
$this->view->title = $this->translate('List entries')
. ': ' . $list->list_name;
$this->getTabs()->add('editlist', array(
'url' => 'director/datalist/edit' . '?id=' . $listId,
'url' => 'director/data/list' . '?id=' . $listId,
'label' => $this->translate('Edit list'),
))->add('datalistentry', array(
'url' => 'director/datalistentry' . '?list_id=' . $listId,
'url' => 'director/data/listentry' . '?list_id=' . $listId,
'label' => $this->view->title,
))->activate('datalistentry');
$this->prepareAndRenderTable('datalistEntry');
$this->prepareTable('datalistEntry')->setList($list);
$this->setViewScript('objects/table');
}
protected function prepareTable($name)
@ -63,11 +176,12 @@ class DataController extends ActionController
$table = $this->loadTable($name)->setConnection($this->db());
$this->view->filterEditor = $table->getFilterEditor($this->getRequest());
$this->view->table = $this->applyPaginationLimits($table);
return $this;
return $table;
}
protected function prepareAndRenderTable($name)
{
$this->prepareTable($name)->render('objects/table', null, 'objects');
$this->prepareTable($name);
$this->setViewScript('objects/table');
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Web\Controller\ActionController;
class DatalistController extends ActionController
{
public function addAction()
{
$this->indexAction();
}
public function editAction()
{
$this->indexAction();
}
public function indexAction()
{
$edit = false;
if ($id = $this->params->get('id')) {
$edit = true;
}
if ($edit) {
$this->view->title = $this->translate('Edit list');
$this->getTabs()->add('editlist', array(
'url' => 'director/datalist/edit' . '?id=' . $id,
'label' => $this->view->title,
))->add('entries', array(
'url' => 'director/data/listentry' . '?list_id=' . $id,
'label' => $this->translate('List entries'),
))->activate('editlist');
} else {
$this->view->title = $this->translate('Add list');
$this->getTabs()->add('addlist', array(
'url' => 'director/datalist/add',
'label' => $this->view->title,
))->activate('addlist');
}
$form = $this->view->form = $this->loadForm('directorDatalist')
->setSuccessUrl('director/data/lists')
->setDb($this->db());
if ($edit) {
$form->loadObject($id);
}
$form->handleRequest();
$this->render('object/form', null, true);
}
}

View File

@ -10,9 +10,21 @@ class DirectorDatalistForm extends DirectorObjectForm
public function setup()
{
$this->addElement('text', 'list_name', array(
'label' => $this->translate('List name'),
'required' => true,
'label' => $this->translate('List name'),
'description' => $this->translate(
'Data lists are mainly used as data providers for custom variables'
. ' presented as dropdown boxes boxes. You can manually manage'
. ' their entries here in place, but you could also create dedicated'
. ' sync rules after creating a new empty list. This would allow you'
. ' to keep your available choices in sync with external data providers'
),
'required' => true,
));
$this->addSimpleDisplayGroup(array('list_name'), 'list', array(
'legend' => $this->translate('Data list')
));
$this->setButtons();
}
public function onSuccess()

View File

@ -2,35 +2,50 @@
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\DirectorDatalist;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class DirectorDatalistEntryForm extends DirectorObjectForm
{
protected $listId;
protected $datalist;
public function setup()
{
$this->addElement('text', 'entry_name', array(
'label' => 'Name'
'label' => $this->translate('Key'),
'required' => true,
'description' => $this->translate(
'Will be stored as a custom variable value when this entry'
. ' is chosen from the list'
)
));
$this->addElement('text', 'entry_value', array(
'label' => 'Value'
'label' => $this->translate('Value'),
'required' => true,
'description' => $this->translate(
'This will be the visible caption for this entry'
)
));
$this->addElement('select', 'format', array(
'label' => 'Type',
'multiOptions' => array('string' => $this->translate('String'))
$this->addHidden('list_id', $this->datalist->id);
$this->addHidden('format', 'string');
if (!$this->isNew()) {
$this->addHidden('entry_name', $this->object->entry_name);
}
$this->addSimpleDisplayGroup(array('entry_name', 'entry_value'), 'entry', array(
'legend' => $this->isNew()
? $this->translate('Add data list entry')
: $this->translate('Modify data list entry')
));
$this->setButtons();
}
public function onSuccess()
public function setList(DirectorDatalist $list)
{
$this->object()->list_id = $this->listId;
parent::onSuccess();
}
public function setListId($id)
{
$this->listId = $id;
$this->datalist = $list;
return $this;
}
}

View File

@ -2,14 +2,29 @@
namespace Icinga\Module\Director\Tables;
use Icinga\Module\Director\Objects\DirectorDatalist;
use Icinga\Module\Director\Web\Table\QuickTable;
class DatalistEntryTable extends QuickTable
{
protected $datalist;
protected $searchColumns = array(
'entry_name',
'entry_value'
);
public function setList(DirectorDatalist $list)
{
$this->datalist = $list;
return $this;
}
public function getList()
{
return $this->datalist;
}
public function getColumns()
{
return array(
@ -21,23 +36,18 @@ class DatalistEntryTable extends QuickTable
protected function getActionUrl($row)
{
return $this->url('director/datalistentry/edit', array(
return $this->url('director/data/listentry/edit', array(
'list_id' => $row->list_id,
'entry_name' => $row->entry_name,
));
}
public function getListId()
{
return $this->view()->lastId;
}
public function getTitles()
{
$view = $this->view();
return array(
'entry_name' => $view->translate('Name'),
'entry_value' => $view->translate('Value'),
'entry_name' => $view->translate('Key'),
'entry_value' => $view->translate('Label'),
);
}
@ -48,7 +58,10 @@ class DatalistEntryTable extends QuickTable
$query = $db->select()->from(
array('l' => 'director_datalist_entry'),
array()
)->where('l.list_id = ?', $this->getListId())->order('l.entry_name ASC');
)->where(
'l.list_id = ?',
$this->getList()->id
)->order('l.entry_name ASC');
return $query;
}

View File

@ -21,7 +21,10 @@ class DatalistTable extends QuickTable
protected function getActionUrl($row)
{
return $this->url('director/data/listentry', array('list_id' => $row->id));
return $this->url(
'director/data/listentry',
array('list_id' => $row->id)
);
}
public function getTitles()

View File

@ -3,7 +3,7 @@
<?= $this->tabs ?>
<h1><?= $this->escape($this->title) ?><?= $this->quickSearch ?></h1>
<span data-base-target="_next">
<span<?php if (! $this->stayHere): ?> data-base-target="_next"<?php endif ?>>
<?= $this->addLink ?>
</span>
<?php if ($this->filterEditor): ?>