Terugluisterknopjes (nog niet werkend)
This commit is contained in:
@@ -11,8 +11,8 @@ class ProgramController extends Controller
|
|||||||
{
|
{
|
||||||
private static $SCHEDULE_SQL = <<<QUERY
|
private static $SCHEDULE_SQL = <<<QUERY
|
||||||
SELECT `schedule`.`id` AS `scheduleid`, `programs`.`id` AS `id`, `programs`.`longname` AS `name`, `programs`.`description` AS `description`, `programs`.`email`, `schedule`.`priority`,
|
SELECT `schedule`.`id` AS `scheduleid`, `programs`.`id` AS `id`, `programs`.`longname` AS `name`, `programs`.`description` AS `description`, `programs`.`email`, `schedule`.`priority`,
|
||||||
`schedule`.`startdate`, `schedule`.`startday`, `schedule`.`starttime`,
|
`schedule`.`startdate`, `schedule`.`startday`, `schedule`.`starttime`, `schedule`.`enddate`, `schedule`.`endday`, `schedule`.`endtime`,
|
||||||
`schedule`.`enddate`, `schedule`.`endday`, `schedule`.`endtime`, `schedule`.`shortnamesuffix` AS `suffix`
|
`schedule`.`shortnamesuffix` AS `suffix`, `schedule`.`state` AS `state`
|
||||||
FROM `programs_schedule` AS `schedule` LEFT JOIN `programs` ON `schedule`.`program` = `programs`.`id`
|
FROM `programs_schedule` AS `schedule` LEFT JOIN `programs` ON `schedule`.`program` = `programs`.`id`
|
||||||
WHERE ((`schedule`.`startdate` IS NULL) OR (`schedule`.`startdate` < :enddate)) AND
|
WHERE ((`schedule`.`startdate` IS NULL) OR (`schedule`.`startdate` < :enddate)) AND
|
||||||
((`schedule`.`enddate` IS NULL) OR (`schedule`.`enddate` >= :startdate)) AND
|
((`schedule`.`enddate` IS NULL) OR (`schedule`.`enddate` >= :startdate)) AND
|
||||||
@@ -55,13 +55,15 @@ QUERY;
|
|||||||
$schedule = [];
|
$schedule = [];
|
||||||
|
|
||||||
$START = 1; $END = -1;
|
$START = 1; $END = -1;
|
||||||
$WEEK = new \DateInterval('P7D');
|
$WEEK = new \DateInterval('P7D'); $DAY = new \DateInterval('P1D');
|
||||||
$WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" /* == 0 and 7 */];
|
$WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" /* == 0 and 7 */];
|
||||||
$scheduleItems = app('db')->select(self::$SCHEDULE_SQL,
|
$scheduleItems = app('db')->select(self::$SCHEDULE_SQL,
|
||||||
['enddate' => $end->format('Y-m-d'), 'startdate' => $start->format('Y-m-d')]);
|
['enddate' => $end->format('Y-m-d'), 'startdate' => $start->format('Y-m-d')]);
|
||||||
$scheduleChanges = new \SplPriorityQueue();
|
$scheduleChanges = new \SplPriorityQueue();
|
||||||
$active = [];
|
$active = [];
|
||||||
foreach($scheduleItems as $item) {
|
foreach($scheduleItems as $item) {
|
||||||
|
if($item->startdate) { $item->startdate = new \DateTimeImmutable($item->startdate); }
|
||||||
|
if($item->enddate) { $item->enddate = (new \DateTimeImmutable($item->enddate))->add($DAY); }
|
||||||
// Find the start time and end time of the last occurrence on or before the start time
|
// Find the start time and end time of the last occurrence on or before the start time
|
||||||
$itemStart = new \DateTime("last " . $WEEKDAYS[$item->startday] . " " . $start->format('Y-m-d') . " " . $item->starttime);
|
$itemStart = new \DateTime("last " . $WEEKDAYS[$item->startday] . " " . $start->format('Y-m-d') . " " . $item->starttime);
|
||||||
$itemEnd = new \DateTime("last " . $WEEKDAYS[$item->endday] . " " . $start->format('Y-m-d') . " " . $item->endtime);
|
$itemEnd = new \DateTime("last " . $WEEKDAYS[$item->endday] . " " . $start->format('Y-m-d') . " " . $item->endtime);
|
||||||
@@ -97,76 +99,84 @@ QUERY;
|
|||||||
while(true) {
|
while(true) {
|
||||||
$item = $scheduleChanges->extract();
|
$item = $scheduleChanges->extract();
|
||||||
|
|
||||||
if($DEBUG) print "<b>{$item[1]->format('d-m-Y H:i')}: {$item[2]->name} " . ($item[0] == $START ? "begint" : "eindigt") . "</b><br/>";
|
|
||||||
|
|
||||||
// If we passed the end time, we are (almost) done
|
// If we passed the end time, we are (almost) done
|
||||||
if($item[1] > $end) {
|
if($item[1] > $end) {
|
||||||
if($DEBUG) { print "Reached end at {$item[1]->format('d-m-Y H:i')} ({$item[2]->name}).<br/>"; }
|
if($DEBUG) { print "Reached end at {$item[1]->format('d-m-Y H:i')} ({$item[2]->name}).<br/>"; }
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$priority = $item[2]->priority;
|
|
||||||
if($item[0] == $START) {
|
|
||||||
if($active[$priority] != null) {
|
|
||||||
print( "<b>Error:</b>" . $item[2]->name . ' starts at ' . $item[1]->format('d-m-Y H:i') . ' but ' . $active[$priority]->name . ' is still active then.' );
|
|
||||||
print "<pre>"; var_dump($active); print "</pre>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// When a program starts, it becomes the active program in its layer
|
|
||||||
$active[$priority] = $item[2];
|
|
||||||
|
|
||||||
// In addition, if it has higher priority than the currently active program, it replaces that.
|
// Only use this item if it is valid
|
||||||
if($activeProgram == null || $priority < $activeProgram->priority) {
|
if(($item[2]->startdate == null || $item[2]->startdate <= $activeProgramStart) && ($item[2]->enddate == null || $item[2]->enddate > $activeProgramStart))
|
||||||
if(($activeProgram != null) && ($item[1] > $start)) {
|
{
|
||||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} (reason: {$item[2]->name} starts).<br/>";
|
if($DEBUG) print "<b>{$item[1]->format('d-m-Y H:i')}: {$item[2]->name} {$item[2]->suffix} " . ($item[0] == $START ? "begint" : "eindigt") . "</b><br/>";
|
||||||
$this->addToSchedule($schedule, $activeProgramStart, $item[1], $activeProgram);
|
|
||||||
}
|
$priority = $item[2]->priority;
|
||||||
|
if($item[0] == $START) {
|
||||||
$activeProgramStart = ($item[1] < $start) ? $start : clone $item[1];
|
if($active[$priority] != null) {
|
||||||
$activeProgram = $item[2];
|
print( "<b>Error:</b>" . $item[2]->name . ' starts at ' . $item[1]->format('d-m-Y H:i') . ' but ' . $active[$priority]->name . ' is still active then.' );
|
||||||
}
|
print "<pre>"; var_dump($active); print "</pre>";
|
||||||
|
|
||||||
} else if($item[0] == $END) {
|
|
||||||
if($active[$priority] == null) {
|
|
||||||
print( "<b>Error:</b>" . $item[2]->name . ' ends at ' . $item[1]->format('d-m-Y H:i') . ' but no program is active at that priority and timestamp.');
|
|
||||||
print "<pre>"; var_dump($active); print "</pre>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// When the program ends, its layer becomes inactive.
|
|
||||||
$active[$priority] = null;
|
|
||||||
|
|
||||||
// In addition, when the ending program was the active program, we need to schedule it and find the new currently active program
|
|
||||||
if($activeProgram == $item[2]) {
|
|
||||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} (reason: {$item[2]->name} ends).<br/>";
|
|
||||||
$this->addToSchedule($schedule, $activeProgramStart, $item[1], $activeProgram);
|
|
||||||
|
|
||||||
$activeProgram = null;
|
|
||||||
foreach($active as $newPriority => $newActive) {
|
|
||||||
if(($newActive != null) && ($activeProgram == null || $newPriority < $activeProgram->priority)) {
|
|
||||||
$activeProgram = $newActive;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($activeProgram != null) {
|
// When a program starts, it becomes the active program in its layer
|
||||||
$activeProgramStart = clone $item[1];
|
$active[$priority] = $item[2];
|
||||||
|
|
||||||
|
// In addition, if it has higher priority than the currently active program, it replaces that.
|
||||||
|
if($activeProgram == null || $priority < $activeProgram->priority) {
|
||||||
|
if(($activeProgram != null) && ($item[1] > $start)) {
|
||||||
|
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} (reason: {$item[2]->name} starts).<br/>";
|
||||||
|
$this->addToSchedule($schedule, $activeProgramStart, $item[1], $activeProgram);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activeProgramStart = ($item[1] < $start) ? $start : clone $item[1];
|
||||||
|
$activeProgram = $item[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
if($DEBUG) print " New active program is " . ($activeProgram ? $activeProgram->name : "---") . "<br/>";
|
} else if($item[0] == $END) {
|
||||||
|
if($active[$priority] == null) {
|
||||||
|
print( "<b>Error:</b>" . $item[2]->name . ' ends at ' . $item[1]->format('d-m-Y H:i') . ' but no program is active at that priority and timestamp.');
|
||||||
|
print "<pre>"; var_dump($active); print "</pre>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the program ends, its layer becomes inactive.
|
||||||
|
$active[$priority] = null;
|
||||||
|
|
||||||
|
// In addition, when the ending program was the active program, we need to schedule it and find the new currently active program
|
||||||
|
if($activeProgram == $item[2]) {
|
||||||
|
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} (reason: {$item[2]->name} ends).<br/>";
|
||||||
|
$this->addToSchedule($schedule, $activeProgramStart, $item[1], $activeProgram);
|
||||||
|
|
||||||
|
$activeProgram = null;
|
||||||
|
foreach($active as $newPriority => $newActive) {
|
||||||
|
if(($newActive != null) && ($activeProgram == null || $newPriority < $activeProgram->priority)) {
|
||||||
|
$activeProgram = $newActive;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($activeProgram != null) {
|
||||||
|
$activeProgramStart = clone $item[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if($DEBUG) print " New active program is " . ($activeProgram ? $activeProgram->name : "---") . "<br/>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return response()->abort(500, "Invalid item type: expected START ($START) or END ($END), but got {$item[0]}.");
|
||||||
}
|
}
|
||||||
} else {
|
} else if($DEBUG) {
|
||||||
return response()->abort(500, "Invalid item type: expected START ($START) or END ($END), but got {$item[0]}.");
|
print "<i>Skipped {$item[2]->name} {$item[2]->suffix} on {$item[1]->format('d-m-Y H:i')} "
|
||||||
|
. " because startdate is " . ($item[2]->startdate == null ? "[null]" : $item[2]->startdate->format('d-m-Y H:i'))
|
||||||
|
. " and end date is " . ($item[2]->enddate == null ? "[null]" : $item[2]->enddate->format('d-m-Y H:i')) . "</i><br/>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// The item will recur in a week
|
// The item will recur in a week
|
||||||
$item[1]->add($WEEK);
|
$item[1]->add($WEEK);
|
||||||
$scheduleChanges->insert($item, [-$item[1]->getTimestamp(), -$item[0]]);
|
$scheduleChanges->insert($item, [-$item[1]->getTimestamp(), -$item[0]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(($activeProgram != null) && ($activeProgramStart != $end)) {
|
if(($activeProgram != null) && ($activeProgramStart != $end)) {
|
||||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$end->format('d-m-Y H:i')}: {$activeProgram->name} (reason: schedule finished).<br/>";
|
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$end->format('d-m-Y H:i')}: {$activeProgram->name} (reason: schedule finished).<br/>";
|
||||||
$this->addToSchedule($schedule, $activeProgramStart, $end, $activeProgram);
|
$this->addToSchedule($schedule, $activeProgramStart, $end, $activeProgram);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ['startdate' => $start, 'enddate' => $end, 'schedule' => $schedule];
|
return ['startdate' => $start, 'enddate' => $end, 'schedule' => $schedule];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,15 +214,15 @@ QUERY;
|
|||||||
}
|
}
|
||||||
|
|
||||||
$now = new \DateTimeImmutable('now');
|
$now = new \DateTimeImmutable('now');
|
||||||
$past = $this->createSchedule($now->add(\DateInterval::createFromDateString('-3 weeks')), $now->add(\DateInterval::createFromDateString('+1 week')));
|
$past = $this->createSchedule($now->add(\DateInterval::createFromDateString('-2 weeks')), $now->add(\DateInterval::createFromDateString('+1 week')));
|
||||||
$program->recent = [];
|
$program->recent = [];
|
||||||
$program->next = [];
|
$program->next = [];
|
||||||
foreach($past['schedule'] as $item) {
|
foreach(array_reverse($past['schedule']) as $item) {
|
||||||
if($item['program']->id == $id) {
|
if($item['program']->id == $id) {
|
||||||
if(($item['start'] > $now) && !($program->next)) {
|
if($item['start'] > $now) {
|
||||||
$program->next = $item['start'];
|
array_unshift($program->next, ['start' => $item['start'], 'name' => $item['program']->name, 'nonstop' => $item['program']->nonstop, 'rerun' => $item['program']->rerun]);
|
||||||
} else if(($item['start'] < $now) && count($program->recent) < 2) {
|
} else if($item['program']->priority < 3 && $item['start'] < $now) {
|
||||||
$program->recent[] = $item['start'];
|
$program->recent[] = ['start' => $item['start'], 'name' => $item['program']->name, 'nonstop' => $item['program']->nonstop, 'rerun' => $item['program']->rerun];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ class Program extends Model {
|
|||||||
public $name;
|
public $name;
|
||||||
public $description;
|
public $description;
|
||||||
public $email;
|
public $email;
|
||||||
|
public $nonstop;
|
||||||
|
public $rerun;
|
||||||
public $priority;
|
public $priority;
|
||||||
|
|
||||||
public $hosts;
|
public $hosts;
|
||||||
@@ -20,10 +22,14 @@ class Program extends Model {
|
|||||||
$this->name .= ' ' . $data->suffix;
|
$this->name .= ' ' . $data->suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::ConvertToDateTime($this->next);
|
|
||||||
if($this->recent && $this->recent) {
|
if($this->recent && $this->recent) {
|
||||||
foreach($this->recent as &$recent) {
|
foreach($this->recent as &$recent) {
|
||||||
parent::ConvertToDateTime($recent);
|
parent::ConvertToDateTime($recent->start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if($this->next && $this->next) {
|
||||||
|
foreach($this->next as &$next) {
|
||||||
|
parent::ConvertToDateTime($next->start);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,6 +37,11 @@ class Program extends Model {
|
|||||||
$this->email = substr($data->email, 0, $mailComma);
|
$this->email = substr($data->email, 0, $mailComma);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(isset($data->state)) {
|
||||||
|
$this->nonstop = $data->state == 0;
|
||||||
|
$this->rerun = $data->state == 3;
|
||||||
|
}
|
||||||
|
|
||||||
$this->url = "/{$this->id}/" . parent::url_slug($this->name);
|
$this->url = "/{$this->id}/" . parent::url_slug($this->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user