-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.php
318 lines (274 loc) · 12.5 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
require 'flight/Flight.php';
require 'sendMail.php';
require 'lang.php';
Flight::register('db', 'PDO', array('pgsql:host=localhost;port=5432;dbname=osm;user=osm;'));
Flight::route('/about', function () {
checkLang();
Flight::render(
'about.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => 'About')
);
});
Flight::route('GET /contact', function () {
checkLang();
Flight::render(
'contact.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => 'Contact')
);
});
Flight::route('POST /contact', function () {
$result=sendMail();
Flight::render(
'contact.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => 'Contact','mailResult'=>$result)
);
});
Flight::route('/', function () {
checkLang();
$db = Flight::db();
$query = "SELECT osm_id,cod_istat,name,safe_name FROM it_regioni reg ORDER BY safe_name";
$res = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);
Flight::render(
'listHome.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => 'Italy',
'list'=>$res)
);
});
Flight::route('/api', function () {
$db = Flight::db();
$query = "SELECT osm_id,cod_istat,name,safe_name FROM it_regioni reg ORDER BY safe_name";
$res = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);
//href = baseurl + rawurlencode($row['safe_name']), text = $row['name']
Flight::json(['list' => $res]);
});
Flight::route('/api/@region', function ($region) {
$jsonResponse = [];
$db = Flight::db();
$query = "SELECT reg.osm_id,reg.cod_istat,reg.name,reg.safe_name,reg.bbox,ST_AsGeoJSON(ST_Simplify(reg.geom,0.0001),5)";
$query .=" FROM it_regioni reg WHERE reg.safe_name=".$db->quote($region);
$res = $db->query($query);
$mainData=$res->fetchAll();
if (count($mainData)==0) {
return Flight::notFound();
}
$mainData=$mainData[0];
$arrBbox=explode(',',$mainData['bbox']);
$arrBbox=array_map('floatval',$arrBbox);
$mainData['bbox']=json_encode([[min($arrBbox[1],$arrBbox[3]),min($arrBbox[0],$arrBbox[2])],[max($arrBbox[1],$arrBbox[3]),max($arrBbox[0],$arrBbox[2])]]);
$jsonResponse['mainData'] = $mainData;
$query = "SELECT pro.osm_id,pro.cod_istat,pro.name,pro.safe_name FROM it_province pro JOIN it_regioni reg";
$query .= " ON pro.cod_istat_reg=reg.cod_istat WHERE reg.cod_istat=".$db->quote($mainData['cod_istat'])." ORDER BY pro.safe_name";
$res = $db->query($query);
$res=$res->fetchAll(PDO::FETCH_ASSOC);
$jsonResponse['list'] = $res;
Flight::json($jsonResponse);
});
Flight::route('/api/@region/stats/@days:[0-9]+', function ($region, $days) {
$jsonResponse = [];
$db = Flight::db();
$query = "SELECT stat.* FROM it_regioni reg JOIN it_stats stat ON reg.osm_id = stat.osm_id";
$query .= " WHERE reg.safe_name=".$db->quote($region);
if ($days > 0) {
$query .= " AND data > (CURRENT_DATE - 30)";
}
$query .= " ORDER BY data ASC";
$res = $db->query($query);
$stats=$res->fetchAll(PDO::FETCH_ASSOC);
$jsonResponse['stats'] = $stats;
Flight::json($jsonResponse);
});
Flight::route('/api/@region/@province', function ($region, $province) {
$db = Flight::db();
$query = "SELECT com.osm_id,com.cod_istat,com.name,com.safe_name FROM it_province pro JOIN it_regioni reg ON pro.cod_istat_reg=reg.cod_istat JOIN it_comuni com ON com.cod_istat_reg=reg.cod_istat AND com.cod_istat_pro=pro.cod_istat WHERE reg.safe_name=".$db->quote($region)." AND pro.safe_name=".$db->quote($province)." ORDER BY name";
$res = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);
Flight::json(['list' => $res]);
});
Flight::route('/api/@region/@province/@municipality', function ($region, $province, $municipality) {
$jsonResponse = [];
$db = Flight::db();
$query = "SELECT com.osm_id,com.cod_istat,com.name, com.safe_name, pro.name AS prov_name, reg.name AS reg_name,com.bbox,ST_AsGeoJSON(ST_Simplify(com.geom,0.0001),5) FROM it_regioni reg JOIN it_province pro ON reg.cod_istat = pro.cod_istat_reg JOIN it_comuni com ON pro.cod_istat=com.cod_istat_pro WHERE com.safe_name=".$db->quote($municipality)." AND pro.safe_name=".$db->quote($province);
$res = $db->query($query);
$mainData=$res->fetchAll(PDO::FETCH_ASSOC);
if (count($mainData)==0) {
return Flight::notFound();
}
$mainData=$mainData[0];
$arrBbox=explode(',',$mainData['bbox']);
$arrBbox=array_map('floatval',$arrBbox);
$mainData['bbox']=json_encode([[min($arrBbox[1],$arrBbox[3]),min($arrBbox[0],$arrBbox[2])],[max($arrBbox[1],$arrBbox[3]),max($arrBbox[0],$arrBbox[2])]]);
$jsonResponse['mainData'] = $mainData;
Flight::json($jsonResponse);
});
Flight::route('/api/@region/@province/@municipality/stats/@days', function ($region, $province, $municipality, $days) {
$jsonResponse = [];
$db = Flight::db();
$query = "SELECT stat.* FROM it_comuni com JOIN it_regioni reg ON com.cod_istat_reg = reg.cod_istat JOIN it_stats stat ON com.osm_id = stat.osm_id WHERE com.safe_name=".$db->quote($municipality)." AND reg.safe_name=".$db->quote($region);
if ($days > 0) {
$query .= " AND data > (CURRENT_DATE - 30)";
}
$query .= " ORDER BY data ASC";
$res = $db->query($query);
$stats=$res->fetchAll(PDO::FETCH_ASSOC);
$jsonResponse['stats'] = $stats;
Flight::json($jsonResponse);
});
Flight::route('/@region', function ($region) {
checkLang();
$db = Flight::db();
$query = "SELECT reg.osm_id,reg.cod_istat,reg.name,reg.safe_name,reg.bbox,ST_AsGeoJSON(ST_Simplify(reg.geom,0.0001),5)";
$query .=" FROM it_regioni reg WHERE reg.safe_name=".$db->quote($region);
$res = $db->query($query);
$mainData=$res->fetchAll(PDO::FETCH_ASSOC);
if (count($mainData)==0) {
return Flight::notFound();
}
$mainData=$mainData[0];
$arrBbox=explode(',',$mainData['bbox']);
$arrBbox=array_map('floatval',$arrBbox);
$mainData['bbox']=json_encode([[min($arrBbox[1],$arrBbox[3]),min($arrBbox[0],$arrBbox[2])],[max($arrBbox[1],$arrBbox[3]),max($arrBbox[0],$arrBbox[2])]]);
//name with diacritics for display
$realName=$mainData['name'];
$query = "SELECT stat.* FROM it_regioni reg JOIN it_stats stat ON reg.osm_id = stat.osm_id";
$query .= " WHERE reg.cod_istat=".$db->quote($mainData['cod_istat'])." AND data > (CURRENT_DATE - 30) ORDER BY data ASC";
$res = $db->query($query);
$stats=$res->fetchAll();
$query = "SELECT pro.osm_id,pro.cod_istat,pro.name,pro.safe_name FROM it_province pro JOIN it_regioni reg";
$query .= " ON pro.cod_istat_reg=reg.cod_istat WHERE reg.cod_istat=".$db->quote($mainData['cod_istat'])." ORDER BY pro.safe_name";
$res = $db->query($query);
$res=$res->fetchAll(PDO::FETCH_ASSOC);
Flight::render(
'extractAndList.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => $realName,
'region_safe' => $region,
'region' => $realName,
'list' => $res,
'mainData' => $mainData,
'stats' => $stats)
);
});
Flight::route('/@region/stats', function ($region) {
checkLang();
$db = Flight::db();
$query = "SELECT reg.osm_id,reg.cod_istat,reg.name,reg.safe_name";
$query .=" FROM it_regioni reg WHERE reg.safe_name=".$db->quote($region);
$res = $db->query($query);
$mainData=$res->fetchAll();
if (count($mainData)==0) {
return Flight::notFound();
}
$mainData=$mainData[0];
$realName=$mainData['name'];
$query = "SELECT stat.* FROM it_regioni reg JOIN it_stats stat ON reg.osm_id = stat.osm_id";
$query .= " WHERE reg.cod_istat=".$db->quote($mainData['cod_istat'])." ORDER BY data ASC";
$res = $db->query($query);
$stats=$res->fetchAll(PDO::FETCH_ASSOC);
Flight::render(
'extractStats.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => $realName,
'region_safe' => $region,
'region' => $realName,
'mainData' => $mainData,
'stats' => $stats)
);
});
Flight::route('/@region/@province', function ($region, $province) {
checkLang();
$db = Flight::db();
$query = "SELECT com.osm_id,com.cod_istat,com.name,com.safe_name FROM it_province pro JOIN it_regioni reg ON pro.cod_istat_reg=reg.cod_istat JOIN it_comuni com ON com.cod_istat_reg=reg.cod_istat AND com.cod_istat_pro=pro.cod_istat WHERE reg.safe_name=".$db->quote($region)." AND pro.safe_name=".$db->quote($province)." ORDER BY name";
$res = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);
Flight::render(
'list.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => $province.', '.$region,
'region' => $region,
'province' => $province,
'list'=>$res)
);
});
Flight::route('/@region/@province/@municipality', function ($region, $province, $municipality) {
checkLang();
$db = Flight::db();
$query = "SELECT com.osm_id,com.cod_istat,com.name, com.safe_name, pro.name AS prov_name, reg.name AS reg_name,com.bbox,ST_AsGeoJSON(ST_Simplify(com.geom,0.0001),5) FROM it_regioni reg JOIN it_province pro ON reg.cod_istat = pro.cod_istat_reg JOIN it_comuni com ON pro.cod_istat=com.cod_istat_pro WHERE com.safe_name=".$db->quote($municipality)." AND pro.safe_name=".$db->quote($province);
$res = $db->query($query);
$mainData=$res->fetchAll();
if (count($mainData)==0) {
return Flight::notFound();
}
$mainData=$mainData[0];
$arrBbox=explode(',',$mainData['bbox']);
$arrBbox=array_map('floatval',$arrBbox);
$mainData['bbox']=json_encode([[min($arrBbox[1],$arrBbox[3]),min($arrBbox[0],$arrBbox[2])],[max($arrBbox[1],$arrBbox[3]),max($arrBbox[0],$arrBbox[2])]]);
$proName=$mainData['prov_name'];
$regName=$mainData['reg_name'];
$munName=$mainData['name'];
$query = "SELECT stat.* FROM it_comuni com JOIN it_stats stat ON com.osm_id = stat.osm_id WHERE com.cod_istat=".$db->quote($mainData['cod_istat'])." AND data > (CURRENT_DATE - 30) ORDER BY data ASC";
$res = $db->query($query);
$stats=$res->fetchAll();
Flight::render(
'extract.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => $municipality.', '.$province.', '.$region,
'region' => $regName,
'region_safe' => $region,
'province' => $proName,
'province_safe'=> $province,
'municipality' => $munName,
'municipality_safe'=> $municipality,
'mainData' => $mainData,
'stats' => $stats)
);
});
Flight::route('/@region/@province/@municipality/stats', function ($region, $province, $municipality) {
checkLang();
$db = Flight::db();
$query = "SELECT com.osm_id,com.cod_istat,com.name, com.safe_name, pro.name AS prov_name, reg.name AS reg_name FROM it_regioni reg JOIN it_province pro ON reg.cod_istat = pro.cod_istat_reg JOIN it_comuni com ON pro.cod_istat=com.cod_istat_pro WHERE com.safe_name=".$db->quote($municipality)." AND pro.safe_name=".$db->quote($province);
$res = $db->query($query);
$mainData=$res->fetchAll(PDO::FETCH_ASSOC);
if (count($mainData)==0) {
return Flight::notFound();
}
$mainData=$mainData[0];
$proName=$mainData['prov_name'];
$regName=$mainData['reg_name'];
$munName=$mainData['name'];
$query = "SELECT stat.* FROM it_comuni com JOIN it_stats stat ON com.osm_id = stat.osm_id WHERE com.cod_istat=".$db->quote($mainData['cod_istat'])." ORDER BY data ASC";
$res = $db->query($query);
$stats=$res->fetchAll();
Flight::render(
'extractStats.php',
array(
'baseUrl'=>Flight::request()->base,
'pTitle' => $municipality.', '.$province.', '.$region,
'region' => $regName,
'region_safe' => $region,
'province' => $proName,
'province_safe'=> $province,
'municipality' => $munName,
'municipality_safe'=> $municipality,
'mainData' => $mainData,
'stats' => $stats)
);
});
Flight::map('notFound', function () {
Flight::render(
'404.php',
array(
'baseUrl'=>Flight::request()->base)
);
});
Flight::start();