2017-07-03 15:24:22 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2017-07-05 05:46:16 +02:00
|
|
|
use Icinga\Module\Director\Db;
|
2017-07-05 05:22:18 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaTemplateChoice;
|
2017-07-03 15:24:22 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
|
|
|
|
2017-07-05 05:22:18 +02:00
|
|
|
class IcingaTemplateChoiceForm extends DirectorObjectForm
|
2017-07-03 15:24:22 +02:00
|
|
|
{
|
2017-07-05 05:22:18 +02:00
|
|
|
private $choiceType;
|
|
|
|
|
2017-07-05 05:46:16 +02:00
|
|
|
public static function create($type, Db $db)
|
|
|
|
{
|
|
|
|
return static::load()->setDb($db)->setChoiceType($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function optionallyLoad($name)
|
|
|
|
{
|
|
|
|
if ($name !== null) {
|
|
|
|
/** @var IcingaTemplateChoice $class - cheating IDE */
|
|
|
|
$class = $this->getObjectClassName();
|
|
|
|
$this->setObject($class::load($name, $this->getDb()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-07-05 05:22:18 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-03 15:24:22 +02:00
|
|
|
public function setup()
|
|
|
|
{
|
|
|
|
$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,
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Your users will be allowed to choose among those templates'
|
|
|
|
),
|
|
|
|
'multiOptions' => $this->fetchUnboundTemplates()
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->setButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function fetchUnboundTemplates()
|
|
|
|
{
|
2017-07-05 05:22:18 +02:00
|
|
|
/** @var IcingaTemplateChoice $object */
|
|
|
|
$object = $this->object();
|
2017-07-03 15:24:22 +02:00
|
|
|
$db = $this->getDb()->getDbAdapter();
|
2017-07-05 05:22:18 +02:00
|
|
|
$table = $object->getObjectTableName();
|
2017-07-03 15:24:22 +02:00
|
|
|
$query = $db->select()->from(
|
2017-07-05 05:22:18 +02:00
|
|
|
['o' => $table],
|
2017-07-03 15:24:22 +02:00
|
|
|
[
|
|
|
|
'k' => 'o.object_name',
|
|
|
|
'v' => 'o.object_name',
|
|
|
|
]
|
|
|
|
)->where("o.object_type = 'template'");
|
2017-07-05 05:22:18 +02:00
|
|
|
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');
|
|
|
|
}
|
2017-07-03 15:24:22 +02:00
|
|
|
|
|
|
|
return $db->fetchPairs($query);
|
|
|
|
}
|
2017-07-05 05:22:18 +02:00
|
|
|
|
|
|
|
protected function setObjectSuccessUrl()
|
|
|
|
{
|
|
|
|
/** @var IcingaTemplateChoice $object */
|
|
|
|
$object = $this->object();
|
|
|
|
$this->setSuccessUrl(
|
|
|
|
'director/templatechoice/' . $object->getObjectshortTableName(),
|
|
|
|
$object->getUrlParams()
|
|
|
|
);
|
|
|
|
}
|
2017-07-03 15:24:22 +02:00
|
|
|
}
|