Completed
Push — master ( b282d7...87953e )
by Yannick
08:34
created

Stats::addStatArrivalAirports()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 13
nop 8
dl 0
loc 25
rs 8.439
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
                
642
                if (empty($all)) {
643
			$filters = array('airlines' => array($stats_airline));
644
			if ($filter_name != '') {
645
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
646
			}
647
            		$Spotter = new Spotter($this->db);
648
            		$all = $Spotter->countAllMonths($filters);
649
                }
650
                
651
                return $all;
652
	}
653
	public function countAllMilitaryMonths($filter_name = '') {
654
		global $globalStatsFilters;
655
		if ($filter_name == '') $filter_name = $this->filter_name;
656
	    	$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";
657
                 try {
658
                        $sth = $this->db->prepare($query);
659
                        $sth->execute(array(':filter_name' => $filter_name));
660
                } catch(PDOException $e) {
661
                        echo "error : ".$e->getMessage();
662
                }
663
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
664
                if (empty($all)) {
665
            		$filters = array();
666
            		if ($filter_name != '') {
667
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
668
			}
669
            		$Spotter = new Spotter($this->db);
670
            		$all = $Spotter->countAllMilitaryMonths($filters);
671
                }
672
                return $all;
673
	}
674
	public function countAllHours($orderby = 'hour',$limit = true,$stats_airline = '',$filter_name = '') {
675
		global $globalTimezone, $globalDBdriver, $globalStatsFilters;
676
		if ($filter_name == '') $filter_name = $this->filter_name;
677
		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";
678
		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";
679
		if ($orderby == 'hour') {
680
			/*
681
			if ($globalDBdriver == 'mysql') {
682
				$query .= " ORDER BY flight_date ASC";
683
			} else {
684
			*/
685
			$query .= " ORDER BY CAST(flight_date AS integer) ASC";
686
		}
687
		if ($orderby == 'count') $query .= " ORDER BY hour_count DESC";
688
                 try {
689
                        $sth = $this->db->prepare($query);
690
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
691
                } catch(PDOException $e) {
692
                        echo "error : ".$e->getMessage();
693
                }
694
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
695
                if (empty($all)) {
696
			$filters = array('airlines' => array($stats_airline));
697
			if ($filter_name != '') {
698
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
699
			}
700
            		$Spotter = new Spotter($this->db);
701
            		$all = $Spotter->countAllHours($orderby,$filters);
702
                }
703
                return $all;
704
	}
705
	
706
	public function countOverallFlights($stats_airline = '', $filter_name = '',$year = '',$month = '') {
707
		global $globalStatsFilters;
708
		if ($filter_name == '') $filter_name = $this->filter_name;
709
		if ($year == '') $year = date('Y');
710
		$all = $this->getSumStats('flights_bymonth',$year,$stats_airline,$filter_name,$month);
711
		if (empty($all)) {
712
			$filters = array('airlines' => array($stats_airline));
713
			if ($filter_name != '') {
714
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
715
			}
716
			$Spotter = new Spotter($this->db);
717
			$all = $Spotter->countOverallFlights($filters,$year,$month);
718
		}
719
		return $all;
720
	}
721
	public function countOverallMilitaryFlights($filter_name = '',$year = '', $month = '') {
722
		global $globalStatsFilters;
723
		if ($filter_name == '') $filter_name = $this->filter_name;
724
		if ($year == '') $year = date('Y');
725
		$all = $this->getSumStats('military_flights_bymonth',$year,'',$filter_name,$month);
726
		if (empty($all)) {
727
		        $filters = array();
728
            		if ($filter_name != '') {
729
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
730
			}
731
			$Spotter = new Spotter($this->db);
732
			$all = $Spotter->countOverallMilitaryFlights($filters,$year,$month);
733
		}
734
		return $all;
735
	}
736
	public function countOverallArrival($stats_airline = '',$filter_name = '', $year = '', $month = '') {
737
		global $globalStatsFilters;
738
		if ($filter_name == '') $filter_name = $this->filter_name;
739
		if ($year == '') $year = date('Y');
740
		$all = $this->getSumStats('realarrivals_bymonth',$year,$stats_airline,$filter_name,$month);
741
		if (empty($all)) {
742
			$filters = array('airlines' => array($stats_airline));
743
			if ($filter_name != '') {
744
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
745
			}
746
			$Spotter = new Spotter($this->db);
747
			$all = $Spotter->countOverallArrival($filters,$year,$month);
748
		}
749
		return $all;
750
	}
751
	public function countOverallAircrafts($stats_airline = '',$filter_name = '',$year = '', $month = '') {
752
		global $globalStatsFilters;
753
		if ($filter_name == '') $filter_name = $this->filter_name;
754
		if ($year == '' && $month == '') {
755
			$query = "SELECT COUNT(*) AS nb FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
756
			try {
757
				$sth = $this->db->prepare($query);
758
				$sth->execute(array(':filter_name' => $filter_name,':stats_airline' => $stats_airline));
759
			} catch(PDOException $e) {
760
				echo "error : ".$e->getMessage();
761
			}
762
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
763
			$all = $result[0]['nb'];
764
		} else $all = $this->getSumStats('aircrafts_bymonth',$year,$stats_airline,$filter_name,$month);
765
		if (empty($all)) {
766
			$filters = array('airlines' => array($stats_airline));
767
			if ($filter_name != '') {
768
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
769
			}
770
			$Spotter = new Spotter($this->db);
771
			$all = $Spotter->countOverallAircrafts($filters,$year,$month);
772
		}
773
		return $all;
774
	}
775
	public function countOverallAirlines($filter_name = '',$year = '',$month = '') {
776
		global $globalStatsFilters;
777
		if ($filter_name == '') $filter_name = $this->filter_name;
778
		if ($year == '' && $month == '') {
779
			$query = "SELECT COUNT(*) AS nb_airline FROM stats_airline WHERE filter_name = :filter_name";
780
			try {
781
				$sth = $this->db->prepare($query);
782
				$sth->execute(array(':filter_name' => $filter_name));
783
			} catch(PDOException $e) {
784
				echo "error : ".$e->getMessage();
785
			}
786
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
787
			$all = $result[0]['nb_airline'];
788
		} else $all = $this->getSumStats('airlines_bymonth',$year,'',$filter_name,$month);
789
		if (empty($all)) {
790
            		$filters = array();
791
            		if ($filter_name != '') {
792
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
793
			}
794
			$Spotter = new Spotter($this->db);
795
			$all = $Spotter->countOverallAirlines($filters,$year,$month);
796
		}
797
		return $all;
798
	}
799
	public function countOverallOwners($stats_airline = '',$filter_name = '',$year = '', $month = '') {
800
		global $globalStatsFilters;
801
		if ($filter_name == '') $filter_name = $this->filter_name;
802
		if ($year == '' && $month == '') {
803
			$query = "SELECT count(*) as nb FROM stats_owner WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
804
			$query_values = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
805
			try {
806
				$sth = $this->db->prepare($query);
807
				$sth->execute($query_values);
808
			} catch(PDOException $e) {
809
				echo "error : ".$e->getMessage();
810
			}
811
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
812
			$all = $result[0]['nb'];
813
		} else {
814
			$all = $this->getSumStats('owners_bymonth',$year,$stats_airline,$filter_name,$month);
815
		}
816
		if (empty($all)) {
817
			$filters = array('airlines' => array($stats_airline));
818
			if ($filter_name != '') {
819
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
820
			}
821
			$Spotter = new Spotter($this->db);
822
			$all = $Spotter->countOverallOwners($filters,$year,$month);
823
		}
824
		return $all;
825
	}
