mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-28 08:14:04 +02:00
IcingaTimeperiod: Add isActive support for include/exclude
This commit is contained in:
parent
e8c64a422b
commit
c86c168e43
@ -19,6 +19,10 @@ class IcingaTimePeriod extends IcingaObject
|
||||
'update_method' => null,
|
||||
);
|
||||
|
||||
protected $booleans = [
|
||||
'prefer_includes' => 'prefer_includes',
|
||||
];
|
||||
|
||||
protected $supportsImports = true;
|
||||
|
||||
protected $supportsRanges = true;
|
||||
@ -61,15 +65,64 @@ class IcingaTimePeriod extends IcingaObject
|
||||
. ' import "legacy-timeperiod"' . "\n";
|
||||
}
|
||||
|
||||
protected function checkPeriodInRange($now, $name = null)
|
||||
{
|
||||
if ($name !== null) {
|
||||
$period = static::load($name, $this->connection);
|
||||
} else {
|
||||
$period = $this;
|
||||
}
|
||||
|
||||
foreach ($period->ranges()->getRanges() as $range) {
|
||||
if ($range->isActive($now)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isActive($now = null)
|
||||
{
|
||||
if ($now === null) {
|
||||
$now = time();
|
||||
}
|
||||
|
||||
foreach ($this->ranges()->getRanges() as $range) {
|
||||
if ($range->isActive($now)) {
|
||||
$preferIncludes = $this->get('prefer_includes') !== 'n';
|
||||
|
||||
$active = $this->checkPeriodInRange($now);
|
||||
$included = false;
|
||||
$excluded = false;
|
||||
|
||||
$variants = [
|
||||
'includes' => &$included,
|
||||
'excludes' => &$excluded
|
||||
];
|
||||
|
||||
foreach ($variants as $key => &$var) {
|
||||
foreach ($this->get($key) as $name) {
|
||||
if ($this->checkPeriodInRange($now, $name)) {
|
||||
$var = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($preferIncludes) {
|
||||
if ($included) {
|
||||
return true;
|
||||
} elseif ($excluded) {
|
||||
return false;
|
||||
} else {
|
||||
return $active;
|
||||
}
|
||||
} else {
|
||||
if ($excluded) {
|
||||
return false;
|
||||
} elseif ($included) {
|
||||
return true;
|
||||
} else {
|
||||
return $active;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user