Completed
Push — master ( 34a40a...b282d7 )
by Yannick
06:06
created

Stats::addStatArrivalAirports()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   Many Parameters   

Many Parameters

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

There are several approaches to avoid long parameter lists:

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