parent
db227f9851
commit
bb25de6126
|
@ -11,6 +11,7 @@ use Icinga\Module\Director\Exception\NestingError;
|
||||||
use Icinga\Module\Director\IcingaConfig\StateFilterSet;
|
use Icinga\Module\Director\IcingaConfig\StateFilterSet;
|
||||||
use Icinga\Module\Director\IcingaConfig\TypeFilterSet;
|
use Icinga\Module\Director\IcingaConfig\TypeFilterSet;
|
||||||
use Icinga\Module\Director\Objects\IcingaObject;
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
|
use Icinga\Module\Director\Util;
|
||||||
use Zend_Form_Element as ZfElement;
|
use Zend_Form_Element as ZfElement;
|
||||||
use Zend_Form_Element_Select as ZfSelect;
|
use Zend_Form_Element_Select as ZfSelect;
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
|
|
||||||
protected $preferredObjectType;
|
protected $preferredObjectType;
|
||||||
|
|
||||||
|
/** @var IcingaObjectFieldLoader */
|
||||||
protected $fieldLoader;
|
protected $fieldLoader;
|
||||||
|
|
||||||
private $allowsExperimental;
|
private $allowsExperimental;
|
||||||
|
@ -47,6 +49,16 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
|
|
||||||
private $presetImports;
|
private $presetImports;
|
||||||
|
|
||||||
|
private $earlyProperties = array(
|
||||||
|
'imports',
|
||||||
|
'check_command',
|
||||||
|
'check_command_id',
|
||||||
|
'command',
|
||||||
|
'command_id',
|
||||||
|
'event_command',
|
||||||
|
'event_command_id',
|
||||||
|
);
|
||||||
|
|
||||||
public function setPreferredObjectType($type)
|
public function setPreferredObjectType($type)
|
||||||
{
|
{
|
||||||
$this->preferredObjectType = $type;
|
$this->preferredObjectType = $type;
|
||||||
|
@ -112,9 +124,23 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->hasBeenSent()) {
|
if ($this->hasBeenSent()) {
|
||||||
if ($el = $this->getElement('imports')) {
|
// prefill special properties, required to resolve fields and similar
|
||||||
$this->populate($this->getRequest()->getPost());
|
$post = $this->getRequest()->getPost();
|
||||||
$object->set('imports', $el->getValue());
|
foreach ($this->earlyProperties as $key) {
|
||||||
|
if ($el = $this->getElement($key)) {
|
||||||
|
if (array_key_exists($key, $post)) {
|
||||||
|
$this->populate(array($key => $post[$key]));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$old = $object->get($key);
|
||||||
|
$object->set($key, $el->getValue());
|
||||||
|
$object->resolveUnresolvedRelatedProperties();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$object->set($key, $old);
|
||||||
|
$this->addException($e, $key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,7 +261,7 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
$resolve = $this->assertResolvedImports();
|
$resolve = $this->assertResolvedImports();
|
||||||
if ($this->hasBeenSent()) {
|
if ($this->hasBeenSent()) {
|
||||||
foreach ($values as $key => $value) {
|
foreach ($values as $key => $value) {
|
||||||
if ($key === 'imports' || substr($key, 0, 4) === 'var_') {
|
if (in_array($key, $this->earlyProperties) || substr($key, 0, 4) === 'var_') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,7 +292,11 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
$this->setDefaults($this->removeEmptyProperties($props));
|
$this->setDefaults($this->removeEmptyProperties($props));
|
||||||
|
|
||||||
if ($resolve) {
|
if ($resolve) {
|
||||||
$this->showInheritedProperties($object);
|
try {
|
||||||
|
$this->showInheritedProperties($object);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->addException($e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,11 +327,11 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function loadFields($object)
|
protected function prepareFields($object)
|
||||||
{
|
{
|
||||||
if ($this->assertResolvedImports()) {
|
if ($this->assertResolvedImports()) {
|
||||||
$loader = $this->fieldLoader($object);
|
$this->fieldLoader = new IcingaObjectFieldLoader($object);
|
||||||
$loader->addFieldsToForm($this);
|
$this->fieldLoader->prepareElements($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -309,14 +339,21 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
|
|
||||||
protected function setCustomVarValues($object, & $values)
|
protected function setCustomVarValues($object, & $values)
|
||||||
{
|
{
|
||||||
if ($this->assertResolvedImports()) {
|
if ($this->fieldLoader) {
|
||||||
$loader = $this->fieldLoader($object);
|
$this->fieldLoader->setValues($values, 'var_');
|
||||||
$loader->setValues($values, 'var_');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function addFields()
|
||||||
|
{
|
||||||
|
if ($this->fieldLoader) {
|
||||||
|
$this->fieldLoader->addFieldsToForm($this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: remove, used in sets I guess
|
||||||
protected function fieldLoader($object)
|
protected function fieldLoader($object)
|
||||||
{
|
{
|
||||||
if ($this->fieldLoader === null) {
|
if ($this->fieldLoader === null) {
|
||||||
|
@ -602,7 +639,7 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
$values = array();
|
$values = array();
|
||||||
|
|
||||||
$object = $this->object();
|
$object = $this->object();
|
||||||
$this->loadFields($object);
|
$this->prepareFields($object);
|
||||||
if ($this->hasBeenSent()) {
|
if ($this->hasBeenSent()) {
|
||||||
|
|
||||||
if ($this->shouldBeDeleted()) {
|
if ($this->shouldBeDeleted()) {
|
||||||
|
@ -614,9 +651,10 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
$values = $this->getValues();
|
$values = $this->getValues();
|
||||||
|
|
||||||
if ($object instanceof IcingaObject) {
|
if ($object instanceof IcingaObject) {
|
||||||
$this->setCustomVarValues($object, $values);
|
$this->setCustomVarValues($object, $post);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->addFields();
|
||||||
|
|
||||||
if ($object instanceof IcingaObject) {
|
if ($object instanceof IcingaObject) {
|
||||||
$this->handleRanges($object, $values);
|
$this->handleRanges($object, $values);
|
||||||
|
@ -913,8 +951,21 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
|
|
||||||
protected function addImportsElement($required = null)
|
protected function addImportsElement($required = null)
|
||||||
{
|
{
|
||||||
|
$required = $required !== null ? $required : !$this->isTemplate();
|
||||||
$enum = $this->enumAllowedTemplates();
|
$enum = $this->enumAllowedTemplates();
|
||||||
if (empty($enum)) {
|
if (empty($enum)) {
|
||||||
|
if ($required) {
|
||||||
|
if ($this->hasBeenSent()) {
|
||||||
|
$this->addError($this->translate('No template has been chosen'));
|
||||||
|
} else {
|
||||||
|
if ($this->hasPermission('director/admin')) {
|
||||||
|
$html = $this->translate('Please define a related template first');
|
||||||
|
} else {
|
||||||
|
$html = $this->translate('No related template has been provided yet');
|
||||||
|
}
|
||||||
|
$this->addHtml('<p class="warning">' . $html . '</p>');
|
||||||
|
}
|
||||||
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -925,7 +976,7 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
. ' matters when importing properties from multiple templates: last one'
|
. ' matters when importing properties from multiple templates: last one'
|
||||||
. ' wins'
|
. ' wins'
|
||||||
),
|
),
|
||||||
'required' => ($required !== null ? $required : !$this->isTemplate()),
|
'required' => $required,
|
||||||
'multiOptions' => $this->optionallyAddFromEnum($enum),
|
'multiOptions' => $this->optionallyAddFromEnum($enum),
|
||||||
'sorted' => true,
|
'sorted' => true,
|
||||||
'value' => $this->presetImports,
|
'value' => $this->presetImports,
|
||||||
|
@ -1293,6 +1344,15 @@ abstract class DirectorObjectForm extends QuickForm
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $permission
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasPermission($permission)
|
||||||
|
{
|
||||||
|
return Util::hasPermission($permission);
|
||||||
|
}
|
||||||
|
|
||||||
protected function allowsExperimental()
|
protected function allowsExperimental()
|
||||||
{
|
{
|
||||||
// NO, it is NOT a good idea to use this. You'll break your monitoring
|
// NO, it is NOT a good idea to use this. You'll break your monitoring
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
|
|
||||||
namespace Icinga\Module\Director\Web\Form;
|
namespace Icinga\Module\Director\Web\Form;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Icinga\Exception\IcingaException;
|
use Icinga\Exception\IcingaException;
|
||||||
use stdClass;
|
|
||||||
use Icinga\Module\Director\Objects\IcingaObject;
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
use Icinga\Module\Director\Objects\DirectorDatafield;
|
use Icinga\Module\Director\Objects\DirectorDatafield;
|
||||||
|
use stdClass;
|
||||||
use Zend_Form_Element as ZfElement;
|
use Zend_Form_Element as ZfElement;
|
||||||
|
|
||||||
class IcingaObjectFieldLoader
|
class IcingaObjectFieldLoader
|
||||||
|
@ -102,10 +103,12 @@ class IcingaObjectFieldLoader
|
||||||
throw new IcingaException('No such element %s', $key);
|
throw new IcingaException('No such element %s', $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$el->setValue($value);
|
||||||
$value = $el->getValue();
|
$value = $el->getValue();
|
||||||
if ($value === '') {
|
if ($value === '' || $value === array()) {
|
||||||
$value = null;
|
$value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$vars->set($varName, $value);
|
$vars->set($varName, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,6 +146,22 @@ class IcingaObjectFieldLoader
|
||||||
return $this->elements;
|
return $this->elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the form elements for our fields
|
||||||
|
*
|
||||||
|
* @param QuickForm $form Optional
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function prepareElements(QuickForm $form = null)
|
||||||
|
{
|
||||||
|
if ($this->object->supportsFields()) {
|
||||||
|
$this->getElements($form);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach our form fields to the given form
|
* Attach our form fields to the given form
|
||||||
*
|
*
|
||||||
|
@ -233,14 +252,20 @@ class IcingaObjectFieldLoader
|
||||||
/**
|
/**
|
||||||
* Create the fields for our object
|
* Create the fields for our object
|
||||||
*
|
*
|
||||||
*
|
* @param IcingaObject $object
|
||||||
* @return DirectorDatafield[]
|
* @return DirectorDatafield[]
|
||||||
*/
|
*/
|
||||||
protected function prepareObjectFields($object)
|
protected function prepareObjectFields($object)
|
||||||
{
|
{
|
||||||
$fields = $this->loadResolvedFieldsForObject($object);
|
$fields = $this->loadResolvedFieldsForObject($object);
|
||||||
if ($object->hasRelation('check_command')) {
|
if ($object->hasRelation('check_command')) {
|
||||||
$command = $object->getResolvedRelated('check_command');
|
try {
|
||||||
|
$command = $object->getResolvedRelated('check_command');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// Ignore failures
|
||||||
|
$command = null;
|
||||||
|
}
|
||||||
|
|
||||||
if ($command) {
|
if ($command) {
|
||||||
$cmdFields = $this->loadResolvedFieldsForObject($command);
|
$cmdFields = $this->loadResolvedFieldsForObject($command);
|
||||||
foreach ($cmdFields as $varname => $field) {
|
foreach ($cmdFields as $varname => $field) {
|
||||||
|
@ -336,6 +361,7 @@ class IcingaObjectFieldLoader
|
||||||
foreach ($res as $r) {
|
foreach ($res as $r) {
|
||||||
$id = $r->object_id;
|
$id = $r->object_id;
|
||||||
unset($r->object_id);
|
unset($r->object_id);
|
||||||
|
|
||||||
$r->object = $objects[$id];
|
$r->object = $objects[$id];
|
||||||
if (! array_key_exists($id, $result)) {
|
if (! array_key_exists($id, $result)) {
|
||||||
$result[$id] = new stdClass;
|
$result[$id] = new stdClass;
|
||||||
|
|
Loading…
Reference in New Issue