DataList: load by name, fix basket

This commit is contained in:
Thomas Gelf 2018-11-26 00:11:35 +01:00
parent 264732ba98
commit 7926d558ba
8 changed files with 145 additions and 56 deletions

View File

@ -18,11 +18,8 @@ class DataController extends ActionController
public function listsAction() public function listsAction()
{ {
$this->addTitle($this->translate('Data lists')); $this->addTitle($this->translate('Data lists'));
$this->actions()->add(Link::create( $this->actions()->add(
$this->translate('Add'), Link::create($this->translate('Add'), 'director/data/list', null, [
'director/data/list',
null,
[
'class' => 'icon-plus', 'class' => 'icon-plus',
'data-base-target' => '_next' 'data-base-target' => '_next'
] ]
@ -32,18 +29,24 @@ class DataController extends ActionController
(new DatalistTable($this->db()))->renderTo($this); (new DatalistTable($this->db()))->renderTo($this);
} }
/**
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError
*/
public function listAction() public function listAction()
{ {
$form = DirectorDatalistForm::load() $form = DirectorDatalistForm::load()
->setSuccessUrl('director/data/lists') ->setSuccessUrl('director/data/lists')
->setDb($this->db()); ->setDb($this->db());
if ($id = $this->params->get('id')) { if ($name = $this->params->get('name')) {
$form->loadObject($id); $list = $this->requireList('name');
$form->setObject($list);
$this->addListActions($list);
$this->addTitle( $this->addTitle(
$this->translate('Data List: %s'), $this->translate('Data List: %s'),
$form->getObject()->list_name $list->get('list_name')
)->addListTabs($id, 'list'); )->addListTabs($name, 'list');
} else { } else {
$this $this
->addTitle($this->translate('Add a new Data List')) ->addTitle($this->translate('Add a new Data List'))
@ -74,17 +77,22 @@ class DataController extends ActionController
(new CustomvarTable($this->db()))->renderTo($this); (new CustomvarTable($this->db()))->renderTo($this);
} }
/**
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError
*/
public function listentryAction() public function listentryAction()
{ {
$url = $this->url(); $entryName = $this->params->get('entry_name');
$entryName = $url->shift('entry_name'); $list = $this->requireList('list');
$list = DirectorDatalist::load($url->shift('list_id'), $this->db()); $this->addListActions($list);
$listId = $list->id; $listId = $list->get('id');
$title = $title = $this->translate('List Entries') . ': ' . $list->list_name; $listName = $list->get('list_name');
$title = $title = $this->translate('List Entries') . ': ' . $listName;
$this->addTitle($title); $this->addTitle($title);
$form = DirectorDatalistEntryForm::load() $form = DirectorDatalistEntryForm::load()
->setSuccessUrl('director/data/listentry', ['list_id' => $listId]) ->setSuccessUrl('director/data/listentry', ['list' => $listName])
->setList($list); ->setList($list);
if (null !== $entryName) { if (null !== $entryName) {
@ -95,13 +103,13 @@ class DataController extends ActionController
$this->actions()->add(Link::create( $this->actions()->add(Link::create(
$this->translate('back'), $this->translate('back'),
'director/data/listentry', 'director/data/listentry',
['list_id' => $listId], ['list' => $listName],
['class' => 'icon-left-big'] ['class' => 'icon-left-big']
)); ));
} }
$form->handleRequest(); $form->handleRequest();
$this->addListTabs($listId, 'entries'); $this->addListTabs($listName, 'entries');
$table = new DatalistEntryTable($this->db()); $table = new DatalistEntryTable($this->db());
$table->getAttributes()->set('data-base-target', '_self'); $table->getAttributes()->set('data-base-target', '_self');
@ -109,15 +117,41 @@ class DataController extends ActionController
$this->content()->add([$form, $table]); $this->content()->add([$form, $table]);
} }
protected function addListTabs($id, $activate) protected function addListActions(DirectorDatalist $list)
{
$this->actions()->add(
Link::create(
$this->translate('Add to Basket'),
'director/basket/add',
[
'type' => 'DataList',
'names' => $list->getUniqueIdentifier()
],
['class' => 'icon-tag']
)
);
}
/**
* @param $paramName
* @return DirectorDatalist
* @throws \Icinga\Exception\MissingParameterException
* @throws \Icinga\Exception\NotFoundError
*/
protected function requireList($paramName)
{
return DirectorDatalist::load($this->params->getRequired($paramName), $this->db());
}
protected function addListTabs($name, $activate)
{ {
$this->tabs()->add('list', [ $this->tabs()->add('list', [
'url' => 'director/data/list', 'url' => 'director/data/list',
'urlParams' => ['id' => $id], 'urlParams' => ['name' => $name],
'label' => $this->translate('Edit list'), 'label' => $this->translate('Edit list'),
])->add('entries', [ ])->add('entries', [
'url' => 'director/data/listentry', 'url' => 'director/data/listentry',
'urlParams' => ['list_id' => $id], 'urlParams' => ['list' => $name],
'label' => $this->translate('List entries'), 'label' => $this->translate('List entries'),
])->activate($activate); ])->activate($activate);

View File

@ -24,6 +24,7 @@ class BasketForm extends DirectorObjectForm
'ServiceSet' => $this->translate('Service Sets'), 'ServiceSet' => $this->translate('Service Sets'),
'Notification' => $this->translate('Notifications'), 'Notification' => $this->translate('Notifications'),
'Dependency' => $this->translate('Dependencies'), 'Dependency' => $this->translate('Dependencies'),
'DataList' => $this->translate('Data Lists'),
'ImportSource' => $this->translate('Import Sources'), 'ImportSource' => $this->translate('Import Sources'),
'SyncRule' => $this->translate('Sync Rules'), 'SyncRule' => $this->translate('Sync Rules'),
'DirectorJob' => $this->translate('Job Definitions'), 'DirectorJob' => $this->translate('Job Definitions'),

View File

@ -12,24 +12,27 @@ class DirectorDatalistEntryForm extends DirectorObjectForm
/** @var DirectorDatalist */ /** @var DirectorDatalist */
protected $datalist; protected $datalist;
/**
* @throws \Zend_Form_Exception
*/
public function setup() public function setup()
{ {
$this->addElement('text', 'entry_name', array( $this->addElement('text', 'entry_name', [
'label' => $this->translate('Key'), 'label' => $this->translate('Key'),
'required' => true, 'required' => true,
'description' => $this->translate( 'description' => $this->translate(
'Will be stored as a custom variable value when this entry' 'Will be stored as a custom variable value when this entry'
. ' is chosen from the list' . ' is chosen from the list'
) )
)); ]);
$this->addElement('text', 'entry_value', array( $this->addElement('text', 'entry_value', [
'label' => $this->translate('Label'), 'label' => $this->translate('Label'),
'required' => true, 'required' => true,
'description' => $this->translate( 'description' => $this->translate(
'This will be the visible caption for this entry' 'This will be the visible caption for this entry'
) )
)); ]);
$rolesConfig = Config::app('roles', true); $rolesConfig = Config::app('roles', true);
$roles = []; $roles = [];
@ -37,14 +40,14 @@ class DirectorDatalistEntryForm extends DirectorObjectForm
$roles[$name] = $name; $roles[$name] = $name;
} }
$this->addElement('extensibleSet', 'allowed_roles', array( $this->addElement('extensibleSet', 'allowed_roles', [
'label' => $this->translate('Allowed roles'), 'label' => $this->translate('Allowed roles'),
'required' => false, 'required' => false,
'multiOptions' => $roles, 'multiOptions' => $roles,
'description' => $this->translate( 'description' => $this->translate(
'Allow to use this entry only to users with one of these Icinga Web 2 roles' 'Allow to use this entry only to users with one of these Icinga Web 2 roles'
) )
)); ]);
$this->addHidden('list_id', $this->datalist->get('id')); $this->addHidden('list_id', $this->datalist->get('id'));
$this->addHidden('format', 'string'); $this->addHidden('format', 'string');
@ -52,11 +55,11 @@ class DirectorDatalistEntryForm extends DirectorObjectForm
$this->addHidden('entry_name', $this->object->get('entry_name')); $this->addHidden('entry_name', $this->object->get('entry_name'));
} }
$this->addSimpleDisplayGroup(array('entry_name', 'entry_value', 'allowed_roles'), 'entry', array( $this->addSimpleDisplayGroup(['entry_name', 'entry_value', 'allowed_roles'], 'entry', [
'legend' => $this->isNew() 'legend' => $this->isNew()
? $this->translate('Add data list entry') ? $this->translate('Add data list entry')
: $this->translate('Modify data list entry') : $this->translate('Modify data list entry')
)); ]);
$this->setButtons(); $this->setButtons();
} }
@ -71,6 +74,7 @@ class DirectorDatalistEntryForm extends DirectorObjectForm
/** @var Db $db */ /** @var Db $db */
$db = $list->getConnection(); $db = $list->getConnection();
$this->setDb($db); $this->setDb($db);
return $this; return $this;
} }
} }

