Completed
Push — master ( 7b031c...3082e8 )
by Yannick
11:44
created

Stats::getStatsOwnerTotal()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 12
nop 1
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
/*
3
* This class save stats older than a year and $globalArchiveMonths
4
*/
5
6
require_once(dirname(__FILE__).'/class.Spotter.php');
7
require_once(dirname(__FILE__).'/class.SpotterArchive.php');
8
require_once(dirname(__FILE__).'/class.Common.php');
9
class Stats {
10
	public $db;
11
	public $filter_name = '';
12
	
13
	public function __construct($dbc = null) {
14
		global $globalFilterName;
15
		if (isset($globalFilterName)) $this->filter_name = $globalFilterName;
16
		$Connection = new Connection($dbc);
17
		$this->db = $Connection->db();
18
        }
19
              
20
	public function addLastStatsUpdate($type,$stats_date) {
21
                $query = "DELETE FROM config WHERE name = :type;
22
            		INSERT INTO config (name,value) VALUES (:type,:stats_date);";
23
                $query_values = array('type' => $type,':stats_date' => $stats_date);
24
                 try {
25
                        $sth = $this->db->prepare($query);
26
                        $sth->execute($query_values);
27
                } catch(PDOException $e) {
28
                        return "error : ".$e->getMessage();
29
                }
30
        }
31
32
	public function getLastStatsUpdate($type = 'last_update_stats') {
33
                $query = "SELECT value FROM config WHERE name = :type";
34
                 try {
35
                        $sth = $this->db->prepare($query);
36
                        $sth->execute(array(':type' => $type));
37
                } catch(PDOException $e) {
38
                        echo "error : ".$e->getMessage();
39
                }
40
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
41
                return $all;
42
        }
43
        public function deleteStats($filter_name = '') {
44
        	/*
45
        	$query = "DELETE FROM config WHERE name = 'last_update_stats'";
46
                 try {
47
                        $sth = $this->db->prepare($query);
48
                        $sth->execute();
49
                } catch(PDOException $e) {
50
                        return "error : ".$e->getMessage();
51
                }
52
                */
53
        	$query = "DELETE FROM stats WHERE filter_name = :filter_name;DELETE FROM stats_aircraft WHERE filter_name = :filter_name;DELETE FROM stats_airline WHERE filter_name = :filter_name;DELETE FROM stats_airport WHERE filter_name = :filter_name;DELETE FROM stats_callsign WHERE filter_name = :filter_name;DELETE FROM stats_country WHERE filter_name = :filter_name;DELETE FROM stats_flight WHERE filter_name = :filter_name;DELETE FROM stats_owner WHERE filter_name = :filter_name;DELETE FROM stats_pilot WHERE filter_name = :filter_name;DELETE FROM stats_registration WHERE filter_name = :filter_name;";
54
                 try {
55
                        $sth = $this->db->prepare($query);
56
                        $sth->execute(array(':filter_name' => $filter_name));
57
                } catch(PDOException $e) {
58
                        return "error : ".$e->getMessage();
59
                }
60
        }
61
        public function deleteOldStats($filter_name = '') {
62
        	
63
        	$query = "DELETE FROM config WHERE name = 'last_update_stats'";
64
                 try {
65
                        $sth = $this->db->prepare($query);
66
                        $sth->execute();
67
                } catch(PDOException $e) {
68
                        return "error : ".$e->getMessage();
69
                }
70
                
71
        	$query = "DELETE FROM stats_aircraft WHERE filter_name = :filter_name;DELETE FROM stats_airline WHERE filter_name = :filter_name;DELETE FROM stats_callsign WHERE filter_name = :filter_name;DELETE FROM stats_country WHERE filter_name = :filter_name;DELETE FROM stats_owner WHERE filter_name = :filter_name;DELETE FROM stats_pilot WHERE filter_name = :filter_name;DELETE FROM stats_registration WHERE filter_name = :filter_name;";
72
                 try {
73
                        $sth = $this->db->prepare($query);
74
                        $sth->execute(array(':filter_name' => $filter_name));
75
                } catch(PDOException $e) {
76
                        return "error : ".$e->getMessage();
77
                }
78
        }
79
	public function getAllAirlineNames($filter_name = '') {
80
		if ($filter_name == '') $filter_name = $this->filter_name;
81
                $query = "SELECT * FROM stats_airline WHERE filter_name = :filter_name ORDER BY airline_name ASC";
82
                 try {
83
                        $sth = $this->db->prepare($query);
84
                        $sth->execute(array(':filter_name' => $filter_name));
85
                } catch(PDOException $e) {
86
                        echo "error : ".$e->getMessage();
87
                }
88
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
89
                return $all;
90
        }
91
	public function getAllAircraftTypes($stats_airline = '',$filter_name = '') {
92
		if ($filter_name == '') $filter_name = $this->filter_name;
93
                $query = "SELECT * FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY aircraft_manufacturer ASC";
94
                 try {
95
                        $sth = $this->db->prepare($query);
96
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
97
                } catch(PDOException $e) {
98
                        echo "error : ".$e->getMessage();
99
                }
100
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
101
                return $all;
102
        }
103
	public function getAllManufacturers($stats_airline = '',$filter_name = '') {
104
		if ($filter_name == '') $filter_name = $this->filter_name;
105
                $query = "SELECT DISTINCT(aircraft_manufacturer) FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name AND aircraft_manufacturer <> '' ORDER BY aircraft_manufacturer ASC";
106
                 try {
107
                        $sth = $this->db->prepare($query);
108
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
109
                } catch(PDOException $e) {
110
                        echo "error : ".$e->getMessage();
111
                }
112
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
113
                return $all;
114
        }
115
	public function getAllAirportNames($stats_airline = '',$filter_name = '') {
116
		if ($filter_name == '') $filter_name = $this->filter_name;
117
                $query = "SELECT airport_icao, airport_name,airport_city,airport_country FROM stats_airport WHERE stats_airline = :stats_airline AND filter_name = :filter_name AND stats_type = 'daily' GROUP BY airport_icao,airport_name,airport_city,airport_country ORDER BY airport_city ASC";
118
                 try {
119
                        $sth = $this->db->prepare($query);
120
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
121
                } catch(PDOException $e) {
122
                        echo "error : ".$e->getMessage();
123
                }
124
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
125
                return $all;
126
        }
127
128
	public function getAllOwnerNames($stats_airline = '',$filter_name = '') {
129
		if ($filter_name == '') $filter_name = $this->filter_name;
130
                $query = "SELECT owner_name FROM stats_owner WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY owner_name ASC";
131
                 try {
132
                        $sth = $this->db->prepare($query);
133
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
134
                } catch(PDOException $e) {
135
                        echo "error : ".$e->getMessage();
136
                }
137
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
138
                return $all;
139
        }
140
141
	public function getAllPilotNames($stats_airline = '',$filter_name = '') {
142
		if ($filter_name == '') $filter_name = $this->filter_name;
143
                $query = "SELECT pilot_id,pilot_name FROM stats_pilot WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY pilot_name ASC";
144
                 try {
145
                        $sth = $this->db->prepare($query);
146
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
147
                } catch(PDOException $e) {
148
                        echo "error : ".$e->getMessage();
149
                }
150
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
151
                return $all;
152
        }
153
154
155
	public function countAllAircraftTypes($limit = true, $stats_airline = '', $filter_name = '',$year = '', $month = '') {
156
		global $globalStatsFilters;
157
		if ($filter_name == '') $filter_name = $this->filter_name;
158
		if ($year == '' && $month == '') {
159
			if ($limit) $query = "SELECT aircraft_icao, cnt AS aircraft_icao_count, aircraft_name, aircraft_manufacturer FROM stats_aircraft WHERE aircraft_name <> '' AND aircraft_icao <> '' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY aircraft_icao_count DESC LIMIT 10 OFFSET 0";
160
			else $query = "SELECT aircraft_icao, cnt AS aircraft_icao_count, aircraft_name, aircraft_manufacturer FROM stats_aircraft WHERE aircraft_name <> '' AND aircraft_icao <> '' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY aircraft_icao_count DESC";
161
			try {
162
				$sth = $this->db->prepare($query);
163
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
164
			} catch(PDOException $e) {
165
				echo "error : ".$e->getMessage();
166
			}
167
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
168
		} else $all = array();
169
                if (empty($all)) {
170
            	    $filters = array('airlines' => array($stats_airline));
171
            	    if ($filter_name != '') {
172
            		    $filters = array_merge($filters,$globalStatsFilters[$filter_name]);
173
            	    }
174
            	    $Spotter = new Spotter($this->db);
175
            	    $all = $Spotter->countAllAircraftTypes($limit,0,'',$filters,$year,$month);
176
                }
177
                return $all;
178
	}
179
	public function countAllAirlineCountries($limit = true,$filter_name = '',$year = '',$month = '') {
180
		global $globalStatsFilters;
181
		if ($filter_name == '') $filter_name = $this->filter_name;
182
		if ($year == '' && $month == '') {
183
			if ($limit) $query = "SELECT airlines.country AS airline_country, SUM(stats_airline.cnt) as airline_country_count FROM stats_airline,airlines WHERE stats_airline.airline_icao=airlines.icao AND filter_name = :filter_name GROUP BY airline_country ORDER BY airline_country_count DESC LIMIT 10 OFFSET 0";
184
			else $query = "SELECT airlines.country AS airline_country, SUM(stats_airline.cnt) as airline_country_count FROM stats_airline,airlines WHERE stats_airline.airline_icao=airlines.icao AND filter_name = :filter_name GROUP BY airline_country ORDER BY airline_country_count DESC";
185
			try {
186
				$sth = $this->db->prepare($query);
187
				$sth->execute(array(':filter_name' => $filter_name));
188
			} catch(PDOException $e) {
189
				echo "error : ".$e->getMessage();
190
			}
191
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
192
		} else $all = array();
193
                if (empty($all)) {
194
            		$Spotter = new Spotter($this->db);
195
            		$filters = array();
196
            		if ($filter_name != '') {
197
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
198
			}
199
            		$all = $Spotter->countAllAirlineCountries($limit,$filters,$year,$month);
200
                }
201
                return $all;
202
	}
203
	public function countAllAircraftManufacturers($limit = true,$stats_airline = '', $filter_name = '',$year = '', $month = '') {
204
		global $globalStatsFilters;
205
		if ($filter_name == '') $filter_name = $this->filter_name;
206
		if ($year == '' && $month == '') {
207
			if ($limit) $query = "SELECT aircraft_manufacturer, SUM(stats_aircraft.cnt) as aircraft_manufacturer_count FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY aircraft_manufacturer ORDER BY aircraft_manufacturer_count DESC LIMIT 10 OFFSET 0";
208
			else $query = "SELECT aircraft_manufacturer, SUM(stats_aircraft.cnt) as aircraft_manufacturer_count FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY aircraft_manufacturer ORDER BY aircraft_manufacturer_count DESC";
209
			try {
210
				$sth = $this->db->prepare($query);
211
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
212
			} catch(PDOException $e) {
213
				echo "error : ".$e->getMessage();
214
			}
215
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
216
		} else $all = array();
217
		if (empty($all)) {
218
			$filters = array('airlines' => array($stats_airline));
219
			if ($filter_name != '') {
220
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
221
			}
222
			$Spotter = new Spotter($this->db);
223
			$all = $Spotter->countAllAircraftManufacturers($filters,$year,$month);
224
		}
225
		return $all;
226
	}
227
228
	public function countAllArrivalCountries($limit = true, $stats_airline = '', $filter_name = '',$year = '', $month = '') {
229
		global $globalStatsFilters;
230
		if ($filter_name == '') $filter_name = $this->filter_name;
231
		if ($year == '' && $month == '') {
232
			if ($limit) $query = "SELECT airport_country AS airport_arrival_country, SUM(arrival) as airport_arrival_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_arrival_country ORDER BY airport_arrival_country_count DESC LIMIT 10 OFFSET 0";
233
			else $query = "SELECT airport_country AS airport_arrival_country, SUM(arrival) as airport_arrival_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_arrival_country ORDER BY airport_arrival_country_count DESC";
234
			try {
235
				$sth = $this->db->prepare($query);
236
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
237
			} catch(PDOException $e) {
238
				echo "error : ".$e->getMessage();
239
			}
240
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
241
		} else $all = array();
242
                if (empty($all)) {
243
			$filters = array('airlines' => array($stats_airline));
244
			if ($filter_name != '') {
245
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
246
			}
247
			$Spotter = new Spotter($this->db);
248
			$all = $Spotter->countAllArrivalCountries($limit,$filters,$year,$month);
249
                }
250
                return $all;
251
	}
252
	public function countAllDepartureCountries($limit = true, $stats_airline = '', $filter_name = '', $year = '', $month = '') {
253
		global $globalStatsFilters;
254
		if ($filter_name == '') $filter_name = $this->filter_name;
255
		if ($limit) $query = "SELECT airport_country AS airport_departure_country, SUM(departure) as airport_departure_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_departure_country ORDER BY airport_departure_country_count DESC LIMIT 10 OFFSET 0";
256
		else $query = "SELECT airport_country AS airport_departure_country, SUM(departure) as airport_departure_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_departure_country ORDER BY airport_departure_country_count DESC";
257
                 try {
258
                        $sth = $this->db->prepare($query);
259
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
260
                } catch(PDOException $e) {
261
                        echo "error : ".$e->getMessage();
262
                }
263
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
264
                if (empty($all)) {
265
			$filters = array('airlines' => array($stats_airline));
266
			if ($filter_name != '') {
267
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
268
			}
269
			$Spotter = new Spotter($this->db);
270
			$all = $Spotter->countAllDepartureCountries($filters,$year,$month);
271
                }
272
                return $all;
273
	}
274
275
	public function countAllAirlines($limit = true,$filter_name = '',$year = '',$month = '') {
276
		global $globalStatsFilters;
277
		if ($filter_name == '') $filter_name = $this->filter_name;
278
		if ($year == '' && $month == '') {
279
			if ($limit) $query = "SELECT DISTINCT stats_airline.airline_icao, stats_airline.cnt AS airline_count, stats_airline.airline_name, airlines.country as airline_country FROM stats_airline, airlines WHERE stats_airline.airline_name <> '' AND stats_airline.airline_icao <> '' AND airlines.icao = stats_airline.airline_icao AND filter_name = :filter_name ORDER BY airline_count DESC LIMIT 10 OFFSET 0";
280
			else $query = "SELECT DISTINCT stats_airline.airline_icao, stats_airline.cnt AS airline_count, stats_airline.airline_name, airlines.country as airline_country FROM stats_airline, airlines WHERE stats_airline.airline_name <> '' AND stats_airline.airline_icao <> '' AND airlines.icao = stats_airline.airline_icao AND filter_name = :filter_name ORDER BY airline_count DESC";
281
			try {
282
				$sth = $this->db->prepare($query);
283
				$sth->execute(array(':filter_name' => $filter_name));
284
			} catch(PDOException $e) {
285
				echo "error : ".$e->getMessage();
286
			}
287
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
288
		} else $all = array();
289
                if (empty($all)) {
290
	                $Spotter = new Spotter($this->db);
291
            		$filters = array();
292
            		if ($filter_name != '') {
293
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
294
			}
295
296
    		        $all = $Spotter->countAllAirlines($limit,0,'',$filters,$year,$month);
297
                }
298
                return $all;
299
	}
300
	public function countAllAircraftRegistrations($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
301
		global $globalStatsFilters;
302
		if ($filter_name == '') $filter_name = $this->filter_name;
303
		if ($year == '' && $month == '') {
304
			if ($limit) $query = "SELECT s.aircraft_icao, s.cnt AS aircraft_registration_count, a.type AS aircraft_name, s.registration FROM stats_registration s, aircraft a WHERE s.registration <> '' AND a.icao = s.aircraft_icao AND s.stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY aircraft_registration_count DESC LIMIT 10 OFFSET 0";
305
			else $query = "SELECT s.aircraft_icao, s.cnt AS aircraft_registration_count, a.type AS aircraft_name FROM stats_registration s, aircraft a WHERE s.registration <> '' AND a.icao = s.aircraft_icao AND s.stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY aircraft_registration_count DESC";
306
			try {
307
				$sth = $this->db->prepare($query);
308
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
309
			} catch(PDOException $e) {
310
				echo "error : ".$e->getMessage();
311
			}
312
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
313
		} else $all = array();
314
                if (empty($all)) {
315
			$filters = array('airlines' => array($stats_airline));
316
			if ($filter_name != '') {
317
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
318
			}
319
	                $Spotter = new Spotter($this->db);
320
    		        $all = $Spotter->countAllAircraftRegistrations($limit,0,'',$filters,$year,$month);
321
                }
322
                return $all;
323
	}
324
	public function countAllCallsigns($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
325
		global $globalStatsFilters;
326
		if ($filter_name == '') $filter_name = $this->filter_name;
327
		if ($year == '' && $month == '') {
328
			if ($limit) $query = "SELECT s.callsign_icao, s.cnt AS callsign_icao_count, a.name AS airline_name, a.icao as airline_icao FROM stats_callsign s, airlines a WHERE s.callsign_icao <> '' AND a.icao = s.airline_icao AND s.airline_icao = :stats_airline AND filter_name = :filter_name ORDER BY callsign_icao_count DESC LIMIT 10 OFFSET 0";
329
			else $query = "SELECT s.callsign_icao, s.cnt AS callsign_icao_count, a.name AS airline_name, a.icao as airline_icao FROM stats_callsign s, airlines a WHERE s.callsign_icao <> '' AND a.icao = s.airline_icao AND s.airline_icao = :stats_airline AND filter_name = :filter_name ORDER BY callsign_icao_count DESC";
330
			 try {
331
				$sth = $this->db->prepare($query);
332
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
333
			} catch(PDOException $e) {
334
				echo "error : ".$e->getMessage();
335
			}
336
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
337
		} else $all = array();
338
		if (empty($all)) {
339
			$filters = array('airlines' => array($stats_airline));
340
			if ($filter_name != '') {
341
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
342
			}
343
			$Spotter = new Spotter($this->db);
344
			$all = $Spotter->countAllCallsigns($limit,0,'',$filters,$year,$month);
345
		}
346
		return $all;
347
	}
348
	public function countAllFlightOverCountries($limit = true, $stats_airline = '',$filter_name = '',$year = '',$month = '') {
349
		$Connection = new Connection();
350
		if ($filter_name == '') $filter_name = $this->filter_name;
351
		if ($Connection->tableExists('countries')) {
352
			if ($year == '' && $month == '') {
353
				if ($limit) $query = "SELECT countries.iso3 as flight_country_iso3, countries.iso2 as flight_country_iso2, countries.name as flight_country, cnt as flight_count, lat as flight_country_latitude, lon as flight_country_longitude FROM stats_country, countries WHERE stats_country.iso2 = countries.iso2 AND stats_country.stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY flight_count DESC LIMIT 20 OFFSET 0";
354
				else $query = "SELECT countries.iso3 as flight_country_iso3, countries.iso2 as flight_country_iso2, countries.name as flight_country, cnt as flight_count, lat as flight_country_latitude, lon as flight_country_longitude FROM stats_country, countries WHERE stats_country.iso2 = countries.iso2 AND stats_country.stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY flight_count DESC";
355
				 try {
356
					$sth = $this->db->prepare($query);
357
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
358
				} catch(PDOException $e) {
359
					echo "error : ".$e->getMessage();
360
				}
361
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
362
                /*
363
                if (empty($all)) {
364
	                $Spotter = new Spotter($this->db);
365
    		        $all = $Spotter->countAllFlightOverCountries($limit);
366
                }
367
                */
368
				return $all;
369
			} else return array();
370
		} else {
371
			return array();
372
		}
373
	}
374
	public function countAllPilots($limit = true,$stats_airline = '',$filter_name = '', $year = '',$month = '') {
375
		global $globalStatsFilters;
376
		if ($filter_name == '') $filter_name = $this->filter_name;
377
		if ($year == '' && $month == '') {
378
			if ($limit) $query = "SELECT pilot_id, cnt AS pilot_count, pilot_name, format_source FROM stats_pilot WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY pilot_count DESC LIMIT 10 OFFSET 0";
379
			else $query = "SELECT pilot_id, cnt AS pilot_count, pilot_name, format_source FROM stats_pilot WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY pilot_count DESC";
380
			try {
381
				$sth = $this->db->prepare($query);
382
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
383
			} catch(PDOException $e) {
384
				echo "error : ".$e->getMessage();
385
			}
386
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
387
		} else $all = array();
388
		if (empty($all)) {
389
			$filters = array('airlines' => array($stats_airline));
390
			if ($filter_name != '') {
391
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
392
			}
393
			$Spotter = new Spotter($this->db);
394
			$all = $Spotter->countAllPilots($limit,0,'',$filters,$year,$month);
395
		}
396
		return $all;
397
	}
398
399
	public function countAllOwners($limit = true,$stats_airline = '', $filter_name = '',$year = '',$month = '') {
400
		global $globalStatsFilters;
401
		if ($filter_name == '') $filter_name = $this->filter_name;
402
		if ($year == '' && $month == '') {
403
			if ($limit) $query = "SELECT owner_name, cnt AS owner_count FROM stats_owner WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY owner_count DESC LIMIT 10 OFFSET 0";
404
			else $query = "SELECT owner_name, cnt AS owner_count FROM stats_owner WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY owner_count DESC";
405
			try {
406
				$sth = $this->db->prepare($query);
407
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
408
			} catch(PDOException $e) {
409
				echo "error : ".$e->getMessage();
410
			}
411
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
412
		} else $all = array();
413
                if (empty($all)) {
414
			$filters = array('airlines' => array($stats_airline));
415
			if ($filter_name != '') {
416
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
417
			}
418
            		$Spotter = new Spotter($this->db);
419
            		$all = $Spotter->countAllOwners($limit,0,'',$filters,$year,$month);
420
                }
421
                return $all;
422
	}
423
	public function countAllDepartureAirports($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
424
		global $globalStatsFilters;
425
		if ($filter_name == '') $filter_name = $this->filter_name;
426
		if ($year == '' && $month == '') {
427
			if ($limit) $query = "SELECT DISTINCT airport_icao AS airport_departure_icao,airport_city AS airport_departure_city,airport_country AS airport_departure_country,departure AS airport_departure_icao_count FROM stats_airport WHERE departure > 0 AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY airport_departure_icao_count DESC LIMIT 10 OFFSET 0";
428
			else $query = "SELECT DISTINCT airport_icao AS airport_departure_icao,airport_city AS airport_departure_city,airport_country AS airport_departure_country,departure AS airport_departure_icao_count FROM stats_airport WHERE departure > 0 AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY airport_departure_icao_count DESC";
429
			try {
430
				$sth = $this->db->prepare($query);
431
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
432
			} catch(PDOException $e) {
433
				echo "error : ".$e->getMessage();
434
			}
435
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
436
		} else $all = array();
437
                if (empty($all)) {
438
			$filters = array('airlines' => array($stats_airline));
439
            		if ($filter_name != '') {
440
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
441
			}
442
            		$Spotter = new Spotter($this->db);
443
            		$pall = $Spotter->countAllDepartureAirports($limit,0,'',$filters,$year,$month);
444
        		$dall = $Spotter->countAllDetectedDepartureAirports($limit,0,'',$filters,$year,$month);
445
        		$all = array();
446
        		foreach ($pall as $value) {
447
        			$icao = $value['airport_departure_icao'];
448
        			$all[$icao] = $value;
449
        		}
450
        		
451
        		foreach ($dall as $value) {
452
        			$icao = $value['airport_departure_icao'];
453
        			if (isset($all[$icao])) {
454
        				$all[$icao]['airport_departure_icao_count'] = $all[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
455
        			} else $all[$icao] = $value;
456
        		}
457
        		$count = array();
458
        		foreach ($all as $key => $row) {
459
        			$count[$key] = $row['airport_departure_icao_count'];
460
        		}
461
        		array_multisort($count,SORT_DESC,$all);
462
                }
463
                return $all;
464
	}
465
	public function countAllArrivalAirports($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
466
		global $globalStatsFilters;
467
		if ($filter_name == '') $filter_name = $this->filter_name;
468
		if ($year == '' && $month == '') {
469
			if ($limit) $query = "SELECT DISTINCT airport_icao AS airport_arrival_icao,airport_city AS airport_arrival_city,airport_country AS airport_arrival_country,arrival AS airport_arrival_icao_count FROM stats_airport WHERE arrival > 0 AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY airport_arrival_icao_count DESC LIMIT 10 OFFSET 0";
470
			else $query = "SELECT DISTINCT airport_icao AS airport_arrival_icao,airport_city AS airport_arrival_city,airport_country AS airport_arrival_country,arrival AS airport_arrival_icao_count FROM stats_airport WHERE arrival > 0 AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY airport_arrival_icao_count DESC";
471
			try {
472
				$sth = $this->db->prepare($query);
473
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
474
			} catch(PDOException $e) {
475
				echo "error : ".$e->getMessage();
476
			}
477
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
478
		} else $all = array();
479
		if (empty($all)) {
480
			$filters = array('airlines' => array($stats_airline));
481
			if ($filter_name != '') {
482
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
483
			}
484
			$Spotter = new Spotter($this->db);
485
			$pall = $Spotter->countAllArrivalAirports($limit,0,'',false,$filters,$year,$month);
486
			$dall = $Spotter->countAllDetectedArrivalAirports($limit,0,'',false,$filters,$year,$month);
487
        		$all = array();
488
        		foreach ($pall as $value) {
489
        			$icao = $value['airport_arrival_icao'];
490
        			$all[$icao] = $value;
491
        		}
492
        		
493
        		foreach ($dall as $value) {
494
        			$icao = $value['airport_arrival_icao'];
495
        			if (isset($all[$icao])) {
496
        				$all[$icao]['airport_arrival_icao_count'] = $all[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
497
        			} else $all[$icao] = $value;
498
        		}
499
        		$count = array();
500
        		foreach ($all as $key => $row) {
501
        			$count[$key] = $row['airport_arrival_icao_count'];
502
        		}
503
        		array_multisort($count,SORT_DESC,$all);
504
                }
505
 
506
                return $all;
507
	}
508
	public function countAllMonthsLastYear($limit = true,$stats_airline = '',$filter_name = '') {
509
		global $globalDBdriver, $globalStatsFilters;
510
		if ($filter_name == '') $filter_name = $this->filter_name;
511
		if ($globalDBdriver == 'mysql') {
512
			if ($limit) $query = "SELECT MONTH(stats_date) as month_name, YEAR(stats_date) as year_name, cnt as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 12 MONTH) AND stats_airline = :stats_airline AND filter_name = :filter_name";
513
			else $query = "SELECT MONTH(stats_date) as month_name, YEAR(stats_date) as year_name, cnt as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_airline = :stats_airline AND filter_name = :filter_name";
514
		} else {
515
			if ($limit) $query = "SELECT EXTRACT(MONTH FROM stats_date) as month_name, EXTRACT(YEAR FROM stats_date) as year_name, cnt as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_date >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '12 MONTHS' AND stats_airline = :stats_airline AND filter_name = :filter_name";
516
			else $query = "SELECT EXTRACT(MONTH FROM stats_date) as month_name, EXTRACT(YEAR FROM stats_date) as year_name, cnt as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_airline = :stats_airline AND filter_name = :filter_name";
517
		}
518
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
519
                 try {
520
                        $sth = $this->db->prepare($query);
521
                        $sth->execute($query_data);
522
                } catch(PDOException $e) {
523
                        echo "error : ".$e->getMessage();
524
                }
525
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
526
                if (empty($all)) {
527
			$filters = array('airlines' => array($stats_airline));
528
			if ($filter_name != '') {
529
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
530
			}
531
            		$Spotter = new Spotter($this->db);
532
            		$all = $Spotter->countAllMonthsLastYear($filters);
533
                }
534
                
535
                return $all;
536
	}
537
	
538
	public function countAllDatesLastMonth($stats_airline = '',$filter_name = '') {
539
		global $globalStatsFilters;
540
		if ($filter_name == '') $filter_name = $this->filter_name;
541
		$query = "SELECT flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'month' AND stats_airline = :stats_airline AND filter_name = :filter_name";
542
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
543
                 try {
544
                        $sth = $this->db->prepare($query);
545
                        $sth->execute($query_data);
546
                } catch(PDOException $e) {
547
                        echo "error : ".$e->getMessage();
548
                }
549
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
550
                if (empty($all)) {
551
			$filters = array('airlines' => array($stats_airline));
552
			if ($filter_name != '') {
553
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
554
			}
555
            		$Spotter = new Spotter($this->db);
556
            		$all = $Spotter->countAllDatesLastMonth($filters);
557
                }
558
                return $all;
559
	}
560
	public function countAllDatesLast7Days($stats_airline = '',$filter_name = '') {
561
		global $globalDBdriver, $globalStatsFilters;
562
		if ($filter_name == '') $filter_name = $this->filter_name;
563
		if ($globalDBdriver == 'mysql') {
564
			$query = "SELECT flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'month' AND flight_date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY) AND stats_airline = :stats_airline AND filter_name = :filter_name";
565
		} else {
566
			$query = "SELECT flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'month' AND flight_date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '7 DAYS' AND stats_airline = :stats_airline AND filter_name = :filter_name";
567
		}
568
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
569
                 try {
570
                        $sth = $this->db->prepare($query);
571
                        $sth->execute($query_data);
572
                } catch(PDOException $e) {
573
                        echo "error : ".$e->getMessage();
574
                }
575
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
576
                if (empty($all)) {
577
			$filters = array('airlines' => array($stats_airline));
578
			if ($filter_name != '') {
579
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
580
			}
581
            		$Spotter = new Spotter($this->db);
582
            		$all = $Spotter->countAllDatesLast7Days($filters);
583
                }
584
                return $all;
585
	}
586
	public function countAllDates($stats_airline = '',$filter_name = '') {
587
		global $globalStatsFilters;
588
		if ($filter_name == '') $filter_name = $this->filter_name;
589
		$query = "SELECT flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'date' AND stats_airline = :stats_airline AND filter_name = :filter_name";
590
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
591
                 try {
592
                        $sth = $this->db->prepare($query);
593
                        $sth->execute($query_data);
594
                } catch(PDOException $e) {
595
                        echo "error : ".$e->getMessage();
596
                }
597
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
598
                if (empty($all)) {
599
			$filters = array('airlines' => array($stats_airline));
600
			if ($filter_name != '') {
601
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
602
			}
603
            		$Spotter = new Spotter($this->db);
604
            		$all = $Spotter->countAllDates($filters);
605
                }
606
                return $all;
607
	}
608
	public function countAllDatesByAirlines($filter_name = '') {
609
		global $globalStatsFilters;
610
		if ($filter_name == '') $filter_name = $this->filter_name;
611
		$query = "SELECT stats_airline as airline_icao, flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'date' AND filter_name = :filter_name";
612
		$query_data = array('filter_name' => $filter_name);
613
                 try {
614
                        $sth = $this->db->prepare($query);
615
                        $sth->execute($query_data);
616
                } catch(PDOException $e) {
617
                        echo "error : ".$e->getMessage();
618
                }
619
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
620
                if (empty($all)) {
621
            		$filters = array();
622
            		if ($filter_name != '') {
623
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
624
			}
625
            		$Spotter = new Spotter($this->db);
626
            		$all = $Spotter->countAllDatesByAirlines($filters);
627
                }
628
                return $all;
629
	}
630
	public function countAllMonths($stats_airline = '',$filter_name = '') {
631
		global $globalStatsFilters;
632
		if ($filter_name == '') $filter_name = $this->filter_name;
633
	    	$query = "SELECT YEAR(stats_date) AS year_name,MONTH(stats_date) AS month_name, cnt as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_airline = :stats_airline AND filter_name = :filter_name";
634
                 try {
635
                        $sth = $this->db->prepare($query);
636
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
637
                } catch(PDOException $e) {
638
                        echo "error : ".$e->getMessage();
639
                }
640
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
641
                if (empty($all)) {
642
			$filters = array('airlines' => array($stats_airline));
643
			if ($filter_name != '') {
644
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
645
			}
646
            		$Spotter = new Spotter($this->db);
647
            		$all = $Spotter->countAllMonths($filters);
648
                }
649
                return $all;
650
	}
651
	public function countAllMilitaryMonths($filter_name = '') {
652
		global $globalStatsFilters;
653
		if ($filter_name == '') $filter_name = $this->filter_name;
654
	    	$query = "SELECT YEAR(stats_date) AS year_name,MONTH(stats_date) AS month_name, cnt as date_count FROM stats WHERE stats_type = 'military_flights_bymonth' AND filter_name = :filter_name";
655
                 try {
656
                        $sth = $this->db->prepare($query);
657
                        $sth->execute(array(':filter_name' => $filter_name));
658
                } catch(PDOException $e) {
659
                        echo "error : ".$e->getMessage();
660
                }
661
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
662
                if (empty($all)) {
663
            		$filters = array();
664
            		if ($filter_name != '') {
665
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
666
			}
667
            		$Spotter = new Spotter($this->db);
668
            		$all = $Spotter->countAllMilitaryMonths($filters);
669
                }
670
                return $all;
671
	}
672
	public function countAllHours($orderby = 'hour',$limit = true,$stats_airline = '',$filter_name = '') {
673
		global $globalTimezone, $globalDBdriver, $globalStatsFilters;
674
		if ($filter_name == '') $filter_name = $this->filter_name;
675
		if ($limit) $query = "SELECT flight_date as hour_name, cnt as hour_count FROM stats_flight WHERE stats_type = 'hour' AND stats_airline = :stats_airline AND filter_name = :filter_name";
676
		else $query = "SELECT flight_date as hour_name, cnt as hour_count FROM stats_flight WHERE stats_type = 'hour' AND stats_airline = :stats_airline AND filter_name = :filter_name";
677
		if ($orderby == 'hour') {
678
			/*
679
			if ($globalDBdriver == 'mysql') {
680
				$query .= " ORDER BY flight_date ASC";
681
			} else {
682
			*/
683
			$query .= " ORDER BY CAST(flight_date AS integer) ASC";
684
		}
685
		if ($orderby == 'count') $query .= " ORDER BY hour_count DESC";
686
                 try {
687
                        $sth = $this->db->prepare($query);
688
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
689
                } catch(PDOException $e) {
690
                        echo "error : ".$e->getMessage();
691
                }
692
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
693
                if (empty($all)) {
694
			$filters = array('airlines' => array($stats_airline));
695
			if ($filter_name != '') {
696
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
697
			}
698
            		$Spotter = new Spotter($this->db);
699
            		$all = $Spotter->countAllHours($orderby,$filters);
700
                }
701
                return $all;
702
	}
703
	
704
	public function countOverallFlights($stats_airline = '', $filter_name = '',$year = '',$month = '') {
705
		global $globalStatsFilters;
706
		if ($filter_name == '') $filter_name = $this->filter_name;
707
		if ($year == '') $year = date('Y');
708
		$all = $this->getSumStats('flights_bymonth',$year,$stats_airline,$filter_name,$month);
709
		if (empty($all)) {
710
			$filters = array('airlines' => array($stats_airline));
711
			if ($filter_name != '') {
712
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
713
			}
714
			$Spotter = new Spotter($this->db);
715
			$all = $Spotter->countOverallFlights($filters,$year,$month);
716
		}
717
		return $all;
718
	}
719
	public function countOverallMilitaryFlights($filter_name = '',$year = '', $month = '') {
720
		global $globalStatsFilters;
721
		if ($filter_name == '') $filter_name = $this->filter_name;
722
		if ($year == '') $year = date('Y');
723
		$all = $this->getSumStats('military_flights_bymonth',$year,'',$filter_name,$month);
724
		if (empty($all)) {
725
		        $filters = array();
726
            		if ($filter_name != '') {
727
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
728
			}
729
			$Spotter = new Spotter($this->db);
730
			$all = $Spotter->countOverallMilitaryFlights($filters,$year,$month);
731
		}
732
		return $all;
733
	}
734
	public function countOverallArrival($stats_airline = '',$filter_name = '', $year = '', $month = '') {
735
		global $globalStatsFilters;
736
		if ($filter_name == '') $filter_name = $this->filter_name;
737
		if ($year == '') $year = date('Y');
738
		$all = $this->getSumStats('realarrivals_bymonth',$year,$stats_airline,$filter_name,$month);
739
		if (empty($all)) {
740
			$filters = array('airlines' => array($stats_airline));
741
			if ($filter_name != '') {
742
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
743
			}
744
			$Spotter = new Spotter($this->db);
745
			$all = $Spotter->countOverallArrival($filters,$year,$month);
746
		}
747
		return $all;
748
	}
749
	public function countOverallAircrafts($stats_airline = '',$filter_name = '',$year = '', $month = '') {
750
		global $globalStatsFilters;
751
		if ($filter_name == '') $filter_name = $this->filter_name;
752
		if ($year == '') $year = date('Y');
753
		$all = $this->getSumStats('aircrafts_bymonth',$year,$stats_airline,$filter_name,$month);
754
		if (empty($all)) {
755
			$filters = array('airlines' => array($stats_airline));
756
			if ($filter_name != '') {
757
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
758
			}
759
			$Spotter = new Spotter($this->db);
760
			$all = $Spotter->countOverallAircrafts($filters,$year,$month);
761
		}
762
		return $all;
763
	}
764
	public function countOverallAirlines($filter_name = '',$year = '',$month = '') {
765
		global $globalStatsFilters;
766
		if ($filter_name == '') $filter_name = $this->filter_name;
767
		if ($year == '' && $month == '') {
768
			$query = "SELECT COUNT(*) AS nb_airline FROM stats_airline WHERE filter_name = :filter_name";
769
			try {
770
				$sth = $this->db->prepare($query);
771
				$sth->execute(array(':filter_name' => $filter_name));
772
			} catch(PDOException $e) {
773
				echo "error : ".$e->getMessage();
774
			}
775
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
776
			$all = $result[0]['nb_airline'];
777
		} else $all = $this->getSumStats('airlines_bymonth',$year,'',$filter_name,$month);
778
		if (empty($all)) {
779
            		$filters = array();
780
            		if ($filter_name != '') {
781
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
782
			}
783
			$Spotter = new Spotter($this->db);
784
			$all = $Spotter->countOverallAirlines($filters,$year,$month);
785
		}
786
		return $all;
787
	}
788
	public function countOverallOwners($stats_airline = '',$filter_name = '',$year = '', $month = '') {
789
		global $globalStatsFilters;
790
		if ($filter_name == '') $filter_name = $this->filter_name;
791
		if ($year == '') $year = date('Y');
792
		/*
793
		$query = "SELECT COUNT(*) AS nb_owner FROM stats_owner";
794
                 try {
795
                        $sth = $this->db->prepare($query);
796
                        $sth->execute();
797
                } catch(PDOException $e) {
798
                        echo "error : ".$e->getMessage();
799
                }
800
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
801
                $all = $result[0]['nb_owner'];
802
                */
803
		$all = $this->getSumStats('owners_bymonth',$year,$stats_airline,$filter_name,$month);
804
		if (empty($all)) {
805
			$filters = array('airlines' => array($stats_airline));
806
			if ($filter_name != '') {
807
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
808
			}
809
			$Spotter = new Spotter($this->db);
810
			$all = $Spotter->countOverallOwners($filters,$year,$month);
811
		}
812
		return $all;
813
	}
814
	public function countOverallPilots($stats_airline = '',$filter_name = '',$year = '',$month = '') {
815
		global $globalStatsFilters;
816
		if ($filter_name == '') $filter_name = $this->filter_name;
817
		if ($year == '') $year = date('Y');
818
		$all = $this->getSumStats('pilots_bymonth',$year,$stats_airline,$filter_name,$month);
819
		if (empty($all)) {
820
			$filters = array('airlines' => array($stats_airline));
821
			if ($filter_name != '') {
822
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
823
			}
824
			$Spotter = new Spotter($this->db);
825
			$all = $Spotter->countOverallPilots($filters,$year,$month);
826
		}
827
		return $all;
828
	}
829
830
	public function getLast7DaysAirports($airport_icao = '', $stats_airline = '',$filter_name = '') {
831
		if ($filter_name == '') $filter_name = $this->filter_name;
832
		$query = "SELECT * FROM stats_airport WHERE stats_type = 'daily' AND airport_icao = :airport_icao AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY date";
833
		$query_values = array(':airport_icao' => $airport_icao,':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
834
                 try {
835
                        $sth = $this->db->prepare($query);
836
                        $sth->execute($query_values);
837
                } catch(PDOException $e) {
838
                        echo "error : ".$e->getMessage();
839
                }
840
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
841
                return $all;
842
	}
843
	public function getStats($type,$stats_airline = '', $filter_name = '') {
844
		if ($filter_name == '') $filter_name = $this->filter_name;
845
                $query = "SELECT * FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY stats_date";
846
                $query_values = array(':type' => $type,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
847
                 try {
848
                        $sth = $this->db->prepare($query);
849
                        $sth->execute($query_values);
850
                } catch(PDOException $e) {
851
                        echo "error : ".$e->getMessage();
852
                }
853
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
854
                return $all;
855
        }
856
	public function getSumStats($type,$year,$stats_airline = '',$filter_name = '',$month = '') {
857
		if ($filter_name == '') $filter_name = $this->filter_name;
858
    		global $globalArchiveMonths, $globalDBdriver;
859
    		if ($globalDBdriver == 'mysql') {
860
    			if ($month == '') {
861
				$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND YEAR(stats_date) = :year AND stats_airline = :stats_airline AND filter_name = :filter_name";
862
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
863
			} else {
864
				$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND YEAR(stats_date) = :year AND MONTH(stats_date) = :month AND stats_airline = :stats_airline AND filter_name = :filter_name";
865
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
866
			}
867
		} else {
868
			if ($month == '') {
869
				$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND EXTRACT(YEAR FROM stats_date) = :year AND stats_airline = :stats_airline AND filter_name = :filter_name";
870
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
871
			} else {
872
				$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND EXTRACT(YEAR FROM stats_date) = :year AND EXTRACT(MONTH FROM stats_date) = :month AND stats_airline = :stats_airline AND filter_name = :filter_name";
873
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
874
			}
875
                }
876
                 try {
877
                        $sth = $this->db->prepare($query);
878
                        $sth->execute($query_values);
879
                } catch(PDOException $e) {
880
                        echo "error : ".$e->getMessage();
881
                }
882
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
883
                return $all[0]['total'];
884
        }
885
	public function getStatsTotal($type, $stats_airline = '', $filter_name = '') {
886
    		global $globalArchiveMonths, $globalDBdriver;
887
		if ($filter_name == '') $filter_name = $this->filter_name;
888
    		if ($globalDBdriver == 'mysql') {
889
			$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND stats_date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL ".$globalArchiveMonths." MONTH) AND stats_airline = :stats_airline AND filter_name = :filter_name";
890
		} else {
891
			$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND stats_date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS' AND stats_airline = :stats_airline AND filter_name = :filter_name";
892
                }
893
                $query_values = array(':type' => $type, ':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
894
                 try {
895
                        $sth = $this->db->prepare($query);
896
                        $sth->execute($query_values);
897
                } catch(PDOException $e) {
898
                        echo "error : ".$e->getMessage();
899
                }
900
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
901
                return $all[0]['total'];
902
        }
903
	public function getStatsAircraftTotal($stats_airline = '', $filter_name = '') {
904
    		global $globalArchiveMonths, $globalDBdriver;
905
		if ($filter_name == '') $filter_name = $this->filter_name;
906
    		if ($globalDBdriver == 'mysql') {
907
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
908
                } else {
909
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
910
                }
911
                 try {
912
                        $sth = $this->db->prepare($query);
913
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
914
                } catch(PDOException $e) {
915
                        echo "error : ".$e->getMessage();
916
                }
917
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
918
                return $all[0]['total'];
919
        }
920
	public function getStatsAirlineTotal($filter_name = '') {
921
    		global $globalArchiveMonths, $globalDBdriver;
922
		if ($filter_name == '') $filter_name = $this->filter_name;
923
    		if ($globalDBdriver == 'mysql') {
924
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
925
                } else {
926
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
927
                }
928
                 try {
929
                        $sth = $this->db->prepare($query);
930
                        $sth->execute(array(':filter_name' => $filter_name));
931
                } catch(PDOException $e) {
932
                        echo "error : ".$e->getMessage();
933
                }
934
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
935
                return $all[0]['total'];
936
        }
937
	public function getStatsOwnerTotal($filter_name = '') {
938
    		global $globalArchiveMonths, $globalDBdriver;
939
		if ($filter_name == '') $filter_name = $this->filter_name;
940
    		if ($globalDBdriver == 'mysql') {
941
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
942
		} else {
943
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
944
                }
945
                 try {
946
                        $sth = $this->db->prepare($query);
947
                        $sth->execute(array(':filter_name' => $filter_name));
948
                } catch(PDOException $e) {
949
                        echo "error : ".$e->getMessage();
950
                }
951
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
952
                return $all[0]['total'];
953
        }
954
	public function getStatsPilotTotal($filter_name = '') {
955
    		global $globalArchiveMonths, $globalDBdriver;
956
		if ($filter_name == '') $filter_name = $this->filter_name;
957
    		if ($globalDBdriver == 'mysql') {
958
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
959
            	} else {
960
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
961
            	}
962
                 try {
963
                        $sth = $this->db->prepare($query);
964
                        $sth->execute(array(':filter_name' => $filter_name));
965
                } catch(PDOException $e) {
966
                        echo "error : ".$e->getMessage();
967
                }
968
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
969
                return $all[0]['total'];
970
        }
971
972
	public function addStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
973
		global $globalDBdriver;
974
		if ($filter_name == '') $filter_name = $this->filter_name;
975
		if ($globalDBdriver == 'mysql') {
976
			$query = "INSERT INTO stats (stats_type,cnt,stats_date,stats_airline,filter_name) VALUES (:type,:cnt,:stats_date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt";
977
                } else {
978
			$query = "UPDATE stats SET cnt = :cnt WHERE stats_type = :type AND stats_date = :stats_date AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats (stats_type,cnt,stats_date,stats_airline,filter_name) SELECT :type,:cnt,:stats_date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats WHERE  stats_type = :type AND stats_date = :stats_date AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
979
		}
980
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
981
                 try {
982
                        $sth = $this->db->prepare($query);
983
                        $sth->execute($query_values);
984
                } catch(PDOException $e) {
985
                        return "error : ".$e->getMessage();
986
                }
987
        }
988
	public function updateStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
989
		global $globalDBdriver;
990
		if ($filter_name == '') $filter_name = $this->filter_name;
991
		if ($globalDBdriver == 'mysql') {
992
			$query = "INSERT INTO stats (stats_type,cnt,stats_date,stats_airline,filter_name) VALUES (:type,:cnt,:stats_date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
993
		} else {
994
            		//$query = "INSERT INTO stats (stats_type,cnt,stats_date) VALUES (:type,:cnt,:stats_date) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
995
			$query = "UPDATE stats SET cnt = cnt+:cnt WHERE stats_type = :type AND stats_date = :stats_date AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats (stats_type,cnt,stats_date,stats_airline,filter_name) SELECT :type,:cnt,:stats_date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats WHERE  stats_type = :type AND stats_date = :stats_date AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
996
                }
997
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
998
                 try {
999
                        $sth = $this->db->prepare($query);
1000
                        $sth->execute($query_values);
1001
                } catch(PDOException $e) {
1002
                        return "error : ".$e->getMessage();
1003
                }
1004
        }
1005
        /*
1006
	public function getStatsSource($date,$stats_type = '') {
1007
		if ($stats_type == '') {
1008
			$query = "SELECT * FROM stats_source WHERE stats_date = :date ORDER BY source_name";
1009
			$query_values = array(':date' => $date);
1010
		} else {
1011
			$query = "SELECT * FROM stats_source WHERE stats_date = :date AND stats_type = :stats_type ORDER BY source_name";
1012
			$query_values = array(':date' => $date,':stats_type' => $stats_type);
1013
		}
1014
                 try {
1015
                        $sth = $this->db->prepare($query);
1016
                        $sth->execute($query_values);
1017
                } catch(PDOException $e) {
1018
                        echo "error : ".$e->getMessage();
1019
                }
1020
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1021
                return $all;
1022
        }
1023
        */
1024
1025
	public function getStatsSource($stats_type,$year = '',$month = '',$day = '') {
1026
		global $globalDBdriver;
1027
		$query = "SELECT * FROM stats_source WHERE stats_type = :stats_type";
1028
		$query_values = array();
1029
		if ($globalDBdriver == 'mysql') {
1030
			if ($year != '') {
1031
				$query .= ' AND YEAR(stats_date) = :year';
1032
				$query_values = array_merge($query_values,array(':year' => $year));
1033
			}
1034
			if ($month != '') {
1035
				$query .= ' AND MONTH(stats_date) = :month';
1036
				$query_values = array_merge($query_values,array(':month' => $month));
1037
			}
1038
			if ($day != '') {
1039
				$query .= ' AND DAY(stats_date) = :day';
1040
				$query_values = array_merge($query_values,array(':day' => $day));
1041
			}
1042
		} else {
1043
			if ($year != '') {
1044
				$query .= ' AND EXTRACT(YEAR FROM stats_date) = :year';
1045
				$query_values = array_merge($query_values,array(':year' => $year));
1046
			}
1047
			if ($month != '') {
1048
				$query .= ' AND EXTRACT(MONTH FROM stats_date) = :month';
1049
				$query_values = array_merge($query_values,array(':month' => $month));
1050
			}
1051
			if ($day != '') {
1052
				$query .= ' AND EXTRACT(DAY FROM stats_date) = :day';
1053
				$query_values = array_merge($query_values,array(':day' => $day));
1054
			}
1055
		}
1056
		$query .= " ORDER BY source_name";
1057
		$query_values = array_merge($query_values,array(':stats_type' => $stats_type));
1058
		try {
1059
			$sth = $this->db->prepare($query);
1060
			$sth->execute($query_values);
1061
		} catch(PDOException $e) {
1062
			echo "error : ".$e->getMessage();
1063
		}
1064
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1065
		return $all;
1066
	}
1067
1068
	public function addStatSource($data,$source_name,$stats_type,$date) {
1069
		global $globalDBdriver;
1070
		if ($globalDBdriver == 'mysql') {
1071
			$query = "INSERT INTO stats_source (source_data,source_name,stats_type,stats_date) VALUES (:data,:source_name,:stats_type,:stats_date) ON DUPLICATE KEY UPDATE source_data = :data";
1072
		} else {
1073
			$query = "UPDATE stats_source SET source_data = :data WHERE stats_date = :stats_date AND source_name = :source_name AND stats_type = :stats_type; INSERT INTO stats_source (source_data,source_name,stats_type,stats_date) SELECT :data,:source_name,:stats_type,:stats_date WHERE NOT EXISTS (SELECT 1 FROM stats_source WHERE stats_date = :stats_date AND source_name = :source_name AND stats_type = :stats_type);"; 
1074
                }
1075
                $query_values = array(':data' => $data,':stats_date' => $date,':source_name' => $source_name,':stats_type' => $stats_type);
1076
                 try {
1077
                        $sth = $this->db->prepare($query);
1078
                        $sth->execute($query_values);
1079
                } catch(PDOException $e) {
1080
                        return "error : ".$e->getMessage();
1081
                }
1082
        }
1083
	public function addStatFlight($type,$date_name,$cnt,$stats_airline = '',$filter_name = '') {
1084
                $query = "INSERT INTO stats_flight (stats_type,flight_date,cnt,stats_airline,filter_name) VALUES (:type,:flight_date,:cnt,:stats_airline,:filter_name)";
1085
                $query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1086
                 try {
1087
                        $sth = $this->db->prepare($query);
1088
                        $sth->execute($query_values);
1089
                } catch(PDOException $e) {
1090
                        return "error : ".$e->getMessage();
1091
                }
1092
        }
1093
	public function addStatAircraftRegistration($registration,$cnt,$aircraft_icao = '',$airline_icao = '',$filter_name = '',$reset = false) {
1094
		global $globalDBdriver;
1095
		if ($globalDBdriver == 'mysql') {
1096
			if ($reset) {
1097
				$query = "INSERT INTO stats_registration (aircraft_icao,registration,cnt,stats_airline,filter_name) VALUES (:aircraft_icao,:registration,:cnt,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt";
1098
			} else {
1099
				$query = "INSERT INTO stats_registration (aircraft_icao,registration,cnt,stats_airline,filter_name) VALUES (:aircraft_icao,:registration,:cnt,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
1100
			}
1101
		} else {
1102
			if ($reset) {
1103
				$query = "UPDATE stats_registration SET cnt = :cnt WHERE registration = :registration AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_registration (aircraft_icao,registration,cnt,stats_airline,filter_name) SELECT :aircraft_icao,:registration,:cnt,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_registration WHERE registration = :registration AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1104
			} else {
1105
				$query = "UPDATE stats_registration SET cnt = cnt+:cnt WHERE registration = :registration AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_registration (aircraft_icao,registration,cnt,stats_airline,filter_name) SELECT :aircraft_icao,:registration,:cnt,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_registration WHERE registration = :registration AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1106
			}
1107
		}
1108
                $query_values = array(':aircraft_icao' => $aircraft_icao,':registration' => $registration,':cnt' => $cnt,':stats_airline' => $airline_icao, ':filter_name' => $filter_name);
1109
                 try {
1110
                        $sth = $this->db->prepare($query);
1111
                        $sth->execute($query_values);
1112
                } catch(PDOException $e) {
1113
                        return "error : ".$e->getMessage();
1114
                }
1115
        }
1116
	public function addStatCallsign($callsign_icao,$cnt,$airline_icao = '', $filter_name = '', $reset = false) {
1117
		global $globalDBdriver;
1118
		if ($globalDBdriver == 'mysql') {
1119
			if ($reset) {
1120
				$query = "INSERT INTO stats_callsign (callsign_icao,airline_icao,cnt,filter_name) VALUES (:callsign_icao,:airline_icao,:cnt,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt";
1121
			} else {
1122
				$query = "INSERT INTO stats_callsign (callsign_icao,airline_icao,cnt,filter_name) VALUES (:callsign_icao,:airline_icao,:cnt,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
1123
			}
1124
		} else {
1125
			if ($reset) {
1126
				$query = "UPDATE stats_callsign SET cnt = :cnt WHERE callsign_icao = :callsign_icao AND filter_name = :filter_name; INSERT INTO stats_callsign (callsign_icao,airline_icao,cnt,filter_name) SELECT :callsign_icao,:airline_icao,:cnt,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_callsign WHERE callsign_icao = :callsign_icao AND filter_name = :filter_name);"; 
1127
			} else {
1128
				$query = "UPDATE stats_callsign SET cnt = cnt+:cnt WHERE callsign_icao = :callsign_icao AND filter_name = :filter_name; INSERT INTO stats_callsign (callsign_icao,airline_icao,cnt,filter_name) SELECT :callsign_icao,:airline_icao,:cnt,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_callsign WHERE callsign_icao = :callsign_icao AND filter_name = :filter_name);"; 
1129
			}
1130
		}
1131
                $query_values = array(':callsign_icao' => $callsign_icao,':airline_icao' => $airline_icao,':cnt' => $cnt, ':filter_name' => $filter_name);
1132
                 try {
1133
                        $sth = $this->db->prepare($query);
1134
                        $sth->execute($query_values);
1135
                } catch(PDOException $e) {
1136
                        return "error : ".$e->getMessage();
1137
                }
1138
        }
1139
	public function addStatCountry($iso2,$iso3,$name,$cnt,$filter_name = '',$reset = false) {
1140
		global $globalDBdriver;
1141
		if ($globalDBdriver == 'mysql') {
1142
			if ($reset) {
1143
				$query = "INSERT INTO stats_country (iso2,iso3,name,cnt,filter_name) VALUES (:iso2,:iso3,:name,:cnt,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt";
1144
			} else {
1145
				$query = "INSERT INTO stats_country (iso2,iso3,name,cnt,filter_name) VALUES (:iso2,:iso3,:name,:cnt,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
1146
			}
1147
		} else {
1148
			if ($reset) {
1149
				$query = "UPDATE stats_country SET cnt = :cnt WHERE iso2 = :iso2 AND filter_name = :filter_name; INSERT INTO stats_country (iso2,iso3,name,cnt,filter_name) SELECT :iso2,:iso3,:name,:cnt,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_country WHERE iso2 = :iso2 AND filter_name = :filter_name);"; 
1150
			} else {
1151
				$query = "UPDATE stats_country SET cnt = cnt+:cnt WHERE iso2 = :iso2 AND filter_name = :filter_name; INSERT INTO stats_country (iso2,iso3,name,cnt,filter_name) SELECT :iso2,:iso3,:name,:cnt,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_country WHERE iso2 = :iso2 AND filter_name = :filter_name);"; 
1152
			}
1153
		}
1154
                $query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt,':filter_name' => $filter_name);
1155
                 try {
1156
                        $sth = $this->db->prepare($query);
1157
                        $sth->execute($query_values);
1158
                } catch(PDOException $e) {
1159
                        return "error : ".$e->getMessage();
1160
                }
1161
        }
1162
	public function addStatAircraft($aircraft_icao,$cnt,$aircraft_name = '',$aircraft_manufacturer = '', $airline_icao = '', $filter_name = '', $reset = false) {
1163
		global $globalDBdriver;
1164
		if ($globalDBdriver == 'mysql') {
1165
			if ($reset) {
1166
				$query = "INSERT INTO stats_aircraft (aircraft_icao,aircraft_name,aircraft_manufacturer,cnt,stats_airline, filter_name) VALUES (:aircraft_icao,:aircraft_name,:aircraft_manufacturer,:cnt,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt, aircraft_name = :aircraft_name, aircraft_manufacturer = :aircraft_manufacturer, stats_airline = :stats_airline";
1167
			} else {
1168
				$query = "INSERT INTO stats_aircraft (aircraft_icao,aircraft_name,aircraft_manufacturer,cnt,stats_airline, filter_name) VALUES (:aircraft_icao,:aircraft_name,:aircraft_manufacturer,:cnt,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, aircraft_name = :aircraft_name, aircraft_manufacturer = :aircraft_manufacturer, stats_airline = :stats_airline";
1169
			}
1170
		} else {
1171
			if ($reset) {
1172
				$query = "UPDATE stats_aircraft SET cnt = :cnt, aircraft_name = :aircraft_name, aircraft_manufacturer = :aircraft_manufacturer, filter_name = :filter_name WHERE aircraft_icao = :aircraft_icao AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_aircraft (aircraft_icao,aircraft_name,aircraft_manufacturer,cnt,stats_airline,filter_name) SELECT :aircraft_icao,:aircraft_name,:aircraft_manufacturer,:cnt,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_aircraft WHERE aircraft_icao = :aircraft_icao AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1173
			} else {
1174
				$query = "UPDATE stats_aircraft SET cnt = cnt+:cnt, aircraft_name = :aircraft_name, aircraft_manufacturer = :aircraft_manufacturer, filter_name = :filter_name WHERE aircraft_icao = :aircraft_icao AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_aircraft (aircraft_icao,aircraft_name,aircraft_manufacturer,cnt,stats_airline,filter_name) SELECT :aircraft_icao,:aircraft_name,:aircraft_manufacturer,:cnt,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_aircraft WHERE aircraft_icao = :aircraft_icao AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1175
			}
1176
		}
1177
                $query_values = array(':aircraft_icao' => $aircraft_icao,':aircraft_name' => $aircraft_name,':cnt' => $cnt, ':aircraft_manufacturer' => $aircraft_manufacturer,':stats_airline' => $airline_icao, ':filter_name' => $filter_name);
1178
                 try {
1179
                        $sth = $this->db->prepare($query);
1180
                        $sth->execute($query_values);
1181
                } catch(PDOException $e) {
1182
                        return "error : ".$e->getMessage();
1183
                }
1184
        }
1185
	public function addStatAirline($airline_icao,$cnt,$airline_name = '',$filter_name = '', $reset = false) {
1186
		global $globalDBdriver;
1187
		if ($globalDBdriver == 'mysql') {
1188
			if ($reset) {
1189
				$query = "INSERT INTO stats_airline (airline_icao,airline_name,cnt,filter_name) VALUES (:airline_icao,:airline_name,:cnt,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt,airline_name = :airline_name";
1190
			} else {
1191
				$query = "INSERT INTO stats_airline (airline_icao,airline_name,cnt,filter_name) VALUES (:airline_icao,:airline_name,:cnt,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt,airline_name = :airline_name";
1192
			}
1193
		} else {
1194
			if ($reset) {
1195
				$query = "UPDATE stats_airline SET cnt = :cnt WHERE airline_icao = :airline_icao AND filter_name = :filter_name; INSERT INTO stats_airline (airline_icao,airline_name,cnt,filter_name) SELECT :airline_icao,:airline_name,:cnt,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airline WHERE airline_icao = :airline_icao AND filter_name = :filter_name);"; 
1196
			} else {
1197
				$query = "UPDATE stats_airline SET cnt = cnt+:cnt WHERE airline_icao = :airline_icao AND filter_name = :filter_name; INSERT INTO stats_airline (airline_icao,airline_name,cnt,filter_name) SELECT :airline_icao,:airline_name,:cnt,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airline WHERE airline_icao = :airline_icao AND filter_name = :filter_name);"; 
1198
			}
1199
		}
1200
                $query_values = array(':airline_icao' => $airline_icao,':airline_name' => $airline_name,':cnt' => $cnt,':filter_name' => $filter_name);
1201
                 try {
1202
                        $sth = $this->db->prepare($query);
1203
                        $sth->execute($query_values);
1204
                } catch(PDOException $e) {
1205
                        return "error : ".$e->getMessage();
1206
                }
1207
        }
1208
	public function addStatOwner($owner_name,$cnt,$stats_airline = '', $filter_name = '', $reset = false) {
1209
		global $globalDBdriver;
1210
		if ($globalDBdriver == 'mysql') {
1211
			if ($reset) {
1212
				$query = "INSERT INTO stats_owner (owner_name,cnt,stats_airline,filter_name) VALUES (:owner_name,:cnt,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt";
1213
			} else {
1214
				$query = "INSERT INTO stats_owner (owner_name,cnt,stats_airline,filter_name) VALUES (:owner_name,:cnt,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
1215
			}
1216
		} else {
1217
			if ($reset) {
1218
				$query = "UPDATE stats_owner SET cnt = :cnt WHERE owner_name = :owner_name AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_owner (owner_name,cnt,stats_airline,filter_name) SELECT :owner_name,:cnt,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_owner WHERE owner_name = :owner_name AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1219
			} else {
1220
				$query = "UPDATE stats_owner SET cnt = cnt+:cnt WHERE owner_name = :owner_name AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_owner (owner_name,cnt,stats_airline,filter_name) SELECT :owner_name,:cnt,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_owner WHERE owner_name = :owner_name AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1221
			}
1222
		}
1223
                $query_values = array(':owner_name' => $owner_name,':cnt' => $cnt,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1224
                 try {
1225
                        $sth = $this->db->prepare($query);
1226
                        $sth->execute($query_values);
1227
                } catch(PDOException $e) {
1228
                        return "error : ".$e->getMessage();
1229
                }
1230
        }
1231
	public function addStatPilot($pilot_id,$cnt,$pilot_name,$stats_airline = '',$filter_name = '',$format_source = '',$reset = false) {
1232
		global $globalDBdriver;
1233
		if ($globalDBdriver == 'mysql') {
1234
			if ($reset) {
1235
				$query = "INSERT INTO stats_pilot (pilot_id,cnt,pilot_name,stats_airline,filter_name,format_source) VALUES (:pilot_id,:cnt,:pilot_name,:stats_airline,:filter_name,:format_source) ON DUPLICATE KEY UPDATE cnt = :cnt, pilot_name = :pilot_name";
1236
			} else {
1237
				$query = "INSERT INTO stats_pilot (pilot_id,cnt,pilot_name,stats_airline,filter_name,format_source) VALUES (:pilot_id,:cnt,:pilot_name,:stats_airline,:filter_name,:format_source) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, pilot_name = :pilot_name";
1238
			}
1239
		} else {
1240
			if ($reset) {
1241
				$query = "UPDATE stats_pilot SET cnt = :cnt, pilot_name = :pilot_name WHERE pilot_id = :pilot_id AND stats_airline = :stats_airline AND filter_name = :filter_name AND format_source = :format_source; INSERT INTO stats_pilot (pilot_id,cnt,pilot_name,stats_airline,filter_name,format_source) SELECT :pilot_id,:cnt,:pilot_name,:stats_airline,:filter_name,:format_source WHERE NOT EXISTS (SELECT 1 FROM stats_pilot WHERE pilot_id = :pilot_id AND stats_airline = :stats_airline AND filter_name = :filter_name AND format_source = :format_source);"; 
1242
			} else {
1243
				$query = "UPDATE stats_pilot SET cnt = cnt+:cnt, pilot_name = :pilot_name WHERE pilot_id = :pilot_id AND stats_airline = :stats_airline AND filter_name = :filter_name AND format_source = :format_source; INSERT INTO stats_pilot (pilot_id,cnt,pilot_name,stats_airline,filter_name,format_source) SELECT :pilot_id,:cnt,:pilot_name,:stats_airline,:filter_name,:format_source WHERE NOT EXISTS (SELECT 1 FROM stats_pilot WHERE pilot_id = :pilot_id AND stats_airline = :stats_airline AND filter_name = :filter_name AND format_source = :format_source);"; 
1244
			}
1245
		}
1246
                $query_values = array(':pilot_id' => $pilot_id,':cnt' => $cnt,':pilot_name' => $pilot_name,':stats_airline' => $stats_airline,':filter_name' => $filter_name,':format_source' => $format_source);
1247
                 try {
1248
                        $sth = $this->db->prepare($query);
1249
                        $sth->execute($query_values);
1250
                } catch(PDOException $e) {
1251
                        return "error : ".$e->getMessage();
1252
                }
1253
        }
1254
	public function addStatDepartureAirports($airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '',$reset = false) {
1255
		global $globalDBdriver;
1256
		if ($airport_icao != '') {
1257
			if ($globalDBdriver == 'mysql') {
1258
				if ($reset) {
1259
					$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline,filter_name) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:departure,'yearly',:date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE departure = :departure";
1260
				} else {
1261
					$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline,filter_name) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:departure,'yearly',:date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE departure = departure+:departure";
1262
				}
1263
			} else {
1264
				if ($reset) {
1265
					$query = "UPDATE stats_airport SET departure = :departure WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline,filter_name) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:departure,'yearly',:date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name);"; 
1266
				} else {
1267
					$query = "UPDATE stats_airport SET departure = departure+:departure WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline,filter_name) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:departure,'yearly',:date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name);"; 
1268
				}
1269
			}
1270
			$query_values = array(':airport_icao' => $airport_icao,':airport_name' => $airport_name,':airport_city' => $airport_city,':airport_country' => $airport_country,':departure' => $departure,':date' => date('Y').'-01-01 00:00:00', ':stats_airline' => $airline_icao,':filter_name' => $filter_name);
1271
			try {
1272
				$sth = $this->db->prepare($query);
1273
				$sth->execute($query_values);
1274
			} catch(PDOException $e) {
1275
				return "error : ".$e->getMessage();
1276
			}
1277
                }
1278
        }
1279
	public function addStatDepartureAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '') {
1280
		global $globalDBdriver;
1281
		if ($airport_icao != '') {
1282
			if ($globalDBdriver == 'mysql') {
1283
				$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline,filter_name) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:departure,'daily',:date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE departure = :departure";
1284
			} else {
1285
				$query = "UPDATE stats_airport SET departure = :departure WHERE airport_icao = :airport_icao AND stats_type = 'daily' AND date = :date AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline,filter_name) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:departure,'daily',:date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'daily' AND date = :date AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1286
			}
1287
			$query_values = array(':airport_icao' => $airport_icao,':airport_name' => $airport_name,':airport_city' => $airport_city,':airport_country' => $airport_country,':departure' => $departure,':date' => $date,':stats_airline' => $airline_icao,':filter_name' => $filter_name);
1288
			 try {
1289
				$sth = $this->db->prepare($query);
1290
				$sth->execute($query_values);
1291
			} catch(PDOException $e) {
1292
				return "error : ".$e->getMessage();
1293
			}
1294
                }
