source: trunk/Modules/Measure/Page.php

Last change on this file was 97, checked in by chronos, 2 days ago
  • Fixed: Wrong parameter type in time edit function.
File size: 13.5 KB
Line 
1<?php
2
3class PageMain extends Page
4{
5 var int $Time;
6 var array $Months;
7 var array $GraphTimeRanges;
8
9 function __construct(System $System)
10 {
11 parent::__construct($System);
12 $this->Months = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září',
13 'Říjen', 'Listopad', 'Prosinec');
14
15 $this->GraphTimeRanges = array(
16 'hour' => array(
17 'caption' => 'Hodina',
18 'period' => 3600,
19 ),
20 'day' => array(
21 'caption' => 'Den',
22 'period' => 3600 * 24,
23 ),
24 'week' => array(
25 'caption' => 'Týden',
26 'period' => 3600 * 24 * 7,
27 ),
28 'month' => array(
29 'caption' => 'Měsíc',
30 'period' => 3600 * 24 * 30,
31 ),
32 'year' => array(
33 'caption' => 'Rok',
34 'period' => 3600 * 24 * 365,
35 ),
36 'years' => array(
37 'caption' => 'Desetiletí',
38 'period' => 3600 * 24 * 365 * 10,
39 ),
40 'all' => array(
41 'caption' => 'Vše',
42 'period' => -1,
43 ),
44 );
45 $this->Time = time();
46 }
47
48 function GetTimeRange(): string
49 {
50 if (!array_key_exists($_SESSION['Period'], $this->GraphTimeRanges))
51 $_SESSION['Period'] = 'day';
52
53 $Result = $this->GraphTimeRanges[$_SESSION['Period']]['period'];
54 if ($Result == -1)
55 {
56 $Measure = $this->LoadMeasure($_SESSION['Measure']);
57 $FirstValue = $this->GetFirstMeasure($Measure);
58 $LastValue = $this->GetLastMeasure($Measure);
59 $Result = $LastValue['Time'] - $FirstValue['Time'];
60 }
61 return $Result;
62 }
63
64 function EditTime(string $Time): string
65 {
66 $Output = '<form style="display: inline;" action="?Operation=SetTime&amp;Time='.$Time.'" method="post">';
67 $TimeParts = getdate($_SESSION[$Time]);
68
69 // Day selection
70 $Output .= '<select name="Day">';
71 for ($I = 1; $I < 32; $I++)
72 {
73 if ($I == $TimeParts['mday']) $Selected = ' selected="1"'; else $Selected = '';
74 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
75 }
76 $Output .= '</select>. ';
77
78 // Month selection
79 $Output .= '<select name="Month">';
80 foreach ($this->Months as $Index => $Month)
81 {
82 if ($Index == $TimeParts['mon']) $Selected = ' selected="1"'; else $Selected = '';
83 if ($Index > 0) $Output .= '<option value="'.$Index.'"'.$Selected.'>'.$Month.'</option>';
84 }
85 $Output .= '</select>. ';
86
87 // Year selection
88 $Output .= '<select name="Year">';
89 for ($I = 2000; $I <= date("Y"); $I++)
90 {
91 if ($I == $TimeParts['year']) $Selected = ' selected="1"'; else $Selected = '';
92 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
93 }
94 $Output .= '</select> &nbsp;&nbsp; ';
95
96 // Hour selection
97 $Output .= '<select name="Hour">';
98 for ($I = 0; $I < 24; $I++)
99 {
100 if ($I == $TimeParts['hours']) $Selected = ' selected="1"'; else $Selected = '';
101 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
102 }
103 $Output .= '</select> : ';
104
105 // Minute selection
106 $Output .= '<select name="Minute">';
107 for ($I = 0; $I < 60; $I++)
108 {
109 if ($I == $TimeParts['minutes']) $Selected = ' selected="1"'; else $Selected = '';
110 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
111 }
112 $Output .= '</select> ';
113
114 $Output .= '<input type="submit" value="Nastavit">';
115 $Output .= '</form>';
116 $Output .= ' <form style="display: inline;" action="?Operation=SetTimeNow&amp;Time='.$Time.'" method="post">';
117 $Output .= '<input type="submit" value="Aktuální čas">';
118 $Output .= '</form>';
119
120 return $Output;
121 }
122
123 function GetFirstMeasure(array $Measure): array
124 {
125 $Result2 = $this->Database->select($Measure['DataTable'], '`Time`, `Avg`', '(`Measure`='.$Measure['Id'].') AND (`Level`=0) ORDER BY `Time` ASC LIMIT 1');
126 if ($Result2->num_rows > 0)
127 {
128 $Row = $Result2->fetch_array();
129 $LastMeasureTime = MysqlDateTimeToTime($Row['Time']);
130 $LastMeasureValue = $Row['Avg'];
131 } else {
132 $LastMeasureTime = $this->Time;
133 $LastMeasureValue = 0;
134 }
135 return array('Time' => $LastMeasureTime, 'Value' => $LastMeasureValue);
136 }
137
138 function GetLastMeasure(array $Measure): array
139 {
140 $Result2 = $this->Database->select($Measure['DataTable'], '`Time`, `Avg`', '(`Measure`='.$Measure['Id'].') AND (`Level`=0) ORDER BY `Time` DESC LIMIT 1');
141 if ($Result2->num_rows > 0)
142 {
143 $Row = $Result2->fetch_array();
144 $LastMeasureTime = MysqlDateTimeToTime($Row['Time']);
145 $LastMeasureValue = $Row['Avg'];
146 } else {
147 $LastMeasureTime = $this->Time;
148 $LastMeasureValue = 0;
149 }
150 return array('Time' => $LastMeasureTime, 'Value' => $LastMeasureValue);
151 }
152
153 function LoadMeasure(int $Id): array
154 {
155 $DbResult = $this->Database->select('Measure', '*', '( `Enabled`=1) AND (`Id`='.$Id.') AND ((`PermissionView`="all") OR (`PermissionView`="'.
156 gethostbyaddr($_SERVER['REMOTE_ADDR']).'"))');
157 $DbRow = $DbResult->fetch_array();
158 return $DbRow;
159 }
160
161 /* Produce table with available measures */
162 function ShowMeasureTable(): string
163 {
164 $PrefixMultiplier = new PrefixMultiplier();
165 $Output = '<table border="1" cellspacing="0" cellpadding="2" style="font-size: small; margin: 0px auto;">';
166 $Output .= '<tr><th>Měřená veličina</th><th>Poslední hodnota</th><th>Čas posledního měření</th><th>Interpolace</th><th>Poznámky</th>';
167 if (array_key_exists('Debug', $_GET)) $Output .= '<th>Počet položek</th><th>Čas vykonání</th>';
168 $Output .= '</tr>';
169 $Result = $this->Database->select('Measure', '*', '( `Enabled`=1) AND ((`PermissionView`="all") OR (`PermissionView`="'.
170 gethostbyaddr($_SERVER['REMOTE_ADDR']).'")) ORDER BY `Description`');
171 while ($Measure = $Result->fetch_array())
172 {
173 $StopWatchStart = GetMicrotime();
174 if (array_key_exists('Debug', $_GET))
175 {
176 $DbResult = $this->Database->select($Measure['DataTable'], 'COUNT(*)', '`Measure`='.$Measure['Id']);
177 $RowCount = $DbResult->fetch_array();
178 $RowCount = $RowCount[0];
179 }
180 $Result2 = $this->Database->select($Measure['DataTable'], '`Time`, `Avg`', '(`Measure`='.
181 $Measure['Id'].') AND (`Level`=0) ORDER BY `Time` DESC LIMIT 1');
182 if ($Result2->num_rows > 0)
183 {
184 $Row = $Result2->fetch_array();
185 $LastMeasureTime = date('j.n.Y G:i:s', MysqlDateTimeToTime($Row['Time']));
186 $LastMeasureValue = $PrefixMultiplier->Add($Row['Avg'], $Measure['Unit']);
187 } else {
188 $LastMeasureTime = '&nbsp;';
189 $LastMeasureValue = '&nbsp;';
190 }
191 if ($Measure['Continuity'] == 1) $Interpolate = 'Ano'; else $Interpolate = 'Ne';
192 if ($Measure['Info'] == '') $Measure['Info'] = '&nbsp;';
193 $GenerationTime = floor((GetMicrotime() - $StopWatchStart) * 1000 ) / 1000;
194 $Output .= '<tr><td style="text-align: left"><a href="?Measure='.$Measure['Id'].
195 '&amp;Differential=0">'.$Measure['Description'].'</a></td><td align="center">'.
196 $LastMeasureValue.'</td><td align="center">'.$LastMeasureTime.'</td><td align="center">'.
197 $Interpolate.'</td><td>'.$Measure['Info'].'</td>';
198 if (array_key_exists('Debug', $_GET))
199 $Output .= '<td>'.$RowCount.'</td><td>'.$GenerationTime.'</td>';
200 $Output .= '</tr>';
201 }
202 $Output .= '</table>';
203 return $Output;
204 }
205
206 function ShowGraph(): string
207 {
208 $Output = '<strong>Graf:</strong><br>';
209 $Output .= '<img alt="Graf" src="Graph.php?Measure='.$_SESSION['Measure'].'&amp;From='.
210 $_SESSION['TimeStart'].'&amp;To='.$_SESSION['TimeEnd'].
211 '&amp;Width=750&amp;Height=200&amp;Differential='.
212 $_SESSION['Differential'].'" width="750" height="200"><br>';
213 $Output .= '<a href="?Measure='.$_SESSION['Measure'].'&amp;TimeStart='.
214 $_SESSION['TimeStart'].'&amp;TimeEnd='.$_SESSION['TimeEnd'].
215 '&amp;TimeSpecify=1&amp;Differential='.$_SESSION['Differential'].
216 '">Odkaz na vybraný graf</a><br/>';
217 return $Output;
218 }
219
220 function ShowTimeRange(): string
221 {
222 $Output = '';
223
224 if (!array_key_exists('Operation', $_GET)) $_GET['Operation'] = '';
225 switch ($_GET['Operation'])
226 {
227 case 'SetTime':
228 if (array_key_exists('Time', $_GET) and array_key_exists('Month', $_POST) and array_key_exists('Day', $_POST) and
229 array_key_exists('Year', $_POST) and array_key_exists('Hour', $_POST) and array_key_exists('Minute', $_POST))
230 {
231 if (($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
232 {
233 $_SESSION[$_GET['Time']] = mktime($_POST['Hour'], $_POST['Minute'], 0, $_POST['Month'],
234 $_POST['Day'], $_POST['Year']);
235 }
236 }
237 break;
238 case 'SetTimeNow':
239 if (array_key_exists('Time', $_GET))
240 {
241 if (($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
242 {
243 $_SESSION[$_GET['Time']] = $this->Time;
244 }
245 }
246 break;
247
248 }
249 $Output .= '<strong>Časový úsek:</strong><br>';
250
251 // Show graph time range menu
252 if ($_SESSION['TimeSpecify'] == 0)
253 {
254 $Output .= 'Délka úseku: ';
255 foreach ($this->GraphTimeRanges as $Index => $Item)
256 $Output .= '<a href="?Period='.$Index.'">'.$Item['caption'].'</a>&nbsp;';
257 $Output .= '<br>';
258 $Output .= '<a href="?TimeSpecify=1">Přesnější nastavení...</a><br>';
259 } else {
260 $Output .= '<table cellspacing="0" cellpadding="2" border="0" style="margin: 0px auto;">';
261 $Output .= '<tr><td>Počátek:</td><td>'.$this->EditTime('TimeStart').'</td></tr>';
262 $Output .= '<tr><td>Konec:</td><td>'.$this->EditTime('TimeEnd').'</td></tr>';
263 $Output .= '</table>';
264 $Output .= '<a href="?TimeSpecify=0">Jednoduché nastavení...</a><br>';
265 }
266 $Output .= 'Posun: <a href="?Move=LeftEnd">|&lt;</a> <a href="?Move=Left">&lt;</a> '.
267 '<a href="?Move=Right">&gt;</a> <a href="?Move=RightEnd">&gt;|</a> <a href="?Move=Now">Nyní</a><br>';
268 $Output .= '<br/>';
269 return $Output;
270 }
271
272 function HandleURL(): void
273 {
274 foreach ($this->System->Config['DefaultVariables'] as $Index => $Variable)
275 {
276 if (!array_key_exists($Index, $_SESSION)) $_SESSION[$Index] = $Variable;
277 //if (array_key_exists($Index, $_GET)) $_SESSION[$Index] = $_GET[$Index];
278 //if (array_key_exists($Index, $_POST)) $_SESSION[$Index] = $_POST[$Index];
279 //$$Index = $_SESSION[$Index];
280 }
281 if (array_key_exists('Period', $_GET))
282 {
283 $_SESSION['Period'] = $_GET['Period'];
284 // Update time start according time period
285 if ($_SESSION['Period'] == 'all')
286 {
287 $Measure = $this->LoadMeasure($_SESSION['Measure']);
288 $FirstValue = $this->GetFirstMeasure($Measure);
289 $LastValue = $this->GetLastMeasure($Measure);
290 $_SESSION['TimeStart'] = $FirstValue['Time'];
291 $_SESSION['TimeEnd'] = $LastValue['Time'];
292 } else
293 {
294 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
295 }
296 }
297 if (array_key_exists('TimeStart', $_GET))
298 {
299 $_SESSION['TimeStart'] = $_GET['TimeStart'];
300 }
301 if (array_key_exists('TimeEnd', $_GET))
302 {
303 $_SESSION['TimeStart'] = $_GET['TimeEnd'];
304 }
305 if (array_key_exists('Differential', $_GET))
306 {
307 $_SESSION['Differential'] = $_GET['Differential'];
308 }
309 if (array_key_exists('Measure', $_GET))
310 {
311 $_SESSION['Measure'] = $_GET['Measure'];
312 }
313 if (array_key_exists('TimeSpecify', $_GET))
314 {
315 if (($_SESSION['TimeSpecify'] == 1) and ($_GET['TimeSpecify'] == 0))
316 {
317 $_SESSION['TimeEnd'] = $this->Time - 60;
318 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
319 }
320 $_SESSION['TimeSpecify'] = $_GET['TimeSpecify'];
321 }
322
323 if (array_key_exists('Move', $_GET))
324 {
325 $Move = $_GET['Move'];
326 if ($Move == 'Left')
327 {
328 $_SESSION['TimeStart'] = $_SESSION['TimeStart'] - $this->GetTimeRange();
329 $_SESSION['TimeEnd'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
330 } else
331 if ($Move == 'Right')
332 {
333 $_SESSION['TimeStart'] = $_SESSION['TimeStart'] + $this->GetTimeRange();
334 $_SESSION['TimeEnd'] = $_SESSION['TimeEnd'] + $this->GetTimeRange();
335 } else
336 if ($Move == 'Now')
337 {
338 $_SESSION['TimeEnd'] = $this->Time - 60;
339 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
340 } else
341 if ($Move == 'LeftEnd')
342 {
343 $Measure = $this->LoadMeasure($_SESSION['Measure']);
344 $FirstValue = $this->GetFirstMeasure($Measure);
345 $_SESSION['TimeStart'] = $FirstValue['Time'];
346 $_SESSION['TimeEnd'] = $_SESSION['TimeStart'] + $this->GetTimeRange();
347 } else
348 if ($Move == 'RightEnd')
349 {
350 $Measure = $this->LoadMeasure($_SESSION['Measure']);
351 $LastValue = $this->GetLastMeasure($Measure);
352 $_SESSION['TimeEnd'] = $LastValue['Time'];
353 $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GetTimeRange();
354 }
355 }
356 }
357
358 function Show(): string
359 {
360 global $Config;
361
362 $this->HandleURL();
363
364 $Output = '<div style="text-align: center"><div class="Title">'.$Config['Web']['Title'].'</div>';
365
366 $Result = $this->Database->select('Measure', 'Id', '(`Enabled`=1) AND '.
367 '((`PermissionView`="all") OR (`PermissionView`="'.gethostbyaddr($_SERVER['REMOTE_ADDR']).'")) '.
368 'AND (`Id`='.($_SESSION['Measure'] * 1).')');
369 if ($Result->num_rows == 0)
370 $_SESSION['Measure'] = $Config['DefaultVariables']['Measure'];
371
372 $Output .= $this->ShowTimeRange();
373 $Output .= $this->ShowGraph();
374 $Output .= '<br/>';
375 $Output .= $this->ShowMeasureTable();
376 $Output .= '</div>';
377 return $Output;
378 }
379}
Note: See TracBrowser for help on using the repository browser.