Completed
Push — master ( adb4a8...c8a161 )
by Yannick
28:16
created

Stats::addStatDepartureAirports()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   Many Parameters   

Many Parameters

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

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
* This class save stats older than a year and $globalArchiveMonths
4
*/
5
6
require_once(dirname(__FILE__).'/class.Spotter.php');
7
require_once(dirname(__FILE__).'/class.Accident.php');
8
require_once(dirname(__FILE__).'/class.SpotterArchive.php');
9
require_once(dirname(__FILE__).'/class.Common.php');
10
class Stats {
11
	public $db;
12
	public $filter_name = '';
13
	
14
	public function __construct($dbc = null) {
15
		global $globalFilterName;
16
		if (isset($globalFilterName)) $this->filter_name = $globalFilterName;
17
		$Connection = new Connection($dbc);
18
		$this->db = $Connection->db();
19
        }
20
              
21
	public function addLastStatsUpdate($type,$stats_date) {
22
                $query = "DELETE FROM config WHERE name = :type;
23
            		INSERT INTO config (name,value) VALUES (:type,:stats_date);";
24
                $query_values = array('type' => $type,':stats_date' => $stats_date);
25
                 try {
26
                        $sth = $this->db->prepare($query);
27
                        $sth->execute($query_values);
28
                } catch(PDOException $e) {
29
                        return "error : ".$e->getMessage();
30
                }
31
        }
32
33
	public function getLastStatsUpdate($type = 'last_update_stats') {
34
                $query = "SELECT value FROM config WHERE name = :type";
35
                 try {
36
                        $sth = $this->db->prepare($query);
37
                        $sth->execute(array(':type' => $type));
38
                } catch(PDOException $e) {
39
                        echo "error : ".$e->getMessage();
40
                }
41
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
42
                return $all;
43
        }
44
        public function deleteStats($filter_name = '') {
45
        	/*
46
        	$query = "DELETE FROM config WHERE name = 'last_update_stats'";
47
                 try {
48
                        $sth = $this->db->prepare($query);
49
                        $sth->execute();
50
                } catch(PDOException $e) {
51
                        return "error : ".$e->getMessage();
52
                }
53
                */
54
        	$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;";
55
                 try {
56
                        $sth = $this->db->prepare($query);
57
                        $sth->execute(array(':filter_name' => $filter_name));
58
                } catch(PDOException $e) {
59
                        return "error : ".$e->getMessage();
60
                }
61
        }
62
        public function deleteOldStats($filter_name = '') {
63
        	if ($filter_name == '') {
64
        		$query = "DELETE FROM config WHERE name = 'last_update_stats'";
65
        	} else {
66
        		$query = "DELETE FROM config WHERE name = 'last_update_stats_".$filter_name."'";
67
        	}
68
                 try {
69
                        $sth = $this->db->prepare($query);
70
                        $sth->execute();
71
                } catch(PDOException $e) {
72
                        return "error : ".$e->getMessage();
73
                }
74
                
75
        	$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;";
76
                 try {
77
                        $sth = $this->db->prepare($query);
78
                        $sth->execute(array(':filter_name' => $filter_name));
79
                } catch(PDOException $e) {
80
                        return "error : ".$e->getMessage();
81
                }
82
        }
83
	public function getAllAirlineNames($filter_name = '') {
84
		if ($filter_name == '') $filter_name = $this->filter_name;
85
		$query = "SELECT * FROM stats_airline WHERE filter_name = :filter_name ORDER BY airline_name ASC";
86
		 try {
87
			$sth = $this->db->prepare($query);
88
			$sth->execute(array(':filter_name' => $filter_name));
89
		} catch(PDOException $e) {
90
			echo "error : ".$e->getMessage();
91
		}
92
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
93
		if (empty($all)) {
94
			$filters = array();
95
			if ($filter_name != '') {
96
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
0 ignored issues
show
Bug introduced by
The variable $globalStatsFilters does not exist. Did you mean $filters?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
97
			}
98
			$Spotter = new Spotter($this->db);
99
			$all = $Spotter->getAllAirlineNames('',NULL,$filters);
100
		}
101
		return $all;
102
	}
103
	public function getAllAircraftTypes($stats_airline = '',$filter_name = '') {
104
		if ($filter_name == '') $filter_name = $this->filter_name;
105
		$query = "SELECT * FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name 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 getAllManufacturers($stats_airline = '',$filter_name = '') {
116
		if ($filter_name == '') $filter_name = $this->filter_name;
117
		$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";
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
	public function getAllAirportNames($stats_airline = '',$filter_name = '') {
128
		if ($filter_name == '') $filter_name = $this->filter_name;
129
		$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";
130
		try {
131
			$sth = $this->db->prepare($query);
132
			$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
133
		} catch(PDOException $e) {
134
			echo "error : ".$e->getMessage();
135
		}
136
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
137
		return $all;
138
	}
139
140
	public function getAllOwnerNames($stats_airline = '',$filter_name = '') {
141
		if ($filter_name == '') $filter_name = $this->filter_name;
142
		$query = "SELECT owner_name FROM stats_owner WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY owner_name ASC";
143
		try {
144
			$sth = $this->db->prepare($query);
145
			$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
146
		} catch(PDOException $e) {
147
			echo "error : ".$e->getMessage();
148
		}
149
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
150
		return $all;
151
	}
152
153
	public function getAllPilotNames($stats_airline = '',$filter_name = '') {
154
		if ($filter_name == '') $filter_name = $this->filter_name;
155
		$query = "SELECT pilot_id,pilot_name FROM stats_pilot WHERE stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY pilot_name ASC";
156
		try {
157
			$sth = $this->db->prepare($query);
158
			$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
159
		} catch(PDOException $e) {
160
			echo "error : ".$e->getMessage();
161
		}
162
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
163
		return $all;
164
	}
165
166
167
	public function countAllAircraftTypes($limit = true, $stats_airline = '', $filter_name = '',$year = '', $month = '') {
168
		global $globalStatsFilters;
169
		if ($filter_name == '') $filter_name = $this->filter_name;
170
		if (strpos($stats_airline,'alliance_') !== FALSE) {
171
			$Spotter = new Spotter($this->db);
172
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
173
			$alliance_airlines = array();
174
			foreach ($airlines as $airline) {
175
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
176
			}
177
			if ($year == '' && $month == '') {
178
				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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY aircraft_icao_count DESC LIMIT 10 OFFSET 0";
179
				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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY aircraft_icao_count DESC";
180
				try {
181
					$sth = $this->db->prepare($query);
182
					$sth->execute(array(':filter_name' => $filter_name));
183
				} catch(PDOException $e) {
184
					echo "error : ".$e->getMessage();
185
				}
186
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
187
			} else $all = array();
188
		} else {
189
			if ($year == '' && $month == '') {
190
				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";
191
				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";
192
				try {
193
					$sth = $this->db->prepare($query);
194
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
195
				} catch(PDOException $e) {
196
					echo "error : ".$e->getMessage();
197
				}
198
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
199
			} else $all = array();
200
		}
201
		if (empty($all)) {
202
			if (strpos($stats_airline,'alliance_') !== FALSE) {
203
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
204
			} else {
205
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
206
			}
207
			if ($filter_name != '') {
208
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
209
			}
210
			$Spotter = new Spotter($this->db);
211
			//$all = $Spotter->countAllAircraftTypes($limit,0,'',$filters,$year,$month);
212
			$all = $Spotter->countAllAircraftTypes($limit,0,'',$filters);
213
		}
214
		return $all;
215
	}
216
	public function countAllAirlineCountries($limit = true,$filter_name = '',$year = '',$month = '') {
217
		global $globalStatsFilters;
218
		if ($filter_name == '') $filter_name = $this->filter_name;
219
		if ($year == '' && $month == '') {
220
			if ($limit) $query = "SELECT airlines.country AS airline_country, SUM(stats_airline.cnt) as airline_country_count, countries.iso3 AS airline_country_iso3 FROM stats_airline,airlines,countries WHERE countries.name = airlines.country AND stats_airline.airline_icao=airlines.icao AND filter_name = :filter_name GROUP BY airline_country, countries.iso3 ORDER BY airline_country_count DESC LIMIT 10 OFFSET 0";
221
			else $query = "SELECT airlines.country AS airline_country, SUM(stats_airline.cnt) as airline_country_count, countries.iso3 AS airline_country_iso3 FROM stats_airline,airlines,countries WHERE countries.name = airlines.country AND stats_airline.airline_icao=airlines.icao AND filter_name = :filter_name GROUP BY airline_country, countries.iso3 ORDER BY airline_country_count DESC";
222
			try {
223
				$sth = $this->db->prepare($query);
224
				$sth->execute(array(':filter_name' => $filter_name));
225
			} catch(PDOException $e) {
226
				echo "error : ".$e->getMessage();
227
			}
228
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
229
		} else $all = array();
230
                if (empty($all)) {
231
            		$Spotter = new Spotter($this->db);
232
            		$filters = array();
0 ignored issues
show
Unused Code introduced by
$filters 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...
233
            		$filters = array('year' => $year,'month' => $month);
234
            		if ($filter_name != '') {
235
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
236
			}
237
            		//$all = $Spotter->countAllAirlineCountries($limit,$filters,$year,$month);
238
            		$all = $Spotter->countAllAirlineCountries($limit,$filters);
239
                }
240
                return $all;
241
	}
242
	public function countAllAircraftManufacturers($limit = true,$stats_airline = '', $filter_name = '',$year = '', $month = '') {
243
		global $globalStatsFilters;
244
		if ($filter_name == '') $filter_name = $this->filter_name;
245
		if (strpos($stats_airline,'alliance_') !== FALSE) {
246
			$Spotter = new Spotter($this->db);
247
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
248
			$alliance_airlines = array();
249
			foreach ($airlines as $airline) {
250
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
251
			}
252
			if ($year == '' && $month == '') {
253
				if ($limit) $query = "SELECT aircraft_manufacturer, SUM(stats_aircraft.cnt) as aircraft_manufacturer_count FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY aircraft_manufacturer ORDER BY aircraft_manufacturer_count DESC LIMIT 10 OFFSET 0";
254
				else $query = "SELECT aircraft_manufacturer, SUM(stats_aircraft.cnt) as aircraft_manufacturer_count FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY aircraft_manufacturer ORDER BY aircraft_manufacturer_count DESC";
255
				try {
256
					$sth = $this->db->prepare($query);
257
					$sth->execute(array(':filter_name' => $filter_name));
258
				} catch(PDOException $e) {
259
					echo "error : ".$e->getMessage();
260
				}
261
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
262
			} else $all = array();
263
		} else {
264
			if ($year == '' && $month == '') {
265
				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";
266
				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";
267
				try {
268
					$sth = $this->db->prepare($query);
269
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
270
				} catch(PDOException $e) {
271
					echo "error : ".$e->getMessage();
272
				}
273
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
274
			} else $all = array();
275
		}
276
		if (empty($all)) {
277
			if (strpos($stats_airline,'alliance_') !== FALSE) {
278
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
279
			} else {
280
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
281
			}
282
			if ($filter_name != '') {
283
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
284
			}
285
			$Spotter = new Spotter($this->db);
286
			//$all = $Spotter->countAllAircraftManufacturers($filters,$year,$month);
287
			$all = $Spotter->countAllAircraftManufacturers($filters);
288
		}
289
		return $all;
290
	}
291
292
	public function countAllArrivalCountries($limit = true, $stats_airline = '', $filter_name = '',$year = '', $month = '') {
293
		global $globalStatsFilters;
294
		if ($filter_name == '') $filter_name = $this->filter_name;
295
		if (strpos($stats_airline,'alliance_') !== FALSE) {
296
			$Spotter = new Spotter($this->db);
297
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
298
			$alliance_airlines = array();
299
			foreach ($airlines as $airline) {
300
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
301
			}
302
			if ($year == '' && $month == '') {
303
				if ($limit) $query = "SELECT airport_country AS airport_arrival_country, SUM(arrival) as airport_arrival_country_count, countries.iso3 AS airport_arrival_country_iso3 FROM stats_airport, countries WHERE countries.name = stats_airport.airport_country AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY airport_arrival_country, countries.iso3 ORDER BY airport_arrival_country_count DESC LIMIT 10 OFFSET 0";
304
				else $query = "SELECT airport_country AS airport_arrival_country, SUM(arrival) as airport_arrival_country_count, countries.iso3 AS airport_arrival_country_iso3 FROM stats_airport, countries WHERE countries.name = stats_aiport.airport_country AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY airport_arrival_country, countries.iso3 ORDER BY airport_arrival_country_count DESC";
305
				try {
306
					$sth = $this->db->prepare($query);
307
					$sth->execute(array(':filter_name' => $filter_name));
308
				} catch(PDOException $e) {
309
					echo "error : ".$e->getMessage();
310
				}
311
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
312
			} else $all = array();
313
		} else {
314
			if ($year == '' && $month == '') {
315
				if ($limit) $query = "SELECT airport_country AS airport_arrival_country, SUM(arrival) as airport_arrival_country_count, countries.iso3 AS airport_arrival_country_iso3 FROM stats_airport, countries WHERE countries.name = stats_airport.airport_country AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_arrival_country, countries.iso3 ORDER BY airport_arrival_country_count DESC LIMIT 10 OFFSET 0";
316
				else $query = "SELECT airport_country AS airport_arrival_country, SUM(arrival) as airport_arrival_country_count, countries.iso3 AS airport_arrival_country_iso3 FROM stats_airport, countries WHERE countries.name = stats_aiport.airport_country AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_arrival_country, countries.iso3 ORDER BY airport_arrival_country_count DESC";
317
				try {
318
					$sth = $this->db->prepare($query);
319
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
320
				} catch(PDOException $e) {
321
					echo "error : ".$e->getMessage();
322
				}
323
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
324
			} else $all = array();
325
		}
326
		if (empty($all)) {
327
			if (strpos($stats_airline,'alliance_') !== FALSE) {
328
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
329
			} else {
330
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
331
			}
332
			if ($filter_name != '') {
333
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
334
			}
335
			$Spotter = new Spotter($this->db);
336
			//$all = $Spotter->countAllArrivalCountries($limit,$filters,$year,$month);
337
			$all = $Spotter->countAllArrivalCountries($limit,$filters);
338
		}
339
		return $all;
340
	}
341
	public function countAllDepartureCountries($limit = true, $stats_airline = '', $filter_name = '', $year = '', $month = '') {
342
		global $globalStatsFilters;
343
		if ($filter_name == '') $filter_name = $this->filter_name;
344
		if (strpos($stats_airline,'alliance_') !== FALSE) {
345
			$Spotter = new Spotter($this->db);
346
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
347
			$alliance_airlines = array();
348
			foreach ($airlines as $airline) {
349
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
350
			}
351
			if ($limit) $query = "SELECT airport_country AS airport_departure_country, SUM(departure) as airport_departure_country_count, countries.iso3 as airport_departure_country_iso3 FROM stats_airport, countries WHERE countries.name = stats_airport.airport_country AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY airport_departure_country, countries.iso3 ORDER BY airport_departure_country_count DESC LIMIT 10 OFFSET 0";
352
			else $query = "SELECT airport_country AS airport_departure_country, SUM(departure) as airport_departure_country_count, countries.iso3 as airport_departure_country_iso3 FROM stats_airport, countries WHERE countries.iso3 = stats_airport.airport_country AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY airport_departure_country, countries.iso3 ORDER BY airport_departure_country_count DESC";
353
			$query_values = array(':filter_name' => $filter_name);
354
		} else {
355
			if ($limit) $query = "SELECT airport_country AS airport_departure_country, SUM(departure) as airport_departure_country_count, countries.iso3 as airport_departure_country_iso3 FROM stats_airport, countries WHERE countries.name = stats_airport.airport_country AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_departure_country, countries.iso3 ORDER BY airport_departure_country_count DESC LIMIT 10 OFFSET 0";
356
			else $query = "SELECT airport_country AS airport_departure_country, SUM(departure) as airport_departure_country_count, countries.iso3 as airport_departure_country_iso3 FROM stats_airport, countries WHERE countries.iso3 = stats_airport.airport_country AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name GROUP BY airport_departure_country, countries.iso3 ORDER BY airport_departure_country_count DESC";
357
			$query_values = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
358
		}
359
		try {
360
			$sth = $this->db->prepare($query);
361
			$sth->execute($query_values);
362
		} catch(PDOException $e) {
363
			echo "error : ".$e->getMessage();
364
		}
365
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
366
		if (empty($all)) {
367
			if (strpos($stats_airline,'alliance_') !== FALSE) {
368
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
369
			} else {
370
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
371
			}
372
			if ($filter_name != '') {
373
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
374
			}
375
			$Spotter = new Spotter($this->db);
376
			//$all = $Spotter->countAllDepartureCountries($filters,$year,$month);
377
			$all = $Spotter->countAllDepartureCountries($filters);
378
		}
379
		return $all;
380
	}
381
382
	public function countAllAirlines($limit = true,$filter_name = '',$year = '',$month = '') {
383
		global $globalStatsFilters, $globalVATSIM, $globalIVAO;
384
		if ($filter_name == '') $filter_name = $this->filter_name;
385
		if ($year == '' && $month == '') {
386
			if ($globalVATSIM) $forsource = 'vatsim';
387
			if ($globalIVAO) $forsource = 'ivao';
388
			if (isset($forsource)) {
389
				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";
390
				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";
391
				$query_values = array(':filter_name' => $filter_name,':forsource' => $forsource);
392
			} else {
393
				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";
394
				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";
395
				$query_values = array(':filter_name' => $filter_name);
396
			}
397
			try {
398
				$sth = $this->db->prepare($query);
399
				$sth->execute($query_values);
400
			} catch(PDOException $e) {
401
				echo "error : ".$e->getMessage();
402
			}
403
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
404
		} else $all = array();
405
                if (empty($all)) {
406
	                $Spotter = new Spotter($this->db);
407
            		$filters = array();
0 ignored issues
show
Unused Code introduced by
$filters 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...
408
			$filters = array('year' => $year,'month' => $month);
409
            		if ($filter_name != '') {
410
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
411
			}
412
			//$all = $Spotter->countAllAirlines($limit,0,'',$filters,$year,$month);
413
    		        $all = $Spotter->countAllAirlines($limit,0,'',$filters);
414
                }
415
                return $all;
416
	}
417
	public function countAllAircraftRegistrations($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
418
		global $globalStatsFilters;
419
		if ($filter_name == '') $filter_name = $this->filter_name;
420
		if (strpos($stats_airline,'alliance_') !== FALSE) {
421
			$Spotter = new Spotter($this->db);
422
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
423
			$alliance_airlines = array();
424
			foreach ($airlines as $airline) {
425
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
426
			}
427
			if ($year == '' && $month == '') {
428
				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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY aircraft_registration_count DESC LIMIT 10 OFFSET 0";
429
				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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY aircraft_registration_count DESC";
430
				try {
431
					$sth = $this->db->prepare($query);
432
					$sth->execute(array(':filter_name' => $filter_name));
433
				} catch(PDOException $e) {
434
					echo "error : ".$e->getMessage();
435
				}
436
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
437
			} else $all = array();
438
		} else {
439
			if ($year == '' && $month == '') {
440
				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";
441
				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";
442
				try {
443
					$sth = $this->db->prepare($query);
444
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
445
				} catch(PDOException $e) {
446
					echo "error : ".$e->getMessage();
447
				}
448
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
449
			} else $all = array();
450
		}
451
		if (empty($all)) {
452
			if (strpos($stats_airline,'alliance_') !== FALSE) {
453
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
454
			} else {
455
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
456
			}
457
			if ($filter_name != '') {
458
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
459
			}
460
			$Spotter = new Spotter($this->db);
461
			//$all = $Spotter->countAllAircraftRegistrations($limit,0,'',$filters,$year,$month);
462
			$all = $Spotter->countAllAircraftRegistrations($limit,0,'',$filters);
463
		}
464
		return $all;
465
	}
466
	public function countAllCallsigns($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
467
		global $globalStatsFilters;
468
		if ($filter_name == '') $filter_name = $this->filter_name;
469
		if (strpos($stats_airline,'alliance_') !== FALSE) {
470
			$Spotter = new Spotter($this->db);
471
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
472
			$alliance_airlines = array();
473
			foreach ($airlines as $airline) {
474
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
475
			}
476
			if ($year == '' && $month == '') {
477
				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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY callsign_icao_count DESC LIMIT 10 OFFSET 0";
478
				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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY callsign_icao_count DESC";
479
				 try {
480
					$sth = $this->db->prepare($query);
481
					$sth->execute(array(':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
		} else {
488
			if ($year == '' && $month == '') {
489
				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";
490
				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";
491
				 try {
492
					$sth = $this->db->prepare($query);
493
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
494
				} catch(PDOException $e) {
495
					echo "error : ".$e->getMessage();
496
				}
497
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
498
			} else $all = array();
499
		}
500
		if (empty($all)) {
501
			if (strpos($stats_airline,'alliance_') !== FALSE) {
502
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
503
			} else {
504
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
505
			}
506
			if ($filter_name != '') {
507
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
508
			}
509
			$Spotter = new Spotter($this->db);
510
			//$all = $Spotter->countAllCallsigns($limit,0,'',$filters,$year,$month);
511
			$all = $Spotter->countAllCallsigns($limit,0,'',$filters);
512
		}
513
		return $all;
514
	}
515
	public function countAllFlightOverCountries($limit = true, $stats_airline = '',$filter_name = '',$year = '',$month = '') {
516
		$Connection = new Connection();
517
		if ($filter_name == '') $filter_name = $this->filter_name;
518
		if ($Connection->tableExists('countries')) {
519
			if (strpos($stats_airline,'alliance_') !== FALSE) {
520
				$Spotter = new Spotter($this->db);
521
				$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
522
				if ($year == '' && $month == '') {
523
					$alliance_airlines = array();
524
					foreach ($airlines as $airline) {
525
						$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
526
					}
527
					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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY flight_count DESC LIMIT 20 OFFSET 0";
528
					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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY flight_count DESC";
529
					 try {
530
						$sth = $this->db->prepare($query);
531
						$sth->execute(array(':filter_name' => $filter_name));
532
					} catch(PDOException $e) {
533
						echo "error : ".$e->getMessage();
534
					}
535
					$all = $sth->fetchAll(PDO::FETCH_ASSOC);
536
					return $all;
537
				} else return array();
538
			} else {
539
				if ($year == '' && $month == '') {
540
					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";
541
					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";
542
					 try {
543
						$sth = $this->db->prepare($query);
544
						$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
545
					} catch(PDOException $e) {
546
						echo "error : ".$e->getMessage();
547
					}
548
					$all = $sth->fetchAll(PDO::FETCH_ASSOC);
549
					return $all;
550
				} else return array();
551
			}
552
		} else {
553
			/*
554
			if (empty($all)) {
555
				$Spotter = new Spotter($this->db);
556
				$all = $Spotter->countAllFlightOverCountries($limit);
557
			}
558
			*/
559
			return array();
560
		}
561
	}
562
	public function countAllPilots($limit = true,$stats_airline = '',$filter_name = '', $year = '',$month = '') {
563
		global $globalStatsFilters;
564
		if ($filter_name == '') $filter_name = $this->filter_name;
565
		if ($year == '' && $month == '') {
566
			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";
567
			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";
568
			try {
569
				$sth = $this->db->prepare($query);
570
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
571
			} catch(PDOException $e) {
572
				echo "error : ".$e->getMessage();
573
			}
574
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
575
		} else $all = array();
576
		if (empty($all)) {
577
			$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
578
			if ($filter_name != '') {
579
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
580
			}
581
			$Spotter = new Spotter($this->db);
582
			//$all = $Spotter->countAllPilots($limit,0,'',$filters,$year,$month);
583
			$all = $Spotter->countAllPilots($limit,0,'',$filters);
584
		}
585
		return $all;
586
	}
587
588
	public function countAllOwners($limit = true,$stats_airline = '', $filter_name = '',$year = '',$month = '') {
589
		global $globalStatsFilters;
590
		if ($filter_name == '') $filter_name = $this->filter_name;
591
		if (strpos($stats_airline,'alliance_') !== FALSE) {
592
			$Spotter = new Spotter($this->db);
593
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
594
			if ($year == '' && $month == '') {
595
				$alliance_airlines = array();
596
				foreach ($airlines as $airline) {
597
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
598
				}
599
				if ($limit) $query = "SELECT owner_name, cnt AS owner_count FROM stats_owner WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY owner_count DESC LIMIT 10 OFFSET 0";
600
				else $query = "SELECT owner_name, cnt AS owner_count FROM stats_owner WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY owner_count DESC";
601
				try {
602
					$sth = $this->db->prepare($query);
603
					$sth->execute(array(':filter_name' => $filter_name));
604
				} catch(PDOException $e) {
605
					echo "error : ".$e->getMessage();
606
				}
607
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
608
			} else $all = array();
609
		} else {
610
			if ($year == '' && $month == '') {
611
				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";
612
				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";
613
				try {
614
					$sth = $this->db->prepare($query);
615
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
616
				} catch(PDOException $e) {
617
					echo "error : ".$e->getMessage();
618
				}
619
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
620
			} else $all = array();
621
		}
622
		if (empty($all)) {
623
			if (strpos($stats_airline,'alliance_') !== FALSE) {
624
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
625
			} else {
626
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
627
			}
628
			if ($filter_name != '') {
629
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
630
			}
631
			$Spotter = new Spotter($this->db);
632
			//$all = $Spotter->countAllOwners($limit,0,'',$filters,$year,$month);
633
			$all = $Spotter->countAllOwners($limit,0,'',$filters);
634
		}
635
		return $all;
636
	}
637
	public function countAllDepartureAirports($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
638
		global $globalStatsFilters;
639
		if ($filter_name == '') $filter_name = $this->filter_name;
640
		if (strpos($stats_airline,'alliance_') !== FALSE) {
641
			$Spotter = new Spotter($this->db);
642
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
643
			if ($year == '' && $month == '') {
644
				$alliance_airlines = array();
645
				foreach ($airlines as $airline) {
646
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
647
				}
648
				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, airport.latitude AS airport_departure_latitude, airport.longitude AS airport_departure_longitude FROM stats_airport,airport WHERE airport.icao = stats_airport.airport_icao AND departure > 0 AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY airport_departure_icao_count DESC LIMIT 10 OFFSET 0";
649
				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, airport.latitude AS airport_departure_latitude, airport.longitude AS airport_departure_longitude FROM stats_airport,airport WHERE airport.icao = stats_airport.airport_icao AND departure > 0 AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY airport_departure_icao_count DESC";
650
				try {
651
					$sth = $this->db->prepare($query);
652
					$sth->execute(array(':filter_name' => $filter_name));
653
				} catch(PDOException $e) {
654
					echo "error : ".$e->getMessage();
655
				}
656
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
657
			} else $all = array();
658
		} else {
659
			if ($year == '' && $month == '') {
660
				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, airport.latitude AS airport_departure_latitude, airport.longitude AS airport_departure_longitude FROM stats_airport,airport WHERE airport.icao = stats_airport.airport_icao AND 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";
661
				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, airport.latitude AS airport_departure_latitude, airport.longitude AS airport_departure_longitude FROM stats_airport,airport WHERE airport.icao = stats_airport.airport_icao AND departure > 0 AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY airport_departure_icao_count DESC";
662
				try {
663
					$sth = $this->db->prepare($query);
664
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
665
				} catch(PDOException $e) {
666
					echo "error : ".$e->getMessage();
667
				}
668
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
669
			} else $all = array();
670
		}
671
		if (empty($all)) {
672
			if (strpos($stats_airline,'alliance_') !== FALSE) {
673
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
674
			} else {
675
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
676
			}
677
			if ($filter_name != '') {
678
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
679
			}
680
			$Spotter = new Spotter($this->db);
681
//            		$pall = $Spotter->countAllDepartureAirports($limit,0,'',$filters,$year,$month);
682
  //      		$dall = $Spotter->countAllDetectedDepartureAirports($limit,0,'',$filters,$year,$month);
683
			$pall = $Spotter->countAllDepartureAirports($limit,0,'',$filters);
684
			$dall = $Spotter->countAllDetectedDepartureAirports($limit,0,'',$filters);
685
			$all = array();
686
			foreach ($pall as $value) {
687
				$icao = $value['airport_departure_icao'];
688
				$all[$icao] = $value;
689
			}
690
			foreach ($dall as $value) {
691
				$icao = $value['airport_departure_icao'];
692
				if (isset($all[$icao])) {
693
					$all[$icao]['airport_departure_icao_count'] = $all[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
694
				} else $all[$icao] = $value;
695
			}
696
			$count = array();
697
			foreach ($all as $key => $row) {
698
				$count[$key] = $row['airport_departure_icao_count'];
699
			}
700
			array_multisort($count,SORT_DESC,$all);
701
		}
702
		return $all;
703
	}
704
	public function countAllArrivalAirports($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
705
		global $globalStatsFilters;
706
		if ($filter_name == '') $filter_name = $this->filter_name;
707
		if (strpos($stats_airline,'alliance_') !== FALSE) {
708
			$Spotter = new Spotter($this->db);
709
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
710
			if ($year == '' && $month == '') {
711
				$alliance_airlines = array();
712
				foreach ($airlines as $airline) {
713
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
714
				}
715
				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, airport.latitude AS airport_arrival_latitude, airport.longitude AS airport_arrival_longitude FROM stats_airport, airport WHERE airport.icao = stats_airport.airport_icao AND arrival > 0 AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY airport_arrival_icao_count DESC LIMIT 10 OFFSET 0";
716
				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, airport.latitude AS airport_arrival_latitude, airport.longitude AS airport_arrival_longitude FROM stats_airport, airport WHERE airport.icao = stats_airport.airport_icao AND arrival > 0 AND stats_type = 'yearly' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY airport_arrival_icao_count DESC";
717
				try {
718
					$sth = $this->db->prepare($query);
719
					$sth->execute(array(':filter_name' => $filter_name));
720
				} catch(PDOException $e) {
721
					echo "error : ".$e->getMessage();
722
				}
723
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
724
			} else $all = array();
725
		} else {
726
			if ($year == '' && $month == '') {
727
				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, airport.latitude AS airport_arrival_latitude, airport.longitude AS airport_arrival_longitude FROM stats_airport, airport WHERE airport.icao = stats_airport.airport_icao AND 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";
728
				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, airport.latitude AS airport_arrival_latitude, airport.longitude AS airport_arrival_longitude FROM stats_airport, airport WHERE airport.icao = stats_airport.airport_icao AND arrival > 0 AND stats_type = 'yearly' AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY airport_arrival_icao_count DESC";
729
				try {
730
					$sth = $this->db->prepare($query);
731
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
732
				} catch(PDOException $e) {
733
					echo "error : ".$e->getMessage();
734
				}
735
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
736
			} else $all = array();
737
		}
738
		if (empty($all)) {
739
			if (strpos($stats_airline,'alliance_') !== FALSE) {
740
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
741
			} else {
742
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
743
			}
744
			if ($filter_name != '') {
745
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
746
			}
747
			$Spotter = new Spotter($this->db);
748
//			$pall = $Spotter->countAllArrivalAirports($limit,0,'',false,$filters,$year,$month);
749
//			$dall = $Spotter->countAllDetectedArrivalAirports($limit,0,'',false,$filters,$year,$month);
750
			$pall = $Spotter->countAllArrivalAirports($limit,0,'',false,$filters);
751
			$dall = $Spotter->countAllDetectedArrivalAirports($limit,0,'',false,$filters);
752
			$all = array();
753
			foreach ($pall as $value) {
754
				$icao = $value['airport_arrival_icao'];
755
				$all[$icao] = $value;
756
			}
757
			foreach ($dall as $value) {
758
				$icao = $value['airport_arrival_icao'];
759
				if (isset($all[$icao])) {
760
					$all[$icao]['airport_arrival_icao_count'] = $all[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
761
				} else $all[$icao] = $value;
762
			}
763
			$count = array();
764
			foreach ($all as $key => $row) {
765
				$count[$key] = $row['airport_arrival_icao_count'];
766
			}
767
			array_multisort($count,SORT_DESC,$all);
768
		}
769
		return $all;
770
	}
771
	public function countAllMonthsLastYear($limit = true,$stats_airline = '',$filter_name = '') {
772
		global $globalDBdriver, $globalStatsFilters;
773
		if ($filter_name == '') $filter_name = $this->filter_name;
774
		if (strpos($stats_airline,'alliance_') !== FALSE) {
775
			$Spotter = new Spotter($this->db);
776
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
777
			$alliance_airlines = array();
778
			foreach ($airlines as $airline) {
779
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
780
			}
781
			if ($globalDBdriver == 'mysql') {
782
				if ($limit) $query = "SELECT MONTH(stats_date) as month_name, YEAR(stats_date) as year_name, SUM(cnt) as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 12 MONTH) AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY stats_date";
783
				else $query = "SELECT MONTH(stats_date) as month_name, YEAR(stats_date) as year_name, SUM(cnt) as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY stats_date";
784
			} else {
785
				if ($limit) $query = "SELECT EXTRACT(MONTH FROM stats_date) as month_name, EXTRACT(YEAR FROM stats_date) as year_name, SUM(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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY stats_date";
786
				else $query = "SELECT EXTRACT(MONTH FROM stats_date) as month_name, EXTRACT(YEAR FROM stats_date) as year_name, SUM(cnt) as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY stats_date";
787
			}
788
			$query_data = array(':filter_name' => $filter_name);
789
		} else {
790
			if ($globalDBdriver == 'mysql') {
791
				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";
792
				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";
793
			} else {
794
				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";
795
				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";
796
			}
797
			$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
798
		}
799
		try {
800
			$sth = $this->db->prepare($query);
801
			$sth->execute($query_data);
802
		} catch(PDOException $e) {
803
			echo "error : ".$e->getMessage();
804
		}
805
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
806
		if (empty($all)) {
807
			if (strpos($stats_airline,'alliance_') !== FALSE) {
808
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
809
			} else {
810
				$filters = array('airlines' => array($stats_airline));
811
			}
812
			if ($filter_name != '') {
813
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
814
			}
815
			$Spotter = new Spotter($this->db);
816
			$all = $Spotter->countAllMonthsLastYear($filters);
817
		}
818
		return $all;
819
	}
820
	
821
	public function countAllDatesLastMonth($stats_airline = '',$filter_name = '') {
822
		global $globalStatsFilters;
823
		if ($filter_name == '') $filter_name = $this->filter_name;
824
		if (strpos($stats_airline,'alliance_') !== FALSE) {
825
			$Spotter = new Spotter($this->db);
826
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
827
			$alliance_airlines = array();
828
			foreach ($airlines as $airline) {
829
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
830
			}
831
			$query = "SELECT flight_date as date_name, SUM(cnt) as date_count FROM stats_flight WHERE stats_type = 'month' AND stats_airline  IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY flight_date";
832
			$query_data = array(':filter_name' => $filter_name);
833
		} else {
834
			$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";
835
			$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
836
		}
837
		try {
838
			$sth = $this->db->prepare($query);
839
			$sth->execute($query_data);
840
		} catch(PDOException $e) {
841
			echo "error : ".$e->getMessage();
842
		}
843
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
844
		if (empty($all)) {
845
			if (strpos($stats_airline,'alliance_') !== FALSE) {
846
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
847
			} else {
848
				$filters = array('airlines' => array($stats_airline));
849
			}
850
			if ($filter_name != '') {
851
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
852
			}
853
			$Spotter = new Spotter($this->db);
854
			$all = $Spotter->countAllDatesLastMonth($filters);
855
		}
856
		return $all;
857
	}
858
	public function countAllDatesLast7Days($stats_airline = '',$filter_name = '') {
859
		global $globalDBdriver, $globalStatsFilters;
860
		if ($filter_name == '') $filter_name = $this->filter_name;
861
		if (strpos($stats_airline,'alliance_') !== FALSE) {
862
			$Spotter = new Spotter($this->db);
863
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
864
			$alliance_airlines = array();
865
			foreach ($airlines as $airline) {
866
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
867
			}
868
			if ($globalDBdriver == 'mysql') {
869
				$query = "SELECT flight_date as date_name, SUM(cnt) as date_count FROM stats_flight WHERE stats_type = 'month' AND flight_date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY) AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY flight_date";
870
			} else {
871
				$query = "SELECT flight_date as date_name, SUM(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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY flight_date";
872
			}
873
			$query_data = array(':filter_name' => $filter_name);
874
		} else {
875
			if ($globalDBdriver == 'mysql') {
876
				$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";
877
			} else {
878
				$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";
879
			}
880
			$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
881
		}
882
		try {
883
			$sth = $this->db->prepare($query);
884
			$sth->execute($query_data);
885
		} catch(PDOException $e) {
886
			echo "error : ".$e->getMessage();
887
		}
888
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
889
		if (empty($all)) {
890
			if (strpos($stats_airline,'alliance_') !== FALSE) {
891
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
892
			} else {
893
				$filters = array('airlines' => array($stats_airline));
894
			}
895
			if ($filter_name != '') {
896
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
897
			}
898
			$Spotter = new Spotter($this->db);
899
			$all = $Spotter->countAllDatesLast7Days($filters);
900
		}
901
		return $all;
902
	}
903
	public function countAllDates($stats_airline = '',$filter_name = '') {
904
		global $globalStatsFilters;
905
		if ($filter_name == '') $filter_name = $this->filter_name;
906
		if (strpos($stats_airline,'alliance_') !== FALSE) {
907
			$Spotter = new Spotter($this->db);
908
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
909
			$alliance_airlines = array();
910
			foreach ($airlines as $airline) {
911
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
912
			}
913
			$query = "SELECT flight_date as date_name, SUM(cnt) as date_count FROM stats_flight WHERE stats_type = 'date' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY flight_date ORDER BY date_count DESC";
914
			$query_data = array(':filter_name' => $filter_name);
915
		} else {
916
			$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 ORDER BY date_count DESC";
917
			$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
918
		}
919
		try {
920
			$sth = $this->db->prepare($query);
921
			$sth->execute($query_data);
922
		} catch(PDOException $e) {
923
			echo "error : ".$e->getMessage();
924
		}
925
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
926
		if (empty($all)) {
927
			if (strpos($stats_airline,'alliance_') !== FALSE) {
928
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
929
			} else {
930
				$filters = array('airlines' => array($stats_airline));
931
			}
932
			if ($filter_name != '') {
933
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
934
			}
935
			$Spotter = new Spotter($this->db);
936
			$all = $Spotter->countAllDates($filters);
937
		}
938
		return $all;
939
	}
940
	public function countAllDatesByAirlines($filter_name = '') {
941
		global $globalStatsFilters;
942
		if ($filter_name == '') $filter_name = $this->filter_name;
943
		$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";
944
		$query_data = array('filter_name' => $filter_name);
945
                 try {
946
                        $sth = $this->db->prepare($query);
947
                        $sth->execute($query_data);
948
                } catch(PDOException $e) {
949
                        echo "error : ".$e->getMessage();
950
                }
951
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
952
                if (empty($all)) {
953
            		$filters = array();
954
            		if ($filter_name != '') {
955
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
956
			}
957
            		$Spotter = new Spotter($this->db);
958
            		$all = $Spotter->countAllDatesByAirlines($filters);
959
                }
960
                return $all;
961
	}
962
	public function countAllMonths($stats_airline = '',$filter_name = '') {
963
		global $globalStatsFilters, $globalDBdriver;
964
		if ($filter_name == '') $filter_name = $this->filter_name;
965
		if (strpos($stats_airline,'alliance_') !== FALSE) {
966
			$Spotter = new Spotter($this->db);
967
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
968
			$alliance_airlines = array();
969
			foreach ($airlines as $airline) {
970
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
971
			}
972
			if ($globalDBdriver == 'mysql') {
973
				$query = "SELECT YEAR(stats_date) AS year_name,MONTH(stats_date) AS month_name, SUM(cnt) as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY stats_date ORDER BY date_count DESC";
974
			} else {
975
				$query = "SELECT EXTRACT(YEAR FROM stats_date) AS year_name,EXTRACT(MONTH FROM stats_date) AS month_name, SUM(cnt) as date_count FROM stats WHERE stats_type = 'flights_bymonth' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY stats_date ORDER BY date_count DESC";
976
			}
977
			$query_data = array(':filter_name' => $filter_name);
978
		} else {
979
			if ($globalDBdriver == 'mysql') {
980
				$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 ORDER BY date_count DESC";
981
			} else {
982
				$query = "SELECT EXTRACT(YEAR FROM stats_date) AS year_name,EXTRACT(MONTH FROM 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 ORDER BY date_count DESC";
983
			}
984
			$query_data = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
985
		}
986
		try {
987
			$sth = $this->db->prepare($query);
988
			$sth->execute($query_data);
989
		} catch(PDOException $e) {
990
			echo "error : ".$e->getMessage();
991
		}
992
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
993
		if (empty($all)) {
994
			if (strpos($stats_airline,'alliance_') !== FALSE) {
995
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
996
			} else {
997
				$filters = array('airlines' => array($stats_airline));
998
			}
999
			if ($filter_name != '') {
1000
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1001
			}
1002
			$Spotter = new Spotter($this->db);
1003
			$all = $Spotter->countAllMonths($filters);
1004
		}
1005
		return $all;
1006
	}
1007
	public function countFatalitiesLast12Months() {
1008
		global $globalStatsFilters, $globalDBdriver;
1009
		if ($globalDBdriver == 'mysql') {
1010
			$query = "SELECT YEAR(stats_date) AS year, MONTH(stats_date) as month,cnt as count FROM stats WHERE stats_type = 'fatalities_bymonth' ORDER BY stats_date";
1011
		} else {
1012
			$query = "SELECT EXTRACT(YEAR FROM stats_date) AS year, EXTRACT(MONTH FROM stats_date) as month,cnt as count FROM stats WHERE stats_type = 'fatalities_bymonth' ORDER BY stats_date";
1013
		}
1014
                 try {
1015
                        $sth = $this->db->prepare($query);
1016
                        $sth->execute();
1017
                } catch(PDOException $e) {
1018
                        echo "error : ".$e->getMessage();
1019
                }
1020
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1021
                
1022
                if (empty($all)) {
1023
            		$Accident = new Accident($this->db);
1024
            		$all = $Accident->countFatalitiesLast12Months();
1025
                }
1026
                return $all;
1027
	}
1028
	public function countFatalitiesByYear() {
1029
		global $globalStatsFilters, $globalDBdriver;
1030
		if ($globalDBdriver == 'mysql') {
1031
			$query = "SELECT YEAR(stats_date) AS year, cnt as count FROM stats WHERE stats_type = 'fatalities_byyear' ORDER BY stats_date";
1032
		} else {
1033
			$query = "SELECT EXTRACT(YEAR FROM stats_date) AS year, cnt as count FROM stats WHERE stats_type = 'fatalities_byyear' ORDER BY stats_date";
1034
		}
1035
                 try {
1036
                        $sth = $this->db->prepare($query);
1037
                        $sth->execute();
1038
                } catch(PDOException $e) {
1039
                        echo "error : ".$e->getMessage();
1040
                }
1041
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1042
                
1043
                if (empty($all)) {
1044
            		$Accident = new Accident($this->db);
1045
            		$all = $Accident->countFatalitiesByYear();
1046
                }
1047
                return $all;
1048
	}
1049
	public function countAllMilitaryMonths($filter_name = '') {
1050
		global $globalStatsFilters;
1051
		if ($filter_name == '') $filter_name = $this->filter_name;
1052
	    	$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";
1053
                 try {
1054
                        $sth = $this->db->prepare($query);
1055
                        $sth->execute(array(':filter_name' => $filter_name));
1056
                } catch(PDOException $e) {
1057
                        echo "error : ".$e->getMessage();
1058
                }
1059
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1060
                if (empty($all)) {
1061
            		$filters = array();
1062
            		if ($filter_name != '') {
1063
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1064
			}
1065
            		$Spotter = new Spotter($this->db);
1066
            		$all = $Spotter->countAllMilitaryMonths($filters);
1067
                }
1068
                return $all;
1069
	}
1070
	public function countAllHours($orderby = 'hour',$limit = true,$stats_airline = '',$filter_name = '') {
1071
		global $globalTimezone, $globalDBdriver, $globalStatsFilters;
1072
		if ($filter_name == '') $filter_name = $this->filter_name;
1073
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1074
			$Spotter = new Spotter($this->db);
1075
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1076
			$alliance_airlines = array();
1077
			foreach ($airlines as $airline) {
1078
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1079
			}
1080
			if ($limit) $query = "SELECT flight_date as hour_name, SUM(cnt) as hour_count FROM stats_flight WHERE stats_type = 'hour' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY flight_date";
1081
			else $query = "SELECT flight_date as hour_name, SUM(cnt) as hour_count FROM stats_flight WHERE stats_type = 'hour' AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name GROUP BY flight_date";
1082
			$query_data = array(':filter_name' => $filter_name);
1083
		} else {
1084
			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";
1085
			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";
1086
			$query_data = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1087
		}
1088
		if ($orderby == 'hour') {
1089
			if ($globalDBdriver == 'mysql') {
1090
				$query .= " ORDER BY CAST(flight_date AS UNSIGNED) ASC";
1091
			} else {
1092
				$query .= " ORDER BY CAST(flight_date AS integer) ASC";
1093
			}
1094
		}
1095
		if ($orderby == 'count') $query .= " ORDER BY hour_count DESC";
1096
		try {
1097
			$sth = $this->db->prepare($query);
1098
			$sth->execute($query_data);
1099
		} catch(PDOException $e) {
1100
			echo "error : ".$e->getMessage();
1101
		}
1102
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1103
		if (empty($all)) {
1104
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1105
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1106
			} else {
1107
				$filters = array('airlines' => array($stats_airline));
1108
			}
1109
			if ($filter_name != '') {
1110
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1111
			}
1112
			$Spotter = new Spotter($this->db);
1113
			$all = $Spotter->countAllHours($orderby,$filters);
1114
		}
1115
		return $all;
1116
	}
1117
	public function countOverallFlights($stats_airline = '', $filter_name = '',$year = '',$month = '') {
1118
		global $globalStatsFilters;
1119
		if ($filter_name == '') $filter_name = $this->filter_name;
1120
		if ($year == '') $year = date('Y');
1121
		$all = $this->getSumStats('flights_bymonth',$year,$stats_airline,$filter_name,$month);
1122
		if (empty($all)) {
1123
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1124
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1125
			} else {
1126
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1127
			}
1128
			if ($filter_name != '') {
1129
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1130
			}
1131
			$Spotter = new Spotter($this->db);
1132
			//$all = $Spotter->countOverallFlights($filters,$year,$month);
1133
			$all = $Spotter->countOverallFlights($filters);
1134
		}
1135
		return $all;
1136
	}
1137
	public function countOverallMilitaryFlights($filter_name = '',$year = '', $month = '') {
1138
		global $globalStatsFilters;
1139
		if ($filter_name == '') $filter_name = $this->filter_name;
1140
		if ($year == '') $year = date('Y');
1141
		$all = $this->getSumStats('military_flights_bymonth',$year,'',$filter_name,$month);
1142
		if (empty($all)) {
1143
		        $filters = array();
0 ignored issues
show
Unused Code introduced by
$filters 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...
1144
			$filters = array('year' => $year,'month' => $month);
1145
            		if ($filter_name != '') {
1146
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1147
			}
1148
			$Spotter = new Spotter($this->db);
1149
			//$all = $Spotter->countOverallMilitaryFlights($filters,$year,$month);
1150
			$all = $Spotter->countOverallMilitaryFlights($filters);
1151
		}
1152
		return $all;
1153
	}
1154
	public function countOverallArrival($stats_airline = '',$filter_name = '', $year = '', $month = '') {
1155
		global $globalStatsFilters;
1156
		if ($filter_name == '') $filter_name = $this->filter_name;
1157
		if ($year == '') $year = date('Y');
1158
		$all = $this->getSumStats('realarrivals_bymonth',$year,$stats_airline,$filter_name,$month);
1159
		if (empty($all)) {
1160
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1161
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1162
			} else {
1163
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1164
			}
1165
			if ($filter_name != '') {
1166
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1167
			}
1168
			$Spotter = new Spotter($this->db);
1169
			//$all = $Spotter->countOverallArrival($filters,$year,$month);
1170
			$all = $Spotter->countOverallArrival($filters);
1171
		}
1172
		return $all;
1173
	}
1174
	public function countOverallAircrafts($stats_airline = '',$filter_name = '',$year = '', $month = '') {
1175
		global $globalStatsFilters;
1176
		if ($filter_name == '') $filter_name = $this->filter_name;
1177
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1178
			$Spotter = new Spotter($this->db);
1179
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1180
			if ($year == '' && $month == '') {
1181
				$alliance_airlines = array();
1182
				foreach ($airlines as $airline) {
1183
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1184
				}
1185
				$query = "SELECT COUNT(*) AS nb FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1186
				try {
1187
					$sth = $this->db->prepare($query);
1188
					$sth->execute(array(':filter_name' => $filter_name));
1189
				} catch(PDOException $e) {
1190
					echo "error : ".$e->getMessage();
1191
				}
1192
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1193
				$all = $result[0]['nb'];
1194
			} else $all = $this->getSumStats('aircrafts_bymonth',$year,$stats_airline,$filter_name,$month);
1195
		} else {
1196
			if ($year == '' && $month == '') {
1197
				$query = "SELECT COUNT(*) AS nb FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1198
				try {
1199
					$sth = $this->db->prepare($query);
1200
					$sth->execute(array(':filter_name' => $filter_name,':stats_airline' => $stats_airline));
1201
				} catch(PDOException $e) {
1202
					echo "error : ".$e->getMessage();
1203
				}
1204
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1205
				$all = $result[0]['nb'];
1206
			} else $all = $this->getSumStats('aircrafts_bymonth',$year,$stats_airline,$filter_name,$month);
1207
		}
1208
		if (empty($all)) {
1209
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1210
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1211
			} else {
1212
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1213
			}
1214
			if ($filter_name != '') {
1215
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1216
			}
1217
			$Spotter = new Spotter($this->db);
1218
			//$all = $Spotter->countOverallAircrafts($filters,$year,$month);
1219
			$all = $Spotter->countOverallAircrafts($filters);
1220
		}
1221
		return $all;
1222
	}
1223
	public function countOverallAirlines($filter_name = '',$year = '',$month = '') {
1224
		global $globalStatsFilters;
1225
		if ($filter_name == '') $filter_name = $this->filter_name;
1226
		if ($year == '' && $month == '') {
1227
			$query = "SELECT COUNT(*) AS nb_airline FROM stats_airline WHERE filter_name = :filter_name";
1228
			try {
1229
				$sth = $this->db->prepare($query);
1230
				$sth->execute(array(':filter_name' => $filter_name));
1231
			} catch(PDOException $e) {
1232
				echo "error : ".$e->getMessage();
1233
			}
1234
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1235
			$all = $result[0]['nb_airline'];
1236
		} else $all = $this->getSumStats('airlines_bymonth',$year,'',$filter_name,$month);
1237
		if (empty($all)) {
1238
            		$filters = array();
0 ignored issues
show
Unused Code introduced by
$filters 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...
1239
			$filters = array('year' => $year,'month' => $month);
1240
            		if ($filter_name != '') {
1241
            			$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1242
			}
1243
			$Spotter = new Spotter($this->db);
1244
			//$all = $Spotter->countOverallAirlines($filters,$year,$month);
1245
			$all = $Spotter->countOverallAirlines($filters);
1246
		}
1247
		return $all;
1248
	}
1249
	public function countOverallOwners($stats_airline = '',$filter_name = '',$year = '', $month = '') {
1250
		global $globalStatsFilters;
1251
		if ($filter_name == '') $filter_name = $this->filter_name;
1252
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1253
			$Spotter = new Spotter($this->db);
1254
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1255
			if ($year == '' && $month == '') {
1256
				$alliance_airlines = array();
1257
				foreach ($airlines as $airline) {
1258
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1259
				}
1260
				$query = "SELECT count(*) as nb FROM stats_owner WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1261
				$query_values = array(':filter_name' => $filter_name);
1262
				try {
1263
					$sth = $this->db->prepare($query);
1264
					$sth->execute($query_values);
1265
				} catch(PDOException $e) {
1266
					echo "error : ".$e->getMessage();
1267
				}
1268
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1269
				$all = $result[0]['nb'];
1270
			} else {
1271
				$all = $this->getSumStats('owners_bymonth',$year,$stats_airline,$filter_name,$month);
1272
			}
1273
		} else {
1274
			if ($year == '' && $month == '') {
1275
				$query = "SELECT count(*) as nb FROM stats_owner WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1276
				$query_values = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1277
				try {
1278
					$sth = $this->db->prepare($query);
1279
					$sth->execute($query_values);
1280
				} catch(PDOException $e) {
1281
					echo "error : ".$e->getMessage();
1282
				}
1283
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1284
				$all = $result[0]['nb'];
1285
			} else {
1286
				$all = $this->getSumStats('owners_bymonth',$year,$stats_airline,$filter_name,$month);
1287
			}
1288
		}
1289
		if (empty($all)) {
1290
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1291
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1292
			} else {
1293
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1294
			}
1295
			if ($filter_name != '') {
1296
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1297
			}
1298
			$Spotter = new Spotter($this->db);
1299
			//$all = $Spotter->countOverallOwners($filters,$year,$month);
1300
			$all = $Spotter->countOverallOwners($filters);
1301
		}
1302
		return $all;
1303
	}
