Completed
Push — master ( f566c3...1577fa )
by Yannick
07:04
created

getAltitudeSpeedArchiveMarineDataById()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 3
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
class MarineArchive {
3
	public $global_query = "SELECT marine_archive.* FROM marine_archive";
4
	public $db;
5
6
	public function __construct($dbc = null) {
7
		$Connection = new Connection($dbc);
8
		$this->db = $Connection->db;
9
	}
10
11
	/**
12
	* Get SQL query part for filter used
13
	* @param Array $filter the filter
14
	* @return Array the SQL part
15
	*/
16
	public function getFilter($filter = array(),$where = false,$and = false) {
17
		global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver;
18
		$filters = array();
19
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
20
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
21
				$filters = $globalStatsFilters[$globalFilterName];
22
			} else {
23
				$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]);
24
			}
25
		}
26
		if (isset($filter[0]['source'])) {
27
			$filters = array_merge($filters,$filter);
28
		}
29
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
30
		$filter_query_join = '';
31
		$filter_query_where = '';
32
		foreach($filters as $flt) {
33
			if (isset($flt['idents']) && !empty($flt['idents'])) {
34
				if (isset($flt['source'])) {
35
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_archive_output WHERE marine_archive_output.ident IN ('".implode("','",$flt['idents'])."') AND marine_archive_output.format_source IN ('".implode("','",$flt['source'])."')) spid ON spid.fammarine_id = marine_archive.fammarine_id";
36
				} else {
37
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_archive_output WHERE marine_archive_output.ident IN ('".implode("','",$flt['idents'])."')) spid ON spid.fammarine_id = marine_archive.fammarine_id";
38
				}
39
			}
40
		}
41
		if (isset($filter['source']) && !empty($filter['source'])) {
42
			$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
43
		}
44
		if (isset($filter['ident']) && !empty($filter['ident'])) {
45
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
46
		}
47
		if ((isset($filter['year']) && $filter['year'] != '') || (isset($filter['month']) && $filter['month'] != '') || (isset($filter['day']) && $filter['day'] != '')) {
48
			$filter_query_date = '';
49
			if (isset($filter['year']) && $filter['year'] != '') {
50
				if ($globalDBdriver == 'mysql') {
51
					$filter_query_date .= " AND YEAR(marine_archive_output.date) = '".$filter['year']."'";
52
				} else {
53
					$filter_query_date .= " AND EXTRACT(YEAR FROM marine_archive_output.date) = '".$filter['year']."'";
54
				}
55
			}
56
			if (isset($filter['month']) && $filter['month'] != '') {
57
				if ($globalDBdriver == 'mysql') {
58
					$filter_query_date .= " AND MONTH(marine_archive_output.date) = '".$filter['month']."'";
59
				} else {
60
					$filter_query_date .= " AND EXTRACT(MONTH FROM marine_archive_output.date) = '".$filter['month']."'";
61
				}
62
			}
63
			if (isset($filter['day']) && $filter['day'] != '') {
64
				if ($globalDBdriver == 'mysql') {
65
					$filter_query_date .= " AND DAY(marine_archive_output.date) = '".$filter['day']."'";
66
				} else {
67
					$filter_query_date .= " AND EXTRACT(DAY FROM marine_archive_output.date) = '".$filter['day']."'";
68
				}
69
			}
70
			$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_archive_output".preg_replace('/^ AND/',' WHERE',$filter_query_date).") sd ON sd.fammarine_id = marine_archive.fammarine_id";
71
		}
72
		if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
73
			$filter_query_where .= " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
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
	// marine_archive
85
	public function addMarineArchiveData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $heading = '', $groundspeed = '', $date = '', $putinarchive = false, $mmsi = '',$type = '',$typeid = '',$imo = '', $callsign = '',$arrival_code = '',$arrival_date = '',$status = '',$noarchive = false,$format_source = '', $source_name = '', $over_country = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $putinarchive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $typeid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $callsign is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $noarchive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
		/*
87
		require_once(dirname(__FILE__).'/class.Marine.php');
88
		if ($over_country == '') {
89
			$Marine = new Marine($this->db);
90
			$data_country = $Marine->getCountryFromLatitudeLongitude($latitude,$longitude);
91
			if (!empty($data_country)) $country = $data_country['iso2'];
92
			else $country = '';
93
		} else $country = $over_country;
94
		*/
95
		$country = $over_country;
0 ignored issues
show
Unused Code introduced by
$country is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
96
		// Route is not added in marine_archive
97
		$query  = 'INSERT INTO marine_archive (fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, over_country, mmsi, type,status,imo,arrival_port_name,arrival_port_date) 
98
		    VALUES (:fammarine_id,:ident,:latitude,:longitude,:heading,:groundspeed,:date,:format_source, :source_name, :over_country,:mmsi,:type,:status,:imo,:arrival_port_name,:arrival_port_date)';
99
100
		$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':heading' => $heading,':groundspeed' => $groundspeed,':date' => $date, ':format_source' => $format_source, ':source_name' => $source_name, ':over_country' => $over_country,':mmsi' => $mmsi,':type' => $type,':status' => $status,':imo' => $imo,':arrival_port_name' => $arrival_code,':arrival_port_date' => $arrival_date);
101
102
		try {
103
			$sth = $this->db->prepare($query);
104
			$sth->execute($query_values);
105
			$sth->closeCursor();
106
		} catch(PDOException $e) {
107
			return "error : ".$e->getMessage();
108
		}
109
		return "success";
110
	}
111
112
113
        /**
114
        * Gets all the spotter information based on a particular callsign
115
        *
116
        * @return Array the spotter information
117
        *
118
        */
119
        public function getLastArchiveMarineDataByIdent($ident)
120
        {
121
		$Marine = new Marine($this->db);
122
                date_default_timezone_set('UTC');
123
124
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
125
                //$query  = "SELECT marine_archive.* FROM marine_archive INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE l.ident = :ident GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate LIMIT 1";
126
                $query  = "SELECT marine_archive.* FROM marine_archive WHERE ident = :ident ORDER BY date DESC LIMIT 1";
127
128
                $spotter_array = $Marine->getDataFromDB($query,array(':ident' => $ident));
129
130
                return $spotter_array;
131
        }
132
133
134
        /**
135
        * Gets last the spotter information based on a particular id
136
        *
137
        * @return Array the spotter information
138
        *
139
        */
140
        public function getLastArchiveMarineDataById($id)
141
        {
142
    		$Marine = new Marine($this->db);
143
                date_default_timezone_set('UTC');
144
                $id = filter_var($id, FILTER_SANITIZE_STRING);
145
                //$query  = MarineArchive->$global_query." WHERE marine_archive.fammarine_id = :id";
146
                //$query  = "SELECT marine_archive.* FROM marine_archive INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE l.fammarine_id = :id GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate LIMIT 1";
147
                $query  = "SELECT * FROM marine_archive WHERE fammarine_id = :id ORDER BY date DESC LIMIT 1";
148
149
//              $spotter_array = Marine->getDataFromDB($query,array(':id' => $id));
150
                  /*
151
                try {
152
                        $Connection = new Connection();
153
                        $sth = Connection->$db->prepare($query);
154
                        $sth->execute(array(':id' => $id));
155
                } catch(PDOException $e) {
156
                        return "error";
157
                }
158
                $spotter_array = $sth->fetchAll(PDO->FETCH_ASSOC);
159
                */
160
                $spotter_array = $Marine->getDataFromDB($query,array(':id' => $id));
161
162
                return $spotter_array;
163
        }