1295
        }
1296
	public function addStatArrivalAirports($airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '',$reset = false) {
1297
		global $globalDBdriver;
1298
		if ($airport_icao != '') {
1299
			if ($globalDBdriver == 'mysql') {
1300
				if ($reset) {
1301
					$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline,filter_name) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'yearly',:date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE arrival = :arrival";
1302
				} else {
1303
					$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline,filter_name) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'yearly',:date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE arrival = arrival+:arrival";
1304
				}
1305
			} else {
1306
				if ($reset) {
1307
					$query = "UPDATE stats_airport SET arrival = :arrival WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline,filter_name) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'yearly',:date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name);"; 
1308
				} else {
1309
					$query = "UPDATE stats_airport SET arrival = arrival+:arrival WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline,filter_name) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'yearly',:date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline AND date = :date AND filter_name = :filter_name);"; 
1310
				}
1311
			}
1312
	                $query_values = array(':airport_icao' => $airport_icao,':airport_name' => $airport_name,':airport_city' => $airport_city,':airport_country' => $airport_country,':arrival' => $arrival,':date' => date('Y').'-01-01 00:00:00',':stats_airline' => $airline_icao,':filter_name' => $filter_name);
1313
			 try {
1314
                    		$sth = $this->db->prepare($query);
1315
	                        $sth->execute($query_values);
1316
    		        } catch(PDOException $e) {
1317
            		        return "error : ".$e->getMessage();
1318
	                }
