ea9d332e10b3d44d38f0a0bad3bb9ea1c9c1efbc
[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                 (object) array(
12                     //main playlists in the store?
13                     'name'  => null,
14                     'id'    => -1,
15                     'image' => null,
16                     'tiles' => array()
17                 ),
18                 (object) array(
19                     //mainpage
20                     'name'  => null,
21                     'id'    => -2,
22                     'image' => null,
23                     'tiles' => array()
24                 )
25             )
26         );
27
28         $mainpage = 0;
29         $id = 0;
30         foreach ($this->groupByParent($this->getDirs()) as $dir => $arDirs) {
31             $plnum = count($playlists->playlists);
32             $playlists->playlists[$plnum] = (object) array(
33                 'id'    => ++$id,
34                 'image' => '',
35                 'name'  => $dir,
36                 'tiles' => array()
37             );
38             $playlists->playlists[0]->tiles[] = array('playlist' => $id);
39
40             foreach ($arDirs as $dirInfo) {
41                 $uuid = str_replace(
42                     '/', '-.-', $this->getRelPath($dirInfo->getPathname())
43                 );
44                 $playlists->games[] = (object) array(
45                     'content_rating' => 'Everyone', 
46                     'image'   => $this->getImageUrl($this->getFirstImage($dirInfo)),
47                     'title'   => basename($dirInfo->getPathname()), 
48                     'uuid'    =>  $uuid,
49                     'version' => '11111111-0000-1111-0000-111111111111',
50                     '__details' => $this->getDetailsUrl($uuid),
51                 );
52                 $playlists->playlists[$plnum]->tiles[] = (object) array(
53                     'game' => $uuid
54                 );
55
56                 if (++$mainpage < 10) {
57                     $playlists->playlists[1]->tiles[] = (object) array(
58                         'game' => $uuid
59                     );
60                 }
61             }
62         }
63
64         header('Content-Type: application/json');
65         echo json_encode($playlists, JSON_PRETTY_PRINT);
66     }
67
68     protected function getDirs()
69     {
70         $dirs = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*');
71         $dirs2 = new \GlobIterator($GLOBALS['imagestore']['basedir'] . '/*/*');
72
73         $all = new \AppendIterator();
74         $all->append($dirs);
75         $all->append($dirs2);
76
77         $allDirs = new \CallbackFilterIterator(
78             $all,
79             function ($fileInfo) {
80                 return $fileInfo->isDir() && $this->hasImages($fileInfo);
81             }
82         );
83         return new \CachingIterator($allDirs);
84     }
85
86     protected function hasImages(\SplFileInfo $dirInfo)
87     {
88         $it = $this->getImageIterator($dirInfo);
89         $it->rewind();
90         return $it->valid();
91     }
92
93     protected function groupByParent($dirs)
94     {
95         $arGroups = array();
96         foreach ($dirs as $dirInfo) {
97             $arGroups[basename($dirInfo->getPathInfo())][] = $dirInfo;
98         }
99         return $arGroups;
100     }
101
102     protected function getDetailsUrl($uuid)
103     {
104         return $this->getBaseUrl()
105             . 'api/v1/apps/' . rawurlencode($uuid);
106     }
107 }
108 ?>