| 1 |
<?php |
| 2 |
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */ |
| 3 |
|
| 4 |
namespace Icinga\Module\Toplevelview\Controllers; |
| 5 |
|
| 6 |
use Icinga\Module\Toplevelview\Model\View; |
| 7 |
use Icinga\Module\Toplevelview\Forms\EditForm; |
| 8 |
use Icinga\Module\Toplevelview\ViewConfig; |
| 9 |
use Icinga\Module\Toplevelview\Web\Controller; |
| 10 |
|
| 11 |
use Icinga\Web\Url; |
| 12 |
use Icinga\Application\Icinga; |
| 13 |
|
| 14 |
class EditController extends Controller |
| 15 |
{ |
| 16 |
public function init() |
| 17 |
{ |
| 18 |
$this->assertPermission('toplevelview/edit'); |
| 19 |
|
| 20 |
$tabs = $this->getTabs(); |
| 21 |
|
| 22 |
if ($name = $this->getParam('name')) { |
| 23 |
$tabs->add('tiles', [ |
| 24 |
'title' => $this->translate('Tiles'), |
| 25 |
'url' => Url::fromPath('toplevelview/show', ['name' => $name]) |
| 26 |
]); |
| 27 |
|
| 28 |
$tabs->add('index', [ |
| 29 |
'title' => $this->translate('Edit'), |
| 30 |
'url' => Url::fromPath('toplevelview/edit', ['name' => $name]) |
| 31 |
]); |
| 32 |
} |
| 33 |
|
| 34 |
$action = $this->getRequest()->getActionName(); |
| 35 |
|
| 36 |
if ($tab = $tabs->get($action)) { |
| 37 |
$tab->setActive(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public function indexAction() |
| 42 |
{ |
| 43 |
$action = $this->getRequest()->getActionName(); |
| 44 |
|
| 45 |
$config_dir_module = Icinga::app() |
| 46 |
->getModuleManager() |
| 47 |
->getModule('toplevelview') |
| 48 |
->getConfigDir(); |
| 49 |
|
| 50 |
$c = new ViewConfig($config_dir_module); |
| 51 |
$view = null; |
| 52 |
|
| 53 |
if ($action === 'add') { |
| 54 |
$this->view->title = sprintf('%s Top Level View', $this->translate('Add')); |
| 55 |
$view = new View('', $c::FORMAT_YAML); |
| 56 |
} elseif ($action === 'clone') { |
| 57 |
// Clone the view and give it to the View |
| 58 |
$name = $this->params->getRequired('name'); |
| 59 |
$this->view->title = sprintf('%s Top Level View', $this->translate('Clone')); |
| 60 |
|
| 61 |
// Check if the user has permissions/restrictions for this View |
| 62 |
$restrictions = $c->getRestrictions('toplevelview/filter/edit'); |
| 63 |
$c->assertAccessToView($restrictions, $name); |
| 64 |
|
| 65 |
$view = clone $c->loadByName($name); |
| 66 |
} else { |
| 67 |
$this->view->name = $name = $this->params->getRequired('name'); |
| 68 |
$this->view->title = sprintf('%s Top Level View: %s', $this->translate('Edit'), $this->params->getRequired('name')); |
| 69 |
|
| 70 |
// Check if the user has permissions/restrictions for this View |
| 71 |
$restrictions = $c->getRestrictions('toplevelview/filter/edit'); |
| 72 |
$c->assertAccessToView($restrictions, $name); |
| 73 |
|
| 74 |
$view = $c->loadByName($name); |
| 75 |
} |
| 76 |
|
| 77 |
$this->view->form = $form = new EditForm(); |
| 78 |
$form->setViewConfig($c); |
| 79 |
$form->setViews($view); |
| 80 |
|
| 81 |
$form->handleRequest(); |
| 82 |
$this->setViewScript('edit/index'); |
| 83 |
} |
| 84 |
|
| 85 |
public function addAction() |
| 86 |
{ |
| 87 |
$this->indexAction(); |
| 88 |
} |
| 89 |
|
| 90 |
public function cloneAction() |
| 91 |
{ |
| 92 |
$this->indexAction(); |
| 93 |
} |
| 94 |
} |