1319
	        }
1320
        }
1321
	public function addStatArrivalAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '') {
1322
		global $globalDBdriver;
1323
		if ($airport_icao != '') {
1324
			if ($globalDBdriver == 'mysql') {
1325
				$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline,filter_name) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'daily',:date,:stats_airline,:filter_name) ON DUPLICATE KEY UPDATE arrival = :arrival";
1326
			} else {
1327
				$query = "UPDATE stats_airport SET arrival = :arrival WHERE airport_icao = :airport_icao AND stats_type = 'daily' AND date = :date AND stats_airline = :stats_airline AND filter_name = :filter_name; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline,filter_name) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'daily',:date,:stats_airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'daily' AND date = :date AND stats_airline = :stats_airline AND filter_name = :filter_name);"; 
1328
			}
1329
			$query_values = array(':airport_icao' => $airport_icao,':airport_name' => $airport_name,':airport_city' => $airport_city,':airport_country' => $airport_country,':arrival' => $arrival, ':date' => $date,':stats_airline' => $airline_icao,':filter_name' => $filter_name);
1330
			try {
1331
				$sth = $this->db->prepare($query);
1332
				$sth->execute($query_values);
1333
			} catch(PDOException $e) {
1334
				return "error : ".$e->getMessage();
1335
			}
