From c72b51c5385e15f2597e3eea31ddb277505ae2d1 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 15 Jun 2018 02:08:06 +0200 Subject: [PATCH] ScheduledDowntimes: a bunch of url-related changes --- .../ScheduledDowntimesController.php | 7 ++++ application/controllers/SuggestController.php | 8 +++- .../forms/IcingaScheduledDowntimeForm.php | 13 +----- .../Dashlet/ScheduledDowntimeApplyDashlet.php | 2 +- .../Objects/IcingaScheduledDowntime.php | 3 ++ .../Web/ActionBar/TemplateActionBar.php | 2 +- .../Web/Controller/ObjectController.php | 6 ++- .../Web/Controller/ObjectsController.php | 16 ++++--- .../Director/Web/Table/ApplyRulesTable.php | 42 +++++++++++++++---- library/Director/Web/Table/ObjectsTable.php | 15 ++++++- library/Director/Web/Tabs/ObjectTabs.php | 2 +- library/Director/Web/Tabs/ObjectsTabs.php | 12 +++--- 12 files changed, 91 insertions(+), 37 deletions(-) diff --git a/application/controllers/ScheduledDowntimesController.php b/application/controllers/ScheduledDowntimesController.php index 5f8b8a08..24f7ca5b 100644 --- a/application/controllers/ScheduledDowntimesController.php +++ b/application/controllers/ScheduledDowntimesController.php @@ -6,6 +6,13 @@ use Icinga\Module\Director\Web\Controller\ObjectsController; class ScheduledDowntimesController extends ObjectsController { + protected function addObjectsTabs() + { + $res = parent::addObjectsTabs(); + $this->tabs()->remove('index'); + return $res; + } + public function getType() { return 'scheduledDowntime'; diff --git a/application/controllers/SuggestController.php b/application/controllers/SuggestController.php index 691d35cc..aad01494 100644 --- a/application/controllers/SuggestController.php +++ b/application/controllers/SuggestController.php @@ -188,6 +188,12 @@ class SuggestController extends ActionController return $this->fetchTemplateNames('icinga_user'); } + protected function suggestScheduled_downtimetemplates() + { + $this->assertPermission('director/scheduled-downtimes'); + return $this->fetchTemplateNames('icinga_scheduled_downtime'); + } + protected function suggestCheckcommandnames() { $db = $this->db()->getDbAdapter(); @@ -199,7 +205,7 @@ class SuggestController extends ActionController return $db->fetchCol($query); } - protected function fetchTemplateNames($table, $where) + protected function fetchTemplateNames($table, $where = null) { $db = $this->db()->getDbAdapter(); $query = $db->select() diff --git a/application/forms/IcingaScheduledDowntimeForm.php b/application/forms/IcingaScheduledDowntimeForm.php index d34a485c..40de0b6f 100644 --- a/application/forms/IcingaScheduledDowntimeForm.php +++ b/application/forms/IcingaScheduledDowntimeForm.php @@ -11,18 +11,7 @@ class IcingaScheduledDowntimeForm extends DirectorObjectForm */ public function setup() { - $isTemplate = isset($_POST['object_type']) && $_POST['object_type'] === 'template'; - $this->addElement('select', 'object_type', [ - 'label' => $this->translate('Object type'), - 'description' => $this->translate('Whether this should be a template'), - 'class' => 'autosubmit', - 'multiOptions' => $this->optionalEnum([ - 'object' => $this->translate('Object'), - 'template' => $this->translate('Template'), - ]) - ]); - - if ($isTemplate) { + if ($this->isTemplate()) { $this->addElement('text', 'object_name', [ 'label' => $this->translate('Template name'), 'required' => true, diff --git a/library/Director/Dashboard/Dashlet/ScheduledDowntimeApplyDashlet.php b/library/Director/Dashboard/Dashlet/ScheduledDowntimeApplyDashlet.php index 32fd8f30..8e39851e 100644 --- a/library/Director/Dashboard/Dashlet/ScheduledDowntimeApplyDashlet.php +++ b/library/Director/Dashboard/Dashlet/ScheduledDowntimeApplyDashlet.php @@ -15,6 +15,6 @@ class ScheduledDowntimeApplyDashlet extends Dashlet public function getUrl() { - return 'director/scheduled_downtimes/applyrules'; + return 'director/scheduled-downtimes/applyrules'; } } diff --git a/library/Director/Objects/IcingaScheduledDowntime.php b/library/Director/Objects/IcingaScheduledDowntime.php index 684b4b57..de4a35d4 100644 --- a/library/Director/Objects/IcingaScheduledDowntime.php +++ b/library/Director/Objects/IcingaScheduledDowntime.php @@ -17,12 +17,15 @@ class IcingaScheduledDowntime extends IcingaObject 'comment' => null, 'fixed' => null, 'duration' => null, + 'assign_filter' => null, ]; protected $supportsImports = true; protected $supportsRanges = true; + protected $supportsApplyRules = true; + protected $relations = [ 'zone' => 'IcingaZone', ]; diff --git a/library/Director/Web/ActionBar/TemplateActionBar.php b/library/Director/Web/ActionBar/TemplateActionBar.php index 9278559b..5364c16e 100644 --- a/library/Director/Web/ActionBar/TemplateActionBar.php +++ b/library/Director/Web/ActionBar/TemplateActionBar.php @@ -8,7 +8,7 @@ class TemplateActionBar extends DirectorBaseActionBar { protected function assemble() { - $type = $this->type; + $type = str_replace('_', '-', $this->type); $plType = preg_replace('/cys$/', 'cies', $type . 's'); $renderTree = $this->url->getParam('render') === 'tree'; $renderParams = $renderTree ? null : ['render' => 'tree']; diff --git a/library/Director/Web/Controller/ObjectController.php b/library/Director/Web/Controller/ObjectController.php index 0dbcba34..75ef7d4d 100644 --- a/library/Director/Web/Controller/ObjectController.php +++ b/library/Director/Web/Controller/ObjectController.php @@ -79,7 +79,11 @@ abstract class ObjectController extends ActionController 'add' ); } else { - $this->tabs(new ObjectTabs($this->getType(), $this->getAuth(), $this->object)); + $this->tabs(new ObjectTabs( + $this->getRequest()->getControllerName(), + $this->getAuth(), + $this->object + )); } } } diff --git a/library/Director/Web/Controller/ObjectsController.php b/library/Director/Web/Controller/ObjectsController.php index e4bd2760..7d189089 100644 --- a/library/Director/Web/Controller/ObjectsController.php +++ b/library/Director/Web/Controller/ObjectsController.php @@ -48,8 +48,11 @@ abstract class ObjectsController extends ActionController if (substr($this->getType(), -5) === 'Group') { $tabName = 'groups'; } - $this->tabs(new ObjectsTabs($this->getBaseType(), $this->Auth())) - ->activate($tabName); + $this->tabs(new ObjectsTabs( + $this->getBaseType(), + $this->Auth(), + $this->getBaseObjectUrl() + ))->activate($tabName); return $this; } @@ -126,7 +129,8 @@ abstract class ObjectsController extends ActionController protected function getTable() { return ObjectsTable::create($this->getType(), $this->db()) - ->setAuth($this->getAuth()); + ->setAuth($this->getAuth()) + ->setBaseObjectUrl($this->getBaseObjectUrl()); } /** @@ -240,12 +244,13 @@ abstract class ObjectsController extends ActionController $this->translate('All your %s Apply Rules'), $tType ); + $baseUrl = 'director/' . $this->getBaseObjectUrl(); $this->actions() //->add($this->getBackToDashboardLink()) ->add( Link::create( $this->translate('Add'), - "director/$type/add", + "${baseUrl}/add", ['type' => 'apply'], [ 'title' => sprintf( @@ -259,7 +264,8 @@ abstract class ObjectsController extends ActionController ); $table = new ApplyRulesTable($this->db()); - $table->setType($this->getType()); + $table->setType($this->getType()) + ->setBaseObjectUrl($this->getBaseObjectUrl()); $this->eventuallyFilterCommand($table); $table->renderTo($this); } diff --git a/library/Director/Web/Table/ApplyRulesTable.php b/library/Director/Web/Table/ApplyRulesTable.php index 01dc7cf9..29c75d75 100644 --- a/library/Director/Web/Table/ApplyRulesTable.php +++ b/library/Director/Web/Table/ApplyRulesTable.php @@ -25,6 +25,11 @@ class ApplyRulesTable extends ZfQueryBasedTable private $type; + /** @var IcingaObject */ + protected $dummyObject; + + protected $baseObjectUrl; + public static function create($type, Db $db) { $table = new static($db); @@ -35,6 +40,14 @@ class ApplyRulesTable extends ZfQueryBasedTable public function setType($type) { $this->type = $type; + + return $this; + } + + public function setBaseObjectUrl($url) + { + $this->baseObjectUrl = $url; + return $this; } @@ -99,32 +112,32 @@ class ApplyRulesTable extends ZfQueryBasedTable public function createActionLinks($row) { - $type = $this->type; + $baseUrl = 'director/' . $this->baseObjectUrl; $links = []; $links[] = Link::create( Icon::create('sitemap'), - "director/${type}template/applytargets", + "${baseUrl}template/applytargets", ['id' => $row->id], ['title' => $this->translate('Show affected Objects')] ); $links[] = Link::create( Icon::create('edit'), - "director/$type/edit", + "$baseUrl/edit", ['id' => $row->id], ['title' => $this->translate('Modify this Apply Rule')] ); $links[] = Link::create( Icon::create('doc-text'), - "director/$type/render", + "$baseUrl/render", ['id' => $row->id], ['title' => $this->translate('Apply Rule rendering preview')] ); $links[] = Link::create( Icon::create('history'), - "director/$type/history", + "$baseUrl/history", ['id' => $row->id], ['title' => $this->translate('Apply rule history')] ); @@ -149,9 +162,22 @@ class ApplyRulesTable extends ZfQueryBasedTable return FilterRenderer::applyToQuery($filter, $query); } + + /** + * @return IcingaObject + */ + protected function getDummyObject() + { + if ($this->dummyObject === null) { + $type = $this->type; + $this->dummyObject = IcingaObject::createByType($type); + } + return $this->dummyObject; + } + public function prepareQuery() { - $type = $this->type; + $table = $this->getDummyObject()->getTableName(); $columns = [ 'id' => 'o.id', 'object_name' => 'o.object_name', @@ -159,13 +185,13 @@ class ApplyRulesTable extends ZfQueryBasedTable 'assign_filter' => 'o.assign_filter', ]; $query = $this->db()->select()->from( - ['o' => "icinga_$type"], + ['o' => $table], $columns )->where( "object_type = 'apply'" )->order('o.object_name'); - if ($type === 'service') { + if ($this->type === 'service') { $query->where('service_set_id IS NULL'); } diff --git a/library/Director/Web/Table/ObjectsTable.php b/library/Director/Web/Table/ObjectsTable.php index bb49c162..4d20209e 100644 --- a/library/Director/Web/Table/ObjectsTable.php +++ b/library/Director/Web/Table/ObjectsTable.php @@ -33,6 +33,8 @@ class ObjectsTable extends ZfQueryBasedTable protected $type; + protected $baseObjectUrl; + /** @var IcingaObject */ protected $dummyObject; @@ -62,6 +64,17 @@ class ObjectsTable extends ZfQueryBasedTable return $this->type; } + /** + * @param string $url + * @return $this + */ + public function setBaseObjectUrl($url) + { + $this->baseObjectUrl = $url; + + return $this; + } + /** * @return Auth */ @@ -119,7 +132,7 @@ class ObjectsTable extends ZfQueryBasedTable protected function renderObjectNameColumn($row) { - $type = $this->getType(); + $type = $this->baseObjectUrl; $url = Url::fromPath("director/${type}", [ 'name' => $row->object_name ]); diff --git a/library/Director/Web/Tabs/ObjectTabs.php b/library/Director/Web/Tabs/ObjectTabs.php index f17f01e6..8953ccf1 100644 --- a/library/Director/Web/Tabs/ObjectTabs.php +++ b/library/Director/Web/Tabs/ObjectTabs.php @@ -117,7 +117,7 @@ class ObjectTabs extends Tabs if ($object->supportsRanges()) { $this->add('ranges', [ - 'url' => 'director/timeperiod/ranges', + 'url' => "director/${type}/ranges", 'urlParams' => $params, 'label' => $this->translate('Ranges') ]); diff --git a/library/Director/Web/Tabs/ObjectsTabs.php b/library/Director/Web/Tabs/ObjectsTabs.php index 7519eaee..017d042d 100644 --- a/library/Director/Web/Tabs/ObjectsTabs.php +++ b/library/Director/Web/Tabs/ObjectsTabs.php @@ -11,14 +11,14 @@ class ObjectsTabs extends Tabs { use TranslationHelper; - public function __construct($type, Auth $auth) + public function __construct($type, Auth $auth, $typeUrl) { $object = IcingaObject::createByType($type); if ($object->isGroup()) { - $object = IcingaObject::createByType(substr($type, 0, -5)); + $object = IcingaObject::createByType(substr($typeUrl, 0, -5)); } - $plType = strtolower(preg_replace('/cys$/', 'cies', $type . 's')); + $plType = strtolower(preg_replace('/cys$/', 'cies', $typeUrl . 's')); if ($auth->hasPermission("director/${plType}")) { $this->add('index', array( 'url' => sprintf('director/%s', $plType), @@ -56,7 +56,7 @@ class ObjectsTabs extends Tabs if ($object->supportsGroups()) { $this->add('groups', array( - 'url' => sprintf('director/%sgroups', $type), + 'url' => sprintf('director/%sgroups', $typeUrl), 'label' => $this->translate('Groups') )); } @@ -65,12 +65,12 @@ class ObjectsTabs extends Tabs if ($auth->hasPermission('director/admin')) { if ($object->supportsChoices()) { $this->add('choices', array( - 'url' => sprintf('director/templatechoices/%s', $type), + 'url' => sprintf('director/templatechoices/%s', $typeUrl), 'label' => $this->translate('Choices') )); } } - if ($object->supportsSets() && $auth->hasPermission("director/${type}sets")) { + if ($object->supportsSets() && $auth->hasPermission("director/${typeUrl}sets")) { $this->add('sets', array( 'url' => sprintf('director/%s/sets', $plType), 'label' => $this->translate('Sets')