From c86c168e4343bcd73d497ecc8bf12f02430c16f9 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Thu, 13 Sep 2018 14:58:04 +0200 Subject: [PATCH] IcingaTimeperiod: Add isActive support for include/exclude --- library/Director/Objects/IcingaTimePeriod.php | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/library/Director/Objects/IcingaTimePeriod.php b/library/Director/Objects/IcingaTimePeriod.php index fadb68e6..2c459c35 100644 --- a/library/Director/Objects/IcingaTimePeriod.php +++ b/library/Director/Objects/IcingaTimePeriod.php @@ -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; } }