164
165
        /**
166
        * Gets all the spotter information based on a particular id
167
        *
168
        * @return Array the spotter information
169
        *
170
        */
171
        public function getAllArchiveMarineDataById($id)
172
	{
173
                date_default_timezone_set('UTC');
174
                $id = filter_var($id, FILTER_SANITIZE_STRING);
175
                $query  = $this->global_query." WHERE marine_archive.fammarine_id = :id ORDER BY date";
176
177
//              $spotter_array = Marine->getDataFromDB($query,array(':id' => $id));
178
179
                try {
180
                        $sth = $this->db->prepare($query);
181
                        $sth->execute(array(':id' => $id));
182
                } catch(PDOException $e) {
183
                        echo $e->getMessage();
184
                        die;
185
                }
186
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
187
188
                return $spotter_array;
189
        }
190
191
        /**
192
        * Gets coordinate & time spotter information based on a particular id
193
        *
194
        * @return Array the spotter information
195
        *
196
        */
197
        public function getCoordArchiveMarineDataById($id)
198
        {
199
                date_default_timezone_set('UTC');
200
                $id = filter_var($id, FILTER_SANITIZE_STRING);
201
                $query  = "SELECT marine_archive.latitude, marine_archive.longitude, marine_archive.date FROM marine_archive WHERE marine_archive.fammarine_id = :id";
202
203
//              $spotter_array = Marine->getDataFromDB($query,array(':id' => $id));
204
205
                try {
206
                        $sth = $this->db->prepare($query);
207
                        $sth->execute(array(':id' => $id));
208
                } catch(PDOException $e) {
209
                        echo $e->getMessage();
210
                        die;
211
                }
212
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
213
214
                return $spotter_array;
215
        }
216
217
218
        /**
219
        * Gets altitude information based on a particular callsign
220
        *
221
        * @return Array the spotter information
222
        *
223
        */
224
        public function getAltitudeArchiveMarineDataByIdent($ident)
225
        {
226
227
                date_default_timezone_set('UTC');
228
229
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
230
                $query  = "SELECT marine_archive.altitude, marine_archive.date FROM marine_archive WHERE marine_archive.ident = :ident AND marine_archive.latitude <> 0 AND marine_archive.longitude <> 0 ORDER BY date";
231
232
                try {
233
                        $sth = $this->db->prepare($query);
234
                        $sth->execute(array(':ident' => $ident));
235
                } catch(PDOException $e) {
236
                        echo $e->getMessage();
237
                        die;
238
                }
239
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
240
241
                return $spotter_array;
242
        }
243
244
        /**
245
        * Gets altitude information based on a particular id
246
        *
247
        * @return Array the spotter information
248
        *
249
        */
250
        public function getAltitudeArchiveMarineDataById($id)
251
        {
252
253
                date_default_timezone_set('UTC');
254
255
                $id = filter_var($id, FILTER_SANITIZE_STRING);
256
                $query  = "SELECT marine_archive.altitude, marine_archive.date FROM marine_archive WHERE marine_archive.fammarine_id = :id AND marine_archive.latitude <> 0 AND marine_archive.longitude <> 0 ORDER BY date";
257
258
                try {
259
                        $sth = $this->db->prepare($query);
260
                        $sth->execute(array(':id' => $id));
261
                } catch(PDOException $e) {
262
                        echo $e->getMessage();
263
                        die;
264
                }
265
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
266
267
                return $spotter_array;
268
        }
269
270
        /**
271
        * Gets altitude & speed information based on a particular id
272
        *
273
        * @return Array the spotter information
274
        *
275
        */
276
        public function getAltitudeSpeedArchiveMarineDataById($id)
277
        {
278
279
                date_default_timezone_set('UTC');
280
281
                $id = filter_var($id, FILTER_SANITIZE_STRING);
282
                $query  = "SELECT marine_archive.altitude, marine_archive.ground_speed, marine_archive.date FROM marine_archive WHERE marine_archive.fammarine_id = :id ORDER BY date";
283
284
                try {
285
                        $sth = $this->db->prepare($query);
286
                        $sth->execute(array(':id' => $id));
287
                } catch(PDOException $e) {
288
                        echo $e->getMessage();
289
                        die;
290
                }
291
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
292
293
                return $spotter_array;
294
        }
295
296
297
        /**
298
        * Gets altitude information based on a particular callsign
299
        *
300
        * @return Array the spotter information
301
        *
302
        */
303
        public function getLastAltitudeArchiveMarineDataByIdent($ident)
304
        {
305
306
                date_default_timezone_set('UTC');
307
308
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
309
                $query  = "SELECT marine_archive.altitude, marine_archive.date FROM marine_archive INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE l.ident = :ident GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate LIMIT 1";
310
//                $query  = "SELECT marine_archive.altitude, marine_archive.date FROM marine_archive WHERE marine_archive.ident = :ident";
311
312
                try {
313
                        $sth = $this->db->prepare($query);
314
                        $sth->execute(array(':ident' => $ident));
315
                } catch(PDOException $e) {
316
                        echo $e->getMessage();
317
                        die;
318
                }
319
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
320
321
                return $spotter_array;
322
        }
323
324
325
326
       /**
327
        * Gets all the archive spotter information
328
        *
329
        * @return Array the spotter information
330
        *
331
        */
332
        public function getMarineArchiveData($ident,$fammarine_id,$date)
333
        {
334
    		$Marine = new Marine($this->db);
335
                $ident = filter_var($ident, FILTER_SANITIZE_STRING);
336
                $query  = "SELECT spotter_live.* FROM spotter_live INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM spotter_live l WHERE l.ident = :ident AND l.fammarine_id = :fammarine_id AND l.date LIKE :date GROUP BY l.fammarine_id) s on spotter_live.fammarine_id = s.fammarine_id AND spotter_live.date = s.maxdate";
337
338
                $spotter_array = $Marine->getDataFromDB($query,array(':ident' => $ident,':fammarine_id' => $fammarine_id,':date' => $date.'%'));
339
340
                return $spotter_array;
341
        }
342
        
343
        public function deleteMarineArchiveTrackData()
344
        {
345
		global $globalArchiveKeepTrackMonths;
346
                date_default_timezone_set('UTC');
347
		$query = 'DELETE FROM marine_archive WHERE marine_archive.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveKeepTrackMonths.' MONTH)';
348
                try {
349
                        $sth = $this->db->prepare($query);
350
                        $sth->execute();
351
                } catch(PDOException $e) {
352
                        echo $e->getMessage();
353
                        die;
354
                }
