Program controller
This commit is contained in:
138
api/app/Http/Controllers/ProgramController.php
Normal file
138
api/app/Http/Controllers/ProgramController.php
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors', true);
|
||||||
|
|
||||||
|
class ProgramController extends Controller
|
||||||
|
{
|
||||||
|
private static $SCHEDULE_SQL = <<<QUERY
|
||||||
|
SELECT `schedule`.`id`, `programs`.`longname`, `schedule`.`priority`,
|
||||||
|
`schedule`.`startdate`, `schedule`.`startday`, `schedule`.`starttime`,
|
||||||
|
`schedule`.`enddate`, `schedule`.`endday`, `schedule`.`endtime`
|
||||||
|
FROM `programs_schedule` AS `schedule` LEFT JOIN `programs` ON `schedule`.`program` = `programs`.`id`
|
||||||
|
WHERE ((`schedule`.`startdate` IS NULL) OR (`schedule`.`startdate` < :enddate)) AND
|
||||||
|
((`schedule`.`enddate` IS NULL) OR (`schedule`.`enddate` >= :startdate)) AND
|
||||||
|
`schedule`.`active` = 1 AND
|
||||||
|
`schedule`.`final` = 1
|
||||||
|
ORDER BY `schedule`.`priority` DESC, MOD(`startday` + 6, 7) + 1, `schedule`.`starttime` ASC
|
||||||
|
QUERY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new controller instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Programmaschema per week
|
||||||
|
*/
|
||||||
|
public function schedule(Request $request, $weeksAhead = null) {
|
||||||
|
define('PROGRAMMA_START', 1);
|
||||||
|
define('PROGRAMMA_EINDE', -1);
|
||||||
|
|
||||||
|
define('PW_TIJDSTIP', 0);
|
||||||
|
define('PW_TYPE', 1);
|
||||||
|
define('PW_PROGRAMMA', 2);
|
||||||
|
|
||||||
|
$oneWeek = \DateInterval::createFromDateString('1 week');
|
||||||
|
|
||||||
|
if($weeksAhead <= 0) { $weeksAhead = 1; }
|
||||||
|
$start = new \DateTimeImmutable('Monday this week');
|
||||||
|
$einde = $start->add(\DateInterval::createFromDateString((int)$weeksAhead . ' weeks'));
|
||||||
|
|
||||||
|
$programmas = app('db')->select(self::$SCHEDULE_SQL,
|
||||||
|
['enddate' => $einde->format('Y-m-d'), 'startdate' => $start->format('Y-m-d')]);
|
||||||
|
|
||||||
|
// Maak een lijstje van alle start en eindtijden en welk programma dan begint/eindigt
|
||||||
|
$startEinde = [];
|
||||||
|
foreach($programmas as $programma) {
|
||||||
|
$programmaStart = $start->add(\DateInterval::createFromDateString(($programma->startday - 1) % 7 . ' days'));
|
||||||
|
$programmaEinde = $start->add(\DateInterval::createFromDateString(($programma->endday - 1) % 7 . ' days'));
|
||||||
|
|
||||||
|
$startTijd = new \DateTime($programma->starttime);
|
||||||
|
$eindTijd = new \DateTime($programma->endtime);
|
||||||
|
|
||||||
|
$programmaStart = $programmaStart->setTime($startTijd->format('H'), $startTijd->format('i'), $startTijd->format('s'));
|
||||||
|
$programmaEinde = $programmaEinde->setTime($eindTijd->format('H'), $eindTijd->format('i'), $eindTijd->format('s'));
|
||||||
|
|
||||||
|
if($programmaEinde <= $programmaStart) {
|
||||||
|
$programmaEinde = $programmaEinde->add($oneWeek);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($programmaEinde < $start) {
|
||||||
|
$programmaStart = $programmaStart->add($oneWeek);
|
||||||
|
$programmaEinde = $programmaEinde->add($oneWeek);
|
||||||
|
}
|
||||||
|
|
||||||
|
while($programmaStart < $einde) {
|
||||||
|
if(($programma->startdate != null) && ($programmaStart < new \DateTime($programma->startdate))) { continue; }
|
||||||
|
if(($programma->enddate != null) && ($programmaStart > new \DateTime($programma->enddate))) { continue; }
|
||||||
|
|
||||||
|
$startEinde[] = [$programmaStart, PROGRAMMA_START, $programma];
|
||||||
|
$startEinde[] = [$programmaEinde, PROGRAMMA_EINDE, $programma];
|
||||||
|
|
||||||
|
$programmaStart = $programmaStart->add($oneWeek);
|
||||||
|
$programmaEinde = $programmaEinde->add($oneWeek);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
array_multisort($startEinde);
|
||||||
|
|
||||||
|
// return response()->json($startEinde);
|
||||||
|
|
||||||
|
$actieveProgrammas = [null, null, null, null];
|
||||||
|
$schema = [];
|
||||||
|
$fouten = [];
|
||||||
|
$index = -1;
|
||||||
|
foreach($startEinde as $programmaWissel) {
|
||||||
|
$programma = $programmaWissel[PW_PROGRAMMA];
|
||||||
|
$tijdstip = $programmaWissel[PW_TIJDSTIP];
|
||||||
|
if($programmaWissel[PW_TIJDSTIP] < $start) { $tijdstip = $start; }
|
||||||
|
if($programmaWissel[PW_TIJDSTIP] > $einde) { $tijdstip = $einde; }
|
||||||
|
if($tijdstip == $einde) { break; }
|
||||||
|
|
||||||
|
if($programmaWissel[PW_TYPE] == PROGRAMMA_START) {
|
||||||
|
if($actieveProgrammas[$programma->priority] != null) {
|
||||||
|
$fouten[] = [$tijdstip, "Begin van {$programma->longname} maar {$actieveProgrammas[$programma->priority]->longname} is nog actief."];
|
||||||
|
}
|
||||||
|
|
||||||
|
$actieveProgrammas[$programma->priority] = $programma;
|
||||||
|
for($prio = $programma->priority; $prio >= 0; --$prio) {
|
||||||
|
if($actieveProgrammas[$prio] != null) {
|
||||||
|
if(($index == -1) || ($schema[$index]['tijdstip'] != $tijdstip)) { $index++; }
|
||||||
|
$schema[$index] = ['tijdstip' => $tijdstip, 'programma' => $actieveProgrammas[$prio]];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else /*if($programmaWissel[PW_TYPE] == PROGRAMMA_EINDE)*/ {
|
||||||
|
$actieveProgrammas[$programma->priority] = null;
|
||||||
|
if(($index == -1) || ($schema[$index]['programma'] != null)) { $index++; }
|
||||||
|
$schema[$index] = ['tijdstip' => $tijdstip, 'programma' => null];
|
||||||
|
for($prio = $programma->priority; $prio < count($actieveProgrammas); ++$prio) {
|
||||||
|
if($actieveProgrammas[$prio] != null) {
|
||||||
|
$schema[$index] = ['tijdstip' => $tijdstip, 'programma' => $actieveProgrammas[$prio]];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(false) {
|
||||||
|
for($i = 1; $i <= count($schema); $i++) {
|
||||||
|
$eindTijd = ($i == count($schema)) ? $einde : $schema[$i]['tijdstip'];
|
||||||
|
$programma = $schema[$i-1]['programma'];
|
||||||
|
print $schema[$i-1]['tijdstip']->format('D d-m-Y, H:i:s') . " - " . $eindTijd->format('D d-m-Y, H:i:s') . ": " . ($programma != null ? $programma->longname : "NULL") . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(['wah' => $weeksAhead, 'startdate' => $start, 'enddate' => $einde, 'schema' => $schema, 'fouten' => $fouten, 'programmas' => $programmas]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,7 +30,7 @@ $app->get('podcast/download/{id:\d+}/[{title}]', 'PodcastController@download' );
|
|||||||
$app->get('podcast/stream/{id:\d+}/[{title}]', 'PodcastController@stream' );
|
$app->get('podcast/stream/{id:\d+}/[{title}]', 'PodcastController@stream' );
|
||||||
|
|
||||||
// programma/nustraks
|
// programma/nustraks
|
||||||
// programma/schema
|
$app->get('programma/schema/week[/{weeksAhead:\d+}]', 'ProgramController@schedule' );
|
||||||
// programma/details/{id:\d+}
|
// programma/details/{id:\d+}
|
||||||
|
|
||||||
// live/onair
|
// live/onair
|
||||||
|
|||||||
Reference in New Issue
Block a user