1304
	public function countOverallPilots($stats_airline = '',$filter_name = '',$year = '',$month = '') {
1305
		global $globalStatsFilters;
1306
		if ($filter_name == '') $filter_name = $this->filter_name;
1307
		//if ($year == '') $year = date('Y');
1308
		if ($year == '' && $month == '') {
1309
			$query = "SELECT count(*) as nb FROM stats_pilot WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1310
			$query_values = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1311
			try {
1312
				$sth = $this->db->prepare($query);
1313
				$sth->execute($query_values);
1314
			} catch(PDOException $e) {
1315
				echo "error : ".$e->getMessage();
1316
			}
1317
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1318
			$all = $result[0]['nb'];
1319
		} else {
1320
			$all = $this->getSumStats('pilots_bymonth',$year,$stats_airline,$filter_name,$month);
1321
		}
1322
		if (empty($all)) {
1323
			$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1324
			if ($filter_name != '') {
1325
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1326
			}
1327
			$Spotter = new Spotter($this->db);
1328
			//$all = $Spotter->countOverallPilots($filters,$year,$month);
1329
			$all = $Spotter->countOverallPilots($filters);
1330
		}
1331
		return $all;
1332
	}
1333
1334
	public function getLast7DaysAirports($airport_icao = '', $stats_airline = '',$filter_name = '') {
1335
		if ($filter_name == '') $filter_name = $this->filter_name;
1336
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1337
			$Spotter = new Spotter($this->db);
1338
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1339
			$alliance_airlines = array();
1340
			foreach ($airlines as $airline) {
1341
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1342
			}
1343
			$query = "SELECT * FROM stats_airport WHERE stats_type = 'daily' AND airport_icao = :airport_icao AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name ORDER BY date";
1344
			$query_values = array(':airport_icao' => $airport_icao,':filter_name' => $filter_name);
1345
		} else {
1346
			$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";
1347
			$query_values = array(':airport_icao' => $airport_icao,':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1348
		}
1349
		try {
1350
			$sth = $this->db->prepare($query);
1351
			$sth->execute($query_values);
1352
		} catch(PDOException $e) {
1353
			echo "error : ".$e->getMessage();
1354
		}
1355
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1356
		return $all;
1357
	}
