move path creation to separate file
[stouyapi.git] / bin / build-html.php
1 <?php
2 /**
3  * Take the generated JSON files and convert them to HTML for a browser
4  *
5  * @author Christian Weiske <cweiske@cweiske.de>
6  */
7 $wwwDir = __DIR__ . '/../www/';
8 $discoverDir = __DIR__ . '/../www/api/v1/discover-data/';
9 $wwwDiscoverDir = $wwwDir . 'discover/';
10
11 if (!is_dir($wwwDiscoverDir)) {
12     mkdir($wwwDiscoverDir, 0755);
13 }
14
15 foreach (glob($discoverDir . '*.json') as $discoverFile) {
16     $htmlFile = basename($discoverFile, '.json') . '.htm';
17     if ($htmlFile == 'discover.htm') {
18         $htmlFile = 'index.htm';
19     }
20     file_put_contents(
21         $wwwDiscoverDir . $htmlFile,
22         renderDiscoverFile($discoverFile)
23     );
24 }
25
26 function renderDiscoverFile($discoverFile)
27 {
28     $json = json_decode(file_get_contents($discoverFile));
29
30     $title    = $json->title;
31     $sections = [];
32     foreach ($json->rows as $row) {
33         $section = (object) [
34             'title' => $row->title,
35             'tiles' => [],
36         ];
37         foreach ($row->tiles as $tileId) {
38             $tileData = $json->tiles[$tileId];
39             if ($tileData->type == 'app') {
40                 $section->tiles[] = (object) [
41                     'type'        => $tileData->type,//app
42                     'thumb'       => $tileData->image,
43                     'title'       => $tileData->title,
44                     'rating'      => $tileData->rating->average,
45                     'ratingCount' => $tileData->rating->count,
46                     'detailUrl'   => '../game/' . str_replace(
47                         'ouya://launcher/details?app=',
48                         '',
49                         $tileData->url
50                     ) . '.htm',
51                 ];
52             } else {
53                 $section->tiles[] = (object) [
54                     'type'        => $tileData->type,//discover
55                     'thumb'       => $tileData->image,
56                     'title'       => $tileData->title,
57                     'detailUrl'   => str_replace(
58                         'ouya://launcher/discover/',
59                         '',
60                         $tileData->url
61                     ) . '.htm',
62                 ];
63             }
64         }
65         $sections[] = $section;
66     }
67
68     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
69     ob_start();
70     include $discoverTemplate;
71     $html = ob_get_contents();
72     ob_end_clean();
73
74     return $html;
75 }
76 ?>