2017-06-20 06:53:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2017-06-23 00:17:02 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\QuickForm;
|
|
|
|
|
2017-06-20 06:53:12 +02:00
|
|
|
class IcingaTemplateChoice extends IcingaObject
|
|
|
|
{
|
2017-06-27 00:20:40 +02:00
|
|
|
protected $objectTable;
|
2017-06-20 06:53:12 +02:00
|
|
|
|
|
|
|
protected $defaultProperties = [
|
|
|
|
'id' => null,
|
|
|
|
'object_name' => null,
|
|
|
|
'description' => null,
|
|
|
|
'min_required' => 0,
|
|
|
|
'max_allowed' => 1,
|
|
|
|
];
|
|
|
|
|
|
|
|
private $choices;
|
|
|
|
|
|
|
|
private $unstoredChoices;
|
|
|
|
|
|
|
|
public function getObjectTableName()
|
|
|
|
{
|
|
|
|
return substr($this->table, 0, -16);
|
|
|
|
}
|
|
|
|
|
2017-06-23 00:17:02 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
$chosen = [];
|
|
|
|
foreach ($imports as $import) {
|
|
|
|
if (array_key_exists($import, $choices)) {
|
|
|
|
$chosen[] = $import;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'label' => $this->getObjectName(),
|
|
|
|
'description' => $this->get('description'),
|
|
|
|
'required' => $required,
|
|
|
|
'ignore' => true,
|
|
|
|
'value' => $chosen,
|
|
|
|
'multiOptions' => $form->optionalEnum($choices),
|
|
|
|
'class' => 'autosubmit'
|
|
|
|
];
|
|
|
|
|
|
|
|
// unused
|
|
|
|
if ($type === 'extensibleSet') {
|
|
|
|
$attributes['sorted'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $namePrefix . $this->get('id');
|
|
|
|
return $form->createElement($type, $key, $attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isRequired()
|
|
|
|
{
|
|
|
|
return (int) $this->min_required > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function allowsMultipleChoices()
|
|
|
|
{
|
|
|
|
return (int) $this->max_allowed > 1;
|
|
|
|
}
|
|
|
|
|
2017-06-20 06:53:12 +02:00
|
|
|
public function getChoices()
|
|
|
|
{
|
|
|
|
if ($this->choices === null) {
|
|
|
|
$this->choices = $this->fetchChoices();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->choices;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchChoices()
|
|
|
|
{
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$db = $this->getDb();
|
|
|
|
$query = $db->select()->from(
|
2017-06-27 00:20:40 +02:00
|
|
|
['o' => $this->getObjectTableName()],
|
2017-06-20 06:53:12 +02:00
|
|
|
['o.id', 'o.object_name']
|
|
|
|
)->where("o.object_type = 'template'")
|
2017-06-27 00:20:40 +02:00
|
|
|
->where('o.template_choice_id IS NULL OR o.template_choice_id = ?', $this->get('id'));
|
2017-06-20 06:53:12 +02:00
|
|
|
return $db->fetchPairs($query);
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function enumChoices()
|
|
|
|
{
|
|
|
|
$choices = $this->getChoices();
|
|
|
|
return array_combine($choices, $choices);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: mukti?
|
|
|
|
protected $relations = [
|
|
|
|
'depends_on' => 'IcingaHost',
|
|
|
|
];
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*/
|
|
|
|
public function setObject_type($type)
|
|
|
|
{
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
Normale Imports -> Windows Basis Checks
|
|
|
|
|
|
|
|
Execution speed:
|
|
|
|
|
|
|
|
Add field
|
|
|
|
* Type: host/service template choice
|
|
|
|
*
|
|
|
|
|
|
|
|
Build a host:
|
|
|
|
* Kinds of templates
|
|
|
|
|
|
|
|
*/
|