1358
	public function getStats($type,$stats_airline = '', $filter_name = '') {
1359
		if ($filter_name == '') $filter_name = $this->filter_name;
1360
		$query = "SELECT * FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY stats_date";
1361
		$query_values = array(':type' => $type,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1362
		try {
1363
			$sth = $this->db->prepare($query);
1364
			$sth->execute($query_values);
1365
		} catch(PDOException $e) {
1366
			echo "error : ".$e->getMessage();
1367
		}
1368
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1369
		return $all;
1370
	}
1371
	public function deleteStatsByType($type,$stats_airline = '', $filter_name = '') {
1372
		if ($filter_name == '') $filter_name = $this->filter_name;
1373
		$query = "DELETE FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline AND filter_name = :filter_name";
1374
		$query_values = array(':type' => $type,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1375
		try {
1376
			$sth = $this->db->prepare($query);
1377
			$sth->execute($query_values);
1378
		} catch(PDOException $e) {
1379
			echo "error : ".$e->getMessage();
1380
		}
1381
	}
1382
	public function getSumStats($type,$year,$stats_airline = '',$filter_name = '',$month = '') {
1383
		if ($filter_name == '') $filter_name = $this->filter_name;
1384
		global $globalArchiveMonths, $globalDBdriver;
1385
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1386
			$Spotter = new Spotter($this->db);
1387
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1388
			$alliance_airlines = array();
1389
			foreach ($airlines as $airline) {
1390
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1391
			}
1392
			if ($globalDBdriver == 'mysql') {
1393
				if ($month == '') {
1394
					$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND YEAR(stats_date) = :year AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1395
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name);
1396
				} else {
1397
					$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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1398
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name,':month' => $month);
1399
				}
1400
			} else {
1401
				if ($month == '') {
1402
					$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND EXTRACT(YEAR FROM stats_date) = :year AND stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1403
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name);
1404
				} else {
1405
					$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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1406
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name,':month' => $month);
1407
				}
