2017-06-20 06:53:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2017-09-27 20:44:50 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
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,
|
2017-09-27 20:41:41 +02:00
|
|
|
'required_template_id' => null,
|
|
|
|
'allowed_roles' => null,
|
2017-06-20 06:53:12 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private $choices;
|
|
|
|
|
2017-07-05 05:22:18 +02:00
|
|
|
private $newChoices;
|
|
|
|
|
|
|
|
public function getObjectShortTableName()
|
|
|
|
{
|
|
|
|
return substr(substr($this->table, 0, -16), 7);
|
|
|
|
}
|
2017-06-20 06:53:12 +02:00
|
|
|
|
2017-09-27 20:44:50 +02:00
|
|
|
public function isMainChoice()
|
|
|
|
{
|
|
|
|
return $this->hasBeenLoadedFromDb()
|
|
|
|
&& $this->connection->settings()->get('main_host_choice');
|
|
|
|
}
|
|
|
|
|
2017-06-20 06:53:12 +02:00
|
|
|
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')
|
|
|
|
{
|
|
|
|
$required = $this->isRequired() && !$this->isTemplate();
|
|
|
|
$type = $this->allowsMultipleChoices() ? 'multiselect' : 'select';
|
2017-07-05 05:22:18 +02:00
|
|
|
$choices = $this->enumChoices();
|
2017-06-23 00:17:02 +02:00
|
|
|
|
|
|
|
$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-07-05 05:22:18 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-20 06:53:12 +02:00
|
|
|
public function getChoices()
|
|
|
|
{
|
2017-07-05 05:22:18 +02:00
|
|
|
if ($this->newChoices !== null) {
|
|
|
|
return $this->newChoices;
|
|
|
|
}
|
|
|
|
|
2017-06-20 06:53:12 +02:00
|
|
|
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'")
|
2018-05-29 12:40:14 +02:00
|
|
|
->where('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);
|
|
|
|
}
|
|
|
|
|
2017-07-05 05:22:18 +02:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 06:53:12 +02:00
|
|
|
|
2017-09-27 20:44:50 +02:00
|
|
|
/**
|
|
|
|
* @param $roles
|
|
|
|
* @throws ProgrammingError
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*/
|
|
|
|
public function setAllowed_roles($roles)
|
|
|
|
{
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
$key = 'allowed_roles';
|
|
|
|
if (is_array($roles)) {
|
|
|
|
$this->reallySet($key, json_encode($roles));
|
|
|
|
} elseif (null === $roles) {
|
|
|
|
$this->reallySet($key, null);
|
|
|
|
} else {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Expected array or null for allowed_roles, got %s',
|
|
|
|
var_export($roles, 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|null
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*/
|
|
|
|
public function getAllowed_roles()
|
|
|
|
{
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
|
|
|
|
// Might be removed once all choice types have allowed_roles
|
|
|
|
if (! array_key_exists('allowed_roles', $this->properties)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$roles = $this->getProperty('allowed_roles');
|
|
|
|
if (is_string($roles)) {
|
|
|
|
return json_decode($roles);
|
|
|
|
} else {
|
|
|
|
return $roles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 06:53:12 +02:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*/
|
|
|
|
public function setObject_type($type)
|
|
|
|
{
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
}
|
|
|
|
}
|