2015-06-30 11:19:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Controller;
|
|
|
|
|
|
|
|
use Icinga\Web\Url;
|
2015-08-02 15:01:07 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2015-06-30 11:19:31 +02:00
|
|
|
|
|
|
|
abstract class ObjectController extends ActionController
|
|
|
|
{
|
|
|
|
protected $object;
|
|
|
|
|
2015-12-10 13:11:21 +01:00
|
|
|
protected $isApified = true;
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2015-12-10 13:11:21 +01:00
|
|
|
parent::init();
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
$type = $this->getType();
|
2015-08-02 15:03:35 +02:00
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
$params = array();
|
2015-08-02 15:01:07 +02:00
|
|
|
if ($object = $this->loadObject()) {
|
2015-06-30 11:19:31 +02:00
|
|
|
|
2015-08-02 15:03:35 +02:00
|
|
|
$params['name'] = $object->object_name;
|
|
|
|
|
|
|
|
$tabs = $this->getTabs()->add('modify', array(
|
|
|
|
'url' => sprintf('director/%s/edit', $type),
|
2015-07-28 15:15:08 +02:00
|
|
|
'urlParams' => $params,
|
2015-08-02 15:03:35 +02:00
|
|
|
'label' => $this->translate(ucfirst($type))
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($object->hasBeenLoadedFromDb()
|
|
|
|
&& $object->supportsFields()
|
|
|
|
&& $object->isTemplate()
|
|
|
|
) {
|
|
|
|
$tabs->add('fields', array(
|
|
|
|
'url' => sprintf('director/%s/fields', $type),
|
|
|
|
'urlParams' => $params,
|
|
|
|
'label' => $this->translate('Fields')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$tabs->add('render', array(
|
|
|
|
'url' => sprintf('director/%s/render', $type),
|
2015-07-30 10:04:12 +02:00
|
|
|
'urlParams' => $params,
|
|
|
|
'label' => $this->translate('Preview'),
|
2015-06-30 11:19:31 +02:00
|
|
|
))->add('history', array(
|
2015-08-02 15:03:35 +02:00
|
|
|
'url' => sprintf('director/%s/history', $type),
|
2015-06-30 11:19:31 +02:00
|
|
|
'urlParams' => $params,
|
|
|
|
'label' => $this->translate('History')
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$this->getTabs()->add('add', array(
|
2015-07-30 10:04:12 +02:00
|
|
|
'url' => sprintf('director/%s/add', $type),
|
2015-06-30 11:19:31 +02:00
|
|
|
'label' => sprintf($this->translate('Add %s'), ucfirst($type)),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function indexAction()
|
2015-07-30 10:04:12 +02:00
|
|
|
{
|
2015-12-10 13:11:21 +01:00
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
|
|
|
return $this->sendJson(
|
|
|
|
$this->object->toPlainObject($this->params->shift('resolved'))
|
|
|
|
);
|
2015-12-02 03:32:42 +01:00
|
|
|
}
|
|
|
|
|
2015-07-30 10:04:12 +02:00
|
|
|
return $this->editAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderAction()
|
2015-06-30 11:19:31 +02:00
|
|
|
{
|
|
|
|
$type = $this->getType();
|
2015-07-30 10:04:12 +02:00
|
|
|
$this->getTabs()->activate('render');
|
2015-08-02 15:15:52 +02:00
|
|
|
$this->view->object = $this->object;
|
2015-10-15 23:49:34 +02:00
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Config preview: %s'),
|
|
|
|
$this->object->object_name
|
|
|
|
);
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->render('object/show', null, true);
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:55:13 +02:00
|
|
|
// TODO: Remove or leave here for API access only. Probably not needed.
|
2015-07-28 15:15:08 +02:00
|
|
|
public function deleteAction()
|
|
|
|
{
|
2015-08-03 13:41:22 +02:00
|
|
|
$type = $this->getType();
|
2015-07-28 15:15:08 +02:00
|
|
|
$this->getTabs()->activate('delete');
|
|
|
|
|
|
|
|
$this->view->form = $form = $this->loadForm(
|
|
|
|
'icingaDeleteObject'
|
2015-08-02 15:03:35 +02:00
|
|
|
)->setObject($this->object);
|
2015-07-28 15:15:08 +02:00
|
|
|
|
2015-08-02 15:03:35 +02:00
|
|
|
$url = Url::fromPath(sprintf('director/%ss', $type));
|
2015-07-28 15:15:08 +02:00
|
|
|
$form->setSuccessUrl($url);
|
|
|
|
|
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Delete Icinga %s'),
|
2015-08-02 15:03:35 +02:00
|
|
|
ucfirst($type)
|
2015-07-28 15:15:08 +02:00
|
|
|
);
|
|
|
|
$this->view->form->handleRequest();
|
|
|
|
$this->render('object/form', null, true);
|
|
|
|
}
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function editAction()
|
|
|
|
{
|
2015-08-02 15:03:35 +02:00
|
|
|
$object = $this->object;
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->getTabs()->activate('modify');
|
2015-08-02 15:03:35 +02:00
|
|
|
$ltype = $this->getType();
|
|
|
|
$type = ucfirst($ltype);
|
2015-06-30 11:19:31 +02:00
|
|
|
|
2015-08-02 15:03:35 +02:00
|
|
|
$formName = 'icinga' . $type;
|
|
|
|
$this->view->form = $form = $this->loadForm($formName)->setDb($this->db());
|
|
|
|
$form->setObject($object);
|
2015-06-30 11:19:31 +02:00
|
|
|
|
|
|
|
$url = Url::fromPath(
|
2015-08-02 15:03:35 +02:00
|
|
|
sprintf('director/%s', $type),
|
2015-07-31 14:50:05 +02:00
|
|
|
array('name' => $object->object_name)
|
2015-06-30 11:19:31 +02:00
|
|
|
);
|
|
|
|
$form->setSuccessUrl($url);
|
|
|
|
|
2015-07-31 14:50:05 +02:00
|
|
|
if ($object->isTemplate()) {
|
|
|
|
$title = $this->translate('Modify Icinga %s template');
|
2015-07-31 17:33:05 +02:00
|
|
|
$form->setObjectType('template'); // WHY??
|
2015-07-31 14:50:05 +02:00
|
|
|
} else {
|
|
|
|
$title = $this->translate('Modify Icinga %s');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->title = sprintf($title, ucfirst($ltype));
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->view->form->handleRequest();
|
2015-08-02 15:03:35 +02:00
|
|
|
|
2015-12-17 19:52:10 +01:00
|
|
|
$this->view->actionLinks = $this->view->qlink(
|
|
|
|
sprintf($this->translate('Clone'), $this->translate(ucfirst($ltype))),
|
|
|
|
'director/' . $ltype .'/clone',
|
|
|
|
array('name' => $object->object_name)
|
|
|
|
);
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->render('object/form', null, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addAction()
|
|
|
|
{
|
|
|
|
$this->getTabs()->activate('add');
|
|
|
|
$type = $this->getType();
|
|
|
|
$ltype = strtolower($type);
|
|
|
|
|
|
|
|
$url = sprintf('director/%ss', $ltype);
|
2015-07-30 09:26:02 +02:00
|
|
|
$form = $this->view->form = $this->loadForm('icinga' . ucfirst($type))
|
2015-06-30 11:19:31 +02:00
|
|
|
->setDb($this->db())
|
|
|
|
->setSuccessUrl($url);
|
|
|
|
|
2015-07-30 09:26:02 +02:00
|
|
|
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');
|
2015-07-30 09:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->title = sprintf($title, ucfirst($ltype));
|
|
|
|
$form->handleRequest();
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->render('object/form', null, true);
|
2015-12-10 13:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cloneAction()
|
|
|
|
{
|
|
|
|
$type = $this->getType();
|
2015-12-17 19:52:10 +01:00
|
|
|
$ltype = strtolower($type);
|
2015-12-10 13:19:16 +01:00
|
|
|
$this->getTabs()->activate('modify');
|
|
|
|
|
|
|
|
$this->view->form = $form = $this->loadForm(
|
|
|
|
'icingaCloneObject'
|
|
|
|
)->setObject($this->object);
|
|
|
|
|
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Clone Icinga %s'),
|
|
|
|
ucfirst($type)
|
|
|
|
);
|
|
|
|
$this->view->form->handleRequest();
|
2015-12-17 19:52:10 +01:00
|
|
|
|
|
|
|
$this->view->actionLinks = $this->view->qlink(
|
|
|
|
sprintf($this->translate('<- back'), $this->translate(ucfirst($ltype))),
|
|
|
|
'director/' . $ltype,
|
|
|
|
array('name' => $this->object->object_name)
|
|
|
|
);
|
|
|
|
|
2015-12-10 13:19:16 +01:00
|
|
|
$this->render('object/form', null, true);
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2015-09-14 16:54:43 +02:00
|
|
|
$form = $this->view->form = $this
|
2015-08-02 15:01:47 +02:00
|
|
|
->loadForm('icingaObjectField')
|
|
|
|
->setDb($this->db)
|
2015-09-14 16:54:43 +02:00
|
|
|
->setIcingaObject($object);
|
|
|
|
|
|
|
|
if ($id = $this->params->get('field_id')) {
|
2015-10-13 17:58:50 +02:00
|
|
|
$form->loadObject(array(
|
|
|
|
$type . '_id' => $object->id,
|
|
|
|
'datafield_id' => $id
|
|
|
|
));
|
2015-09-14 16:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$form->handleRequest();
|
2015-08-02 15:01:47 +02:00
|
|
|
|
|
|
|
$this->view->table = $this
|
|
|
|
->loadTable('icingaObjectDatafield')
|
|
|
|
->setObject($object);
|
|
|
|
|
|
|
|
$this->render('object/fields', null, true);
|
|
|
|
}
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function historyAction()
|
|
|
|
{
|
|
|
|
$type = $this->getType();
|
|
|
|
$this->getTabs()->activate('history');
|
|
|
|
$this->view->title = $this->translate('Activity Log');
|
2015-07-23 16:19:22 +02:00
|
|
|
$this->view->table = $this->applyPaginationLimits(
|
|
|
|
$this->loadTable('activityLog')->setConnection($this->db())
|
2015-08-02 15:15:52 +02:00
|
|
|
->filterObject('icinga_' . $type, $this->object->object_name)
|
2015-07-23 16:19:22 +02:00
|
|
|
);
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->render('object/history', null, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getType()
|
|
|
|
{
|
|
|
|
// Strip final 's' and upcase an eventual 'group'
|
|
|
|
return preg_replace(
|
2015-12-17 14:58:43 +01:00
|
|
|
array('/group$/', '/period$/', '/argument$/', '/apiuser$/'),
|
|
|
|
array('Group', 'Period', 'Argument', 'ApiUser'),
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->getRequest()->getControllerName()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-02 15:01:07 +02:00
|
|
|
protected function loadObject()
|
2015-06-30 11:19:31 +02:00
|
|
|
{
|
2015-08-03 13:41:42 +02:00
|
|
|
if ($this->object === null && $name = $this->params->get('name')) {
|
2015-08-02 15:01:07 +02:00
|
|
|
$this->object = IcingaObject::loadByType(
|
|
|
|
$this->getType(),
|
|
|
|
$name,
|
|
|
|
$this->db()
|
|
|
|
);
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->object;
|
|
|
|
}
|
|
|
|
}
|