Skip to content

Commit

Permalink
[WIP] Refactor Views and ViewConfig
Browse files Browse the repository at this point in the history
 - In preparation for adding restrictions
  • Loading branch information
martialblog committed Jul 16, 2024
1 parent 54c1aa2 commit 902157e
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 405 deletions.
20 changes: 13 additions & 7 deletions application/controllers/EditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

namespace Icinga\Module\Toplevelview\Controllers;

use Icinga\Module\Toplevelview\Model\View;
use Icinga\Module\Toplevelview\Forms\EditForm;
use Icinga\Module\Toplevelview\ViewConfig;
use Icinga\Module\Toplevelview\Web\Controller;

use Icinga\Web\Url;

class EditController extends Controller
Expand Down Expand Up @@ -39,26 +41,30 @@ public function indexAction()
{
$action = $this->getRequest()->getActionName();

$c = new ViewConfig();
$view = null;

if ($action === 'add') {
$this->view->title = sprintf('%s Top Level View', $this->translate('Add'));
$view = new ViewConfig();
$view->setConfigDir();
$view = new View('', $c::FORMAT_YAML);
} elseif ($action === 'clone') {
$name = $this->params->getRequired('name');
$this->view->title = sprintf('%s Top Level View', $this->translate('Clone'));
$view = clone ViewConfig::loadByName($name);
// Clone the view and give it to the $config
$view = clone $c->loadByName($name);
} else {
$this->view->name = $name = $this->params->getRequired('name');
$this->view->title = sprintf('%s Top Level View: %s', $this->translate('Edit'), $this->params->getRequired('name'));
$view = ViewConfig::loadByName($name);
$view = $c->loadByName($name);
}

$view->setFormat($c::FORMAT_YAML);

$this->view->form = $form = new EditForm();
$form->setViewConfig($c);
$form->setViews($view);

$view->setFormat(ViewConfig::FORMAT_YAML);
$form->setViewConfig($view);
$form->handleRequest();

$this->setViewScript('edit/index');
}

Expand Down
5 changes: 4 additions & 1 deletion application/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public function indexAction()
])->activate('index');

// Load add views from the existing YAML files
$this->view->views = ViewConfig::loadAll();
$c = new ViewConfig();
$views = $c->loadAll();

$this->view->views = $views;

$this->setAutorefreshInterval(30);
}
Expand Down
17 changes: 14 additions & 3 deletions application/controllers/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Icinga\Module\Toplevelview\ViewConfig;
use Icinga\Module\Toplevelview\Web\Controller;

use Icinga\Web\Url;

