Completed
Push — master ( 5e27ff...ea1d88 )
by Yannick
06:20
created

Stats::addStatArrivalAirports()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   Many Parameters   

Many Parameters

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

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
* This class save stats older than a year and $globalArchiveMonths
4
*/
5
6
require_once(dirname(__FILE__).'/class.Spotter.php');
7
require_once(dirname(__FILE__).'/class.SpotterArchive.php');
8
require_once(dirname(__FILE__).'/class.Common.php');
9
class Stats {
10
	public $db;
11
	public $filter_name = '';
12
	
13
	public function __construct($dbc = null) {
14
		global $globalFilterName;
15
		if (isset($globalFilterName)) $this->filter_name = $globalFilterName;
16
		$Connection = new Connection($dbc);
17
		$this->db = $Connection->db();
18
        }
19
              
20
	public function addLastStatsUpdate($type,$stats_date) {
21
                $query = "DELETE FROM config WHERE name = :type;
22
            		INSERT INTO config (name,value) VALUES (:type,:stats_date);";
23
                $query_values = array('type' => $type,':stats_date' => $stats_date);
24
                 try {
25
                        $sth = $this->db->prepare($query);
26
                        $sth->execute($query_values);
27
                } catch(PDOException $e) {
28
                        return "error : ".$e->getMessage();
29
                }
30
        }
31
32
	public function getLastStatsUpdate($type = 'last_update_stats') {
33
                $query = "SELECT value FROM config WHERE name = :type";
34
                 try {
35
                        $sth = $this->db->prepare($query);
36
                        $sth->execute(array(':type' => $type));
37
                } catch(PDOException $e) {
38
                        echo "error : ".$e->getMessage();
39
                }
40
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
41
                return $all;
42
        }
43
        public function deleteStats($filter_name = '') {
44
        	/*
45
        	$query = "DELETE FROM config WHERE name = 'last_update_stats'";
46
                 try {
47
                        $sth = $this->db->prepare($query);
48
                        $sth->execute();
49
                } catch(PDOException $e) {
50
                        return "error : ".$e->getMessage();
51
                }
52
                */
53
        	$query = "DELETE FROM stats WHERE filter_name = :filter_name;DELETE FROM stats_aircraft WHERE filter_name = :filter_name;DELETE FROM stats_airline WHERE filter_name = :filter_name;DELETE FROM stats_airport WHERE filter_name = :filter_name;DELETE FROM stats_callsign WHERE filter_name = :filter_name;DELETE FROM stats_country WHERE filter_name = :filter_name;DELETE FROM stats_flight WHERE filter_name = :filter_name;DELETE FROM stats_owner WHERE filter_name = :filter_name;DELETE FROM stats_pilot WHERE filter_name = :filter_name;DELETE FROM stats_registration WHERE filter_name = :filter_name;";
54
                 try {
55
                        $sth = $this->db->prepare($query);
56
                        $sth->execute(array(':filter_name' => $filter_name));
57
                } catch(PDOException $e) {
58
                        return "error : ".$e->getMessage();
59
                }
60
        }
61
        public function deleteOldStats($filter_name = '') {
62
        	
63
        	$query = "DELETE FROM config WHERE name = 'last_update_stats'";
64
                 try {
65
                        $sth = $this->db->prepare($query);
66
                        $sth->execute();
67
                } catch(PDOException $e) {
68
                        return "error : ".$e->getMessage();
69
                }
70
                
71
        	$query = "DELETE FROM stats_aircraft WHERE filter_name = :filter_name;DELETE FROM stats_airline WHERE filter_name = :filter_name;DELETE FROM stats_callsign WHERE filter_name = :filter_name;DELETE FROM stats_country WHERE filter_name = :filter_name;DELETE FROM stats_owner WHERE filter_name = :filter_name;DELETE FROM stats_pilot WHERE filter_name = :filter_name;DELETE FROM stats_registration WHERE filter_name = :filter_name;";
72
                 try {
73
                        $sth = $this->db->prepare($query);
74
                        $sth->execute(array(':filter_name' => $filter_name));
75
                } catch(PDOException $e) {
76
                        return "error : ".$e->getMessage();
77
                }
78
        }
79
	public function getAllAirlineNames($filter_name = '') {
80
		if ($filter_name == '') $filter_name = $this->filter_name;
81
                $query = "SELECT * FROM stats_airline WHERE filter_name = :filter_name ORDER BY airline_name ASC";
82
                 try {
83
                        $sth = $this->db->prepare($query);
84
                        $sth->execute(array(':filter_name' => $filter_name));
85
                } catch(PDOException $e) {
86
                        echo "error : ".$e->getMessage();
87
                }
88
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
89
                return $all;
90
        }
91
	public function getAllAircraftTypes($stats_airline = '',$filter_name = '') {
92
		if ($filter_name == '') $filter_name = $this->filter_name;
93
                $query = "SELECT * FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY aircraft_manufacturer ASC";
94
                 try {
95
                        $sth = $this->db->prepare($query);
96
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
97
                } catch(PDOException $e) {
98
                        echo "error : ".$e->getMessage();
99
                }
100
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
101
                return $all;
102
        }
103
	public function getAllManufacturers($stats_airline = '',$filter_name = '') {
104
		if ($filter_name == '') $filter_name = $this->filter_name;
105
                $query = "SELECT DISTINCT(aircraft_manufacturer) FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name AND aircraft_manufacturer <> '' ORDER BY aircraft_manufacturer ASC";
106
                 try {
107
                        $sth = $this->db->prepare($query);
108
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
109
                } catch(PDOException $e) {
110
                        echo "error : ".$e->getMessage();
111
                }
112
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
113
                return $all;
114
        }
115
	public function getAllAirportNames($stats_airline = '',$filter_name = '') {
116
		if ($filter_name == '') $filter_name = $this->filter_name;
117
                $query = "SELECT airport_icao, airport_name,airport_city,airport_country FROM stats_airport WHERE stats_airline = :stats_airline AND filter_name = :filter_name AND stats_type = 'daily' GROUP BY airport_icao,airport_name,airport_city,airport_country ORDER BY airport_city ASC";
118
                 try {
119
                        $sth = $this->db->prepare($query);
120
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
121
                } catch(PDOException $e) {
122
                        echo "error : ".$e->getMessage();
123
                }
124
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
125
                return $all;
126
        }
127
128
129
	public function countAllAircraftTypes($limit = true, $stats_airline = '', $filter_name = '') {
130
		global $globalStatsFilters;
131
		if ($filter_name == '') $filter_name = $this->filter_name;
132
		if ($limit) $query = "SELECT aircraft_icao, cnt AS aircraft_icao_count, aircraft_name 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";
133
		else $query = "SELECT aircraft_icao, cnt AS aircraft_icao_count, aircraft_name 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";
134
                 try {
135
                        $sth = $this->db->prepare($query);
136
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
137
                } catch(PDOException $e) {
138
                        echo "error : ".$e->getMessage();
139
                }
140
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
141
                if (empty($all)) {
142
            	    $filters = array('airlines' => array($stats_airline));
143
            	    if ($filter_name != '') {
144
            		    $filters = array_merge($filters,$globalStatsFilters[$filter_name]);
145
            	    }
146
            	    $Spotter = new Spotter($this->db);
147
            	    $all = $Spotter->countAllAircraftTypes($limit,0,'',$filters);
148
                }
149
                return $all;
150
	}
151
	public function countAllAirlineCountries($limit = true,$filter_name = '') {
152
		global $globalStatsFilters;
153
		if ($filter_name == '') $filter_name = $this->filter_name;
154
		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";
155
		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";
156
                 try {
157
                        $sth = $this->db->prepare($query);
158
                        $sth->execute(array(':filter_name' => $filter_name));
159
                } catch(PDOException $e) {
160
                        echo "error : ".$e->getMessage();
161
                }
162
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
163
                if (empty($all)) {
164
            		$Spotter = new Spotter($this->db);
165
            		$filters = array();
166
            		if ($filter_name != '') {
167
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
168
			}
169
            		$all = $Spotter->countAllAirlineCountries($limit,$filters);
170
                }
171
                return $all;
172
	}
173
	public function countAllAircraftManufacturers($limit = true,$stats_airline = '', $filter_name = '') {
174
		global $globalStatsFilters;
175
		if ($filter_name == '') $filter_name = $this->filter_name;
176
		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";
177
		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";
178
                 try {
179
                        $sth = $this->db->prepare($query);
180
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
181
                } catch(PDOException $e) {
182
                        echo "error : ".$e->getMessage();
183
                }
184
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
185
                if (empty($all)) {
186
            		$filters = array('airlines' => array($stats_airline));
187
            		if ($filter_name != '') {
188
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
189
			}
190
            		$Spotter = new Spotter($this->db);
191
			$all = $Spotter->countAllAircraftManufacturers($filters);
192
                }
193
                return $all;
194
	}
195
196
	public function countAllArrivalCountries($limit = true, $stats_airline = '', $filter_name = '') {
197
		global $globalStatsFilters;
198
		if ($filter_name == '') $filter_name = $this->filter_name;
199
		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";
200
		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";
201
                 try {
202
                        $sth = $this->db->prepare($query);
203
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
204
                } catch(PDOException $e) {
205
                        echo "error : ".$e->getMessage();
206
                }
207
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
208
                if (empty($all)) {
209
			$filters = array('airlines' => array($stats_airline));
210
			if ($filter_name != '') {
211
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
212
			}
213
			$Spotter = new Spotter($this->db);
214
			$all = $Spotter->countAllArrivalCountries($limit,$filters);
215
                }
216
                return $all;
217
	}
218
	public function countAllDepartureCountries($limit = true, $stats_airline = '', $filter_name = '') {
219
		global $globalStatsFilters;
220
		if ($filter_name == '') $filter_name = $this->filter_name;
221
		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";
222
		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";
223
                 try {
224
                        $sth = $this->db->prepare($query);
225
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
226
                } catch(PDOException $e) {
227
                        echo "error : ".$e->getMessage();
228
                }
229
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
230
                if (empty($all)) {
231
			$filters = array('airlines' => array($stats_airline));
232
			if ($filter_name != '') {
233
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
234
			}
235
			$Spotter = new Spotter($this->db);
236
			$all = $Spotter->countAllDepartureCountries($filters);
237
                }
238
                return $all;
239
	}
240
241
	public function countAllAirlines($limit = true,$filter_name = '') {
242
		global $globalStatsFilters;
243
		if ($filter_name == '') $filter_name = $this->filter_name;
244
		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";
245
		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";
246
                 try {
247
                        $sth = $this->db->prepare($query);
248
                        $sth->execute(array(':filter_name' => $filter_name));
249
                } catch(PDOException $e) {
250
                        echo "error : ".$e->getMessage();
251
                }
252
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
253
                if (empty($all)) {
254
	                $Spotter = new Spotter($this->db);
255
            		$filters = array();
256
            		if ($filter_name != '') {
257
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
258
			}
259
260
    		        $all = $Spotter->countAllAirlines($limit,0,'',$filters);
261
                }
262
                return $all;
263
	}
264
	public function countAllAircraftRegistrations($limit = true,$stats_airline = '',$filter_name = '') {
265
		global $globalStatsFilters;
266
		if ($filter_name == '') $filter_name = $this->filter_name;
267
		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";
268
		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";
269
                 try {
270
                        $sth = $this->db->prepare($query);
271
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
272
                } catch(PDOException $e) {
273
                        echo "error : ".$e->getMessage();
274
                }
275
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
276
                if (empty($all)) {
277
			$filters = array('airlines' => array($stats_airline));
278
			if ($filter_name != '') {
279
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
280
			}
281
	                $Spotter = new Spotter($this->db);
282
    		        $all = $Spotter->countAllAircraftRegistrations($limit,0,'',$filters);
283
                }
284
                return $all;
285
	}
286
	public function countAllCallsigns($limit = true,$stats_airline = '',$filter_name = '') {
287
		global $globalStatsFilters;
288
		if ($filter_name == '') $filter_name = $this->filter_name;
289
		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";
290
		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";
291
		 try {
292
			$sth = $this->db->prepare($query);
293
			$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
294
		} catch(PDOException $e) {
295
			echo "error : ".$e->getMessage();
296
		}
297
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
298
		if (empty($all)) {
299
			$filters = array('airlines' => array($stats_airline));
300
			if ($filter_name != '') {
301
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
302
			}
303
			$Spotter = new Spotter($this->db);
304
			$all = $Spotter->countAllCallsigns($limit,0,'',$filters);
305
		}
306
		return $all;
307
	}
308
	public function countAllFlightOverCountries($limit = true, $stats_airline = '',$filter_name = '') {
309
		$Connection = new Connection();
310
		if ($filter_name == '') $filter_name = $this->filter_name;
311
		if ($Connection->tableExists('countries')) {
312
			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";
313
			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";
314
			 try {
315
				$sth = $this->db->prepare($query);
316
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
317
			} catch(PDOException $e) {
318
				echo "error : ".$e->getMessage();
319
			}
320
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
321
                /*
322
                if (empty($all)) {
323
	                $Spotter = new Spotter($this->db);
324
    		        $all = $Spotter->countAllFlightOverCountries($limit);
325
                }
326
                */
327
			return $all;
328
		} else {
329
			return array();
330
		}
331
	}
332
	public function countAllPilots($limit = true,$stats_airline = '',$filter_name = '') {
333
		global $globalStatsFilters;
334
		if ($filter_name == '') $filter_name = $this->filter_name;
335
		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";
336
		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";
337
                 try {
338
                        $sth = $this->db->prepare($query);
339
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
340
                } catch(PDOException $e) {
341
                        echo "error : ".$e->getMessage();
342
                }
343
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
344
                if (empty($all)) {
345
			$filters = array('airlines' => array($stats_airline));
346
			if ($filter_name != '') {
347
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
348
			}
349
            		$Spotter = new Spotter($this->db);
350
            		$all = $Spotter->countAllPilots($limit,0,'',$filters);
351
                }
352
                return $all;
353
	}
354
	public function countAllOwners($limit = true,$stats_airline = '', $filter_name = '') {
355
		global $globalStatsFilters;
356
		if ($filter_name == '') $filter_name = $this->filter_name;
357
		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";
358
		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";
359
                 try {
360
                        $sth = $this->db->prepare($query);
361
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
362
                } catch(PDOException $e) {
363
                        echo "error : ".$e->getMessage();
364
                }
365
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
366
                if (empty($all)) {
367
			$filters = array('airlines' => array($stats_airline));
368
			if ($filter_name != '') {
369
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
370
			}
371
            		$Spotter = new Spotter($this->db);
372
            		$all = $Spotter->countAllOwners($limit,0,'',$filters);
373
                }
374
                return $all;
375
	}
376
	public function countAllDepartureAirports($limit = true,$stats_airline = '',$filter_name = '') {
377
		global $globalStatsFilters;
378
		if ($filter_name == '') $filter_name = $this->filter_name;
379
		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";
380
		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";
381
                 try {
382
                        $sth = $this->db->prepare($query);
383
                        $sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
384
                } catch(PDOException $e) {
385
                        echo "error : ".$e->getMessage();
386
                }
387
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
388
                if (empty($all)) {
389
			$filters = array('airlines' => array($stats_airline));
390
            		if ($filter_name != '') {
391
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
392
			}
393
            		$Spotter = new Spotter($this->db);
394
            		$pall = $Spotter->countAllDepartureAirports($limit,0,'',$filters);
395
        		$dall = $Spotter->countAllDetectedDepartureAirports($limit,0,'',$filters);
396
        		$all = array();
397
        		foreach ($pall as $value) {
398
        			$icao = $value['airport_departure_icao'];
399
        			$all[$icao] = $value;
400
        		}
401
        		
402
        		foreach ($dall as $value) {
403
        			$icao = $value['airport_departure_icao'];
404
        			if (isset($all[$icao])) {
405
        				$all[$icao]['airport_departure_icao_count'] = $all[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
406
        			} else $all[$icao] = $value;
407
        		}
408
        		$count = array();
409
        		foreach ($all as $key => $row) {
410
        			$count[$key] = $row['airport_departure_icao_count'];
411
        		}
412
        		array_multisort($count,SORT_DESC,$all);
413
                }
414
                return $all;
415
	}
416
	public function countAllArrivalAirports($limit = true,$stats_airline = '',$filter_name = '') {
417
		global $globalStatsFilters;
418
		if ($filter_name == '') $filter_name = $this->filter_name;
419
		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";
420
		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";
421
		try {
422
			$sth = $this->db->prepare($query);
423
			$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
424
		} catch(PDOException $e) {
425
			echo "error : ".$e->getMessage();
426
		}
427
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
428
		if (empty($all)) {
429
			$filters = array('airlines' => array($stats_airline));
430
			if ($filter_name != '') {
431
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
432
			}
433
			$Spotter = new Spotter($this->db);
434
			$pall = $Spotter->countAllArrivalAirports($limit,0,'',false,$filters);
435
			$dall = $Spotter->countAllDetectedArrivalAirports($limit,0,'',false,$filters);
436
        		$all = array();
437
        		foreach ($pall as $value) {
438
        			$icao = $value['airport_arrival_icao'];
439
        			$all[$icao] = $value;
440
        		}
441
        		
442
        		foreach ($dall as $value) {
443
        			$icao = $value['airport_arrival_icao'];
444
        			if (isset($all[$icao])) {
445
        				$all[$icao]['airport_arrival_icao_count'] = $all[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
446
        			} else $all[$icao] = $value;
447
        		}
448
        		$count = array();
449
        		foreach ($all as $key => $row) {
450
        			$count[$key] = $row['airport_arrival_icao_count'];
451
        		}
452
        		array_multisort($count,SORT_DESC,$all);
453
                }
454
 
455
                return $all;
456
	}
457
	public function countAllMonthsLastYear($limit = true,$stats_airline = '',$filter_name = '') {
458
		global $globalDBdriver, $globalStatsFilters;
459
		if ($filter_name == '') $filter_name = $this->filter_name;
460
		if ($globalDBdriver == 'mysql') {
461
			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";
462
			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";
463
		} else {
464
			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";
465
			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";
466
		}
467
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
468
                 try {
469
                        $sth = $this->db->prepare($query);
470
                        $sth->execute($query_data);
471
                } catch(PDOException $e) {
472
                        echo "error : ".$e->getMessage();
473
                }
474
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
475
                if (empty($all)) {
476
			$filters = array('airlines' => array($stats_airline));
477
			if ($filter_name != '') {
478
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
479
			}
480
            		$Spotter = new Spotter($this->db);
481
            		$all = $Spotter->countAllMonthsLastYear($filters);
482
                }
483
                
484
                return $all;
485
	}
486
	
487
	public function countAllDatesLastMonth($stats_airline = '',$filter_name = '') {
488
		global $globalStatsFilters;
489
		if ($filter_name == '') $filter_name = $this->filter_name;
490
		$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";
491
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
492
                 try {
493
                        $sth = $this->db->prepare($query);
494
                        $sth->execute($query_data);
495
                } catch(PDOException $e) {
496
                        echo "error : ".$e->getMessage();
497
                }
498
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
499
                if (empty($all)) {
500
			$filters = array('airlines' => array($stats_airline));
501
			if ($filter_name != '') {
502
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
503
			}
504
            		$Spotter = new Spotter($this->db);
505
            		$all = $Spotter->countAllDatesLastMonth($filters);
506
                }
507
                return $all;
508
	}
509
	public function countAllDatesLast7Days($stats_airline = '',$filter_name = '') {
510
		global $globalDBdriver, $globalStatsFilters;
511
		if ($filter_name == '') $filter_name = $this->filter_name;
512
		if ($globalDBdriver == 'mysql') {
513
			$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";
514
		} else {
515
			$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";
516
		}
517
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
518
                 try {
519
                        $sth = $this->db->prepare($query);
520
                        $sth->execute($query_data);
521
                } catch(PDOException $e) {
522
                        echo "error : ".$e->getMessage();
523
                }
524
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
525
                if (empty($all)) {
526
			$filters = array('airlines' => array($stats_airline));
527
			if ($filter_name != '') {
528
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
529
			}
530
            		$Spotter = new Spotter($this->db);
531
            		$all = $Spotter->countAllDatesLast7Days($filters);
532
                }
533
                return $all;
534
	}
535
	public function countAllDates($stats_airline = '',$filter_name = '') {
536
		global $globalStatsFilters;
537
		if ($filter_name == '') $filter_name = $this->filter_name;
538
		$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";
539
		$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
540
                 try {
541
                        $sth = $this->db->prepare($query);
542
                        $sth->execute($query_data);
543
                } catch(PDOException $e) {
544
                        echo "error : ".$e->getMessage();
545
                }
546
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
547
                if (empty($all)) {
548
			$filters = array('airlines' => array($stats_airline));
549
			if ($filter_name != '') {
550
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
551
			}
552
            		$Spotter = new Spotter($this->db);
553
            		$all = $Spotter->countAllDates($filters);
554
                }
555
                return $all;
556
	}
557
	public function countAllDatesByAirlines($filter_name = '') {
558
		global $globalStatsFilters;
559
		if ($filter_name == '') $filter_name = $this->filter_name;
560
		$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";
561
		$query_data = array('filter_name' => $filter_name);
562
                 try {
563
                        $sth = $this->db->prepare($query);
564
                        $sth->execute($query_data);
565
                } catch(PDOException $e) {
566
                        echo "error : ".$e->getMessage();
567
                }
568
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
569
                if (empty($all)) {
570
            		$filters = array();
571
            		if ($filter_name != '') {
572
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
573
			}
574
            		$Spotter = new Spotter($this->db);
575
            		$all = $Spotter->countAllDatesByAirlines($filters);
576
                }
577
                return $all;
578
	}
579
	public function countAllMonths($stats_airline = '',$filter_name = '') {
580
		global $globalStatsFilters;
581
		if ($filter_name == '') $filter_name = $this->filter_name;
582
	    	$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";
583
                 try {
584
                        $sth = $this->db->prepare($query);
585
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
586
                } catch(PDOException $e) {
587
                        echo "error : ".$e->getMessage();
588
                }
589
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
590
                if (empty($all)) {
591
			$filters = array('airlines' => array($stats_airline));
592
			if ($filter_name != '') {
593
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
594
			}
595
            		$Spotter = new Spotter($this->db);
596
            		$all = $Spotter->countAllMonths($filters);
597
                }
598
                return $all;
599
	}
600
	public function countAllMilitaryMonths($filter_name = '') {
601
		global $globalStatsFilters;
602
		if ($filter_name == '') $filter_name = $this->filter_name;
603
	    	$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";
604
                 try {
605
                        $sth = $this->db->prepare($query);
606
                        $sth->execute(array(':filter_name' => $filter_name));
607
                } catch(PDOException $e) {
608
                        echo "error : ".$e->getMessage();
609
                }
610
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
611
                if (empty($all)) {
612
            		$filters = array();
613
            		if ($filter_name != '') {
614
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
615
			}
616
            		$Spotter = new Spotter($this->db);
617
            		$all = $Spotter->countAllMilitaryMonths($filters);
618
                }
619
                return $all;
620
	}
621
	public function countAllHours($orderby = 'hour',$limit = true,$stats_airline = '',$filter_name = '') {
622
		global $globalTimezone, $globalDBdriver, $globalStatsFilters;
623
		if ($filter_name == '') $filter_name = $this->filter_name;
624
		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";
625
		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";
626
		if ($orderby == 'hour') {
627
			/*
628
			if ($globalDBdriver == 'mysql') {
629
				$query .= " ORDER BY flight_date ASC";
630
			} else {
631
			*/
632
			$query .= " ORDER BY CAST(flight_date AS integer) ASC";
633
		}
634
		if ($orderby == 'count') $query .= " ORDER BY hour_count DESC";
635
                 try {
636
                        $sth = $this->db->prepare($query);
637
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
638
                } catch(PDOException $e) {
639
                        echo "error : ".$e->getMessage();
640
                }
641
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
642
                if (empty($all)) {
643
			$filters = array('airlines' => array($stats_airline));
644
			if ($filter_name != '') {
645
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
646
			}
647
            		$Spotter = new Spotter($this->db);
648
            		$all = $Spotter->countAllHours($orderby,$filters);
649
                }
650
                return $all;
651
	}
652
	
653
	public function countOverallFlights($stats_airline = '', $filter_name = '') {
654
		global $globalStatsFilters;
655
		if ($filter_name == '') $filter_name = $this->filter_name;
656
		$all = $this->getSumStats('flights_bymonth',date('Y'),$stats_airline,$filter_name);
657
		if (empty($all)) {
658
			$filters = array('airlines' => array($stats_airline));
659
			if ($filter_name != '') {
660
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
661
			}
662
			$Spotter = new Spotter($this->db);
663
			$all = $Spotter->countOverallFlights($filters);
664
		}
665
		return $all;
666
	}
667
	public function countOverallMilitaryFlights($filter_name = '') {
668
		global $globalStatsFilters;
669
		if ($filter_name == '') $filter_name = $this->filter_name;
670
		$all = $this->getSumStats('military_flights_bymonth',date('Y'),'',$filter_name);
671
		if (empty($all)) {
672
		        $filters = array();
673
            		if ($filter_name != '') {
674
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
675
			}
676
			$Spotter = new Spotter($this->db);
677
			$all = $Spotter->countOverallMilitaryFlights($filters);
678
		}
679
		return $all;
680
	}
681
	public function countOverallArrival($stats_airline = '',$filter_name = '') {
682
		global $globalStatsFilters;
683
		if ($filter_name == '') $filter_name = $this->filter_name;
684
		$all = $this->getSumStats('realarrivals_bymonth',date('Y'),$stats_airline,$filter_name);
685
		if (empty($all)) {
686
			$filters = array('airlines' => array($stats_airline));
687
			if ($filter_name != '') {
688
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
689
			}
690
			$Spotter = new Spotter($this->db);
691
			$all = $Spotter->countOverallArrival($filters);
692
		}
693
		return $all;
694
	}
695
	public function countOverallAircrafts($stats_airline = '',$filter_name = '') {
696
		global $globalStatsFilters;
697
		if ($filter_name == '') $filter_name = $this->filter_name;
698
		$all = $this->getSumStats('aircrafts_bymonth',date('Y'),$stats_airline,$filter_name);
699
		if (empty($all)) {
700
			$filters = array('airlines' => array($stats_airline));
701
			if ($filter_name != '') {
702
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
703
			}
704
			$Spotter = new Spotter($this->db);
705
			$all = $Spotter->countOverallAircrafts($filters);
706
		}
707
		return $all;
708
	}
709
	public function countOverallAirlines($filter_name = '') {
710
		global $globalStatsFilters;
711
		if ($filter_name == '') $filter_name = $this->filter_name;
712
		$query = "SELECT COUNT(*) AS nb_airline FROM stats_airline WHERE filter_name = :filter_name";
713
                 try {
714
                        $sth = $this->db->prepare($query);
715
                        $sth->execute(array(':filter_name' => $filter_name));
716
                } catch(PDOException $e) {
717
                        echo "error : ".$e->getMessage();
718
                }
719
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
720
                $all = $result[0]['nb_airline'];
721
		//$all = $this->getSumStats('airlines_bymonth',date('Y'));
722
		if (empty($all)) {
723
            		$filters = array();
724
            		if ($filter_name != '') {
725
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
726
			}
727
			$Spotter = new Spotter($this->db);
728
			$all = $Spotter->countOverallAirlines($filters);
729
		}
730
		return $all;
731
	}
732
	public function countOverallOwners($stats_airline = '',$filter_name = '') {
733
		global $globalStatsFilters;
734
		if ($filter_name == '') $filter_name = $this->filter_name;
735
		/*
736
		$query = "SELECT COUNT(*) AS nb_owner FROM stats_owner";
737
                 try {
738
                        $sth = $this->db->prepare($query);
739
                        $sth->execute();
740
                } catch(PDOException $e) {
741
                        echo "error : ".$e->getMessage();
742
                }
743
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
744
                $all = $result[0]['nb_owner'];
745
                */
746
		$all = $this->getSumStats('owners_bymonth',date('Y'),$stats_airline,$filter_name);
747
		if (empty($all)) {
748
			$filters = array('airlines' => array($stats_airline));
749
			if ($filter_name != '') {
750
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
751
			}
752
			$Spotter = new Spotter($this->db);
753
			$all = $Spotter->countOverallOwners($filters);
754
		}
755
		return $all;
756
	}
757
	public function countOverallPilots($stats_airline = '',$filter_name = '') {
758
		global $globalStatsFilters;
759
		if ($filter_name == '') $filter_name = $this->filter_name;
760
		$all = $this->getSumStats('pilots_bymonth',date('Y'),$stats_airline,$filter_name);
761
		if (empty($all)) {
762
			$filters = array('airlines' => array($stats_airline));
763
			if ($filter_name != '') {
764
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
765
			}
766
			$Spotter = new Spotter($this->db);
767
			$all = $Spotter->countOverallPilots($filters);
768
		}
769
		return $all;
770
	}
771
772
	public function getLast7DaysAirports($airport_icao = '', $stats_airline = '',$filter_name = '') {
773
		if ($filter_name == '') $filter_name = $this->filter_name;
774
		$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";
775
		$query_values = array(':airport_icao' => $airport_icao,':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
776
                 try {
777
                        $sth = $this->db->prepare($query);
778
                        $sth->execute($query_values);
779
                } catch(PDOException $e) {
780
                        echo "error : ".$e->getMessage();
781
                }
782
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
783
                return $all;
784
	}
785
	public function getStats($type,$stats_airline = '', $filter_name = '') {
786
		if ($filter_name == '') $filter_name = $this->filter_name;
787
                $query = "SELECT * FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY stats_date";
788
                $query_values = array(':type' => $type,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
789
                 try {
790
                        $sth = $this->db->prepare($query);
791
                        $sth->execute($query_values);
792
                } catch(PDOException $e) {
793
                        echo "error : ".$e->getMessage();
794
                }
795
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
796
                return $all;
797
        }
798
	public function getSumStats($type,$year,$stats_airline = '',$filter_name = '') {
799
		if ($filter_name == '') $filter_name = $this->filter_name;
800
    		global $globalArchiveMonths, $globalDBdriver;
801
    		if ($globalDBdriver == 'mysql') {
802
	                $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";
803
	        } else {
804
            		$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";
805
                }
806
                $query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
807
                 try {
808
                        $sth = $this->db->prepare($query);
809
                        $sth->execute($query_values);
810
                } catch(PDOException $e) {
811
                        echo "error : ".$e->getMessage();
812
                }
813
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
814
                return $all[0]['total'];
815
        }
816
	public function getStatsTotal($type, $stats_airline = '', $filter_name = '') {
817
    		global $globalArchiveMonths, $globalDBdriver;
818
		if ($filter_name == '') $filter_name = $this->filter_name;
819
    		if ($globalDBdriver == 'mysql') {
820
			$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";
821
		} else {
822
			$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";
823
                }
824
                $query_values = array(':type' => $type, ':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
825
                 try {
826
                        $sth = $this->db->prepare($query);
827
                        $sth->execute($query_values);
828
                } catch(PDOException $e) {
829
                        echo "error : ".$e->getMessage();
830
                }
831
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
832
                return $all[0]['total'];
833
        }
834
	public function getStatsAircraftTotal($stats_airline = '', $filter_name = '') {
835
    		global $globalArchiveMonths, $globalDBdriver;
836
		if ($filter_name == '') $filter_name = $this->filter_name;
837
    		if ($globalDBdriver == 'mysql') {
838
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
839
                } else {
840
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
841
                }
842
                 try {
843
                        $sth = $this->db->prepare($query);
844
                        $sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
845
                } catch(PDOException $e) {
846
                        echo "error : ".$e->getMessage();
847
                }
848
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
849
                return $all[0]['total'];
850
        }
851
	public function getStatsAirlineTotal($filter_name = '') {
852
    		global $globalArchiveMonths, $globalDBdriver;
853
		if ($filter_name == '') $filter_name = $this->filter_name;
854
    		if ($globalDBdriver == 'mysql') {
855
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
856
                } else {
857
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
858
                }
859
                 try {
860
                        $sth = $this->db->prepare($query);
861
                        $sth->execute(array(':filter_name' => $filter_name));
862
                } catch(PDOException $e) {
863
                        echo "error : ".$e->getMessage();
864
                }
865
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
866
                return $all[0]['total'];
867
        }
868
	public function getStatsOwnerTotal($filter_name = '') {
869
    		global $globalArchiveMonths, $globalDBdriver;
870
		if ($filter_name == '') $filter_name = $this->filter_name;
871
    		if ($globalDBdriver == 'mysql') {
872
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
873
		} else {
874
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
875
                }
876
                 try {
877
                        $sth = $this->db->prepare($query);
878
                        $sth->execute(array(':filter_name' => $filter_name));
879
                } catch(PDOException $e) {
880
                        echo "error : ".$e->getMessage();
881
                }
882
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
883
                return $all[0]['total'];
884
        }
885
	public function getStatsPilotTotal($filter_name = '') {
886
    		global $globalArchiveMonths, $globalDBdriver;
887
		if ($filter_name == '') $filter_name = $this->filter_name;
888
    		if ($globalDBdriver == 'mysql') {
889
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
890
            	} else {
891
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
892
            	}
893
                 try {
894
                        $sth = $this->db->prepare($query);
895
                        $sth->execute(array(':filter_name' => $filter_name));
896
                } catch(PDOException $e) {
897
                        echo "error : ".$e->getMessage();
898
                }
899
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
900
                return $all[0]['total'];
901
        }
902
903
	public function addStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
904
		global $globalDBdriver;
905
		if ($filter_name == '') $filter_name = $this->filter_name;
906
		if ($globalDBdriver == 'mysql') {
907
			$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";
908
                } else {
909
			$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);"; 
910
		}
911
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
912
                 try {
913
                        $sth = $this->db->prepare($query);
914
                        $sth->execute($query_values);
915
                } catch(PDOException $e) {
916
                        return "error : ".$e->getMessage();
917
                }
918
        }
919
	public function updateStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
920
		global $globalDBdriver;
921
		if ($filter_name == '') $filter_name = $this->filter_name;
922
		if ($globalDBdriver == 'mysql') {
923
			$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";
924
		} else {
925
            		//$query = "INSERT INTO stats (stats_type,cnt,stats_date) VALUES (:type,:cnt,:stats_date) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
926
			$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);"; 
927
                }
928
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
929
                 try {
930
                        $sth = $this->db->prepare($query);
931
                        $sth->execute($query_values);
932
                } catch(PDOException $e) {
933
                        return "error : ".$e->getMessage();
934
                }
935
        }
936
	public function getStatsSource($date,$stats_type = '') {
937
		if ($stats_type == '') {
938
			$query = "SELECT * FROM stats_source WHERE stats_date = :date ORDER BY source_name";
939
			$query_values = array(':date' => $date);
940
		} else {
941
			$query = "SELECT * FROM stats_source WHERE stats_date = :date AND stats_type = :stats_type ORDER BY source_name";
942
			$query_values = array(':date' => $date,':stats_type' => $stats_type);
943
		}
944
                 try {
945
                        $sth = $this->db->prepare($query);
946
                        $sth->execute($query_values);
947
                } catch(PDOException $e) {
948
                        echo "error : ".$e->getMessage();
949
                }
950
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
951
                return $all;
952
        }
953
954
	public function addStatSource($data,$source_name,$stats_type,$date) {
955
		global $globalDBdriver;
956
		if ($globalDBdriver == 'mysql') {
957
			$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";
958
		} else {
959
			$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);"; 
960
                }
961
                $query_values = array(':data' => $data,':stats_date' => $date,':source_name' => $source_name,':stats_type' => $stats_type);
962
                 try {
963
                        $sth = $this->db->prepare($query);
964
                        $sth->execute($query_values);
965
                } catch(PDOException $e) {
966
                        return "error : ".$e->getMessage();
967
                }
968
        }
969
	public function addStatFlight($type,$date_name,$cnt,$stats_airline = '',$filter_name = '') {
970
                $query = "INSERT INTO stats_flight (stats_type,flight_date,cnt,stats_airline,filter_name) VALUES (:type,:flight_date,:cnt,:stats_airline,:filter_name)";
971
                $query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt, ':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
	public function addStatAircraftRegistration($registration,$cnt,$aircraft_icao = '',$airline_icao = '',$filter_name = '',$reset = false) {
980
		global $globalDBdriver;
981
		if ($globalDBdriver == 'mysql') {
982
			if ($reset) {
983
				$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";
984
			} else {
985
				$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";
986
			}
987
		} else {
988
			if ($reset) {
989
				$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);"; 
990
			} else {
991
				$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);"; 
992
			}
993
		}
994
                $query_values = array(':aircraft_icao' => $aircraft_icao,':registration' => $registration,':cnt' => $cnt,':stats_airline' => $airline_icao, ':filter_name' => $filter_name);
995
                 try {
996
                        $sth = $this->db->prepare($query);
997
                        $sth->execute($query_values);
998
                } catch(PDOException $e) {
999
                        return "error : ".$e->getMessage();
1000
                }
1001
        }