1336
                }
1337
        }
1338
1339
	public function deleteStat($id) {
1340
                $query = "DELETE FROM stats WHERE stats_id = :id";
1341
                $query_values = array(':id' => $id);
1342
                 try {
1343
                        $sth = $this->db->prepare($query);
1344
                        $sth->execute($query_values);
1345
                } catch(PDOException $e) {
1346
                        return "error : ".$e->getMessage();
1347
                }
1348
        }
1349
	public function deleteStatFlight($type) {
1350
                $query = "DELETE FROM stats_flight WHERE stats_type = :type";
1351
                $query_values = array(':type' => $type);
1352
                 try {
1353
                        $sth = $this->db->prepare($query);
1354
                        $sth->execute($query_values);
1355
                } catch(PDOException $e) {
1356
                        return "error : ".$e->getMessage();
1357
                }
1358
        }
1359
	public function deleteStatAirport($type) {
1360
                $query = "DELETE FROM stats_airport WHERE stats_type = :type";
1361
                $query_values = array(':type' => $type);
1362
                 try {
1363
                        $sth = $this->db->prepare($query);
1364
                        $sth->execute($query_values);
1365
                } catch(PDOException $e) {
1366
                        return "error : ".$e->getMessage();
1367
                }
1368
        }
1369
        
