TemplateChoice: delegate storing members to...

...the object, use a single form
This commit is contained in:
Thomas Gelf 2017-07-05 05:22:18 +02:00
parent 456c8557a4
commit 0ec5ec8197
4 changed files with 131 additions and 99 deletions

View File

@ -18,8 +18,9 @@ class TemplatechoiceController extends ActionController
$this->addSingleTab('Choice')
->addTitle($this->translate('Host template choice'));
$this->content()->add(
$form = $this->loadForm('IcingaTemplateChoiceHost')
$form = $this->loadForm('IcingaTemplateChoice')
->setDb($this->db())
->setChoiceType('host')
);
if ($name = $this->params->get('name')) {
$form->setObject(IcingaTemplateChoiceHost::load($name, $this->db()));
@ -32,8 +33,9 @@ class TemplatechoiceController extends ActionController
$this->addSingleTab('Choice')
->addTitle($this->translate('Service template choice'));
$this->content()->add(
$form = $this->loadForm('IcingaTemplateChoiceService')
$form = $this->loadForm('IcingaTemplateChoice')
->setDb($this->db())
->setChoiceType('service')
);
if ($name = $this->params->get('name')) {
$form->setObject(IcingaTemplateChoiceService::load($name, $this->db()));

View File

@ -2,15 +2,32 @@
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaTemplateChoiceService;
use Icinga\Module\Director\Objects\IcingaTemplateChoice;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
// TODO: combine with the one for hosts
class IcingaTemplateChoiceServiceForm extends DirectorObjectForm
class IcingaTemplateChoiceForm extends DirectorObjectForm
{
private $choiceType;
protected function getObjectClassname()
{
if ($this->className === null) {
return 'Icinga\\Module\\Director\\Objects\\IcingaTemplateChoice'
. ucfirst($this->choiceType);
}
return $this->className;
}
public function setChoiceType($type)
{
$this->choiceType = $type;
return $this;
}
public function setup()
{
/** @var IcingaTemplateChoiceService $object */
/** @var IcingaTemplateChoice $object */
$object = $this->object();
$this->addElement('text', 'object_name', array(
@ -32,11 +49,9 @@ class IcingaTemplateChoiceServiceForm extends DirectorObjectForm
$this->addElement('extensibleSet', 'members', array(
'label' => $this->translate('Available choices'),
'required' => true,
'ignore' => true,
'description' => $this->translate(
'Your users will be allowed to choose among those templates'
),
'value' => $object->getChoices(),
'multiOptions' => $this->fetchUnboundTemplates()
));
@ -45,15 +60,36 @@ class IcingaTemplateChoiceServiceForm extends DirectorObjectForm
protected function fetchUnboundTemplates()
{
/** @var IcingaTemplateChoice $object */
$object = $this->object();
$db = $this->getDb()->getDbAdapter();
$table = $object->getObjectTableName();
$query = $db->select()->from(
['o' => 'icinga_service'],
['o' => $table],
[
'k' => 'o.object_name',
'v' => 'o.object_name',
]
)->where("o.object_type = 'template'");
if ($object->hasBeenLoadedFromDb()) {
$query->where(
'o.template_choice_id IS NULL OR o.template_choice_id = ?',
$this->object()->getId()
);
} else {
$query->where('o.template_choice_id IS NULL');
}
return $db->fetchPairs($query);
}
protected function setObjectSuccessUrl()
{
/** @var IcingaTemplateChoice $object */
$object = $this->object();
$this->setSuccessUrl(
'director/templatechoice/' . $object->getObjectshortTableName(),
$object->getUrlParams()
);
}
}

View File

@ -1,58 +0,0 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaTemplateChoiceHost;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaTemplateChoiceHostForm extends DirectorObjectForm
{
public function setup()
{
/** @var IcingaTemplateChoiceHost $object */
$object = $this->object();
$this->addElement('text', 'object_name', array(
'label' => $this->translate('Choice name'),
'required' => true,
'description' => $this->translate(
'This will be shown as a label for the given choice'
)
));
$this->addElement('textarea', 'description', array(
'label' => $this->translate('Description'),
'rows' => 4,
'description' => $this->translate(
'A detailled description explaining what this choice is all about'
)
));
$this->addElement('extensibleSet', 'members', array(
'label' => $this->translate('Available choices'),
'required' => true,
'ignore' => true,
'description' => $this->translate(
'Your users will be allowed to choose among those templates'
),
'value' => $object->getChoices(),
'multiOptions' => $this->fetchUnboundTemplates()
));
$this->setButtons();
}
protected function fetchUnboundTemplates()
{
$db = $this->getDb()->getDbAdapter();
$query = $db->select()->from(
['h' => 'icinga_host'],
[
'k' => 'h.object_name',
'v' => 'h.object_name',
]
)->where("h.object_type = 'template'");
// ->where('')
return $db->fetchPairs($query);
}
}

View File

@ -18,7 +18,12 @@ class IcingaTemplateChoice extends IcingaObject
private $choices;
private $unstoredChoices;
private $newChoices;
public function getObjectShortTableName()
{
return substr(substr($this->table, 0, -16), 7);
}
public function getObjectTableName()
{
@ -27,16 +32,9 @@ class IcingaTemplateChoice extends IcingaObject
public function createFormElement(QuickForm $form, $imports = [], $namePrefix = 'choice')
{
$db = $this->getDb();
$query = $db->select()->from($this->getObjectTableName(), [
'value' => 'object_name',
'label' => 'object_name'
])->where('template_choice_id = ?', $this->get('id'));
$required = $this->isRequired() && !$this->isTemplate();
$type = $this->allowsMultipleChoices() ? 'multiselect' : 'select';
$choices = $db->fetchPairs($query);
$choices = $this->enumChoices();
$chosen = [];
foreach ($imports as $import) {
@ -74,8 +72,44 @@ class IcingaTemplateChoice extends IcingaObject
return (int) $this->max_allowed > 1;
}
public function hasBeenModified()
{
if ($this->newChoices !== null && $this->choices !== $this->newChoices) {
return true;
}
return parent::hasBeenModified();
}
public function getMembers()
{
return $this->enumChoices();
}
public function setMembers($members)
{
if (empty($members)) {
$this->newChoices = array();
return $this;
}
$db = $this->getDb();
$query = $db->select()->from(
['o' => $this->getObjectTableName()],
['o.id', 'o.object_name']
)->where("o.object_type = 'template'")
->where('o.object_name IN (?)', $members)
->order('o.object_name');
$this->newChoices = $db->fetchPairs($query);
return $this;
}
public function getChoices()
{
if ($this->newChoices !== null) {
return $this->newChoices;
}
if ($this->choices === null) {
$this->choices = $this->fetchChoices();
}
@ -91,7 +125,7 @@ class IcingaTemplateChoice extends IcingaObject
['o' => $this->getObjectTableName()],
['o.id', 'o.object_name']
)->where("o.object_type = 'template'")
->where('o.template_choice_id IS NULL OR o.template_choice_id = ?', $this->get('id'));
->where('o.template_choice_id = ?', $this->get('id'));
return $db->fetchPairs($query);
} else {
return [];
@ -104,12 +138,46 @@ class IcingaTemplateChoice extends IcingaObject
return array_combine($choices, $choices);
}
/*
* TODO: mukti?
protected $relations = [
'depends_on' => 'IcingaHost',
];
*/
public function onStore()
{
parent::onStore();
if ($this->newChoices !== $this->choices) {
$this->storeChoices();
}
}
protected function storeChoices()
{
$id = $this->getProperty('id');
$db = $this->getDb();
$ids = array_keys($this->newChoices);
$table = $this->getObjectTableName();
if (empty($ids)) {
$db->update(
$table,
['template_choice_id' => null],
$db->quoteInto(
sprintf('template_choice_id = %d', $id),
$ids
)
);
} else {
$db->update(
$table,
['template_choice_id' => null],
$db->quoteInto(
sprintf('template_choice_id = %d AND id NOT IN (?)', $id),
$ids
)
);
$db->update(
$table,
['template_choice_id' => $id],
$db->quoteInto('id IN (?)', $ids)
);
}
}
/**
* @param $type
@ -120,19 +188,3 @@ class IcingaTemplateChoice extends IcingaObject
// @codingStandardsIgnoreEnd
}
}
/*
Normale Imports -> Windows Basis Checks
Execution speed:
Add field
* Type: host/service template choice
*
Build a host:
* Kinds of templates
*/