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
|
|
|
$imo = filter_var($imo,FILTER_SANITIZE_NUMBER_INT); |
475
|
|
|
$ident = filter_var($ident,FILTER_SANITIZE_STRING); |
476
|
|
|
$callsign = filter_var($callsign,FILTER_SANITIZE_STRING); |
477
|
|
|
$type = filter_var($type,FILTER_SANITIZE_STRING); |
478
|
|
|
if (empty($this->getIdentity)) { |
|
|
|
|
479
|
|
|
$query = "INSERT INTO marine_identity (mmsi,imo,call_sign,ship_name,type) VALUES (:mmsi,:imo,:call_sign,:ship_name,:type)"; |
480
|
|
|
$sth = $this->db->prepare($query); |
481
|
|
|
$sth->execute(array(':mmsi' => $mmsi,':imo' => $imo,':call_sign' => $callsign,':ship_name' => $ident,':type' => $type)); |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/* |
486
|
|
|
* Gets a list of all dates |
487
|
|
|
* |
488
|
|
|
* @return Array list of date names |
489
|
|
|
* |
490
|
|
|
*/ |
491
|
|
|
public function getAllDates() |
492
|
|
|
{ |
493
|
|
|
global $globalTimezone, $globalDBdriver; |
494
|
|
|
if ($globalTimezone != '') { |
495
|
|
|
date_default_timezone_set($globalTimezone); |
496
|
|
|
$datetime = new DateTime(); |
497
|
|
|
$offset = $datetime->format('P'); |
498
|
|
|
} else $offset = '+00:00'; |
499
|
|
|
|
500
|
|
|
if ($globalDBdriver == 'mysql') { |
501
|
|
|
$query = "SELECT DISTINCT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) as date |
502
|
|
|
FROM marine_output |
503
|
|
|
WHERE marine_output.date <> '' |
504
|
|
|
ORDER BY marine_output.date ASC LIMIT 0,100"; |
505
|
|
|
} else { |
506
|
|
|
$query = "SELECT DISTINCT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') as date |
507
|
|
|
FROM marine_output |
508
|
|
|
WHERE marine_output.date <> '' |
509
|
|
|
ORDER BY marine_output.date ASC LIMIT 0,100"; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
$sth = $this->db->prepare($query); |
513
|
|
|
$sth->execute(array(':offset' => $offset)); |
514
|
|
|
|
515
|
|
|
$date_array = array(); |
516
|
|
|
$temp_array = array(); |
517
|
|
|
|
518
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
519
|
|
|
{ |
520
|
|
|
$temp_array['date'] = $row['date']; |
521
|
|
|
|
522
|
|
|
$date_array[] = $temp_array; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
return $date_array; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Update ident tracker data |
530
|
|
|
* |
531
|
|
|
* @param String $fammarine_id the ID |
532
|
|
|
* @param String $ident the marine ident |
533
|
|
|
* @return String success or false |
534
|
|
|
* |
535
|
|
|
*/ |
536
|
|
|
public function updateIdentMarineData($fammarine_id = '', $ident = '',$fromsource = NULL) |
|
|
|
|
537
|
|
|
{ |
538
|
|
|
$query = 'UPDATE marine_output SET ident = :ident WHERE fammarine_id = :fammarine_id'; |
539
|
|
|
$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident); |
540
|
|
|
try { |
541
|
|
|
$sth = $this->db->prepare($query); |
542
|
|
|
$sth->execute($query_values); |
543
|
|
|
} catch (PDOException $e) { |
544
|
|
|
return "error : ".$e->getMessage(); |
545
|
|
|
} |
546
|
|
|
return "success"; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Update Status data |
551
|
|
|
* |
552
|
|
|
* @param String $fammarine_id the ID |
553
|
|
|
* @param String $status_id the marine status id |
554
|
|
|
* @param String $status the marine status |
555
|
|
|
* @return String success or false |
556
|
|
|
* |
557
|
|
|
*/ |
558
|
|
|
public function updateStatusMarineData($fammarine_id = '', $status_id = '',$status = '') |
559
|
|
|
{ |
560
|
|
|
|
561
|
|
|
$query = 'UPDATE marine_output SET status = :status, status_id = :status_id WHERE fammarine_id = :fammarine_id'; |
562
|
|
|
$query_values = array(':fammarine_id' => $fammarine_id,':status' => $status,':status_id' => $status_id); |
563
|
|
|
|
564
|
|
|
try { |
565
|
|
|
$sth = $this->db->prepare($query); |
566
|
|
|
$sth->execute($query_values); |
567
|
|
|
} catch (PDOException $e) { |
568
|
|
|
return "error : ".$e->getMessage(); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
return "success"; |
572
|
|
|
|
573
|
|
|
} |
574
|
|
|
/** |
575
|
|
|
* Update latest marine data |
576
|
|
|
* |
577
|
|
|
* @param String $fammarine_id the ID |
578
|
|
|
* @param String $ident the marine ident |
579
|
|
|
* @return String success or false |
580
|
|
|
* |
581
|
|
|
*/ |
582
|
|
|
public function updateLatestMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $groundspeed = NULL, $date = '') |
583
|
|
|
{ |
584
|
|
|
$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'; |
585
|
|
|
$query_values = array(':fammarine_id' => $fammarine_id,':last_latitude' => $latitude,':last_longitude' => $longitude, ':last_ground_speed' => $groundspeed,':last_seen' => $date,':ident' => $ident); |
586
|
|
|
|
587
|
|
|
try { |
588
|
|
|
$sth = $this->db->prepare($query); |
589
|
|
|
$sth->execute($query_values); |
590
|
|
|
} catch (PDOException $e) { |
591
|
|
|
return "error : ".$e->getMessage(); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
return "success"; |
595
|
|
|
|
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Adds a new spotter data |
600
|
|
|
* |
601
|
|
|
* @param String $fammarine_id the ID |
602
|
|
|
* @param String $ident the marine ident |
603
|
|
|
* @param String $departure_airport_icao the departure airport |
|
|
|
|
604
|
|
|
* @param String $arrival_airport_icao the arrival airport |
|
|
|
|
605
|
|
|
* @param String $latitude latitude of flight |
606
|
|
|
* @param String $longitude latitude of flight |
607
|
|
|
* @param String $waypoints waypoints of flight |
|
|
|
|
608
|
|
|
* @param String $heading heading of flight |
609
|
|
|
* @param String $groundspeed speed of flight |
610
|
|
|
* @param String $date date of flight |
611
|
|
|
* @param String $departure_airport_time departure time of flight |
|
|
|
|
612
|
|
|
* @param String $arrival_airport_time arrival time of flight |
|
|
|
|
613
|
|
|
* @param String $squawk squawk code of flight |
|
|
|
|
614
|
|
|
* @param String $route_stop route stop of flight |
|
|
|
|
615
|
|
|
* @param String $highlight highlight or not |
|
|
|
|
616
|
|
|
* @param String $ModeS ModesS code of flight |
|
|
|
|
617
|
|
|
* @param String $registration registration code of flight |
|
|
|
|
618
|
|
|
* @param String $pilot_id pilot id of flight (for virtual airlines) |
|
|
|
|
619
|
|
|
* @param String $pilot_name pilot name of flight (for virtual airlines) |
|
|
|
|
620
|
|
|
* @param String $verticalrate vertival rate of flight |
|
|
|
|
621
|
|
|
* @return String success or false |
622
|
|
|
*/ |
623
|
|
|
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 = '') |
|
|
|
|
624
|
|
|
{ |
625
|
|
|
global $globalURL, $globalMarineImageFetch; |
626
|
|
|
|
627
|
|
|
//$Image = new Image($this->db); |
628
|
|
|
$Common = new Common(); |
629
|
|
|
|
630
|
|
|
date_default_timezone_set('UTC'); |
631
|
|
|
|
632
|
|
|
//getting the registration |
633
|
|
|
if ($fammarine_id != "") |
634
|
|
|
{ |
635
|
|
|
if (!is_string($fammarine_id)) |
636
|
|
|
{ |
637
|
|
|
return false; |
638
|
|
|
} |
639
|
|
|
} |
640
|
|
|
$fromsource = NULL; |
|
|
|
|
641
|
|
|
//getting the airline information |
642
|
|
|
if ($ident != "") |
643
|
|
|
{ |
644
|
|
|
if (!is_string($ident)) |
645
|
|
|
{ |
646
|
|
|
return false; |
647
|
|
|
} |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
if ($latitude != "") |
651
|
|
|
{ |
652
|
|
|
if (!is_numeric($latitude)) |
653
|
|
|
{ |
654
|
|
|
return false; |
655
|
|
|
} |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
if ($longitude != "") |
659
|
|
|
{ |
660
|
|
|
if (!is_numeric($longitude)) |
661
|
|
|
{ |
662
|
|
|
return false; |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
if ($heading != "") |
667
|
|
|
{ |
668
|
|
|
if (!is_numeric($heading)) |
669
|
|
|
{ |
670
|
|
|
return false; |
671
|
|
|
} |
672
|
|
|
} |
673
|
|
|
if ($mmsi != "") |
674
|
|
|
{ |
675
|
|
|
if (!is_numeric($mmsi)) |
676
|
|
|
{ |
677
|
|
|
return false; |
678
|
|
|
} |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
if ($groundspeed != "") |
682
|
|
|
{ |
683
|
|
|
if (!is_numeric($groundspeed)) |
684
|
|
|
{ |
685
|
|
|
return false; |
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
|
690
|
|
|
if ($date == "" || strtotime($date) < time()-20*60) |
691
|
|
|
{ |
692
|
|
|
$date = date("Y-m-d H:i:s", time()); |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING); |
696
|
|
|
$ident = filter_var($ident,FILTER_SANITIZE_STRING); |
697
|
|
|
$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
698
|
|
|
$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
699
|
|
|
$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT); |
700
|
|
|
$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
701
|
|
|
$format_source = filter_var($format_source,FILTER_SANITIZE_STRING); |
702
|
|
|
$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING); |
703
|
|
|
$type = filter_var($type,FILTER_SANITIZE_STRING); |
704
|
|
|
$status = filter_var($status,FILTER_SANITIZE_STRING); |
705
|
|
|
$imo = filter_var($imo,FILTER_SANITIZE_STRING); |
706
|
|
|
$callsign = filter_var($callsign,FILTER_SANITIZE_STRING); |
|
|
|
|
707
|
|
|
$arrival_code = filter_var($arrival_code,FILTER_SANITIZE_STRING); |
708
|
|
|
$arrival_date = filter_var($arrival_date,FILTER_SANITIZE_STRING); |
709
|
|
|
|
710
|
|
|
if (isset($globalMarineImageFetch) && $globalMarineImageFetch === TRUE) { |
711
|
|
|
$Image = new Image($this->db); |
712
|
|
|
$image_array = $Image->getMarineImage($mmsi,$imo,$ident); |
713
|
|
|
if (!isset($image_array[0]['mmsi'])) { |
714
|
|
|
$Image->addMarineImage($mmsi,$imo,$ident); |
715
|
|
|
} |
716
|
|
|
unset($Image); |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
if ($latitude == '' && $longitude == '') { |
720
|
|
|
$latitude = 0; |
721
|
|
|
$longitude = 0; |
722
|
|
|
} |
723
|
|
|
if ($heading == '' || $Common->isInteger($heading) === false) $heading = 0; |
724
|
|
|
if ($groundspeed == '' || $Common->isInteger($groundspeed) === false) $groundspeed = 0; |
725
|
|
|
if ($arrival_date == '') $arrival_date = NULL; |
726
|
|
|
$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) |
727
|
|
|
VALUES (:fammarine_id,:ident,:latitude,:longitude,:heading,:speed,:date,:format_source, :source_name,:mmsi,:type,:status,:imo,:arrival_port_name,:arrival_port_date)"; |
728
|
|
|
|
729
|
|
|
$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); |
730
|
|
|
try { |
731
|
|
|
|
732
|
|
|
$sth = $this->db->prepare($query); |
733
|
|
|
$sth->execute($query_values); |
734
|
|
|
$this->db = null; |
735
|
|
|
} catch (PDOException $e) { |
736
|
|
|
return "error : ".$e->getMessage(); |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
return "success"; |
740
|
|
|
|
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* Gets the aircraft ident within the last hour |
746
|
|
|
* |
747
|
|
|
* @return String the ident |
748
|
|
|
* |
749
|
|
|
*/ |
750
|
|
|
public function getIdentFromLastHour($ident) |
751
|
|
|
{ |
752
|
|
|
global $globalDBdriver, $globalTimezone; |
753
|
|
|
if ($globalDBdriver == 'mysql') { |
754
|
|
|
$query = "SELECT marine_output.ident FROM marine_output |
755
|
|
|
WHERE marine_output.ident = :ident |
756
|
|
|
AND marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) |
757
|
|
|
AND marine_output.date < UTC_TIMESTAMP()"; |
758
|
|
|
$query_data = array(':ident' => $ident); |
759
|
|
|
} else { |
760
|
|
|
$query = "SELECT marine_output.ident FROM marine_output |
761
|
|
|
WHERE marine_output.ident = :ident |
762
|
|
|
AND marine_output.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS' |
763
|
|
|
AND marine_output.date < now() AT TIME ZONE 'UTC'"; |
764
|
|
|
$query_data = array(':ident' => $ident); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
$sth = $this->db->prepare($query); |
768
|
|
|
$sth->execute($query_data); |
769
|
|
|
$ident_result=''; |
770
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
771
|
|
|
{ |
772
|
|
|
$ident_result = $row['ident']; |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
return $ident_result; |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* Gets the aircraft data from the last 20 seconds |
781
|
|
|
* |
782
|
|
|
* @return Array the spotter data |
783
|
|
|
* |
784
|
|
|
*/ |
785
|
|
|
public function getRealTimeData($q = '') |
786
|
|
|
{ |
787
|
|
|
global $globalDBdriver; |
788
|
|
|
$additional_query = ''; |
789
|
|
|
if ($q != "") |
790
|
|
|
{ |
791
|
|
|
if (!is_string($q)) |
792
|
|
|
{ |
793
|
|
|
return false; |
794
|
|
|
} else { |
795
|
|
|
$q_array = explode(" ", $q); |
796
|
|
|
foreach ($q_array as $q_item){ |
797
|
|
|
$q_item = filter_var($q_item,FILTER_SANITIZE_STRING); |
798
|
|
|
$additional_query .= " AND ("; |
799
|
|
|
$additional_query .= "(marine_output.ident like '%".$q_item."%')"; |
800
|
|
|
$additional_query .= ")"; |
801
|
|
|
} |
802
|
|
|
} |
803
|
|
|
} |
804
|
|
|
if ($globalDBdriver == 'mysql') { |
805
|
|
|
$query = "SELECT marine_output.* FROM marine_output |
806
|
|
|
WHERE marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 SECOND) ".$additional_query." |
807
|
|
|
AND marine_output.date < UTC_TIMESTAMP()"; |
808
|
|
|
} else { |
809
|
|
|
$query = "SELECT marine_output.* FROM marine_output |
810
|
|
|
WHERE marine_output.date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 SECONDS' ".$additional_query." |
811
|
|
|
AND marine_output.date::timestamp < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'"; |
812
|
|
|
} |
813
|
|
|
$spotter_array = $this->getDataFromDB($query, array()); |
814
|
|
|
|
815
|
|
|
return $spotter_array; |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
|
819
|
|
|
|
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* Gets all number of flight over countries |
823
|
|
|
* |
824
|
|
|
* @return Array the airline country list |
825
|
|
|
* |
826
|
|
|
*/ |
827
|
|
|
|
828
|
|
|
public function countAllMarineOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array()) |
829
|
|
|
{ |
830
|
|
|
global $globalDBdriver, $globalArchive; |
831
|
|
|
//$filter_query = $this->getFilter($filters,true,true); |
832
|
|
|
$Connection= new Connection($this->db); |
833
|
|
|
if (!$Connection->tableExists('countries')) return array(); |
834
|
|
|
require_once('class.SpotterLive.php'); |
835
|
|
|
if (!isset($globalArchive) || $globalArchive !== TRUE) { |
836
|
|
|
$MarineLive = new MarineLive($this->db); |
837
|
|
|
$filter_query = $MarineLive->getFilter($filters,true,true); |
838
|
|
|
$filter_query .= " over_country IS NOT NULL AND over_country <> ''"; |
839
|
|
|
if ($olderthanmonths > 0) { |
840
|
|
|
if ($globalDBdriver == 'mysql') { |
841
|
|
|
$filter_query .= ' AND marine_live.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) '; |
842
|
|
|
} else { |
843
|
|
|
$filter_query .= " AND marine_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
844
|
|
|
} |
845
|
|
|
} |
846
|
|
|
if ($sincedate != '') { |
847
|
|
|
if ($globalDBdriver == 'mysql') { |
848
|
|
|
$filter_query .= " AND marine_live.date > '".$sincedate."' "; |
849
|
|
|
} else { |
850
|
|
|
$filter_query .= " AND marine_live.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
851
|
|
|
} |
852
|
|
|
} |
853
|
|
|
$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 "; |
854
|
|
|
} else { |
855
|
|
|
require_once(dirname(__FILE__)."/class.MarineArchive.php"); |
856
|
|
|
$MarineArchive = new MarineArchive($this->db); |
857
|
|
|
$filter_query = $MarineArchive->getFilter($filters,true,true); |
858
|
|
|
$filter_query .= " over_country <> ''"; |
859
|
|
|
if ($olderthanmonths > 0) { |
860
|
|
|
if ($globalDBdriver == 'mysql') { |
861
|
|
|
$filter_query .= ' AND marine_archive.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) '; |
862
|
|
|
} else { |
863
|
|
|
$filter_query .= " AND marine_archive.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
864
|
|
|
} |
865
|
|
|
} |
866
|
|
|
if ($sincedate != '') { |
867
|
|
|
if ($globalDBdriver == 'mysql') { |
868
|
|
|
$filter_query .= " AND marine_archive.date > '".$sincedate."' "; |
869
|
|
|
} else { |
870
|
|
|
$filter_query .= " AND marine_archive.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
871
|
|
|
} |
872
|
|
|
} |
873
|
|
|
$filter_query .= " LIMIT 100 OFFSET 0"; |
874
|
|
|
$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 "; |
875
|
|
|
} |
876
|
|
|
$query .= "GROUP BY c.name,c.iso3,c.iso2 ORDER BY nb DESC"; |
877
|
|
|
if ($limit) $query .= " LIMIT 10 OFFSET 0"; |
878
|
|
|
|
879
|
|
|
$sth = $this->db->prepare($query); |
880
|
|
|
$sth->execute(); |
881
|
|
|
|
882
|
|
|
$flight_array = array(); |
883
|
|
|
$temp_array = array(); |
884
|
|
|
|
885
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
886
|
|
|
{ |
887
|
|
|
$temp_array['marine_count'] = $row['nb']; |
888
|
|
|
$temp_array['marine_country'] = $row['name']; |
889
|
|
|
$temp_array['marine_country_iso3'] = $row['iso3']; |
890
|
|
|
$temp_array['marine_country_iso2'] = $row['iso2']; |
891
|
|
|
$flight_array[] = $temp_array; |
892
|
|
|
} |
893
|
|
|
return $flight_array; |
894
|
|
|
} |
895
|
|
|
|
896
|
|
|
|
897
|
|
|
|
898
|
|
|
/** |
899
|
|
|
* Gets all callsigns that have flown over |
900
|
|
|
* |
901
|
|
|
* @return Array the callsign list |
902
|
|
|
* |
903
|
|
|
*/ |
904
|
|
|
public function countAllCallsigns($limit = true, $olderthanmonths = 0, $sincedate = '',$filters = array(),$year = '', $month = '', $day = '') |
905
|
|
|
{ |
906
|
|
|
global $globalDBdriver; |
907
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
908
|
|
|
$query = "SELECT DISTINCT marine_output.ident, COUNT(marine_output.ident) AS callsign_icao_count |
909
|
|
|
FROM marine_output".$filter_query." marine_output.ident <> ''"; |
910
|
|
|
if ($olderthanmonths > 0) { |
911
|
|
|
if ($globalDBdriver == 'mysql') $query .= ' AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH)'; |
912
|
|
|
else $query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
913
|
|
|
} |
914
|
|
|
if ($sincedate != '') { |
915
|
|
|
if ($globalDBdriver == 'mysql') $query .= " AND marine_output.date > '".$sincedate."'"; |
916
|
|
|
else $query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
917
|
|
|
} |
918
|
|
|
$query_values = array(); |
919
|
|
|
if ($year != '') { |
920
|
|
|
if ($globalDBdriver == 'mysql') { |
921
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
922
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
923
|
|
|
} else { |
924
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
925
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
926
|
|
|
} |
927
|
|
|
} |
928
|
|
|
if ($month != '') { |
929
|
|
|
if ($globalDBdriver == 'mysql') { |
930
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
931
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
932
|
|
|
} else { |
933
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
934
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
935
|
|
|
} |
936
|
|
|
} |
937
|
|
|
if ($day != '') { |
938
|
|
|
if ($globalDBdriver == 'mysql') { |
939
|
|
|
$query .= " AND DAY(marine_output.date) = :day"; |
940
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
941
|
|
|
} else { |
942
|
|
|
$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day"; |
943
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
944
|
|
|
} |
945
|
|
|
} |
946
|
|
|
$query .= " GROUP BY marine_output.ident ORDER BY callsign_icao_count DESC"; |
947
|
|
|
if ($limit) $query .= " LIMIT 10 OFFSET 0"; |
948
|
|
|
|
949
|
|
|
$sth = $this->db->prepare($query); |
950
|
|
|
$sth->execute($query_values); |
951
|
|
|
|
952
|
|
|
$callsign_array = array(); |
953
|
|
|
$temp_array = array(); |
954
|
|
|
|
955
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
956
|
|
|
{ |
957
|
|
|
$temp_array['callsign_icao'] = $row['ident']; |
958
|
|
|
$temp_array['airline_name'] = $row['airline_name']; |
959
|
|
|
$temp_array['airline_icao'] = $row['airline_icao']; |
960
|
|
|
$temp_array['callsign_icao_count'] = $row['callsign_icao_count']; |
961
|
|
|
|
962
|
|
|
$callsign_array[] = $temp_array; |
963
|
|
|
} |
964
|
|
|
|
965
|
|
|
return $callsign_array; |
966
|
|
|
} |
967
|
|
|
|
968
|
|
|
|
969
|
|
|
/** |
970
|
|
|
* Counts all dates |
971
|
|
|
* |
972
|
|
|
* @return Array the date list |
973
|
|
|
* |
974
|
|
|
*/ |
975
|
|
|
public function countAllDates($filters = array()) |
976
|
|
|
{ |
977
|
|
|
global $globalTimezone, $globalDBdriver; |
978
|
|
|
if ($globalTimezone != '') { |
979
|
|
|
date_default_timezone_set($globalTimezone); |
980
|
|
|
$datetime = new DateTime(); |
981
|
|
|
$offset = $datetime->format('P'); |
982
|
|
|
} else $offset = '+00:00'; |
983
|
|
|
|
984
|
|
|
if ($globalDBdriver == 'mysql') { |
985
|
|
|
$query = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count |
986
|
|
|
FROM marine_output"; |
987
|
|
|
$query .= $this->getFilter($filters); |
988
|
|
|
$query .= " GROUP BY date_name |
989
|
|
|
ORDER BY date_count DESC |
990
|
|
|
LIMIT 10 OFFSET 0"; |
991
|
|
|
} else { |
992
|
|
|
$query = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') 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
|
|
|
} |
999
|
|
|
|
1000
|
|
|
|
1001
|
|
|
$sth = $this->db->prepare($query); |
1002
|
|
|
$sth->execute(array(':offset' => $offset)); |
1003
|
|
|
|
1004
|
|
|
$date_array = array(); |
1005
|
|
|
$temp_array = array(); |
1006
|
|
|
|
1007
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1008
|
|
|
{ |
1009
|
|
|
$temp_array['date_name'] = $row['date_name']; |
1010
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1011
|
|
|
|
1012
|
|
|
$date_array[] = $temp_array; |
1013
|
|
|
} |
1014
|
|
|
|
1015
|
|
|
return $date_array; |
1016
|
|
|
} |
1017
|
|
|
|
1018
|
|
|
|
1019
|
|
|
/** |
1020
|
|
|
* Counts all dates during the last 7 days |
1021
|
|
|
* |
1022
|
|
|
* @return Array the date list |
1023
|
|
|
* |
1024
|
|
|
*/ |
1025
|
|
|
public function countAllDatesLast7Days($filters = array()) |
1026
|
|
|
{ |
1027
|
|
|
global $globalTimezone, $globalDBdriver; |
1028
|
|
|
if ($globalTimezone != '') { |
1029
|
|
|
date_default_timezone_set($globalTimezone); |
1030
|
|
|
$datetime = new DateTime(); |
1031
|
|
|
$offset = $datetime->format('P'); |
1032
|
|
|
} else $offset = '+00:00'; |
1033
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1034
|
|
|
if ($globalDBdriver == 'mysql') { |
1035
|
|
|
$query = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count |
1036
|
|
|
FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY)"; |
1037
|
|
|
$query .= " GROUP BY date_name |
1038
|
|
|
ORDER BY marine_output.date ASC"; |
1039
|
|
|
$query_data = array(':offset' => $offset); |
1040
|
|
|
} else { |
1041
|
|
|
$query = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count |
1042
|
|
|
FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '7 DAYS'"; |
1043
|
|
|
$query .= " GROUP BY date_name |
1044
|
|
|
ORDER BY date_name ASC"; |
1045
|
|
|
$query_data = array(':offset' => $offset); |
1046
|
|
|
} |
1047
|
|
|
|
1048
|
|
|
$sth = $this->db->prepare($query); |
1049
|
|
|
$sth->execute($query_data); |
1050
|
|
|
|
1051
|
|
|
$date_array = array(); |
1052
|
|
|
$temp_array = array(); |
1053
|
|
|
|
1054
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1055
|
|
|
{ |
1056
|
|
|
$temp_array['date_name'] = $row['date_name']; |
1057
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1058
|
|
|
|
1059
|
|
|
$date_array[] = $temp_array; |
1060
|
|
|
} |
1061
|
|
|
|
1062
|
|
|
return $date_array; |
1063
|
|
|
} |
1064
|
|
|
|
1065
|
|
|
/** |
1066
|
|
|
* Counts all dates during the last month |
1067
|
|
|
* |
1068
|
|
|
* @return Array the date list |
1069
|
|
|
* |
1070
|
|
|
*/ |
1071
|
|
|
public function countAllDatesLastMonth($filters = array()) |
1072
|
|
|
{ |
1073
|
|
|
global $globalTimezone, $globalDBdriver; |
1074
|
|
|
if ($globalTimezone != '') { |
1075
|
|
|
date_default_timezone_set($globalTimezone); |
1076
|
|
|
$datetime = new DateTime(); |
1077
|
|
|
$offset = $datetime->format('P'); |
1078
|
|
|
} else $offset = '+00:00'; |
1079
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1080
|
|
|
if ($globalDBdriver == 'mysql') { |
1081
|
|
|
$query = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count |
1082
|
|
|
FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MONTH)"; |
1083
|
|
|
$query .= " GROUP BY date_name |
1084
|
|
|
ORDER BY marine_output.date ASC"; |
1085
|
|
|
$query_data = array(':offset' => $offset); |
1086
|
|
|
} else { |
1087
|
|
|
$query = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count |
1088
|
|
|
FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 MONTHS'"; |
1089
|
|
|
$query .= " GROUP BY date_name |
1090
|
|
|
ORDER BY date_name ASC"; |
1091
|
|
|
$query_data = array(':offset' => $offset); |
1092
|
|
|
} |
1093
|
|
|
|
1094
|
|
|
$sth = $this->db->prepare($query); |
1095
|
|
|
$sth->execute($query_data); |
1096
|
|
|
|
1097
|
|
|
$date_array = array(); |
1098
|
|
|
$temp_array = array(); |
1099
|
|
|
|
1100
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1101
|
|
|
{ |
1102
|
|
|
$temp_array['date_name'] = $row['date_name']; |
1103
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1104
|
|
|
|
1105
|
|
|
$date_array[] = $temp_array; |
1106
|
|
|
} |
1107
|
|
|
|
1108
|
|
|
return $date_array; |
1109
|
|
|
} |
1110
|
|
|
|
1111
|
|
|
|
1112
|
|
|
|
1113
|
|
|
/** |
1114
|
|
|
* Counts all month |
1115
|
|
|
* |
1116
|
|
|
* @return Array the month list |
1117
|
|
|
* |
1118
|
|
|
*/ |
1119
|
|
|
public function countAllMonths($filters = array()) |
1120
|
|
|
{ |
1121
|
|
|
global $globalTimezone, $globalDBdriver; |
1122
|
|
|
if ($globalTimezone != '') { |
1123
|
|
|
date_default_timezone_set($globalTimezone); |
1124
|
|
|
$datetime = new DateTime(); |
1125
|
|
|
$offset = $datetime->format('P'); |
1126
|
|
|
} else $offset = '+00:00'; |
1127
|
|
|
|
1128
|
|
|
if ($globalDBdriver == 'mysql') { |
1129
|
|
|
$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 |
1130
|
|
|
FROM marine_output"; |
1131
|
|
|
$query .= $this->getFilter($filters); |
1132
|
|
|
$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC"; |
1133
|
|
|
} else { |
1134
|
|
|
$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 |
1135
|
|
|
FROM marine_output"; |
1136
|
|
|
$query .= $this->getFilter($filters); |
1137
|
|
|
$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC"; |
1138
|
|
|
} |
1139
|
|
|
|
1140
|
|
|
|
1141
|
|
|
$sth = $this->db->prepare($query); |
1142
|
|
|
$sth->execute(array(':offset' => $offset)); |
1143
|
|
|
|
1144
|
|
|
$date_array = array(); |
1145
|
|
|
$temp_array = array(); |
1146
|
|
|
|
1147
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1148
|
|
|
{ |
1149
|
|
|
$temp_array['month_name'] = $row['month_name']; |
1150
|
|
|
$temp_array['year_name'] = $row['year_name']; |
1151
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1152
|
|
|
|
1153
|
|
|
$date_array[] = $temp_array; |
1154
|
|
|
} |
1155
|
|
|
|
1156
|
|
|
return $date_array; |
1157
|
|
|
} |
1158
|
|
|
|
1159
|
|
|
|
1160
|
|
|
|
1161
|
|
|
|
1162
|
|
|
/** |
1163
|
|
|
* Counts all dates during the last year |
1164
|
|
|
* |
1165
|
|
|
* @return Array the date list |
1166
|
|
|
* |
1167
|
|
|
*/ |
1168
|
|
|
public function countAllMonthsLastYear($filters) |
1169
|
|
|
{ |
1170
|
|
|
global $globalTimezone, $globalDBdriver; |
1171
|
|
|
if ($globalTimezone != '') { |
1172
|
|
|
date_default_timezone_set($globalTimezone); |
1173
|
|
|
$datetime = new DateTime(); |
1174
|
|
|
$offset = $datetime->format('P'); |
1175
|
|
|
} else $offset = '+00:00'; |
1176
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1177
|
|
|
if ($globalDBdriver == 'mysql') { |
1178
|
|
|
$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 |
1179
|
|
|
FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)"; |
1180
|
|
|
$query .= " GROUP BY year_name, month_name |
1181
|
|
|
ORDER BY year_name, month_name ASC"; |
1182
|
|
|
$query_data = array(':offset' => $offset); |
1183
|
|
|
} else { |
1184
|
|
|
$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 |
1185
|
|
|
FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'"; |
1186
|
|
|
$query .= " GROUP BY year_name, month_name |
1187
|
|
|
ORDER BY year_name, month_name ASC"; |
1188
|
|
|
$query_data = array(':offset' => $offset); |
1189
|
|
|
} |
1190
|
|
|
|
1191
|
|
|
$sth = $this->db->prepare($query); |
1192
|
|
|
$sth->execute($query_data); |
1193
|
|
|
|
1194
|
|
|
$date_array = array(); |
1195
|
|
|
$temp_array = array(); |
1196
|
|
|
|
1197
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1198
|
|
|
{ |
1199
|
|
|
$temp_array['year_name'] = $row['year_name']; |
1200
|
|
|
$temp_array['month_name'] = $row['month_name']; |
1201
|
|
|
$temp_array['date_count'] = $row['date_count']; |
1202
|
|
|
|
1203
|
|
|
$date_array[] = $temp_array; |
1204
|
|
|
} |
1205
|
|
|
|
1206
|
|
|
return $date_array; |
1207
|
|
|
} |
1208
|
|
|
|
1209
|
|
|
|
1210
|
|
|
|
1211
|
|
|
/** |
1212
|
|
|
* Counts all hours |
1213
|
|
|
* |
1214
|
|
|
* @return Array the hour list |
1215
|
|
|
* |
1216
|
|
|
*/ |
1217
|
|
|
public function countAllHours($orderby,$filters = array()) |
1218
|
|
|
{ |
1219
|
|
|
global $globalTimezone, $globalDBdriver; |
1220
|
|
|
if ($globalTimezone != '') { |
1221
|
|
|
date_default_timezone_set($globalTimezone); |
1222
|
|
|
$datetime = new DateTime(); |
1223
|
|
|
$offset = $datetime->format('P'); |
1224
|
|
|
} else $offset = '+00:00'; |
1225
|
|
|
|
1226
|
|
|
$orderby_sql = ''; |
1227
|
|
|
if ($orderby == "hour") |
1228
|
|
|
{ |
1229
|
|
|
$orderby_sql = "ORDER BY hour_name ASC"; |
1230
|
|
|
} |
1231
|
|
|
if ($orderby == "count") |
1232
|
|
|
{ |
1233
|
|
|
$orderby_sql = "ORDER BY hour_count DESC"; |
1234
|
|
|
} |
1235
|
|
|
|
1236
|
|
|
if ($globalDBdriver == 'mysql') { |
1237
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1238
|
|
|
FROM marine_output"; |
1239
|
|
|
$query .= $this->getFilter($filters); |
1240
|
|
|
$query .= " GROUP BY hour_name |
1241
|
|
|
".$orderby_sql; |
1242
|
|
|
|
1243
|
|
|
/* $query = "SELECT HOUR(marine_output.date) AS hour_name, count(*) as hour_count |
1244
|
|
|
FROM marine_output |
1245
|
|
|
GROUP BY hour_name |
1246
|
|
|
".$orderby_sql." |
1247
|
|
|
LIMIT 10 OFFSET 00"; |
1248
|
|
|
*/ |
1249
|
|
|
$query_data = array(':offset' => $offset); |
1250
|
|
|
} else { |
1251
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1252
|
|
|
FROM marine_output"; |
1253
|
|
|
$query .= $this->getFilter($filters); |
1254
|
|
|
$query .= " GROUP BY hour_name |
1255
|
|
|
".$orderby_sql; |
1256
|
|
|
$query_data = array(':offset' => $offset); |
1257
|
|
|
} |
1258
|
|
|
|
1259
|
|
|
$sth = $this->db->prepare($query); |
1260
|
|
|
$sth->execute($query_data); |
1261
|
|
|
|
1262
|
|
|
$hour_array = array(); |
1263
|
|
|
$temp_array = array(); |
1264
|
|
|
|
1265
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1266
|
|
|
{ |
1267
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1268
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1269
|
|
|
|
1270
|
|
|
$hour_array[] = $temp_array; |
1271
|
|
|
} |
1272
|
|
|
|
1273
|
|
|
return $hour_array; |
1274
|
|
|
} |
1275
|
|
|
|
1276
|
|
|
|
1277
|
|
|
|
1278
|
|
|
/** |
1279
|
|
|
* Counts all hours by date |
1280
|
|
|
* |
1281
|
|
|
* @return Array the hour list |
1282
|
|
|
* |
1283
|
|
|
*/ |
1284
|
|
|
public function countAllHoursByDate($date, $filters = array()) |
1285
|
|
|
{ |
1286
|
|
|
global $globalTimezone, $globalDBdriver; |
1287
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1288
|
|
|
$date = filter_var($date,FILTER_SANITIZE_STRING); |
1289
|
|
|
if ($globalTimezone != '') { |
1290
|
|
|
date_default_timezone_set($globalTimezone); |
1291
|
|
|
$datetime = new DateTime($date); |
1292
|
|
|
$offset = $datetime->format('P'); |
1293
|
|
|
} else $offset = '+00:00'; |
1294
|
|
|
|
1295
|
|
|
if ($globalDBdriver == 'mysql') { |
1296
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1297
|
|
|
FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date |
1298
|
|
|
GROUP BY hour_name |
1299
|
|
|
ORDER BY hour_name ASC"; |
1300
|
|
|
} else { |
1301
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1302
|
|
|
FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset, 'YYYY-mm-dd') = :date |
1303
|
|
|
GROUP BY hour_name |
1304
|
|
|
ORDER BY hour_name ASC"; |
1305
|
|
|
} |
1306
|
|
|
|
1307
|
|
|
$sth = $this->db->prepare($query); |
1308
|
|
|
$sth->execute(array(':date' => $date, ':offset' => $offset)); |
1309
|
|
|
|
1310
|
|
|
$hour_array = array(); |
1311
|
|
|
$temp_array = array(); |
1312
|
|
|
|
1313
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1314
|
|
|
{ |
1315
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1316
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1317
|
|
|
|
1318
|
|
|
$hour_array[] = $temp_array; |
1319
|
|
|
} |
1320
|
|
|
|
1321
|
|
|
return $hour_array; |
1322
|
|
|
} |
1323
|
|
|
|
1324
|
|
|
|
1325
|
|
|
|
1326
|
|
|
/** |
1327
|
|
|
* Counts all hours by a ident/callsign |
1328
|
|
|
* |
1329
|
|
|
* @return Array the hour list |
1330
|
|
|
* |
1331
|
|
|
*/ |
1332
|
|
|
public function countAllHoursByIdent($ident, $filters = array()) |
1333
|
|
|
{ |
1334
|
|
|
global $globalTimezone, $globalDBdriver; |
1335
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1336
|
|
|
$ident = filter_var($ident,FILTER_SANITIZE_STRING); |
1337
|
|
|
if ($globalTimezone != '') { |
1338
|
|
|
date_default_timezone_set($globalTimezone); |
1339
|
|
|
$datetime = new DateTime(); |
1340
|
|
|
$offset = $datetime->format('P'); |
1341
|
|
|
} else $offset = '+00:00'; |
1342
|
|
|
|
1343
|
|
|
if ($globalDBdriver == 'mysql') { |
1344
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1345
|
|
|
FROM marine_output".$filter_query." marine_output.ident = :ident |
1346
|
|
|
GROUP BY hour_name |
1347
|
|
|
ORDER BY hour_name ASC"; |
1348
|
|
|
} else { |
1349
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1350
|
|
|
FROM marine_output".$filter_query." marine_output.ident = :ident |
1351
|
|
|
GROUP BY hour_name |
1352
|
|
|
ORDER BY hour_name ASC"; |
1353
|
|
|
} |
1354
|
|
|
|
1355
|
|
|
|
1356
|
|
|
$sth = $this->db->prepare($query); |
1357
|
|
|
$sth->execute(array(':ident' => $ident,':offset' => $offset)); |
1358
|
|
|
|
1359
|
|
|
$hour_array = array(); |
1360
|
|
|
$temp_array = array(); |
1361
|
|
|
|
1362
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1363
|
|
|
{ |
1364
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1365
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1366
|
|
|
|
1367
|
|
|
$hour_array[] = $temp_array; |
1368
|
|
|
} |
1369
|
|
|
|
1370
|
|
|
return $hour_array; |
1371
|
|
|
} |
1372
|
|
|
|
1373
|
|
|
|
1374
|
|
|
|
1375
|
|
|
/** |
1376
|
|
|
* Counts all vessels |
1377
|
|
|
* |
1378
|
|
|
* @return Integer the number of vessels |
1379
|
|
|
* |
1380
|
|
|
*/ |
1381
|
|
|
public function countOverallMarine($filters = array(),$year = '',$month = '') |
1382
|
|
|
{ |
1383
|
|
|
global $globalDBdriver; |
1384
|
|
|
//$queryi = "SELECT COUNT(marine_output.marine_id) AS flight_count FROM marine_output"; |
1385
|
|
|
$queryi = "SELECT COUNT(DISTINCT marine_output.mmsi) AS flight_count FROM marine_output"; |
1386
|
|
|
$query_values = array(); |
1387
|
|
|
$query = ''; |
1388
|
|
|
if ($year != '') { |
1389
|
|
|
if ($globalDBdriver == 'mysql') { |
1390
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
1391
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1392
|
|
|
} else { |
1393
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
1394
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1395
|
|
|
} |
1396
|
|
|
} |
1397
|
|
|
if ($month != '') { |
1398
|
|
|
if ($globalDBdriver == 'mysql') { |
1399
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
1400
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1401
|
|
|
} else { |
1402
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
1403
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1404
|
|
|
} |
1405
|
|
|
} |
1406
|
|
|
if (empty($query_values)) $queryi .= $this->getFilter($filters); |
1407
|
|
|
else $queryi .= $this->getFilter($filters,true,true).substr($query,4); |
1408
|
|
|
|
1409
|
|
|
$sth = $this->db->prepare($queryi); |
1410
|
|
|
$sth->execute($query_values); |
1411
|
|
|
return $sth->fetchColumn(); |
1412
|
|
|
} |
1413
|
|
|
|
1414
|
|
|
/** |
1415
|
|
|
* Counts all vessel type |
1416
|
|
|
* |
1417
|
|
|
* @return Integer the number of vessels |
1418
|
|
|
* |
1419
|
|
|
*/ |
1420
|
|
|
public function countOverallMarineTypes($filters = array(),$year = '',$month = '') |
1421
|
|
|
{ |
1422
|
|
|
global $globalDBdriver; |
1423
|
|
|
$queryi = "SELECT COUNT(DISTINCT marine_output.type) AS marine_count FROM marine_output"; |
1424
|
|
|
$query_values = array(); |
1425
|
|
|
$query = ''; |
1426
|
|
|
if ($year != '') { |
1427
|
|
|
if ($globalDBdriver == 'mysql') { |
1428
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
1429
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1430
|
|
|
} else { |
1431
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
1432
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1433
|
|
|
} |
1434
|
|
|
} |
1435
|
|
|
if ($month != '') { |
1436
|
|
|
if ($globalDBdriver == 'mysql') { |
1437
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
1438
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1439
|
|
|
} else { |
1440
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
1441
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1442
|
|
|
} |
1443
|
|
|
} |
1444
|
|
|
if (empty($query_values)) $queryi .= $this->getFilter($filters); |
1445
|
|
|
else $queryi .= $this->getFilter($filters,true,true).substr($query,4); |
1446
|
|
|
|
1447
|
|
|
$sth = $this->db->prepare($queryi); |
1448
|
|
|
$sth->execute($query_values); |
1449
|
|
|
return $sth->fetchColumn(); |
1450
|
|
|
} |
1451
|
|
|
|
1452
|
|
|
|
1453
|
|
|
/** |
1454
|
|
|
* Counts all hours of today |
1455
|
|
|
* |
1456
|
|
|
* @return Array the hour list |
1457
|
|
|
* |
1458
|
|
|
*/ |
1459
|
|
|
public function countAllHoursFromToday($filters = array()) |
1460
|
|
|
{ |
1461
|
|
|
global $globalTimezone, $globalDBdriver; |
1462
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1463
|
|
|
if ($globalTimezone != '') { |
1464
|
|
|
date_default_timezone_set($globalTimezone); |
1465
|
|
|
$datetime = new DateTime(); |
1466
|
|
|
$offset = $datetime->format('P'); |
1467
|
|
|
} else $offset = '+00:00'; |
1468
|
|
|
|
1469
|
|
|
if ($globalDBdriver == 'mysql') { |
1470
|
|
|
$query = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count |
1471
|
|
|
FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = CURDATE() |
1472
|
|
|
GROUP BY hour_name |
1473
|
|
|
ORDER BY hour_name ASC"; |
1474
|
|
|
} else { |
1475
|
|
|
$query = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count |
1476
|
|
|
FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') = CAST(NOW() AS date) |
1477
|
|
|
GROUP BY hour_name |
1478
|
|
|
ORDER BY hour_name ASC"; |
1479
|
|
|
} |
1480
|
|
|
|
1481
|
|
|
$sth = $this->db->prepare($query); |
1482
|
|
|
$sth->execute(array(':offset' => $offset)); |
1483
|
|
|
|
1484
|
|
|
$hour_array = array(); |
1485
|
|
|
$temp_array = array(); |
1486
|
|
|
|
1487
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1488
|
|
|
{ |
1489
|
|
|
$temp_array['hour_name'] = $row['hour_name']; |
1490
|
|
|
$temp_array['hour_count'] = $row['hour_count']; |
1491
|
|
|
$hour_array[] = $temp_array; |
1492
|
|
|
} |
1493
|
|
|
|
1494
|
|
|
return $hour_array; |
1495
|
|
|
} |
1496
|
|
|
|
1497
|
|
|
|
1498
|
|
|
/** |
1499
|
|
|
* Gets the Barrie Spotter ID based on the FlightAware ID |
1500
|
|
|
* |
1501
|
|
|
* @return Integer the Barrie Spotter ID |
1502
|
|
|
q * |
1503
|
|
|
*/ |
1504
|
|
|
public function getMarineIDBasedOnFamMarineID($fammarine_id) |
1505
|
|
|
{ |
1506
|
|
|
$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING); |
1507
|
|
|
|
1508
|
|
|
$query = "SELECT marine_output.marine_id |
1509
|
|
|
FROM marine_output |
1510
|
|
|
WHERE marine_output.fammarine_id = '".$fammarine_id."'"; |
1511
|
|
|
|
1512
|
|
|
|
1513
|
|
|
$sth = $this->db->prepare($query); |
1514
|
|
|
$sth->execute(); |
1515
|
|
|
|
1516
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1517
|
|
|
{ |
1518
|
|
|
return $row['marine_id']; |
1519
|
|
|
} |
1520
|
|
|
} |
1521
|
|
|
|
1522
|
|
|
|
1523
|
|
|
/** |
1524
|
|
|
* Parses a date string |
1525
|
|
|
* |
1526
|
|
|
* @param String $dateString the date string |
1527
|
|
|
* @param String $timezone the timezone of a user |
1528
|
|
|
* @return Array the time information |
1529
|
|
|
* |
1530
|
|
|
*/ |
1531
|
|
|
public function parseDateString($dateString, $timezone = '') |
1532
|
|
|
{ |
1533
|
|
|
$time_array = array(); |
1534
|
|
|
|
1535
|
|
|
if ($timezone != "") |
1536
|
|
|
{ |
1537
|
|
|
date_default_timezone_set($timezone); |
1538
|
|
|
} |
1539
|
|
|
|
1540
|
|
|
$current_date = date("Y-m-d H:i:s"); |
1541
|
|
|
$date = date("Y-m-d H:i:s",strtotime($dateString." UTC")); |
1542
|
|
|
|
1543
|
|
|
$diff = abs(strtotime($current_date) - strtotime($date)); |
1544
|
|
|
|
1545
|
|
|
$time_array['years'] = floor($diff / (365*60*60*24)); |
1546
|
|
|
$years = $time_array['years']; |
1547
|
|
|
|
1548
|
|
|
$time_array['months'] = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); |
1549
|
|
|
$months = $time_array['months']; |
1550
|
|
|
|
1551
|
|
|
$time_array['days'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); |
1552
|
|
|
$days = $time_array['days']; |
1553
|
|
|
$time_array['hours'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); |
1554
|
|
|
$hours = $time_array['hours']; |
1555
|
|
|
$time_array['minutes'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); |
1556
|
|
|
$minutes = $time_array['minutes']; |
1557
|
|
|
$time_array['seconds'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60)); |
1558
|
|
|
|
1559
|
|
|
return $time_array; |
1560
|
|
|
} |
1561
|
|
|
|
1562
|
|
|
/** |
1563
|
|
|
* Parses the direction degrees to working |
1564
|
|
|
* |
1565
|
|
|
* @param Float $direction the direction in degrees |
1566
|
|
|
* @return Array the direction information |
1567
|
|
|
* |
1568
|
|
|
*/ |
1569
|
|
|
public function parseDirection($direction = 0) |
1570
|
|
|
{ |
1571
|
|
|
if ($direction == '') $direction = 0; |
1572
|
|
|
$direction_array = array(); |
1573
|
|
|
$temp_array = array(); |
1574
|
|
|
|
1575
|
|
|
if ($direction == 360 || ($direction >= 0 && $direction < 22.5)) |
1576
|
|
|
{ |
1577
|
|
|
$temp_array['direction_degree'] = $direction; |
1578
|
|
|
$temp_array['direction_shortname'] = "N"; |
1579
|
|
|
$temp_array['direction_fullname'] = "North"; |
1580
|
|
|
} elseif ($direction >= 22.5 && $direction < 45){ |
1581
|
|
|
$temp_array['direction_degree'] = $direction; |
1582
|
|
|
$temp_array['direction_shortname'] = "NNE"; |
1583
|
|
|
$temp_array['direction_fullname'] = "North-Northeast"; |
1584
|
|
|
} elseif ($direction >= 45 && $direction < 67.5){ |
1585
|
|
|
$temp_array['direction_degree'] = $direction; |
1586
|
|
|
$temp_array['direction_shortname'] = "NE"; |
1587
|
|
|
$temp_array['direction_fullname'] = "Northeast"; |
1588
|
|
|
} elseif ($direction >= 67.5 && $direction < 90){ |
1589
|
|
|
$temp_array['direction_degree'] = $direction; |
1590
|
|
|
$temp_array['direction_shortname'] = "ENE"; |
1591
|
|
|
$temp_array['direction_fullname'] = "East-Northeast"; |
1592
|
|
|
} elseif ($direction >= 90 && $direction < 112.5){ |
1593
|
|
|
$temp_array['direction_degree'] = $direction; |
1594
|
|
|
$temp_array['direction_shortname'] = "E"; |
1595
|
|
|
$temp_array['direction_fullname'] = "East"; |
1596
|
|
|
} elseif ($direction >= 112.5 && $direction < 135){ |
1597
|
|
|
$temp_array['direction_degree'] = $direction; |
1598
|
|
|
$temp_array['direction_shortname'] = "ESE"; |
1599
|
|
|
$temp_array['direction_fullname'] = "East-Southeast"; |
1600
|
|
|
} elseif ($direction >= 135 && $direction < 157.5){ |
1601
|
|
|
$temp_array['direction_degree'] = $direction; |
1602
|
|
|
$temp_array['direction_shortname'] = "SE"; |
1603
|
|
|
$temp_array['direction_fullname'] = "Southeast"; |
1604
|
|
|
} elseif ($direction >= 157.5 && $direction < 180){ |
1605
|
|
|
$temp_array['direction_degree'] = $direction; |
1606
|
|
|
$temp_array['direction_shortname'] = "SSE"; |
1607
|
|
|
$temp_array['direction_fullname'] = "South-Southeast"; |
1608
|
|
|
} elseif ($direction >= 180 && $direction < 202.5){ |
1609
|
|
|
$temp_array['direction_degree'] = $direction; |
1610
|
|
|
$temp_array['direction_shortname'] = "S"; |
1611
|
|
|
$temp_array['direction_fullname'] = "South"; |
1612
|
|
|
} elseif ($direction >= 202.5 && $direction < 225){ |
1613
|
|
|
$temp_array['direction_degree'] = $direction; |
1614
|
|
|
$temp_array['direction_shortname'] = "SSW"; |
1615
|
|
|
$temp_array['direction_fullname'] = "South-Southwest"; |
1616
|
|
|
} elseif ($direction >= 225 && $direction < 247.5){ |
1617
|
|
|
$temp_array['direction_degree'] = $direction; |
1618
|
|
|
$temp_array['direction_shortname'] = "SW"; |
1619
|
|
|
$temp_array['direction_fullname'] = "Southwest"; |
1620
|
|
|
} elseif ($direction >= 247.5 && $direction < 270){ |
1621
|
|
|
$temp_array['direction_degree'] = $direction; |
1622
|
|
|
$temp_array['direction_shortname'] = "WSW"; |
1623
|
|
|
$temp_array['direction_fullname'] = "West-Southwest"; |
1624
|
|
|
} elseif ($direction >= 270 && $direction < 292.5){ |
1625
|
|
|
$temp_array['direction_degree'] = $direction; |
1626
|
|
|
$temp_array['direction_shortname'] = "W"; |
1627
|
|
|
$temp_array['direction_fullname'] = "West"; |
1628
|
|
|
} elseif ($direction >= 292.5 && $direction < 315){ |
1629
|
|
|
$temp_array['direction_degree'] = $direction; |
1630
|
|
|
$temp_array['direction_shortname'] = "WNW"; |
1631
|
|
|
$temp_array['direction_fullname'] = "West-Northwest"; |
1632
|
|
|
} elseif ($direction >= 315 && $direction < 337.5){ |
1633
|
|
|
$temp_array['direction_degree'] = $direction; |
1634
|
|
|
$temp_array['direction_shortname'] = "NW"; |
1635
|
|
|
$temp_array['direction_fullname'] = "Northwest"; |
1636
|
|
|
} elseif ($direction >= 337.5 && $direction < 360){ |
1637
|
|
|
$temp_array['direction_degree'] = $direction; |
1638
|
|
|
$temp_array['direction_shortname'] = "NNW"; |
1639
|
|
|
$temp_array['direction_fullname'] = "North-Northwest"; |
1640
|
|
|
} |
1641
|
|
|
$direction_array[] = $temp_array; |
1642
|
|
|
return $direction_array; |
1643
|
|
|
} |
1644
|
|
|
|
1645
|
|
|
|
1646
|
|
|
/** |
1647
|
|
|
* Gets Country from latitude/longitude |
1648
|
|
|
* |
1649
|
|
|
* @param Float $latitude latitute of the flight |
1650
|
|
|
* @param Float $longitude longitute of the flight |
1651
|
|
|
* @return String the countrie |
1652
|
|
|
*/ |
1653
|
|
|
public function getCountryFromLatitudeLongitude($latitude,$longitude) |
1654
|
|
|
{ |
1655
|
|
|
global $globalDBdriver, $globalDebug; |
1656
|
|
|
$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
1657
|
|
|
$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); |
1658
|
|
|
|
1659
|
|
|
$Connection = new Connection($this->db); |
1660
|
|
|
if (!$Connection->tableExists('countries')) return ''; |
1661
|
|
|
|
1662
|
|
|
try { |
1663
|
|
|
/* |
1664
|
|
|
if ($globalDBdriver == 'mysql') { |
1665
|
|
|
//$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(:latitude :longitude)'), ogc_geom) LIMIT 1"; |
1666
|
|
|
$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(".$longitude.' '.$latitude.")'), ogc_geom) LIMIT 1"; |
1667
|
|
|
} |
1668
|
|
|
*/ |
1669
|
|
|
// This query seems to work both for MariaDB and PostgreSQL |
1670
|
|
|
$query = "SELECT name,iso2,iso3 FROM countries WHERE ST_Within(ST_GeomFromText('POINT(".$longitude." ".$latitude.")',4326), ogc_geom) LIMIT 1"; |
1671
|
|
|
|
1672
|
|
|
$sth = $this->db->prepare($query); |
1673
|
|
|
//$sth->execute(array(':latitude' => $latitude,':longitude' => $longitude)); |
1674
|
|
|
$sth->execute(); |
1675
|
|
|
|
1676
|
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC); |
1677
|
|
|
$sth->closeCursor(); |
1678
|
|
|
if (count($row) > 0) { |
1679
|
|
|
return $row; |
1680
|
|
|
} else return ''; |
1681
|
|
|
} catch (PDOException $e) { |
1682
|
|
|
if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n"; |
1683
|
|
|
return ''; |
1684
|
|
|
} |
1685
|
|
|
|
1686
|
|
|
} |
1687
|
|
|
|
1688
|
|
|
/** |
1689
|
|
|
* Gets Country from iso2 |
1690
|
|
|
* |
1691
|
|
|
* @param String $iso2 ISO2 country code |
1692
|
|
|
* @return String the countrie |
1693
|
|
|
*/ |
1694
|
|
|
public function getCountryFromISO2($iso2) |
1695
|
|
|
{ |
1696
|
|
|
global $globalDBdriver, $globalDebug; |
1697
|
|
|
$iso2 = filter_var($iso2,FILTER_SANITIZE_STRING); |
1698
|
|
|
|
1699
|
|
|
$Connection = new Connection($this->db); |
1700
|
|
|
if (!$Connection->tableExists('countries')) return ''; |
1701
|
|
|
|
1702
|
|
|
try { |
1703
|
|
|
$query = "SELECT name,iso2,iso3 FROM countries WHERE iso2 = :iso2 LIMIT 1"; |
1704
|
|
|
|
1705
|
|
|
$sth = $this->db->prepare($query); |
1706
|
|
|
$sth->execute(array(':iso2' => $iso2)); |
1707
|
|
|
|
1708
|
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC); |
1709
|
|
|
$sth->closeCursor(); |
1710
|
|
|
if (count($row) > 0) { |
1711
|
|
|
return $row; |
1712
|
|
|
} else return ''; |
1713
|
|
|
} catch (PDOException $e) { |
1714
|
|
|
if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n"; |
1715
|
|
|
return ''; |
1716
|
|
|
} |
1717
|
|
|
|
1718
|
|
|
} |
1719
|
|
|
|
1720
|
|
|
|
1721
|
|
|
/** |
1722
|
|
|
* Gets the short url from bit.ly |
1723
|
|
|
* |
1724
|
|
|
* @param String $url the full url |
1725
|
|
|
* @return String the bit.ly url |
1726
|
|
|
* |
1727
|
|
|
*/ |
1728
|
|
|
public function getBitlyURL($url) |
1729
|
|
|
{ |
1730
|
|
|
global $globalBitlyAccessToken; |
1731
|
|
|
|
1732
|
|
|
if ($globalBitlyAccessToken == '') return $url; |
1733
|
|
|
|
1734
|
|
|
$google_url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$globalBitlyAccessToken.'&longUrl='.$url; |
1735
|
|
|
|
1736
|
|
|
$ch = curl_init(); |
1737
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
1738
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
1739
|
|
|
curl_setopt($ch, CURLOPT_URL, $google_url); |
1740
|
|
|
$bitly_data = curl_exec($ch); |
1741
|
|
|
curl_close($ch); |
1742
|
|
|
|
1743
|
|
|
$bitly_data = json_decode($bitly_data); |
1744
|
|
|
$bitly_url = ''; |
1745
|
|
|
if ($bitly_data->status_txt = "OK"){ |
1746
|
|
|
$bitly_url = $bitly_data->data->url; |
1747
|
|
|
} |
1748
|
|
|
|
1749
|
|
|
return $bitly_url; |
1750
|
|
|
} |
1751
|
|
|
|
1752
|
|
|
|
1753
|
|
|
/** |
1754
|
|
|
* Gets all vessels types that have flown over |
1755
|
|
|
* |
1756
|
|
|
* @return Array the vessel type list |
1757
|
|
|
* |
1758
|
|
|
*/ |
1759
|
|
|
public function countAllMarineTypes($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array(),$year = '',$month = '',$day = '') |
1760
|
|
|
{ |
1761
|
|
|
global $globalDBdriver; |
1762
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1763
|
|
|
$query = "SELECT marine_output.type AS marine_type, COUNT(marine_output.type) AS marine_type_count |
1764
|
|
|
FROM marine_output ".$filter_query." marine_output.type <> ''"; |
1765
|
|
|
if ($olderthanmonths > 0) { |
1766
|
|
|
if ($globalDBdriver == 'mysql') { |
1767
|
|
|
$query .= ' AND marine_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$olderthanmonths.' MONTH)'; |
1768
|
|
|
} else { |
1769
|
|
|
$query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'"; |
1770
|
|
|
} |
1771
|
|
|
} |
1772
|
|
|
if ($sincedate != '') { |
1773
|
|
|
if ($globalDBdriver == 'mysql') { |
1774
|
|
|
$query .= " AND marine_output.date > '".$sincedate."'"; |
1775
|
|
|
} else { |
1776
|
|
|
$query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)"; |
1777
|
|
|
} |
1778
|
|
|
} |
1779
|
|
|
$query_values = array(); |
1780
|
|
|
if ($year != '') { |
1781
|
|
|
if ($globalDBdriver == 'mysql') { |
1782
|
|
|
$query .= " AND YEAR(marine_output.date) = :year"; |
1783
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1784
|
|
|
} else { |
1785
|
|
|
$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year"; |
1786
|
|
|
$query_values = array_merge($query_values,array(':year' => $year)); |
1787
|
|
|
} |
1788
|
|
|
} |
1789
|
|
|
if ($month != '') { |
1790
|
|
|
if ($globalDBdriver == 'mysql') { |
1791
|
|
|
$query .= " AND MONTH(marine_output.date) = :month"; |
1792
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1793
|
|
|
} else { |
1794
|
|
|
$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month"; |
1795
|
|
|
$query_values = array_merge($query_values,array(':month' => $month)); |
1796
|
|
|
} |
1797
|
|
|
} |
1798
|
|
|
if ($day != '') { |
1799
|
|
|
if ($globalDBdriver == 'mysql') { |
1800
|
|
|
$query .= " AND DAY(marine_output.date) = :day"; |
1801
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
1802
|
|
|
} else { |
1803
|
|
|
$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day"; |
1804
|
|
|
$query_values = array_merge($query_values,array(':day' => $day)); |
1805
|
|
|
} |
1806
|
|
|
} |
1807
|
|
|
$query .= " GROUP BY marine_output.type ORDER BY marine_type_count DESC"; |
1808
|
|
|
if ($limit) $query .= " LIMIT 10 OFFSET 0"; |
1809
|
|
|
$sth = $this->db->prepare($query); |
1810
|
|
|
$sth->execute($query_values); |
1811
|
|
|
$marine_array = array(); |
1812
|
|
|
$temp_array = array(); |
1813
|
|
|
while($row = $sth->fetch(PDO::FETCH_ASSOC)) |
1814
|
|
|
{ |
1815
|
|
|
$temp_array['marine_type'] = $row['marine_type']; |
1816
|
|
|
$temp_array['marine_type_count'] = $row['marine_type_count']; |
1817
|
|
|
$marine_array[] = $temp_array; |
1818
|
|
|
} |
1819
|
|
|
return $marine_array; |
1820
|
|
|
} |
1821
|
|
|
|
1822
|
|
|
/** |
1823
|
|
|
* Gets all the tracker information |
1824
|
|
|
* |
1825
|
|
|
* @return Array the tracker information |
1826
|
|
|
* |
1827
|
|
|
*/ |
1828
|
|
|
public function searchMarineData($q = '', $callsign = '',$mmsi = '', $imo = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '',$filters = array()) |
|
|
|
|
1829
|
|
|
{ |
1830
|
|
|
global $globalTimezone, $globalDBdriver; |
1831
|
|
|
date_default_timezone_set('UTC'); |
1832
|
|
|
$query_values = array(); |
1833
|
|
|
$additional_query = ''; |
1834
|
|
|
$filter_query = $this->getFilter($filters,true,true); |
1835
|
|
|
if ($q != "") |
1836
|
|
|
{ |
1837
|
|
|
if (!is_string($q)) |
1838
|
|
|
{ |
1839
|
|
|
return false; |
1840
|
|
|
} else { |
1841
|
|
|
$q_array = explode(" ", $q); |
1842
|
|
|
foreach ($q_array as $q_item){ |
1843
|
|
|
$q_item = filter_var($q_item,FILTER_SANITIZE_STRING); |
1844
|
|
|
$additional_query .= " AND ("; |
1845
|
|
|
if (is_int($q_item)) $additional_query .= "(marine_output.marine_id = '".$q_item."') OR "; |
1846
|
|
|
if (is_int($q_item)) $additional_query .= "(marine_output.mmsi = '".$q_item."') OR "; |
1847
|
|
|
if (is_int($q_item)) $additional_query .= "(marine_output.imo = '".$q_item."') OR "; |
1848
|
|
|
$additional_query .= "(marine_output.ident like '%".$q_item."%') OR "; |
1849
|
|
|
$additional_query .= ")"; |
1850
|
|
|
} |
1851
|
|
|
} |
1852
|
|
|
} |
1853
|
|
|
if ($callsign != "") |
1854
|
|
|
{ |
1855
|
|
|
$callsign = filter_var($callsign,FILTER_SANITIZE_STRING); |
1856
|
|
|
if (!is_string($callsign)) |
1857
|
|
|
{ |
1858
|
|
|
return false; |
1859
|
|
|
} else { |
1860
|
|
|
$additional_query .= " AND marine_output.ident = :callsign"; |
1861
|
|
|
$query_values = array_merge($query_values,array(':callsign' => $callsign)); |
1862
|
|
|
} |
1863
|
|
|
} |
1864
|
|
|
if ($mmsi != "") |
1865
|
|
|
{ |
1866
|
|
|
$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING); |
1867
|
|
|
if (!is_numeric($mmsi)) |
1868
|
|
|
{ |
1869
|
|
|
return false; |
1870
|
|
|
} else { |
1871
|
|
|
$additional_query .= " AND marine_output.mmsi = :mmsi"; |
1872
|
|
|
$query_values = array_merge($query_values,array(':mmsi' => $mmsi)); |
1873
|
|
|
} |
1874
|
|
|
} |
1875
|
|
|
if ($imo != "") |
1876
|
|
|
{ |
1877
|
|
|
$imo = filter_var($imo,FILTER_SANITIZE_STRING); |
1878
|
|
|
if (!is_numeric($imo)) |
1879
|
|
|
{ |
1880
|
|
|
return false; |
1881
|
|
|
} else { |
1882
|
|
|
$additional_query .= " AND marine_output.imo = :imo"; |
1883
|
|
|
$query_values = array_merge($query_values,array(':imo' => $imo)); |
1884
|
|
|
} |
1885
|
|
|
} |
1886
|
|
|
if ($date_posted != "") |
1887
|
|
|
{ |
1888
|
|
|
$date_array = explode(",", $date_posted); |
1889
|
|
|
$date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING); |
1890
|
|
|
$date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING); |
1891
|
|
|
if ($globalTimezone != '') { |
1892
|
|
|
date_default_timezone_set($globalTimezone); |
1893
|
|
|
$datetime = new DateTime(); |
1894
|
|
|
$offset = $datetime->format('P'); |
1895
|
|
|
} else $offset = '+00:00'; |
1896
|
|
|
if ($date_array[1] != "") |
1897
|
|
|
{ |
1898
|
|
|
$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0])); |
1899
|
|
|
$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1])); |
1900
|
|
|
if ($globalDBdriver == 'mysql') { |
1901
|
|
|
$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]."' "; |
1902
|
|
|
} else { |
1903
|
|
|
$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]."' "; |
1904
|
|
|
} |
1905
|
|
|
} else { |
1906
|
|
|
$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0])); |
1907
|
|
|
if ($globalDBdriver == 'mysql') { |
1908
|
|
|
$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' "; |
1909
|
|
|
} else { |
1910
|
|
|
$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' "; |
1911
|
|
|
} |
1912
|
|
|
} |
1913
|
|
|
} |
1914
|
|
|
if ($limit != "") |
1915
|
|
|
{ |
1916
|
|
|
$limit_array = explode(",", $limit); |
1917
|
|
|
$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT); |
1918
|
|
|
$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT); |
1919
|
|
|
if ($limit_array[0] >= 0 && $limit_array[1] >= 0) |
1920
|
|
|
{ |
1921
|
|
|
$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0]; |
1922
|
|
|
} else $limit_query = ""; |
1923
|
|
|
} else $limit_query = ""; |
1924
|
|
|
if ($sort != "") |
1925
|
|
|
{ |
1926
|
|
|
$search_orderby_array = $this->getOrderBy(); |
1927
|
|
|
$orderby_query = $search_orderby_array[$sort]['sql']; |
1928
|
|
|
} else { |
1929
|
|
|
if ($origLat != "" && $origLon != "" && $dist != "") { |
1930
|
|
|
$orderby_query = " ORDER BY distance ASC"; |
1931
|
|
|
} else { |
1932
|
|
|
$orderby_query = " ORDER BY marine_output.date DESC"; |
1933
|
|
|
} |
1934
|
|
|
} |
1935
|
|
|
if ($origLat != "" && $origLon != "" && $dist != "") { |
1936
|
|
|
$dist = number_format($dist*0.621371,2,'.',''); // convert km to mile |
1937
|
|
|
if ($globalDBdriver == 'mysql') { |
1938
|
|
|
$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 |
1939
|
|
|
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)) |
1940
|
|
|
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; |
1941
|
|
|
} else { |
1942
|
|
|
$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 |
1943
|
|
|
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)) |
1944
|
|
|
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; |
1945
|
|
|
} |
1946
|
|
|
} else { |
1947
|
|
|
$query = "SELECT marine_output.* FROM marine_output".$filter_query." marine_output.ident <> '' |
1948
|
|
|
".$additional_query." |
1949
|
|
|
".$orderby_query; |
1950
|
|
|
} |
1951
|
|
|
$marine_array = $this->getDataFromDB($query, $query_values,$limit_query); |
1952
|
|
|
return $marine_array; |
1953
|
|
|
} |
1954
|
|
|
|
1955
|
|
|
public function getOrderBy() |
1956
|
|
|
{ |
1957
|
|
|
$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")); |
1958
|
|
|
|
1959
|
|
|
return $orderby; |
1960
|
|
|
|
1961
|
|
|
} |
1962
|
|
|
|
1963
|
|
|
} |
1964
|
|
|
?> |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.