1002
	public function addStatCallsign($callsign_icao,$cnt,$airline_icao = '', $filter_name = '', $reset = false) {
1003
		global $globalDBdriver;
1004
		if ($globalDBdriver == 'mysql') {
1005
			if ($reset) {
1006
				$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";
1007
			} else {
1008
				$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";
1009
			}
1010
		} else {
1011
			if ($reset) {
1012
				$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);"; 
1013
			} else {
1014
				$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);"; 
1015
			}
1016
		}
1017
                $query_values = array(':callsign_icao' => $callsign_icao,':airline_icao' => $airline_icao,':cnt' => $cnt, ':filter_name' => $filter_name);
1018
                 try {
1019
                        $sth = $this->db->prepare($query);
1020
                        $sth->execute($query_values);
1021
                } catch(PDOException $e) {
1022
                        return "error : ".$e->getMessage();
1023
                }
1024
        }
1025
	public function addStatCountry($iso2,$iso3,$name,$cnt,$filter_name = '',$reset = false) {
1026
		global $globalDBdriver;
1027
		if ($globalDBdriver == 'mysql') {
1028
			if ($reset) {
1029
				$query = "INSERT INTO stats_country (iso2,iso3,name,cnt,filter_name) VALUES (:iso2,:iso3,:name,:cnt,:filter_name) ON DUPLICATE KEY UPDATE cnt = :cnt";
1030
			} else {
1031
				$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";
1032
			}
1033
		} else {
1034
			if ($reset) {
1035
				$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);"; 
1036
			} else {
1037
				$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);"; 
1038
			}