class ShowController extends Controller
Expand Down Expand Up @@ -43,7 +44,11 @@ public function init()
public function indexAction()
{
$this->view->name = $name = $this->params->getRequired('name');
$this->view->view = $view = ViewConfig::loadByName($name);

$c = new ViewConfig();
$view = $c->loadByName($name);
$this->view->view = $view;

$tree = $view->getTree();

if (($lifetime = $this->getParam('cache')) !== null) {
Expand All @@ -56,7 +61,10 @@ public function indexAction()
public function treeAction()
{
$this->view->name = $name = $this->params->getRequired('name');
$this->view->view = $view = ViewConfig::loadByName($name);

$c = new ViewConfig();
$view = $c->loadByName($name);
$this->view->view = $view;

$tree = $view->getTree();

Expand All @@ -72,7 +80,10 @@ public function treeAction()
public function sourceAction()
{
$this->view->name = $name = $this->params->getRequired('name');
$this->view->view = $view = ViewConfig::loadByName($name);

$c = new ViewConfig();
$view = $c->loadByName($name);
$this->view->view = $view;

$this->view->text = $view->getText();
$this->setViewScript('index', 'text');
Expand Down
53 changes: 33 additions & 20 deletions application/forms/EditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace Icinga\Module\Toplevelview\Forms;

use Exception;
use Icinga\Module\Toplevelview\ViewConfig;
use Icinga\Module\Toplevelview\Model\View;

use Exception;
use Icinga\Web\Form;
use Icinga\Web\Notification;
use Icinga\Web\Url;
Expand All @@ -14,7 +16,12 @@ class EditForm extends Form
/**
* @var ViewConfig
*/
protected $viewConfig;
protected $viewconfig;

/**
* @var View
*/
protected $view;

/**
* {@inheritdoc}
Expand All @@ -24,9 +31,15 @@ public function init()
$this->setName('form_toplevelview_edit');
}

public function setViewConfig(ViewConfig $viewConfig)
public function setViews(View $view)
{
$this->view = $view;
return $this;
}

public function setViewConfig(ViewConfig $config)
{
$this->viewConfig = $viewConfig;
$this->viewconfig = $config;
return $this;
}

Expand All @@ -36,26 +49,27 @@ public function setViewConfig(ViewConfig $viewConfig)
public function onSuccess()
{
try {
$this->viewConfig->setName($this->getValue('name'));
$this->viewConfig->setText($this->getValue('config'));
$this->view->setName($this->getValue('name'));
$this->view->setText($this->getValue('config'));

// ensure config can be parsed...
$this->viewConfig->getMetaData();
$this->viewConfig->getTree();
$this->view->getMetaData();
$this->view->getTree();

$this->viewConfig->storeToSession();
// Store the view to the session
$this->viewconfig->storeToSession($this->view);

$cancel = $this->getElement('btn_submit_cancel');
$delete = $this->getElement('btn_submit_delete');

if ($this->getElement('btn_submit_save_file')->getValue() !== null) {
$this->viewConfig->store();
$this->viewconfig->storeToFile($this->view);
Notification::success($this->translate('Top Level View successfully saved'));
} elseif ($cancel !== null && $cancel->getValue() !== null) {
$this->viewConfig->clearSession();
$this->viewconfig->clearSession($this->view);
Notification::success($this->translate('Top Level View restored from disk'));
} elseif ($delete != null && $delete->getValue() !== null) {
$this->viewConfig->delete();
$this->viewconfig->delete($this->view);
$this->setRedirectUrl('toplevelview');
Notification::success($this->translate('Top Level View successfully deleted'));
} else {
Expand All @@ -70,8 +84,8 @@ public function onSuccess()

public function getRedirectUrl()
{
if ($this->redirectUrl === null && ($name = $this->viewConfig->getName()) !== null) {
$this->redirectUrl = Url::fromPath('toplevelview/show', array('name' => $name));
if ($this->redirectUrl === null && ($name = $this->view->getName()) !== null) {
$this->redirectUrl = Url::fromPath('toplevelview/show', ['name' => $name]);
}
return parent::getRedirectUrl();
}
Expand All @@ -84,8 +98,8 @@ public function getRedirectUrl()
public function onRequest()
{
$values = array();
$values['name'] = $this->viewConfig->getName();
$values['config'] = $this->viewConfig->getText();
$values['name'] = $this->view->getName();
$values['config'] = $this->view->getText();

$this->populate($values);
}
Expand All @@ -95,7 +109,7 @@ public function onRequest()
*/
public function createElements(array $formData)
{
if ($this->viewConfig->hasBeenLoadedFromSession()) {
if ($this->view->hasBeenLoadedFromSession()) {
$this->warning(
$this->translate(
'This config is only stored in your session!'
Expand All @@ -117,7 +131,6 @@ public function createElements(array $formData)
'textarea',
'config',
array(
//'required' => true,
'label' => $this->translate('YAML Config'),
'class' => 'code-editor codemirror',
'decorators' => array(
Expand Down Expand Up @@ -149,7 +162,7 @@ public function createElements(array $formData)
)
);

if ($this->viewConfig->hasBeenLoadedFromSession()) {
if ($this->view->hasBeenLoadedFromSession()) {
$this->addElement(
'submit',
'btn_submit_cancel',
Expand All @@ -162,7 +175,7 @@ public function createElements(array $formData)
);
}

if ($this->viewConfig->hasBeenLoaded()) {
if ($this->view->hasBeenLoaded()) {
$this->addElement(
'submit',
'btn_submit_delete',
Expand Down
2 changes: 1 addition & 1 deletion application/views/helpers/Tiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function tiles(TLVTreeNode $node, $levels = 2, $classes = array())
$title . $badges,
'toplevelview/show/tree',
array(
'name' => $node->getRoot()->getConfig()->getName(),
'name' => $node->getRoot()->getView()->getName(),
'id' => $node->getFullId()
),
array(
Expand Down
2 changes: 1 addition & 1 deletion application/views/helpers/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function tree(TLVTreeNode $node, $classes = array(), $level = 0)
$url = Url::fromPath(
'toplevelview/show/tree',
array(
'name' => $node->getRoot()->getConfig()->getName(),
'name' => $node->getRoot()->getView()->getName(),
'id' => $node->getFullId()
)
);
Expand Down
14 changes: 13 additions & 1 deletion configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@

$this->providePermission('toplevelview/edit', $this->translate('Allow the user to edit Top Level Views'));

// TODO Implement these restrictions in the ViewConfig
// $this->provideRestriction(
// 'toplevelview/filter/edit',
// $this->translate('Restrict edit rights to Views that match the filter (comma-separated values)')
// );

// $this->provideRestriction(
// 'toplevelview/filter/views',
// $this->translate('Restrict access to Views that match the filter (comma-separated values)')
// );

/** @var \Icinga\Web\Navigation\NavigationItem $section */
$section = $this->menuSection('toplevelview');
$section
Expand All @@ -19,7 +30,8 @@
try {
if (extension_loaded('yaml')) {
/** @var \Icinga\Application\Modules\MenuItemContainer $section */
$views = ViewConfig::loadAll();
$c = new ViewConfig();
$views = $c->loadAll();

foreach ($views as $name => $viewConfig) {
$section->add($name, array(
Expand Down
Loading

0 comments on commit 902157e

Please sign in to comment.