1408
			}
1409
		} else {
1410
			if ($globalDBdriver == 'mysql') {
1411
				if ($month == '') {
1412
					$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";
1413
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1414
				} else {
1415
					$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";
1416
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
1417
				}
1418
			} else {
1419
				if ($month == '') {
1420
					$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";
1421
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1422
				} else {
1423
					$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";
1424
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
1425
				}
1426
			}
1427
		}
1428
		try {
1429
			$sth = $this->db->prepare($query);
1430
			$sth->execute($query_values);
1431
		} catch(PDOException $e) {
1432
			echo "error : ".$e->getMessage();
1433
		}
1434
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1435
		return $all[0]['total'];
1436
	}
1437
	public function getStatsTotal($type, $stats_airline = '', $filter_name = '') {
1438
		global $globalArchiveMonths, $globalDBdriver;
1439
		if ($filter_name == '') $filter_name = $this->filter_name;
1440
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1441
			$Spotter = new Spotter($this->db);
1442
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1443
			$alliance_airlines = array();
1444
			foreach ($airlines as $airline) {
1445
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1446
			}
1447
			if ($globalDBdriver == 'mysql') {
1448
				$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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1449
			} else {
1450
				$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 IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1451
			}
1452
			$query_values = array(':type' => $type, ':filter_name' => $filter_name);
1453
		} else {
1454
			if ($globalDBdriver == 'mysql') {
1455
				$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";
1456
			} else {
1457
				$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";
1458
			}
1459
			$query_values = array(':type' => $type, ':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1460
		}
1461
		try {
1462
			$sth = $this->db->prepare($query);
1463
			$sth->execute($query_values);
1464
		} catch(PDOException $e) {
1465
			echo "error : ".$e->getMessage();
1466
		}
1467
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1468
		return $all[0]['total'];
1469
	}
