summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/inc/scheduler.inc.php
blob: 839d7d8131edba986d8a3ae8ed9ff12e6b5aec79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

class Scheduler
{

	public static function updateSchedule($locationid, $action, $options, $openingTimes) {
		if ($openingTimes == '') {
			self::deleteSchedule($locationid, $action);
			return false;
		}
		$nextwake = self::calcNextwake($action, $openingTimes);
		$json_options = json_encode($options);
		self::upsert($locationid, $action, $nextwake, $json_options);
		return true;
	}

	public static function deleteSchedule($locationid, $action) {
		Database::exec("DELETE FROM `scheduler` 
			WHERE locationid = :lid AND action = :act", array(
				'lid' => $locationid,
				'act' => $action
		));
	}

	private static function calcNextwake($action, $openingTimes) {
		//TODO: Calculate nextWake based on openingTimes. Needs action to know if openTimes or closingTimes are used.
		return 0;
	}

	private static function upsert($locationid, $action, $nextwake, $options) {
		$schedule = Database::queryFirst("SELECT locationid, action 
			FROM `scheduler`
			WHERE locationid = :lid AND action = :act", array(
				'lid' => $locationid,
				'act' => $action
		));
		if ($schedule === false) {
			Database::exec("INSERT INTO `scheduler` (locationid, action, nextwake, options) 
				VALUES (:lid, :act, :next, :opt)", array(
					'lid' => $locationid,
					'act' => $action,
					'next' => $nextwake,
					'opt' => $options
			));
		} else {
			Database::exec("UPDATE `scheduler` 
				SET nextwake = :next, options = :opt
				WHERE locationid = :lid AND action = :act", array(
					'next' => $nextwake,
					'opt' => $options,
					'lid' => $locationid,
					'act' => $action
			));
		}
		return true;
	}

}