355
	}
356
357
	/**
358
        * Gets Minimal Live Marine data
359
        *
360
        * @return Array the spotter information
361
        *
362
        */
363
        public function getMinLiveMarineData($begindate,$enddate,$filter = array())
364
        {
365
                global $globalDBdriver, $globalLiveInterval;
366
                date_default_timezone_set('UTC');
367
368
                $filter_query = '';
369
                if (isset($filter['source']) && !empty($filter['source'])) {
370
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
371
                }
372
                // Use spotter_output also ?
373
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
374
                        $filter_query .= " INNER JOIN (SELECT fammarine_id FROM marine_archive_output WHERE marine_archive_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.fammarine_id = marine_archive.fammarine_id ";
375
                }
376
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
377
                        $filter_query .= " INNER JOIN (SELECT fammarine_id FROM marine_archive_output WHERE marine_archive_output.airline_type = '".$filter['airlinestype']."') sa ON sa.fammarine_id = marine_archive.fammarine_id ";
378
                }
379
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
380
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
381
                }
382
383
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
384
                if ($globalDBdriver == 'mysql') {
385
                        /*
386
                        $query  = 'SELECT a.aircraft_shadow, marine_archive.ident, marine_archive.fammarine_id, marine_archive.aircraft_icao, marine_archive.departure_airport_icao as departure_airport, marine_archive.arrival_airport_icao as arrival_airport, marine_archive.latitude, marine_archive.longitude, marine_archive.altitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.squawk 
387
                    		    FROM marine_archive 
388
                    		    INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE (l.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON marine_archive.aircraft_icao = a.icao';
389
			*/
390
/*
391
			$query  = 'SELECT a.aircraft_shadow, marine_archive.ident, marine_archive.fammarine_id, marine_archive.aircraft_icao, marine_archive.departure_airport_icao as departure_airport, marine_archive.arrival_airport_icao as arrival_airport, marine_archive.latitude, marine_archive.longitude, marine_archive.altitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.squawk 
392
				    FROM marine_archive 
393
				    INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate 
394
						FROM marine_archive l 
395
						WHERE (l.date BETWEEN DATE_SUB('."'".$begindate."'".',INTERVAL '.$globalLiveInterval.' SECOND) AND '."'".$begindate."'".') 
396
						GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id 
397
				    AND marine_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON marine_archive.aircraft_icao = a.icao';
398
*/
399
			$query  = 'SELECT marine_archive.date,marine_archive.fammarine_id, marine_archive.ident, marine_archive.aircraft_icao, marine_archive.departure_airport_icao as departure_airport, marine_archive.arrival_airport_icao as arrival_airport, marine_archive.latitude, marine_archive.longitude, marine_archive.altitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.squawk, a.aircraft_shadow,a.engine_type, a.engine_count, a.wake_category 
400
				    FROM marine_archive 
401
				    INNER JOIN (SELECT * FROM aircraft) a on marine_archive.aircraft_icao = a.icao
402
				    WHERE marine_archive.date BETWEEN '."'".$begindate."'".' AND '."'".$begindate."'".' 
403
                        	    '.$filter_query.' ORDER BY fammarine_id';
404
                } else {
405
                        //$query  = 'SELECT marine_archive.ident, marine_archive.fammarine_id, marine_archive.aircraft_icao, marine_archive.departure_airport_icao as departure_airport, marine_archive.arrival_airport_icao as arrival_airport, marine_archive.latitude, marine_archive.longitude, marine_archive.altitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.squawk, a.aircraft_shadow FROM marine_archive INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate '.$filter_query.'INNER JOIN (SELECT * FROM aircraft) a on marine_archive.aircraft_icao = a.icao';
406
                        $query  = 'SELECT marine_archive.date,marine_archive.fammarine_id, marine_archive.ident, marine_archive.aircraft_icao, marine_archive.departure_airport_icao as departure_airport, marine_archive.arrival_airport_icao as arrival_airport, marine_archive.latitude, marine_archive.longitude, marine_archive.altitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.squawk, a.aircraft_shadow,a.engine_type, a.engine_count, a.wake_category 
407
                        	    FROM marine_archive 
408
                        	    INNER JOIN (SELECT * FROM aircraft) a on marine_archive.aircraft_icao = a.icao
409
                        	    WHERE marine_archive.date >= '."'".$begindate."'".' AND marine_archive.date <= '."'".$enddate."'".'
410
                        	    '.$filter_query.' ORDER BY fammarine_id';
411
                }
412
                //echo $query;
413
                try {
414
                        $sth = $this->db->prepare($query);
415
                        $sth->execute();
416
                } catch(PDOException $e) {
417
                        echo $e->getMessage();
418
                        die;
419
                }
420
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
421
422
                return $spotter_array;
423
        }
424
425
	/**
426
        * Gets Minimal Live Marine data
427
        *
428
        * @return Array the spotter information
429
        *
430
        */
431
        public function getMinLiveMarineDataPlayback($begindate,$enddate,$filter = array())
432
        {
433
                global $globalDBdriver, $globalLiveInterval;
434
                date_default_timezone_set('UTC');
435
436
                $filter_query = '';
437
                if (isset($filter['source']) && !empty($filter['source'])) {
438
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
439
                }
440
                // Should use spotter_output also ?
441
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
442
                        $filter_query .= " INNER JOIN (SELECT fammarine_id FROM marine_archive_output WHERE marine_archive_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.fammarine_id = marine_archive.fammarine_id ";
443
                }
444
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
445
                        $filter_query .= " INNER JOIN (SELECT fammarine_id FROM marine_archive_output WHERE marine_archive_output.airline_type = '".$filter['airlinestype']."') sa ON sa.fammarine_id = marine_archive.fammarine_id ";
446
                }
447
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
448
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
449
                }