1039
		}
1040
                $query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt,':filter_name' => $filter_name);
1041
                 try {
1042
                        $sth = $this->db->prepare($query);
1043
                        $sth->execute($query_values);
1044
                } catch(PDOException $e) {
1045
                        return "error : ".$e->getMessage();
1046
                }
1047
        }
1048
	public function addStatAircraft($aircraft_icao,$cnt,$aircraft_name = '',$aircraft_manufacturer = '', $airline_icao = '', $filter_name = '', $reset = false) {
1049
		global $globalDBdriver;
1050
		if ($globalDBdriver == 'mysql') {
1051
			if ($reset) {
1052
				$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";
1053
			} else {
1054
				$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";
1055
			}
1056
		} else {
1057
			if ($reset) {
1058
				$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);"; 
1059
			} else {
1060
				$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);"; 
1061
			}
1062
		}
1063
                $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);
1064
                 try {
1065
                        $sth = $this->db->prepare($query);
1066
                        $sth->execute($query_values);
1067
                } catch(PDOException $e) {
1068
                        return "error : ".$e->getMessage();
1069
                }
1070
        }
1071
	public function addStatAirline($airline_icao,$cnt,$airline_name = '',$filter_name = '', $reset = false) {
1072
		global $globalDBdriver;
1073
		if ($globalDBdriver == 'mysql') {
1074
			if ($reset) {
1075
				$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";
1076
			} else {
1077
				$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";
1078
			}
1079
		} else {
1080
			if ($reset) {
1081
				$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);"; 
1082
			} else {
1083
				$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);"; 
1084
			}