1370
        public function addOldStats() {
1371
    		global $globalDebug, $globalArchiveMonths, $globalArchive, $globalArchiveYear, $globalDBdriver, $globalStatsFilters,$globalDeleteLastYearStats,$globalStatsReset,$globalStatsResetYear;
1372
    		$Common = new Common();
1373
    		$Connection = new Connection();
1374
    		date_default_timezone_set('UTC');
1375
    		$last_update = $this->getLastStatsUpdate('last_update_stats');
1376
			if ($globalDebug) echo 'Update stats !'."\n";
1377
			if (isset($last_update[0]['value'])) {
1378
				$last_update_day = $last_update[0]['value'];
1379
			} else $last_update_day = '2012-12-12 12:12:12';
1380
			$reset = false;
1381
			if ($globalStatsResetYear) {
1382
				$reset = true;
1383
				$last_update_day = date('Y').'-01-01 00:00:00';
1384
			}
1385
			$Spotter = new Spotter($this->db);
1386
1387
			if ($globalDebug) echo 'Count all aircraft types...'."\n";
1388
			$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day);
1389
			foreach ($alldata as $number) {
1390
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'','',$reset);
1391
			}
1392
			if ($globalDebug) echo 'Count all airlines...'."\n";
1393
			$alldata = $Spotter->countAllAirlines(false,0,$last_update_day);
