Old version cleaned
[weathermon.git] / web / archive.php
1 <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
5   <meta name="GENERATOR" content="Mozilla/4.72 (X11; U; Linux 2.2.12-20smp i686) [Netscape]">    
6   <title>WeatherMon (архив)</title>                      
7   <link rel="icon" href="favicon.png" />
8   <style type="text/css">
9     a.year   {
10         padding:20px;
11         margin: 10px;
12         border:2px solid darkgrey;
13         border-radius: 10px;
14         display:inline-block;
15         width:200px;
16         float:left;
17         font-size:100%;
18         text-align:center;
19     }                                          
20     a.month   {
21         font-size:100%;
22         text-align:center;
23     }                                          
24     a:visited { text-decoration: none; color:darkblue; }
25     a:link { text-decoration: none; color:blue; }
26     a:hover { text-decoration: none; color:navy; }  
27   </style>
28 </head>
29 <body text="black" bgcolor="silver" link="blue" vlink="#000080" alink="#FF0000">         
30 <?php
31
32 include('config_local.php');
33 include('calendar.php');
34 include('units.php');
35
36 $year = $_REQUEST['year']; 
37 $month = $_REQUEST['month'];
38 $day = $_REQUEST['day'];
39
40 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
41   die($err);
42   }  
43
44 $db -> exec('SET CHARACTER SET utf8');
45
46 if (! $year) {
47         
48   $q = $db -> prepare(
49     'select 
50        date_format(day,\'%Y\') as year
51      from 
52        calendar
53      group by year  
54      order by year');
55   $q -> execute();
56 ?>
57 <h1>Архив метеоданных</h1>
58 <?php
59   while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
60 ?> 
61   <a class="year" href="?year=<?php echo $row['year']; ?>">Данные за <?php echo $row['year']; ?> год</a><br>
62 <?php
63   }
64       
65 } else {
66
67   if (! $month) {
68       
69     $next_year = $year+1;
70     $year      = sprintf('%04d',$year);
71     $next_year = sprintf('%04d',$next_year);
72     $q = $db -> prepare(
73           'select 
74              date_format(day,\'%m\') as month,
75              date_format(day,\'%d\') as day
76            from 
77              calendar
78            where 
79              day>=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
80              day<str_to_date(\''.$next_year.'-01-01\',\'%Y-%m-%d\')
81            group by month,day
82            order by month,day'
83           );
84     $q -> execute();
85 ?>
86 <h1 align="center"><?php echo $year; ?> год</h1>
87 <?php
88     $data = $q -> fetchAll(PDO::FETCH_ASSOC);
89     $months = [];
90     foreach ($data as $row) {
91       $days [$year.$row['month'].$row['day']] = '&month='.$row['month'].'&day='.$row['day'];
92       $months[$row['month']]=1;
93     }
94     $m = 1;
95     echo '<table align="center">';
96     for ($i=1; $i<=3; $i++) {
97       echo '<tr>';
98       for ($j=1; $j<=4; $j++) {
99         echo '<td valign="top">';
100         $month=sprintf('%02d',$m);
101         $month_name = strftime('%B',mktime(0,0,0,$month,1,$year));
102         if (!empty($months[$month])) {
103           echo '<h3 align="center"><a class="month" href="?year='.$year.'&month='.$month.'">'.$month_name.'</a></h3>';
104         } else {
105           echo '<h3 align="center">'.$month_name.'</h3>';
106         }
107         calendar($year,$month,$days,'?year='.$year.'%s',0);
108         echo '</td>';
109         $m++;
110         }
111       echo '</tr>';
112     }
113     echo '</table>';
114     
115     $next_year = $year+1;
116
117     $q = $db -> prepare (
118       'select 
119          s.id sensor,p.id param,s.s_description sensor_name,p.st_description param_name,
120          count(distinct day) cnt,
121          round(min(v.min),1) min_value,
122          round(max(v.max),1) max_value,
123          u.name_short unit
124        from
125          sensors_ranges v,sensors s,st_parameters p,units u
126        where 
127          v.sensor=s.id and
128          v.parameter=p.id and
129          p.st_unit=u.id and
130          v.day>=str_to_date(\''.$year.'-01-01\',\'%Y-%m-%d\') and 
131          v.day<str_to_date(\''.$next_year.'-01-01\',\'%Y-%m-%d\')
132       group by s.id,p.id,s.s_description,p.st_description
133       order by s.id,p.id'
134       );
135         
136     $q -> execute();
137     $sensors = $q -> fetchAll(PDO::FETCH_ASSOC);
138       
139     foreach ($sensors as $sensor) {
140
141       $units = get_unit($db,$sensor['param']);
142       $from_unit = $units['from'];
143       $to_unit = $units['to'];
144       $param_unit = $units['name'];
145
146       $min_val = convert_unit($db,$sensor['min_value'],$from_unit,$to_unit);
147       $max_val = convert_unit($db,$sensor['max_value'],$from_unit,$to_unit);
148      
149       echo '<h3 align="center">'.$sensor['param_name'].'('.$sensor['sensor_name'].')</h3>';
150       echo '<table align="center">';
151       echo '<tr><td>';
152       echo 'Минимальное значение за год <b>'.$min_val.' '.$param_unit.'</b><br>';
153       echo 'Максимальное значение за год <b>'.$max_val.' '.$param_unit.'</b><br>';
154       echo '</td></tr>';
155       echo '</table>';
156
157       if ($sensor['cnt']>1) {
158         
159         echo '<center><img src="image_minmax.php?sensor='.$sensor['sensor'].'&param='.$sensor['param'].
160           '&type=year&year='.$year.'"></center>';
161         
162       }
163       
164     }
165
166
167   } else {
168       
169     if (!$day) {
170       
171       $next_year = $year;
172       $next_month = $month+1;
173
174       if ($next_month == 13) {
175         $next_month = 1;
176         $next_year = $next_year+1;
177       }
178
179       $next_month = sprintf('%02d',$next_month); 
180       $next_year  = sprintf('%04d',$next_year);
181       $month      = sprintf('%02d',$month);
182       $year      = sprintf('%04d',$year);
183
184       $q = $db -> prepare( 
185           'select  
186              date_format(day,\'%d\') as day
187            from 
188              sensors_ranges
189            where 
190              day>=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
191              day<str_to_date(\''.$next_year.'-'.$next_month.'-01\',\'%Y-%m-%d\')
192            group by day
193            order by day'
194           );
195       $q -> execute();
196       
197       $data = $q -> fetchAll(PDO::FETCH_ASSOC);
198
199       $month_name = strftime('%B %Y',mktime(0,0,0,$month,1,$year));
200
201 ?>
202 <h1 align="center"><?php echo 'Данные за '.$month_name; ?></h1>
203 <?php          
204       foreach ($data as $row) {
205         $days [$year.$month.$row['day']] = $row['day'];
206       }
207
208       calendar($year,$month,$days,'?year='.$year.'&month='.$month.'&day=%s',3);
209               
210       $q = $db -> prepare (
211           'select 
212              s.id sensor,p.id param,s.s_description sensor_name,p.st_description param_name,
213              count(distinct day) cnt,
214              round(min(v.min),1) min_value,
215              round(max(v.max),1) max_value,
216              u.name_short unit
217            from
218              sensors_ranges v,sensors s,st_parameters p,units u
219            where 
220              v.sensor=s.id and
221              v.parameter=p.id and
222              p.st_unit=u.id and
223              v.day>=str_to_date(\''.$year.'-'.$month.'-01\',\'%Y-%m-%d\') and 
224              v.day<str_to_date(\''.$next_year.'-'.$next_month.'-01\',\'%Y-%m-%d\')
225            group by s.id,p.id,s.s_description,p.st_description
226            order by s.id,p.id'
227         );
228         
229       $q -> execute();
230       $sensors = $q -> fetchAll(PDO::FETCH_ASSOC);
231       
232       foreach ($sensors as $sensor) {
233
234         $units = get_unit($db,$sensor['param']);
235         $from_unit = $units['from'];
236         $to_unit = $units['to'];
237         $param_unit = $units['name'];
238
239         $min_val = convert_unit($db,$sensor['min_value'],$from_unit,$to_unit);
240         $max_val = convert_unit($db,$sensor['max_value'],$from_unit,$to_unit);
241       
242         echo '<h3 align="center">'.$sensor['param_name'].'('.$sensor['sensor_name'].')</h3>';
243         echo '<table align="center">';
244         echo '<tr><td>';
245         echo 'Минимальное значение за месяц <b>'.$min_val.' '.$param_unit.'</b><br>';
246         echo 'Максимальное значение за месяц <b>'.$max_val.' '.$param_unit.'</b><br>';
247         echo '</td></tr>';
248         echo '</table>';
249
250         if ($sensor['cnt']>1) {
251         
252           echo '<center><img src="image_minmax.php?sensor='.$sensor['sensor'].'&param='.$sensor['param'].
253             '&type=month&year='.$year.'&month='.$month.'"></center>';
254         
255         }
256       
257       }
258
259     } else {
260
261       $month      = sprintf('%02d',$month);
262       $year      = sprintf('%04d',$year);
263       $day       = sprintf('%02d',$day);
264
265       $q = $db -> prepare(
266             'select 
267                 distinct v.sensor as sensor_id,s.s_description,p.id as param_id,p.st_description 
268              from 
269                 sensors_ranges v,st_parameters p,sensors s 
270              where 
271                 v.day=str_to_date(\''.$year.'-'.$month.'-'.$day.'\',\'%Y-%m-%d\')
272                 and v.sensor=s.id 
273                 and v.parameter=p.id'
274             );
275       $q -> execute();
276
277       while ($row = $q -> fetch(PDO::FETCH_ASSOC)) {
278
279         echo '<h3 align="center">'.$row['s_description'].'/'.$row['st_description'].'</h3>';
280 ?>
281 <center><img src="image.php?sensor=<?php echo $row['sensor_id']; ?>&param=<?php echo $row['param_id']?>&type=range&fromdate=<?php echo $year.$month.$day.'000000'; ?>&todate=<?php echo $year.$month.$day.'235959'?>"></center>
282 <?php  
283
284       }                                                            
285     }
286   }
287 }
288 ?>
289 </body>
290