support file template array
[usb-wde1-tools.git] / htmlreport / gen-html.php
1 #!/usr/bin/env php
2 <?php
3 $cfgfile = __DIR__ . '/config.php';
4 if (!file_exists($cfgfile)) {
5     echo "Copy config.php.dist to config.php and adjust it\n";
6     exit(1);
7 }
8 require $cfgfile;
9
10 if (!function_exists('rrd_lastupdate')) {
11     echo "rrd PHP extension is missing\n";
12     exit(2);
13 }
14
15 if (!is_array($filetemplate)) {
16     $filetemplate = array_combine(
17         array_keys($names),
18         array_fill(0, count($names), $filetemplate)
19     );
20 }
21
22 $data = array();
23 foreach ($names as $id => $name) {
24     $data[$id]['name'] = $name;
25     foreach (array('humidity', 'temperature') as $item) {
26         $lu = rrd_lastupdate(
27             str_replace(
28                 array('{item}', '{id}'),
29                 array($item, $id),
30                 $filetemplate[$id]
31             )
32         );
33         if ($lu === false) {
34             throw new Exception(rrd_error());
35         }
36                       
37         $data[$id][$item] = reset($lu['data']);
38     }
39 }
40
41 foreach (glob(__DIR__ . '/templates/*.php') as $tplfile) {
42     $outfile = $outdir . basename($tplfile, '.php');
43     ob_start();
44     include $tplfile;
45     $content = ob_get_contents();
46     ob_end_clean();
47     file_put_contents($outfile, $content);
48 }
49 ?>