<?php
namespace App\Controller\Api;
use Carbon\Carbon;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class PlaylistApiController extends FrontendController
{
/**
* @Route("/api/playlist-now", name="playlist-now")
*/
public function defaultAction(Request $request)
{
$json = [
'success' => false,
'songs' => [
'current' => [],
'last' => [],
'next' => []
],
'updateSeconds' => 10
];
date_default_timezone_set('Europe/Berlin');
$now = time();
$currentSong = -1;
$playlist = self::readPlaylist();
if ($playlist['success']) {
$json['success'] = true;
foreach($playlist['songs'] as $i => $song) {
// echo $song['date'] . ' ' . $song['starttime'] . ' ' . $song['title'] . '<br/>';
if ($song['date'] && $song['date'] < $now) {
$currentSong = $i;
}
}
if ($currentSong > -1 && $currentSong < count($playlist['songs'])) {
$json['songs']['current'] = $playlist['songs'][$currentSong];
if ($currentSong - 1 > 0) {
$json['songs']['last'] = $playlist['songs'][$currentSong - 1];
}
if ($currentSong + 1 < count($playlist['songs'])) {
$json['songs']['next'] = $playlist['songs'][$currentSong + 1];
$json['updateSeconds'] = $json['songs']['next']['date'] - $now + 2; // 2 Sec threshold
}
}
} else {
$json['error'] = $playlist['error'];
}
$response = $this->json($json);
$response->setExpires(new \DateTime('+20 seconds'));
return $response;
}
/**
* @Route("/api/playlist-history", name="playlist-history")
*/
public function historyAction(Request $request)
{
$json = [
'success' => false,
'songs' => []
];
date_default_timezone_set('Europe/Berlin');
$now = time();
$currentSong = -1;
$playlist = self::readPlaylist();
if ($playlist['success']) {
$json['success'] = true;
foreach($playlist['songs'] as $i => $song) {
if ($song['date'] && $song['date'] < $now) {
$d = new Carbon($song['date']);
$d->setTimezone('Europe/Berlin');
$song['starttime'] = $d->format('Y-m-d H:i:s');
$json['songs'][] = $song;
}
}
} else {
$json['error'] = $playlist['error'];
}
$response = $this->json($json);
$response->setExpires(new \DateTime('+1 minute'));
return $response;
}
/**
* @return array
*/
private function readPlaylist() {
$return = [
'success' => false,
'error' => '',
'songs' => [],
'path' => ''
];
$pathPublic = getcwd() . '/playlisten/playlist.xml';
$pathAssets = getcwd() . '/var/assets/playlisten/playlist.xml';
$pathLive = getcwd() . '/web/images/playlisten/playlist.xml';
if (file_exists($pathPublic)) {
$finalPath = $pathPublic;
} elseif (file_exists($pathAssets)) {
$finalPath = $pathAssets;
} elseif (file_exists($pathLive)) {
$finalPath = $pathLive;
} else {
$finalPath = '';
}
if ($finalPath) {
$file = file_get_contents($finalPath);
if ($file) {
$file = str_replace('&', '&', $file);
try {
$domDocument = new \DOMDocument();
$domDocument->loadXML($file);
$return['success'] = true;
$nodeList = $domDocument->getElementsByTagName('item');
$data = [];
for ($i = 0; $i < $nodeList->count(); $i++) {
/*
<item>
<title> Supreme </title>
<author> Robbie Williams </author>
<starttime> 24/08/2021 00:07:39 </starttime>
<isplaying> False </isplaying>
</item>
*/
$node = $nodeList->item($i);
$children = $node->childNodes;
$song = [
'title' => '',
'author' => '',
'starttime' => ''
];
for ($c = 0; $c < $children->count(); $c++) {
if (isset($song[$children->item($c)->nodeName])) {
$song[$children->item($c)->nodeName] = trim($children->item($c)->nodeValue);
}
}
if ($song['starttime']) {
$song['date'] = strtotime(str_replace('/', '.', $song['starttime']));
}
$return['songs'][] = $song;
}
} catch (\Exception $e) {
$return['error'] = $e->getMessage();
}
}
}
return $return;
}
}