450
451
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
452
                if ($globalDBdriver == 'mysql') {
453
                        /*
454
                        $query  = 'SELECT a.aircraft_shadow, marine_archive.ident, marine_archive.fammarine_id, marine_archive.aircraft_icao, marine_archive.departure_airport_icao as departure_airport, marine_archive.arrival_airport_icao as arrival_airport, marine_archive.latitude, marine_archive.longitude, marine_archive.altitude, marine_archive.heading, marine_archive.ground_speed, marine_archive.squawk 
455
                    		    FROM marine_archive 
456
                    		    INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE (l.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate '.$filter_query.'LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON marine_archive.aircraft_icao = a.icao';
457
			*/
458
			$query  = 'SELECT a.aircraft_shadow, marine_archive_output.ident, marine_archive_output.fammarine_id, marine_archive_output.aircraft_icao, marine_archive_output.departure_airport_icao as departure_airport, marine_archive_output.arrival_airport_icao as arrival_airport, marine_archive_output.latitude, marine_archive_output.longitude, marine_archive_output.altitude, marine_archive_output.heading, marine_archive_output.ground_speed, marine_archive_output.squawk 
459
				    FROM marine_archive_output 
460
				    LEFT JOIN (SELECT aircraft_shadow,icao FROM aircraft) a ON marine_archive_output.aircraft_icao = a.icao 
461
				    WHERE (marine_archive_output.date BETWEEN '."'".$begindate."'".' AND '."'".$enddate."'".') 
462
                        	    '.$filter_query.' GROUP BY marine_archive_output.fammarine_id, marine_archive_output.ident, marine_archive_output.aircraft_icao, marine_archive_output.departure_airport_icao, marine_archive_output.arrival_airport_icao, marine_archive_output.latitude, marine_archive_output.longitude, marine_archive_output.altitude, marine_archive_output.heading, marine_archive_output.ground_speed, marine_archive_output.squawk, a.aircraft_shadow';
463
464
                } else {
465
                        //$query  = 'SELECT marine_archive_output.ident, marine_archive_output.fammarine_id, marine_archive_output.aircraft_icao, marine_archive_output.departure_airport_icao as departure_airport, marine_archive_output.arrival_airport_icao as arrival_airport, marine_archive_output.latitude, marine_archive_output.longitude, marine_archive_output.altitude, marine_archive_output.heading, marine_archive_output.ground_speed, marine_archive_output.squawk, a.aircraft_shadow FROM marine_archive_output INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive_output l WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$globalLiveInterval.' SECOND) <= l.date GROUP BY l.fammarine_id) s on marine_archive_output.fammarine_id = s.fammarine_id AND marine_archive_output.date = s.maxdate '.$filter_query.'INNER JOIN (SELECT * FROM aircraft) a on marine_archive_output.aircraft_icao = a.icao';
466
                       /*
467
                        $query  = 'SELECT marine_archive_output.ident, marine_archive_output.fammarine_id, marine_archive_output.aircraft_icao, marine_archive_output.departure_airport_icao as departure_airport, marine_archive_output.arrival_airport_icao as arrival_airport, marine_archive_output.latitude, marine_archive_output.longitude, marine_archive_output.altitude, marine_archive_output.heading, marine_archive_output.ground_speed, marine_archive_output.squawk, a.aircraft_shadow
468
                        	    FROM marine_archive_output 
469
                        	    INNER JOIN (SELECT * FROM aircraft) a on marine_archive_output.aircraft_icao = a.icao
470
                        	    WHERE marine_archive_output.date >= '."'".$begindate."'".' AND marine_archive_output.date <= '."'".$enddate."'".'
471
                        	    '.$filter_query.' GROUP BY marine_archive_output.fammarine_id, marine_archive_output.ident, marine_archive_output.aircraft_icao, marine_archive_output.departure_airport_icao, marine_archive_output.arrival_airport_icao, marine_archive_output.latitude, marine_archive_output.longitude, marine_archive_output.altitude, marine_archive_output.heading, marine_archive_output.ground_speed, marine_archive_output.squawk, a.aircraft_shadow';
472
                        */
473
                        $query  = 'SELECT DISTINCT marine_archive_output.fammarine_id, marine_archive_output.ident, marine_archive_output.aircraft_icao, marine_archive_output.departure_airport_icao as departure_airport, marine_archive_output.arrival_airport_icao as arrival_airport, marine_archive_output.latitude, marine_archive_output.longitude, marine_archive_output.altitude, marine_archive_output.heading, marine_archive_output.ground_speed, marine_archive_output.squawk, a.aircraft_shadow
474
                        	    FROM marine_archive_output 
475
                        	    INNER JOIN (SELECT * FROM aircraft) a on marine_archive_output.aircraft_icao = a.icao
476
                        	    WHERE marine_archive_output.date >= '."'".$begindate."'".' AND marine_archive_output.date <= '."'".$enddate."'".'
477
                        	    '.$filter_query.' LIMIT 200 OFFSET 0';
478
//                        	    .' GROUP BY spotter_output.fammarine_id, spotter_output.ident, spotter_output.aircraft_icao, spotter_output.departure_airport_icao, spotter_output.arrival_airport_icao, spotter_output.latitude, spotter_output.longitude, spotter_output.altitude, spotter_output.heading, spotter_output.ground_speed, spotter_output.squawk, a.aircraft_shadow';
479
                        	    
480
                }
481
                //echo $query;
482
                try {
483
                        $sth = $this->db->prepare($query);
484
                        $sth->execute();
485
                } catch(PDOException $e) {
486
                        echo $e->getMessage();
487
                        die;
488
                }
489
                $spotter_array = $sth->fetchAll(PDO::FETCH_ASSOC);
490
491
                return $spotter_array;
492
        }
493
494
	 /**
495
        * Gets count Live Marine data
496
        *
497
        * @return Array the spotter information
498
        *
499
        */
500
        public function getLiveMarineCount($begindate,$enddate,$filter = array())
501
        {
502
                global $globalDBdriver, $globalLiveInterval;
503
                date_default_timezone_set('UTC');
504
505
                $filter_query = '';
506
                if (isset($filter['source']) && !empty($filter['source'])) {
507
                        $filter_query .= " AND format_source IN ('".implode("','",$filter['source'])."') ";
508
                }
509
                if (isset($filter['airlines']) && !empty($filter['airlines'])) {
510
                        $filter_query .= " INNER JOIN (SELECT fammarine_id FROM spotter_output WHERE spotter_output.airline_icao IN ('".implode("','",$filter['airlines'])."')) so ON so.fammarine_id = marine_archive.fammarine_id ";
511
                }
512
                if (isset($filter['airlinestype']) && !empty($filter['airlinestype'])) {
513
                        $filter_query .= " INNER JOIN (SELECT fammarine_id FROM spotter_output WHERE spotter_output.airline_type = '".$filter['airlinestype']."') sa ON sa.fammarine_id = marine_archive.fammarine_id ";
514
                }
515
                if (isset($filter['source_aprs']) && !empty($filter['source_aprs'])) {
516
                        $filter_query = " AND format_source = 'aprs' AND source_name IN ('".implode("','",$filter['source_aprs'])."')";
517
                }
518
519
                //if (!isset($globalLiveInterval)) $globalLiveInterval = '200';
520
                if ($globalDBdriver == 'mysql') {
521
			$query = 'SELECT COUNT(DISTINCT fammarine_id) as nb 
522
			FROM marine_archive l 
523
			WHERE (l.date BETWEEN DATE_SUB('."'".$begindate."'".',INTERVAL '.$globalLiveInterval.' SECOND) AND '."'".$begindate."'".')'.$filter_query;
524
                } else {
525
			$query = 'SELECT COUNT(DISTINCT fammarine_id) as nb FROM marine_archive l WHERE (l.date BETWEEN '."'".$begindate."' - INTERVAL '".$globalLiveInterval." SECONDS' AND "."'".$enddate."'".')'.$filter_query;
526
                }
527
                //echo $query;
528
                try {
529
                        $sth = $this->db->prepare($query);
530
                        $sth->execute();
531
                } catch(PDOException $e) {
532
                        echo $e->getMessage();
533
                        die;
534
                }
535
		$result = $sth->fetch(PDO::FETCH_ASSOC);
536
		$sth->closeCursor();
537
                return $result['nb'];
538
539
        }
