ObjectController: code cleanup, use fields

This commit is contained in:
Thomas Gelf 2015-08-02 15:03:35 +02:00
parent 1bc7bc2cd9
commit 237aaffc45

View File

@ -12,26 +12,41 @@ abstract class ObjectController extends ActionController
public function init() public function init()
{ {
$type = $this->getType(); $type = $this->getType();
$ltype = strtolower($type);
$params = array(); $params = array();
if ($object = $this->loadObject()) { if ($object = $this->loadObject()) {
$this->getTabs()->add('modify', array( $params['name'] = $object->object_name;
'url' => sprintf('director/%s/edit', $ltype),
$tabs = $this->getTabs()->add('modify', array(
'url' => sprintf('director/%s/edit', $type),
'urlParams' => $params, 'urlParams' => $params,
'label' => $this->translate(ucfirst($ltype)) 'label' => $this->translate(ucfirst($type))
))->add('delete', array( ));
'url' => sprintf('director/%s/delete', $ltype),
'urlParams' => $params, if ($object->hasBeenLoadedFromDb()
'label' => $this->translate('Delete') && $object->supportsFields()
))->add('render', array( && $object->isTemplate()
'url' => sprintf('director/%s/render', $ltype), ) {
$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),
'urlParams' => $params, 'urlParams' => $params,
'label' => $this->translate('Preview'), 'label' => $this->translate('Preview'),
))->add('history', array( ))->add('history', array(
'url' => sprintf('director/%s/history', $ltype), 'url' => sprintf('director/%s/history', $type),
'urlParams' => $params, 'urlParams' => $params,
'label' => $this->translate('History') 'label' => $this->translate('History')
))->add('delete', array(
'url' => sprintf('director/%s/delete', $type),
'urlParams' => $params,
'label' => $this->translate('Delete')
)); ));
} else { } else {
$this->getTabs()->add('add', array( $this->getTabs()->add('add', array(
@ -57,19 +72,17 @@ abstract class ObjectController extends ActionController
public function deleteAction() public function deleteAction()
{ {
$this->getTabs()->activate('delete'); $this->getTabs()->activate('delete');
$type = $this->getType();
$ltype = strtolower($type);
$this->view->form = $form = $this->loadForm( $this->view->form = $form = $this->loadForm(
'icingaDeleteObject' 'icingaDeleteObject'
)->setObject($this->object()); )->setObject($this->object);
$url = Url::fromPath(sprintf('director/%ss', $ltype)); $url = Url::fromPath(sprintf('director/%ss', $type));
$form->setSuccessUrl($url); $form->setSuccessUrl($url);
$this->view->title = sprintf( $this->view->title = sprintf(
$this->translate('Delete Icinga %s'), $this->translate('Delete Icinga %s'),
ucfirst($ltype) ucfirst($type)
); );
$this->view->form->handleRequest(); $this->view->form->handleRequest();
$this->render('object/form', null, true); $this->render('object/form', null, true);
@ -77,18 +90,17 @@ abstract class ObjectController extends ActionController
public function editAction() public function editAction()
{ {
$object = $this->object;
$this->getTabs()->activate('modify'); $this->getTabs()->activate('modify');
$type = $this->getType(); $ltype = $this->getType();
$ltype = strtolower($type); $type = ucfirst($ltype);
$this->view->form = $form = $this->loadForm( $formName = 'icinga' . $type;
'icinga' . ucfirst($type) $this->view->form = $form = $this->loadForm($formName)->setDb($this->db());
)->setDb($this->db()); $form->setObject($object);
$form->loadObject($this->params->get('name'));
$object = $form->getObject();
$url = Url::fromPath( $url = Url::fromPath(
sprintf('director/%s', $ltype), sprintf('director/%s', $type),
array('name' => $object->object_name) array('name' => $object->object_name)
); );
$form->setSuccessUrl($url); $form->setSuccessUrl($url);
@ -102,6 +114,7 @@ abstract class ObjectController extends ActionController
$this->view->title = sprintf($title, ucfirst($ltype)); $this->view->title = sprintf($title, ucfirst($ltype));
$this->view->form->handleRequest(); $this->view->form->handleRequest();
$this->render('object/form', null, true); $this->render('object/form', null, true);
} }