Terugluisterknopjes (nog niet werkend)

This commit is contained in:
2017-08-01 03:43:02 +02:00
parent d04baa0367
commit 9201c50812
2 changed files with 83 additions and 62 deletions

View File

@@ -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,14 +99,17 @@ 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;
} }
// Only use this item if it is valid
if(($item[2]->startdate == null || $item[2]->startdate <= $activeProgramStart) && ($item[2]->enddate == null || $item[2]->enddate > $activeProgramStart))
{
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/>";
$priority = $item[2]->priority; $priority = $item[2]->priority;
if($item[0] == $START) { if($item[0] == $START) {
if($active[$priority] != null) { if($active[$priority] != null) {
@@ -156,6 +161,11 @@ QUERY;
} else { } else {
return response()->abort(500, "Invalid item type: expected START ($START) or END ($END), but got {$item[0]}."); return response()->abort(500, "Invalid item type: expected START ($START) or END ($END), but got {$item[0]}.");
} }
} else if($DEBUG) {
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);
@@ -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];
} }
} }
} }

View File

@@ -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);
} }