826
	public function countOverallPilots($stats_airline = '',$filter_name = '',$year = '',$month = '') {
827
		global $globalStatsFilters;
828
		if ($filter_name == '') $filter_name = $this->filter_name;
829
		//if ($year == '') $year = date('Y');
830
		if ($year == '' && $month == '') {
831
			$query = "SELECT count(*) as nb FROM stats_pilot WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
832
			$query_values = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
833
			try {
834
				$sth = $this->db->prepare($query);
835
				$sth->execute($query_values);
836
			} catch(PDOException $e) {
837
				echo "error : ".$e->getMessage();
838
			}
839
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
840
			$all = $result[0]['nb'];
841
		} else {
842
			$all = $this->getSumStats('pilots_bymonth',$year,$stats_airline,$filter_name,$month);
843
		}
844
		if (empty($all)) {
845
			$filters = array('airlines' => array($stats_airline));
846
			if ($filter_name != '') {
847
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
848
			}
849
			$Spotter = new Spotter($this->db);
850
			$all = $Spotter->countOverallPilots($filters,$year,$month);
851
		}
852
		return $all;
853
	}
854
855
	public function getLast7DaysAirports($airport_icao = '', $stats_airline = '',$filter_name = '') {
856
		if ($filter_name == '') $filter_name = $this->filter_name;
857
		$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";
858
		$query_values = array(':airport_icao' => $airport_icao,':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
859
                 try {
860
                        $sth = $this->db->prepare($query);
861
                        $sth->execute($query_values);
862
                } catch(PDOException $e) {
863
                        echo "error : ".$e->getMessage();
864
                }
865
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
866
                return $all;
867
	}
868
	public function getStats($type,$stats_airline = '', $filter_name = '') {
869
		if ($filter_name == '') $filter_name = $this->filter_name;
870
                $query = "SELECT * FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY stats_date";
871
                $query_values = array(':type' => $type,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
872
                 try {
873
                        $sth = $this->db->prepare($query);
874
                        $sth->execute($query_values);
875
                } catch(PDOException $e) {
876
                        echo "error : ".$e->getMessage();
877
                }
878
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
879
                return $all;
880
        }
881
	public function getSumStats($type,$year,$stats_airline = '',$filter_name = '',$month = '') {
882
		if ($filter_name == '') $filter_name = $this->filter_name;
883
    		global $globalArchiveMonths, $globalDBdriver;
884
    		if ($globalDBdriver == 'mysql') {
885
    			if ($month == '') {
886
				$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";
887
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
888
			} else {
889
				$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";
890
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
891
			}
892
		} else {
893
			if ($month == '') {
894
				$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";
895
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
896
			} else {
897
				$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";
898
				$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
899
			}
900
                }
901
                 try {
902
                        $sth = $this->db->prepare($query);
903
                        $sth->execute($query_values);
904
                } catch(PDOException $e) {
905
                        echo "error : ".$e->getMessage();
906
                }
907
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
908
                return $all[0]['total'];
909
        }
910
	public function getStatsTotal($type, $stats_airline = '', $filter_name = '') {
911
    		global $globalArchiveMonths, $globalDBdriver;
912
		if ($filter_name == '') $filter_name = $this->filter_name;
913
    		if ($globalDBdriver == 'mysql') {
914
			$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";
915
		} else {
916
			$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";
917
                }
918
                $query_values = array(':type' => $type, ':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
919
                 try {
920
                        $sth = $this->db->prepare($query);
921
                        $sth->execute($query_values);
922
                } catch(PDOException $e) {
923
                        echo "error : ".$e->getMessage();
924
                }
925
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
926
                return $all[0]['total'];
927
        }
928
	public function getStatsAircraftTotal($stats_airline = '', $filter_name = '') {
929
    		global $globalArchiveMonths, $globalDBdriver;
930
		if ($filter_name == '') $filter_name = $this->filter_name;
931
    		if ($globalDBdriver == 'mysql') {
932
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
933
                } else {
934
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
935
                }
936
                 try {
937
                        $sth = $this->db->prepare($query);
938
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
939
                } catch(PDOException $e) {
940
                        echo "error : ".$e->getMessage();
941
                }
942
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
943
                return $all[0]['total'];
944
        }
945
	public function getStatsAirlineTotal($filter_name = '') {
946
    		global $globalArchiveMonths, $globalDBdriver;
947
		if ($filter_name == '') $filter_name = $this->filter_name;
948
    		if ($globalDBdriver == 'mysql') {
949
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
950
                } else {
951
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
952
                }
953
                 try {
954
                        $sth = $this->db->prepare($query);
955
                        $sth->execute(array(':filter_name' => $filter_name));
956
                } catch(PDOException $e) {
957
                        echo "error : ".$e->getMessage();
958
                }
959
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
960
                return $all[0]['total'];
961
        }
962
	public function getStatsOwnerTotal($filter_name = '') {
963
    		global $globalArchiveMonths, $globalDBdriver;
964
		if ($filter_name == '') $filter_name = $this->filter_name;
965
    		if ($globalDBdriver == 'mysql') {
966
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
967
		} else {
968
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
969
                }
970
                 try {
971
                        $sth = $this->db->prepare($query);
972
                        $sth->execute(array(':filter_name' => $filter_name));
973
                } catch(PDOException $e) {
974
                        echo "error : ".$e->getMessage();
975
                }
976
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
977
                return $all[0]['total'];
978
        }
979
	public function getStatsOwner($owner_name,$filter_name = '') {
980
    		global $globalArchiveMonths, $globalDBdriver;
981
		if ($filter_name == '') $filter_name = $this->filter_name;
982
		$query = "SELECT cnt FROM stats_owner WHERE filter_name = :filter_name AND owner_name = :owner_name";
983
                 try {
984
                        $sth = $this->db->prepare($query);
985
                        $sth->execute(array(':filter_name' => $filter_name,':owner_name' => $owner_name));
986
                } catch(PDOException $e) {
987
                        echo "error : ".$e->getMessage();
988
                }
989
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
990
                if (isset($all[0]['cnt'])) return $all[0]['cnt'];
991
                else return 0;
992
        }
993
	public function getStatsPilotTotal($filter_name = '') {
994
    		global $globalArchiveMonths, $globalDBdriver;
995
		if ($filter_name == '') $filter_name = $this->filter_name;
996
    		if ($globalDBdriver == 'mysql') {
997
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
998
            	} else {
999
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
1000
            	}
1001
                 try {
1002
                        $sth = $this->db->prepare($query);
1003
                        $sth->execute(array(':filter_name' => $filter_name));
1004
                } catch(PDOException $e) {
1005
                        echo "error : ".$e->getMessage();
1006
                }
1007
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1008
                return $all[0]['total'];
1009
        }
1010
	public function getStatsPilot($pilot,$filter_name = '') {
1011
    		global $globalArchiveMonths, $globalDBdriver;
1012
		if ($filter_name == '') $filter_name = $this->filter_name;
1013
		$query = "SELECT cnt FROM stats_pilot WHERE filter_name = :filter_name AND (pilot_name = :pilot OR pilot_id = :pilot)";
1014
                 try {
1015
                        $sth = $this->db->prepare($query);
1016
                        $sth->execute(array(':filter_name' => $filter_name,':pilot' => $pilot));
1017
                } catch(PDOException $e) {
1018
                        echo "error : ".$e->getMessage();
1019
                }
1020
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1021
                if (isset($all[0]['cnt'])) return $all[0]['cnt'];
1022
                else return 0;
1023
        }
1024
1025
	public function addStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
1026
		global $globalDBdriver;
1027
		if ($filter_name == '') $filter_name = $this->filter_name;
1028
		if ($globalDBdriver == 'mysql') {
1029
			$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";
1030
                } else {
1031
			$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);"; 
1032
		}
1033
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1034
                 try {
1035
                        $sth = $this->db->prepare($query);
1036
                        $sth->execute($query_values);
1037
                } catch(PDOException $e) {
1038
                        return "error : ".$e->getMessage();
1039
                }
1040
        }
1041
	public function updateStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
1042
		global $globalDBdriver;
1043
		if ($filter_name == '') $filter_name = $this->filter_name;
1044
		if ($globalDBdriver == 'mysql') {
1045
			$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";
1046
		} else {
1047
            		//$query = "INSERT INTO stats (stats_type,cnt,stats_date) VALUES (:type,:cnt,:stats_date) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
1048
			$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);"; 
1049
                }