540
541
542
543
	// marine_archive_output
544
	
545
    /**
546
    * Gets all the spotter information
547
    *
548
    * @return Array the spotter information
549
    *
550
    */
551
    public function searchMarineData($q = '', $registration = '', $aircraft_icao = '', $aircraft_manufacturer = '', $highlights = '', $airline_icao = '', $airline_country = '', $airline_type = '', $airport = '', $airport_country = '', $callsign = '', $departure_airport_route = '', $arrival_airport_route = '', $owner = '',$pilot_id = '',$pilot_name = '',$altitude = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '', $filters=array())
552
    {
553
	global $globalTimezone, $globalDBdriver;
554
	require_once(dirname(__FILE__).'/class.Translation.php');
555
	$Translation = new Translation();
556
	$Marine = new Marine($this->db);
557
558
	date_default_timezone_set('UTC');
559
	
560
	$query_values = array();
561
	$additional_query = '';
562
	$limit_query = '';
563
	$filter_query = $this->getFilter($filters);
564
	if ($q != "")
565
	{
566
	    if (!is_string($q))
567
	    {
568
		return false;
569
	    } else {
570
	        
571
		$q_array = explode(" ", $q);
572
		
573
		foreach ($q_array as $q_item){
574
		    $additional_query .= " AND (";
575
		    $additional_query .= "(marine_archive_output.spotter_id like '%".$q_item."%') OR ";
576
		    $additional_query .= "(marine_archive_output.aircraft_icao like '%".$q_item."%') OR ";
577
		    $additional_query .= "(marine_archive_output.aircraft_name like '%".$q_item."%') OR ";
578
		    $additional_query .= "(marine_archive_output.aircraft_manufacturer like '%".$q_item."%') OR ";
579
		    $additional_query .= "(marine_archive_output.airline_icao like '%".$q_item."%') OR ";
580
		    $additional_query .= "(marine_archive_output.airline_name like '%".$q_item."%') OR ";
581
		    $additional_query .= "(marine_archive_output.airline_country like '%".$q_item."%') OR ";
582
		    $additional_query .= "(marine_archive_output.departure_airport_icao like '%".$q_item."%') OR ";
583
		    $additional_query .= "(marine_archive_output.departure_airport_name like '%".$q_item."%') OR ";
584
		    $additional_query .= "(marine_archive_output.departure_airport_city like '%".$q_item."%') OR ";
585
		    $additional_query .= "(marine_archive_output.departure_airport_country like '%".$q_item."%') OR ";
586
		    $additional_query .= "(marine_archive_output.arrival_airport_icao like '%".$q_item."%') OR ";
587
		    $additional_query .= "(marine_archive_output.arrival_airport_name like '%".$q_item."%') OR ";
588
		    $additional_query .= "(marine_archive_output.arrival_airport_city like '%".$q_item."%') OR ";
589
		    $additional_query .= "(marine_archive_output.arrival_airport_country like '%".$q_item."%') OR ";
590
		    $additional_query .= "(marine_archive_output.registration like '%".$q_item."%') OR ";
591
		    $additional_query .= "(marine_archive_output.owner_name like '%".$q_item."%') OR ";
592
		    $additional_query .= "(marine_archive_output.pilot_id like '%".$q_item."%') OR ";
593
		    $additional_query .= "(marine_archive_output.pilot_name like '%".$q_item."%') OR ";
594
		    $additional_query .= "(marine_archive_output.ident like '%".$q_item."%') OR ";
595
		    $translate = $Translation->ident2icao($q_item);
596
		    if ($translate != $q_item) $additional_query .= "(marine_archive_output.ident like '%".$translate."%') OR ";
597
		    $additional_query .= "(marine_archive_output.highlight like '%".$q_item."%')";
598
		    $additional_query .= ")";
599
		}
600
	    }
601
	}
602
	
603
	if ($registration != "")
604
	{
605
	    $registration = filter_var($registration,FILTER_SANITIZE_STRING);
606
	    if (!is_string($registration))
607
	    {
608
		return false;
609
	    } else {
610
		$additional_query .= " AND (marine_archive_output.registration = '".$registration."')";
611
	    }
612
	}
613
	
614
	if ($aircraft_icao != "")
615
	{
616
	    $aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING);
617
	    if (!is_string($aircraft_icao))
618
	    {
619
		return false;
620
	    } else {
621
		$additional_query .= " AND (marine_archive_output.aircraft_icao = '".$aircraft_icao."')";
622
	    }
623
	}
624
	
625
	if ($aircraft_manufacturer != "")
626
	{
627
	    $aircraft_manufacturer = filter_var($aircraft_manufacturer,FILTER_SANITIZE_STRING);
628
	    if (!is_string($aircraft_manufacturer))
629
	    {
630
		return false;
631
	    } else {
632
		$additional_query .= " AND (marine_archive_output.aircraft_manufacturer = '".$aircraft_manufacturer."')";
633
	    }
634
	}
635
	
636
	if ($highlights == "true")
637
	{
638
	    if (!is_string($highlights))
639
	    {
640
		return false;
641
	    } else {
642
		$additional_query .= " AND (marine_archive_output.highlight <> '')";
643
	    }
644
	}
645
	
646
	if ($airline_icao != "")
647
	{
648
	    $airline_icao = filter_var($airline_icao,FILTER_SANITIZE_STRING);
649
	    if (!is_string($airline_icao))
650
	    {
651
		return false;
652
	    } else {
653
		$additional_query .= " AND (marine_archive_output.airline_icao = '".$airline_icao."')";
654
	    }
655
	}
656
	
657
	if ($airline_country != "")
658
	{
659
	    $airline_country = filter_var($airline_country,FILTER_SANITIZE_STRING);
660
	    if (!is_string($airline_country))
661
	    {
662
		return false;
663
	    } else {
664
		$additional_query .= " AND (marine_archive_output.airline_country = '".$airline_country."')";
665
	    }
666
	}
667
	
668
	if ($airline_type != "")
669
	{
670
	    $airline_type = filter_var($airline_type,FILTER_SANITIZE_STRING);
671
	    if (!is_string($airline_type))
672
	    {
673
		return false;
674
	    } else {
675
		if ($airline_type == "passenger")
676
		{
677
		    $additional_query .= " AND (marine_archive_output.airline_type = 'passenger')";
678
		}
679
		if ($airline_type == "cargo")
680
		{
681
		    $additional_query .= " AND (marine_archive_output.airline_type = 'cargo')";
682
		}
683
		if ($airline_type == "military")
684
		{
685
		    $additional_query .= " AND (marine_archive_output.airline_type = 'military')";
686
		}
687
	    }
688
	}
689
	
690
	if ($airport != "")