1470
	public function getStatsAircraftTotal($stats_airline = '', $filter_name = '') {
1471
		global $globalArchiveMonths, $globalDBdriver;
1472
		if ($filter_name == '') $filter_name = $this->filter_name;
1473
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1474
			$Spotter = new Spotter($this->db);
1475
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1476
			$alliance_airlines = array();
1477
			foreach ($airlines as $airline) {
1478
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1479
			}
1480
			if ($globalDBdriver == 'mysql') {
1481
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1482
			} else {
1483
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1484
			}
1485
		} else {
1486
			if ($globalDBdriver == 'mysql') {
1487
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1488
			} else {
1489
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1490
			}
1491
		}
1492
		try {
1493
			$sth = $this->db->prepare($query);
1494
			$sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
1495
		} catch(PDOException $e) {
1496
			echo "error : ".$e->getMessage();
1497
		}
1498
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1499
		return $all[0]['total'];
1500
	}
1501
	public function getStatsAirlineTotal($filter_name = '') {
1502
    		global $globalArchiveMonths, $globalDBdriver;
1503
		if ($filter_name == '') $filter_name = $this->filter_name;
1504
    		if ($globalDBdriver == 'mysql') {
1505
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
1506
                } else {
1507
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
1508
                }
1509
                 try {
1510
                        $sth = $this->db->prepare($query);
1511
                        $sth->execute(array(':filter_name' => $filter_name));
1512
                } catch(PDOException $e) {
1513
                        echo "error : ".$e->getMessage();
1514
                }
1515
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1516
                return $all[0]['total'];
1517
        }
1518
	public function getStatsOwnerTotal($filter_name = '') {
1519
    		global $globalArchiveMonths, $globalDBdriver;
1520
		if ($filter_name == '') $filter_name = $this->filter_name;
1521
    		if ($globalDBdriver == 'mysql') {
1522
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
1523
		} else {
1524
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
1525
                }
1526
                 try {
1527
                        $sth = $this->db->prepare($query);
1528
                        $sth->execute(array(':filter_name' => $filter_name));
1529
                } catch(PDOException $e) {
1530
                        echo "error : ".$e->getMessage();
1531
                }
1532
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1533
                return $all[0]['total'];
1534
        }
1535
	public function getStatsOwner($owner_name,$filter_name = '') {
1536
    		global $globalArchiveMonths, $globalDBdriver;
1537
		if ($filter_name == '') $filter_name = $this->filter_name;
1538
		$query = "SELECT cnt FROM stats_owner WHERE filter_name = :filter_name AND owner_name = :owner_name";
1539
                 try {
1540
                        $sth = $this->db->prepare($query);
1541
                        $sth->execute(array(':filter_name' => $filter_name,':owner_name' => $owner_name));
1542
                } catch(PDOException $e) {
1543
                        echo "error : ".$e->getMessage();
1544
                }
1545
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1546
                if (isset($all[0]['cnt'])) return $all[0]['cnt'];
1547
                else return 0;
1548
        }
1549
	public function getStatsPilotTotal($filter_name = '') {
1550
    		global $globalArchiveMonths, $globalDBdriver;
1551
		if ($filter_name == '') $filter_name = $this->filter_name;
1552
    		if ($globalDBdriver == 'mysql') {
1553
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
1554
            	} else {
1555
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
1556
            	}
1557
                 try {
1558
                        $sth = $this->db->prepare($query);
1559
                        $sth->execute(array(':filter_name' => $filter_name));
1560
                } catch(PDOException $e) {
1561
                        echo "error : ".$e->getMessage();
1562
                }
1563
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1564
                return $all[0]['total'];
1565
        }
1566
	public function getStatsPilot($pilot,$filter_name = '') {
1567
    		global $globalArchiveMonths, $globalDBdriver;
1568
		if ($filter_name == '') $filter_name = $this->filter_name;
1569
		$query = "SELECT cnt FROM stats_pilot WHERE filter_name = :filter_name AND (pilot_name = :pilot OR pilot_id = :pilot)";
1570
                 try {
1571
                        $sth = $this->db->prepare($query);
1572
                        $sth->execute(array(':filter_name' => $filter_name,':pilot' => $pilot));
1573
                } catch(PDOException $e) {
1574
                        echo "error : ".$e->getMessage();
1575
                }
1576
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1577
                if (isset($all[0]['cnt'])) return $all[0]['cnt'];
1578
                else return 0;
1579
        }
1580
1581
	public function addStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
1582
		global $globalDBdriver;
1583
		if ($filter_name == '') $filter_name = $this->filter_name;
1584
		if ($globalDBdriver == 'mysql') {
1585
			$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";
1586
                } else {
1587
			$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);"; 
1588
		}
1589
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1590
                 try {
1591
                        $sth = $this->db->prepare($query);
1592
                        $sth->execute($query_values);
1593
                } catch(PDOException $e) {
1594
                        return "error : ".$e->getMessage();
1595
                }
1596
        }
1597
	public function updateStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
1598
		global $globalDBdriver;
1599
		if ($filter_name == '') $filter_name = $this->filter_name;
1600
		if ($globalDBdriver == 'mysql') {
1601
			$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";
1602
		} else {
1603
            		//$query = "INSERT INTO stats (stats_type,cnt,stats_date) VALUES (:type,:cnt,:stats_date) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
1604
			$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);"; 
1605
                }
1606
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1607
                 try {
1608
                        $sth = $this->db->prepare($query);
1609
                        $sth->execute($query_values);
1610
                } catch(PDOException $e) {
1611
                        return "error : ".$e->getMessage();
1612
                }
1613
        }
1614
        /*
1615
	public function getStatsSource($date,$stats_type = '') {
1616
		if ($stats_type == '') {
1617
			$query = "SELECT * FROM stats_source WHERE stats_date = :date ORDER BY source_name";
1618
			$query_values = array(':date' => $date);
1619
		} else {
1620
			$query = "SELECT * FROM stats_source WHERE stats_date = :date AND stats_type = :stats_type ORDER BY source_name";
1621
			$query_values = array(':date' => $date,':stats_type' => $stats_type);
1622
		}
1623
                 try {
1624
                        $sth = $this->db->prepare($query);
1625
                        $sth->execute($query_values);
1626
                } catch(PDOException $e) {
1627
                        echo "error : ".$e->getMessage();
1628
                }
1629
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1630
                return $all;
1631
        }
1632
        */
1633
1634
	public function getStatsSource($stats_type,$year = '',$month = '',$day = '') {
1635
		global $globalDBdriver;
1636
		$query = "SELECT * FROM stats_source WHERE stats_type = :stats_type";
1637
		$query_values = array();
1638
		if ($globalDBdriver == 'mysql') {
1639
			if ($year != '') {
1640
				$query .= ' AND YEAR(stats_date) = :year';
1641
				$query_values = array_merge($query_values,array(':year' => $year));
1642
			}
1643
			if ($month != '') {
1644
				$query .= ' AND MONTH(stats_date) = :month';
1645
				$query_values = array_merge($query_values,array(':month' => $month));
1646
			}
1647
			if ($day != '') {
1648
				$query .= ' AND DAY(stats_date) = :day';
1649
				$query_values = array_merge($query_values,array(':day' => $day));
1650
			}
1651
		} else {
1652
			if ($year != '') {
1653
				$query .= ' AND EXTRACT(YEAR FROM stats_date) = :year';
1654
				$query_values = array_merge($query_values,array(':year' => $year));
1655
			}
1656
			if ($month != '') {
1657
				$query .= ' AND EXTRACT(MONTH FROM stats_date) = :month';
1658
				$query_values = array_merge($query_values,array(':month' => $month));
1659
			}
1660
			if ($day != '') {
1661
				$query .= ' AND EXTRACT(DAY FROM stats_date) = :day';
1662
				$query_values = array_merge($query_values,array(':day' => $day));
1663
			}
1664
		}
1665
		$query .= " ORDER BY source_name";
1666
		$query_values = array_merge($query_values,array(':stats_type' => $stats_type));
1667
		try {
1668
			$sth = $this->db->prepare($query);
1669
			$sth->execute($query_values);
1670
		} catch(PDOException $e) {
1671
			echo "error : ".$e->getMessage();
1672
		}
1673
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1674
		return $all;
1675
	}
1676
1677
	public function addStatSource($data,$source_name,$stats_type,$date) {
1678
		global $globalDBdriver;
1679
		if ($globalDBdriver == 'mysql') {
1680
			$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";
1681
		} else {
1682
			$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);"; 
1683
                }
1684
                $query_values = array(':data' => $data,':stats_date' => $date,':source_name' => $source_name,':stats_type' => $stats_type);
1685
                 try {
1686
                        $sth = $this->db->prepare($query);
1687
                        $sth->execute($query_values);
1688
                } catch(PDOException $e) {
1689
                        return "error : ".$e->getMessage();
1690
                }
1691
        }
1692
	public function addStatFlight($type,$date_name,$cnt,$stats_airline = '',$filter_name = '') {
1693
                $query = "INSERT INTO stats_flight (stats_type,flight_date,cnt,stats_airline,filter_name) VALUES (:type,:flight_date,:cnt,:stats_airline,:filter_name)";
1694
                $query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1695
                 try {
1696
                        $sth = $this->db->prepare($query);
1697
                        $sth->execute($query_values);
1698
                } catch(PDOException $e) {
1699
                        return "error : ".$e->getMessage();
1700
                }
1701
        }
1702
	public function addStatAircraftRegistration($registration,$cnt,$aircraft_icao = '',$airline_icao = '',$filter_name = '',$reset = false) {
1703
		global $globalDBdriver;
1704
		if ($globalDBdriver == 'mysql') {
1705
			if ($reset) {
1706
				$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";
1707
			} else {
1708
				$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";
1709
			}
1710
		} else {
1711
			if ($reset) {
1712
				$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);"; 
1713
			} else {
1714
				$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);"; 
1715
			}
1716
		}
1717
                $query_values = array(':aircraft_icao' => $aircraft_icao,':registration' => $registration,':cnt' => $cnt,':stats_airline' => $airline_icao, ':filter_name' => $filter_name);
1718
                 try {
1719
                        $sth = $this->db->prepare($query);
1720
                        $sth->execute($query_values);
1721
                } catch(PDOException $e) {
1722
                        return "error : ".$e->getMessage();
1723
                }
1724
        }
