Completed
Push — master ( 3b5bd1...2095b3 )
by Yannick
07:43
created

Stats::countAllAirlines()   F

Complexity

Conditions 12
Paths 294

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 28
nc 294
nop 4
dl 0
loc 33
rs 3.7956
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ($globalVATSIM) $forsource = 'vatsim';
0 ignored issues
show
Bug introduced by
The variable $globalVATSIM does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
280
			if ($globalIVAO) $forsource = 'ivao';
0 ignored issues
show
Bug introduced by
The variable $globalIVAO does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

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