1085
		}
1086
                $query_values = array(':airline_icao' => $airline_icao,':airline_name' => $airline_name,':cnt' => $cnt,':filter_name' => $filter_name);
1087
                 try {
1088
                        $sth = $this->db->prepare($query);
1089
                        $sth->execute($query_values);
1090
                } catch(PDOException $e) {
1091
                        return "error : ".$e->getMessage();
1092
                }
1093
        }
1094
	public function addStatOwner($owner_name,$cnt,$stats_airline = '', $filter_name = '', $reset = false) {
1095
		global $globalDBdriver;
1096
		if ($globalDBdriver == 'mysql') {
1097
			if ($reset) {
1098
				$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";
1099
			} else {
1100
				$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";
1101
			}
1102
		} else {
1103
			if ($reset) {
1104
				$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);"; 
1105
			} else {
1106
				$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);"; 
1107
			}
1108
		}
1109
                $query_values = array(':owner_name' => $owner_name,':cnt' => $cnt,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1110
                 try {
1111
                        $sth = $this->db->prepare($query);
1112
                        $sth->execute($query_values);
1113
                } catch(PDOException $e) {
1114
                        return "error : ".$e->getMessage();
1115
                }
1116
        }
1117
	public function addStatPilot($pilot_id,$cnt,$pilot_name,$stats_airline = '',$filter_name = '',$format_source = '',$reset = false) {
1118
		global $globalDBdriver;
1119
		if ($globalDBdriver == 'mysql') {
1120
			if ($reset) {
1121
				$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";
1122
			} else {
1123
				$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";
1124
			}
1125
		} else {
1126
			if ($reset) {
1127
				$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);"; 
1128
			} else {
1129
				$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);"; 
1130
			}