691
	{
692
	    $airport = filter_var($airport,FILTER_SANITIZE_STRING);
693
	    if (!is_string($airport))
694
	    {
695
		return false;
696
	    } else {
697
		$additional_query .= " AND ((marine_archive_output.departure_airport_icao = '".$airport."') OR (marine_archive_output.arrival_airport_icao = '".$airport."'))";
698
	    }
699
	}
700
	
701
	if ($airport_country != "")
702
	{
703
	    $airport_country = filter_var($airport_country,FILTER_SANITIZE_STRING);
704
	    if (!is_string($airport_country))
705
	    {
706
		return false;
707
	    } else {
708
		$additional_query .= " AND ((marine_archive_output.departure_airport_country = '".$airport_country."') OR (marine_archive_output.arrival_airport_country = '".$airport_country."'))";
709
	    }
710
	}
711
    
712
	if ($callsign != "")
713
	{
714
	    $callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
715
	    if (!is_string($callsign))
716
	    {
717
		return false;
718
	    } else {
719
		$translate = $Translation->ident2icao($callsign);
720
		if ($translate != $callsign) {
721
			$additional_query .= " AND (marine_archive_output.ident = :callsign OR marine_archive_output.ident = :translate)";
722
			$query_values = array_merge($query_values,array(':callsign' => $callsign,':translate' => $translate));
723
		} else {
724
			$additional_query .= " AND (marine_archive_output.ident = '".$callsign."')";
725
		}
726
	    }
727
	}
728
729
	if ($owner != "")
730
	{
731
	    $owner = filter_var($owner,FILTER_SANITIZE_STRING);
732
	    if (!is_string($owner))
733
	    {
734
		return false;
735
	    } else {
736
		$additional_query .= " AND (marine_archive_output.owner_name = '".$owner."')";
737
	    }
738
	}
739
740
	if ($pilot_name != "")
741
	{
742
	    $pilot_name = filter_var($pilot_name,FILTER_SANITIZE_STRING);
743
	    if (!is_string($pilot_name))
744
	    {
745
		return false;
746
	    } else {
747
		$additional_query .= " AND (marine_archive_output.pilot_name = '".$pilot_name."')";
748
	    }
749
	}
750
	
751
	if ($pilot_id != "")
752
	{
753
	    $pilot_id = filter_var($pilot_id,FILTER_SANITIZE_NUMBER_INT);
754
	    if (!is_string($pilot_id))
755
	    {
756
		return false;
757
	    } else {
758
		$additional_query .= " AND (marine_archive_output.pilot_id = '".$pilot_id."')";
759
	    }
760
	}
761
	
762
	if ($departure_airport_route != "")
763
	{
764
	    $departure_airport_route = filter_var($departure_airport_route,FILTER_SANITIZE_STRING);
765
	    if (!is_string($departure_airport_route))
766
	    {
767
		return false;
768
	    } else {
769
		$additional_query .= " AND (marine_archive_output.departure_airport_icao = '".$departure_airport_route."')";
770
	    }
771
	}
772
	
773
	if ($arrival_airport_route != "")
774
	{
775
	    $arrival_airport_route = filter_var($arrival_airport_route,FILTER_SANITIZE_STRING);
776
	    if (!is_string($arrival_airport_route))
777
	    {
778
		return false;
779
	    } else {
780
		$additional_query .= " AND (marine_archive_output.arrival_airport_icao = '".$arrival_airport_route."')";
781
	    }
782
	}
783
	
784
	if ($altitude != "")
785
	{
786
	    $altitude_array = explode(",", $altitude);
787
	    
788
	    $altitude_array[0] = filter_var($altitude_array[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
789
	    $altitude_array[1] = filter_var($altitude_array[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
790
	    
791
792
	    if ($altitude_array[1] != "")
793
	    {                
794
		$altitude_array[0] = substr($altitude_array[0], 0, -2);
795
		$altitude_array[1] = substr($altitude_array[1], 0, -2);
796
		$additional_query .= " AND altitude BETWEEN '".$altitude_array[0]."' AND '".$altitude_array[1]."' ";
797
	    } else {
798
		$altitude_array[0] = substr($altitude_array[0], 0, -2);
799
		$additional_query .= " AND altitude <= '".$altitude_array[0]."' ";
800
	    }
801
	}
802
	
803
	if ($date_posted != "")
804
	{
805
	    $date_array = explode(",", $date_posted);
806
	    
807
	    $date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING);
808
	    $date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING);
809
	    
810
	    if ($globalTimezone != '') {
811
		date_default_timezone_set($globalTimezone);
812
		$datetime = new DateTime();
813
		$offset = $datetime->format('P');
814
	    } else $offset = '+00:00';
815
816
817
	    if ($date_array[1] != "")
818
	    {                
819
		$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
820
		$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1]));
821
		if ($globalDBdriver == 'mysql') {
822
			$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_archive_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' AND TIMESTAMP(CONVERT_TZ(marine_archive_output.date,'+00:00', '".$offset."')) <= '".$date_array[1]."' ";
823
		} else {
824
			$additional_query .= " AND marine_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." >= CAST('".$date_array[0]."' AS TIMESTAMP) AND marine_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." <= CAST('".$date_array[1]."' AS TIMESTAMP) ";
825
		}
826
	    } else {
827
		$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
828
                if ($globalDBdriver == 'mysql') {
829
			$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_archive_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' ";
830
		} else {
831
			$additional_query .= " AND marine_archive_output.date::timestamp AT TIME ZONE INTERVAL ".$offset." >= CAST('".$date_array[0]."' AS TIMESTAMP) ";
832
		}
833
	    }
834
	}
835
	
836
	if ($limit != "")
837
	{
838
	    $limit_array = explode(",", $limit);
839
	    
840
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
841
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
842
	    
843
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
844
	    {
845
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
846
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
847
	    }
848
	}
849
	
850
851
	if ($origLat != "" && $origLon != "" && $dist != "") {
852
		$dist = number_format($dist*0.621371,2,'.','');
853
		$query="SELECT marine_archive_output.*, 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - ABS(CAST(marine_archive.latitude as double precision)))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(ABS(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 
854
                          FROM marine_archive_output, marine_archive WHERE spotter_output_archive.fammarine_id = marine_archive.fammarine_id AND spotter_output.ident <> '' ".$additional_query."AND CAST(marine_archive.longitude as double precision) between ($origLon-$dist/ABS(cos(radians($origLat))*69)) and ($origLon+$dist/ABS(cos(radians($origLat))*69)) and CAST(marine_archive.latitude as double precision) between ($origLat-($dist/69)) and ($origLat+($dist/69)) 
855
                          AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - ABS(CAST(marine_archive.latitude as double precision)))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(ABS(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." ORDER BY distance";
856
	} else {
857
		if ($sort != "")
858
		{
859
			$search_orderby_array = $Marine->getOrderBy();
860
			$orderby_query = $search_orderby_array[$sort]['sql'];
861
		} else {
862
			$orderby_query = " ORDER BY marine_archive_output.date DESC";
863
		}
864
	
865
		if ($includegeodata == "true")
866
		{
867
			$additional_query .= " AND (marine_archive_output.waypoints <> '')";
868
		}
869
870
		$query  = "SELECT marine_archive_output.* FROM marine_archive_output 
871
		    WHERE marine_archive_output.ident <> '' 
872
		    ".$additional_query."
873
		    ".$filter_query.$orderby_query;
874
	}
875
	$spotter_array = $Marine->getDataFromDB($query, $query_values,$limit_query);
876
877
	return $spotter_array;
878
    }
