TimePeriodRange: ranges should be more generic...

...as they will not only be used in timeperiods
This commit is contained in:
Thomas Gelf 2016-08-31 16:23:23 +00:00
parent 6427b22621
commit 029907f01f
6 changed files with 50 additions and 38 deletions

View File

@ -33,9 +33,9 @@ class TimeperiodController extends ObjectController
array('class' => 'icon-left-big')
);
$form->loadObject(array(
'timeperiod_id' => $this->object->id,
'timeperiod_key' => $name,
'range_type' => $this->params->get('range_type')
'timeperiod_id' => $this->object->id,
'range_key' => $name,
'range_type' => $this->params->get('range_type')
));
}
$form->handleRequest();

View File

@ -15,14 +15,14 @@ class IcingaTimePeriodRangeForm extends DirectorObjectForm
public function setup()
{
$this->addHidden('timeperiod_id', $this->period->id);
$this->addElement('text', 'timeperiod_key', array(
$this->addElement('text', 'range_key', array(
'label' => $this->translate('Day(s)'),
'description' => $this->translate(
'Might by, monday, tuesday, 2016-01-28 - have a look at the documentation for more examples'
),
));
$this->addElement('text', 'timeperiod_value', array(
$this->addElement('text', 'range_value', array(
'label' => $this->translate('Timerperiods'),
'description' => $this->translate(
'One or more time periods, e.g. 00:00-24:00 or 00:00-09:00,17:00-24:00'
@ -44,8 +44,8 @@ class IcingaTimePeriodRangeForm extends DirectorObjectForm
$object = $this->object();
if ($object->hasBeenModified()) {
$this->period->ranges()->setRange(
$this->getValue('timeperiod_key'),
$this->getValue('timeperiod_value')
$this->getValue('range_key'),
$this->getValue('range_value')
);
}

View File

@ -10,16 +10,16 @@ class IcingaTimePeriodRangeTable extends QuickTable
protected $period;
protected $searchColumns = array(
'timeperiod_key',
'timeperiod_value',
'range_key',
'range_value',
);
public function getColumns()
{
return array(
'timeperiod_id' => 'r.timeperiod_id',
'timeperiod_key' => 'r.timeperiod_key',
'timeperiod_value' => 'r.timeperiod_value',
'timeperiod_id' => 'r.timeperiod_id',
'range_key' => 'r.range_key',
'range_value' => 'r.range_value',
);
}
@ -36,7 +36,7 @@ class IcingaTimePeriodRangeTable extends QuickTable
'director/timeperiod/ranges',
array(
'name' => $this->period->object_name,
'range' => $row->timeperiod_key,
'range' => $row->range_key,
'range_type' => 'include'
)
);
@ -46,8 +46,8 @@ class IcingaTimePeriodRangeTable extends QuickTable
{
$view = $this->view();
return array(
'timeperiod_key' => $view->translate('Day(s)'),
'timeperiod_value' => $view->translate('Timeperiods'),
'range_key' => $view->translate('Day(s)'),
'range_value' => $view->translate('Timeperiods'),
);
}

View File

@ -34,6 +34,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected $supportsApplyRules = false;
protected $rangeClass;
protected $type;
/* key/value!! */
@ -536,16 +538,26 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
$this->assertRangesSupport();
if ($this->ranges === null) {
$class = $this->getRangeClass();
if ($this->hasBeenLoadedFromDb()) {
$this->ranges = IcingaTimePeriodRanges::loadForStoredObject($this);
$this->ranges = $class::loadForStoredObject($this);
} else {
$this->ranges = new IcingaTimePeriodRanges($this);
$this->ranges = new $class($this);
}
}
return $this->ranges;
}
protected function getRangeClass()
{
if ($this->rangeClass === null) {
$this->rangeClass = get_class($this) . 'Ranges';
}
return $this->rangeClass;
}
public function arguments()
{
$this->assertArgumentsSupport();

View File

@ -6,16 +6,16 @@ use Icinga\Module\Director\Data\Db\DbObject;
class IcingaTimePeriodRange extends DbObject
{
protected $keyName = array('timeperiod_id', 'timeperiod_key', 'range_type');
protected $keyName = array('timeperiod_id', 'range_key', 'range_type');
protected $table = 'icinga_timeperiod_range';
protected $defaultProperties = array(
'timeperiod_id' => null,
'timeperiod_key' => null,
'timeperiod_value' => null,
'range_type' => 'include',
'merge_behaviour' => 'set',
'timeperiod_id' => null,
'range_key' => null,
'range_value' => null,
'range_type' => 'include',
'merge_behaviour' => 'set',
);
public function isActive($now = null)
@ -24,18 +24,18 @@ class IcingaTimePeriodRange extends DbObject
$now = time();
}
if (false === ($wday = $this->getWeekDay($this->timeperiod_key))) {
if (false === ($wday = $this->getWeekDay($this->range_key))) {
// TODO, dates are not yet supported
return false;
}
$wdayName = $this->timeperiod_key;
$wdayName = $this->range_key;
if ((int) strftime('%w', $now) !== $wday) {
return false;
}
$timeRanges = preg_split('/\s*,\s*/', $this->timeperiod_value, -1, PREG_SPLIT_NO_EMPTY);
$timeRanges = preg_split('/\s*,\s*/', $this->range_value, -1, PREG_SPLIT_NO_EMPTY);
foreach ($timeRanges as $timeRange) {
if ($this->timeRangeIsActive($timeRange, $now)) {
return true;

View File

@ -79,7 +79,7 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
{
$res = array();
foreach ($this->ranges as $key => $range) {
$res[$key] = $range->timeperiod_value;
$res[$key] = $range->range_value;
}
return (object) $res;
@ -89,7 +89,7 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
{
$res = array();
foreach ($this->storedRanges as $key => $range) {
$res[$key] = $range->timeperiod_value;
$res[$key] = $range->range_value;
}
return (object) $res;
@ -102,7 +102,7 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
protected function modify($range, $value)
{
$this->ranges[$range]->timeperiod_key = $value;
$this->ranges[$range]->range_key = $value;
}
public function set($ranges)
@ -127,17 +127,17 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
}
if (array_key_exists($range, $this->ranges)) {
if ($this->ranges[$range]->timeperiod_value === $value) {
if ($this->ranges[$range]->range_value === $value) {
return $this;
} else {
$this->ranges[$range]->timeperiod_value = $value;
$this->ranges[$range]->range_value = $value;
$this->modified = true;
}
} else {
$this->ranges[$range] = IcingaTimePeriodRange::create(array(
'timeperiod_id' => $this->object->id,
'timeperiod_key' => $range,
'timeperiod_value' => $value,
'timeperiod_id' => $this->object->id,
'range_key' => $range,
'range_value' => $value,
));
$this->modified = true;
}
@ -208,10 +208,10 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
$query = $db->select()->from(
array('o' => $table)
)->where('o.timeperiod_id = ?', (int) $this->object->id)
->order('o.timeperiod_key');
->order('o.range_key');
$class = $this->getClass();
$this->ranges = $class::loadAll($connection, $query, 'timeperiod_key');
$this->ranges = $class::loadAll($connection, $query, 'range_key');
$this->storedRanges = array();
foreach ($this->ranges as $key => $range) {
@ -259,8 +259,8 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
foreach ($this->ranges as $range) {
$string .= sprintf(
" %s\t= %s\n",
c::renderString($range->timeperiod_key),
c::renderString($range->timeperiod_value)
c::renderString($range->range_key),
c::renderString($range->range_value)
);
}