View File

@ -24,6 +24,7 @@ class BasketSnapshot extends DbObject
'ServiceTemplate' => '\\Icinga\\Module\\Director\\Objects\\IcingaService', 'ServiceTemplate' => '\\Icinga\\Module\\Director\\Objects\\IcingaService',
'ServiceSet' => '\\Icinga\\Module\\Director\\Objects\\IcingaServiceSet', 'ServiceSet' => '\\Icinga\\Module\\Director\\Objects\\IcingaServiceSet',
'Notification' => '\\Icinga\\Module\\Director\\Objects\\IcingaNotification', 'Notification' => '\\Icinga\\Module\\Director\\Objects\\IcingaNotification',
'DataList' => '\\Icinga\\Module\\Director\\Objects\\DirectorDatalist',
'Dependency' => '\\Icinga\\Module\\Director\\Objects\\IcingaDependency', 'Dependency' => '\\Icinga\\Module\\Director\\Objects\\IcingaDependency',
'ImportSource' => '\\Icinga\\Module\\Director\\Objects\\ImportSource', 'ImportSource' => '\\Icinga\\Module\\Director\\Objects\\ImportSource',
'SyncRule' => '\\Icinga\\Module\\Director\\Objects\\SyncRule', 'SyncRule' => '\\Icinga\\Module\\Director\\Objects\\SyncRule',
@ -53,6 +54,7 @@ class BasketSnapshot extends DbObject
'ServiceSet', 'ServiceSet',
'Notification', 'Notification',
'Dependency', 'Dependency',
'DataList',
'ImportSource', 'ImportSource',
'SyncRule', 'SyncRule',
'DirectorJob', 'DirectorJob',