879
880
    public function deleteMarineArchiveData()
881
    {
882
		global $globalArchiveKeepMonths, $globalDBdriver;
883
                date_default_timezone_set('UTC');
884
                if ($globalDBdriver == 'mysql') {
885
			$query = 'DELETE FROM marine_archive_output WHERE marine_archive_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveKeepMonths.' MONTH)';
886
		} else {
887
			$query = "DELETE FROM marine_archive_output WHERE marine_archive_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveKeepMonths." MONTH'";
888
		}
889
                try {
890
                        $sth = $this->db->prepare($query);
891
                        $sth->execute();
892
                } catch(PDOException $e) {
893
                        return "error";
894
                }
895
	}
896
897
    /**
898
    * Gets all the spotter information based on the callsign
899
    *
900
    * @return Array the spotter information
901
    *
902
    */
903
    public function getMarineDataByIdent($ident = '', $limit = '', $sort = '')
904
    {
905
	$global_query = "SELECT marine_archive_output.* FROM marine_archive_output";
906
	
907
	date_default_timezone_set('UTC');
908
	$Marine = new Marine($this->db);
909
	
910
	$query_values = array();
911
	$limit_query = '';
912
	$additional_query = '';
913
	
914
	if ($ident != "")
915
	{
916
	    if (!is_string($ident))
917
	    {
918
		return false;
919
	    } else {
920
		$additional_query = " AND (marine_archive_output.ident = :ident)";
921
		$query_values = array(':ident' => $ident);
922
	    }
923
	}
924
	
925
	if ($limit != "")
926
	{
927
	    $limit_array = explode(",", $limit);
928
	    
929
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
930
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
931
	    
932
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
933
	    {
934
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
935
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
936
	    }
937
	}
938
939
	if ($sort != "")
940
	{
941
	    $search_orderby_array = $Marine->getOrderBy();
942
	    $orderby_query = $search_orderby_array[$sort]['sql'];
943
	} else {
944
	    $orderby_query = " ORDER BY marine_archive_output.date DESC";
945
	}
946
947
	$query = $global_query." WHERE marine_archive_output.ident <> '' ".$additional_query." ".$orderby_query;
948
949
	$spotter_array = $Marine->getDataFromDB($query, $query_values, $limit_query);
950
951
	return $spotter_array;
952
    }
953
954
955
    /**
956
    * Gets all the spotter information based on the owner
957
    *
958
    * @return Array the spotter information
959
    *
960
    */
961
    public function getMarineDataByOwner($owner = '', $limit = '', $sort = '', $filter = array())
962
    {
963
	$global_query = "SELECT marine_archive_output.* FROM marine_archive_output";
964
	
965
	date_default_timezone_set('UTC');
966
	$Marine = new Marine($this->db);
967
	
968
	$query_values = array();
969
	$limit_query = '';
970
	$additional_query = '';
971
	$filter_query = $this->getFilter($filter,true,true);
972
	
973
	if ($owner != "")
974
	{
975
	    if (!is_string($owner))
976
	    {
977
		return false;
978
	    } else {
979
		$additional_query = " AND (marine_archive_output.owner_name = :owner)";
980
		$query_values = array(':owner' => $owner);
981
	    }
982
	}
983
	
984
	if ($limit != "")
985
	{
986
	    $limit_array = explode(",", $limit);
987
	    
988
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
989
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
990
	    
991
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
992
	    {
993
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
994
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
995
	    }
996
	}
997
998
	if ($sort != "")
999
	{
1000
	    $search_orderby_array = $Marine->getOrderBy();
1001
	    $orderby_query = $search_orderby_array[$sort]['sql'];
1002
	} else {
1003
	    $orderby_query = " ORDER BY marine_archive_output.date DESC";
1004
	}
1005
1006
	$query = $global_query.$filter_query." marine_archive_output.owner_name <> '' ".$additional_query." ".$orderby_query;
1007
1008
	$spotter_array = $Marine->getDataFromDB($query, $query_values, $limit_query);
1009
1010
	return $spotter_array;
1011
    }
1012
1013
    /**
1014
    * Gets all the spotter information based on the pilot
1015
    *
1016
    * @return Array the spotter information
1017
    *
1018
    */
1019
    public function getMarineDataByPilot($pilot = '', $limit = '', $sort = '', $filter = array())
1020
    {
1021
	$global_query = "SELECT marine_archive_output.* FROM marine_archive_output";
1022
	
1023
	date_default_timezone_set('UTC');
1024
	$Marine = new Marine($this->db);
1025
	
1026
	$query_values = array();
1027
	$limit_query = '';
1028
	$additional_query = '';
1029
	$filter_query = $this->getFilter($filter,true,true);
1030
	
1031
	if ($pilot != "")
1032
	{
1033
		$additional_query = " AND (marine_archive_output.pilot_id = :pilot OR marine_archive_output.pilot_name = :pilot)";
1034
		$query_values = array(':pilot' => $pilot);
1035
	}
1036
	
1037
	if ($limit != "")
1038
	{
1039
	    $limit_array = explode(",", $limit);
1040
	    
1041
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
1042
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
1043
	    
1044
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
1045
	    {
1046
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
1047
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
1048
	    }
1049
	}
1050
1051
	if ($sort != "")
1052
	{
1053
	    $search_orderby_array = $Marine->getOrderBy();
1054
	    $orderby_query = $search_orderby_array[$sort]['sql'];
1055
	} else {
1056
	    $orderby_query = " ORDER BY marine_archive_output.date DESC";
1057
	}
1058
1059
	$query = $global_query.$filter_query." marine_archive_output.pilot_name <> '' ".$additional_query." ".$orderby_query;
1060
1061
	$spotter_array = $Marine->getDataFromDB($query, $query_values, $limit_query);
1062
1063
	return $spotter_array;
1064
    }
1065
1066
    /**
1067
    * Gets all number of flight over countries
1068
    *
1069
    * @return Array the airline country list
1070
    *
1071
    */
1072
    public function countAllFlightOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '')
