src/Controller/Api/PlaylistApiController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use Carbon\Carbon;
  4. use Pimcore\Controller\FrontendController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. class PlaylistApiController extends FrontendController
  8. {
  9.     /**
  10.      * @Route("/api/playlist-now", name="playlist-now")
  11.      */
  12.     public function defaultAction(Request $request)
  13.     {
  14.         $json = [
  15.             'success' => false,
  16.             'songs' => [
  17.                 'current' => [],
  18.                 'last' => [],
  19.                 'next' => []
  20.             ],
  21.             'updateSeconds' => 10
  22.         ];
  23.         date_default_timezone_set('Europe/Berlin');
  24.         $now time();
  25.         $currentSong = -1;
  26.         $playlist self::readPlaylist();
  27.         if ($playlist['success']) {
  28.             $json['success'] = true;
  29.             foreach($playlist['songs'] as $i => $song) {
  30. //                echo $song['date'] . ' ' . $song['starttime'] . ' ' . $song['title'] . '<br/>';
  31.                 if ($song['date'] && $song['date'] < $now) {
  32.                     $currentSong $i;
  33.                 }
  34.             }
  35.             if ($currentSong > -&& $currentSong count($playlist['songs'])) {
  36.                 $json['songs']['current'] = $playlist['songs'][$currentSong];
  37.                 if ($currentSong 0) {
  38.                     $json['songs']['last'] = $playlist['songs'][$currentSong 1];
  39.                 }
  40.                 if ($currentSong count($playlist['songs'])) {
  41.                     $json['songs']['next'] = $playlist['songs'][$currentSong 1];
  42.                     $json['updateSeconds'] = $json['songs']['next']['date'] - $now 2// 2 Sec threshold
  43.                 }
  44.             }
  45.         } else {
  46.             $json['error'] = $playlist['error'];
  47.         }
  48.         $response $this->json($json);
  49.         $response->setExpires(new \DateTime('+20 seconds'));
  50.         return $response;
  51.     }
  52.     /**
  53.      * @Route("/api/playlist-history", name="playlist-history")
  54.      */
  55.     public function historyAction(Request $request)
  56.     {
  57.         $json = [
  58.             'success' => false,
  59.             'songs' => []
  60.         ];
  61.         date_default_timezone_set('Europe/Berlin');
  62.         $now time();
  63.         $currentSong = -1;
  64.         $playlist self::readPlaylist();
  65.         if ($playlist['success']) {
  66.             $json['success'] = true;
  67.             foreach($playlist['songs'] as $i => $song) {
  68.                 if ($song['date'] && $song['date'] < $now) {
  69.                     $d = new Carbon($song['date']);
  70.                     $d->setTimezone('Europe/Berlin');
  71.                     $song['starttime'] = $d->format('Y-m-d H:i:s');
  72.                     $json['songs'][] = $song;
  73.                 }
  74.             }
  75.         } else {
  76.             $json['error'] = $playlist['error'];
  77.         }
  78.         $response $this->json($json);
  79.         $response->setExpires(new \DateTime('+1 minute'));
  80.         return $response;
  81.     }
  82.     /**
  83.      * @return array
  84.      */
  85.     private function readPlaylist() {
  86.         $return = [
  87.             'success' => false,
  88.             'error' => '',
  89.             'songs' => [],
  90.             'path' => ''
  91.         ];
  92.         $pathPublic getcwd() . '/playlisten/playlist.xml';
  93.         $pathAssets getcwd() . '/var/assets/playlisten/playlist.xml';
  94.         $pathLive getcwd() . '/web/images/playlisten/playlist.xml';
  95.         if (file_exists($pathPublic)) {
  96.             $finalPath $pathPublic;
  97.         } elseif (file_exists($pathAssets)) {
  98.             $finalPath $pathAssets;
  99.         } elseif (file_exists($pathLive)) {
  100.             $finalPath $pathLive;
  101.         } else {
  102.             $finalPath '';
  103.         }
  104.         if ($finalPath) {
  105.             $file file_get_contents($finalPath);
  106.             if ($file) {
  107.                 $file str_replace('&''&amp;'$file);
  108.                 try {
  109.                     $domDocument = new \DOMDocument();
  110.                     $domDocument->loadXML($file);
  111.                     $return['success'] = true;
  112.                     $nodeList $domDocument->getElementsByTagName('item');
  113.                     $data = [];
  114.                     for ($i 0$i $nodeList->count(); $i++) {
  115.                         /*
  116.                         <item>
  117.                           <title> Supreme </title>
  118.                           <author> Robbie Williams </author>
  119.                           <starttime> 24/08/2021 00:07:39 </starttime>
  120.                           <isplaying> False </isplaying>
  121.                         </item>
  122.                          */
  123.                         $node $nodeList->item($i);
  124.                         $children $node->childNodes;
  125.                         $song = [
  126.                             'title' => '',
  127.                             'author' => '',
  128.                             'starttime' => ''
  129.                         ];
  130.                         for ($c 0$c $children->count(); $c++) {
  131.                             if (isset($song[$children->item($c)->nodeName])) {
  132.                                 $song[$children->item($c)->nodeName] = trim($children->item($c)->nodeValue);
  133.                             }
  134.                         }
  135.                         if ($song['starttime']) {
  136.                             $song['date'] = strtotime(str_replace('/''.'$song['starttime']));
  137.                         }
  138.                         $return['songs'][] = $song;
  139.                     }
  140.                 } catch (\Exception $e) {
  141.                     $return['error'] = $e->getMessage();
  142.                 }
  143.             }
  144.         }
  145.         return $return;
  146.     }
  147. }