1131
		}
1132
                $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);
1133
                 try {
1134
                        $sth = $this->db->prepare($query);
1135
                        $sth->execute($query_values);
1136
                } catch(PDOException $e) {
1137
                        return "error : ".$e->getMessage();
1138
                }
1139
        }
1140
	public function addStatDepartureAirports($airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '',$reset = false) {
1141
		global $globalDBdriver;
1142
		if ($airport_icao != '') {
1143
			if ($globalDBdriver == 'mysql') {
1144
				if ($reset) {
1145
					$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";
1146
				} else {
1147
					$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";
1148
				}
1149
			} else {
1150
				if ($reset) {
1151
					$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);"; 
1152
				} else {
1153
					$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);"; 
1154
				}
1155
			}
1156
			$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);
1157
			try {
1158
				$sth = $this->db->prepare($query);
1159
				$sth->execute($query_values);
1160
			} catch(PDOException $e) {
1161
				return "error : ".$e->getMessage();
1162
			}
1163
                }
1164
        }
1165
	public function addStatDepartureAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '') {
1166
		global $globalDBdriver;
1167
		if ($airport_icao != '') {
1168
			if ($globalDBdriver == 'mysql') {
1169
				$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";
1170
			} else {
1171
				$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);"; 
1172
			}
1173
			$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);
1174
			 try {
1175
				$sth = $this->db->prepare($query);
1176
				$sth->execute($query_values);
1177
			} catch(PDOException $e) {
1178
				return "error : ".$e->getMessage();
1179
			}
1180
                }
1181
        }
1182
	public function addStatArrivalAirports($airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '',$reset = false) {
1183
		global $globalDBdriver;
1184
		if ($airport_icao != '') {
1185
			if ($globalDBdriver == 'mysql') {
1186
				if ($reset) {
1187
					$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";
1188
				} else {
1189
					$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";
1190
				}
1191
			} else {
1192
				if ($reset) {
1193
					$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);"; 
1194
				} else {
1195
					$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);"; 
1196
				}
1197
			}
1198
	                $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);
1199
			 try {
1200
                    		$sth = $this->db->prepare($query);
1201
	                        $sth->execute($query_values);
1202
    		        } catch(PDOException $e) {
1203
            		        return "error : ".$e->getMessage();
1204
	                }
1205
	        }
1206
        }
1207
	public function addStatArrivalAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '') {
1208
		global $globalDBdriver;
1209
		if ($airport_icao != '') {
1210
			if ($globalDBdriver == 'mysql') {
1211
				$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";
1212
			} else {
1213
				$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);"; 
1214
			}
1215
			$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);
1216
			try {
1217
				$sth = $this->db->prepare($query);
1218
				$sth->execute($query_values);
1219
			} catch(PDOException $e) {
1220
				return "error : ".$e->getMessage();
1221
			}
1222
                }
1223
        }
1224
1225
	public function deleteStat($id) {
1226
                $query = "DELETE FROM stats WHERE stats_id = :id";
1227
                $query_values = array(':id' => $id);
1228
                 try {
1229
                        $sth = $this->db->prepare($query);
1230
                        $sth->execute($query_values);
1231
                } catch(PDOException $e) {
1232
                        return "error : ".$e->getMessage();
1233
                }
1234
        }
1235
	public function deleteStatFlight($type) {
1236
                $query = "DELETE FROM stats_flight WHERE stats_type = :type";
1237
                $query_values = array(':type' => $type);
1238
                 try {
1239
                        $sth = $this->db->prepare($query);
1240
                        $sth->execute($query_values);
1241
                } catch(PDOException $e) {
1242
                        return "error : ".$e->getMessage();
1243
                }
1244
        }
1245
	public function deleteStatAirport($type) {
1246
                $query = "DELETE FROM stats_airport WHERE stats_type = :type";
1247
                $query_values = array(':type' => $type);
1248
                 try {
1249
                        $sth = $this->db->prepare($query);
1250
                        $sth->execute($query_values);
1251
                } catch(PDOException $e) {
1252
                        return "error : ".$e->getMessage();
1253
                }
1254
        }
1255
        
1256
        public function addOldStats() {
1257
    		global $globalDebug, $globalArchiveMonths, $globalArchive, $globalArchiveYear, $globalDBdriver, $globalStatsFilters,$globalDeleteLastYearStats,$globalStatsReset,$globalStatsResetYear;
1258
    		$Common = new Common();
1259
    		$Connection = new Connection();
1260
    		date_default_timezone_set('UTC');
1261
    		$last_update = $this->getLastStatsUpdate('last_update_stats');
1262
			if ($globalDebug) echo 'Update stats !'."\n";
1263
			if (isset($last_update[0]['value'])) {
1264
				$last_update_day = $last_update[0]['value'];
1265
			} else $last_update_day = '2012-12-12 12:12:12';
1266
			$reset = false;
1267
			if ($globalStatsResetYear) {
1268
				$reset = true;
1269
				$last_update_day = date('Y').'-01-01 00:00:00';
1270
			}
1271
			$Spotter = new Spotter($this->db);
1272
1273
			if ($globalDebug) echo 'Count all aircraft types...'."\n";
1274
			$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day);
1275
			foreach ($alldata as $number) {
1276
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'','','',$reset);
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Stats::addStatAircraft() has too many arguments starting with $reset.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1277
			}
1278
			if ($globalDebug) echo 'Count all airlines...'."\n";
1279
			$alldata = $Spotter->countAllAirlines(false,0,$last_update_day);
1280
			foreach ($alldata as $number) {
1281
				$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],'',$reset);
1282
			}
1283
			if ($globalDebug) echo 'Count all registrations...'."\n";
1284
			$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day);
1285
			foreach ($alldata as $number) {
1286
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'','',$reset);
1287
			}
1288
			if ($globalDebug) echo 'Count all callsigns...'."\n";