1725
	public function addStatCallsign($callsign_icao,$cnt,$airline_icao = '', $filter_name = '', $reset = false) {
1726
		global $globalDBdriver;
1727
		if ($globalDBdriver == 'mysql') {
1728
			if ($reset) {
1729
				$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";
1730
			} else {
1731
				$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";
1732
			}
1733
		} else {
1734
			if ($reset) {
1735
				$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);"; 
1736
			} else {
1737
				$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);"; 
1738
			}
1739
		}
1740
                $query_values = array(':callsign_icao' => $callsign_icao,':airline_icao' => $airline_icao,':cnt' => $cnt, ':filter_name' => $filter_name);
1741
                 try {
1742
                        $sth = $this->db->prepare($query);
1743
                        $sth->execute($query_values);
1744
                } catch(PDOException $e) {
1745
                        return "error : ".$e->getMessage();
1746
                }
1747
        }
1748
	public function addStatCountry($iso2,$iso3,$name,$cnt,$airline_icao = '',$filter_name = '',$reset = false) {
1749
		global $globalDBdriver;
1750
		if ($globalDBdriver == 'mysql') {
1751
			if ($reset) {
1752
				$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";
1753
			} else {
1754
				$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";
1755
			}
1756
		} else {
1757
			if ($reset) {
1758
				$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);"; 
1759
			} else {
1760
				$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);"; 
1761
			}
1762
		}
1763
                $query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt,':filter_name' => $filter_name,':airline' => $airline_icao);
1764
                 try {
1765
                        $sth = $this->db->prepare($query);
1766
                        $sth->execute($query_values);
1767
                } catch(PDOException $e) {
1768
                        return "error : ".$e->getMessage();
1769
                }
1770
        }
1771
	public function addStatAircraft($aircraft_icao,$cnt,$aircraft_name = '',$aircraft_manufacturer = '', $airline_icao = '', $filter_name = '', $reset = false) {
1772
		global $globalDBdriver;
1773
		if ($globalDBdriver == 'mysql') {
1774
			if ($reset) {
1775
				$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";
1776
			} else {
1777
				$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";
1778
			}
1779
		} else {
1780
			if ($reset) {
1781
				$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);"; 
1782
			} else {
1783
				$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);"; 
1784
			}
1785
		}
1786
                $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);
1787
                 try {
1788
                        $sth = $this->db->prepare($query);
1789
                        $sth->execute($query_values);
1790
                } catch(PDOException $e) {
1791
                        return "error : ".$e->getMessage();
1792
                }
1793
        }
1794
	public function addStatAirline($airline_icao,$cnt,$airline_name = '',$filter_name = '', $reset = false) {
1795
		global $globalDBdriver;
1796
		if ($globalDBdriver == 'mysql') {
1797
			if ($reset) {
1798
				$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";
1799
			} else {
1800
				$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";
1801
			}
1802
		} else {
1803
			if ($reset) {
1804
				$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);"; 
1805
			} else {
1806
				$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);"; 
1807
			}
1808
		}
1809
                $query_values = array(':airline_icao' => $airline_icao,':airline_name' => $airline_name,':cnt' => $cnt,':filter_name' => $filter_name);
1810
                 try {
1811
                        $sth = $this->db->prepare($query);
1812
                        $sth->execute($query_values);
1813
                } catch(PDOException $e) {
1814
                        return "error : ".$e->getMessage();
1815
                }
1816
        }
1817
	public function addStatOwner($owner_name,$cnt,$stats_airline = '', $filter_name = '', $reset = false) {
1818
		global $globalDBdriver;
1819
		if ($globalDBdriver == 'mysql') {
1820
			if ($reset) {
1821
				$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";
1822
			} else {
1823
				$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";
1824
			}
1825
		} else {
1826
			if ($reset) {
1827
				$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);"; 
1828
			} else {
1829
				$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);"; 
1830
			}
1831
		}
1832
                $query_values = array(':owner_name' => $owner_name,':cnt' => $cnt,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1833
                 try {
1834
                        $sth = $this->db->prepare($query);
1835
                        $sth->execute($query_values);
1836
                } catch(PDOException $e) {
1837
                        return "error : ".$e->getMessage();
1838
                }
1839
        }
1840
	public function addStatPilot($pilot_id,$cnt,$pilot_name,$stats_airline = '',$filter_name = '',$format_source = '',$reset = false) {
1841
		global $globalDBdriver;
1842
		if ($globalDBdriver == 'mysql') {
1843
			if ($reset) {
1844
				$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";
1845
			} else {
1846
				$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";
1847
			}
1848
		} else {
1849
			if ($reset) {
1850
				$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);"; 
1851
			} else {
1852
				$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);"; 
1853
			}
1854
		}
1855
                $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);
1856
                 try {
1857
                        $sth = $this->db->prepare($query);
1858
                        $sth->execute($query_values);
1859
                } catch(PDOException $e) {
1860
                        return "error : ".$e->getMessage();
1861
                }
1862
        }
1863
	public function addStatDepartureAirports($airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '',$reset = false) {
1864
		global $globalDBdriver;
1865
		if ($airport_icao != '') {
1866
			if ($globalDBdriver == 'mysql') {
1867
				if ($reset) {
1868
					$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";
1869
				} else {
1870
					$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";
1871
				}
1872
			} else {
1873
				if ($reset) {
1874
					$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);"; 
1875
				} else {
1876
					$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);"; 
1877
				}
1878
			}
1879
			$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);
1880
			try {
1881
				$sth = $this->db->prepare($query);
1882
				$sth->execute($query_values);
1883
			} catch(PDOException $e) {
1884
				return "error : ".$e->getMessage();
1885
			}
1886
                }
1887
        }
1888
	public function addStatDepartureAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '') {
1889
		global $globalDBdriver;
1890
		if ($airport_icao != '') {
1891
			if ($globalDBdriver == 'mysql') {
1892
				$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";
1893
			} else {
1894
				$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);"; 
1895
			}
1896
			$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);
1897
			 try {
1898
				$sth = $this->db->prepare($query);
1899
				$sth->execute($query_values);
1900
			} catch(PDOException $e) {
1901
				return "error : ".$e->getMessage();
1902
			}
1903
                }
1904
        }
1905
	public function addStatArrivalAirports($airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '',$reset = false) {
1906
		global $globalDBdriver;
1907
		if ($airport_icao != '') {
1908
			if ($globalDBdriver == 'mysql') {
1909
				if ($reset) {
1910
					$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";
1911
				} else {
1912
					$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";
1913
				}
1914
			} else {
1915
				if ($reset) {
1916
					$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);"; 
1917
				} else {
1918
					$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);"; 
1919
				}
1920
			}
1921
	                $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);
1922
			 try {
1923
                    		$sth = $this->db->prepare($query);
1924
	                        $sth->execute($query_values);
1925
    		        } catch(PDOException $e) {
1926
            		        return "error : ".$e->getMessage();
1927
	                }
1928
	        }
1929
        }
1930
	public function addStatArrivalAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '') {
1931
		global $globalDBdriver;
1932
		if ($airport_icao != '') {
1933
			if ($globalDBdriver == 'mysql') {
1934
				$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";
1935
			} else {
1936
				$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);"; 
1937
			}
1938
			$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);
1939
			try {
1940
				$sth = $this->db->prepare($query);
1941
				$sth->execute($query_values);
1942
			} catch(PDOException $e) {
1943
				return "error : ".$e->getMessage();
1944
			}
1945
                }
1946
        }
1947
1948
	public function deleteStat($id) {
1949
                $query = "DELETE FROM stats WHERE stats_id = :id";
1950
                $query_values = array(':id' => $id);
1951
                 try {
1952
                        $sth = $this->db->prepare($query);
1953
                        $sth->execute($query_values);
1954
                } catch(PDOException $e) {
1955
                        return "error : ".$e->getMessage();
1956
                }
1957
        }
1958
	public function deleteStatFlight($type) {
1959
                $query = "DELETE FROM stats_flight WHERE stats_type = :type";
1960
                $query_values = array(':type' => $type);
1961
                 try {
1962
                        $sth = $this->db->prepare($query);
1963
                        $sth->execute($query_values);
1964
                } catch(PDOException $e) {
1965
                        return "error : ".$e->getMessage();
1966
                }
1967
        }
1968
	public function deleteStatAirport($type) {
1969
                $query = "DELETE FROM stats_airport WHERE stats_type = :type";
1970
                $query_values = array(':type' => $type);
1971
                 try {
1972
                        $sth = $this->db->prepare($query);
1973
                        $sth->execute($query_values);
1974
                } catch(PDOException $e) {
1975
                        return "error : ".$e->getMessage();
1976
                }
1977
        }
1978
        
1979
        public function addOldStats() {
1980
    		global $globalDebug, $globalArchiveMonths, $globalArchive, $globalArchiveYear, $globalDBdriver, $globalStatsFilters,$globalDeleteLastYearStats,$globalStatsReset,$globalStatsResetYear, $globalAccidents;
1981
    		$Common = new Common();
1982
    		$Connection = new Connection();
1983
    		date_default_timezone_set('UTC');
1984
    		$last_update = $this->getLastStatsUpdate('last_update_stats');
1985
			if ($globalDebug) echo 'Update stats !'."\n";
1986
			if (isset($last_update[0]['value'])) {
1987
				$last_update_day = $last_update[0]['value'];
1988
			} else $last_update_day = '2012-12-12 12:12:12';
1989
			$reset = false;
1990
			//if ($globalStatsResetYear && date('Y',strtotime($last_update_day)) != date('Y')) {
1991
			if ($globalStatsResetYear) {
1992
				$reset = true;
1993
				$last_update_day = date('Y').'-01-01 00:00:00';
1994
			}
1995
			$Spotter = new Spotter($this->db);
1996
1997
			if ($globalDebug) echo 'Count all aircraft types...'."\n";
1998
			$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day);
1999
			foreach ($alldata as $number) {
2000
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'','',$reset);
2001
			}
2002
			if ($globalDebug) echo 'Count all airlines...'."\n";
2003
			$alldata = $Spotter->countAllAirlines(false,0,$last_update_day);
2004
			foreach ($alldata as $number) {
2005
				$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],'',$reset);
2006
			}
2007
			if ($globalDebug) echo 'Count all registrations...'."\n";
2008
			$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day);
2009
			foreach ($alldata as $number) {
2010
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'','',$reset);
2011
			}
2012
			if ($globalDebug) echo 'Count all callsigns...'."\n";
2013
			$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day);
2014
			foreach ($alldata as $number) {
2015
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
2016
			}
2017
			if ($globalDebug) echo 'Count all owners...'."\n";
2018
			$alldata = $Spotter->countAllOwners(false,0,$last_update_day);
2019
			foreach ($alldata as $number) {
2020
				$this->addStatOwner($number['owner_name'],$number['owner_count'],'','',$reset);
2021
			}
2022
			if ($globalDebug) echo 'Count all pilots...'."\n";
2023
			$alldata = $Spotter->countAllPilots(false,0,$last_update_day);
2024
			foreach ($alldata as $number) {
2025
				if ($number['pilot_id'] == 0 || $number['pilot_id'] == '') $number['pilot_id'] = $number['pilot_name'];
2026
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'','',$number['format_source'],$reset);
2027
			}
2028
			
2029
			if ($globalDebug) echo 'Count all departure airports...'."\n";
2030
			$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day);
2031
			if ($globalDebug) echo 'Count all detected departure airports...'."\n";
2032
        		$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day);
2033
			if ($globalDebug) echo 'Order departure airports...'."\n";
2034
	        	$alldata = array();
2035
	        	
2036
    			foreach ($pall as $value) {
2037
	        		$icao = $value['airport_departure_icao'];
2038
    				$alldata[$icao] = $value;
2039
	        	}
2040
	        	foreach ($dall as $value) {
2041
    				$icao = $value['airport_departure_icao'];
2042
        			if (isset($alldata[$icao])) {
2043
    					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2044
        			} else $alldata[$icao] = $value;
2045
			}
2046
    			$count = array();
2047
    			foreach ($alldata as $key => $row) {
2048
    				$count[$key] = $row['airport_departure_icao_count'];
2049
        		}
2050
			array_multisort($count,SORT_DESC,$alldata);
2051
			foreach ($alldata as $number) {
2052
				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);
2053
			}
2054
			if ($globalDebug) echo 'Count all arrival airports...'."\n";
2055
			$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day);
2056
			if ($globalDebug) echo 'Count all detected arrival airports...'."\n";
2057
        		$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day);
2058
			if ($globalDebug) echo 'Order arrival airports...'."\n";
2059
	        	$alldata = array();
2060
    			foreach ($pall as $value) {
2061
	        		$icao = $value['airport_arrival_icao'];
2062
    				$alldata[$icao] = $value;
2063
	        	}
2064
	        	foreach ($dall as $value) {
2065
    				$icao = $value['airport_arrival_icao'];
2066
        			if (isset($alldata[$icao])) {
2067
        				$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2068
	        		} else $alldata[$icao] = $value;
2069
    			}
2070
        		$count = array();
2071
        		foreach ($alldata as $key => $row) {
2072
        			$count[$key] = $row['airport_arrival_icao_count'];
2073
	        	}
2074
    			array_multisort($count,SORT_DESC,$alldata);
2075
                        foreach ($alldata as $number) {
2076
				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);
2077
			}
2078
			if ($Connection->tableExists('countries')) {
2079
				if ($globalDebug) echo 'Count all flights by countries...'."\n";
2080
				//$SpotterArchive = new SpotterArchive();
2081
				//$alldata = $SpotterArchive->countAllFlightOverCountries(false,0,$last_update_day);
2082
				$Spotter = new Spotter($this->db);
2083
				$alldata = $Spotter->countAllFlightOverCountries(false,0,$last_update_day);
2084
				foreach ($alldata as $number) {
2085
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],'','',$reset);
2086
				}
2087
			}
2088
			
