1 |
<?php |
2 |
/** |
3 |
* Abstract box class for reports. |
4 |
* |
5 |
* PHP version 5 |
6 |
* |
7 |
* @category Dashboard |
8 |
* @package Buildsystem |
9 |
* @author Romain d'Alverny <rda@mageia.org> |
10 |
* @license MIT License, see LICENSE.txt |
11 |
* @link http://svnweb.mageia.org/svn/soft/dashboard/ |
12 |
*/ |
13 |
|
14 |
/** |
15 |
*/ |
16 |
abstract class Report_Box |
17 |
{ |
18 |
/** |
19 |
*/ |
20 |
var $title = null; |
21 |
|
22 |
/** |
23 |
*/ |
24 |
var $_new_values = null; |
25 |
|
26 |
/** |
27 |
*/ |
28 |
var $working = false; |
29 |
|
30 |
/** |
31 |
*/ |
32 |
function __construct() |
33 |
{ |
34 |
$this->_labels = array_merge(array('?' => '%s %s'), |
35 |
$this->_get_var_definitions()); |
36 |
} |
37 |
|
38 |
/** |
39 |
*/ |
40 |
final public function run_report() |
41 |
{ |
42 |
$path = realpath(dirname(__FILE__)); |
43 |
$files = glob(sprintf('%s/Box/*.php', $path)); |
44 |
|
45 |
foreach ($files as $f) { |
46 |
if (substr(basename($f), 0, 7) != 'ignore_') |
47 |
include $f; |
48 |
} |
49 |
|
50 |
$reports = array(); |
51 |
foreach (get_declared_classes() as $class) { |
52 |
if (is_subclass_of($class, 'Report_Box')) { |
53 |
//echo $class, "\n"; |
54 |
$c = new $class; |
55 |
$reports[] = $c->render(); |
56 |
} |
57 |
} |
58 |
|
59 |
return Report_HTML::render($reports); |
60 |
} |
61 |
|
62 |
/** |
63 |
* lowercase keys! |
64 |
*/ |
65 |
final public function update() |
66 |
{ |
67 |
$this->_get_new_values(); |
68 |
$this->_db_save($this->_new_values); |
69 |
} |
70 |
|
71 |
/** |
72 |
*/ |
73 |
function render () |
74 |
{ |
75 |
$this->load(); |
76 |
|
77 |
// associate key/values with string labels |
78 |
// compute global statuts |
79 |
// push |
80 |
$status = count($this->_new_values); |
81 |
$status_max = count($this->_new_values); |
82 |
$links = $this->_get_links(); |
83 |
$values = array(); |
84 |
|
85 |
$this->_tmp_labels = $this->_labels; |
86 |
if (is_null($this->_new_values)) |
87 |
$this->_new_values = array(); |
88 |
|
89 |
foreach ($this->_tmp_labels as $k => $v) { |
90 |
//foreach ($this->_new_values as $k => $v) { |
91 |
if ($k == '?') |
92 |
continue; |
93 |
|
94 |
$v = $this->_render_value_gen($k); |
95 |
|
96 |
$status -= $v['s']; |
97 |
$values[] = $v; |
98 |
} |
99 |
|
100 |
if (count($this->_tmp_labels) > 0) { |
101 |
foreach ($this->_tmp_labels as $k => $v) { |
102 |
if ($k == '?') |
103 |
continue; |
104 |
|
105 |
$values[] = $this->_render_value_gen($k); |
106 |
} |
107 |
} |
108 |
|
109 |
//echo $status / $status_max; |
110 |
|
111 |
return array( |
112 |
'title' => $this->title, |
113 |
'status' => $status_max > 0 ? round($status / $status_max, 2) : 0, |
114 |
'values' => $values, |
115 |
'links' => $links |
116 |
); |
117 |
} |
118 |
|
119 |
/** |
120 |
*/ |
121 |
final private function _render_value_gen($k = null) |
122 |
{ |
123 |
if (array_key_exists($k, $this->_tmp_labels)) { |
124 |
|
125 |
if ($this->_tmp_labels[$k] == ':render') { |
126 |
$this->_cur_val = isset($this->_new_values[$k]) ? $this->_new_values[$k] : null; |
127 |
$v = call_user_func(array($this, '_render_value_' . $k)); |
128 |
unset($this->_cur_val); |
129 |
} else { |
130 |
$v = $this->_render_value_default($k, $this->_tmp_labels[$k]); |
131 |
} |
132 |
} else { |
133 |
$v = $this->_render_value_default($k); |
134 |
} |
135 |
unset($this->_tmp_labels[$k]); |
136 |
|
137 |
return $v; |
138 |
} |
139 |
|
140 |
/** |
141 |
*/ |
142 |
final private function _render_value_default($k = null, $format = null) |
143 |
{ |
144 |
$score = 0; |
145 |
$class = 'unk'; |
146 |
$weight = 1; |
147 |
$test_case = null; |
148 |
|
149 |
if (is_null($format)) { |
150 |
$format = '%s %s'; |
151 |
} elseif (!is_string($format)) { |
152 |
$test_case = $format['t']; |
153 |
$format = $format['l']; |
154 |
$weight = isset($format['w']) ? $format['w'] : $weight; |
155 |
} |
156 |
|
157 |
$v = isset($this->_new_values[$k]) ? $this->_new_values[$k] : null; |
158 |
if (!is_null($v)) { |
159 |
if (!is_null($test_case)) { |
160 |
$test_case = sprintf('$evalres = ($v %s);', $test_case); |
161 |
eval($test_case); |
162 |
|
163 |
$class = $evalres === true ? 'ok' : 'failed'; |
164 |
} else { |
165 |
$class = 'ok'; |
166 |
} |
167 |
} else { |
168 |
$class = 'unk'; |
169 |
} |
170 |
|
171 |
if ($class == 'failed') |
172 |
$score = -1; |
173 |
elseif ($class == 'ok') |
174 |
$score = 1; |
175 |
|
176 |
$score *= $weight; |
177 |
|
178 |
return array( |
179 |
't' => sprintf($format, $v, $k), |
180 |
'c' => $class, |
181 |
's' => $score |
182 |
); |
183 |
} |
184 |
|
185 |
/** |
186 |
*/ |
187 |
final public function load() |
188 |
{ |
189 |
// go in table TABLE, load all most recent key/values |
190 |
// |
191 |
// TODO(rda) |
192 |
$this->_get_new_values(); |
193 |
$this->_values = array(); |
194 |
} |
195 |
|
196 |
/** |
197 |
*/ |
198 |
final private function _db_save($vals) |
199 |
{ |
200 |
//echo "Saving:\n"; |
201 |
//echo get_class($this),"\n"; |
202 |
//print_r($vals); |
203 |
} |
204 |
|
205 |
/** |
206 |
*/ |
207 |
final private function _get_new_values() |
208 |
{ |
209 |
$values = null; |
210 |
$methods = array(); |
211 |
foreach (get_class_methods($this) as $m) { |
212 |
if (substr($m, 0, 7) == '_fetch_') { |
213 |
$methods[] = $m; |
214 |
} |
215 |
} |
216 |
if (count($methods) == 0) { |
217 |
//echo '> Nothing to fetch.', "\n"; |
218 |
} else { |
219 |
$values = array(); |
220 |
foreach ($methods as $m) { |
221 |
//echo sprintf("> Calling [%s]", $m), "\n"; |
222 |
$values = array_merge($values, $this->$m()); |
223 |
} |
224 |
} |
225 |
|
226 |
$this->_new_values = $values; |
227 |
} |
228 |
|
229 |
/** |
230 |
*/ |
231 |
function _get_links() |
232 |
{ |
233 |
return null; |
234 |
} |
235 |
} |