1073
    {
1074
	global $globalDBdriver;
1075
	/*
1076
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb 
1077
		    FROM countries c, marine_archive s
1078
		    WHERE Within(GeomFromText(CONCAT('POINT(',s.longitude,' ',s.latitude,')')), ogc_geom) ";
1079
	*/
1080
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb
1081
		    FROM countries c, marine_archive s
1082
		    WHERE c.iso2 = s.over_country ";
1083
                if ($olderthanmonths > 0) {
1084
            		if ($globalDBdriver == 'mysql') {
1085
				$query .= 'AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1086
			} else {
1087
				$query .= "AND date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1088
			}
1089
		}
1090
                if ($sincedate != '') $query .= "AND date > '".$sincedate."' ";
1091
	$query .= "GROUP BY c.name, c.iso3, c.iso2 ORDER BY nb DESC";
1092
	if ($limit) $query .= " LIMIT 0,10";
1093
      
1094
	
1095
	$sth = $this->db->prepare($query);
1096
	$sth->execute();
1097
 
1098
	$flight_array = array();
1099
	$temp_array = array();
1100
        
1101
	while($row = $sth->fetch(PDO::FETCH_ASSOC))
1102
	{
1103
	    $temp_array['flight_count'] = $row['nb'];
1104
	    $temp_array['flight_country'] = $row['name'];
1105
	    $temp_array['flight_country_iso3'] = $row['iso3'];
1106
	    $temp_array['flight_country_iso2'] = $row['iso2'];
1107
	    $flight_array[] = $temp_array;
1108
	}
1109
	return $flight_array;
1110
    }
1111
1112
    /**
1113
    * Gets all number of flight over countries
1114
    *
1115
    * @return Array the airline country list
1116
    *
1117
    */
1118
    public function countAllFlightOverCountriesByAirlines($limit = true,$olderthanmonths = 0,$sincedate = '')
1119
    {
1120
	global $globalDBdriver;
1121
	/*
1122
	$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb 
1123
		    FROM countries c, marine_archive s
1124
		    WHERE Within(GeomFromText(CONCAT('POINT(',s.longitude,' ',s.latitude,')')), ogc_geom) ";
1125
	*/
1126
	$query = "SELECT o.airline_icao,c.name, c.iso3, c.iso2, count(c.name) as nb
1127
		    FROM countries c, marine_archive s, spotter_output o
1128
		    WHERE c.iso2 = s.over_country AND o.airline_icao <> '' AND o.fammarine_id = s.fammarine_id ";
1129
                if ($olderthanmonths > 0) {
1130
            		if ($globalDBdriver == 'mysql') {
1131
				$query .= 'AND s.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1132
			} else {
1133
				$query .= "AND s.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1134
			}
1135
		}
1136
                if ($sincedate != '') $query .= "AND s.date > '".$sincedate."' ";
1137
	$query .= "GROUP BY o.airline_icao,c.name, c.iso3, c.iso2 ORDER BY nb DESC";
1138
	if ($limit) $query .= " LIMIT 0,10";
1139
      
1140
	
1141
	$sth = $this->db->prepare($query);
1142
	$sth->execute();
1143
 
1144
	$flight_array = array();
1145
	$temp_array = array();
1146
        
1147
	while($row = $sth->fetch(PDO::FETCH_ASSOC))
1148
	{
1149
	    $temp_array['airline_icao'] = $row['airline_icao'];
1150
	    $temp_array['flight_count'] = $row['nb'];
1151
	    $temp_array['flight_country'] = $row['name'];
1152
	    $temp_array['flight_country_iso3'] = $row['iso3'];
1153
	    $temp_array['flight_country_iso2'] = $row['iso2'];
1154
	    $flight_array[] = $temp_array;
1155
	}
1156
	return $flight_array;
1157
    }
1158
1159
    /**
1160
    * Gets last spotter information based on a particular callsign
1161
    *
1162
    * @return Array the spotter information
1163
    *
1164
    */
1165
    public function getDateArchiveMarineDataById($id,$date)
1166
    {
1167
	$Marine = new Marine($this->db);
1168
	date_default_timezone_set('UTC');
1169
	$id = filter_var($id, FILTER_SANITIZE_STRING);
1170
	$query  = 'SELECT marine_archive.* FROM marine_archive INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE l.fammarine_id = :id AND l.date <= :date GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate ORDER BY marine_archive.date DESC';
1171
	$date = date('c',$date);
1172
	$spotter_array = $Marine->getDataFromDB($query,array(':id' => $id,':date' => $date));
1173
	return $spotter_array;
1174
    }
1175
1176
    /**
1177
    * Gets all the spotter information based on a particular callsign
1178
    *
1179
    * @return Array the spotter information
1180
    *
1181
    */
1182
    public function getDateArchiveMarineDataByIdent($ident,$date)
1183
    {
1184
	$Marine = new Marine($this->db);
1185
	date_default_timezone_set('UTC');
1186
	$ident = filter_var($ident, FILTER_SANITIZE_STRING);
1187
	$query  = 'SELECT marine_archive.* FROM marine_archive INNER JOIN (SELECT l.fammarine_id, max(l.date) as maxdate FROM marine_archive l WHERE l.ident = :ident AND l.date <= :date GROUP BY l.fammarine_id) s on marine_archive.fammarine_id = s.fammarine_id AND marine_archive.date = s.maxdate ORDER BY marine_archive.date DESC';
1188
	$date = date('c',$date);
1189
	$spotter_array = $Marine->getDataFromDB($query,array(':ident' => $ident,':date' => $date));
1190
	return $spotter_array;
1191
    }
1192
1193
    /**
1194
    * Gets all the spotter information based on the airport
1195
    *
1196
    * @return Array the spotter information
1197
    *
1198
    */
1199
    public function getMarineDataByAirport($airport = '', $limit = '', $sort = '',$filters = array())
1200
    {
1201
	global $global_query;
1202
	$Marine = new Marine();
1203
	date_default_timezone_set('UTC');
1204
	$query_values = array();
1205
	$limit_query = '';
1206
	$additional_query = '';
1207
	$filter_query = $this->getFilter($filters,true,true);
1208
	
1209
	if ($airport != "")
1210
	{
1211
	    if (!is_string($airport))
1212
	    {
1213
		return false;
1214
	    } else {
1215
		$additional_query .= " AND ((marine_archive_output.departure_airport_icao = :airport) OR (marine_archive_output.arrival_airport_icao = :airport))";
1216
		$query_values = array(':airport' => $airport);
1217
	    }
1218
	}
1219
	
1220
	if ($limit != "")
1221
	{
1222
	    $limit_array = explode(",", $limit);
1223
	    
1224
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
1225
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
1226
	    
1227
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
1228
	    {
1229
		//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
1230
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
1231
	    }
1232
	}
1233
	
1234
	if ($sort != "")
1235
	{
1236
	    $search_orderby_array = $Marine->getOrderBy();
1237
	    $orderby_query = $search_orderby_array[$sort]['sql'];
1238
	} else {
1239
	    $orderby_query = " ORDER BY marine_archive_output.date DESC";
1240
	}
1241
1242
	$query = $global_query.$filter_query." marine_archive_output.ident <> '' ".$additional_query." AND ((marine_archive_output.departure_airport_icao <> 'NA') AND (marine_archive_output.arrival_airport_icao <> 'NA')) ".$orderby_query;
1243
1244
	$spotter_array = $Marine->getDataFromDB($query, $query_values, $limit_query);
1245
1246
	return $spotter_array;
1247
    }
1248
}
1249
?>