app details (image list)
[ouya-imagestore.git] / src / imagestore / Controller / Api / Playlists.php
1 <?php
2 namespace imagestore;
3
4 class Controller_Api_Playlists extends Controller_Api_ImageBase
5 {
6     public function handle()
7     {
8         $playlists = (object) array(
9             'games' => array(),
10             'playlists' => array()
11         );
12         //-1 - link to playlists
13         //-2 - ????
14         //1 - featured
15         //2 - trending now
16         $id = 0;
17         foreach ($this->groupByParent($this->getDirs()) as $dir => $arDirs) {
18             $plnum = count($playlists->playlists);
19             $playlists->playlists[$plnum] = (object) array(
20                 'id'    => ++$id,
21                 'image' => '',
22                 'name'  => $dir,
23                 'tiles' => array()
24             );
25             foreach ($arDirs as $dirInfo) {
26                 $uuid = str_replace(
27                     '/', '-.-', $this->getRelPath($dirInfo->getPathname())
28                 );
29                 $playlists->games[] = (object) array(
30                     'content_rating' => 'Everyone', 
31                     'image'   => $this->getImageUrl($this->getFirstImage($dirInfo)),
32                     'title'   => basename($dirInfo->getPathname()), 
33                     'uuid'    =>  $uuid,
34                     'version' => '11111111-0000-1111-0000-111111111111',
35                     '__details' => $this->getDetailsUrl($uuid),
36                 );
37                 $playlists->playlists[$plnum]->tiles[] = (object) array(
38                     'game' => $uuid
39                 );
40             }
41         }
42
43         header('Content-Type: application/json');
44         echo json_encode($playlists, JSON_PRETTY_PRINT);
45     }
46
47     protected function getDirs()
48     {
49         $dirs = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*');
50         $dirs2 = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*/*');
51
52         $all = new \AppendIterator();
53         $all->append($dirs);
54         $all->append($dirs2);
55
56         $allDirs = new \CallbackFilterIterator(
57             $all,
58             function ($fileInfo) {
59                 return $fileInfo->isDir() && $this->hasImages($fileInfo);
60             }
61         );
62         return new \CachingIterator($allDirs);
63     }
64
65     protected function hasImages(\SplFileInfo $dirInfo)
66     {
67         $it = $this->getImageIterator($dirInfo);
68         $it->rewind();
69         return $it->valid();
70     }
71
72     protected function groupByParent($dirs)
73     {
74         $arGroups = array();
75         foreach ($dirs as $dirInfo) {
76             $arGroups[basename($dirInfo->getPathInfo())][] = $dirInfo;
77         }
78         return $arGroups;
79     }
80
81     protected function getDetailsUrl($uuid)
82     {
83         return $this->getBaseUrl()
84             . 'api/v1/apps/' . rawurlencode($uuid);
85     }
86 }
87 ?>