2016-04-22 14:28:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
|
|
|
use Icinga\Module\Director\Hook\JobHook;
|
2016-04-22 14:28:36 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
|
|
|
use Icinga\Web\Hook;
|
|
|
|
|
|
|
|
class DirectorJobForm extends DirectorObjectForm
|
|
|
|
{
|
|
|
|
public function setup()
|
|
|
|
{
|
2016-06-16 14:42:02 +02:00
|
|
|
$jobTypes = $this->enumJobTypes();
|
|
|
|
|
2016-04-22 14:28:36 +02:00
|
|
|
$this->addElement('select', 'job_class', array(
|
|
|
|
'label' => $this->translate('Job Type'),
|
|
|
|
'required' => true,
|
2016-06-16 14:42:02 +02:00
|
|
|
'multiOptions' => $this->optionalEnum($jobTypes),
|
2016-04-22 14:28:36 +02:00
|
|
|
'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)
|
|
|
|
));
|
|
|
|
|
2016-06-17 14:23:34 +02:00
|
|
|
$periods = $this->db->enumTimeperiods();
|
|
|
|
|
2017-01-13 20:48:50 +01:00
|
|
|
if (!empty($periods)) {
|
2016-06-17 14:23:34 +02:00
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'timeperiod_id',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Time period'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'The name of a time period within this job should be active.'
|
|
|
|
. ' Supports only simple time periods (weekday and multiple'
|
|
|
|
. ' time definitions)'
|
|
|
|
),
|
|
|
|
'multiOptions' => $this->optionalEnum($periods),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-22 14:28:36 +02:00
|
|
|
$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) {
|
2016-11-01 18:28:36 +01:00
|
|
|
/** @var DbObjectWithSettings $object */
|
2016-04-22 14:28:36 +02:00
|
|
|
$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()
|
|
|
|
{
|
2016-11-01 18:28:36 +01:00
|
|
|
/** @var JobHook[] $hooks */
|
2016-04-22 14:28:36 +02:00
|
|
|
$hooks = Hook::all('Director\\Job');
|
|
|
|
|
|
|
|
$enum = array();
|
2016-11-01 18:28:36 +01:00
|
|
|
|
2016-04-22 14:28:36 +02:00
|
|
|
foreach ($hooks as $hook) {
|
|
|
|
$enum[get_class($hook)] = $hook->getName();
|
|
|
|
}
|
|
|
|
asort($enum);
|
|
|
|
|
|
|
|
return $enum;
|
|
|
|
}
|
|
|
|
}
|