1289
			$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day);
1290
			foreach ($alldata as $number) {
1291
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
1292
			}
1293
			if ($globalDebug) echo 'Count all owners...'."\n";
1294
			$alldata = $Spotter->countAllOwners(false,0,$last_update_day);
1295
			foreach ($alldata as $number) {
1296
				$this->addStatOwner($number['owner_name'],$number['owner_count'],'','',$reset);
1297
			}
1298
			if ($globalDebug) echo 'Count all pilots...'."\n";
1299
			$alldata = $Spotter->countAllPilots(false,0,$last_update_day);
1300
			foreach ($alldata as $number) {
1301
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'','',$number['format_source'],$reset);
1302
			}
1303
			
1304
			if ($globalDebug) echo 'Count all departure airports...'."\n";
1305
			$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day);
1306
			if ($globalDebug) echo 'Count all detected departure airports...'."\n";
1307
        		$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day);
1308
			if ($globalDebug) echo 'Order departure airports...'."\n";
1309
	        	$alldata = array();
1310
	        	
1311
    			foreach ($pall as $value) {
1312
	        		$icao = $value['airport_departure_icao'];
1313
    				$alldata[$icao] = $value;
1314
	        	}
1315
	        	foreach ($dall as $value) {
1316
    				$icao = $value['airport_departure_icao'];
1317
        			if (isset($alldata[$icao])) {
1318
    					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1319
        			} else $alldata[$icao] = $value;
1320
			}
1321
    			$count = array();
1322
    			foreach ($alldata as $key => $row) {
1323
    				$count[$key] = $row['airport_departure_icao_count'];
1324
        		}
1325
			array_multisort($count,SORT_DESC,$alldata);
1326
			foreach ($alldata as $number) {
1327
				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);
1328
			}
1329
			if ($globalDebug) echo 'Count all arrival airports...'."\n";
1330
			$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day);
1331
			if ($globalDebug) echo 'Count all detected arrival airports...'."\n";
1332
        		$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day);
1333
			if ($globalDebug) echo 'Order arrival airports...'."\n";
1334
	        	$alldata = array();
1335
    			foreach ($pall as $value) {
1336
	        		$icao = $value['airport_arrival_icao'];
1337
    				$alldata[$icao] = $value;
1338
	        	}
1339
	        	foreach ($dall as $value) {
1340
    				$icao = $value['airport_arrival_icao'];
1341
        			if (isset($alldata[$icao])) {
1342
        				$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1343
	        		} else $alldata[$icao] = $value;
1344
    			}
1345
        		$count = array();
1346
        		foreach ($alldata as $key => $row) {
1347
        			$count[$key] = $row['airport_arrival_icao_count'];
1348
	        	}
1349
    			array_multisort($count,SORT_DESC,$alldata);
1350
                        foreach ($alldata as $number) {
1351
				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);
1352
			}
1353
			if ($Connection->tableExists('countries')) {
1354
				if ($globalDebug) echo 'Count all flights by countries...'."\n";
1355
				$SpotterArchive = new SpotterArchive();
1356
				$alldata = $SpotterArchive->countAllFlightOverCountries(false,0,$last_update_day);
1357
				foreach ($alldata as $number) {
1358
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],'',$reset);
1359
				}
1360
			}
1361
			
1362
1363
			// Add by month using getstat if month finish...
1364
1365
			//if (date('m',strtotime($last_update_day)) != date('m')) {
1366
			if ($globalDebug) echo 'Count all flights by months...'."\n";
1367
			$Spotter = new Spotter($this->db);
1368
			$alldata = $Spotter->countAllMonths();
1369
			$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...
1370
			foreach ($alldata as $number) {
1371
				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...
1372
				$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'])));
1373
			}
1374
			if ($globalDebug) echo 'Count all military flights by months...'."\n";
1375
			$alldata = $Spotter->countAllMilitaryMonths();
1376
			foreach ($alldata as $number) {
1377
				$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'])));
1378
			}
1379
			if ($globalDebug) echo 'Count all owners by months...'."\n";
1380
			$alldata = $Spotter->countAllMonthsOwners();
1381
			foreach ($alldata as $number) {
1382
				$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'])));
1383
			}
1384
			if ($globalDebug) echo 'Count all pilots by months...'."\n";
1385
			$alldata = $Spotter->countAllMonthsPilots();
1386
			foreach ($alldata as $number) {
1387
				$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'])));
1388
			}
1389
			if ($globalDebug) echo 'Count all airlines by months...'."\n";
1390
			$alldata = $Spotter->countAllMonthsAirlines();
1391
			foreach ($alldata as $number) {
1392
				$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'])));
1393
			}
1394
			if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
1395
			$alldata = $Spotter->countAllMonthsAircrafts();
1396
			foreach ($alldata as $number) {
1397
				$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'])));
1398
			}
1399
			if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
1400
			$alldata = $Spotter->countAllMonthsRealArrivals();
1401
			foreach ($alldata as $number) {
1402
				$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'])));
1403
			}
1404
			if ($globalDebug) echo 'Airports data...'."\n";
1405
			if ($globalDebug) echo '...Departure'."\n";
1406
			$this->deleteStatAirport('daily');
1407
//			$pall = $Spotter->getLast7DaysAirportsDeparture();
1408
  //      		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1409
			$pall = $Spotter->getLast7DaysAirportsDeparture();
1410
        		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1411
        		/*
1412
	        	$alldata = array();
1413
    			foreach ($pall as $value) {
1414
	        		$icao = $value['departure_airport_icao'];
1415
    				$alldata[$icao] = $value;
1416
	        	}
1417
	        	foreach ($dall as $value) {
1418
    				$icao = $value['departure_airport_icao'];
1419
    				$ddate = $value['date'];
1420
        			if (isset($alldata[$icao])) {
1421
        				$alldata[$icao]['departure_airport_count'] = $alldata[$icao]['departure_airport_count'] + $value['departure_airport_count'];
1422
	        		} else $alldata[$icao] = $value;
1423
    			}
1424
        		$count = array();
1425
        		foreach ($alldata as $key => $row) {
1426
        			$count[$key] = $row['departure_airport_count'];
1427
	        	}
1428
    			array_multisort($count,SORT_DESC,$alldata);
1429
    			*/
1430
    			foreach ($dall as $value) {
1431
    				$icao = $value['departure_airport_icao'];
1432
    				$ddate = $value['date'];
1433
    				$find = false;
1434
    				foreach ($pall as $pvalue) {
1435
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1436
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1437
    						$find = true;
1438
    						break;
1439
    					}
1440
    				}
1441
    				if ($find === false) {
1442
    					$pall[] = $value;
1443
    				}
1444
    			}
1445
    			$alldata = $pall;
1446
			foreach ($alldata as $number) {
1447
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count']);
1448
			}
1449
			echo '...Arrival'."\n";
1450
			$pall = $Spotter->getLast7DaysAirportsArrival();
1451
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrival();
1452
        		/*
1453
	        	$alldata = array();
1454
    			foreach ($pall as $value) {
1455
	        		$icao = $value['arrival_airport_icao'];
1456
    				$alldata[$icao] = $value;
1457
	        	}
1458
	        	foreach ($dall as $value) {
1459
    				$icao = $value['arrival_airport_icao'];
1460
        			if (isset($alldata[$icao])) {
1461
        				$alldata[$icao]['arrival_airport_icao_count'] = $alldata[$icao]['arrival_airport_count'] + $value['arrival_airport_count'];
1462
	        		} else $alldata[$icao] = $value;
1463
    			}
1464
        		$count = array();
1465
        		foreach ($alldata as $key => $row) {
1466
        			$count[$key] = $row['arrival_airport_count'];
1467
	        	}
1468
    			array_multisort($count,SORT_DESC,$alldata);
1469
    			*/
1470
1471
1472
    			foreach ($dall as $value) {
1473
    				$icao = $value['arrival_airport_icao'];
1474
    				$ddate = $value['date'];
1475
    				$find = false;
1476
    				foreach ($pall as $pvalue) {
1477
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1478
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1479
    						$find = true;
1480
    						break;
1481
    					}
1482
    				}
1483
    				if ($find === false) {
1484
    					$pall[] = $value;
1485
    				}
1486
    			}
1487
    			$alldata = $pall;
1488
			foreach ($alldata as $number) {
1489
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count']);
1490
			}
1491
1492
			echo 'Flights data...'."\n";
1493
			$this->deleteStatFlight('month');
1494
			echo '-> countAllDatesLastMonth...'."\n";
1495
			$alldata = $Spotter->countAllDatesLastMonth();
1496
			foreach ($alldata as $number) {
1497
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
1498
			}
1499
			echo '-> countAllDates...'."\n";
1500
			$previousdata = $this->countAllDates();
1501
			$previousdatabyairlines = $this->countAllDatesByAirlines();
1502
			$this->deleteStatFlight('date');
1503
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates());
1504
			$values = array();
1505
			foreach ($alldata as $cnt) {
1506
				$values[] = $cnt['date_count'];
1507
			}
1508
			array_multisort($values,SORT_DESC,$alldata);
1509
			array_splice($alldata,11);
1510
			foreach ($alldata as $number) {
1511
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
1512
			}
1513
			
1514
			$this->deleteStatFlight('hour');
1515
			echo '-> countAllHours...'."\n";
1516
			$alldata = $Spotter->countAllHours('hour');
1517
			foreach ($alldata as $number) {
1518
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
1519
			}
1520
1521
1522
1523
			// Count by airlines
1524
			echo '--- Stats by airlines ---'."\n";
1525
			if ($globalDebug) echo 'Count all aircraft types by airlines...'."\n";