1394
			foreach ($alldata as $number) {
1395
				$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],'',$reset);
1396
			}
1397
			if ($globalDebug) echo 'Count all registrations...'."\n";
1398
			$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day);
1399
			foreach ($alldata as $number) {
1400
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'','',$reset);
1401
			}
1402
			if ($globalDebug) echo 'Count all callsigns...'."\n";
1403
			$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day);
1404
			foreach ($alldata as $number) {
1405
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
1406
			}
1407
			if ($globalDebug) echo 'Count all owners...'."\n";
1408
			$alldata = $Spotter->countAllOwners(false,0,$last_update_day);
1409
			foreach ($alldata as $number) {
1410
				$this->addStatOwner($number['owner_name'],$number['owner_count'],'','',$reset);
1411
			}
1412
			if ($globalDebug) echo 'Count all pilots...'."\n";
1413
			$alldata = $Spotter->countAllPilots(false,0,$last_update_day);
1414
			foreach ($alldata as $number) {
1415
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'','',$number['format_source'],$reset);
1416
			}
1417
			
1418
			if ($globalDebug) echo 'Count all departure airports...'."\n";
1419
			$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day);
1420
			if ($globalDebug) echo 'Count all detected departure airports...'."\n";
1421
        		$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day);
1422
			if ($globalDebug) echo 'Order departure airports...'."\n";
1423
	        	$alldata = array();
1424
	        	
1425
    			foreach ($pall as $value) {
1426
	        		$icao = $value['airport_departure_icao'];
1427
    				$alldata[$icao] = $value;
1428
	        	}
1429
	        	foreach ($dall as $value) {
1430
    				$icao = $value['airport_departure_icao'];
1431
        			if (isset($alldata[$icao])) {
1432
    					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1433
        			} else $alldata[$icao] = $value;
1434
			}
1435
    			$count = array();
1436
    			foreach ($alldata as $key => $row) {
1437
    				$count[$key] = $row['airport_departure_icao_count'];
1438
        		}
1439
			array_multisort($count,SORT_DESC,$alldata);
1440
			foreach ($alldata as $number) {
1441
				echo $this->addStatDepartureAirports($number['airport_departure_icao'],$number['airport_departure_name'],$number['airport_departure_city'],$number['airport_departure_country'],$number['airport_departure_icao_count'],'','',$reset);
1442
			}
1443
			if ($globalDebug) echo 'Count all arrival airports...'."\n";
1444
			$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day);
1445
			if ($globalDebug) echo 'Count all detected arrival airports...'."\n";
1446
        		$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day);
1447
			if ($globalDebug) echo 'Order arrival airports...'."\n";
1448
	        	$alldata = array();
1449
    			foreach ($pall as $value) {
1450
	        		$icao = $value['airport_arrival_icao'];
1451
    				$alldata[$icao] = $value;
1452
	        	}
1453
	        	foreach ($dall as $value) {
1454
    				$icao = $value['airport_arrival_icao'];
1455
        			if (isset($alldata[$icao])) {
1456
        				$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1457
	        		} else $alldata[$icao] = $value;
1458
    			}
1459
        		$count = array();
1460
        		foreach ($alldata as $key => $row) {
1461
        			$count[$key] = $row['airport_arrival_icao_count'];
1462
	        	}
1463
    			array_multisort($count,SORT_DESC,$alldata);
1464
                        foreach ($alldata as $number) {
1465
				echo $this->addStatArrivalAirports($number['airport_arrival_icao'],$number['airport_arrival_name'],$number['airport_arrival_city'],$number['airport_arrival_country'],$number['airport_arrival_icao_count'],'','',$reset);
1466
			}
1467
			if ($Connection->tableExists('countries')) {
1468
				if ($globalDebug) echo 'Count all flights by countries...'."\n";
1469
				$SpotterArchive = new SpotterArchive();
1470
				$alldata = $SpotterArchive->countAllFlightOverCountries(false,0,$last_update_day);
1471
				foreach ($alldata as $number) {
1472
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],'',$reset);
1473
				}
1474
			}
1475
			
1476
1477
			// Add by month using getstat if month finish...
1478
1479
			//if (date('m',strtotime($last_update_day)) != date('m')) {
1480
			if ($globalDebug) echo 'Count all flights by months...'."\n";
1481
			$Spotter = new Spotter($this->db);
1482
			$alldata = $Spotter->countAllMonths();
1483
			$lastyear = false;
