summaryrefslogblamecommitdiffstats
path: root/modules-available/rebootcontrol/inc/scheduler.inc.php
blob: 0776885d042b676c182ebaecdd5d02d756e81404 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                                              
                                                                       
                                                      
                                                                             



                                                                     
                                                               





                                                                          

                                                                                                                             


                         
                                                                                   
                                                                            
                                               




                                                                          
                                                                                                                    


                                                                         
                                                            


                                                         

                                                                         
                                                                                  
                                                            








                                                             
<?php

class Scheduler
{

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

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

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

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

}