icingaweb2-module-director/library/Director/Web/Controller/ObjectController.php

193 lines
5.8 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Director\Web\Controller;
use Icinga\Web\Url;
use Icinga\Module\Director\Objects\IcingaObject;
abstract class ObjectController extends ActionController
{
protected $object;
public function init()
{
$type = $this->getType();
$ltype = strtolower($type);
$params = array();
if ($object = $this->loadObject()) {
$this->getTabs()->add('modify', array(
'url' => sprintf('director/%s/edit', $ltype),
'urlParams' => $params,
'label' => $this->translate(ucfirst($ltype))
2015-07-28 15:15:08 +02:00
))->add('delete', array(
'url' => sprintf('director/%s/delete', $ltype),
'urlParams' => $params,
'label' => $this->translate('Delete')
))->add('render', array(
'url' => sprintf('director/%s/render', $ltype),
'urlParams' => $params,
'label' => $this->translate('Preview'),
))->add('history', array(
'url' => sprintf('director/%s/history', $ltype),
'urlParams' => $params,
'label' => $this->translate('History')
));
} else {
$this->getTabs()->add('add', array(
'url' => sprintf('director/%s/add', $type),
'label' => sprintf($this->translate('Add %s'), ucfirst($type)),
));
}
}
public function indexAction()
{
return $this->editAction();
}
public function renderAction()
{
$type = $this->getType();
$this->getTabs()->activate('render');
$this->view->object = $this->object();
$this->render('object/show', null, true);
}
2015-07-28 15:15:08 +02:00
public function deleteAction()
{
$this->getTabs()->activate('delete');
$type = $this->getType();
$ltype = strtolower($type);
$this->view->form = $form = $this->loadForm(
'icingaDeleteObject'
)->setObject($this->object());
$url = Url::fromPath(sprintf('director/%ss', $ltype));
$form->setSuccessUrl($url);
$this->view->title = sprintf(
$this->translate('Delete Icinga %s'),
ucfirst($ltype)
);
$this->view->form->handleRequest();
$this->render('object/form', null, true);
}
public function editAction()
{
$this->getTabs()->activate('modify');
$type = $this->getType();
$ltype = strtolower($type);
$this->view->form = $form = $this->loadForm(
'icinga' . ucfirst($type)
)->setDb($this->db());
2015-07-02 14:31:35 +02:00
$form->loadObject($this->params->get('name'));
$object = $form->getObject();
$url = Url::fromPath(
sprintf('director/%s', $ltype),
array('name' => $object->object_name)
);
$form->setSuccessUrl($url);
if ($object->isTemplate()) {
$title = $this->translate('Modify Icinga %s template');
$form->setObjectType('template'); // WHY??
} else {
$title = $this->translate('Modify Icinga %s');
}
$this->view->title = sprintf($title, ucfirst($ltype));
$this->view->form->handleRequest();
$this->render('object/form', null, true);
}
public function addAction()
{
$this->getTabs()->activate('add');
$type = $this->getType();
$ltype = strtolower($type);
$url = sprintf('director/%ss', $ltype);
$form = $this->view->form = $this->loadForm('icinga' . ucfirst($type))
->setDb($this->db())
->setSuccessUrl($url);
if ($this->params->get('type') === 'template') {
$form->setObjectType('template');
$title = $this->translate('Add new Icinga %s template');
2015-07-30 16:51:20 +02:00
} else {
$title = $this->translate('Add new Icinga %s');
}
$this->view->title = sprintf($title, ucfirst($ltype));
$form->handleRequest();
$this->render('object/form', null, true);
}
2015-08-02 15:01:47 +02:00
public function fieldsAction()
{
$object = $this->object;
$type = $this->getType();
$this->getTabs()->activate('fields');
$title = $this->translate('%s template "%s": custom fields');
$this->view->title = sprintf(
$title,
$this->translate(ucfirst($type)),
$object->object_name
);
$this->view->form = $this
->loadForm('icingaObjectField')
->setDb($this->db)
->setIcingaObject($object)
->handleRequest();
$this->view->table = $this
->loadTable('icingaObjectDatafield')
->setObject($object);
$this->render('object/fields', null, true);
}
public function historyAction()
{
$type = $this->getType();
$this->getTabs()->activate('history');
$object = $this->object();
$this->view->title = $this->translate('Activity Log');
$this->view->table = $this->applyPaginationLimits(
$this->loadTable('activityLog')->setConnection($this->db())
->filterObject('icinga_' . $type, $object->object_name)
);
$this->render('object/history', null, true);
}
protected function getType()
{
// Strip final 's' and upcase an eventual 'group'
return preg_replace(
array('/group$/', '/period$/', '/argument$/'),
array('Group', 'Period', 'Argument'),
$this->getRequest()->getControllerName()
);
}
protected function loadObject()
{
2015-07-02 14:31:35 +02:00
if ($name = $this->params->get('name')) {
$this->object = IcingaObject::loadByType(
$this->getType(),
$name,
$this->db()
);
}
return $this->object;
}
}