ObjectForm: allow to preset imports

This commit is contained in:
Thomas Gelf 2016-11-16 16:02:28 +01:00
parent 5407c4fe7c
commit b39e7efce7
2 changed files with 22 additions and 3 deletions

View File

@ -9,6 +9,7 @@ use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Web\Url;
abstract class ObjectController extends ActionController
@ -186,8 +187,10 @@ abstract class ObjectController extends ActionController
$ltype = strtolower($type);
$url = sprintf('director/%ss', $ltype);
/** @var DirectorObjectForm $form */
$form = $this->view->form = $this->loadForm('icinga' . ucfirst($type))
->setDb($this->db())
->presetImports($this->params->shift('imports'))
->setApi($this->getApiIfAvailable())
->setSuccessUrl($url);

View File

@ -45,12 +45,27 @@ abstract class DirectorObjectForm extends QuickForm
/** @var CoreApi */
private $api;
private $presetImports;
public function setPreferredObjectType($type)
{
$this->preferredObjectType = $type;
return $this;
}
public function presetImports($imports)
{
if (! empty($imports)) {
if (is_array($imports)) {
$this->presetImports = $imports;
} else {
$this->presetImports = array($imports);
}
}
return $this;
}
/**
* @param array $values
*
@ -248,7 +263,7 @@ abstract class DirectorObjectForm extends QuickForm
unset($props['vars']);
}
$this->setDefaults($this->removeNullProperties($props));
$this->setDefaults($this->removeEmptyProperties($props));
if ($resolve) {
$this->showInheritedProperties($object);
@ -270,11 +285,11 @@ abstract class DirectorObjectForm extends QuickForm
}
}
protected function removeNullProperties($props)
protected function removeEmptyProperties($props)
{
$result = array();
foreach ($props as $k => $v) {
if ($v !== null && $v !== '') {
if ($v !== null && $v !== '' && $v !== array()) {
$result[$k] = $v;
}
}
@ -913,6 +928,7 @@ abstract class DirectorObjectForm extends QuickForm
'required' => ($required !== null ? $required : !$this->isTemplate()),
'multiOptions' => $this->optionallyAddFromEnum($enum),
'sorted' => true,
'value' => $this->presetImports,
'class' => 'autosubmit'
));