2089
			if (isset($globalAccidents) && $globalAccidents) {
2090
				if ($globalDebug) echo 'Count fatalities stats...'."\n";
2091
				$Accident = new Accident();
2092
				$this->deleteStatsByType('fatalities_byyear');
2093
				$alldata = $Accident->countFatalitiesByYear();
2094
				foreach ($alldata as $number) {
2095
					$this->addStat('fatalities_byyear',$number['count'],date('Y-m-d H:i:s',mktime(0,0,0,1,1,$number['year'])));
2096
				}
2097
				$this->deleteStatsByType('fatalities_bymonth');
2098
				$alldata = $Accident->countFatalitiesLast12Months();
2099
				foreach ($alldata as $number) {
2100
					$this->addStat('fatalities_bymonth',$number['count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month'],1,$number['year'])));
2101
				}
2102
			}
2103
2104
2105
			// Add by month using getstat if month finish...
2106
2107
			//if (date('m',strtotime($last_update_day)) != date('m')) {
2108
			if ($globalDebug) echo 'Count all flights by months...'."\n";
2109
			$last_month = date('Y-m-01 00:00:00', strtotime('-1 month', strtotime($last_update_day)));
2110
			$filter_last_month = array('since_date' => $last_month);
2111
			$Spotter = new Spotter($this->db);
2112
			$alldata = $Spotter->countAllMonths($filter_last_month);
2113
			$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...
2114
			foreach ($alldata as $number) {
2115
				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...
2116
				$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'])));
2117
			}
2118
			if ($globalDebug) echo 'Count all military flights by months...'."\n";
2119
			$alldata = $Spotter->countAllMilitaryMonths($filter_last_month);
2120
			foreach ($alldata as $number) {
2121
				$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'])));
2122
			}
2123
			if ($globalDebug) echo 'Count all owners by months...'."\n";
2124
			$alldata = $Spotter->countAllMonthsOwners($filter_last_month);
2125
			foreach ($alldata as $number) {
2126
				$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'])));
2127
			}
2128
			if ($globalDebug) echo 'Count all pilots by months...'."\n";
2129
			$alldata = $Spotter->countAllMonthsPilots($filter_last_month);
2130
			foreach ($alldata as $number) {
2131
				$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'])));
2132
			}
2133
			if ($globalDebug) echo 'Count all airlines by months...'."\n";
2134
			$alldata = $Spotter->countAllMonthsAirlines($filter_last_month);
2135
			foreach ($alldata as $number) {
2136
				$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'])));
2137
			}
2138
			if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
2139
			$alldata = $Spotter->countAllMonthsAircrafts($filter_last_month);
2140
			foreach ($alldata as $number) {
2141
				$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'])));
2142
			}
2143
			if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
2144
			$alldata = $Spotter->countAllMonthsRealArrivals($filter_last_month);
2145
			foreach ($alldata as $number) {
2146
				$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'])));
2147
			}
2148
			if ($globalDebug) echo 'Airports data...'."\n";
2149
			if ($globalDebug) echo '...Departure'."\n";
2150
			$this->deleteStatAirport('daily');
2151
//			$pall = $Spotter->getLast7DaysAirportsDeparture();
2152
  //      		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
2153
			$pall = $Spotter->getLast7DaysAirportsDeparture();
2154
        		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
2155
        		/*
2156
	        	$alldata = array();
2157
    			foreach ($pall as $value) {
2158
	        		$icao = $value['departure_airport_icao'];
2159
    				$alldata[$icao] = $value;
2160
	        	}
2161
	        	foreach ($dall as $value) {
2162
    				$icao = $value['departure_airport_icao'];
2163
    				$ddate = $value['date'];
2164
        			if (isset($alldata[$icao])) {
2165
        				$alldata[$icao]['departure_airport_count'] = $alldata[$icao]['departure_airport_count'] + $value['departure_airport_count'];
2166
	        		} else $alldata[$icao] = $value;
2167
    			}
2168
        		$count = array();
2169
        		foreach ($alldata as $key => $row) {
2170
        			$count[$key] = $row['departure_airport_count'];
2171
	        	}
2172
    			array_multisort($count,SORT_DESC,$alldata);
2173
    			*/
2174
    			foreach ($dall as $value) {
2175
    				$icao = $value['departure_airport_icao'];
2176
    				$ddate = $value['date'];
2177
    				$find = false;
2178
    				foreach ($pall as $pvalue) {
2179
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2180
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2181
    						$find = true;
2182
    						break;
2183
    					}
2184
    				}
2185
    				if ($find === false) {
2186
    					$pall[] = $value;
2187
    				}
2188
    			}
2189
    			$alldata = $pall;
2190
			foreach ($alldata as $number) {
2191
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count']);
2192
			}
2193
			echo '...Arrival'."\n";
2194
			$pall = $Spotter->getLast7DaysAirportsArrival();
2195
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrival();
2196
        		/*
2197
	        	$alldata = array();
2198
    			foreach ($pall as $value) {
2199
	        		$icao = $value['arrival_airport_icao'];
2200
    				$alldata[$icao] = $value;
2201
	        	}
2202
	        	foreach ($dall as $value) {
2203
    				$icao = $value['arrival_airport_icao'];
2204
        			if (isset($alldata[$icao])) {
2205
        				$alldata[$icao]['arrival_airport_icao_count'] = $alldata[$icao]['arrival_airport_count'] + $value['arrival_airport_count'];
2206
	        		} else $alldata[$icao] = $value;
2207
    			}
2208
        		$count = array();
2209
        		foreach ($alldata as $key => $row) {
2210
        			$count[$key] = $row['arrival_airport_count'];
2211
	        	}
2212
    			array_multisort($count,SORT_DESC,$alldata);
2213
    			*/
2214
2215
2216
    			foreach ($dall as $value) {
2217
    				$icao = $value['arrival_airport_icao'];
2218
    				$ddate = $value['date'];
2219
    				$find = false;
2220
    				foreach ($pall as $pvalue) {
2221
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2222
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2223
    						$find = true;
2224
    						break;
2225
    					}
2226
    				}
2227
    				if ($find === false) {
2228
    					$pall[] = $value;
2229
    				}
2230
    			}
2231
    			$alldata = $pall;
2232
			foreach ($alldata as $number) {
2233
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count']);
2234
			}
2235
2236
			echo 'Flights data...'."\n";
2237
			$this->deleteStatFlight('month');
2238
			echo '-> countAllDatesLastMonth...'."\n";
2239
			$alldata = $Spotter->countAllDatesLastMonth($filter_last_month);
2240
			foreach ($alldata as $number) {
2241
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
2242
			}
2243
			echo '-> countAllDates...'."\n";
2244
			$previousdata = $this->countAllDates();
2245
			$previousdatabyairlines = $this->countAllDatesByAirlines();
2246
			$this->deleteStatFlight('date');
2247
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter_last_month));
2248
			$values = array();
2249
			foreach ($alldata as $cnt) {
2250
				$values[] = $cnt['date_count'];
2251
			}
2252
			array_multisort($values,SORT_DESC,$alldata);
2253
			array_splice($alldata,11);
2254
			foreach ($alldata as $number) {
2255
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
2256
			}
2257
			
2258
			$this->deleteStatFlight('hour');
2259
			echo '-> countAllHours...'."\n";
2260
			$alldata = $Spotter->countAllHours('hour',$filter_last_month);
2261
			foreach ($alldata as $number) {
2262
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
2263
			}
2264
2265
2266
2267
			// Count by airlines
2268
			echo '--- Stats by airlines ---'."\n";
2269
			if ($Connection->tableExists('countries')) {
2270
				if ($globalDebug) echo 'Count all flights by countries by airlines...'."\n";
2271
				$SpotterArchive = new SpotterArchive();
2272
				//$Spotter = new Spotter($this->db);
2273
				$alldata = $SpotterArchive->countAllFlightOverCountriesByAirlines(false,0,$last_update_day);
2274
				//$alldata = $Spotter->countAllFlightOverCountriesByAirlines(false,0,$last_update_day);
2275
				foreach ($alldata as $number) {
2276
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],$number['airline_icao'],'',$reset);
2277
				}
2278
			}
2279
			if ($globalDebug) echo 'Count all aircraft types by airlines...'."\n";
2280
			$Spotter = new Spotter($this->db);
2281
			$alldata = $Spotter->countAllAircraftTypesByAirlines(false,0,$last_update_day);
2282
			foreach ($alldata as $number) {
2283
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],$number['airline_icao'],'',$reset);
2284
			}
2285
			if ($globalDebug) echo 'Count all aircraft registrations by airlines...'."\n";
2286
			$alldata = $Spotter->countAllAircraftRegistrationsByAirlines(false,0,$last_update_day);
2287
			foreach ($alldata as $number) {
2288
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],$number['airline_icao'],'',$reset);
2289
			}
2290
			if ($globalDebug) echo 'Count all callsigns by airlines...'."\n";
2291
			$alldata = $Spotter->countAllCallsignsByAirlines(false,0,$last_update_day);
2292
			foreach ($alldata as $number) {
2293
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
2294
			}
2295
			if ($globalDebug) echo 'Count all owners by airlines...'."\n";
2296
			$alldata = $Spotter->countAllOwnersByAirlines(false,0,$last_update_day);
2297
			foreach ($alldata as $number) {
2298
				$this->addStatOwner($number['owner_name'],$number['owner_count'],$number['airline_icao'],'',$reset);
2299
			}
2300
			if ($globalDebug) echo 'Count all pilots by airlines...'."\n";
2301
			$alldata = $Spotter->countAllPilotsByAirlines(false,0,$last_update_day);
2302
			foreach ($alldata as $number) {
2303
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],$number['airline_icao'],'',$number['format_source'],$reset);
2304
			}
2305
			if ($globalDebug) echo 'Count all departure airports by airlines...'."\n";
2306
			$pall = $Spotter->countAllDepartureAirportsByAirlines(false,0,$last_update_day);
2307
			if ($globalDebug) echo 'Count all detected departure airports by airlines...'."\n";
2308
       			$dall = $Spotter->countAllDetectedDepartureAirportsByAirlines(false,0,$last_update_day);
2309
			if ($globalDebug) echo 'Order detected departure airports by airlines...'."\n";
2310
	        	//$alldata = array();
2311
    			foreach ($dall as $value) {
2312
    				$icao = $value['airport_departure_icao'];
2313
    				$dicao = $value['airline_icao'];
2314
    				$find = false;
2315
    				foreach ($pall as $pvalue) {
2316
    					if ($pvalue['airport_departure_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
2317
    						$pvalue['airport_departure_icao_count'] = $pvalue['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2318
    						$find = true;
2319
    						break;
2320
    					}
2321
    				}
2322
    				if ($find === false) {
2323
    					$pall[] = $value;
2324
    				}
2325
    			}
2326
    			$alldata = $pall;
2327
			foreach ($alldata as $number) {
2328
				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);
2329
			}
2330
			if ($globalDebug) echo 'Count all arrival airports by airlines...'."\n";
2331
			$pall = $Spotter->countAllArrivalAirportsByAirlines(false,0,$last_update_day);
2332
			if ($globalDebug) echo 'Count all detected arrival airports by airlines...'."\n";
2333
        		$dall = $Spotter->countAllDetectedArrivalAirportsByAirlines(false,0,$last_update_day);
2334
			if ($globalDebug) echo 'Order arrival airports by airlines...'."\n";
2335
	        	//$alldata = array();
2336
    			foreach ($dall as $value) {
2337
    				$icao = $value['airport_arrival_icao'];
2338
    				$dicao = $value['airline_icao'];
2339
    				$find = false;
2340
    				foreach ($pall as $pvalue) {
2341
    					if ($pvalue['airport_arrival_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
2342
    						$pvalue['airport_arrival_icao_count'] = $pvalue['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2343
    						$find = true;
2344
    						break;
2345
    					}
2346
    				}
2347
    				if ($find === false) {
2348
    					$pall[] = $value;
2349
    				}
2350
    			}
2351
    			$alldata = $pall;
2352
                        foreach ($alldata as $number) {
2353
				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);
2354
			}
2355
			if ($globalDebug) echo 'Count all flights by months by airlines...'."\n";
2356
			$Spotter = new Spotter($this->db);
2357
			$alldata = $Spotter->countAllMonthsByAirlines($filter_last_month);
2358
			$lastyear = false;
2359
			foreach ($alldata as $number) {
2360
				if ($number['year_name'] != date('Y')) $lastyear = true;
2361
				$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']);
2362
			}
2363
			if ($globalDebug) echo 'Count all owners by months by airlines...'."\n";
2364
			$alldata = $Spotter->countAllMonthsOwnersByAirlines($filter_last_month);
2365
			foreach ($alldata as $number) {
2366
				$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']);
2367
			}
2368
			if ($globalDebug) echo 'Count all pilots by months by airlines...'."\n";
2369
			$alldata = $Spotter->countAllMonthsPilotsByAirlines($filter_last_month);
2370
			foreach ($alldata as $number) {
2371
				$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']);
2372
			}
2373
			if ($globalDebug) echo 'Count all aircrafts by months by airlines...'."\n";
2374
			$alldata = $Spotter->countAllMonthsAircraftsByAirlines($filter_last_month);
2375
			foreach ($alldata as $number) {
2376
				$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']);
2377
			}
2378
			if ($globalDebug) echo 'Count all real arrivals by months by airlines...'."\n";
2379
			$alldata = $Spotter->countAllMonthsRealArrivalsByAirlines($filter_last_month);
2380
			foreach ($alldata as $number) {
2381
				$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']);
2382
			}
2383
			if ($globalDebug) echo '...Departure'."\n";
2384
			$pall = $Spotter->getLast7DaysAirportsDepartureByAirlines();
2385
        		$dall = $Spotter->getLast7DaysDetectedAirportsDepartureByAirlines();
2386
    			foreach ($dall as $value) {
2387
    				$icao = $value['departure_airport_icao'];
2388
    				$airline = $value['airline_icao'];
2389
    				$ddate = $value['date'];
2390
    				$find = false;
2391
    				foreach ($pall as $pvalue) {
2392
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] = $airline) {
2393
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2394
    						$find = true;
2395
    						break;
2396
    					}
2397
    				}
2398
    				if ($find === false) {
2399
    					$pall[] = $value;
2400
    				}
2401
    			}
2402
    			$alldata = $pall;
2403
			foreach ($alldata as $number) {
2404
				$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']);
2405
			}