0 ignored issues
show
Unused Code introduced by
$lastyear 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...
1484
			foreach ($alldata as $number) {
1485
				if ($number['year_name'] != date('Y')) $lastyear = true;
0 ignored issues
show
Unused Code introduced by
$lastyear 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...
1486
				$this->addStat('flights_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
1487
			}
1488
			if ($globalDebug) echo 'Count all military flights by months...'."\n";
1489
			$alldata = $Spotter->countAllMilitaryMonths();
1490
			foreach ($alldata as $number) {
1491
				$this->addStat('military_flights_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
1492
			}
1493
			if ($globalDebug) echo 'Count all owners by months...'."\n";
1494
			$alldata = $Spotter->countAllMonthsOwners();
1495
			foreach ($alldata as $number) {
1496
				$this->addStat('owners_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
1497
			}
1498
			if ($globalDebug) echo 'Count all pilots by months...'."\n";
1499
			$alldata = $Spotter->countAllMonthsPilots();
1500
			foreach ($alldata as $number) {
1501
				$this->addStat('pilots_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
1502
			}
1503
			if ($globalDebug) echo 'Count all airlines by months...'."\n";
1504
			$alldata = $Spotter->countAllMonthsAirlines();
1505
			foreach ($alldata as $number) {
1506
				$this->addStat('airlines_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
1507
			}
1508
			if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
1509
			$alldata = $Spotter->countAllMonthsAircrafts();
1510
			foreach ($alldata as $number) {
1511
				$this->addStat('aircrafts_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
1512
			}
1513
			if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
1514
			$alldata = $Spotter->countAllMonthsRealArrivals();
1515
			foreach ($alldata as $number) {
1516
				$this->addStat('realarrivals_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
1517
			}
1518
			if ($globalDebug) echo 'Airports data...'."\n";
1519
			if ($globalDebug) echo '...Departure'."\n";
1520
			$this->deleteStatAirport('daily');
1521
//			$pall = $Spotter->getLast7DaysAirportsDeparture();
1522
  //      		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1523
			$pall = $Spotter->getLast7DaysAirportsDeparture();
1524
        		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1525
        		/*
1526
	        	$alldata = array();
1527
    			foreach ($pall as $value) {
1528
	        		$icao = $value['departure_airport_icao'];
1529
    				$alldata[$icao] = $value;
1530
	        	}
1531
	        	foreach ($dall as $value) {
1532
    				$icao = $value['departure_airport_icao'];
1533
    				$ddate = $value['date'];
1534
        			if (isset($alldata[$icao])) {
1535
        				$alldata[$icao]['departure_airport_count'] = $alldata[$icao]['departure_airport_count'] + $value['departure_airport_count'];
1536
	        		} else $alldata[$icao] = $value;
1537
    			}
1538
        		$count = array();
1539
        		foreach ($alldata as $key => $row) {
1540
        			$count[$key] = $row['departure_airport_count'];
1541
	        	}
1542
    			array_multisort($count,SORT_DESC,$alldata);
1543
    			*/
1544
    			foreach ($dall as $value) {
1545
    				$icao = $value['departure_airport_icao'];
1546
    				$ddate = $value['date'];
1547
    				$find = false;
1548
    				foreach ($pall as $pvalue) {
1549
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1550
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1551
    						$find = true;
1552
    						break;
1553
    					}
1554
    				}
1555
    				if ($find === false) {
1556
    					$pall[] = $value;
1557
    				}
1558
    			}
1559
    			$alldata = $pall;
1560
			foreach ($alldata as $number) {
1561
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count']);
1562
			}
1563
			echo '...Arrival'."\n";
1564
			$pall = $Spotter->getLast7DaysAirportsArrival();
1565
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrival();
1566
        		/*
1567
	        	$alldata = array();
1568
    			foreach ($pall as $value) {
1569
	        		$icao = $value['arrival_airport_icao'];
1570
    				$alldata[$icao] = $value;
1571
	        	}
1572
	        	foreach ($dall as $value) {
1573
    				$icao = $value['arrival_airport_icao'];
1574
        			if (isset($alldata[$icao])) {
1575
        				$alldata[$icao]['arrival_airport_icao_count'] = $alldata[$icao]['arrival_airport_count'] + $value['arrival_airport_count'];
1576
	        		} else $alldata[$icao] = $value;
1577
    			}
1578
        		$count = array();
1579
        		foreach ($alldata as $key => $row) {
1580
        			$count[$key] = $row['arrival_airport_count'];
1581
	        	}
1582
    			array_multisort($count,SORT_DESC,$alldata);
1583
    			*/
1584
1585
1586
    			foreach ($dall as $value) {
1587
    				$icao = $value['arrival_airport_icao'];
1588
    				$ddate = $value['date'];
1589
    				$find = false;
1590
    				foreach ($pall as $pvalue) {
1591
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1592
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1593
    						$find = true;
1594
    						break;
1595
    					}
1596
    				}
1597
    				if ($find === false) {
1598
    					$pall[] = $value;
1599
    				}
1600
    			}
1601
    			$alldata = $pall;
1602
			foreach ($alldata as $number) {
1603
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count']);
1604
			}
1605
1606
			echo 'Flights data...'."\n";
1607
			$this->deleteStatFlight('month');
1608
			echo '-> countAllDatesLastMonth...'."\n";
1609
			$alldata = $Spotter->countAllDatesLastMonth();
1610
			foreach ($alldata as $number) {
1611
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
1612
			}
1613
			echo '-> countAllDates...'."\n";
1614
			$previousdata = $this->countAllDates();
1615
			$previousdatabyairlines = $this->countAllDatesByAirlines();
1616
			$this->deleteStatFlight('date');
1617
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates());
1618
			$values = array();
1619
			foreach ($alldata as $cnt) {
1620
				$values[] = $cnt['date_count'];
1621
			}
1622
			array_multisort($values,SORT_DESC,$alldata);
1623
			array_splice($alldata,11);
1624
			foreach ($alldata as $number) {
1625
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
1626
			}
1627
			
1628
			$this->deleteStatFlight('hour');
1629
			echo '-> countAllHours...'."\n";
1630
			$alldata = $Spotter->countAllHours('hour');
1631
			foreach ($alldata as $number) {
1632
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
1633
			}
1634
1635
1636
1637
			// Count by airlines
1638
			echo '--- Stats by airlines ---'."\n";
1639
			if ($globalDebug) echo 'Count all aircraft types by airlines...'."\n";
1640
			$Spotter = new Spotter($this->db);
1641
			$alldata = $Spotter->countAllAircraftTypesByAirlines(false,0,$last_update_day);
1642
			foreach ($alldata as $number) {
1643
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],$number['airline_icao'],'',$reset);
1644
			}
1645
			if ($globalDebug) echo 'Count all aircraft registrations by airlines...'."\n";
1646
			$alldata = $Spotter->countAllAircraftRegistrationsByAirlines(false,0,$last_update_day);
1647
			foreach ($alldata as $number) {
1648
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],$number['airline_icao'],'',$reset);
1649
			}
1650
			if ($globalDebug) echo 'Count all callsigns by airlines...'."\n";
1651
			$alldata = $Spotter->countAllCallsignsByAirlines(false,0,$last_update_day);
1652
			foreach ($alldata as $number) {
1653
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
1654
			}
1655
			if ($globalDebug) echo 'Count all owners by airlines...'."\n";
1656
			$alldata = $Spotter->countAllOwnersByAirlines(false,0,$last_update_day);
1657
			foreach ($alldata as $number) {
1658
				$this->addStatOwner($number['owner_name'],$number['owner_count'],$number['airline_icao'],'',$reset);
1659
			}
1660
			if ($globalDebug) echo 'Count all pilots by airlines...'."\n";
1661
			$alldata = $Spotter->countAllPilotsByAirlines(false,0,$last_update_day);
1662
			foreach ($alldata as $number) {
1663
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],$number['airline_icao'],'',$number['format_source'],$reset);
1664
			}
1665
			if ($globalDebug) echo 'Count all departure airports by airlines...'."\n";
1666
			$pall = $Spotter->countAllDepartureAirportsByAirlines(false,0,$last_update_day);
1667
			if ($globalDebug) echo 'Count all detected departure airports by airlines...'."\n";
1668
       			$dall = $Spotter->countAllDetectedDepartureAirportsByAirlines(false,0,$last_update_day);
1669
			if ($globalDebug) echo 'Order detected departure airports by airlines...'."\n";
1670
	        	//$alldata = array();
1671
    			foreach ($dall as $value) {
1672
    				$icao = $value['airport_departure_icao'];
1673
    				$dicao = $value['airline_icao'];
1674
    				$find = false;
1675
    				foreach ($pall as $pvalue) {
1676
    					if ($pvalue['airport_departure_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1677
    						$pvalue['airport_departure_icao_count'] = $pvalue['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1678
    						$find = true;
1679
    						break;
1680
    					}
1681
    				}
1682
    				if ($find === false) {
1683
    					$pall[] = $value;
1684
    				}
1685
    			}
1686
    			$alldata = $pall;
1687
			foreach ($alldata as $number) {
1688
				echo $this->addStatDepartureAirports($number['airport_departure_icao'],$number['airport_departure_name'],$number['airport_departure_city'],$number['airport_departure_country'],$number['airport_departure_icao_count'],$number['airline_icao'],'',$reset);
1689
			}
1690
			if ($globalDebug) echo 'Count all arrival airports by airlines...'."\n";
1691
			$pall = $Spotter->countAllArrivalAirportsByAirlines(false,0,$last_update_day);
1692
			if ($globalDebug) echo 'Count all detected arrival airports by airlines...'."\n";
1693
        		$dall = $Spotter->countAllDetectedArrivalAirportsByAirlines(false,0,$last_update_day);
1694
			if ($globalDebug) echo 'Order arrival airports by airlines...'."\n";
1695
	        	//$alldata = array();
1696
    			foreach ($dall as $value) {
1697
    				$icao = $value['airport_arrival_icao'];
1698
    				$dicao = $value['airline_icao'];
1699
    				$find = false;
1700
    				foreach ($pall as $pvalue) {
1701
    					if ($pvalue['airport_arrival_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1702
    						$pvalue['airport_arrival_icao_count'] = $pvalue['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1703
    						$find = true;
1704
    						break;
1705
    					}
1706
    				}
1707
    				if ($find === false) {
1708
    					$pall[] = $value;
1709
    				}
1710
    			}
1711
    			$alldata = $pall;
1712
                        foreach ($alldata as $number) {
1713
				if ($number['airline_icao'] != '') echo $this->addStatArrivalAirports($number['airport_arrival_icao'],$number['airport_arrival_name'],$number['airport_arrival_city'],$number['airport_arrival_country'],$number['airport_arrival_icao_count'],$number['airline_icao'],'',$reset);
1714
			}
1715
			if ($globalDebug) echo 'Count all flights by months by airlines...'."\n";
1716
			$Spotter = new Spotter($this->db);
1717
			$alldata = $Spotter->countAllMonthsByAirlines();
1718
			$lastyear = false;
1719
			foreach ($alldata as $number) {
1720
				if ($number['year_name'] != date('Y')) $lastyear = true;
1721
				$this->addStat('flights_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),$number['airline_icao']);
1722
			}
1723
			if ($globalDebug) echo 'Count all owners by months by airlines...'."\n";
1724
			$alldata = $Spotter->countAllMonthsOwnersByAirlines();
1725
			foreach ($alldata as $number) {
1726
				$this->addStat('owners_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),$number['airline_icao']);
1727
			}
1728
			if ($globalDebug) echo 'Count all pilots by months by airlines...'."\n";
1729
			$alldata = $Spotter->countAllMonthsPilotsByAirlines();
1730
			foreach ($alldata as $number) {
1731
				$this->addStat('pilots_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),$number['airline_icao']);
1732
			}
1733
			if ($globalDebug) echo 'Count all aircrafts by months by airlines...'."\n";
1734
			$alldata = $Spotter->countAllMonthsAircraftsByAirlines();
1735
			foreach ($alldata as $number) {
1736
				$this->addStat('aircrafts_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),$number['airline_icao']);
1737
			}
1738
			if ($globalDebug) echo 'Count all real arrivals by months by airlines...'."\n";
1739
			$alldata = $Spotter->countAllMonthsRealArrivalsByAirlines();
1740
			foreach ($alldata as $number) {
1741
				$this->addStat('realarrivals_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),$number['airline_icao']);
1742
			}
1743
			if ($globalDebug) echo '...Departure'."\n";
1744
			$pall = $Spotter->getLast7DaysAirportsDepartureByAirlines();
1745
        		$dall = $Spotter->getLast7DaysDetectedAirportsDepartureByAirlines();
1746
    			foreach ($dall as $value) {
1747
    				$icao = $value['departure_airport_icao'];
1748
    				$airline = $value['airline_icao'];
1749
    				$ddate = $value['date'];
1750
    				$find = false;
1751
    				foreach ($pall as $pvalue) {
1752
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] = $airline) {
1753
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1754
    						$find = true;
1755
    						break;
1756
    					}
1757
    				}
1758
    				if ($find === false) {
1759
    					$pall[] = $value;
1760
    				}
1761
    			}
1762
    			$alldata = $pall;
1763
			foreach ($alldata as $number) {
1764
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count'],$number['airline_icao']);
1765
			}
1766
			if ($globalDebug) echo '...Arrival'."\n";
1767
			$pall = $Spotter->getLast7DaysAirportsArrivalByAirlines();
1768
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrivalByAirlines();
1769
    			foreach ($dall as $value) {
1770
    				$icao = $value['arrival_airport_icao'];
1771
    				$airline = $value['airline_icao'];
1772
    				$ddate = $value['date'];
1773
    				$find = false;
1774
    				foreach ($pall as $pvalue) {
1775
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] == $airline) {
1776
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1777
    						$find = true;
1778
    						break;
1779
    					}
1780
    				}
1781
    				if ($find === false) {
1782
    					$pall[] = $value;
1783
    				}
1784
    			}
1785
    			$alldata = $pall;
1786
			foreach ($alldata as $number) {
1787
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count'],$number['airline_icao']);
1788
			}
1789
1790
			if ($globalDebug) echo 'Flights data...'."\n";
1791
			if ($globalDebug) echo '-> countAllDatesLastMonth...'."\n";
1792
			$alldata = $Spotter->countAllDatesLastMonthByAirlines();
1793
			foreach ($alldata as $number) {
1794
				$this->addStatFlight('month',$number['date_name'],$number['date_count'], $number['airline_icao']);
1795
			}
1796
			if ($globalDebug) echo '-> countAllDates...'."\n";
1797
			//$previousdata = $this->countAllDatesByAirlines();
1798
			$alldata = $Common->array_merge_noappend($previousdatabyairlines,$Spotter->countAllDatesByAirlines());
1799
			$values = array();
1800
			foreach ($alldata as $cnt) {
1801
				$values[] = $cnt['date_count'];
1802
			}
1803
			array_multisort($values,SORT_DESC,$alldata);
1804
			array_splice($alldata,11);
1805
			foreach ($alldata as $number) {
1806
				$this->addStatFlight('date',$number['date_name'],$number['date_count'],$number['airline_icao']);
1807
			}
1808
			
1809
			if ($globalDebug) echo '-> countAllHours...'."\n";
1810
			$alldata = $Spotter->countAllHoursByAirlines('hour');
1811
			foreach ($alldata as $number) {
1812
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],$number['airline_icao']);
1813
			}
1814
			
1815
1816
			// Stats by filters
1817
			if (!isset($globalStatsFilters) || $globalStatsFilters == '') $globalStatsFilters = array();
1818
			foreach ($globalStatsFilters as $name => $filter) {
1819
				//$filter_name = $filter['name'];
1820
				$filter_name = $name;
1821
1822
				$last_update = $this->getLastStatsUpdate('last_update_stats_'.$filter_name);
1823
				if (isset($last_update[0]['value'])) {
1824
					$last_update_day = $last_update[0]['value'];
1825
				} else $last_update_day = '2012-12-12 12:12:12';
1826
				$reset = false;
1827
1828
				// Count by filter
1829
				if ($globalDebug) echo '--- Stats for filter '.$filter_name.' ---'."\n";
1830
				$Spotter = new Spotter($this->db);
1831
				$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day,$filter);
1832
				foreach ($alldata as $number) {
1833
					$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'',$filter_name,$reset);
1834
				}
1835
				$alldata = $Spotter->countAllAirlines(false,0,$last_update_day,$filter);
1836
				foreach ($alldata as $number) {
1837
					$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],$filter_name,$reset);
1838
				}
1839
				$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day,$filter);
1840
				foreach ($alldata as $number) {
1841
					$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'',$filter_name,$reset);
1842
				}
1843
				$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day,$filter);