1050
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1051
                 try {
1052
                        $sth = $this->db->prepare($query);
1053
                        $sth->execute($query_values);
1054
                } catch(PDOException $e) {
1055
                        return "error : ".$e->getMessage();
1056
                }
1057
        }
1058
        /*
1059
	public function getStatsSource($date,$stats_type = '') {
1060
		if ($stats_type == '') {
1061
			$query = "SELECT * FROM stats_source WHERE stats_date = :date ORDER BY source_name";
1062
			$query_values = array(':date' => $date);
1063
		} else {
1064
			$query = "SELECT * FROM stats_source WHERE stats_date = :date AND stats_type = :stats_type ORDER BY source_name";
1065
			$query_values = array(':date' => $date,':stats_type' => $stats_type);
1066
		}
1067
                 try {
1068
                        $sth = $this->db->prepare($query);
1069
                        $sth->execute($query_values);
1070
                } catch(PDOException $e) {
1071
                        echo "error : ".$e->getMessage();
1072
                }
1073
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1074
                return $all;
1075
        }
1076
        */
1077
1078
	public function getStatsSource($stats_type,$year = '',$month = '',$day = '') {
1079
		global $globalDBdriver;
1080
		$query = "SELECT * FROM stats_source WHERE stats_type = :stats_type";
1081
		$query_values = array();
1082
		if ($globalDBdriver == 'mysql') {
1083
			if ($year != '') {
1084
				$query .= ' AND YEAR(stats_date) = :year';
1085
				$query_values = array_merge($query_values,array(':year' => $year));
1086
			}
1087
			if ($month != '') {
1088
				$query .= ' AND MONTH(stats_date) = :month';
1089
				$query_values = array_merge($query_values,array(':month' => $month));
1090
			}
1091
			if ($day != '') {
1092
				$query .= ' AND DAY(stats_date) = :day';
1093
				$query_values = array_merge($query_values,array(':day' => $day));
1094
			}
1095
		} else {
1096
			if ($year != '') {
1097
				$query .= ' AND EXTRACT(YEAR FROM stats_date) = :year';
1098
				$query_values = array_merge($query_values,array(':year' => $year));
1099
			}
1100
			if ($month != '') {
1101
				$query .= ' AND EXTRACT(MONTH FROM stats_date) = :month';
1102
				$query_values = array_merge($query_values,array(':month' => $month));
1103
			}
1104
			if ($day != '') {
1105
				$query .= ' AND EXTRACT(DAY FROM stats_date) = :day';
1106
				$query_values = array_merge($query_values,array(':day' => $day));
1107
			}
1108
		}
1109
		$query .= " ORDER BY source_name";
1110
		$query_values = array_merge($query_values,array(':stats_type' => $stats_type));
1111
		try {
1112
			$sth = $this->db->prepare($query);
1113
			$sth->execute($query_values);
1114
		} catch(PDOException $e) {
1115
			echo "error : ".$e->getMessage();
1116
		}
1117
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1118
		return $all;
1119
	}
1120
1121
	public function addStatSource($data,$source_name,$stats_type,$date) {
1122
		global $globalDBdriver;
1123
		if ($globalDBdriver == 'mysql') {
1124
			$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";
1125
		} else {
1126
			$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);"; 
1127
                }
1128
                $query_values = array(':data' => $data,':stats_date' => $date,':source_name' => $source_name,':stats_type' => $stats_type);
1129
                 try {
1130
                        $sth = $this->db->prepare($query);
1131
                        $sth->execute($query_values);
1132
                } catch(PDOException $e) {
1133
                        return "error : ".$e->getMessage();
1134
                }
1135
        }
1136
	public function addStatFlight($type,$date_name,$cnt,$stats_airline = '',$filter_name = '') {
1137
                $query = "INSERT INTO stats_flight (stats_type,flight_date,cnt,stats_airline,filter_name) VALUES (:type,:flight_date,:cnt,:stats_airline,:filter_name)";
1138
                $query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1139
                 try {
1140
                        $sth = $this->db->prepare($query);
1141
                        $sth->execute($query_values);
1142
                } catch(PDOException $e) {
1143
                        return "error : ".$e->getMessage();
1144
                }
1145
        }
1146
	public function addStatAircraftRegistration($registration,$cnt,$aircraft_icao = '',$airline_icao = '',$filter_name = '',$reset = false) {
1147
		global $globalDBdriver;
1148
		if ($globalDBdriver == 'mysql') {
1149
			if ($reset) {
1150
				$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";
1151
			} else {
1152
				$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";
1153
			}
1154
		} else {
1155
			if ($reset) {
1156
				$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);"; 
1157
			} else {
1158
				$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);"; 
1159
			}
1160
		}
1161
                $query_values = array(':aircraft_icao' => $aircraft_icao,':registration' => $registration,':cnt' => $cnt,':stats_airline' => $airline_icao, ':filter_name' => $filter_name);
1162
                 try {
1163
                        $sth = $this->db->prepare($query);
1164
                        $sth->execute($query_values);
1165
                } catch(PDOException $e) {
1166
                        return "error : ".$e->getMessage();
1167
                }
1168
        }
1169
	public function addStatCallsign($callsign_icao,$cnt,$airline_icao = '', $filter_name = '', $reset = false) {
1170
		global $globalDBdriver;
1171
		if ($globalDBdriver == 'mysql') {
1172
			if ($reset) {
1173
				$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";
1174
			} else {
1175
				$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";
1176
			}
1177
		} else {
1178
			if ($reset) {
1179
				$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);"; 
1180
			} else {
1181
				$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);"; 
1182
			}
1183
		}
1184
                $query_values = array(':callsign_icao' => $callsign_icao,':airline_icao' => $airline_icao,':cnt' => $cnt, ':filter_name' => $filter_name);
1185
                 try {
1186
                        $sth = $this->db->prepare($query);
1187
                        $sth->execute($query_values);
1188
                } catch(PDOException $e) {
1189
                        return "error : ".$e->getMessage();
1190
                }
1191
        }
1192
	public function addStatCountry($iso2,$iso3,$name,$cnt,$airline_icao = '',$filter_name = '',$reset = false) {
1193
		global $globalDBdriver;
1194
		if ($globalDBdriver == 'mysql') {
1195
			if ($reset) {
1196
				$query = "INSERT INTO stats_country (iso2,iso3,name,cnt,stats_airline,filter_name) VALUES (:iso2,:iso3,:name,:cnt,:airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt";
1197
			} else {
1198
				$query = "INSERT INTO stats_country (iso2,iso3,name,cnt,stats_airline,filter_name) VALUES (:iso2,:iso3,:name,:cnt,:airline,:filter_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
1199
			}
1200
		} else {
1201
			if ($reset) {
1202
				$query = "UPDATE stats_country SET cnt = :cnt WHERE iso2 = :iso2 AND filter_name = :filter_name AND stats_airline = :airline; INSERT INTO stats_country (iso2,iso3,name,cnt,stats_airline,filter_name) SELECT :iso2,:iso3,:name,:cnt,:airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_country WHERE iso2 = :iso2 AND filter_name = :filter_name AND stats_airline = :airline);"; 
1203
			} else {
1204
				$query = "UPDATE stats_country SET cnt = cnt+:cnt WHERE iso2 = :iso2 AND filter_name = :filter_name AND stats_airline = :airline; INSERT INTO stats_country (iso2,iso3,name,cnt,stats_airline,filter_name) SELECT :iso2,:iso3,:name,:cnt,:airline,:filter_name WHERE NOT EXISTS (SELECT 1 FROM stats_country WHERE iso2 = :iso2 AND filter_name = :filter_name AND stats_airline = :airline);"; 
1205
			}
1206
		}
1207
                $query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt,':filter_name' => $filter_name,':airline' => $airline_icao);
