mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
DirectorJobForm: new form, using job hooks
This commit is contained in:
parent
d05d58cc48
commit
b806cb6c64
117
application/forms/DirectorJobForm.php
Normal file
117
application/forms/DirectorJobForm.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Forms;
|
||||
|
||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||
use Icinga\Web\Hook;
|
||||
|
||||
class DirectorJobForm extends DirectorObjectForm
|
||||
{
|
||||
public function setup()
|
||||
{
|
||||
$this->addElement('select', 'job_class', array(
|
||||
'label' => $this->translate('Job Type'),
|
||||
'required' => true,
|
||||
'multiOptions' => $this->optionalEnum($this->enumJobTypes()),
|
||||
'description' => $this->translate(
|
||||
'These are different available job types'
|
||||
),
|
||||
'class' => 'autosubmit'
|
||||
));
|
||||
|
||||
if (! $jobClass = $this->getJobClass()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($desc = $jobClass::getDescription($this)) {
|
||||
$this->addHtmlHint($desc);
|
||||
}
|
||||
|
||||
$this->addBoolean(
|
||||
'disabled',
|
||||
array(
|
||||
'label' => $this->translate('Disabled'),
|
||||
'description' => $this->translate(
|
||||
'This allows to temporarily disable this job'
|
||||
)
|
||||
),
|
||||
'n'
|
||||
);
|
||||
|
||||
$this->addElement('text', 'run_interval', array(
|
||||
'label' => $this->translate('Run interval'),
|
||||
'description' => $this->translate(
|
||||
'Execution interval for this job, in seconds'
|
||||
),
|
||||
'value' => $jobClass::getSuggestedRunInterval($this)
|
||||
));
|
||||
|
||||
$this->addElement('text', 'job_name', array(
|
||||
'label' => $this->translate('Job name'),
|
||||
'description' => $this->translate(
|
||||
'A short name identifying this job. Use something meaningful,'
|
||||
. ' like "Import Puppet Hosts"'
|
||||
),
|
||||
'required' => true,
|
||||
));
|
||||
|
||||
$this->addSettings();
|
||||
$this->setButtons();
|
||||
}
|
||||
|
||||
public function getSentOrObjectSetting($name, $default = null)
|
||||
{
|
||||
if ($this->hasObject()) {
|
||||
$value = $this->getSentValue($name);
|
||||
if ($value === null) {
|
||||
$object = $this->getObject();
|
||||
|
||||
return $object->getSetting($name, $default);
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
} else {
|
||||
return $this->getSentValue($name, $default);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getJobClass($class = null)
|
||||
{
|
||||
if ($class === null) {
|
||||
$class = $this->getSentOrObjectValue('job_class');
|
||||
}
|
||||
|
||||
if (array_key_exists($class, $this->enumJobTypes())) {
|
||||
return $class;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function addSettings($class = null)
|
||||
{
|
||||
if (! $class = $this->getJobClass($class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$class::addSettingsFormFields($this);
|
||||
foreach ($this->object()->getSettings() as $key => $val) {
|
||||
if ($el = $this->getElement($key)) {
|
||||
$el->setValue($val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function enumJobTypes()
|
||||
{
|
||||
$hooks = Hook::all('Director\\Job');
|
||||
|
||||
$enum = array();
|
||||
foreach ($hooks as $hook) {
|
||||
$enum[get_class($hook)] = $hook->getName();
|
||||
}
|
||||
asort($enum);
|
||||
|
||||
return $enum;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user