+<?php
+namespace imagestore;
+
+class Controller_Api_Playlists
+{
+ public function handle()
+ {
+ $playlists = (object) array(
+ 'games' => array(),
+ 'playlists' => array()
+ );
+ //-1 - link to playlists
+ //-2 - ????
+ //1 - featured
+ //2 - trending now
+ $id = 0;
+ foreach ($this->groupByParent($this->getDirs()) as $dir => $arDirs) {
+ $plnum = count($playlists->playlists);
+ $playlists->playlists[$plnum] = (object) array(
+ 'id' => ++$id,
+ 'image' => '',
+ 'name' => $dir,
+ 'tiles' => array()
+ );
+ foreach ($arDirs as $dirInfo) {
+ //FIXME: uuid dots?
+ $uuid = $this->getRelPath($dirInfo->getPathname());
+ $playlists->games[] = (object) array(
+ 'content_rating' => 'Everyone',
+ 'image' => $this->getImageUrl($this->getFirstImage($dirInfo)),
+ 'title' => basename($dirInfo->getPathname()),
+ 'uuid' => $uuid,
+ 'version' => '11111111-0000-1111-0000-111111111111'
+ );
+ $playlists->playlists[$plnum]->tiles[] = (object) array(
+ 'game' => $uuid
+ );
+ }
+ }
+
+ header('Content-Type: application/json');
+ echo json_encode($playlists, JSON_PRETTY_PRINT);
+ }
+
+ protected function getDirs()
+ {
+ $dirs = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*');
+ $dirs2 = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*/*');
+
+ $all = new \AppendIterator();
+ $all->append($dirs);
+ $all->append($dirs2);
+
+ $allDirs = new \CallbackFilterIterator(
+ $all,
+ function ($fileInfo) {
+ return $fileInfo->isDir() && $this->hasImages($fileInfo);
+ }
+ );
+ return new \CachingIterator($allDirs);
+ }
+
+ protected function hasImages(\SplFileInfo $dirInfo)
+ {
+ $it = $this->getImageIterator($dirInfo);
+ $it->rewind();
+ return $it->valid();
+ }
+
+ protected function getImageIterator(\SplFileInfo $dirInfo)
+ {
+ $it = new \AppendIterator();
+ $it->append(
+ new \LimitIterator(
+ new \GlobIterator($dirInfo->getPathName() . '/*.jpg'),
+ 0, 1
+ )
+ );
+ $it->append(
+ new \LimitIterator(
+ new \GlobIterator($dirInfo->getPathName() . '/*.JPG'),
+ 0, 1
+ )
+ );
+ return $it;
+ }
+
+ protected function groupByParent($dirs)
+ {
+ $arGroups = array();
+ foreach ($dirs as $dirInfo) {
+ $arGroups[basename($dirInfo->getPathInfo())][] = $dirInfo;
+ }
+ return $arGroups;
+ }
+
+ protected function getRelPath($path)
+ {
+ return substr($path, strlen($GLOBALS['imagestore']['basedir']));
+ }
+
+ protected function getImageUrl($path)
+ {
+ if (isset($_SERVER['HTTPS'])) {
+ } else {
+ $protocol = 'http';
+ }
+ return $protocol . '://' . $_SERVER['HTTP_HOST']
+ . '/image?path=' . urlencode($this->getRelPath($path));
+ }
+
+ protected function getFirstImage(\SplFileInfo $dirInfo)
+ {
+ $it = $this->getImageIterator($dirInfo);
+ $it->rewind();
+ return $it->current();
+ }
+}
+?>