1208
                 try {
1209
                        $sth = $this->db->prepare($query);
1210
                        $sth->execute($query_values);
1211
                } catch(PDOException $e) {
1212
                        return "error : ".$e->getMessage();
1213
                }
1214
        }
1215
	public function addStatAircraft($aircraft_icao,$cnt,$aircraft_name = '',$aircraft_manufacturer = '', $airline_icao = '', $filter_name = '', $reset = false) {
1216
		global $globalDBdriver;
1217
		if ($globalDBdriver == 'mysql') {
1218
			if ($reset) {
1219
				$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";
1220
			} else {
1221
				$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";
1222
			}
1223
		} else {
1224
			if ($reset) {
1225
				$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);"; 
1226
			} else {
1227
				$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);"; 
1228
			}
1229
		}
1230
                $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);
1231
                 try {
1232
                        $sth = $this->db->prepare($query);
1233
                        $sth->execute($query_values);
1234
                } catch(PDOException $e) {
1235
                        return "error : ".$e->getMessage();
1236
                }
1237
        }
1238
	public function addStatAirline($airline_icao,$cnt,$airline_name = '',$filter_name = '', $reset = false) {
1239
		global $globalDBdriver;
1240
		if ($globalDBdriver == 'mysql') {
1241
			if ($reset) {
1242
				$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";
1243
			} else {
1244
				$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";
1245
			}
1246
		} else {
1247
			if ($reset) {
1248
				$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);"; 
1249
			} else {
1250
				$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);"; 
1251
			}
1252
		}
1253
                $query_values = array(':airline_icao' => $airline_icao,':airline_name' => $airline_name,':cnt' => $cnt,':filter_name' => $filter_name);
1254
                 try {
1255
                        $sth = $this->db->prepare($query);
1256
                        $sth->execute($query_values);
1257
                } catch(PDOException $e) {
1258
                        return "error : ".$e->getMessage();
1259
                }
1260
        }
1261
	public function addStatOwner($owner_name,$cnt,$stats_airline = '', $filter_name = '', $reset = false) {
1262
		global $globalDBdriver;
1263
		if ($globalDBdriver == 'mysql') {
1264
			if ($reset) {
1265
				$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";
1266
			} else {
1267
				$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";
1268
			}
1269
		} else {
1270
			if ($reset) {
1271
				$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);"; 
1272
			} else {
1273
				$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);"; 
1274
			}
1275
		}
1276
                $query_values = array(':owner_name' => $owner_name,':cnt' => $cnt,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1277
                 try {
1278
                        $sth = $this->db->prepare($query);
1279
                        $sth->execute($query_values);
1280
                } catch(PDOException $e) {
1281
                        return "error : ".$e->getMessage();
1282
                }
1283
        }
1284
	public function addStatPilot($pilot_id,$cnt,$pilot_name,$stats_airline = '',$filter_name = '',$format_source = '',$reset = false) {
1285
		global $globalDBdriver;
1286
		if ($globalDBdriver == 'mysql') {
1287
			if ($reset) {
1288
				$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";
1289
			} else {
1290
				$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";
1291
			}
1292
		} else {
1293
			if ($reset) {
1294
				$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);"; 
1295
			} else {
1296
				$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);"; 
1297
			}
1298
		}
1299
                $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);
1300
                 try {
1301
                        $sth = $this->db->prepare($query);
1302
                        $sth->execute($query_values);
1303
                } catch(PDOException $e) {
1304
                        return "error : ".$e->getMessage();
1305
                }
1306
        }
1307
	public function addStatDepartureAirports($airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '',$reset = false) {
1308
		global $globalDBdriver;
1309
		if ($airport_icao != '') {
1310
			if ($globalDBdriver == 'mysql') {
1311
				if ($reset) {
1312
					$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";
1313
				} else {
1314
					$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";
1315
				}
1316
			} else {
1317
				if ($reset) {
1318
					$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);"; 
1319
				} else {
1320
					$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);"; 
1321
				}
1322
			}
1323
			$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);
1324
			try {
1325
				$sth = $this->db->prepare($query);
1326
				$sth->execute($query_values);
1327
			} catch(PDOException $e) {
1328
				return "error : ".$e->getMessage();
1329
			}
1330
                }
1331
        }
1332
	public function addStatDepartureAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '') {
1333
		global $globalDBdriver;
1334
		if ($airport_icao != '') {
1335
			if ($globalDBdriver == 'mysql') {
1336
				$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";
1337
			} else {
1338
				$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);"; 
1339
			}
1340
			$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);
1341
			 try {
1342
				$sth = $this->db->prepare($query);
1343
				$sth->execute($query_values);
1344
			} catch(PDOException $e) {
1345
				return "error : ".$e->getMessage();
1346
			}
1347
                }
1348
        }
1349
	public function addStatArrivalAirports($airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '',$reset = false) {
1350
		global $globalDBdriver;
1351
		if ($airport_icao != '') {
1352
			if ($globalDBdriver == 'mysql') {
1353
				if ($reset) {
1354
					$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";
1355
				} else {
1356
					$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";
1357
				}
1358
			} else {
1359
				if ($reset) {
1360
					$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);"; 
1361
				} else {
1362
					$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);"; 
1363
				}
1364
			}
1365
	                $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);
1366
			 try {
1367
                    		$sth = $this->db->prepare($query);
1368
	                        $sth->execute($query_values);
1369
    		        } catch(PDOException $e) {
1370
            		        return "error : ".$e->getMessage();
1371
	                }
1372
	        }
1373
        }
1374
	public function addStatArrivalAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '') {
1375
		global $globalDBdriver;
1376
		if ($airport_icao != '') {
1377
			if ($globalDBdriver == 'mysql') {
1378
				$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";
1379
			} else {
1380
				$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);"; 
1381
			}
1382
			$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);
1383
			try {
1384
				$sth = $this->db->prepare($query);
1385
				$sth->execute($query_values);
1386
			} catch(PDOException $e) {
1387
				return "error : ".$e->getMessage();
1388
			}
1389
                }
1390
        }
1391
1392
	public function deleteStat($id) {
1393
                $query = "DELETE FROM stats WHERE stats_id = :id";
1394
                $query_values = array(':id' => $id);
1395
                 try {
1396
                        $sth = $this->db->prepare($query);
1397
                        $sth->execute($query_values);
1398
                } catch(PDOException $e) {
1399
                        return "error : ".$e->getMessage();
1400
                }
1401
        }
1402
	public function deleteStatFlight($type) {
1403
                $query = "DELETE FROM stats_flight WHERE stats_type = :type";
1404
                $query_values = array(':type' => $type);
1405
                 try {
1406
                        $sth = $this->db->prepare($query);
1407
                        $sth->execute($query_values);
1408
                } catch(PDOException $e) {
1409
                        return "error : ".$e->getMessage();
1410
                }
1411
        }
1412
	public function deleteStatAirport($type) {
1413
                $query = "DELETE FROM stats_airport WHERE stats_type = :type";
1414
                $query_values = array(':type' => $type);
1415
                 try {
1416
                        $sth = $this->db->prepare($query);
1417
                        $sth->execute($query_values);
1418
                } catch(PDOException $e) {
1419
                        return "error : ".$e->getMessage();
1420
                }
1421
        }
1422
        
