summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaTimePeriodRange.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
commitf66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Objects/IcingaTimePeriodRange.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz
icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Objects/IcingaTimePeriodRange.php')
-rw-r--r--library/Director/Objects/IcingaTimePeriodRange.php88
1 files changed, 88 insertions, 0 deletions
diff --git a/library/Director/Objects/IcingaTimePeriodRange.php b/library/Director/Objects/IcingaTimePeriodRange.php
new file mode 100644
index 0000000..55c1a3e
--- /dev/null
+++ b/library/Director/Objects/IcingaTimePeriodRange.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Icinga\Module\Director\Objects;
+
+use Icinga\Module\Director\Data\Db\DbObject;
+
+class IcingaTimePeriodRange extends DbObject
+{
+ protected $keyName = array('timeperiod_id', 'range_key', 'range_type');
+
+ protected $table = 'icinga_timeperiod_range';
+
+ protected $defaultProperties = array(
+ 'timeperiod_id' => null,
+ 'range_key' => null,
+ 'range_value' => null,
+ 'range_type' => 'include',
+ 'merge_behaviour' => 'set',
+ );
+
+ public function isActive($now = null)
+ {
+ if ($now === null) {
+ $now = time();
+ }
+
+ if (false === ($weekDay = $this->getWeekDay($this->get('range_key')))) {
+ // TODO, dates are not yet supported
+ return false;
+ }
+
+ if ((int) date('w', $now) !== $weekDay) {
+ return false;
+ }
+
+ $timeRanges = preg_split('/\s*,\s*/', $this->get('range_value'), -1, PREG_SPLIT_NO_EMPTY);
+ foreach ($timeRanges as $timeRange) {
+ if ($this->timeRangeIsActive($timeRange, $now)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ protected function timeRangeIsActive($rangeString, $now)
+ {
+ $hBegin = $mBegin = $hEnd = $mEnd = null;
+ if (sscanf($rangeString, '%2d:%2d-%2d:%2d', $hBegin, $mBegin, $hEnd, $mEnd) === 4) {
+ if ($this->timeFromHourMin($hBegin, $mBegin, $now) <= $now
+ && $this->timeFromHourMin($hEnd, $mEnd, $now) >= $now
+ ) {
+ return true;
+ }
+ } else {
+ // TODO: throw exception?
+ }
+
+ return false;
+ }
+
+ protected function timeFromHourMin($hour, $min, $now)
+ {
+ return strtotime(sprintf('%s %02d:%02d:00', date('Y-m-d', $now), $hour, $min));
+ }
+
+ protected function getWeekDay($day)
+ {
+ switch ($day) {
+ case 'sunday':
+ return 0;
+ case 'monday':
+ return 1;
+ case 'tuesday':
+ return 2;
+ case 'wednesday':
+ return 3;
+ case 'thursday':
+ return 4;
+ case 'friday':
+ return 5;
+ case 'saturday':
+ return 6;
+ }
+
+ return false;
+ }
+}