1844
				foreach ($alldata as $number) {
1845
					$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],'',$filter_name,$reset);
1846
				}
1847
				$alldata = $Spotter->countAllOwners(false,0,$last_update_day,$filter);
1848
				foreach ($alldata as $number) {
1849
					$this->addStatOwner($number['owner_name'],$number['owner_count'],'',$filter_name,$reset);
1850
				}
1851
				$alldata = $Spotter->countAllPilots(false,0,$last_update_day,$filter);
1852
				foreach ($alldata as $number) {
1853
					$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'',$filter_name,$number['format_source'],$reset);
1854
				}
1855
				$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day,$filter);
1856
	       			$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day,$filter);
1857
		        	$alldata = array();
1858
	    			foreach ($pall as $value) {
1859
		        		$icao = $value['airport_departure_icao'];
1860
    					$alldata[$icao] = $value;
1861
	    			}
1862
		        	foreach ($dall as $value) {
1863
	    				$icao = $value['airport_departure_icao'];
1864
        				if (isset($alldata[$icao])) {
1865
    						$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1866
        				} else $alldata[$icao] = $value;
1867
				}
1868
	    			$count = array();
1869
    				foreach ($alldata as $key => $row) {
1870
    					$count[$key] = $row['airport_departure_icao_count'];
1871
    				}
1872
				array_multisort($count,SORT_DESC,$alldata);
1873
				foreach ($alldata as $number) {
1874
    					echo $this->addStatDepartureAirports($number['airport_departure_icao'],$number['airport_departure_name'],$number['airport_departure_city'],$number['airport_departure_country'],$number['airport_departure_icao_count'],'',$filter_name,$reset);
1875
				}
1876
				$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day,false,$filter);
1877
    				$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day,false,$filter);
1878
				$alldata = array();
1879
    				foreach ($pall as $value) {
1880
		        		$icao = $value['airport_arrival_icao'];
1881
    					$alldata[$icao] = $value;
1882
	    			}
1883
		        	foreach ($dall as $value) {
1884
	    				$icao = $value['airport_arrival_icao'];
1885
        				if (isset($alldata[$icao])) {
1886
        					$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1887
		        		} else $alldata[$icao] = $value;
1888
	    			}
1889
        			$count = array();
1890
        			foreach ($alldata as $key => $row) {
1891
    					$count[$key] = $row['airport_arrival_icao_count'];
1892
		        	}
1893
        			array_multisort($count,SORT_DESC,$alldata);
1894
				foreach ($alldata as $number) {
1895
					echo $this->addStatArrivalAirports($number['airport_arrival_icao'],$number['airport_arrival_name'],$number['airport_arrival_city'],$number['airport_arrival_country'],$number['airport_arrival_icao_count'],'',$filter_name,$reset);
1896
				}
1897
				$Spotter = new Spotter($this->db);
1898
				$alldata = $Spotter->countAllMonths($filter);
1899
				$lastyear = false;
1900
				foreach ($alldata as $number) {
1901
					if ($number['year_name'] != date('Y')) $lastyear = true;
1902
					$this->addStat('flights_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),'',$filter_name);
1903
				}
1904
				$alldata = $Spotter->countAllMonthsOwners($filter);
1905
				foreach ($alldata as $number) {
1906
					$this->addStat('owners_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),'',$filter_name);
1907
				}
1908
				$alldata = $Spotter->countAllMonthsPilots($filter);
1909
				foreach ($alldata as $number) {
1910
					$this->addStat('pilots_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),'',$filter_name);
1911
				}
1912
				$alldata = $Spotter->countAllMilitaryMonths($filter);
1913
				foreach ($alldata as $number) {
1914
					$this->addStat('military_flights_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),'',$filter_name);
1915
				}
1916
				$alldata = $Spotter->countAllMonthsAircrafts($filter);
1917
				foreach ($alldata as $number) {
1918
					$this->addStat('aircrafts_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),'',$filter_name);
1919
				}
1920
				$alldata = $Spotter->countAllMonthsRealArrivals($filter);
1921
				foreach ($alldata as $number) {
1922
					$this->addStat('realarrivals_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])),'',$filter_name);
1923
				}
1924
				echo '...Departure'."\n";
1925
				$pall = $Spotter->getLast7DaysAirportsDeparture('',$filter);
1926
        			$dall = $Spotter->getLast7DaysDetectedAirportsDeparture('',$filter);
1927
				foreach ($dall as $value) {
1928
    					$icao = $value['departure_airport_icao'];
1929
    					$ddate = $value['date'];
1930
    					$find = false;
1931
    					foreach ($pall as $pvalue) {
1932
    						if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1933
    							$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1934
	    						$find = true;
1935
    							break;
1936
    						}
1937
    					}
1938
    					if ($find === false) {
1939
    						$pall[] = $value;
1940
	    				}
1941
    				}
1942
	    			$alldata = $pall;
1943
				foreach ($alldata as $number) {
1944
					$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count'],'',$filter_name);
1945
				}
1946
				echo '...Arrival'."\n";
1947
				$pall = $Spotter->getLast7DaysAirportsArrival('',$filter);
1948
    				$dall = $Spotter->getLast7DaysDetectedAirportsArrival('',$filter);
1949
				foreach ($dall as $value) {
1950
					$icao = $value['arrival_airport_icao'];
1951
					$ddate = $value['date'];
1952
    					$find = false;
1953
					foreach ($pall as $pvalue) {
1954
    						if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1955
    							$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1956
    							$find = true;
1957
    							break;
1958
	    					}
1959
    					}
1960
    					if ($find === false) {
1961
    						$pall[] = $value;
1962
	    				}
1963
    				}
1964
    				$alldata = $pall;
1965
				foreach ($alldata as $number) {
1966
					$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count'],'',$filter_name);
1967
				}
1968
    
1969
				echo 'Flights data...'."\n";
1970
				echo '-> countAllDatesLastMonth...'."\n";
1971
				$alldata = $Spotter->countAllDatesLastMonth($filter);
1972
				foreach ($alldata as $number) {
1973
					$this->addStatFlight('month',$number['date_name'],$number['date_count'], '',$filter_name);
1974
				}
1975
				echo '-> countAllDates...'."\n";
1976
				$previousdata = $this->countAllDates('',$filter_name);
1977
				$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter));
1978
				$values = array();
1979
				foreach ($alldata as $cnt) {
1980
					$values[] = $cnt['date_count'];
1981
				}
1982
				array_multisort($values,SORT_DESC,$alldata);
1983
				array_splice($alldata,11);
1984
				foreach ($alldata as $number) {
1985
					$this->addStatFlight('date',$number['date_name'],$number['date_count'],'',$filter_name);
1986
				}
1987
				
1988
				echo '-> countAllHours...'."\n";
1989
				$alldata = $Spotter->countAllHours('hour',$filter);
1990
				foreach ($alldata as $number) {
1991
					$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],'',$filter_name);
1992
				}
1993
				echo 'Insert last stats update date...'."\n";
1994
				date_default_timezone_set('UTC');
1995
				$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y-m-d G:i:s'));
1996
				if (isset($filter['DeleteLastYearStats']) && $filter['DeleteLastYearStats'] == true) {
1997
					if (date('Y',strtotime($last_update_day)) != date('Y')) {
1998
						$this->deleteOldStats($filter_name);
1999
						$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y').'-01-01 00:00:00');
2000
					}
2001
				}
2002
2003
			}
2004
	
2005
2006
			// Last year stats
2007
			if ($lastyear) {
2008
				echo 'Data from last year...'."\n";
2009
				// SUM all previous month to put as year
2010
				$previous_year = date('Y');
2011
				$previous_year--;
2012
				$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2013
				$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2014
				$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2015
				$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2016
				$allairlines = $this->getAllAirlineNames();
2017
				foreach ($allairlines as $data) {
2018
					$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2019
					$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2020
					$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2021
					$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2022
				}
2023
				
2024
				if (isset($globalArchiveYear) && $globalArchiveYear) {
2025
					if ($globalArchive) {
2026
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
2027
						try {
2028
							$sth = $this->db->prepare($query);
2029
							$sth->execute();
2030
						} catch(PDOException $e) {
2031
							return "error : ".$e->getMessage().' - query : '.$query."\n";
2032
						}
2033
					}
2034
					echo 'Delete old data'."\n";
2035
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
2036
					try {
2037
						$sth = $this->db->prepare($query);
2038
						$sth->execute();
2039
					} catch(PDOException $e) {
2040
						return "error : ".$e->getMessage().' - query : '.$query."\n";
2041
					}
2042
				}
2043
				if (isset($globalDeleteLastYearStats) && $globalDeleteLastYearStats) {
2044
					$last_update = $this->getLastStatsUpdate('last_update_stats');
2045
					if (date('Y',strtotime($last_update[0]['value'])) != date('Y')) {
2046
						$this->deleteOldStats();
2047
						$this->addLastStatsUpdate('last_update_stats',date('Y').'-01-01 00:00:00');
2048
						$lastyearupdate = true;
2049
					}
2050
				}
2051
			}
2052
			if ($globalArchiveMonths > 0) {
2053
				if ($globalArchive) {
2054
					echo 'Archive old data...'."\n";
2055
					if ($globalDBdriver == 'mysql') {
2056
						//$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2057
						$query = "INSERT INTO spotter_archive_output (spotter_id,flightaware_id,ident,registration,airline_name,airline_icao,airline_country,airline_type,aircraft_icao,aircraft_name,aircraft_manufacturer,departure_airport_icao,departure_airport_name,departure_airport_city,departure_airport_country,departure_airport_time,arrival_airport_icao,arrival_airport_name,arrival_airport_city,arrival_airport_country,arrival_airport_time,route_stop,date,latitude,longitude,waypoints,altitude,heading,ground_speed,highlight,squawk,ModeS,pilot_id,pilot_name,owner_name,verticalrate,format_source,source_name,ground,last_ground,last_seen,last_latitude,last_longitude,last_altitude,last_ground_speed,real_arrival_airport_icao,real_arrival_airport_time,real_departure_airport_icao,real_departure_airport_time)
2058
							    SELECT spotter_id,flightaware_id,ident,registration,airline_name,airline_icao,airline_country,airline_type,aircraft_icao,aircraft_name,aircraft_manufacturer,departure_airport_icao,departure_airport_name,departure_airport_city,departure_airport_country,departure_airport_time,arrival_airport_icao,arrival_airport_name,arrival_airport_city,arrival_airport_country,arrival_airport_time,route_stop,date,latitude,longitude,waypoints,altitude,heading,ground_speed,highlight,squawk,ModeS,pilot_id,pilot_name,owner_name,verticalrate,format_source,source_name,ground,last_ground,last_seen,last_latitude,last_longitude,last_altitude,last_ground_speed,real_arrival_airport_icao,real_arrival_airport_time,real_departure_airport_icao,real_departure_airport_time
2059
	    						     FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2060
					} else {
2061
						$query = "INSERT INTO spotter_archive_output (spotter_id,flightaware_id,ident,registration,airline_name,airline_icao,airline_country,airline_type,aircraft_icao,aircraft_name,aircraft_manufacturer,departure_airport_icao,departure_airport_name,departure_airport_city,departure_airport_country,departure_airport_time,arrival_airport_icao,arrival_airport_name,arrival_airport_city,arrival_airport_country,arrival_airport_time,route_stop,date,latitude,longitude,waypoints,altitude,heading,ground_speed,highlight,squawk,ModeS,pilot_id,pilot_name,owner_name,verticalrate,format_source,source_name,ground,last_ground,last_seen,last_latitude,last_longitude,last_altitude,last_ground_speed,real_arrival_airport_icao,real_arrival_airport_time,real_departure_airport_icao,real_departure_airport_time)
2062
							     SELECT 
2063
								spotter_id,flightaware_id,ident,registration,airline_name,airline_icao,airline_country,airline_type,aircraft_icao,aircraft_name,aircraft_manufacturer,departure_airport_icao,departure_airport_name,departure_airport_city,departure_airport_country,departure_airport_time,arrival_airport_icao,arrival_airport_name,arrival_airport_city,arrival_airport_country,arrival_airport_time,route_stop,date,latitude,longitude,waypoints,altitude,heading,ground_speed,highlight,squawk,ModeS,pilot_id,pilot_name,owner_name,verticalrate,format_source,source_name,ground,last_ground,last_seen,last_latitude,last_longitude,last_altitude,last_ground_speed,real_arrival_airport_icao,real_arrival_airport_time,real_departure_airport_icao,real_departure_airport_time
2064
							    FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
2065
					}
2066
					try {
2067
						$sth = $this->db->prepare($query);
2068
						$sth->execute();
2069
					} catch(PDOException $e) {
2070
						return "error : ".$e->getMessage();
2071
					}
2072
				}
2073
				echo 'Deleting old data...'."\n";
2074
				//$query = 'DELETE FROM spotter_output WHERE spotter_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveMonths.' MONTH)';
2075
				if ($globalDBdriver == 'mysql') {
2076
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2077
				} else {
2078
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
2079
				}
2080
				try {
2081
					$sth = $this->db->prepare($query);
2082
					$sth->execute();
2083
				} catch(PDOException $e) {
2084
					return "error : ".$e->getMessage();
2085
				}
2086
			}
2087
			if (!isset($lastyearupdate)) {
2088
				echo 'Insert last stats update date...'."\n";
2089
				date_default_timezone_set('UTC');
2090
				$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
2091
			}
2092
			if ($globalStatsResetYear) {
2093
				require_once(dirname(__FILE__).'/../install/class.settings.php');
2094
				settings::modify_settings(array('globalStatsResetYear' => 'FALSE'));
2095
			}
2096
2097
		//}
2098
	}
2099
}
2100
2101
?>