1423
        public function addOldStats() {
1424
    		global $globalDebug, $globalArchiveMonths, $globalArchive, $globalArchiveYear, $globalDBdriver, $globalStatsFilters,$globalDeleteLastYearStats,$globalStatsReset,$globalStatsResetYear;
1425
    		$Common = new Common();
1426
    		$Connection = new Connection();
1427
    		date_default_timezone_set('UTC');
1428
    		$last_update = $this->getLastStatsUpdate('last_update_stats');
1429
			if ($globalDebug) echo 'Update stats !'."\n";
1430
			if (isset($last_update[0]['value'])) {
1431
				$last_update_day = $last_update[0]['value'];
1432
			} else $last_update_day = '2012-12-12 12:12:12';
1433
			$reset = false;
1434
			if ($globalStatsResetYear) {
1435
				$reset = true;
1436
				$last_update_day = date('Y').'-01-01 00:00:00';
1437
			}
1438
			$Spotter = new Spotter($this->db);
1439
1440
			if ($globalDebug) echo 'Count all aircraft types...'."\n";
1441
			$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day);
1442
			foreach ($alldata as $number) {
1443
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'','',$reset);
1444
			}
1445
			if ($globalDebug) echo 'Count all airlines...'."\n";
1446
			$alldata = $Spotter->countAllAirlines(false,0,$last_update_day);
1447
			foreach ($alldata as $number) {
1448
				$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],'',$reset);
1449
			}
1450
			if ($globalDebug) echo 'Count all registrations...'."\n";
1451
			$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day);
1452
			foreach ($alldata as $number) {
1453
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'','',$reset);
1454
			}
1455
			if ($globalDebug) echo 'Count all callsigns...'."\n";
1456
			$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day);
1457
			foreach ($alldata as $number) {
1458
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
1459
			}
1460
			if ($globalDebug) echo 'Count all owners...'."\n";
1461
			$alldata = $Spotter->countAllOwners(false,0,$last_update_day);
1462
			foreach ($alldata as $number) {
1463
				$this->addStatOwner($number['owner_name'],$number['owner_count'],'','',$reset);
1464
			}
1465
			if ($globalDebug) echo 'Count all pilots...'."\n";
1466
			$alldata = $Spotter->countAllPilots(false,0,$last_update_day);
1467
			foreach ($alldata as $number) {
1468
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'','',$number['format_source'],$reset);
1469
			}
1470
			
1471
			if ($globalDebug) echo 'Count all departure airports...'."\n";
1472
			$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day);
1473
			if ($globalDebug) echo 'Count all detected departure airports...'."\n";
1474
        		$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day);
1475
			if ($globalDebug) echo 'Order departure airports...'."\n";
1476
	        	$alldata = array();
1477
	        	
1478
    			foreach ($pall as $value) {
1479
	        		$icao = $value['airport_departure_icao'];
1480
    				$alldata[$icao] = $value;
1481
	        	}
1482
	        	foreach ($dall as $value) {
1483
    				$icao = $value['airport_departure_icao'];
1484
        			if (isset($alldata[$icao])) {
1485
    					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1486
        			} else $alldata[$icao] = $value;
1487
			}
1488
    			$count = array();
1489
    			foreach ($alldata as $key => $row) {
1490
    				$count[$key] = $row['airport_departure_icao_count'];
1491
        		}
1492
			array_multisort($count,SORT_DESC,$alldata);
1493
			foreach ($alldata as $number) {
1494
				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);
1495
			}
1496
			if ($globalDebug) echo 'Count all arrival airports...'."\n";
1497
			$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day);
1498
			if ($globalDebug) echo 'Count all detected arrival airports...'."\n";
1499
        		$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day);
1500
			if ($globalDebug) echo 'Order arrival airports...'."\n";
1501
	        	$alldata = array();
1502
    			foreach ($pall as $value) {
1503
	        		$icao = $value['airport_arrival_icao'];
1504
    				$alldata[$icao] = $value;
1505
	        	}
1506
	        	foreach ($dall as $value) {
1507
    				$icao = $value['airport_arrival_icao'];
1508
        			if (isset($alldata[$icao])) {
1509
        				$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1510
	        		} else $alldata[$icao] = $value;
1511
    			}
1512
        		$count = array();
1513
        		foreach ($alldata as $key => $row) {
1514
        			$count[$key] = $row['airport_arrival_icao_count'];
1515
	        	}
1516
    			array_multisort($count,SORT_DESC,$alldata);
1517
                        foreach ($alldata as $number) {
1518
				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);
1519
			}
1520
			if ($Connection->tableExists('countries')) {
1521
				if ($globalDebug) echo 'Count all flights by countries...'."\n";
1522
				$SpotterArchive = new SpotterArchive();
1523
				$alldata = $SpotterArchive->countAllFlightOverCountries(false,0,$last_update_day);
1524
				foreach ($alldata as $number) {
1525
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],'','',$reset);
1526
				}
1527
			}
1528
			
1529
1530
			// Add by month using getstat if month finish...
1531
1532
			//if (date('m',strtotime($last_update_day)) != date('m')) {
1533
			if ($globalDebug) echo 'Count all flights by months...'."\n";
1534
			$Spotter = new Spotter($this->db);
1535
			$alldata = $Spotter->countAllMonths();
1536
			$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...
1537
			foreach ($alldata as $number) {
1538
				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...
1539
				$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'])));
1540
			}
1541
			if ($globalDebug) echo 'Count all military flights by months...'."\n";
1542
			$alldata = $Spotter->countAllMilitaryMonths();
1543
			foreach ($alldata as $number) {
1544
				$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'])));
1545
			}
1546
			if ($globalDebug) echo 'Count all owners by months...'."\n";
1547
			$alldata = $Spotter->countAllMonthsOwners();
1548
			foreach ($alldata as $number) {
1549
				$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'])));
1550
			}
1551
			if ($globalDebug) echo 'Count all pilots by months...'."\n";
1552
			$alldata = $Spotter->countAllMonthsPilots();
1553
			foreach ($alldata as $number) {
1554
				$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'])));
1555
			}
1556
			if ($globalDebug) echo 'Count all airlines by months...'."\n";
1557
			$alldata = $Spotter->countAllMonthsAirlines();
1558
			foreach ($alldata as $number) {
1559
				$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'])));
1560
			}
1561
			if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
1562
			$alldata = $Spotter->countAllMonthsAircrafts();
1563
			foreach ($alldata as $number) {
1564
				$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'])));
1565
			}
1566
			if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
1567
			$alldata = $Spotter->countAllMonthsRealArrivals();
1568
			foreach ($alldata as $number) {
1569
				$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'])));
1570
			}
1571
			if ($globalDebug) echo 'Airports data...'."\n";
1572
			if ($globalDebug) echo '...Departure'."\n";
1573
			$this->deleteStatAirport('daily');
1574
//			$pall = $Spotter->getLast7DaysAirportsDeparture();
1575
  //      		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1576
			$pall = $Spotter->getLast7DaysAirportsDeparture();
1577
        		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1578
        		/*
1579
	        	$alldata = array();
1580
    			foreach ($pall as $value) {
1581
	        		$icao = $value['departure_airport_icao'];
1582
    				$alldata[$icao] = $value;
1583
	        	}
1584
	        	foreach ($dall as $value) {
1585
    				$icao = $value['departure_airport_icao'];
1586
    				$ddate = $value['date'];
1587
        			if (isset($alldata[$icao])) {
1588
        				$alldata[$icao]['departure_airport_count'] = $alldata[$icao]['departure_airport_count'] + $value['departure_airport_count'];
1589
	        		} else $alldata[$icao] = $value;
1590
    			}
1591
        		$count = array();
1592
        		foreach ($alldata as $key => $row) {
1593
        			$count[$key] = $row['departure_airport_count'];
1594
	        	}
1595
    			array_multisort($count,SORT_DESC,$alldata);
1596
    			*/
