1
|
|
|
<?php |
2
|
|
|
require_once(dirname(__FILE__).'/class.Image.php'); |
3
|
|
|
$global_query = "SELECT marine_output.* FROM marine_output"; |
4
|
|
|
|
5
|
|
|
class Marine{ |
6
|
|
|
public $db; |
7
|
|
|
|
8
|
|
|
public function __construct($dbc = null) { |
9
|
|
|
$Connection = new Connection($dbc); |
10
|
|
|
$this->db = $Connection->db(); |
11
|
|
|
if ($this->db === null) die('Error: No DB connection. (Marine)'); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get SQL query part for filter used |
16
|
|
|
* @param Array $filter the filter |
17
|
|
|
* @return Array the SQL part |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
public function getFilter($filter = array(),$where = false,$and = false) { |
21
|
|
|
global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver; |
22
|
|
|
$filters = array(); |
23
|
|
|
if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) { |
24
|
|
|
if (isset($globalStatsFilters[$globalFilterName][0]['source'])) { |
25
|
|
|
$filters = $globalStatsFilters[$globalFilterName]; |
26
|
|
|
} else { |
27
|
|
|
$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
if (isset($filter[0]['source'])) { |
31
|
|
|
$filters = array_merge($filters,$filter); |
32
|
|
|
} |
33
|
|
|
if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter); |
34
|
|
|
$filter_query_join = ''; |
35
|
|
|
$filter_query_where = ''; |
36
|
|
|
foreach($filters as $flt) { |
37
|
|
|
if (isset($flt['idents']) && !empty($flt['idents'])) { |
38
|
|
|
if (isset($flt['source'])) { |
39
|
|
|
$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) spfi ON spfi.fammarine_id = marine_output.fammarine_id"; |
40
|
|
|
} else { |
41
|
|
|
$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."')) spfi ON spfi.fammarine_id = marine_output.fammarine_id"; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
if (isset($filter['source']) && !empty($filter['source'])) { |
46
|
|
|
$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')"; |
47
|
|
|
} |
48
|
|
|
if (isset($filter['ident']) && !empty($filter['ident'])) { |
49
|
|
|
$filter_query_where .= " AND ident = '".$filter['ident']."'"; |
50
|
|
|
} |
51
|
|
|
if (isset($filter['mmsi']) && !empty($filter['mmsi'])) { |
52
|
|
|
$filter_query_where .= " AND mmsi = '".$filter['mmsi']."'"; |
53
|
|
|
} |
54
|
|
|
if (isset($filter['year']) && $filter['year'] != '') { |
55
|
|
|
if ($globalDBdriver == 'mysql') { |
56
|
|
|
$filter_query_where .= " AND YEAR(marine_output.date) = '".$filter['year']."'"; |
57
|
|
|
} else { |
58
|
|
|
$filter_query_where .= " AND EXTRACT(YEAR FROM marine_output.date) = '".$filter['year']."'"; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
if (isset($filter['month']) && $filter['month'] != '') { |
62
|
|
|
if ($globalDBdriver == 'mysql') { |
63
|
|
|
$filter_query_where .= " AND MONTH(marine_output.date) = '".$filter['month']."'"; |
64
|
|
|
} else { |
65
|
|
|
$filter_query_where .= " AND EXTRACT(MONTH FROM marine_output.date) = '".$filter['month']."'"; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
if (isset($filter['day']) && $filter['day'] != '') { |
69
|
|
|
if ($globalDBdriver == 'mysql') { |
70
|
|
|
$filter_query_where .= " AND DAY(marine_output.date) = '".$filter['day']."'"; |
71
|
|
|
} else { |
72
|
|
|
$filter_query_where .= " AND EXTRACT(DAY FROM marine_output.date) = '".$filter['day']."'"; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE'; |
76
|
|
|
elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND'; |
77
|
|
|
if ($filter_query_where != '') { |
78
|
|
|
$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where); |
79
|
|
|
} |
80
|
|
|
$filter_query = $filter_query_join.$filter_query_where; |
81
|
|
|
return $filter_query; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Executes the SQL statements to get the spotter information |
86
|
|
|
* |
87
|
|
|
* @param String $query the SQL query |
88
|
|
|
* @param Array $params parameter of the query |
89
|
|
|
* @param String $limitQuery the limit query |
90
|
|
|
* @return Array the spotter information |
91
|
|
|
* |
92
|
|
|
*/ |
93
|
|
|
public function getDataFromDB($query, $params = array(), $limitQuery = '',$schedules = false) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
date_default_timezone_set('UTC'); |
96
|
|
|
if (!is_string($query)) |
97
|
|
|
{ |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($limitQuery != "") |
102
|
|
|
{ |
103
|
|
|
if (!is_string($limitQuery)) |
104
|
|
|
{ |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$sth = $this->db->prepare($query.$limitQuery); |
111
|
|
|
$sth->execute($params); |
112
|
|
|
} catch (PDOException $e) { |
113
|
|
|
printf("Invalid query : %s\nWhole query: %s\n",$e->getMessage(), $query.$limitQuery); |
114
|
|
|
exit(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$num_rows = 0; |
118
|
|
|
$spotter_array = array(); |
119
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
120
|
|
|
{ |
121
|
|
|
$num_rows++; |
122
|
|
|
$temp_array = array(); |
123
|
|
|
if (isset($row['marine_live_id'])) { |
124
|
|
|
$temp_array['marine_id'] = $this->getMarineIDBasedOnFamMarineID($row['fammarine_id']); |
125
|
|
|
/* |
126
|
|
|
} elseif (isset($row['spotter_archive_id'])) { |
127
|
|
|
$temp_array['spotter_id'] = $row['spotter_archive_id']; |
128
|
|
|
} elseif (isset($row['spotter_archive_output_id'])) { |
129
|
|
|
$temp_array['spotter_id'] = $row['spotter_archive_output_id']; |
130
|
|
|
*/} |
131
|
|
|
elseif (isset($row['marineid'])) { |
132
|
|
|
$temp_array['marine_id'] = $row['marineid']; |
133
|
|
|
} else { |
134
|
|
|
$temp_array['marine_id'] = ''; |
135
|
|
|
} |
136
|
|
|
if (isset($row['fammarine_id'])) $temp_array['fammarine_id'] = $row['fammarine_id']; |
137
|
|
|
if (isset($row['mmsi'])) $temp_array['mmsi'] = $row['mmsi']; |
138
|
|
|
if (isset($row['type'])) $temp_array['type'] = $row['type']; |
139
|
|
|
if (isset($row['type_id'])) $temp_array['type_id'] = $row['type_id']; |
140
|
|
|
if (isset($row['ident'])) $temp_array['ident'] = $row['ident']; |
141
|
|
|
if (isset($row['latitude'])) $temp_array['latitude'] = $row['latitude']; |
142
|
|
|
if (isset($row['longitude'])) $temp_array['longitude'] = $row['longitude']; |
143
|
|
|
if (isset($row['format_source'])) $temp_array['format_source'] = $row['format_source']; |
144
|
|
|
if (isset($row['heading'])) { |
145
|
|
|
$temp_array['heading'] = $row['heading']; |
146
|
|
|
$heading_direction = $this->parseDirection($row['heading']); |
147
|
|
|
if (isset($heading_direction[0]['direction_fullname'])) $temp_array['heading_name'] = $heading_direction[0]['direction_fullname']; |
148
|
|
|
} |
149
|
|
|
if (isset($row['ground_speed'])) $temp_array['ground_speed'] = $row['ground_speed']; |
150
|
|
|
|
151
|
|
|
if(isset($temp_array['mmsi']) && $temp_array['mmsi'] != "") |
152
|
|
|
{ |
153
|
|
|
$Image = new Image($this->db); |
154
|
|
|
if (isset($temp_array['ident']) && $temp_array['ident'] != '') $image_array = $Image->getMarineImage($temp_array['mmsi'],'',$temp_array['ident']); |
155
|
|
|
else $image_array = $Image->getMarineImage($temp_array['mmsi']); |
156
|
|
|
unset($Image); |
157
|
|
|
if (count($image_array) > 0) { |
158
|
|
|
$temp_array['image'] = $image_array[0]['image']; |
159
|
|
|
$temp_array['image_thumbnail'] = $image_array[0]['image_thumbnail']; |
160
|
|
|
$temp_array['image_source'] = $image_array[0]['image_source']; |
161
|
|
|
$temp_array['image_source_website'] = $image_array[0]['image_source_website']; |
162
|
|
|
$temp_array['image_copyright'] = $image_array[0]['image_copyright']; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (isset($row['date'])) { |
167
|
|
|
$dateArray = $this->parseDateString($row['date']); |
168
|
|
|
if ($dateArray['seconds'] < 10) |
169
|
|
|
{ |
170
|
|
|
$temp_array['date'] = "a few seconds ago"; |
171
|
|
|
} elseif ($dateArray['seconds'] >= 5 && $dateArray['seconds'] < 30) |
172
|
|
|
{ |
173
|
|
|
$temp_array['date'] = "half a minute ago"; |
174
|
|
|
} elseif ($dateArray['seconds'] >= 30 && $dateArray['seconds'] < 60) |
175
|
|
|
{ |
176
|
|
|
$temp_array['date'] = "about a minute ago"; |
177
|
|
|
} elseif ($dateArray['minutes'] < 5) |
178
|
|
|
{ |
179
|
|
|
$temp_array['date'] = "a few minutes ago"; |
180
|
|
|
} elseif ($dateArray['minutes'] >= 5 && $dateArray['minutes'] < 60) |
181
|
|
|
{ |
182
|
|
|
$temp_array['date'] = "about ".$dateArray['minutes']." minutes ago"; |
183
|
|
|
} elseif ($dateArray['hours'] < 2) |
184
|
|
|
{ |
185
|
|
|
$temp_array['date'] = "about an hour ago"; |
186
|
|
|
} elseif ($dateArray['hours'] >= 2 && $dateArray['hours'] < 24) |
187
|
|
|
{ |
188
|
|
|
$temp_array['date'] = "about ".$dateArray['hours']." hours ago"; |
189
|
|
|
} else { |
190
|
|
|
$temp_array['date'] = date("M j Y, g:i a",strtotime($row['date']." UTC")); |
191
|
|
|
} |
192
|
|
|
$temp_array['date_minutes_past'] = $dateArray['minutes']; |
193
|
|
|
$temp_array['date_iso_8601'] = date("c",strtotime($row['date']." UTC")); |
194
|
|
|
$temp_array['date_rfc_2822'] = date("r",strtotime($row['date']." UTC")); |
195
|
|
|
$temp_array['date_unix'] = strtotime($row['date']." UTC"); |
196
|
|
|
if (isset($row['last_seen']) && $row['last_seen'] != '') { |
197
|
|
|
if (strtotime($row['last_seen']) > strtotime($row['date'])) { |
198
|
|
|
$temp_array['duration'] = strtotime($row['last_seen']) - strtotime($row['date']); |
199
|
|
|
$temp_array['last_seen_date_iso_8601'] = date("c",strtotime($row['last_seen']." UTC")); |
200
|
|
|
$temp_array['last_seen_date_rfc_2822'] = date("r",strtotime($row['last_seen']." UTC")); |
201
|
|
|
$temp_array['last_seen_date_unix'] = strtotime($row['last_seen']." UTC"); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$fromsource = NULL; |
|
|
|
|
207
|
|
|
if (isset($row['source_name']) && $row['source_name'] != '') $temp_array['source_name'] = $row['source_name']; |
208
|
|
|
if (isset($row['over_country']) && $row['over_country'] != '') $temp_array['over_country'] = $row['over_country']; |
209
|
|
|
if (isset($row['distance']) && $row['distance'] != '') $temp_array['distance'] = $row['distance']; |
210
|
|
|
$temp_array['query_number_rows'] = $num_rows; |
211
|
|
|
$spotter_array[] = $temp_array; |
212
|
|
|
} |
213
|
|
|
if ($num_rows == 0) return array(); |
214
|
|
|
$spotter_array[0]['query_number_rows'] = $num_rows; |
215
|
|
|
return $spotter_array; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Gets all the spotter information based on the latest data entry |
221
|
|
|
* |
222
|
|
|
* @return Array the spotter information |
223
|
|
|
* |
224
|
|
|
*/ |
225
|
|
|
public function getLatestMarineData($limit = '', $sort = '', $filter = array()) |
226
|
|
|
{ |
227
|
|
|
global $global_query; |
228
|
|
|
date_default_timezone_set('UTC'); |
229
|
|
|
$filter_query = $this->getFilter($filter); |
230
|
|
|
if ($limit != "") |
231
|
|
|
{ |
232
|
|
|
$limit_array = explode(",", $limit); |
233
|
|
|
$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT); |
234
|
|
|
$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT); |
235
|
|
|
if ($limit_array[0] >= 0 && $limit_array[1] >= 0) |
236
|
|
|
{ |
237
|
|
|
//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1]; |
238
|
|
|
$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0]; |
239
|
|
|
} else $limit_query = ""; |
240
|
|
|
} else $limit_query = ""; |
241
|
|
|
if ($sort != "") |
242
|
|
|
{ |
243
|
|
|
$search_orderby_array = $this->getOrderBy(); |
244
|
|
|
$orderby_query = $search_orderby_array[$sort]['sql']; |
245
|
|
|
} else { |
246
|
|
|
$orderby_query = " ORDER BY marine_output.date DESC"; |
247
|
|
|
} |
248
|
|
|
$query = $global_query.$filter_query." ".$orderby_query; |
249
|
|
|
$spotter_array = $this->getDataFromDB($query, array(),$limit_query,true); |
250
|
|
|
return $spotter_array; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/* |
254
|
|
|
* Gets all the spotter information based on the spotter id |
255
|
|
|
* |
256
|
|
|
* @return Array the spotter information |
257
|
|
|
* |
258
|
|
|
*/ |
259
|
|
|
public function getMarineDataByID($id = '') |
260
|
|
|
{ |
261
|
|
|
global $global_query; |
262
|
|
|
|
263
|
|
|
date_default_timezone_set('UTC'); |
264
|
|
|
if ($id == '') return array(); |
265
|
|
|
$additional_query = "marine_output.fammarine_id = :id"; |
266
|
|
|
$query_values = array(':id' => $id); |
267
|
|
|
$query = $global_query." WHERE ".$additional_query." "; |
268
|
|
|
$spotter_array = $this->getDataFromDB($query,$query_values); |
269
|
|
|
return $spotter_array; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Gets all the spotter information based on the callsign |
274
|
|
|
* |
275
|
|
|
* @return Array the spotter information |
276
|
|
|
* |
277
|
|
|
*/ |
278
|
|
|
public function getMarineDataByIdent($ident = '', $limit = '', $sort = '', $filter = array()) |
279
|
|
|
{ |
280
|
|
|
global $global_query; |
281
|
|
|
|
282
|
|
|
date_default_timezone_set('UTC'); |
283
|
|
|
|
284
|
|
|
$query_values = array(); |
285
|
|
|
$limit_query = ''; |
286
|
|
|
$additional_query = ''; |
287
|
|
|
$filter_query = $this->getFilter($filter,true,true); |
288
|
|
|
if ($ident != "") |
289
|
|
|
{ |
290
|
|
|
if (!is_string($ident)) |
291
|
|
|
{ |
292
|
|
|
return false; |
293
|
|
|
} else { |
294
|
|
|
$additional_query = " AND (marine_output.ident = :ident)"; |
295
|
|
|
$query_values = array(':ident' => $ident); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if ($limit != "") |
300
|
|
|
{ |
301
|
|
|
$limit_array = explode(",", $limit); |
302
|
|
|
|
303
|
|
|
$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT); |
304
|
|
|
$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT); |
305
|
|
|
|
306
|
|
|
if ($limit_array[0] >= 0 && $limit_array[1] >= 0) |
307
|
|
|
{ |
308
|
|
|
//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1]; |
309
|
|
|
$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0]; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if ($sort != "") |
314
|
|
|
{ |
315
|
|
|
$search_orderby_array = $this->getOrderBy(); |
316
|
|
|
$orderby_query = $search_orderby_array[$sort]['sql']; |
317
|
|
|
} else { |
318
|
|
|
$orderby_query = " ORDER BY marine_output.date DESC"; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$query = $global_query.$filter_query." marine_output.ident <> '' ".$additional_query." ".$orderby_query; |
322
|
|
|
//echo $query."\n"; |
323
|
|
|
$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query); |
324
|
|
|
|
325
|
|
|
return $spotter_array; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function getMarineDataByDate($date = '', $limit = '', $sort = '',$filter = array()) |
329
|
|
|
{ |
330
|
|
|
global $global_query, $globalTimezone, $globalDBdriver; |
331
|
|
|
|
332
|
|
|
$query_values = array(); |
333
|
|
|
$limit_query = ''; |
334
|
|
|
$additional_query = ''; |
335
|
|
|
|
336
|
|
|
$filter_query = $this->getFilter($filter,true,true); |
337
|
|
|
|
338
|
|
|
if ($date != "") |
339
|
|
|
{ |
340
|
|
|
if ($globalTimezone != '') { |
341
|
|
|
date_default_timezone_set($globalTimezone); |
342
|
|
|
$datetime = new DateTime($date); |
343
|
|
|
$offset = $datetime->format('P'); |
344
|
|
|
} else { |
345
|
|
|
date_default_timezone_set('UTC'); |
346
|
|
|
$datetime = new DateTime($date); |
347
|
|
|
$offset = '+00:00'; |
348
|
|
|
} |
349
|
|
|
if ($globalDBdriver == 'mysql') { |
350
|
|
|
$additional_query = " AND DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date "; |
351
|
|
|
$query_values = array(':date' => $datetime->format('Y-m-d'), ':offset' => $offset); |
352
|
|
|
} elseif ($globalDBdriver == 'pgsql') { |
353
|
|
|
$additional_query = " AND to_char(marine_output.date AT TIME ZONE :timezone,'YYYY-mm-dd') = :date "; |
354
|
|
|
$query_values = array(':date' => $datetime->format('Y-m-d'), ':timezone' => $globalTimezone); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
if ($limit != "") |
359
|
|
|
{ |
360
|
|
|
$limit_array = explode(",", $limit); |
361
|
|
|
|
362
|
|
|
$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT); |
363
|
|
|
$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT); |
364
|
|
|
|
365
|
|
|
if ($limit_array[0] >= 0 && $limit_array[1] >= 0) |
366
|
|
|
{ |
367
|
|
|
//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1]; |
368
|
|
|
$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0]; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
if ($sort != "") |
373
|
|
|
{ |
374
|
|
|
$search_orderby_array = $this->getOrderBy(); |
375
|
|
|
$orderby_query = $search_orderby_array[$sort]['sql']; |
376
|
|
|
} else { |
377
|
|
|
$orderby_query = " ORDER BY marine_output.date DESC"; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$query = $global_query.$filter_query." marine_output.ident <> '' ".$additional_query.$orderby_query; |
381
|
|
|
$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query); |
382
|
|
|
return $spotter_array; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
|
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Gets all source name |
389
|
|
|
* |
390
|
|
|
* @param String type format of source |
391
|
|
|
* @return Array list of source name |
392
|
|
|
* |
393
|
|
|
*/ |
394
|
|
|
public function getAllSourceName($type = '',$filters = array()) |
395
|
|
|
{ |
396
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
397
|
|
|
$query_values = array(); |
398
|
|
|
$query = "SELECT DISTINCT marine_output.source_name |
399
|
|
|
FROM marine_output".$filter_query." marine_output.source_name <> ''"; |
400
|
|
|
if ($type != '') { |
401
|
|
|
$query_values = array(':type' => $type); |
402
|
|
|
$query .= " AND format_source = :type"; |
403
|
|
|
} |
404
|
|
|
$query .= " ORDER BY marine_output.source_name ASC"; |
405
|
|
|
|
406
|
|
|
$sth = $this->db->prepare($query); |
407
|
|
|
if (!empty($query_values)) $sth->execute($query_values); |
408
|
|
|
else $sth->execute(); |
409
|
|
|
|
410
|
|
|
$source_array = array(); |
411
|
|
|
$temp_array = array(); |
412
|
|
|
|
413
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
414
|
|
|
{ |
415
|
|
|
$temp_array['source_name'] = $row['source_name']; |
416
|
|
|
$source_array[] = $temp_array; |
417
|
|
|
} |
418
|
|
|
return $source_array; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Gets a list of all idents/callsigns |
424
|
|
|
* |
425
|
|
|
* @return Array list of ident/callsign names |
426
|
|
|
* |
427
|
|
|
*/ |
428
|
|
|
public function getAllIdents($filters = array()) |
429
|
|
|
{ |
430
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
431
|
|
|
$query = "SELECT DISTINCT marine_output.ident |
432
|
|
|
FROM marine_output".$filter_query." marine_output.ident <> '' |
433
|
|
|
ORDER BY marine_output.date ASC LIMIT 700 OFFSET 0"; |
434
|
|
|
|
435
|
|
|
$sth = $this->db->prepare($query); |
436
|
|
|
$sth->execute(); |
437
|
|
|
|
438
|
|
|
$ident_array = array(); |
439
|
|
|
$temp_array = array(); |
440
|
|
|
|
441
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
442
|
|
|
{ |
443
|
|
|
$temp_array['ident'] = $row['ident']; |
444
|
|
|
$ident_array[] = $temp_array; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
return $ident_array; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Gets all info from a mmsi |
452
|
|
|
* |
453
|
|
|
* @return Array list of mmsi info |
454
|
|
|
* |
455
|
|
|
*/ |
456
|
|
|
public function getIdentity($mmsi) |
457
|
|
|
{ |
458
|
|
|
$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT); |
459
|
|
|
$query = "SELECT * FROM marine_identity WHERE mmsi = :mmsi LIMIT 1"; |
460
|
|
|
$sth = $this->db->prepare($query); |
461
|
|
|
$sth->execute(array(':mmsi' => $mmsi)); |
462
|
|
|
$result = $sth->fetchAll(PDO::FETCH_ASSOC); |
463
|
|
|
if (isset($result[0])) return $result[0]; |
464
|
|
|
else return array(); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Add identity |
469
|
|
|
* |
470
|
|
|
*/ |
471
|
|
|
public function addIdentity($mmsi,$imo,$ident,$callsign,$type) |
472
|
|
|
{ |
473
|
|
|
$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT); |
474
|
|
|
if ($mmsi != '') { |
475
|
|
|
$imo = filter_var($imo,FILTER_SANITIZE_NUMBER_INT); |
476
|
|
|
$ident = filter_var($ident,FILTER_SANITIZE_STRING); |
477
|
|
|
$callsign = filter_var($callsign,FILTER_SANITIZE_STRING); |
478
|
|
|
$type = filter_var($type,FILTER_SANITIZE_STRING); |
479
|
|
|
$identinfo = $this->getIdentity($mmsi); |
480
|
|
|
if ($identinfo == '') { |
481
|
|
|
$query = "INSERT INTO marine_identity (mmsi,imo,call_sign,ship_name,type) VALUES (:mmsi,:imo,:call_sign,:ship_name,:type)"; |
482
|
|
|
$sth = $this->db->prepare($query); |
483
|
|
|
$sth->execute(array(':mmsi' => $mmsi,':imo' => $imo,':call_sign' => $callsign,':ship_name' => $ident,':type' => $type)); |
484
|
|
|
} elseif ($ident != '' && $identinfo != $ident) { |
485
|
|
|
$query = "UPDATE marine_identity SET ship_name = :ship_name,type = :type WHERE mmsi = :mmsi"; |
486
|
|
|
$sth = $this->db->prepare($query); |
487
|
|
|
$sth->execute(array(':mmsi' => $mmsi,':ship_name' => $ident,':type' => $type)); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/* |
493
|
|
|
* Gets a list of all dates |
494
|
|
|
* |
495
|
|
|
* @return Array list of date names |
496
|
|
|
* |
497
|
|
|
*/ |
498
|
|
|
public function getAllDates() |
499
|
|
|
{ |
500
|
|
|
global $globalTimezone, $globalDBdriver; |
501
|
|
|
if ($globalTimezone != '') { |
502
|
|
|
date_default_timezone_set($globalTimezone); |
503
|
|
|
$datetime = new DateTime(); |
504
|
|
|
$offset = $datetime->format('P'); |
505
|
|
|
} else $offset = '+00:00'; |
506
|
|
|
|
507
|
|
|
if ($globalDBdriver == 'mysql') { |
508
|
|
|
$query = "SELECT DISTINCT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) as date |
509
|
|
|
FROM marine_output |
510
|
|
|
WHERE marine_output.date <> '' |
511
|
|
|
ORDER BY marine_output.date ASC LIMIT 0,100"; |
512
|
|
|
} else { |
513
|
|
|
$query = "SELECT DISTINCT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') as date |
514
|
|
|
FROM marine_output |
515
|
|
|
WHERE marine_output.date <> '' |
516
|
|
|
ORDER BY marine_output.date ASC LIMIT 0,100"; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
$sth = $this->db->prepare($query); |
520
|
|
|
$sth->execute(array(':offset' => $offset)); |
521
|
|
|
|
522
|
|
|
$date_array = array(); |
523
|
|
|
$temp_array = array(); |
524
|
|
|
|
525
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
526
|
|
|
{ |
527
|
|
|
$temp_array['date'] = $row['date']; |
528
|
|
|
|
529
|
|
|
$date_array[] = $temp_array; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
return $date_array; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* Update ident tracker data |
537
|
|
|
* |
538
|
|
|
* @param String $fammarine_id the ID |
539
|
|
|
* @param String $ident the marine ident |
540
|
|
|
* @return String success or false |
541
|
|
|
* |
542
|
|
|
*/ |
543
|
|
|
public function updateIdentMarineData($fammarine_id = '', $ident = '',$fromsource = NULL) |
|
|
|
|
544
|
|
|
{ |
545
|
|
|
$query = 'UPDATE marine_output SET ident = :ident WHERE fammarine_id = :fammarine_id'; |
546
|
|
|
$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident); |
547
|
|
|
try { |
548
|
|
|
$sth = $this->db->prepare($query); |
549
|
|
|
$sth->execute($query_values); |
550
|
|
|
} catch (PDOException $e) { |
551
|
|
|
return "error : ".$e->getMessage(); |
552
|
|
|
} |
553
|
|
|
return "success"; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Update Status data |
558
|
|
|
* |
559
|
|
|
* @param String $fammarine_id the ID |
560
|
|
|
* @param String $status_id the marine status id |
561
|
|
|
* @param String $status the marine status |
562
|
|
|
* @return String success or false |
563
|
|
|
* |
564
|
|
|
*/ |
565
|
|
|
public function updateStatusMarineData($fammarine_id = '', $status_id = '',$status = '') |
566
|
|
|
{ |
567
|
|
|
|
568
|
|
|
$query = 'UPDATE marine_output SET status = :status, status_id = :status_id WHERE fammarine_id = :fammarine_id'; |
569
|
|
|
$query_values = array(':fammarine_id' => $fammarine_id,':status' => $status,':status_id' => $status_id); |
570
|
|
|
|
571
|
|
|
try { |
572
|
|
|
$sth = $this->db->prepare($query); |
573
|
|
|
$sth->execute($query_values); |
574
|
|
|
} catch (PDOException $e) { |
575
|
|
|
return "error : ".$e->getMessage(); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
return "success"; |
579
|
|
|
|
580
|
|
|
} |
581
|
|
|
/** |
582
|
|
|
* Update latest marine data |
583
|
|
|
* |
584
|
|
|
* @param String $fammarine_id the ID |
585
|
|
|
* @param String $ident the marine ident |
586
|
|
|
* @return String success or false |
587
|
|
|
* |
588
|
|
|
*/ |
589
|
|
|
public function updateLatestMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $groundspeed = NULL, $date = '') |
590
|
|
|
{ |
591
|
|
|
$query = 'UPDATE marine_output SET ident = :ident, last_latitude = :last_latitude, last_longitude = :last_longitude, last_seen = :last_seen, last_ground_speed = :last_ground_speed WHERE fammarine_id = :fammarine_id'; |
592
|
|
|
$query_values = array(':fammarine_id' => $fammarine_id,':last_latitude' => $latitude,':last_longitude' => $longitude, ':last_ground_speed' => $groundspeed,':last_seen' => $date,':ident' => $ident); |
593
|
|
|
|
594
|
|
|
try { |
595
|
|
|
$sth = $this->db->prepare($query); |
596
|
|
|
$sth->execute($query_values); |
597
|
|
|
} catch (PDOException $e) { |
598
|
|
|
return "error : ".$e->getMessage(); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
return "success"; |
602
|
|
|
|
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Adds a new spotter data |
607
|
|
|
* |
608
|
|
|
* @param String $fammarine_id the ID |
609
|
|
|
* @param String $ident the marine ident |
610
|
|
|
* @param String $departure_airport_icao the departure airport |
|
|
|
|
611
|
|
|
* @param String $arrival_airport_icao the arrival airport |
|
|
|
|
612
|
|
|
* @param String $latitude latitude of flight |
613
|
|
|
* @param String $longitude latitude of flight |
614
|
|
|
* @param String $waypoints waypoints of flight |
|
|
|
|
615
|
|
|
* @param String $heading heading of flight |
616
|
|
|
* @param String $groundspeed speed of flight |
617
|
|
|
* @param String $date date of flight |
618
|
|
|
* @param String $departure_airport_time departure time of flight |
|
|
|
|
619
|
|
|
* @param String $arrival_airport_time arrival time of flight |
|
|
|
|
620
|
|
|
* @param String $squawk squawk code of flight |
|
|
|
|
621
|
|
|
* @param String $route_stop route stop of flight |
|
|
|
|
622
|
|
|
* @param String $highlight highlight or not |
|
|
|
|
623
|
|
|
* @param String $ModeS ModesS code of flight |
|
|
|
|
624
|
|
|
* @param String $registration registration code of flight |
|
|
|
|
625
|
|
|
* @param String $pilot_id pilot id of flight (for virtual airlines) |
|
|
|
|
626
|
|
|
* @param String $pilot_name pilot name of flight (for virtual airlines) |
|
|
|
|
627
|
|
|
* @param String $verticalrate vertival rate of flight |
|
|
|
|
628
|
|
|
* @return String success or false |
629
|
|
|
*/ |
630
|
|
|
public function addMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $heading = '', $groundspeed = '', $date = '', $mmsi = '',$type = '',$typeid = '',$imo = '',$callsign = '',$arrival_code = '',$arrival_date = '',$status = '',$statusid = '',$format_source = '', $source_name = '') |
|
|
|
|
631
|
|
|
{ |
632
|
|
|
global $globalURL, $globalMarineImageFetch; |
633
|
|
|
|
634
|
|
|
//$Image = new Image($this->db); |
635
|
|
|
$Common = new Common(); |
636
|
|
|
|
637
|
|
|
date_default_timezone_set('UTC'); |
638
|
|
|
|
639
|
|
|
//getting the registration |
640
|
|
|
if ($fammarine_id != "") |
641
|
|
|
{ |
642
|
|
|
if (!is_string($fammarine_id)) |
643
|
|
|
{ |
644
|
|
|
return false; |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
$fromsource = NULL; |
|
|
|
|
648
|
|
|
//getting the airline information |
649
|
|
|
if ($ident != "") |
650
|
|
|
{ |
651
|
|
|
if (!is_string($ident)) |
652
|
|
|
{ |
653
|
|
|
return false; |
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
if ($latitude != "") |
658
|
|
|
{ |
659
|
|
|
if (!is_numeric($latitude)) |
660
|
|
|
{ |
661
|
|
|
return false; |
662
|
|
|
} |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
if ($longitude != "") |
666
|
|
|
{ |
667
|
|
|
if (!is_numeric($longitude)) |
668
|
|
|
{ |
669
|
|
|
return false; |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
if ($heading != "") |
674
|
|
|
{ |
675
|
|
|
if (!is_numeric($heading)) |
676
|
|
|
{ |
677
|
|
|
return false; |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
if ($mmsi != "") |
681
|
|
|
{ |
682
|
|
|
if (!is_numeric($mmsi)) |
683
|
|
|
{ |
684
|
|
|
return false; |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
if ($groundspeed != "") |
689
|
|
|
{ |
690
|
|
|
if (!is_numeric($groundspeed)) |
691
|
|
|
{ |
692
|
|
|
return false; |
693
|
|
|
} |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
|
697
|
|
|
if ($date == "" || strtotime($date) < time()-20*60) |
698
|
|
|
{ |
699
|
|
|
$date = date("Y-m-d H:i:s", time()); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING); |
703
|
|
|
$ident = filter_var($ident,FILTER_SANITIZE_STRING); |
704
|
|
|
$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
705
|
|
|
$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
706
|
|
|
$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT); |
707
|
|
|
$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
708
|
|
|
$format_source = filter_var($format_source,FILTER_SANITIZE_STRING); |
709
|
|
|
$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING); |
710
|
|
|
$type = filter_var($type,FILTER_SANITIZE_STRING); |
711
|
|
|
$status = filter_var($status,FILTER_SANITIZE_STRING); |
712
|
|
|
$imo = filter_var($imo,FILTER_SANITIZE_STRING); |
713
|
|
|
$callsign = filter_var($callsign,FILTER_SANITIZE_STRING); |
|
|
|
|
714
|
|
|
$arrival_code = filter_var($arrival_code,FILTER_SANITIZE_STRING); |
715
|
|
|
$arrival_date = filter_var($arrival_date,FILTER_SANITIZE_STRING); |
716
|
|
|
|
717
|
|
|
if (isset($globalMarineImageFetch) && $globalMarineImageFetch === TRUE) { |
718
|
|
|
$Image = new Image($this->db); |
719
|
|
|
$image_array = $Image->getMarineImage($mmsi,$imo,$ident); |
720
|
|
|
if (!isset($image_array[0]['mmsi'])) { |
721
|
|
|
$Image->addMarineImage($mmsi,$imo,$ident); |
722
|
|
|
} |
723
|
|
|
unset($Image); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
if ($latitude == '' && $longitude == '') { |
727
|
|
|
$latitude = 0; |
728
|
|
|
$longitude = 0; |
729
|
|
|
} |
730
|
|
|
if ($heading == '' || $Common->isInteger($heading) === false) $heading = 0; |
731
|
|
|
if ($groundspeed == '' || $Common->isInteger($groundspeed) === false) $groundspeed = 0; |
732
|
|
|
if ($arrival_date == '') $arrival_date = NULL; |
733
|
|
|
$query = "INSERT INTO marine_output (fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, mmsi, type, status,imo,arrival_port_name,arrival_port_date) |
734
|
|
|
VALUES (:fammarine_id,:ident,:latitude,:longitude,:heading,:speed,:date,:format_source, :source_name,:mmsi,:type,:status,:imo,:arrival_port_name,:arrival_port_date)"; |
735
|
|
|
|
736
|
|
|
$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':heading' => $heading,':speed' => $groundspeed,':date' => $date,':format_source' => $format_source, ':source_name' => $source_name,':mmsi' => $mmsi,':type' => $type,':status' => $status,':imo' => $imo,':arrival_port_name' => $arrival_code,':arrival_port_date' => $arrival_date); |
737
|
|
|
try { |
738
|
|
|
|
739
|
|
|
$sth = $this->db->prepare($query); |
740
|
|
|
$sth->execute($query_values); |
741
|
|
|
$this->db = null; |
742
|
|
|
} catch (PDOException $e) { |
743
|
|
|
return "error : ".$e->getMessage(); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
return "success"; |
747
|
|
|
|
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
|
751
|
|
|
/** |
752
|
|
|
* Gets the aircraft ident within the last hour |
753
|
|
|
* |
754
|
|
|
* @return String the ident |
755
|
|
|
* |
756
|
|
|
*/ |
757
|
|
|
public function getIdentFromLastHour($ident) |
758
|
|
|
{ |
759
|
|
|
global $globalDBdriver, $globalTimezone; |
760
|
|
|
if ($globalDBdriver == 'mysql') { |
761
|
|
|
$query = "SELECT marine_output.ident FROM marine_output |
762
|
|
|
WHERE marine_output.ident = :ident |
763
|
|
|
AND marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) |
764
|
|
|
AND marine_output.date < UTC_TIMESTAMP()"; |
765
|
|
|
$query_data = array(':ident' => $ident); |
766
|
|
|
} else { |
767
|
|
|
$query = "SELECT marine_output.ident FROM marine_output |
768
|
|
|
WHERE marine_output.ident = :ident |
769
|
|
|
AND marine_output.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS' |
770
|
|
|
AND marine_output.date < now() AT TIME ZONE 'UTC'"; |
771
|
|
|
$query_data = array(':ident' => $ident); |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
$sth = $this->db->prepare($query); |
775
|
|
|
$sth->execute($query_data); |
776
|
|
|
$ident_result=''; |
777
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
778
|
|
|
{ |
779
|
|
|
$ident_result = $row['ident']; |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
return $ident_result; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Gets the aircraft data from the last 20 seconds |
788
|
|
|
* |
789
|
|
|
* @return Array the spotter data |
790
|
|
|
* |
791
|
|
|
*/ |
792
|
|
|
public function getRealTimeData($q = '') |
793
|
|
|
{ |
794
|
|
|
global $globalDBdriver; |
795
|
|
|
$additional_query = ''; |
796
|
|
|
if ($q != "") |
797
|
|
|
{ |
798
|
|
|
if (!is_string($q)) |
799
|
|
|
{ |
800
|
|
|
return false; |
801
|
|
|
} else { |
802
|
|
|
$q_array = explode(" ", $q); |
803
|
|
|
foreach ($q_array as $q_item){ |
804
|
|
|
$q_item = filter_var($q_item,FILTER_SANITIZE_STRING); |
805
|
|
|
$additional_query .= " AND ("; |
806
|
|
|
$additional_query .= "(marine_output.ident like '%".$q_item."%')"; |
807
|
|
|
$additional_query .= ")"; |
808
|
|
|
} |
809
|
|
|
} |
810
|
|
|
} |
811
|
|
|
if ($globalDBdriver == 'mysql') { |
812
|
|
|
$query = "SELECT marine_output.* FROM marine_output |
813
|
|
|
WHERE marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 SECOND) ".$additional_query." |
814
|
|
|
AND marine_output.date < UTC_TIMESTAMP()"; |
815
|
|
|
} else { |
816
|
|
|
$query = "SELECT marine_output.* FROM marine_output |
817
|
|
|
WHERE marine_output.date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 SECONDS' ".$additional_query." |
818
|
|
|
AND marine_output.date::timestamp < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'"; |
819
|
|
|
} |
820
|
|
|
$spotter_array = $this->getDataFromDB($query, array()); |
821
|
|
|
|
822
|
|
|
return $spotter_array; |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
|
826
|
|
|
|
827
|
|
|
|
828
|
|
|
/** |
829
|
|
|
* Gets all number of flight over countries |
830
|
|
|
* |
831
|
|
|
* @return Array the airline country list |
832
|
|
|
* |
833
|
|
|
*/ |
834
|
|
|
|
835
|
|
|
public function countAllMarineOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array()) |
836
|
|
|
{ |
837
|
|
|
global $globalDBdriver, $globalArchive; |
838
|
|
|
//$filter_query = $this->getFilter($filters,true,true); |
839
|
|
|
$Connection= new Connection($this->db); |
840
|
|
|
if (!$Connection->tableExists('countries')) return array(); |
841
|
|
|
require_once('class.SpotterLive.php'); |
842
|
|
|
if (!isset($globalArchive) || $globalArchive !== TRUE) { |
843
|
|
|
$MarineLive = new MarineLive($this->db); |
844
|
|
|
$filter_query = $MarineLive->getFilter($filters,true,true); |
845
|
|
|
$filter_query .= " over_country IS NOT NULL AND over_country <> ''"; |
846
|
|
|
if ($olderthanmonths > 0) { |
847
|
|
|
if ($globalDBdriver == 'mysql') { |
848
|
|
|
$filter_query .= ' AND marine_live.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) '; |
849
|
|
|
} else { |
850
|
|
|
$filter_query .= " AND marine_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
851
|
|
|
} |
852
|
|
|
} |
853
|
|
|
if ($sincedate != '') { |
854
|
|
|
if ($globalDBdriver == 'mysql') { |
855
|
|
|
$filter_query .= " AND marine_live.date > '".$sincedate."' "; |
856
|
|
|
} else { |
857
|
|
|
$filter_query .= " AND marine_live.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
858
|
|
|
} |
859
|
|
|
} |
860
|
|
|
$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT fammarine_id,over_country FROM marine_live".$filter_query.") l ON c.iso2 = l.over_country "; |
861
|
|
|
} else { |
862
|
|
|
require_once(dirname(__FILE__)."/class.MarineArchive.php"); |
863
|
|
|
$MarineArchive = new MarineArchive($this->db); |
864
|
|
|
$filter_query = $MarineArchive->getFilter($filters,true,true); |
865
|
|
|
$filter_query .= " over_country <> ''"; |
866
|
|
|
if ($olderthanmonths > 0) { |
867
|
|
|
if ($globalDBdriver == 'mysql') { |
868
|
|
|
$filter_query .= ' AND marine_archive.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) '; |
869
|
|
|
} else { |
870
|
|
|
$filter_query .= " AND marine_archive.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
871
|
|
|
} |
872
|
|
|
} |
873
|
|
|
if ($sincedate != '') { |
874
|
|
|
if ($globalDBdriver == 'mysql') { |
875
|
|
|
$filter_query .= " AND marine_archive.date > '".$sincedate."' "; |
876
|
|
|
} else { |
877
|
|
|
$filter_query .= " AND marine_archive.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
878
|
|
|
} |
879
|
|
|
} |
880
|
|
|
$filter_query .= " LIMIT 100 OFFSET 0"; |
881
|
|
|
$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT fammarine_id,over_country FROM marine_archive".$filter_query.") l ON c.iso2 = l.over_country "; |
882
|
|
|
} |
883
|
|
|
$query .= "GROUP BY c.name,c.iso3,c.iso2 ORDER BY nb DESC"; |
884
|
|
|
if ($limit) $query .= " LIMIT 10 OFFSET 0"; |
885
|
|
|
|
886
|
|
|
$sth = $this->db->prepare($query); |
887
|
|
|
$sth->execute(); |
888
|
|
|
|
889
|
|
|
$flight_array = array(); |
890
|
|
|
$temp_array = array(); |
891
|
|
|
|
892
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
893
|
|
|
{ |
894
|
|
|
$temp_array['marine_count'] = $row['nb']; |
895
|
|
|
$temp_array['marine_country'] = $row['name']; |
896
|
|
|
$temp_array['marine_country_iso3'] = $row['iso3']; |
897
|
|
|
$temp_array['marine_country_iso2'] = $row['iso2']; |
898
|
|
|
$flight_array[] = $temp_array; |
899
|
|
|
} |
900
|
|
|
return $flight_array; |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
|
904
|
|
|
|
905
|
|
|
/** |
906
|
|
|
* Gets all callsigns that have flown over |
907
|
|
|
* |
908
|
|
|
* @return Array the callsign list |
909
|
|
|
* |
910
|
|
|
*/ |
911
|
|
|
public function countAllCallsigns($limit = true, $olderthanmonths = 0, $sincedate = '',$filters = array(),$year = '', $month = '', $day = '') |
912
|
|
|
{ |
913
|
|
|
global $globalDBdriver; |
914
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
915
|
|
|
$query = "SELECT DISTINCT marine_output.ident, COUNT(marine_output.ident) AS callsign_icao_count |
916
|
|
|
FROM marine_output".$filter_query." marine_output.ident <> ''"; |
917
|
|
|
if ($olderthanmonths > 0) { |
918
|
|
|
if ($globalDBdriver == 'mysql') $query .= ' AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH)'; |
919
|
|
|
else $query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
920
|
|
|
} |
921
|
|
|
if ($sincedate != '') { |
922
|
|
|
if ($globalDBdriver == 'mysql') $query .= " AND marine_output.date > '".$sincedate."'"; |
923
|
|
|
else $query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
924
|
|
|
} |
925
|
|
|
$query_values = array(); |
926
|
|
|
if ($year != '') { |
927
|
|
|
if ($globalDBdriver == 'mysql') { |
928
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
929
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
930
|
|
|
} else { |
931
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
932
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
933
|
|
|
} |
934
|
|
|
} |
935
|
|
|
if ($month != '') { |
936
|
|
|
if ($globalDBdriver == 'mysql') { |
937
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
938
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
939
|
|
|
} else { |
940
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
941
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
942
|
|
|
} |
943
|
|
|
} |
944
|
|
|
if ($day != '') { |
945
|
|
|
if ($globalDBdriver == 'mysql') { |
946
|
|
|
$query .= " AND DAY(marine_output.date) = :day"; |
947
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
948
|
|
|
} else { |
949
|
|
|
$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day"; |
950
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
951
|
|
|
} |
952
|
|
|
} |
953
|
|
|
$query .= " GROUP BY marine_output.ident ORDER BY callsign_icao_count DESC"; |
954
|
|
|
if ($limit) $query .= " LIMIT 10 OFFSET 0"; |
955
|
|
|
|
956
|
|
|
$sth = $this->db->prepare($query); |
957
|
|
|
$sth->execute($query_values); |
958
|
|
|
|
959
|
|
|
$callsign_array = array(); |
960
|
|
|
$temp_array = array(); |
961
|
|
|
|
962
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
963
|
|
|
{ |
964
|
|
|
$temp_array['callsign_icao'] = $row['ident']; |
965
|
|
|
$temp_array['airline_name'] = $row['airline_name']; |
966
|
|
|
$temp_array['airline_icao'] = $row['airline_icao']; |
967
|
|
|
$temp_array['callsign_icao_count'] = $row['callsign_icao_count']; |
968
|
|
|
|
969
|
|
|
$callsign_array[] = $temp_array; |
970
|
|
|
} |
971
|
|
|
|
972
|
|
|
return $callsign_array; |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
|
976
|
|
|
/** |
977
|
|
|
* Counts all dates |
978
|
|
|
* |
979
|
|
|
* @return Array the date list |
980
|
|
|
* |
981
|
|
|
*/ |
982
|
|
|
public function countAllDates($filters = array()) |
983
|
|
|
{ |
984
|
|
|
global $globalTimezone, $globalDBdriver; |
985
|
|
|
if ($globalTimezone != '') { |
986
|
|
|
date_default_timezone_set($globalTimezone); |
987
|
|
|
$datetime = new DateTime(); |
988
|
|
|
$offset = $datetime->format('P'); |
989
|
|
|
} else $offset = '+00:00'; |
990
|
|
|
|
991
|
|
|
if ($globalDBdriver == 'mysql') { |
992
|
|
|
$query = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count |
993
|
|
|
FROM marine_output"; |
994
|
|
|
$query .= $this->getFilter($filters); |
995
|
|
|
$query .= " GROUP BY date_name |
996
|
|
|
ORDER BY date_count DESC |
997
|
|
|
LIMIT 10 OFFSET 0"; |
998
|
|
|
} else { |
999
|
|
|
$query = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count |
1000
|
|
|
FROM marine_output"; |
1001
|
|
|
$query .= $this->getFilter($filters); |
1002
|
|
|
$query .= " GROUP BY date_name |
1003
|
|
|
ORDER BY date_count DESC |
1004
|
|
|
LIMIT 10 OFFSET 0"; |
1005
|
|
|
} |
1006
|
|
|
|
1007
|
|
|
|
1008
|
|
|
$sth = $this->db->prepare($query); |
1009
|
|
|
$sth->execute(array(':offset' => $offset)); |
1010
|
|
|
|
1011
|
|
|
$date_array = array(); |
1012
|
|
|
$temp_array = array(); |
1013
|
|
|
|
1014
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1015
|
|
|
{ |
1016
|
|
|
$temp_array['date_name'] = $row['date_name']; |
1017
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1018
|
|
|
|
1019
|
|
|
$date_array[] = $temp_array; |
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
return $date_array; |
1023
|
|
|
} |
1024
|
|
|
|
1025
|
|
|
|
1026
|
|
|
/** |
1027
|
|
|
* Counts all dates during the last 7 days |
1028
|
|
|
* |
1029
|
|
|
* @return Array the date list |
1030
|
|
|
* |
1031
|
|
|
*/ |
1032
|
|
|
public function countAllDatesLast7Days($filters = array()) |
1033
|
|
|
{ |
1034
|
|
|
global $globalTimezone, $globalDBdriver; |
1035
|
|
|
if ($globalTimezone != '') { |
1036
|
|
|
date_default_timezone_set($globalTimezone); |
1037
|
|
|
$datetime = new DateTime(); |
1038
|
|
|
$offset = $datetime->format('P'); |
1039
|
|
|
} else $offset = '+00:00'; |
1040
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1041
|
|
|
if ($globalDBdriver == 'mysql') { |
1042
|
|
|
$query = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count |
1043
|
|
|
FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY)"; |
1044
|
|
|
$query .= " GROUP BY date_name |
1045
|
|
|
ORDER BY marine_output.date ASC"; |
1046
|
|
|
$query_data = array(':offset' => $offset); |
1047
|
|
|
} else { |
1048
|
|
|
$query = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count |
1049
|
|
|
FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '7 DAYS'"; |
1050
|
|
|
$query .= " GROUP BY date_name |
1051
|
|
|
ORDER BY date_name ASC"; |
1052
|
|
|
$query_data = array(':offset' => $offset); |
1053
|
|
|
} |
1054
|
|
|
|
1055
|
|
|
$sth = $this->db->prepare($query); |
1056
|
|
|
$sth->execute($query_data); |
1057
|
|
|
|
1058
|
|
|
$date_array = array(); |
1059
|
|
|
$temp_array = array(); |
1060
|
|
|
|
1061
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1062
|
|
|
{ |
1063
|
|
|
$temp_array['date_name'] = $row['date_name']; |
1064
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1065
|
|
|
|
1066
|
|
|
$date_array[] = $temp_array; |
1067
|
|
|
} |
1068
|
|
|
|
1069
|
|
|
return $date_array; |
1070
|
|
|
} |
1071
|
|
|
|
1072
|
|
|
/** |
1073
|
|
|
* Counts all dates during the last month |
1074
|
|
|
* |
1075
|
|
|
* @return Array the date list |
1076
|
|
|
* |
1077
|
|
|
*/ |
1078
|
|
|
public function countAllDatesLastMonth($filters = array()) |
1079
|
|
|
{ |
1080
|
|
|
global $globalTimezone, $globalDBdriver; |
1081
|
|
|
if ($globalTimezone != '') { |
1082
|
|
|
date_default_timezone_set($globalTimezone); |
1083
|
|
|
$datetime = new DateTime(); |
1084
|
|
|
$offset = $datetime->format('P'); |
1085
|
|
|
} else $offset = '+00:00'; |
1086
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1087
|
|
|
if ($globalDBdriver == 'mysql') { |
1088
|
|
|
$query = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count |
1089
|
|
|
FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MONTH)"; |
1090
|
|
|
$query .= " GROUP BY date_name |
1091
|
|
|
ORDER BY marine_output.date ASC"; |
1092
|
|
|
$query_data = array(':offset' => $offset); |
1093
|
|
|
} else { |
1094
|
|
|
$query = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count |
1095
|
|
|
FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 MONTHS'"; |
1096
|
|
|
$query .= " GROUP BY date_name |
1097
|
|
|
ORDER BY date_name ASC"; |
1098
|
|
|
$query_data = array(':offset' => $offset); |
1099
|
|
|
} |
1100
|
|
|
|
1101
|
|
|
$sth = $this->db->prepare($query); |
1102
|
|
|
$sth->execute($query_data); |
1103
|
|
|
|
1104
|
|
|
$date_array = array(); |
1105
|
|
|
$temp_array = array(); |
1106
|
|
|
|
1107
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1108
|
|
|
{ |
1109
|
|
|
$temp_array['date_name'] = $row['date_name']; |
1110
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1111
|
|
|
|
1112
|
|
|
$date_array[] = $temp_array; |
1113
|
|
|
} |
1114
|
|
|
|
1115
|
|
|
return $date_array; |
1116
|
|
|
} |
1117
|
|
|
|
1118
|
|
|
|
1119
|
|
|
|
1120
|
|
|
/** |
1121
|
|
|
* Counts all month |
1122
|
|
|
* |
1123
|
|
|
* @return Array the month list |
1124
|
|
|
* |
1125
|
|
|
*/ |
1126
|
|
|
public function countAllMonths($filters = array()) |
1127
|
|
|
{ |
1128
|
|
|
global $globalTimezone, $globalDBdriver; |
1129
|
|
|
if ($globalTimezone != '') { |
1130
|
|
|
date_default_timezone_set($globalTimezone); |
1131
|
|
|
$datetime = new DateTime(); |
1132
|
|
|
$offset = $datetime->format('P'); |
1133
|
|
|
} else $offset = '+00:00'; |
1134
|
|
|
|
1135
|
|
|
if ($globalDBdriver == 'mysql') { |
1136
|
|
|
$query = "SELECT YEAR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS year_name,MONTH(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS month_name, count(*) as date_count |
1137
|
|
|
FROM marine_output"; |
1138
|
|
|
$query .= $this->getFilter($filters); |
1139
|
|
|
$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC"; |
1140
|
|
|
} else { |
1141
|
|
|
$query = "SELECT EXTRACT(YEAR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS year_name,EXTRACT(MONTH FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS month_name, count(*) as date_count |
1142
|
|
|
FROM marine_output"; |
1143
|
|
|
$query .= $this->getFilter($filters); |
1144
|
|
|
$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC"; |
1145
|
|
|
} |
1146
|
|
|
|
1147
|
|
|
|
1148
|
|
|
$sth = $this->db->prepare($query); |
1149
|
|
|
$sth->execute(array(':offset' => $offset)); |
1150
|
|
|
|
1151
|
|
|
$date_array = array(); |
1152
|
|
|
$temp_array = array(); |
1153
|
|
|
|
1154
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1155
|
|
|
{ |
1156
|
|
|
$temp_array['month_name'] = $row['month_name']; |
1157
|
|
|
$temp_array['year_name'] = $row['year_name']; |
1158
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1159
|
|
|
|
1160
|
|
|
$date_array[] = $temp_array; |
1161
|
|
|
} |
1162
|
|
|
|
1163
|
|
|
return $date_array; |
1164
|
|
|
} |
1165
|
|
|
|
1166
|
|
|
|
1167
|
|
|
|
1168
|
|
|
|
1169
|
|
|
/** |
1170
|
|
|
* Counts all dates during the last year |
1171
|
|
|
* |
1172
|
|
|
* @return Array the date list |
1173
|
|
|
* |
1174
|
|
|
*/ |
1175
|
|
|
public function countAllMonthsLastYear($filters) |
1176
|
|
|
{ |
1177
|
|
|
global $globalTimezone, $globalDBdriver; |
1178
|
|
|
if ($globalTimezone != '') { |
1179
|
|
|
date_default_timezone_set($globalTimezone); |
1180
|
|
|
$datetime = new DateTime(); |
1181
|
|
|
$offset = $datetime->format('P'); |
1182
|
|
|
} else $offset = '+00:00'; |
1183
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1184
|
|
|
if ($globalDBdriver == 'mysql') { |
1185
|
|
|
$query = "SELECT MONTH(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS month_name, YEAR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS year_name, count(*) as date_count |
1186
|
|
|
FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)"; |
1187
|
|
|
$query .= " GROUP BY year_name, month_name |
1188
|
|
|
ORDER BY year_name, month_name ASC"; |
1189
|
|
|
$query_data = array(':offset' => $offset); |
1190
|
|
|
} else { |
1191
|
|
|
$query = "SELECT EXTRACT(MONTH FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS month_name, EXTRACT(YEAR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS year_name, count(*) as date_count |
1192
|
|
|
FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'"; |
1193
|
|
|
$query .= " GROUP BY year_name, month_name |
1194
|
|
|
ORDER BY year_name, month_name ASC"; |
1195
|
|
|
$query_data = array(':offset' => $offset); |
1196
|
|
|
} |
1197
|
|
|
|
1198
|
|
|
$sth = $this->db->prepare($query); |
1199
|
|
|
$sth->execute($query_data); |
1200
|
|
|
|
1201
|
|
|
$date_array = array(); |
1202
|
|
|
$temp_array = array(); |
1203
|
|
|
|
1204
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1205
|
|
|
{ |
1206
|
|
|
$temp_array['year_name'] = $row['year_name']; |
1207
|
|
|
$temp_array['month_name'] = $row['month_name']; |
1208
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1209
|
|
|
|
1210
|
|
|
$date_array[] = $temp_array; |
1211
|
|
|
} |
1212
|
|
|
|
1213
|
|
|
return $date_array; |
1214
|
|
|
} |
1215
|
|
|
|
1216
|
|
|
|
1217
|
|
|
|
1218
|
|
|
/** |
1219
|
|
|
* Counts all hours |
1220
|
|
|
* |
1221
|
|
|
* @return Array the hour list |
1222
|
|
|
* |
1223
|
|
|
*/ |
1224
|
|
|
public function countAllHours($orderby,$filters = array()) |
1225
|
|
|
{ |
1226
|
|
|
global $globalTimezone, $globalDBdriver; |
1227
|
|
|
if ($globalTimezone != '') { |
1228
|
|
|
date_default_timezone_set($globalTimezone); |
1229
|
|
|
$datetime = new DateTime(); |
1230
|
|
|
$offset = $datetime->format('P'); |
1231
|
|
|
} else $offset = '+00:00'; |
1232
|
|
|
|
1233
|
|
|
$orderby_sql = ''; |
1234
|
|
|
if ($orderby == "hour") |
1235
|
|
|
{ |
1236
|
|
|
$orderby_sql = "ORDER BY hour_name ASC"; |
1237
|
|
|
} |
1238
|
|
|
if ($orderby == "count") |
1239
|
|
|
{ |
1240
|
|
|
$orderby_sql = "ORDER BY hour_count DESC"; |
1241
|
|
|
} |
1242
|
|
|
|
1243
|
|
|
if ($globalDBdriver == 'mysql') { |
1244
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1245
|
|
|
FROM marine_output"; |
1246
|
|
|
$query .= $this->getFilter($filters); |
1247
|
|
|
$query .= " GROUP BY hour_name |
1248
|
|
|
".$orderby_sql; |
1249
|
|
|
|
1250
|
|
|
/* $query = "SELECT HOUR(marine_output.date) AS hour_name, count(*) as hour_count |
1251
|
|
|
FROM marine_output |
1252
|
|
|
GROUP BY hour_name |
1253
|
|
|
".$orderby_sql." |
1254
|
|
|
LIMIT 10 OFFSET 00"; |
1255
|
|
|
*/ |
1256
|
|
|
$query_data = array(':offset' => $offset); |
1257
|
|
|
} else { |
1258
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1259
|
|
|
FROM marine_output"; |
1260
|
|
|
$query .= $this->getFilter($filters); |
1261
|
|
|
$query .= " GROUP BY hour_name |
1262
|
|
|
".$orderby_sql; |
1263
|
|
|
$query_data = array(':offset' => $offset); |
1264
|
|
|
} |
1265
|
|
|
|
1266
|
|
|
$sth = $this->db->prepare($query); |
1267
|
|
|
$sth->execute($query_data); |
1268
|
|
|
|
1269
|
|
|
$hour_array = array(); |
1270
|
|
|
$temp_array = array(); |
1271
|
|
|
|
1272
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1273
|
|
|
{ |
1274
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1275
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1276
|
|
|
|
1277
|
|
|
$hour_array[] = $temp_array; |
1278
|
|
|
} |
1279
|
|
|
|
1280
|
|
|
return $hour_array; |
1281
|
|
|
} |
1282
|
|
|
|
1283
|
|
|
|
1284
|
|
|
|
1285
|
|
|
/** |
1286
|
|
|
* Counts all hours by date |
1287
|
|
|
* |
1288
|
|
|
* @return Array the hour list |
1289
|
|
|
* |
1290
|
|
|
*/ |
1291
|
|
|
public function countAllHoursByDate($date, $filters = array()) |
1292
|
|
|
{ |
1293
|
|
|
global $globalTimezone, $globalDBdriver; |
1294
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1295
|
|
|
$date = filter_var($date,FILTER_SANITIZE_STRING); |
1296
|
|
|
if ($globalTimezone != '') { |
1297
|
|
|
date_default_timezone_set($globalTimezone); |
1298
|
|
|
$datetime = new DateTime($date); |
1299
|
|
|
$offset = $datetime->format('P'); |
1300
|
|
|
} else $offset = '+00:00'; |
1301
|
|
|
|
1302
|
|
|
if ($globalDBdriver == 'mysql') { |
1303
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1304
|
|
|
FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date |
1305
|
|
|
GROUP BY hour_name |
1306
|
|
|
ORDER BY hour_name ASC"; |
1307
|
|
|
} else { |
1308
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1309
|
|
|
FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset, 'YYYY-mm-dd') = :date |
1310
|
|
|
GROUP BY hour_name |
1311
|
|
|
ORDER BY hour_name ASC"; |
1312
|
|
|
} |
1313
|
|
|
|
1314
|
|
|
$sth = $this->db->prepare($query); |
1315
|
|
|
$sth->execute(array(':date' => $date, ':offset' => $offset)); |
1316
|
|
|
|
1317
|
|
|
$hour_array = array(); |
1318
|
|
|
$temp_array = array(); |
1319
|
|
|
|
1320
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1321
|
|
|
{ |
1322
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1323
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1324
|
|
|
|
1325
|
|
|
$hour_array[] = $temp_array; |
1326
|
|
|
} |
1327
|
|
|
|
1328
|
|
|
return $hour_array; |
1329
|
|
|
} |
1330
|
|
|
|
1331
|
|
|
|
1332
|
|
|
|
1333
|
|
|
/** |
1334
|
|
|
* Counts all hours by a ident/callsign |
1335
|
|
|
* |
1336
|
|
|
* @return Array the hour list |
1337
|
|
|
* |
1338
|
|
|
*/ |
1339
|
|
|
public function countAllHoursByIdent($ident, $filters = array()) |
1340
|
|
|
{ |
1341
|
|
|
global $globalTimezone, $globalDBdriver; |
1342
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1343
|
|
|
$ident = filter_var($ident,FILTER_SANITIZE_STRING); |
1344
|
|
|
if ($globalTimezone != '') { |
1345
|
|
|
date_default_timezone_set($globalTimezone); |
1346
|
|
|
$datetime = new DateTime(); |
1347
|
|
|
$offset = $datetime->format('P'); |
1348
|
|
|
} else $offset = '+00:00'; |
1349
|
|
|
|
1350
|
|
|
if ($globalDBdriver == 'mysql') { |
1351
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1352
|
|
|
FROM marine_output".$filter_query." marine_output.ident = :ident |
1353
|
|
|
GROUP BY hour_name |
1354
|
|
|
ORDER BY hour_name ASC"; |
1355
|
|
|
} else { |
1356
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1357
|
|
|
FROM marine_output".$filter_query." marine_output.ident = :ident |
1358
|
|
|
GROUP BY hour_name |
1359
|
|
|
ORDER BY hour_name ASC"; |
1360
|
|
|
} |
1361
|
|
|
|
1362
|
|
|
|
1363
|
|
|
$sth = $this->db->prepare($query); |
1364
|
|
|
$sth->execute(array(':ident' => $ident,':offset' => $offset)); |
1365
|
|
|
|
1366
|
|
|
$hour_array = array(); |
1367
|
|
|
$temp_array = array(); |
1368
|
|
|
|
1369
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1370
|
|
|
{ |
1371
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1372
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1373
|
|
|
|
1374
|
|
|
$hour_array[] = $temp_array; |
1375
|
|
|
} |
1376
|
|
|
|
1377
|
|
|
return $hour_array; |
1378
|
|
|
} |
1379
|
|
|
|
1380
|
|
|
|
1381
|
|
|
|
1382
|
|
|
/** |
1383
|
|
|
* Counts all vessels |
1384
|
|
|
* |
1385
|
|
|
* @return Integer the number of vessels |
1386
|
|
|
* |
1387
|
|
|
*/ |
1388
|
|
|
public function countOverallMarine($filters = array(),$year = '',$month = '') |
1389
|
|
|
{ |
1390
|
|
|
global $globalDBdriver; |
1391
|
|
|
//$queryi = "SELECT COUNT(marine_output.marine_id) AS flight_count FROM marine_output"; |
1392
|
|
|
$queryi = "SELECT COUNT(DISTINCT marine_output.mmsi) AS flight_count FROM marine_output"; |
1393
|
|
|
$query_values = array(); |
1394
|
|
|
$query = ''; |
1395
|
|
|
if ($year != '') { |
1396
|
|
|
if ($globalDBdriver == 'mysql') { |
1397
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
1398
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1399
|
|
|
} else { |
1400
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
1401
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1402
|
|
|
} |
1403
|
|
|
} |
1404
|
|
|
if ($month != '') { |
1405
|
|
|
if ($globalDBdriver == 'mysql') { |
1406
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
1407
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1408
|
|
|
} else { |
1409
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
1410
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1411
|
|
|
} |
1412
|
|
|
} |
1413
|
|
|
if (empty($query_values)) $queryi .= $this->getFilter($filters); |
1414
|
|
|
else $queryi .= $this->getFilter($filters,true,true).substr($query,4); |
1415
|
|
|
|
1416
|
|
|
$sth = $this->db->prepare($queryi); |
1417
|
|
|
$sth->execute($query_values); |
1418
|
|
|
return $sth->fetchColumn(); |
1419
|
|
|
} |
1420
|
|
|
|
1421
|
|
|
/** |
1422
|
|
|
* Counts all vessel type |
1423
|
|
|
* |
1424
|
|
|
* @return Integer the number of vessels |
1425
|
|
|
* |
1426
|
|
|
*/ |
1427
|
|
|
public function countOverallMarineTypes($filters = array(),$year = '',$month = '') |
1428
|
|
|
{ |
1429
|
|
|
global $globalDBdriver; |
1430
|
|
|
$queryi = "SELECT COUNT(DISTINCT marine_output.type) AS marine_count FROM marine_output"; |
1431
|
|
|
$query_values = array(); |
1432
|
|
|
$query = ''; |
1433
|
|
|
if ($year != '') { |
1434
|
|
|
if ($globalDBdriver == 'mysql') { |
1435
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
1436
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1437
|
|
|
} else { |
1438
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
1439
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1440
|
|
|
} |
1441
|
|
|
} |
1442
|
|
|
if ($month != '') { |
1443
|
|
|
if ($globalDBdriver == 'mysql') { |
1444
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
1445
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1446
|
|
|
} else { |
1447
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
1448
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1449
|
|
|
} |
1450
|
|
|
} |
1451
|
|
|
if (empty($query_values)) $queryi .= $this->getFilter($filters); |
1452
|
|
|
else $queryi .= $this->getFilter($filters,true,true).substr($query,4); |
1453
|
|
|
|
1454
|
|
|
$sth = $this->db->prepare($queryi); |
1455
|
|
|
$sth->execute($query_values); |
1456
|
|
|
return $sth->fetchColumn(); |
1457
|
|
|
} |
1458
|
|
|
|
1459
|
|
|
|
1460
|
|
|
/** |
1461
|
|
|
* Counts all hours of today |
1462
|
|
|
* |
1463
|
|
|
* @return Array the hour list |
1464
|
|
|
* |
1465
|
|
|
*/ |
1466
|
|
|
public function countAllHoursFromToday($filters = array()) |
1467
|
|
|
{ |
1468
|
|
|
global $globalTimezone, $globalDBdriver; |
1469
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1470
|
|
|
if ($globalTimezone != '') { |
1471
|
|
|
date_default_timezone_set($globalTimezone); |
1472
|
|
|
$datetime = new DateTime(); |
1473
|
|
|
$offset = $datetime->format('P'); |
1474
|
|
|
} else $offset = '+00:00'; |
1475
|
|
|
|
1476
|
|
|
if ($globalDBdriver == 'mysql') { |
1477
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1478
|
|
|
FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = CURDATE() |
1479
|
|
|
GROUP BY hour_name |
1480
|
|
|
ORDER BY hour_name ASC"; |
1481
|
|
|
} else { |
1482
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1483
|
|
|
FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') = CAST(NOW() AS date) |
1484
|
|
|
GROUP BY hour_name |
1485
|
|
|
ORDER BY hour_name ASC"; |
1486
|
|
|
} |
1487
|
|
|
|
1488
|
|
|
$sth = $this->db->prepare($query); |
1489
|
|
|
$sth->execute(array(':offset' => $offset)); |
1490
|
|
|
|
1491
|
|
|
$hour_array = array(); |
1492
|
|
|
$temp_array = array(); |
1493
|
|
|
|
1494
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1495
|
|
|
{ |
1496
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1497
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1498
|
|
|
$hour_array[] = $temp_array; |
1499
|
|
|
} |
1500
|
|
|
|
1501
|
|
|
return $hour_array; |
1502
|
|
|
} |
1503
|
|
|
|
1504
|
|
|
|
1505
|
|
|
/** |
1506
|
|
|
* Gets the Barrie Spotter ID based on the FlightAware ID |
1507
|
|
|
* |
1508
|
|
|
* @return Integer the Barrie Spotter ID |
1509
|
|
|
q * |
1510
|
|
|
*/ |
1511
|
|
|
public function getMarineIDBasedOnFamMarineID($fammarine_id) |
1512
|
|
|
{ |
1513
|
|
|
$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING); |
1514
|
|
|
|
1515
|
|
|
$query = "SELECT marine_output.marine_id |
1516
|
|
|
FROM marine_output |
1517
|
|
|
WHERE marine_output.fammarine_id = '".$fammarine_id."'"; |
1518
|
|
|
|
1519
|
|
|
|
1520
|
|
|
$sth = $this->db->prepare($query); |
1521
|
|
|
$sth->execute(); |
1522
|
|
|
|
1523
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1524
|
|
|
{ |
1525
|
|
|
return $row['marine_id']; |
1526
|
|
|
} |
1527
|
|
|
} |
1528
|
|
|
|
1529
|
|
|
|
1530
|
|
|
/** |
1531
|
|
|
* Parses a date string |
1532
|
|
|
* |
1533
|
|
|
* @param String $dateString the date string |
1534
|
|
|
* @param String $timezone the timezone of a user |
1535
|
|
|
* @return Array the time information |
1536
|
|
|
* |
1537
|
|
|
*/ |
1538
|
|
|
public function parseDateString($dateString, $timezone = '') |
1539
|
|
|
{ |
1540
|
|
|
$time_array = array(); |
1541
|
|
|
|
1542
|
|
|
if ($timezone != "") |
1543
|
|
|
{ |
1544
|
|
|
date_default_timezone_set($timezone); |
1545
|
|
|
} |
1546
|
|
|
|
1547
|
|
|
$current_date = date("Y-m-d H:i:s"); |
1548
|
|
|
$date = date("Y-m-d H:i:s",strtotime($dateString." UTC")); |
1549
|
|
|
|
1550
|
|
|
$diff = abs(strtotime($current_date) - strtotime($date)); |
1551
|
|
|
|
1552
|
|
|
$time_array['years'] = floor($diff / (365*60*60*24)); |
1553
|
|
|
$years = $time_array['years']; |
1554
|
|
|
|
1555
|
|
|
$time_array['months'] = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); |
1556
|
|
|
$months = $time_array['months']; |
1557
|
|
|
|
1558
|
|
|
$time_array['days'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); |
1559
|
|
|
$days = $time_array['days']; |
1560
|
|
|
$time_array['hours'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); |
1561
|
|
|
$hours = $time_array['hours']; |
1562
|
|
|
$time_array['minutes'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); |
1563
|
|
|
$minutes = $time_array['minutes']; |
1564
|
|
|
$time_array['seconds'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60)); |
1565
|
|
|
|
1566
|
|
|
return $time_array; |
1567
|
|
|
} |
1568
|
|
|
|
1569
|
|
|
/** |
1570
|
|
|
* Parses the direction degrees to working |
1571
|
|
|
* |
1572
|
|
|
* @param Float $direction the direction in degrees |
1573
|
|
|
* @return Array the direction information |
1574
|
|
|
* |
1575
|
|
|
*/ |
1576
|
|
|
public function parseDirection($direction = 0) |
1577
|
|
|
{ |
1578
|
|
|
if ($direction == '') $direction = 0; |
1579
|
|
|
$direction_array = array(); |
1580
|
|
|
$temp_array = array(); |
1581
|
|
|
|
1582
|
|
|
if ($direction == 360 || ($direction >= 0 && $direction < 22.5)) |
1583
|
|
|
{ |
1584
|
|
|
$temp_array['direction_degree'] = $direction; |
1585
|
|
|
$temp_array['direction_shortname'] = "N"; |
1586
|
|
|
$temp_array['direction_fullname'] = "North"; |
1587
|
|
|
} elseif ($direction >= 22.5 && $direction < 45){ |
1588
|
|
|
$temp_array['direction_degree'] = $direction; |
1589
|
|
|
$temp_array['direction_shortname'] = "NNE"; |
1590
|
|
|
$temp_array['direction_fullname'] = "North-Northeast"; |
1591
|
|
|
} elseif ($direction >= 45 && $direction < 67.5){ |
1592
|
|
|
$temp_array['direction_degree'] = $direction; |
1593
|
|
|
$temp_array['direction_shortname'] = "NE"; |
1594
|
|
|
$temp_array['direction_fullname'] = "Northeast"; |
1595
|
|
|
} elseif ($direction >= 67.5 && $direction < 90){ |
1596
|
|
|
$temp_array['direction_degree'] = $direction; |
1597
|
|
|
$temp_array['direction_shortname'] = "ENE"; |
1598
|
|
|
$temp_array['direction_fullname'] = "East-Northeast"; |
1599
|
|
|
} elseif ($direction >= 90 && $direction < 112.5){ |
1600
|
|
|
$temp_array['direction_degree'] = $direction; |
1601
|
|
|
$temp_array['direction_shortname'] = "E"; |
1602
|
|
|
$temp_array['direction_fullname'] = "East"; |
1603
|
|
|
} elseif ($direction >= 112.5 && $direction < 135){ |
1604
|
|
|
$temp_array['direction_degree'] = $direction; |
1605
|
|
|
$temp_array['direction_shortname'] = "ESE"; |
1606
|
|
|
$temp_array['direction_fullname'] = "East-Southeast"; |
1607
|
|
|
} elseif ($direction >= 135 && $direction < 157.5){ |
1608
|
|
|
$temp_array['direction_degree'] = $direction; |
1609
|
|
|
$temp_array['direction_shortname'] = "SE"; |
1610
|
|
|
$temp_array['direction_fullname'] = "Southeast"; |
1611
|
|
|
} elseif ($direction >= 157.5 && $direction < 180){ |
1612
|
|
|
$temp_array['direction_degree'] = $direction; |
1613
|
|
|
$temp_array['direction_shortname'] = "SSE"; |
1614
|
|
|
$temp_array['direction_fullname'] = "South-Southeast"; |
1615
|
|
|
} elseif ($direction >= 180 && $direction < 202.5){ |
1616
|
|
|
$temp_array['direction_degree'] = $direction; |
1617
|
|
|
$temp_array['direction_shortname'] = "S"; |
1618
|
|
|
$temp_array['direction_fullname'] = "South"; |
1619
|
|
|
} elseif ($direction >= 202.5 && $direction < 225){ |
1620
|
|
|
$temp_array['direction_degree'] = $direction; |
1621
|
|
|
$temp_array['direction_shortname'] = "SSW"; |
1622
|
|
|
$temp_array['direction_fullname'] = "South-Southwest"; |
1623
|
|
|
} elseif ($direction >= 225 && $direction < 247.5){ |
1624
|
|
|
$temp_array['direction_degree'] = $direction; |
1625
|
|
|
$temp_array['direction_shortname'] = "SW"; |
1626
|
|
|
$temp_array['direction_fullname'] = "Southwest"; |
1627
|
|
|
} elseif ($direction >= 247.5 && $direction < 270){ |
1628
|
|
|
$temp_array['direction_degree'] = $direction; |
1629
|
|
|
$temp_array['direction_shortname'] = "WSW"; |
1630
|
|
|
$temp_array['direction_fullname'] = "West-Southwest"; |
1631
|
|
|
} elseif ($direction >= 270 && $direction < 292.5){ |
1632
|
|
|
$temp_array['direction_degree'] = $direction; |
1633
|
|
|
$temp_array['direction_shortname'] = "W"; |
1634
|
|
|
$temp_array['direction_fullname'] = "West"; |
1635
|
|
|
} elseif ($direction >= 292.5 && $direction < 315){ |
1636
|
|
|
$temp_array['direction_degree'] = $direction; |
1637
|
|
|
$temp_array['direction_shortname'] = "WNW"; |
1638
|
|
|
$temp_array['direction_fullname'] = "West-Northwest"; |
1639
|
|
|
} elseif ($direction >= 315 && $direction < 337.5){ |
1640
|
|
|
$temp_array['direction_degree'] = $direction; |
1641
|
|
|
$temp_array['direction_shortname'] = "NW"; |
1642
|
|
|
$temp_array['direction_fullname'] = "Northwest"; |
1643
|
|
|
} elseif ($direction >= 337.5 && $direction < 360){ |
1644
|
|
|
$temp_array['direction_degree'] = $direction; |
1645
|
|
|
$temp_array['direction_shortname'] = "NNW"; |
1646
|
|
|
$temp_array['direction_fullname'] = "North-Northwest"; |
1647
|
|
|
} |
1648
|
|
|
$direction_array[] = $temp_array; |
1649
|
|
|
return $direction_array; |
1650
|
|
|
} |
1651
|
|
|
|
1652
|
|
|
|
1653
|
|
|
/** |
1654
|
|
|
* Gets Country from latitude/longitude |
1655
|
|
|
* |
1656
|
|
|
* @param Float $latitude latitute of the flight |
1657
|
|
|
* @param Float $longitude longitute of the flight |
1658
|
|
|
* @return String the countrie |
1659
|
|
|
*/ |
1660
|
|
|
public function getCountryFromLatitudeLongitude($latitude,$longitude) |
1661
|
|
|
{ |
1662
|
|
|
global $globalDBdriver, $globalDebug; |
1663
|
|
|
$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
1664
|
|
|
$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
1665
|
|
|
|
1666
|
|
|
$Connection = new Connection($this->db); |
1667
|
|
|
if (!$Connection->tableExists('countries')) return ''; |
1668
|
|
|
|
1669
|
|
|
try { |
1670
|
|
|
/* |
1671
|
|
|
if ($globalDBdriver == 'mysql') { |
1672
|
|
|
//$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(:latitude :longitude)'), ogc_geom) LIMIT 1"; |
1673
|
|
|
$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(".$longitude.' '.$latitude.")'), ogc_geom) LIMIT 1"; |
1674
|
|
|
} |
1675
|
|
|
*/ |
1676
|
|
|
// This query seems to work both for MariaDB and PostgreSQL |
1677
|
|
|
$query = "SELECT name,iso2,iso3 FROM countries WHERE ST_Within(ST_GeomFromText('POINT(".$longitude." ".$latitude.")',4326), ogc_geom) LIMIT 1"; |
1678
|
|
|
|
1679
|
|
|
$sth = $this->db->prepare($query); |
1680
|
|
|
//$sth->execute(array(':latitude' => $latitude,':longitude' => $longitude)); |
1681
|
|
|
$sth->execute(); |
1682
|
|
|
|
1683
|
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC); |
1684
|
|
|
$sth->closeCursor(); |
1685
|
|
|
if (count($row) > 0) { |
1686
|
|
|
return $row; |
1687
|
|
|
} else return ''; |
1688
|
|
|
} catch (PDOException $e) { |
1689
|
|
|
if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n"; |
1690
|
|
|
return ''; |
1691
|
|
|
} |
1692
|
|
|
|
1693
|
|
|
} |
1694
|
|
|
|
1695
|
|
|
/** |
1696
|
|
|
* Gets Country from iso2 |
1697
|
|
|
* |
1698
|
|
|
* @param String $iso2 ISO2 country code |
1699
|
|
|
* @return String the countrie |
1700
|
|
|
*/ |
1701
|
|
|
public function getCountryFromISO2($iso2) |
1702
|
|
|
{ |
1703
|
|
|
global $globalDBdriver, $globalDebug; |
1704
|
|
|
$iso2 = filter_var($iso2,FILTER_SANITIZE_STRING); |
1705
|
|
|
|
1706
|
|
|
$Connection = new Connection($this->db); |
1707
|
|
|
if (!$Connection->tableExists('countries')) return ''; |
1708
|
|
|
|
1709
|
|
|
try { |
1710
|
|
|
$query = "SELECT name,iso2,iso3 FROM countries WHERE iso2 = :iso2 LIMIT 1"; |
1711
|
|
|
|
1712
|
|
|
$sth = $this->db->prepare($query); |
1713
|
|
|
$sth->execute(array(':iso2' => $iso2)); |
1714
|
|
|
|
1715
|
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC); |
1716
|
|
|
$sth->closeCursor(); |
1717
|
|
|
if (count($row) > 0) { |
1718
|
|
|
return $row; |
1719
|
|
|
} else return ''; |
1720
|
|
|
} catch (PDOException $e) { |
1721
|
|
|
if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n"; |
1722
|
|
|
return ''; |
1723
|
|
|
} |
1724
|
|
|
|
1725
|
|
|
} |
1726
|
|
|
|
1727
|
|
|
|
1728
|
|
|
/** |
1729
|
|
|
* Gets the short url from bit.ly |
1730
|
|
|
* |
1731
|
|
|
* @param String $url the full url |
1732
|
|
|
* @return String the bit.ly url |
1733
|
|
|
* |
1734
|
|
|
*/ |
1735
|
|
|
public function getBitlyURL($url) |
1736
|
|
|
{ |
1737
|
|
|
global $globalBitlyAccessToken; |
1738
|
|
|
|
1739
|
|
|
if ($globalBitlyAccessToken == '') return $url; |
1740
|
|
|
|
1741
|
|
|
$google_url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$globalBitlyAccessToken.'&longUrl='.$url; |
1742
|
|
|
|
1743
|
|
|
$ch = curl_init(); |
1744
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
1745
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
1746
|
|
|
curl_setopt($ch, CURLOPT_URL, $google_url); |
1747
|
|
|
$bitly_data = curl_exec($ch); |
1748
|
|
|
curl_close($ch); |
1749
|
|
|
|
1750
|
|
|
$bitly_data = json_decode($bitly_data); |
1751
|
|
|
$bitly_url = ''; |
1752
|
|
|
if ($bitly_data->status_txt = "OK"){ |
1753
|
|
|
$bitly_url = $bitly_data->data->url; |
1754
|
|
|
} |
1755
|
|
|
|
1756
|
|
|
return $bitly_url; |
1757
|
|
|
} |
1758
|
|
|
|
1759
|
|
|
|
1760
|
|
|
/** |
1761
|
|
|
* Gets all vessels types that have flown over |
1762
|
|
|
* |
1763
|
|
|
* @return Array the vessel type list |
1764
|
|
|
* |
1765
|
|
|
*/ |
1766
|
|
|
public function countAllMarineTypes($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array(),$year = '',$month = '',$day = '') |
1767
|
|
|
{ |
1768
|
|
|
global $globalDBdriver; |
1769
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1770
|
|
|
$query = "SELECT marine_output.type AS marine_type, COUNT(marine_output.type) AS marine_type_count |
1771
|
|
|
FROM marine_output ".$filter_query." marine_output.type <> ''"; |
1772
|
|
|
if ($olderthanmonths > 0) { |
1773
|
|
|
if ($globalDBdriver == 'mysql') { |
1774
|
|
|
$query .= ' AND marine_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$olderthanmonths.' MONTH)'; |
1775
|
|
|
} else { |
1776
|
|
|
$query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
1777
|
|
|
} |
1778
|
|
|
} |
1779
|
|
|
if ($sincedate != '') { |
1780
|
|
|
if ($globalDBdriver == 'mysql') { |
1781
|
|
|
$query .= " AND marine_output.date > '".$sincedate."'"; |
1782
|
|
|
} else { |
1783
|
|
|
$query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
1784
|
|
|
} |
1785
|
|
|
} |
1786
|
|
|
$query_values = array(); |
1787
|
|
|
if ($year != '') { |
1788
|
|
|
if ($globalDBdriver == 'mysql') { |
1789
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
1790
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1791
|
|
|
} else { |
1792
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
1793
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1794
|
|
|
} |
1795
|
|
|
} |
1796
|
|
|
if ($month != '') { |
1797
|
|
|
if ($globalDBdriver == 'mysql') { |
1798
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
1799
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1800
|
|
|
} else { |
1801
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
1802
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1803
|
|
|
} |
1804
|
|
|
} |
1805
|
|
|
if ($day != '') { |
1806
|
|
|
if ($globalDBdriver == 'mysql') { |
1807
|
|
|
$query .= " AND DAY(marine_output.date) = :day"; |
1808
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
1809
|
|
|
} else { |
1810
|
|
|
$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day"; |
1811
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
1812
|
|
|
} |
1813
|
|
|
} |
1814
|
|
|
$query .= " GROUP BY marine_output.type ORDER BY marine_type_count DESC"; |
1815
|
|
|
if ($limit) $query .= " LIMIT 10 OFFSET 0"; |
1816
|
|
|
$sth = $this->db->prepare($query); |
1817
|
|
|
$sth->execute($query_values); |
1818
|
|
|
$marine_array = array(); |
1819
|
|
|
$temp_array = array(); |
1820
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1821
|
|
|
{ |
1822
|
|
|
$temp_array['marine_type'] = $row['marine_type']; |
1823
|
|
|
$temp_array['marine_type_count'] = $row['marine_type_count']; |
1824
|
|
|
$marine_array[] = $temp_array; |
1825
|
|
|
} |
1826
|
|
|
return $marine_array; |
1827
|
|
|
} |
1828
|
|
|
|
1829
|
|
|
/** |
1830
|
|
|
* Gets all the tracker information |
1831
|
|
|
* |
1832
|
|
|
* @return Array the tracker information |
1833
|
|
|
* |
1834
|
|
|
*/ |
1835
|
|
|
public function searchMarineData($q = '', $callsign = '',$mmsi = '', $imo = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '',$filters = array()) |
|
|
|
|
1836
|
|
|
{ |
1837
|
|
|
global $globalTimezone, $globalDBdriver; |
1838
|
|
|
date_default_timezone_set('UTC'); |
1839
|
|
|
$query_values = array(); |
1840
|
|
|
$additional_query = ''; |
1841
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1842
|
|
|
if ($q != "") |
1843
|
|
|
{ |
1844
|
|
|
if (!is_string($q)) |
1845
|
|
|
{ |
1846
|
|
|
return false; |
1847
|
|
|
} else { |
1848
|
|
|
$q_array = explode(" ", $q); |
1849
|
|
|
foreach ($q_array as $q_item){ |
1850
|
|
|
$q_item = filter_var($q_item,FILTER_SANITIZE_STRING); |
1851
|
|
|
$additional_query .= " AND ("; |
1852
|
|
|
if (is_int($q_item)) $additional_query .= "(marine_output.marine_id = '".$q_item."') OR "; |
1853
|
|
|
if (is_int($q_item)) $additional_query .= "(marine_output.mmsi = '".$q_item."') OR "; |
1854
|
|
|
if (is_int($q_item)) $additional_query .= "(marine_output.imo = '".$q_item."') OR "; |
1855
|
|
|
$additional_query .= "(marine_output.ident like '%".$q_item."%') OR "; |
1856
|
|
|
$additional_query .= ")"; |
1857
|
|
|
} |
1858
|
|
|
} |
1859
|
|
|
} |
1860
|
|
|
if ($callsign != "") |
1861
|
|
|
{ |
1862
|
|
|
$callsign = filter_var($callsign,FILTER_SANITIZE_STRING); |
1863
|
|
|
if (!is_string($callsign)) |
1864
|
|
|
{ |
1865
|
|
|
return false; |
1866
|
|
|
} else { |
1867
|
|
|
$additional_query .= " AND marine_output.ident = :callsign"; |
1868
|
|
|
$query_values = array_merge($query_values,array(':callsign' => $callsign)); |
1869
|
|
|
} |
1870
|
|
|
} |
1871
|
|
|
if ($mmsi != "") |
1872
|
|
|
{ |
1873
|
|
|
$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING); |
1874
|
|
|
if (!is_numeric($mmsi)) |
1875
|
|
|
{ |
1876
|
|
|
return false; |
1877
|
|
|
} else { |
1878
|
|
|
$additional_query .= " AND marine_output.mmsi = :mmsi"; |
1879
|
|
|
$query_values = array_merge($query_values,array(':mmsi' => $mmsi)); |
1880
|
|
|
} |
1881
|
|
|
} |
1882
|
|
|
if ($imo != "") |
1883
|
|
|
{ |
1884
|
|
|
$imo = filter_var($imo,FILTER_SANITIZE_STRING); |
1885
|
|
|
if (!is_numeric($imo)) |
1886
|
|
|
{ |
1887
|
|
|
return false; |
1888
|
|
|
} else { |
1889
|
|
|
$additional_query .= " AND marine_output.imo = :imo"; |
1890
|
|
|
$query_values = array_merge($query_values,array(':imo' => $imo)); |
1891
|
|
|
} |
1892
|
|
|
} |
1893
|
|
|
if ($date_posted != "") |
1894
|
|
|
{ |
1895
|
|
|
$date_array = explode(",", $date_posted); |
1896
|
|
|
$date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING); |
1897
|
|
|
$date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING); |
1898
|
|
|
if ($globalTimezone != '') { |
1899
|
|
|
date_default_timezone_set($globalTimezone); |
1900
|
|
|
$datetime = new DateTime(); |
1901
|
|
|
$offset = $datetime->format('P'); |
1902
|
|
|
} else $offset = '+00:00'; |
1903
|
|
|
if ($date_array[1] != "") |
1904
|
|
|
{ |
1905
|
|
|
$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0])); |
1906
|
|
|
$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1])); |
1907
|
|
|
if ($globalDBdriver == 'mysql') { |
1908
|
|
|
$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) <= '".$date_array[1]."' "; |
1909
|
|
|
} else { |
1910
|
|
|
$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) <= '".$date_array[1]."' "; |
1911
|
|
|
} |
1912
|
|
|
} else { |
1913
|
|
|
$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0])); |
1914
|
|
|
if ($globalDBdriver == 'mysql') { |
1915
|
|
|
$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' "; |
1916
|
|
|
} else { |
1917
|
|
|
$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' "; |
1918
|
|
|
} |
1919
|
|
|
} |
1920
|
|
|
} |
1921
|
|
|
if ($limit != "") |
1922
|
|
|
{ |
1923
|
|
|
$limit_array = explode(",", $limit); |
1924
|
|
|
$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT); |
1925
|
|
|
$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT); |
1926
|
|
|
if ($limit_array[0] >= 0 && $limit_array[1] >= 0) |
1927
|
|
|
{ |
1928
|
|
|
$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0]; |
1929
|
|
|
} else $limit_query = ""; |
1930
|
|
|
} else $limit_query = ""; |
1931
|
|
|
if ($sort != "") |
1932
|
|
|
{ |
1933
|
|
|
$search_orderby_array = $this->getOrderBy(); |
1934
|
|
|
$orderby_query = $search_orderby_array[$sort]['sql']; |
1935
|
|
|
} else { |
1936
|
|
|
if ($origLat != "" && $origLon != "" && $dist != "") { |
1937
|
|
|
$orderby_query = " ORDER BY distance ASC"; |
1938
|
|
|
} else { |
1939
|
|
|
$orderby_query = " ORDER BY marine_output.date DESC"; |
1940
|
|
|
} |
1941
|
|
|
} |
1942
|
|
|
if ($origLat != "" && $origLon != "" && $dist != "") { |
1943
|
|
|
$dist = number_format($dist*0.621371,2,'.',''); // convert km to mile |
1944
|
|
|
if ($globalDBdriver == 'mysql') { |
1945
|
|
|
$query="SELECT marine_output.*, 1.60935*3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - marine_archive.latitude)*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(marine_archive.latitude*pi()/180)*POWER(SIN(($origLon-marine_archive.longitude)*pi()/180/2),2))) as distance |
1946
|
|
|
FROM marine_archive,marine_output".$filter_query." marine_output.fammarine_id = marine_archive.fammarine_id AND marine_output.ident <> '' ".$additional_query."AND marine_archive.longitude between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon+$dist/cos(radians($origLat)*69)) and marine_archive.latitude between ($origLat-($dist/69)) and ($origLat+($dist/69)) |
1947
|
|
|
AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - marine_archive.latitude)*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(marine_archive.latitude*pi()/180)*POWER(SIN(($origLon-marine_archive.longitude)*pi()/180/2),2)))) < $dist".$orderby_query; |
1948
|
|
|
} else { |
1949
|
|
|
$query="SELECT marine_output.*, 1.60935 * 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - CAST(marine_archive.latitude as double precision))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(CAST(marine_archive.latitude as double precision)*pi()/180)*POWER(SIN(($origLon-CAST(marine_archive.longitude as double precision))*pi()/180/2),2))) as distance |
1950
|
|
|
FROM marine_archive,marine_output".$filter_query." marine_output.fammarine_id = marine_archive.fammarine_id AND marine_output.ident <> '' ".$additional_query."AND CAST(marine_archive.longitude as double precision) between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon+$dist/cos(radians($origLat))*69) and CAST(marine_archive.latitude as double precision) between ($origLat-($dist/69)) and ($origLat+($dist/69)) |
1951
|
|
|
AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - CAST(marine_archive.latitude as double precision))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(CAST(marine_archive.latitude as double precision)*pi()/180)*POWER(SIN(($origLon-CAST(marine_archive.longitude as double precision))*pi()/180/2),2)))) < $dist".$filter_query.$orderby_query; |
1952
|
|
|
} |
1953
|
|
|
} else { |
1954
|
|
|
$query = "SELECT marine_output.* FROM marine_output".$filter_query." marine_output.ident <> '' |
1955
|
|
|
".$additional_query." |
1956
|
|
|
".$orderby_query; |
1957
|
|
|
} |
1958
|
|
|
$marine_array = $this->getDataFromDB($query, $query_values,$limit_query); |
1959
|
|
|
return $marine_array; |
1960
|
|
|
} |
1961
|
|
|
|
1962
|
|
|
public function getOrderBy() |
1963
|
|
|
{ |
1964
|
|
|
$orderby = array("type_asc" => array("key" => "type_asc", "value" => "Type - ASC", "sql" => "ORDER BY marine_output.type ASC"), "type_desc" => array("key" => "type_desc", "value" => "Type - DESC", "sql" => "ORDER BY marine_output.type DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY marine_output.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY marine_output.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY marine_output.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY marine_output.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY marine_output.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY marine_output.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure Airport - ASC", "sql" => "ORDER BY marine_output.departure_airport_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY marine_output.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY marine_output.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY marine_output.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY marine_output.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY marine_output.date DESC"),"distance_asc" => array("key" => "distance_asc","value" => "Distance - ASC","sql" => "ORDER BY distance ASC"),"distance_desc" => array("key" => "distance_desc","value" => "Distance - DESC","sql" => "ORDER BY distance DESC")); |
1965
|
|
|
|
1966
|
|
|
return $orderby; |
1967
|
|
|
|
1968
|
|
|
} |
1969
|
|
|
|
1970
|
|
|
} |
1971
|
|
|
?> |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.