1526
			$Spotter = new Spotter($this->db);
1527
			$alldata = $Spotter->countAllAircraftTypesByAirlines(false,0,$last_update_day);
1528
			foreach ($alldata as $number) {
1529
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],$number['airline_icao'],'',$reset);
1530
			}
1531
			if ($globalDebug) echo 'Count all aircraft registrations by airlines...'."\n";
1532
			$alldata = $Spotter->countAllAircraftRegistrationsByAirlines(false,0,$last_update_day);
1533
			foreach ($alldata as $number) {
1534
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],$number['airline_icao'],'',$reset);
1535
			}
1536
			if ($globalDebug) echo 'Count all callsigns by airlines...'."\n";
1537
			$alldata = $Spotter->countAllCallsignsByAirlines(false,0,$last_update_day);
1538
			foreach ($alldata as $number) {
1539
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
1540
			}
1541
			if ($globalDebug) echo 'Count all owners by airlines...'."\n";
1542
			$alldata = $Spotter->countAllOwnersByAirlines(false,0,$last_update_day);
1543
			foreach ($alldata as $number) {
1544
				$this->addStatOwner($number['owner_name'],$number['owner_count'],$number['airline_icao'],'',$reset);
1545
			}
1546
			if ($globalDebug) echo 'Count all pilots by airlines...'."\n";
1547
			$alldata = $Spotter->countAllPilotsByAirlines(false,0,$last_update_day);
1548
			foreach ($alldata as $number) {
1549
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],$number['airline_icao'],'',$number['format_source'],$reset);
1550
			}
1551
			if ($globalDebug) echo 'Count all departure airports by airlines...'."\n";
1552
			$pall = $Spotter->countAllDepartureAirportsByAirlines(false,0,$last_update_day);
1553
			if ($globalDebug) echo 'Count all detected departure airports by airlines...'."\n";
1554
       			$dall = $Spotter->countAllDetectedDepartureAirportsByAirlines(false,0,$last_update_day);
1555
			if ($globalDebug) echo 'Order detected departure airports by airlines...'."\n";
1556
	        	//$alldata = array();
1557
    			foreach ($dall as $value) {
1558
    				$icao = $value['airport_departure_icao'];
1559
    				$dicao = $value['airline_icao'];
1560
    				$find = false;
1561
    				foreach ($pall as $pvalue) {
1562
    					if ($pvalue['airport_departure_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1563
    						$pvalue['airport_departure_icao_count'] = $pvalue['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1564
    						$find = true;
1565
    						break;
1566
    					}
1567
    				}
1568
    				if ($find === false) {
1569
    					$pall[] = $value;
1570
    				}
1571
    			}
1572
    			$alldata = $pall;
1573
			foreach ($alldata as $number) {
1574
				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);
1575
			}
1576
			if ($globalDebug) echo 'Count all arrival airports by airlines...'."\n";
1577
			$pall = $Spotter->countAllArrivalAirportsByAirlines(false,0,$last_update_day);
1578
			if ($globalDebug) echo 'Count all detected arrival airports by airlines...'."\n";
1579
        		$dall = $Spotter->countAllDetectedArrivalAirportsByAirlines(false,0,$last_update_day);
1580
			if ($globalDebug) echo 'Order arrival airports by airlines...'."\n";
1581
	        	//$alldata = array();
1582
    			foreach ($dall as $value) {
1583
    				$icao = $value['airport_arrival_icao'];
1584
    				$dicao = $value['airline_icao'];
1585
    				$find = false;
1586
    				foreach ($pall as $pvalue) {
1587
    					if ($pvalue['airport_arrival_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1588
    						$pvalue['airport_arrival_icao_count'] = $pvalue['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1589
    						$find = true;
1590
    						break;
1591
    					}
1592
    				}
1593
    				if ($find === false) {
1594
    					$pall[] = $value;
1595
    				}
1596
    			}
1597
    			$alldata = $pall;
1598
                        foreach ($alldata as $number) {
1599
				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);
1600
			}
1601
			if ($globalDebug) echo 'Count all flights by months by airlines...'."\n";
1602
			$Spotter = new Spotter($this->db);
1603
			$alldata = $Spotter->countAllMonthsByAirlines();
1604
			$lastyear = false;
1605
			foreach ($alldata as $number) {
1606
				if ($number['year_name'] != date('Y')) $lastyear = true;
1607
				$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']);
1608
			}
1609
			if ($globalDebug) echo 'Count all owners by months by airlines...'."\n";
1610
			$alldata = $Spotter->countAllMonthsOwnersByAirlines();
1611
			foreach ($alldata as $number) {
1612
				$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']);
1613
			}
1614
			if ($globalDebug) echo 'Count all pilots by months by airlines...'."\n";
1615
			$alldata = $Spotter->countAllMonthsPilotsByAirlines();
1616
			foreach ($alldata as $number) {
1617
				$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']);
1618
			}
1619
			if ($globalDebug) echo 'Count all aircrafts by months by airlines...'."\n";
1620
			$alldata = $Spotter->countAllMonthsAircraftsByAirlines();
1621
			foreach ($alldata as $number) {
1622
				$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']);
1623
			}
1624
			if ($globalDebug) echo 'Count all real arrivals by months by airlines...'."\n";
1625
			$alldata = $Spotter->countAllMonthsRealArrivalsByAirlines();
1626
			foreach ($alldata as $number) {
1627
				$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']);
1628
			}
1629
			if ($globalDebug) echo '...Departure'."\n";
1630
			$pall = $Spotter->getLast7DaysAirportsDepartureByAirlines();
1631
        		$dall = $Spotter->getLast7DaysDetectedAirportsDepartureByAirlines();
1632
    			foreach ($dall as $value) {
1633
    				$icao = $value['departure_airport_icao'];
1634
    				$airline = $value['airline_icao'];
1635
    				$ddate = $value['date'];
1636
    				$find = false;
1637
    				foreach ($pall as $pvalue) {
1638
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] = $airline) {
1639
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1640
    						$find = true;
1641
    						break;
1642
    					}
1643
    				}
1644
    				if ($find === false) {
1645
    					$pall[] = $value;
1646
    				}
1647
    			}
1648
    			$alldata = $pall;
1649
			foreach ($alldata as $number) {
1650
				$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']);
1651
			}
1652
			if ($globalDebug) echo '...Arrival'."\n";
1653
			$pall = $Spotter->getLast7DaysAirportsArrivalByAirlines();
1654
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrivalByAirlines();
1655
    			foreach ($dall as $value) {
1656
    				$icao = $value['arrival_airport_icao'];
1657
    				$airline = $value['airline_icao'];
1658
    				$ddate = $value['date'];
1659
    				$find = false;
1660
    				foreach ($pall as $pvalue) {
1661
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] == $airline) {
1662
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1663
    						$find = true;
1664
    						break;
1665
    					}
1666
    				}
1667
    				if ($find === false) {
1668
    					$pall[] = $value;
1669
    				}
1670
    			}
1671
    			$alldata = $pall;
1672
			foreach ($alldata as $number) {
1673
				$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']);
1674
			}
1675
1676
			if ($globalDebug) echo 'Flights data...'."\n";
1677
			if ($globalDebug) echo '-> countAllDatesLastMonth...'."\n";
1678
			$alldata = $Spotter->countAllDatesLastMonthByAirlines();
1679
			foreach ($alldata as $number) {
1680
				$this->addStatFlight('month',$number['date_name'],$number['date_count'], $number['airline_icao']);
1681
			}
1682
			if ($globalDebug) echo '-> countAllDates...'."\n";
1683
			//$previousdata = $this->countAllDatesByAirlines();
1684
			$alldata = $Common->array_merge_noappend($previousdatabyairlines,$Spotter->countAllDatesByAirlines());
1685
			$values = array();
1686
			foreach ($alldata as $cnt) {
1687
				$values[] = $cnt['date_count'];
1688
			}
1689
			array_multisort($values,SORT_DESC,$alldata);
1690
			array_splice($alldata,11);
1691
			foreach ($alldata as $number) {
1692
				$this->addStatFlight('date',$number['date_name'],$number['date_count'],$number['airline_icao']);
1693
			}
1694
			
1695
			if ($globalDebug) echo '-> countAllHours...'."\n";
1696
			$alldata = $Spotter->countAllHoursByAirlines('hour');
1697
			foreach ($alldata as $number) {
1698
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],$number['airline_icao']);
1699
			}
1700
			
1701
1702
			// Stats by filters
1703
			if (!isset($globalStatsFilters) || $globalStatsFilters == '') $globalStatsFilters = array();
1704
			foreach ($globalStatsFilters as $name => $filter) {
1705
				//$filter_name = $filter['name'];
1706
				$filter_name = $name;
1707
1708
				$last_update = $this->getLastStatsUpdate('last_update_stats_'.$filter_name);
1709
				if (isset($last_update[0]['value'])) {
1710
					$last_update_day = $last_update[0]['value'];
1711
				} else $last_update_day = '2012-12-12 12:12:12';
1712
				$reset = false;
1713
1714
				// Count by filter
1715
				if ($globalDebug) echo '--- Stats for filter '.$filter_name.' ---'."\n";
1716
				$Spotter = new Spotter($this->db);
1717
				$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day,$filter);
1718
				foreach ($alldata as $number) {
1719
					$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'',$filter_name,$reset);
1720
				}
1721
				$alldata = $Spotter->countAllAirlines(false,0,$last_update_day,$filter);
1722
				foreach ($alldata as $number) {
1723
					$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],$filter_name,$reset);
1724
				}
1725
				$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day,$filter);
1726
				foreach ($alldata as $number) {
1727
					$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'',$filter_name,$reset);