1597
    			foreach ($dall as $value) {
1598
    				$icao = $value['departure_airport_icao'];
1599
    				$ddate = $value['date'];
1600
    				$find = false;
1601
    				foreach ($pall as $pvalue) {
1602
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1603
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1604
    						$find = true;
1605
    						break;
1606
    					}
1607
    				}
1608
    				if ($find === false) {
1609
    					$pall[] = $value;
1610
    				}
1611
    			}
1612
    			$alldata = $pall;
1613
			foreach ($alldata as $number) {
1614
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count']);
1615
			}
1616
			echo '...Arrival'."\n";
1617
			$pall = $Spotter->getLast7DaysAirportsArrival();
1618
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrival();
1619
        		/*
1620
	        	$alldata = array();
1621
    			foreach ($pall as $value) {
1622
	        		$icao = $value['arrival_airport_icao'];
1623
    				$alldata[$icao] = $value;
1624
	        	}
1625
	        	foreach ($dall as $value) {
1626
    				$icao = $value['arrival_airport_icao'];
1627
        			if (isset($alldata[$icao])) {
1628
        				$alldata[$icao]['arrival_airport_icao_count'] = $alldata[$icao]['arrival_airport_count'] + $value['arrival_airport_count'];
1629
	        		} else $alldata[$icao] = $value;
1630
    			}
1631
        		$count = array();
1632
        		foreach ($alldata as $key => $row) {
1633
        			$count[$key] = $row['arrival_airport_count'];
1634
	        	}
1635
    			array_multisort($count,SORT_DESC,$alldata);
1636
    			*/
1637
1638
1639
    			foreach ($dall as $value) {
1640
    				$icao = $value['arrival_airport_icao'];
1641
    				$ddate = $value['date'];
1642
    				$find = false;
1643
    				foreach ($pall as $pvalue) {
1644
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1645
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1646
    						$find = true;
1647
    						break;
1648
    					}
1649
    				}
1650
    				if ($find === false) {
1651
    					$pall[] = $value;
1652
    				}
1653
    			}
1654
    			$alldata = $pall;
1655
			foreach ($alldata as $number) {
1656
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count']);
1657
			}
1658
1659
			echo 'Flights data...'."\n";
1660
			$this->deleteStatFlight('month');
1661
			echo '-> countAllDatesLastMonth...'."\n";
1662
			$alldata = $Spotter->countAllDatesLastMonth();
1663
			foreach ($alldata as $number) {
1664
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
1665
			}
1666
			echo '-> countAllDates...'."\n";
1667
			$previousdata = $this->countAllDates();
1668
			$previousdatabyairlines = $this->countAllDatesByAirlines();
1669
			$this->deleteStatFlight('date');
1670
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates());
1671
			$values = array();
1672
			foreach ($alldata as $cnt) {
1673
				$values[] = $cnt['date_count'];
1674
			}
1675
			array_multisort($values,SORT_DESC,$alldata);
1676
			array_splice($alldata,11);
1677
			foreach ($alldata as $number) {
1678
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
1679
			}
1680
			
1681
			$this->deleteStatFlight('hour');
1682
			echo '-> countAllHours...'."\n";
1683
			$alldata = $Spotter->countAllHours('hour');
1684
			foreach ($alldata as $number) {
1685
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
1686
			}
1687
1688
1689
1690
			// Count by airlines
1691
			echo '--- Stats by airlines ---'."\n";
1692
			if ($Connection->tableExists('countries')) {
1693
				if ($globalDebug) echo 'Count all flights by countries by airlines...'."\n";
1694
				$SpotterArchive = new SpotterArchive();
1695
				$alldata = $SpotterArchive->countAllFlightOverCountriesByAirlines(false,0,$last_update_day);
1696
				foreach ($alldata as $number) {
1697
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],$number['airline_icao'],'',$reset);
1698
				}
1699
			}
1700
			if ($globalDebug) echo 'Count all aircraft types by airlines...'."\n";
1701
			$Spotter = new Spotter($this->db);
1702
			$alldata = $Spotter->countAllAircraftTypesByAirlines(false,0,$last_update_day);
1703
			foreach ($alldata as $number) {
1704
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],$number['airline_icao'],'',$reset);
1705
			}
1706
			if ($globalDebug) echo 'Count all aircraft registrations by airlines...'."\n";
1707
			$alldata = $Spotter->countAllAircraftRegistrationsByAirlines(false,0,$last_update_day);
1708
			foreach ($alldata as $number) {
1709
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],$number['airline_icao'],'',$reset);
1710
			}
1711
			if ($globalDebug) echo 'Count all callsigns by airlines...'."\n";
1712
			$alldata = $Spotter->countAllCallsignsByAirlines(false,0,$last_update_day);
1713
			foreach ($alldata as $number) {
1714
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
1715
			}
1716
			if ($globalDebug) echo 'Count all owners by airlines...'."\n";
1717
			$alldata = $Spotter->countAllOwnersByAirlines(false,0,$last_update_day);
1718
			foreach ($alldata as $number) {
1719
				$this->addStatOwner($number['owner_name'],$number['owner_count'],$number['airline_icao'],'',$reset);
1720
			}
1721
			if ($globalDebug) echo 'Count all pilots by airlines...'."\n";
1722
			$alldata = $Spotter->countAllPilotsByAirlines(false,0,$last_update_day);
1723
			foreach ($alldata as $number) {
1724
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],$number['airline_icao'],'',$number['format_source'],$reset);
1725
			}
1726
			if ($globalDebug) echo 'Count all departure airports by airlines...'."\n";
1727
			$pall = $Spotter->countAllDepartureAirportsByAirlines(false,0,$last_update_day);
1728
			if ($globalDebug) echo 'Count all detected departure airports by airlines...'."\n";
1729
       			$dall = $Spotter->countAllDetectedDepartureAirportsByAirlines(false,0,$last_update_day);
1730
			if ($globalDebug) echo 'Order detected departure airports by airlines...'."\n";
1731
	        	//$alldata = array();
