From 90c7bf0dc157933420f0bf911e310210a64a0e70 Mon Sep 17 00:00:00 2001 From: Valentina Da Rold Date: Tue, 1 Oct 2019 16:57:10 +0200 Subject: [PATCH] Add DataFieldCategoryController, Tabs and Table --- application/controllers/DataController.php | 19 ++++++ .../DatafieldcategoryController.php | 46 +++++++++++++ .../Web/Table/DatafieldCategoryTable.php | 66 +++++++++++++++++++ library/Director/Web/Tabs/DataTabs.php | 3 + 4 files changed, 134 insertions(+) create mode 100644 application/controllers/DatafieldcategoryController.php create mode 100644 library/Director/Web/Table/DatafieldCategoryTable.php diff --git a/application/controllers/DataController.php b/application/controllers/DataController.php index d3d10345..7ed5e123 100644 --- a/application/controllers/DataController.php +++ b/application/controllers/DataController.php @@ -7,6 +7,7 @@ use Icinga\Module\Director\Forms\DirectorDatalistForm; use Icinga\Module\Director\Objects\DirectorDatalist; use Icinga\Module\Director\Web\Controller\ActionController; use Icinga\Module\Director\Web\Table\CustomvarTable; +use Icinga\Module\Director\Web\Table\DatafieldCategoryTable; use Icinga\Module\Director\Web\Table\DatafieldTable; use Icinga\Module\Director\Web\Table\DatalistEntryTable; use Icinga\Module\Director\Web\Table\DatalistTable; @@ -74,6 +75,24 @@ class DataController extends ActionController (new DatafieldTable($this->db()))->renderTo($this); } + public function fieldcategoriesAction() + { + $this->setAutorefreshInterval(10); + $this->tabs(new DataTabs())->activate('datafieldcategory'); + $this->addTitle($this->translate('Data Field Categories')); + $this->actions()->add(Link::create( + $this->translate('Add'), + 'director/datafieldcategory/add', + null, + [ + 'class' => 'icon-plus', + 'data-base-target' => '_next', + ] + )); + + (new DatafieldCategoryTable($this->db()))->renderTo($this); + } + public function varsAction() { $this->tabs(new DataTabs())->activate('customvars'); diff --git a/application/controllers/DatafieldcategoryController.php b/application/controllers/DatafieldcategoryController.php new file mode 100644 index 00000000..32c76ef6 --- /dev/null +++ b/application/controllers/DatafieldcategoryController.php @@ -0,0 +1,46 @@ +indexAction(); + } + + public function editAction() + { + $this->indexAction(); + } + + public function indexAction() + { + $edit = false; + + if ($name = $this->params->get('name')) { + $edit = true; + } + + $form = DirectorDatafieldCategoryForm::load() + ->setDb($this->db()); + + if ($edit) { + $form->loadObject($name); + $this->addTitle( + $this->translate('Modify %s'), + $form->getObject()->category_name + ); + $this->addSingleTab($this->translate('Edit a Category')); + } else { + $this->addTitle($this->translate('Add a new Data Field Category')); + $this->addSingleTab($this->translate('New Category')); + } + + $form->handleRequest(); + $this->content()->add($form); + } +} diff --git a/library/Director/Web/Table/DatafieldCategoryTable.php b/library/Director/Web/Table/DatafieldCategoryTable.php new file mode 100644 index 00000000..b2593e97 --- /dev/null +++ b/library/Director/Web/Table/DatafieldCategoryTable.php @@ -0,0 +1,66 @@ + 'dfc.id', + 'category_name' => 'dfc.category_name', + 'description' => 'dfc.description', + 'assigned_fields' => 'COUNT(df.id)', + ); + } + + public function renderRow($row) + { + $main = [Link::create( + $row->category_name, + 'director/datafieldcategory/edit', + ['name' => $row->category_name] + )]; + + if (strlen($row->description)) { + $main[] = Html::tag('br'); + $main[] = Html::tag('small', $row->description); + } + return $this::tr([ + $this::td($main), + $this::td($row->assigned_fields) + ]); + } + + public function getColumnsToBeRendered() + { + return array( + $this->translate('Category Name'), + $this->translate('# Used'), + ); + } + + public function prepareQuery() + { + $db = $this->db(); + return $db->select()->from( + ['dfc' => 'director_datafield_category'], + $this->getColumns() + )->joinLeft( + ['df' => 'director_datafield'], + 'df.category_id = dfc.id', + [] + )->group('dfc.id')->group('dfc.category_name')->order('category_name ASC'); + } +} diff --git a/library/Director/Web/Tabs/DataTabs.php b/library/Director/Web/Tabs/DataTabs.php index 791ca14a..ac29310b 100644 --- a/library/Director/Web/Tabs/DataTabs.php +++ b/library/Director/Web/Tabs/DataTabs.php @@ -20,6 +20,9 @@ class DataTabs extends Tabs $this->add('datafield', [ 'label' => $this->translate('Data fields'), 'url' => 'director/data/fields' + ])->add('datafieldcategory', [ + 'label' => $this->translate('Data field categories'), + 'url' => 'director/data/fieldcategories' ])->add('datalist', [ 'label' => $this->translate('Data lists'), 'url' => 'director/data/lists'