2406
			if ($globalDebug) echo '...Arrival'."\n";
2407
			$pall = $Spotter->getLast7DaysAirportsArrivalByAirlines();
2408
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrivalByAirlines();
2409
    			foreach ($dall as $value) {
2410
    				$icao = $value['arrival_airport_icao'];
2411
    				$airline = $value['airline_icao'];
2412
    				$ddate = $value['date'];
2413
    				$find = false;
2414
    				foreach ($pall as $pvalue) {
2415
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] == $airline) {
2416
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2417
    						$find = true;
2418
    						break;
2419
    					}
2420
    				}
2421
    				if ($find === false) {
2422
    					$pall[] = $value;
2423
    				}
2424
    			}
2425
    			$alldata = $pall;
2426
			foreach ($alldata as $number) {
2427
				$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']);
2428
			}
2429
2430
			if ($globalDebug) echo 'Flights data...'."\n";
2431
			if ($globalDebug) echo '-> countAllDatesLastMonth...'."\n";
2432
			$alldata = $Spotter->countAllDatesLastMonthByAirlines($filter_last_month);
2433
			foreach ($alldata as $number) {
2434
				$this->addStatFlight('month',$number['date_name'],$number['date_count'], $number['airline_icao']);
2435
			}
2436
			if ($globalDebug) echo '-> countAllDates...'."\n";
2437
			//$previousdata = $this->countAllDatesByAirlines();
2438
			$alldata = $Common->array_merge_noappend($previousdatabyairlines,$Spotter->countAllDatesByAirlines($filter_last_month));
2439
			$values = array();
2440
			foreach ($alldata as $cnt) {
2441
				$values[] = $cnt['date_count'];
2442
			}
2443
			array_multisort($values,SORT_DESC,$alldata);
2444
			array_splice($alldata,11);
2445
			foreach ($alldata as $number) {
2446
				$this->addStatFlight('date',$number['date_name'],$number['date_count'],$number['airline_icao']);
2447
			}
2448
			
2449
			if ($globalDebug) echo '-> countAllHours...'."\n";
2450
			$alldata = $Spotter->countAllHoursByAirlines('hour',$filter_last_month);
2451
			foreach ($alldata as $number) {
2452
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],$number['airline_icao']);
2453
			}
2454
			
2455
2456
			// Stats by filters
2457
			if (!isset($globalStatsFilters) || $globalStatsFilters == '') $globalStatsFilters = array();
2458
			foreach ($globalStatsFilters as $name => $filter) {
2459
				//$filter_name = $filter['name'];
2460
				$filter_name = $name;
2461
				$reset = false;
2462
				$last_update = $this->getLastStatsUpdate('last_update_stats_'.$filter_name);
2463
				if (isset($filter['resetall']) && isset($last_update[0]['value']) && strtotime($filter['resetall']) > strtotime($last_update[0]['value'])) {
2464
					if ($globalDebug) echo '!!! Delete stats for filter '.$filter_name.' !!!'."\n";
2465
					$this->deleteOldStats($filter_name);
2466
					unset($last_update);
2467
				}
2468
				if (isset($last_update[0]['value'])) {
2469
					$last_update_day = $last_update[0]['value'];
2470
				} else {
2471
					$last_update_day = '2012-12-12 12:12:12';
2472
					if (isset($filter['DeleteLastYearStats'])) {
2473
						$last_update_day = date('Y').'-01-01 00:00:00';
2474
					}
2475
				}
2476
				if (isset($filter['DeleteLastYearStats']) && date('Y',strtotime($last_update_day)) != date('Y')) {
2477
					$last_update_day = date('Y').'-01-01 00:00:00';
2478
					$reset = true;
2479
				}
2480
				
2481
2482
				// Count by filter
2483
				if ($globalDebug) echo '--- Stats for filter '.$filter_name.' ---'."\n";
2484
				$Spotter = new Spotter($this->db);
2485
				$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day,$filter);
2486
				foreach ($alldata as $number) {
2487
					$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'',$filter_name,$reset);
2488
				}
2489
				$alldata = $Spotter->countAllAirlines(false,0,$last_update_day,$filter);
2490
				foreach ($alldata as $number) {
2491
					$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],$filter_name,$reset);
2492
				}
2493
				$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day,$filter);
2494
				foreach ($alldata as $number) {
2495
					$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'',$filter_name,$reset);
2496
				}
2497
				$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day,$filter);
2498
				foreach ($alldata as $number) {
2499
					$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],'',$filter_name,$reset);
2500
				}
2501
				$alldata = $Spotter->countAllOwners(false,0,$last_update_day,$filter);
2502
				foreach ($alldata as $number) {
2503
					$this->addStatOwner($number['owner_name'],$number['owner_count'],'',$filter_name,$reset);
2504
				}
2505
				$alldata = $Spotter->countAllPilots(false,0,$last_update_day,$filter);
2506
				foreach ($alldata as $number) {
2507
					$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'',$filter_name,$number['format_source'],$reset);
2508
				}
2509
				$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day,$filter);
2510
	       			$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day,$filter);
2511
		        	$alldata = array();
2512
	    			foreach ($pall as $value) {
2513
		        		$icao = $value['airport_departure_icao'];
2514
    					$alldata[$icao] = $value;
2515
	    			}
2516
		        	foreach ($dall as $value) {
2517
	    				$icao = $value['airport_departure_icao'];
2518
        				if (isset($alldata[$icao])) {
2519
    						$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2520
        				} else $alldata[$icao] = $value;
2521
				}
2522
	    			$count = array();
2523
    				foreach ($alldata as $key => $row) {
2524
    					$count[$key] = $row['airport_departure_icao_count'];
2525
    				}
2526
				array_multisort($count,SORT_DESC,$alldata);
2527
				foreach ($alldata as $number) {
2528
    					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);
2529
				}
2530
				$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day,false,$filter);
2531
    				$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day,false,$filter);
2532
				$alldata = array();
2533
    				foreach ($pall as $value) {
2534
		        		$icao = $value['airport_arrival_icao'];
2535
    					$alldata[$icao] = $value;
2536
	    			}
2537
		        	foreach ($dall as $value) {
2538
	    				$icao = $value['airport_arrival_icao'];
2539
        				if (isset($alldata[$icao])) {
2540
        					$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2541
		        		} else $alldata[$icao] = $value;
2542
	    			}
2543
        			$count = array();
2544
        			foreach ($alldata as $key => $row) {
2545
    					$count[$key] = $row['airport_arrival_icao_count'];
2546
		        	}
2547
        			array_multisort($count,SORT_DESC,$alldata);
2548
				foreach ($alldata as $number) {
2549
					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);
2550
				}
2551
				$Spotter = new Spotter($this->db);
2552
				$alldata = $Spotter->countAllMonths($filter);
2553
				$lastyear = false;
2554
				foreach ($alldata as $number) {
2555
					if ($number['year_name'] != date('Y')) $lastyear = true;
2556
					$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);
2557
				}
2558
				$alldata = $Spotter->countAllMonthsOwners($filter);
2559
				foreach ($alldata as $number) {
2560
					$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);
2561
				}
2562
				$alldata = $Spotter->countAllMonthsPilots($filter);
2563
				foreach ($alldata as $number) {
2564
					$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);
2565
				}
2566
				$alldata = $Spotter->countAllMilitaryMonths($filter);
2567
				foreach ($alldata as $number) {
2568
					$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);
2569
				}
2570
				$alldata = $Spotter->countAllMonthsAircrafts($filter);
2571
				foreach ($alldata as $number) {
2572
					$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);
2573
				}
2574
				$alldata = $Spotter->countAllMonthsRealArrivals($filter);
2575
				foreach ($alldata as $number) {
2576
					$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);
2577
				}
2578
				echo '...Departure'."\n";
2579
				$pall = $Spotter->getLast7DaysAirportsDeparture('',$filter);
2580
        			$dall = $Spotter->getLast7DaysDetectedAirportsDeparture('',$filter);
2581
				foreach ($dall as $value) {
2582
    					$icao = $value['departure_airport_icao'];
2583
    					$ddate = $value['date'];
2584
    					$find = false;
2585
    					foreach ($pall as $pvalue) {
2586
    						if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2587
    							$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2588
	    						$find = true;
2589
    							break;
2590
    						}
2591
    					}
2592
    					if ($find === false) {
2593
    						$pall[] = $value;
2594
	    				}
2595
    				}
2596
	    			$alldata = $pall;
2597
				foreach ($alldata as $number) {
2598
					$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);
2599
				}
2600
				echo '...Arrival'."\n";
2601
				$pall = $Spotter->getLast7DaysAirportsArrival('',$filter);
2602
    				$dall = $Spotter->getLast7DaysDetectedAirportsArrival('',$filter);
2603
				foreach ($dall as $value) {
2604
					$icao = $value['arrival_airport_icao'];
2605
					$ddate = $value['date'];
2606
    					$find = false;
2607
					foreach ($pall as $pvalue) {
2608
    						if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2609
    							$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2610
    							$find = true;
2611
    							break;
2612
	    					}
2613
    					}
2614
    					if ($find === false) {
2615
    						$pall[] = $value;
2616
	    				}
2617
    				}
2618
    				$alldata = $pall;
2619
				foreach ($alldata as $number) {
2620
					$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);
2621
				}
2622
    
2623
				echo 'Flights data...'."\n";
2624
				echo '-> countAllDatesLastMonth...'."\n";
2625
				$alldata = $Spotter->countAllDatesLastMonth($filter);
2626
				foreach ($alldata as $number) {
2627
					$this->addStatFlight('month',$number['date_name'],$number['date_count'], '',$filter_name);
2628
				}
2629
				echo '-> countAllDates...'."\n";
2630
				$previousdata = $this->countAllDates('',$filter_name);
2631
				$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter));
2632
				$values = array();
2633
				foreach ($alldata as $cnt) {
2634
					$values[] = $cnt['date_count'];
2635
				}
2636
				array_multisort($values,SORT_DESC,$alldata);
2637
				array_splice($alldata,11);
2638
				foreach ($alldata as $number) {
2639
					$this->addStatFlight('date',$number['date_name'],$number['date_count'],'',$filter_name);
2640
				}
2641
				
2642
				echo '-> countAllHours...'."\n";
2643
				$alldata = $Spotter->countAllHours('hour',$filter);
2644
				foreach ($alldata as $number) {
2645
					$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],'',$filter_name);
2646
				}
2647
				echo 'Insert last stats update date...'."\n";
2648
				date_default_timezone_set('UTC');
2649
				$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y-m-d G:i:s'));
2650
				if (isset($filter['DeleteLastYearStats']) && $filter['DeleteLastYearStats'] == true) {
2651
					if (date('Y',strtotime($last_update_day)) != date('Y')) {
2652
						$this->deleteOldStats($filter_name);
2653
						$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y').'-01-01 00:00:00');
2654
					}
2655
				}
2656
2657
			}
2658
	
2659
2660
			// Last year stats
2661
			if ($lastyear) {
2662
				echo 'Data from last year...'."\n";
2663
				// SUM all previous month to put as year
2664
				$previous_year = date('Y');
2665
				$previous_year--;
2666
				$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2667
				$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2668
				$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2669
				$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2670
				$allairlines = $this->getAllAirlineNames();
2671
				foreach ($allairlines as $data) {
2672
					$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2673
					$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2674
					$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2675
					$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2676
				}
2677
				
2678
				if (isset($globalArchiveYear) && $globalArchiveYear) {
2679
					if ($globalArchive) {
2680
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
2681
						try {
2682
							$sth = $this->db->prepare($query);
2683
							$sth->execute();
2684
						} catch(PDOException $e) {
2685
							return "error : ".$e->getMessage().' - query : '.$query."\n";
2686
						}
2687
					}
2688
					echo 'Delete old data'."\n";
2689
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
2690
					try {
2691
						$sth = $this->db->prepare($query);
2692
						$sth->execute();
2693
					} catch(PDOException $e) {
2694
						return "error : ".$e->getMessage().' - query : '.$query."\n";
2695
					}
2696
				}
2697
				if (isset($globalDeleteLastYearStats) && $globalDeleteLastYearStats) {
2698
					$last_update = $this->getLastStatsUpdate('last_update_stats');
2699
					if (date('Y',strtotime($last_update[0]['value'])) != date('Y')) {
2700
						$this->deleteOldStats();
2701
						$this->addLastStatsUpdate('last_update_stats',date('Y').'-01-01 00:00:00');
2702
						$lastyearupdate = true;
2703
					}
2704
				}
2705
			}
2706
			if ($globalArchiveMonths > 0) {
2707
				if ($globalArchive) {
2708
					echo 'Archive old data...'."\n";
2709
					if ($globalDBdriver == 'mysql') {
2710
						//$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2711
						$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)
2712
							    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
2713
	    						     FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2714
					} else {
2715
						$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)
2716
							     SELECT 
2717
								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
2718
							    FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
2719
					}
2720
					try {
2721
						$sth = $this->db->prepare($query);
2722
						$sth->execute();
2723
					} catch(PDOException $e) {
2724
						return "error : ".$e->getMessage();
2725
					}
2726
				}
2727
				echo 'Deleting old data...'."\n";
2728
				//$query = 'DELETE FROM spotter_output WHERE spotter_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveMonths.' MONTH)';
2729
				if ($globalDBdriver == 'mysql') {
2730
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
2731
				} else {
2732
					$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)";
2733
				}
2734
				try {
2735
					$sth = $this->db->prepare($query);
2736
					$sth->execute();
2737
				} catch(PDOException $e) {
2738
					return "error : ".$e->getMessage();
2739
				}
2740
			}
2741
			if (!isset($lastyearupdate)) {
2742
				echo 'Insert last stats update date...'."\n";
2743
				date_default_timezone_set('UTC');
2744
				$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
2745
			}
2746
			if ($globalStatsResetYear) {
2747
				require_once(dirname(__FILE__).'/../install/class.settings.php');
2748
				settings::modify_settings(array('globalStatsResetYear' => 'FALSE'));
2749
			}
2750
2751
		//}
2752
	}
2753
}
2754
2755
?>