1732
    			foreach ($dall as $value) {
1733
    				$icao = $value['airport_departure_icao'];
1734
    				$dicao = $value['airline_icao'];
1735
    				$find = false;
1736
    				foreach ($pall as $pvalue) {
1737
    					if ($pvalue['airport_departure_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1738
    						$pvalue['airport_departure_icao_count'] = $pvalue['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1739
    						$find = true;
1740
    						break;
1741
    					}
1742
    				}
1743
    				if ($find === false) {
1744
    					$pall[] = $value;
1745
    				}
1746
    			}
1747
    			$alldata = $pall;
1748
			foreach ($alldata as $number) {
1749
				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);
1750
			}
1751
			if ($globalDebug) echo 'Count all arrival airports by airlines...'."\n";
1752
			$pall = $Spotter->countAllArrivalAirportsByAirlines(false,0,$last_update_day);
1753
			if ($globalDebug) echo 'Count all detected arrival airports by airlines...'."\n";
1754
        		$dall = $Spotter->countAllDetectedArrivalAirportsByAirlines(false,0,$last_update_day);
1755
			if ($globalDebug) echo 'Order arrival airports by airlines...'."\n";
1756
	        	//$alldata = array();
1757
    			foreach ($dall as $value) {
1758
    				$icao = $value['airport_arrival_icao'];
1759
    				$dicao = $value['airline_icao'];
1760
    				$find = false;
1761
    				foreach ($pall as $pvalue) {
1762
    					if ($pvalue['airport_arrival_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1763
    						$pvalue['airport_arrival_icao_count'] = $pvalue['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1764
    						$find = true;
1765
    						break;
1766
    					}
1767
    				}
1768
    				if ($find === false) {
1769
    					$pall[] = $value;
1770
    				}
1771
    			}
1772
    			$alldata = $pall;
1773
                        foreach ($alldata as $number) {
1774
				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);
1775
			}
1776
			if ($globalDebug) echo 'Count all flights by months by airlines...'."\n";
1777
			$Spotter = new Spotter($this->db);
1778
			$alldata = $Spotter->countAllMonthsByAirlines();
1779
			$lastyear = false;
1780
			foreach ($alldata as $number) {
1781
				if ($number['year_name'] != date('Y')) $lastyear = true;
1782
				$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']);
1783
			}
1784
			if ($globalDebug) echo 'Count all owners by months by airlines...'."\n";
1785
			$alldata = $Spotter->countAllMonthsOwnersByAirlines();
1786
			foreach ($alldata as $number) {
1787
				$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']);
1788
			}
1789
			if ($globalDebug) echo 'Count all pilots by months by airlines...'."\n";
1790
			$alldata = $Spotter->countAllMonthsPilotsByAirlines();
1791
			foreach ($alldata as $number) {
1792
				$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']);
1793
			}
1794
			if ($globalDebug) echo 'Count all aircrafts by months by airlines...'."\n";
1795
			$alldata = $Spotter->countAllMonthsAircraftsByAirlines();
1796
			foreach ($alldata as $number) {
1797
				$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']);
1798
			}
1799
			if ($globalDebug) echo 'Count all real arrivals by months by airlines...'."\n";
1800
			$alldata = $Spotter->countAllMonthsRealArrivalsByAirlines();
1801
			foreach ($alldata as $number) {
1802
				$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']);
1803
			}
1804
			if ($globalDebug) echo '...Departure'."\n";
1805
			$pall = $Spotter->getLast7DaysAirportsDepartureByAirlines();
1806
        		$dall = $Spotter->getLast7DaysDetectedAirportsDepartureByAirlines();
1807
    			foreach ($dall as $value) {
1808
    				$icao = $value['departure_airport_icao'];
1809
    				$airline = $value['airline_icao'];
1810
    				$ddate = $value['date'];
1811
    				$find = false;
1812
    				foreach ($pall as $pvalue) {
1813
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] = $airline) {
1814
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1815
    						$find = true;
1816
    						break;
1817
    					}
1818
    				}
1819
    				if ($find === false) {
1820
    					$pall[] = $value;
1821
    				}
1822
    			}
1823
    			$alldata = $pall;
1824
			foreach ($alldata as $number) {
1825
				$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']);
1826
			}
1827
			if ($globalDebug) echo '...Arrival'."\n";
1828
			$pall = $Spotter->getLast7DaysAirportsArrivalByAirlines();
1829
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrivalByAirlines();
1830
    			foreach ($dall as $value) {
1831
    				$icao = $value['arrival_airport_icao'];
1832
    				$airline = $value['airline_icao'];
1833
    				$ddate = $value['date'];
1834
    				$find = false;
1835
    				foreach ($pall as $pvalue) {
1836
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] == $airline) {
1837
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1838
    						$find = true;
1839
    						break;
1840
    					}
1841
    				}
1842
    				if ($find === false) {
1843
    					$pall[] = $value;
1844
    				}
1845
    			}
1846
    			$alldata = $pall;
1847
			foreach ($alldata as $number) {
1848
				$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']);
1849
			}
1850
1851
			if ($globalDebug) echo 'Flights data...'."\n";
1852
			if ($globalDebug) echo '-> countAllDatesLastMonth...'."\n";
1853
			$alldata = $Spotter->countAllDatesLastMonthByAirlines();
1854
			foreach ($alldata as $number) {
1855
				$this->addStatFlight('month',$number['date_name'],$number['date_count'], $number['airline_icao']);
1856
			}
1857
			if ($globalDebug) echo '-> countAllDates...'."\n";
1858
			//$previousdata = $this->countAllDatesByAirlines();
1859
			$alldata = $Common->array_merge_noappend($previousdatabyairlines,$Spotter->countAllDatesByAirlines());
1860
			$values = array();
1861
			foreach ($alldata as $cnt) {
1862
				$values[] = $cnt['date_count'];
1863
			}
1864
			array_multisort($values,SORT_DESC,$alldata);
1865
			array_splice($alldata,11);
1866
			foreach ($alldata as $number) {
1867
				$this->addStatFlight('date',$number['date_name'],$number['date_count'],$number['airline_icao']);
1868
			}
1869
			
1870
			if ($globalDebug) echo '-> countAllHours...'."\n";
1871
			$alldata = $Spotter->countAllHoursByAirlines('hour');
1872
			foreach ($alldata as $number) {
1873
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],$number['airline_icao']);
1874
			}
1875
			
1876
1877
			// Stats by filters
1878
			if (!isset($globalStatsFilters) || $globalStatsFilters == '') $globalStatsFilters = array();
1879
			foreach ($globalStatsFilters as $name => $filter) {
1880
				//$filter_name = $filter['name'];
1881
				$filter_name = $name;
1882
1883
				$last_update = $this->getLastStatsUpdate('last_update_stats_'.$filter_name);
1884
				if (isset($last_update[0]['value'])) {
1885
					$last_update_day = $last_update[0]['value'];
1886
				} else $last_update_day = '2012-12-12 12:12:12';
1887
				$reset = false;
1888
1889
				// Count by filter
1890
				if ($globalDebug) echo '--- Stats for filter '.$filter_name.' ---'."\n";
1891
				$Spotter = new Spotter($this->db);
1892
				$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day,$filter);
1893
				foreach ($alldata as $number) {
1894
					$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'',$filter_name,$reset);
1895
				}
1896
				$alldata = $Spotter->countAllAirlines(false,0,$last_update_day,$filter);
1897
				foreach ($alldata as $number) {
1898
					$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],$filter_name,$reset);
1899
				}
1900
				$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day,$filter);
1901
				foreach ($alldata as $number) {
1902
					$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'',$filter_name,$reset);
1903
				}
1904
				$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day,$filter);
1905
				foreach ($alldata as $number) {
1906
					$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],'',$filter_name,$reset);
1907
				}
1908
				$alldata = $Spotter->countAllOwners(false,0,$last_update_day,$filter);
1909
				foreach ($alldata as $number) {
1910
					$this->addStatOwner($number['owner_name'],$number['owner_count'],'',$filter_name,$reset);
1911
				}
1912
				$alldata = $Spotter->countAllPilots(false,0,$last_update_day,$filter);
1913
				foreach ($alldata as $number) {
1914
					$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'',$filter_name,$number['format_source'],$reset);
1915
				}
1916
				$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day,$filter);
1917
	       			$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day,$filter);
1918
		        	$alldata = array();
1919
	    			foreach ($pall as $value) {
1920
		        		$icao = $value['airport_departure_icao'];
1921
    					$alldata[$icao] = $value;
1922
	    			}
1923
		        	foreach ($dall as $value) {
1924
	    				$icao = $value['airport_departure_icao'];
1925
        				if (isset($alldata[$icao])) {
1926
    						$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1927
        				} else $alldata[$icao] = $value;
1928
				}
1929
	    			$count = array();
1930
    				foreach ($alldata as $key => $row) {
1931
    					$count[$key] = $row['airport_departure_icao_count'];
1932
    				}
1933
				array_multisort($count,SORT_DESC,$alldata);
1934
				foreach ($alldata as $number) {
1935
    					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);
1936
				}
1937
				$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day,false,$filter);
1938
    				$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day,false,$filter);
1939
				$alldata = array();
1940
    				foreach ($pall as $value) {
1941
		        		$icao = $value['airport_arrival_icao'];
1942
    					$alldata[$icao] = $value;
1943
	    			}
1944
		        	foreach ($dall as $value) {
1945
	    				$icao = $value['airport_arrival_icao'];
1946
        				if (isset($alldata[$icao])) {
1947
        					$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1948
		        		} else $alldata[$icao] = $value;
1949
	    			}
1950
        			$count = array();
1951
        			foreach ($alldata as $key => $row) {
1952
    					$count[$key] = $row['airport_arrival_icao_count'];
1953
		        	}
