Completed
Push — master ( 9aeaea...0f3eb5 )
by Yannick
07:01
created

Stats::addStatDepartureAirportsDaily()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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