From 7926d558ba46f8bd4dbb430732d016f7fa06862a Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 26 Nov 2018 00:11:35 +0100 Subject: [PATCH] DataList: load by name, fix basket --- application/controllers/DataController.php | 74 ++++++++++++++----- application/forms/BasketForm.php | 1 + .../forms/DirectorDatalistEntryForm.php | 20 +++-- .../Automation/BasketSnapshot.php | 2 + library/Director/Objects/DirectorDatalist.php | 50 ++++++++++++- .../Objects/DirectorDatalistEntry.php | 20 +++-- .../Director/Web/Table/DatalistEntryTable.php | 32 ++++---- library/Director/Web/Table/DatalistTable.php | 2 +- 8 files changed, 145 insertions(+), 56 deletions(-) diff --git a/application/controllers/DataController.php b/application/controllers/DataController.php index 769b8f1b..b2d588f5 100644 --- a/application/controllers/DataController.php +++ b/application/controllers/DataController.php @@ -18,11 +18,8 @@ class DataController extends ActionController public function listsAction() { $this->addTitle($this->translate('Data lists')); - $this->actions()->add(Link::create( - $this->translate('Add'), - 'director/data/list', - null, - [ + $this->actions()->add( + Link::create($this->translate('Add'), 'director/data/list', null, [ 'class' => 'icon-plus', 'data-base-target' => '_next' ] @@ -32,18 +29,24 @@ class DataController extends ActionController (new DatalistTable($this->db()))->renderTo($this); } + /** + * @throws \Icinga\Exception\MissingParameterException + * @throws \Icinga\Exception\NotFoundError + */ public function listAction() { $form = DirectorDatalistForm::load() ->setSuccessUrl('director/data/lists') ->setDb($this->db()); - if ($id = $this->params->get('id')) { - $form->loadObject($id); + if ($name = $this->params->get('name')) { + $list = $this->requireList('name'); + $form->setObject($list); + $this->addListActions($list); $this->addTitle( $this->translate('Data List: %s'), - $form->getObject()->list_name - )->addListTabs($id, 'list'); + $list->get('list_name') + )->addListTabs($name, 'list'); } else { $this ->addTitle($this->translate('Add a new Data List')) @@ -74,17 +77,22 @@ class DataController extends ActionController (new CustomvarTable($this->db()))->renderTo($this); } + /** + * @throws \Icinga\Exception\MissingParameterException + * @throws \Icinga\Exception\NotFoundError + */ public function listentryAction() { - $url = $this->url(); - $entryName = $url->shift('entry_name'); - $list = DirectorDatalist::load($url->shift('list_id'), $this->db()); - $listId = $list->id; - $title = $title = $this->translate('List Entries') . ': ' . $list->list_name; + $entryName = $this->params->get('entry_name'); + $list = $this->requireList('list'); + $this->addListActions($list); + $listId = $list->get('id'); + $listName = $list->get('list_name'); + $title = $title = $this->translate('List Entries') . ': ' . $listName; $this->addTitle($title); $form = DirectorDatalistEntryForm::load() - ->setSuccessUrl('director/data/listentry', ['list_id' => $listId]) + ->setSuccessUrl('director/data/listentry', ['list' => $listName]) ->setList($list); if (null !== $entryName) { @@ -95,13 +103,13 @@ class DataController extends ActionController $this->actions()->add(Link::create( $this->translate('back'), 'director/data/listentry', - ['list_id' => $listId], + ['list' => $listName], ['class' => 'icon-left-big'] )); } $form->handleRequest(); - $this->addListTabs($listId, 'entries'); + $this->addListTabs($listName, 'entries'); $table = new DatalistEntryTable($this->db()); $table->getAttributes()->set('data-base-target', '_self'); @@ -109,15 +117,41 @@ class DataController extends ActionController $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', [ 'url' => 'director/data/list', - 'urlParams' => ['id' => $id], + 'urlParams' => ['name' => $name], 'label' => $this->translate('Edit list'), ])->add('entries', [ 'url' => 'director/data/listentry', - 'urlParams' => ['list_id' => $id], + 'urlParams' => ['list' => $name], 'label' => $this->translate('List entries'), ])->activate($activate); diff --git a/application/forms/BasketForm.php b/application/forms/BasketForm.php index 7920e7d2..16a68bf4 100644 --- a/application/forms/BasketForm.php +++ b/application/forms/BasketForm.php @@ -24,6 +24,7 @@ class BasketForm extends DirectorObjectForm 'ServiceSet' => $this->translate('Service Sets'), 'Notification' => $this->translate('Notifications'), 'Dependency' => $this->translate('Dependencies'), + 'DataList' => $this->translate('Data Lists'), 'ImportSource' => $this->translate('Import Sources'), 'SyncRule' => $this->translate('Sync Rules'), 'DirectorJob' => $this->translate('Job Definitions'), diff --git a/application/forms/DirectorDatalistEntryForm.php b/application/forms/DirectorDatalistEntryForm.php index 0bea3754..c6e309f1 100644 --- a/application/forms/DirectorDatalistEntryForm.php +++ b/application/forms/DirectorDatalistEntryForm.php @@ -12,24 +12,27 @@ class DirectorDatalistEntryForm extends DirectorObjectForm /** @var DirectorDatalist */ protected $datalist; + /** + * @throws \Zend_Form_Exception + */ public function setup() { - $this->addElement('text', 'entry_name', array( + $this->addElement('text', 'entry_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( + $this->addElement('text', 'entry_value', [ 'label' => $this->translate('Label'), 'required' => true, 'description' => $this->translate( 'This will be the visible caption for this entry' ) - )); + ]); $rolesConfig = Config::app('roles', true); $roles = []; @@ -37,14 +40,14 @@ class DirectorDatalistEntryForm extends DirectorObjectForm $roles[$name] = $name; } - $this->addElement('extensibleSet', 'allowed_roles', array( + $this->addElement('extensibleSet', 'allowed_roles', [ 'label' => $this->translate('Allowed roles'), 'required' => false, 'multiOptions' => $roles, 'description' => $this->translate( '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('format', 'string'); @@ -52,11 +55,11 @@ class DirectorDatalistEntryForm extends DirectorObjectForm $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() ? $this->translate('Add data list entry') : $this->translate('Modify data list entry') - )); + ]); $this->setButtons(); } @@ -71,6 +74,7 @@ class DirectorDatalistEntryForm extends DirectorObjectForm /** @var Db $db */ $db = $list->getConnection(); $this->setDb($db); + return $this; } } diff --git a/library/Director/DirectorObject/Automation/BasketSnapshot.php b/library/Director/DirectorObject/Automation/BasketSnapshot.php index 85b12f41..0b6d4a5e 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshot.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshot.php @@ -24,6 +24,7 @@ class BasketSnapshot extends DbObject 'ServiceTemplate' => '\\Icinga\\Module\\Director\\Objects\\IcingaService', 'ServiceSet' => '\\Icinga\\Module\\Director\\Objects\\IcingaServiceSet', 'Notification' => '\\Icinga\\Module\\Director\\Objects\\IcingaNotification', + 'DataList' => '\\Icinga\\Module\\Director\\Objects\\DirectorDatalist', 'Dependency' => '\\Icinga\\Module\\Director\\Objects\\IcingaDependency', 'ImportSource' => '\\Icinga\\Module\\Director\\Objects\\ImportSource', 'SyncRule' => '\\Icinga\\Module\\Director\\Objects\\SyncRule', @@ -53,6 +54,7 @@ class BasketSnapshot extends DbObject 'ServiceSet', 'Notification', 'Dependency', + 'DataList', 'ImportSource', 'SyncRule', 'DirectorJob', diff --git a/library/Director/Objects/DirectorDatalist.php b/library/Director/Objects/DirectorDatalist.php index 947fe031..93cdc918 100644 --- a/library/Director/Objects/DirectorDatalist.php +++ b/library/Director/Objects/DirectorDatalist.php @@ -3,12 +3,15 @@ namespace Icinga\Module\Director\Objects; 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 $keyName = 'id'; + protected $keyName = 'list_name'; protected $autoincKeyName = 'id'; @@ -18,6 +21,48 @@ class DirectorDatalist extends DbObject '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() { $plain = (object) $this->getProperties(); @@ -28,7 +73,6 @@ class DirectorDatalist extends DbObject $entries = DirectorDatalistEntry::loadAllForList($this); foreach ($entries as $key => $entry) { $plainEntry = (object) $entry->getProperties(); - unset($plainEntry->id); unset($plainEntry->list_id); $plain->entries[] = $plainEntry; diff --git a/library/Director/Objects/DirectorDatalistEntry.php b/library/Director/Objects/DirectorDatalistEntry.php index 3578dc4b..07c59f38 100644 --- a/library/Director/Objects/DirectorDatalistEntry.php +++ b/library/Director/Objects/DirectorDatalistEntry.php @@ -2,30 +2,28 @@ namespace Icinga\Module\Director\Objects; -use Icinga\Exception\IcingaException; -use Icinga\Exception\ProgrammingError; +use http\Exception\RuntimeException; use Icinga\Module\Director\Data\Db\DbObject; class DirectorDatalistEntry extends DbObject { - protected $keyName = array('list_id', 'entry_name'); + protected $keyName = ['list_id', 'entry_name']; protected $table = 'director_datalist_entry'; private $shouldBeRemoved = false; - protected $defaultProperties = array( + protected $defaultProperties = [ 'list_id' => null, 'entry_name' => null, 'entry_value' => null, 'format' => null, 'allowed_roles' => null, - ); + ]; /** * @param DirectorDatalist $list * @return static[] - * @throws IcingaException */ public static function loadAllForList(DirectorDatalist $list) { @@ -40,7 +38,6 @@ class DirectorDatalistEntry extends DbObject /** * @param $roles - * @throws IcingaException * @codingStandardsIgnoreStart */ public function setAllowed_roles($roles) @@ -52,7 +49,7 @@ class DirectorDatalistEntry extends DbObject } elseif (null === $roles) { $this->reallySet($key, null); } else { - throw new ProgrammingError( + throw new RuntimeException( 'Expected array or null for allowed_roles, got %s', var_export($roles, 1) ); @@ -76,9 +73,9 @@ class DirectorDatalistEntry extends DbObject public function replaceWith(DirectorDatalistEntry $object) { - $this->entry_value = $object->entry_value; - if ($object->format) { - $this->format = $object->format; + $this->set('entry_value', $object->get('entry_value')); + if ($object->get('format')) { + $this->set('format', $object->get('format')); } return $this; @@ -92,6 +89,7 @@ class DirectorDatalistEntry extends DbObject public function markForRemoval($remove = true) { $this->shouldBeRemoved = $remove; + return $this; } diff --git a/library/Director/Web/Table/DatalistEntryTable.php b/library/Director/Web/Table/DatalistEntryTable.php index b42cbf33..42909d47 100644 --- a/library/Director/Web/Table/DatalistEntryTable.php +++ b/library/Director/Web/Table/DatalistEntryTable.php @@ -10,14 +10,15 @@ class DatalistEntryTable extends ZfQueryBasedTable { protected $datalist; - protected $searchColumns = array( + protected $searchColumns = [ 'entry_name', 'entry_value' - ); + ]; public function setList(DirectorDatalist $list) { $this->datalist = $list; + return $this; } @@ -28,18 +29,19 @@ class DatalistEntryTable extends ZfQueryBasedTable public function getColumns() { - return array( - 'list_id' => 'l.list_id', - 'entry_name' => 'l.entry_name', - 'entry_value' => 'l.entry_value', - ); + return [ + 'list_name' => 'l.list_name', + 'list_id' => 'le.list_id', + 'entry_name' => 'le.entry_name', + 'entry_value' => 'le.entry_value', + ]; } public function renderRow($row) { return $this::tr([ $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, ])), $this::td($row->entry_value) @@ -48,20 +50,24 @@ class DatalistEntryTable extends ZfQueryBasedTable public function getColumnsToBeRendered() { - return array( + return [ 'entry_name' => $this->translate('Key'), 'entry_value' => $this->translate('Label'), - ); + ]; } public function prepareQuery() { return $this->db()->select()->from( - array('l' => 'director_datalist_entry'), + ['le' => 'director_datalist_entry'], $this->getColumns() + )->join( + ['l' => 'director_datalist'], + 'l.id = le.list_id', + [] )->where( - 'l.list_id = ?', + 'le.list_id = ?', $this->getList()->id - )->order('l.entry_name ASC'); + )->order('le.entry_name ASC'); } } diff --git a/library/Director/Web/Table/DatalistTable.php b/library/Director/Web/Table/DatalistTable.php index 689ae6dd..8fbdec76 100644 --- a/library/Director/Web/Table/DatalistTable.php +++ b/library/Director/Web/Table/DatalistTable.php @@ -22,7 +22,7 @@ class DatalistTable extends ZfQueryBasedTable return $this::tr($this::td(Link::create( $row->list_name, 'director/data/listentry', - array('list_id' => $row->id) + array('list' => $row->list_name) ))); }