1954
        			array_multisort($count,SORT_DESC,$alldata);
1955
				foreach ($alldata as $number) {
1956
					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);
1957
				}
1958
				$Spotter = new Spotter($this->db);
1959
				$alldata = $Spotter->countAllMonths($filter);
1960
				$lastyear = false;
1961
				foreach ($alldata as $number) {
1962
					if ($number['year_name'] != date('Y')) $lastyear = true;
1963
					$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);
1964
				}
1965
				$alldata = $Spotter->countAllMonthsOwners($filter);
1966
				foreach ($alldata as $number) {
1967
					$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);
1968
				}
1969
				$alldata = $Spotter->countAllMonthsPilots($filter);
1970
				foreach ($alldata as $number) {
1971
					$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);
1972
				}
1973
				$alldata = $Spotter->countAllMilitaryMonths($filter);
1974
				foreach ($alldata as $number) {
1975
					$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);
1976
				}
1977
				$alldata = $Spotter->countAllMonthsAircrafts($filter);
1978
				foreach ($alldata as $number) {
1979
					$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);
1980
				}
1981
				$alldata = $Spotter->countAllMonthsRealArrivals($filter);
1982
				foreach ($alldata as $number) {
1983
					$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);
1984
				}
1985
				echo '...Departure'."\n";
1986
				$pall = $Spotter->getLast7DaysAirportsDeparture('',$filter);
1987
        			$dall = $Spotter->getLast7DaysDetectedAirportsDeparture('',$filter);
1988
				foreach ($dall as $value) {
1989
    					$icao = $value['departure_airport_icao'];
1990
    					$ddate = $value['date'];
1991
    					$find = false;
1992
    					foreach ($pall as $pvalue) {
1993
    						if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1994
    							$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1995
	    						$find = true;
1996
    							break;
1997
    						}
1998
    					}
1999
    					if ($find === false) {
2000
    						$pall[] = $value;
2001
	    				}
2002
    				}
2003
	    			$alldata = $pall;
2004
				foreach ($alldata as $number) {
2005
					$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);
2006
				}
2007
				echo '...Arrival'."\n";
2008
				$pall = $Spotter->getLast7DaysAirportsArrival('',$filter);
2009
    				$dall = $Spotter->getLast7DaysDetectedAirportsArrival('',$filter);
2010
				foreach ($dall as $value) {
2011
					$icao = $value['arrival_airport_icao'];
2012
					$ddate = $value['date'];
2013
    					$find = false;
2014
					foreach ($pall as $pvalue) {
2015
    						if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2016
    							$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2017
    							$find = true;
2018
    							break;
2019
	    					}
2020
    					}
2021
    					if ($find === false) {
2022
    						$pall[] = $value;
2023
	    				}
2024
    				}
2025
    				$alldata = $pall;
2026
				foreach ($alldata as $number) {
2027
					$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);
2028
				}
2029
    
2030
				echo 'Flights data...'."\n";
2031
				echo '-> countAllDatesLastMonth...'."\n";
2032
				$alldata = $Spotter->countAllDatesLastMonth($filter);
2033
				foreach ($alldata as $number) {
2034
					$this->addStatFlight('month',$number['date_name'],$number['date_count'], '',$filter_name);
2035
				}
2036
				echo '-> countAllDates...'."\n";
2037
				$previousdata = $this->countAllDates('',$filter_name);
2038
				$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter));
2039
				$values = array();
2040
				foreach ($alldata as $cnt) {
2041
					$values[] = $cnt['date_count'];
2042
				}
2043
				array_multisort($values,SORT_DESC,$alldata);
2044
				array_splice($alldata,11);
2045
				foreach ($alldata as $number) {
2046
					$this->addStatFlight('date',$number['date_name'],$number['date_count'],'',$filter_name);
2047
				}
2048
				
2049
				echo '-> countAllHours...'."\n";
2050
				$alldata = $Spotter->countAllHours('hour',$filter);
2051
				foreach ($alldata as $number) {
2052
					$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],'',$filter_name);
2053
				}
2054
				echo 'Insert last stats update date...'."\n";
2055
				date_default_timezone_set('UTC');
2056
				$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y-m-d G:i:s'));
2057
				if (isset($filter['DeleteLastYearStats']) && $filter['DeleteLastYearStats'] == true) {
2058
					if (date('Y',strtotime($last_update_day)) != date('Y')) {
2059
						$this->deleteOldStats($filter_name);
2060
						$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y').'-01-01 00:00:00');
2061
					}
2062
				}
2063
2064
			}
2065
	
2066
2067
			// Last year stats
2068
			if ($lastyear) {
2069
				echo 'Data from last year...'."\n";
2070
				// SUM all previous month to put as year
2071
				$previous_year = date('Y');
2072
				$previous_year--;
2073
				$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2074
				$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2075
				$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2076
				$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2077
				$allairlines = $this->getAllAirlineNames();
2078
				foreach ($allairlines as $data) {
2079
					$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2080
					$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2081
					$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2082
					$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2083
				}
2084
				
2085
				if (isset($globalArchiveYear) && $globalArchiveYear) {
2086
					if ($globalArchive) {
2087
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
2088
						try {
2089
							$sth = $this->db->prepare($query);
2090
							$sth->execute();
2091
						} catch(PDOException $e) {
2092
							return "error : ".$e->getMessage().' - query : '.$query."\n";
2093
						}
2094
					}
2095
					echo 'Delete old data'."\n";
2096
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
2097
					try {
2098
						$sth = $this->db->prepare($query);
2099
						$sth->execute();
2100
					} catch(PDOException $e) {
2101
						return "error : ".$e->getMessage().' - query : '.$query."\n";
2102
					}
2103
				}
2104
				if (isset($globalDeleteLastYearStats) && $globalDeleteLastYearStats) {
2105
					$last_update = $this->getLastStatsUpdate('last_update_stats');
2106
					if (date('Y',strtotime($last_update[0]['value'])) != date('Y')) {
2107
						$this->deleteOldStats();
2108
						$this->addLastStatsUpdate('last_update_stats',date('Y').'-01-01 00:00:00');
2109
						$lastyearupdate = true;
2110
					}
2111
				}
2112
			}
2113
			if ($globalArchiveMonths > 0) {
2114
				if ($globalArchive) {
2115
					echo 'Archive old data...'."\n";
2116
					if ($globalDBdriver == 'mysql') {
2117
						//$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2118
						$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)
2119
							    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
2120
	    						     FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2121
					} else {
2122
						$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)
2123
							     SELECT 
2124
								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
2125
							    FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
2126
					}
2127
					try {
2128
						$sth = $this->db->prepare($query);
2129
						$sth->execute();
2130
					} catch(PDOException $e) {
2131
						return "error : ".$e->getMessage();
2132
					}
2133
				}
2134
				echo 'Deleting old data...'."\n";
2135
				//$query = 'DELETE FROM spotter_output WHERE spotter_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveMonths.' MONTH)';
2136
				if ($globalDBdriver == 'mysql') {
2137
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2138
				} else {
2139
					$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)";
2140
				}
2141
				try {
2142
					$sth = $this->db->prepare($query);
2143
					$sth->execute();
2144
				} catch(PDOException $e) {
2145
					return "error : ".$e->getMessage();
2146
				}
2147
			}
2148
			if (!isset($lastyearupdate)) {
2149
				echo 'Insert last stats update date...'."\n";
2150
				date_default_timezone_set('UTC');
2151
				$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
2152
			}
2153
			if ($globalStatsResetYear) {
2154
				require_once(dirname(__FILE__).'/../install/class.settings.php');
2155
				settings::modify_settings(array('globalStatsResetYear' => 'FALSE'));
2156
			}
2157
2158
		//}
2159
	}
2160
}
2161
2162
?>