View File

@ -3,12 +3,15 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
use Icinga\Module\Director\Exception\DuplicateKeyException;
class DirectorDatalist extends DbObject class DirectorDatalist extends DbObject implements ExportInterface
{ {
protected $table = 'director_datalist'; protected $table = 'director_datalist';
protected $keyName = 'id'; protected $keyName = 'list_name';
protected $autoincKeyName = 'id'; protected $autoincKeyName = 'id';
@ -18,6 +21,48 @@ class DirectorDatalist extends DbObject
'owner' => null 'owner' => null
); );
public function getUniqueIdentifier()
{
return $this->get('list_name');
}
/**
* @param $plain
* @param Db $db
* @param bool $replace
* @return static
* @throws \Icinga\Exception\NotFoundError
* @throws DuplicateKeyException
*/
public static function import($plain, Db $db, $replace = false)
{
$properties = (array) $plain;
if (isset($properties['originalId'])) {
$id = $properties['originalId'];
unset($properties['originalId']);
} else {
$id = null;
}
$name = $properties['list_name'];
if ($replace && static::exists($name, $db)) {
$object = static::load($name, $db);
} elseif (static::exists($name, $db)) {
throw new DuplicateKeyException(
'Data List %s already exists',
$name
);
} else {
$object = static::create([], $db);
}
$object->setProperties($properties);
if ($id !== null) {
$object->reallySet('id', $id);
}
return $object;
}
public function export() public function export()
{ {
$plain = (object) $this->getProperties(); $plain = (object) $this->getProperties();
@ -28,7 +73,6 @@ class DirectorDatalist extends DbObject
$entries = DirectorDatalistEntry::loadAllForList($this); $entries = DirectorDatalistEntry::loadAllForList($this);
foreach ($entries as $key => $entry) { foreach ($entries as $key => $entry) {
$plainEntry = (object) $entry->getProperties(); $plainEntry = (object) $entry->getProperties();
unset($plainEntry->id);
unset($plainEntry->list_id); unset($plainEntry->list_id);
$plain->entries[] = $plainEntry; $plain->entries[] = $plainEntry;

View File

@ -2,30 +2,28 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Exception\IcingaException; use http\Exception\RuntimeException;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Data\Db\DbObject;
class DirectorDatalistEntry extends DbObject class DirectorDatalistEntry extends DbObject
{ {
protected $keyName = array('list_id', 'entry_name'); protected $keyName = ['list_id', 'entry_name'];
protected $table = 'director_datalist_entry'; protected $table = 'director_datalist_entry';
private $shouldBeRemoved = false; private $shouldBeRemoved = false;
protected $defaultProperties = array( protected $defaultProperties = [
'list_id' => null, 'list_id' => null,
'entry_name' => null, 'entry_name' => null,
'entry_value' => null, 'entry_value' => null,
'format' => null, 'format' => null,
'allowed_roles' => null, 'allowed_roles' => null,
); ];
/** /**
* @param DirectorDatalist $list * @param DirectorDatalist $list
* @return static[] * @return static[]
* @throws IcingaException
*/ */
public static function loadAllForList(DirectorDatalist $list) public static function loadAllForList(DirectorDatalist $list)
{ {
@ -40,7 +38,6 @@ class DirectorDatalistEntry extends DbObject
/** /**
* @param $roles * @param $roles
* @throws IcingaException
* @codingStandardsIgnoreStart * @codingStandardsIgnoreStart
*/ */
public function setAllowed_roles($roles) public function setAllowed_roles($roles)
@ -52,7 +49,7 @@ class DirectorDatalistEntry extends DbObject
} elseif (null === $roles) { } elseif (null === $roles) {
$this->reallySet($key, null); $this->reallySet($key, null);
} else { } else {
throw new ProgrammingError( throw new RuntimeException(
'Expected array or null for allowed_roles, got %s', 'Expected array or null for allowed_roles, got %s',
var_export($roles, 1) var_export($roles, 1)
); );
@ -76,9 +73,9 @@ class DirectorDatalistEntry extends DbObject
public function replaceWith(DirectorDatalistEntry $object) public function replaceWith(DirectorDatalistEntry $object)
{ {
$this->entry_value = $object->entry_value; $this->set('entry_value', $object->get('entry_value'));
if ($object->format) { if ($object->get('format')) {
$this->format = $object->format; $this->set('format', $object->get('format'));
} }
return $this; return $this;
@ -92,6 +89,7 @@ class DirectorDatalistEntry extends DbObject
public function markForRemoval($remove = true) public function markForRemoval($remove = true)
{ {
$this->shouldBeRemoved = $remove; $this->shouldBeRemoved = $remove;
return $this; return $this;
} }

View File

@ -10,14 +10,15 @@ class DatalistEntryTable extends ZfQueryBasedTable
{ {
protected $datalist; protected $datalist;
protected $searchColumns = array( protected $searchColumns = [
'entry_name', 'entry_name',
'entry_value' 'entry_value'
); ];
public function setList(DirectorDatalist $list) public function setList(DirectorDatalist $list)
{ {
$this->datalist = $list; $this->datalist = $list;
return $this; return $this;
} }
@ -28,18 +29,19 @@ class DatalistEntryTable extends ZfQueryBasedTable
public function getColumns() public function getColumns()
{ {
return array( return [
'list_id' => 'l.list_id', 'list_name' => 'l.list_name',
'entry_name' => 'l.entry_name', 'list_id' => 'le.list_id',
'entry_value' => 'l.entry_value', 'entry_name' => 'le.entry_name',
); 'entry_value' => 'le.entry_value',
];
} }
public function renderRow($row) public function renderRow($row)
{ {
return $this::tr([ return $this::tr([
$this::td(Link::create($row->entry_name, 'director/data/listentry/edit', [ $this::td(Link::create($row->entry_name, 'director/data/listentry/edit', [
'list_id' => $row->list_id, 'list' => $row->list_name,
'entry_name' => $row->entry_name, 'entry_name' => $row->entry_name,
])), ])),
$this::td($row->entry_value) $this::td($row->entry_value)
@ -48,20 +50,24 @@ class DatalistEntryTable extends ZfQueryBasedTable
public function getColumnsToBeRendered() public function getColumnsToBeRendered()
{ {
return array( return [
'entry_name' => $this->translate('Key'), 'entry_name' => $this->translate('Key'),
'entry_value' => $this->translate('Label'), 'entry_value' => $this->translate('Label'),
); ];
} }
public function prepareQuery() public function prepareQuery()
{ {
return $this->db()->select()->from( return $this->db()->select()->from(
array('l' => 'director_datalist_entry'), ['le' => 'director_datalist_entry'],
$this->getColumns() $this->getColumns()
)->join(
['l' => 'director_datalist'],
'l.id = le.list_id',
[]
)->where( )->where(
'l.list_id = ?', 'le.list_id = ?',
$this->getList()->id $this->getList()->id
)->order('l.entry_name ASC'); )->order('le.entry_name ASC');
} }
} }

View File

@ -22,7 +22,7 @@ class DatalistTable extends ZfQueryBasedTable
return $this::tr($this::td(Link::create( return $this::tr($this::td(Link::create(
$row->list_name, $row->list_name,
'director/data/listentry', 'director/data/listentry',
array('list_id' => $row->id) array('list' => $row->list_name)
))); )));
} }