1728
				}
1729
				$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day,$filter);
1730
				foreach ($alldata as $number) {
1731
					$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],'',$filter_name,$reset);
1732
				}
1733
				$alldata = $Spotter->countAllOwners(false,0,$last_update_day,$filter);
1734
				foreach ($alldata as $number) {
1735
					$this->addStatOwner($number['owner_name'],$number['owner_count'],'',$filter_name,$reset);
1736
				}
1737
				$alldata = $Spotter->countAllPilots(false,0,$last_update_day,$filter);
1738
				foreach ($alldata as $number) {
1739
					$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'',$filter_name,$number['format_source'],$reset);
1740
				}
1741
				$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day,$filter);
1742
	       			$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day,$filter);
1743
		        	$alldata = array();
1744
	    			foreach ($pall as $value) {
1745
		        		$icao = $value['airport_departure_icao'];
1746
    					$alldata[$icao] = $value;
1747
	    			}
1748
		        	foreach ($dall as $value) {
1749
	    				$icao = $value['airport_departure_icao'];
1750
        				if (isset($alldata[$icao])) {
1751
    						$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1752
        				} else $alldata[$icao] = $value;
1753
				}
1754
	    			$count = array();
1755
    				foreach ($alldata as $key => $row) {
1756
    					$count[$key] = $row['airport_departure_icao_count'];
1757
    				}
1758
				array_multisort($count,SORT_DESC,$alldata);
1759
				foreach ($alldata as $number) {
1760
    					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);
1761
				}
1762
				$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day,false,$filter);
1763
    				$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day,false,$filter);
1764
				$alldata = array();
1765
    				foreach ($pall as $value) {
1766
		        		$icao = $value['airport_arrival_icao'];
1767
    					$alldata[$icao] = $value;
1768
	    			}
1769
		        	foreach ($dall as $value) {
1770
	    				$icao = $value['airport_arrival_icao'];
1771
        				if (isset($alldata[$icao])) {
1772
        					$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1773
		        		} else $alldata[$icao] = $value;
1774
	    			}
1775
        			$count = array();
1776
        			foreach ($alldata as $key => $row) {
1777
    					$count[$key] = $row['airport_arrival_icao_count'];
1778
		        	}
1779
        			array_multisort($count,SORT_DESC,$alldata);
1780
				foreach ($alldata as $number) {
1781
					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);
1782
				}
1783
				$Spotter = new Spotter($this->db);
1784
				$alldata = $Spotter->countAllMonths($filter);
1785
				$lastyear = false;
1786
				foreach ($alldata as $number) {
1787
					if ($number['year_name'] != date('Y')) $lastyear = true;
1788
					$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);
1789
				}
1790
				$alldata = $Spotter->countAllMonthsOwners($filter);
1791
				foreach ($alldata as $number) {
1792
					$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);
1793
				}
1794
				$alldata = $Spotter->countAllMonthsPilots($filter);
1795
				foreach ($alldata as $number) {
1796
					$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);
1797
				}
1798
				$alldata = $Spotter->countAllMilitaryMonths($filter);
1799
				foreach ($alldata as $number) {
1800
					$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);
1801
				}
1802
				$alldata = $Spotter->countAllMonthsAircrafts($filter);
1803
				foreach ($alldata as $number) {
1804
					$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);
1805
				}
1806
				$alldata = $Spotter->countAllMonthsRealArrivals($filter);
1807
				foreach ($alldata as $number) {
1808
					$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);
1809
				}
1810
				echo '...Departure'."\n";
1811
				$pall = $Spotter->getLast7DaysAirportsDeparture('',$filter);
1812
        			$dall = $Spotter->getLast7DaysDetectedAirportsDeparture('',$filter);
1813
				foreach ($dall as $value) {
1814
    					$icao = $value['departure_airport_icao'];
1815
    					$ddate = $value['date'];
1816
    					$find = false;
1817
    					foreach ($pall as $pvalue) {
1818
    						if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1819
    							$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1820
	    						$find = true;
1821
    							break;
1822
    						}
1823
    					}
1824
    					if ($find === false) {
1825
    						$pall[] = $value;
1826
	    				}
1827
    				}
1828
	    			$alldata = $pall;
1829
				foreach ($alldata as $number) {
1830
					$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);
1831
				}
1832
				echo '...Arrival'."\n";
1833
				$pall = $Spotter->getLast7DaysAirportsArrival('',$filter);
1834
    				$dall = $Spotter->getLast7DaysDetectedAirportsArrival('',$filter);
1835
				foreach ($dall as $value) {
1836
					$icao = $value['arrival_airport_icao'];
1837
					$ddate = $value['date'];
1838
    					$find = false;
1839
					foreach ($pall as $pvalue) {
1840
    						if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1841
    							$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1842
    							$find = true;
1843
    							break;
1844
	    					}
1845
    					}
1846
    					if ($find === false) {
1847
    						$pall[] = $value;
1848
	    				}
1849
    				}
1850
    				$alldata = $pall;
1851
				foreach ($alldata as $number) {
1852
					$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);
1853
				}
1854
    
1855
				echo 'Flights data...'."\n";
1856
				echo '-> countAllDatesLastMonth...'."\n";
1857
				$alldata = $Spotter->countAllDatesLastMonth($filter);
1858
				foreach ($alldata as $number) {
1859
					$this->addStatFlight('month',$number['date_name'],$number['date_count'], '',$filter_name);
1860
				}
1861
				echo '-> countAllDates...'."\n";
1862
				$previousdata = $this->countAllDates('',$filter_name);
1863
				$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter));
1864
				$values = array();
1865
				foreach ($alldata as $cnt) {
1866
					$values[] = $cnt['date_count'];
1867
				}
1868
				array_multisort($values,SORT_DESC,$alldata);
1869
				array_splice($alldata,11);
1870
				foreach ($alldata as $number) {
1871
					$this->addStatFlight('date',$number['date_name'],$number['date_count'],'',$filter_name);
1872
				}
1873
				
1874
				echo '-> countAllHours...'."\n";
1875
				$alldata = $Spotter->countAllHours('hour',$filter);
1876
				foreach ($alldata as $number) {
1877
					$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],'',$filter_name);
1878
				}
1879
				echo 'Insert last stats update date...'."\n";
1880
				date_default_timezone_set('UTC');
1881
				$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y-m-d G:i:s'));
1882
				if (isset($filter['DeleteLastYearStats']) && $filter['DeleteLastYearStats'] == true) {
1883
					if (date('Y',strtotime($last_update_day)) != date('Y')) {
1884
						$this->deleteOldStats($filter_name);
1885
						$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y').'-01-01 00:00:00');
1886
					}
1887
				}
1888
1889
			}
1890
	
1891
1892
			// Last year stats
1893
			if ($lastyear) {
1894
				echo 'Data from last year...'."\n";
1895
				// SUM all previous month to put as year
1896
				$previous_year = date('Y');
1897
				$previous_year--;
1898
				$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1899
				$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1900
				$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1901
				$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1902
				$allairlines = $this->getAllAirlineNames();
1903
				foreach ($allairlines as $data) {
1904
					$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1905
					$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1906
					$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1907
					$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1908
				}
1909
				
1910
				if (isset($globalArchiveYear) && $globalArchiveYear) {
1911
					if ($globalArchive) {
1912
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
1913
						try {
1914
							$sth = $this->db->prepare($query);
1915
							$sth->execute();
1916
						} catch(PDOException $e) {
1917
							return "error : ".$e->getMessage().' - query : '.$query."\n";
1918
						}
1919
					}
1920
					echo 'Delete old data'."\n";
1921
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
1922
					try {
1923
						$sth = $this->db->prepare($query);
1924
						$sth->execute();
1925
					} catch(PDOException $e) {
1926
						return "error : ".$e->getMessage().' - query : '.$query."\n";
1927
					}
1928
				}
1929
				if (isset($globalDeleteLastYearStats) && $globalDeleteLastYearStats) {
1930
					$last_update = $this->getLastStatsUpdate('last_update_stats');
1931
					if (date('Y',strtotime($last_update[0]['value'])) != date('Y')) {
1932
						$this->deleteOldStats();
1933
						$this->addLastStatsUpdate('last_update_stats',date('Y').'-01-01 00:00:00');
1934
						$lastyearupdate = true;
1935
					}
1936
				}
1937
			}
1938
			if ($globalArchiveMonths > 0) {
1939
				if ($globalArchive) {
1940
					echo 'Archive old data...'."\n";
1941
					if ($globalDBdriver == 'mysql') {
1942
						//$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1943
						$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)
1944
							    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
1945
	    						     FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1946
					} else {
1947
						$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)
1948
							     SELECT 
1949
								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
1950
							    FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
1951
					}
1952
					try {
1953
						$sth = $this->db->prepare($query);
1954
						$sth->execute();
1955
					} catch(PDOException $e) {
1956
						return "error : ".$e->getMessage();
1957
					}
1958
				}
1959
				echo 'Deleting old data...'."\n";
1960
				//$query = 'DELETE FROM spotter_output WHERE spotter_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveMonths.' MONTH)';
1961
				if ($globalDBdriver == 'mysql') {
1962
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1963
				} else {
1964
					$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)";
1965
				}
1966
				try {
1967
					$sth = $this->db->prepare($query);
1968
					$sth->execute();
1969
				} catch(PDOException $e) {
1970
					return "error : ".$e->getMessage();
1971
				}
1972
			}
1973
			if (!isset($lastyearupdate)) {
1974
				echo 'Insert last stats update date...'."\n";
1975
				date_default_timezone_set('UTC');
1976
				$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
1977
			}
1978
		//}
1979
	}
1980
}
1981
1982
?>