Completed
Push — master ( b49092...cf2135 )
by Yannick
30:03
created

Stats::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

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

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

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

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

Loading history...
2131
				}
2132
			}
2133
			if ($globalDebug) echo 'Count all vessels by months...'."\n";
2134
			$last_month = date('Y-m-01 00:00:00', strtotime('-1 month', strtotime($last_update_day)));
2135
			$filter_last_month = array('since_date' => $last_month);
2136
			$alldata = $Marine->countAllMonths($filter_last_month);
2137
			$lastyear = false;
2138
			foreach ($alldata as $number) {
2139
				if ($number['year_name'] != date('Y')) $lastyear = true;
2140
				$this->addStat('marine_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
2141
			}
2142
			echo 'Marine data...'."\n";
2143
			$this->deleteStatMarine('month');
2144
			echo '-> countAllDatesLastMonth...'."\n";
2145
			$alldata = $Marine->countAllDatesLastMonth($filter_last_month);
2146
			foreach ($alldata as $number) {
2147
				$this->addStatMarine('month',$number['date_name'],$number['date_count']);
2148
			}
2149
			echo '-> countAllDates...'."\n";
2150
			$previousdata = $this->countAllDatesMarine();
2151
			$this->deleteStatMarine('date');
2152
			$alldata = $Common->array_merge_noappend($previousdata,$Marine->countAllDates($filter_last_month));
2153
			$values = array();
2154
			foreach ($alldata as $cnt) {
2155
				$values[] = $cnt['date_count'];
2156
			}
2157
			array_multisort($values,SORT_DESC,$alldata);
2158
			array_splice($alldata,11);
2159
			foreach ($alldata as $number) {
2160
				$this->addStatMarine('date',$number['date_name'],$number['date_count']);
2161
			}
2162
			
2163
			$this->deleteStatMarine('hour');
2164
			echo '-> countAllHours...'."\n";
2165
			$alldata = $Marine->countAllHours('hour',$filter_last_month);
2166
			foreach ($alldata as $number) {
2167
				$this->addStatMarine('hour',$number['hour_name'],$number['hour_count']);
2168
			}
2169
			echo 'Insert last stats update date...'."\n";
2170
			date_default_timezone_set('UTC');
2171
			$this->addLastStatsUpdate('last_update_stats_marine',date('Y-m-d G:i:s'));
2172
		}
2173
		if (isset($globalTracker) && $globalTracker) {
2174
			$last_update = $this->getLastStatsUpdate('last_update_stats_tracker');
2175
			if ($globalDebug) echo '!!! Update tracker stats !!!'."\n";
2176
			if (isset($last_update[0]['value'])) {
2177
				$last_update_day = $last_update[0]['value'];
2178
			} else $last_update_day = '2012-12-12 12:12:12';
2179
			$reset = false;
2180
			$Tracker = new Tracker($this->db);
2181
			if ($Connection->tableExists('countries')) {
2182
				if ($globalDebug) echo 'Count all trackers by countries...'."\n";
2183
				$alldata = $Tracker->countAllTrackerOverCountries(false,0,$last_update_day);
2184
				foreach ($alldata as $number) {
2185
					$this->addStatCountryTracker($number['tracker_country_iso2'],$number['tracker_country_iso3'],$number['tracker_country'],$number['tracker_count'],'','',$reset);
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

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

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

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

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

Loading history...
2186
				}
2187
			}
2188
			if ($globalDebug) echo 'Count all vessels by months...'."\n";
2189
			$last_month = date('Y-m-01 00:00:00', strtotime('-1 month', strtotime($last_update_day)));
2190
			$filter_last_month = array('since_date' => $last_month);
2191
			$alldata = $Tracker->countAllMonths($filter_last_month);
2192
			$lastyear = false;
2193
			foreach ($alldata as $number) {
2194
				if ($number['year_name'] != date('Y')) $lastyear = true;
2195
				$this->addStat('tracker_bymonth',$number['date_count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month_name'],1,$number['year_name'])));
2196
			}
2197
			echo 'Tracker data...'."\n";
2198
			$this->deleteStatTracker('month');
2199
			echo '-> countAllDatesLastMonth...'."\n";
2200
			$alldata = $Tracker->countAllDatesLastMonth($filter_last_month);
2201
			foreach ($alldata as $number) {
2202
				$this->addStatTracker('month',$number['date_name'],$number['date_count']);
2203
			}
2204
			echo '-> countAllDates...'."\n";
2205
			$previousdata = $this->countAllDatesTracker();
2206
			$this->deleteStatTracker('date');
2207
			$alldata = $Common->array_merge_noappend($previousdata,$Tracker->countAllDates($filter_last_month));
2208
			$values = array();
2209
			foreach ($alldata as $cnt) {
2210
				$values[] = $cnt['date_count'];
2211
			}
2212
			array_multisort($values,SORT_DESC,$alldata);
2213
			array_splice($alldata,11);
2214
			foreach ($alldata as $number) {
2215
				$this->addStatTracker('date',$number['date_name'],$number['date_count']);
2216
			}
2217
			
2218
			$this->deleteStatTracker('hour');
2219
			echo '-> countAllHours...'."\n";
2220
			$alldata = $Tracker->countAllHours('hour',$filter_last_month);
2221
			foreach ($alldata as $number) {
2222
				$this->addStatTracker('hour',$number['hour_name'],$number['hour_count']);
2223
			}
2224
			echo 'Insert last stats update date...'."\n";
2225
			date_default_timezone_set('UTC');
2226
			$this->addLastStatsUpdate('last_update_stats_tracker',date('Y-m-d G:i:s'));
2227
		}
2228
2229
		if (isset($globalAircraft) && $globalAircraft) {
2230
			$last_update = $this->getLastStatsUpdate('last_update_stats');
2231
			if ($globalDebug) echo '!!! Update aicraft stats !!!'."\n";
2232
			if (isset($last_update[0]['value'])) {
2233
				$last_update_day = $last_update[0]['value'];
2234
			} else $last_update_day = '2012-12-12 12:12:12';
2235
			$reset = false;
2236
			//if ($globalStatsResetYear && date('Y',strtotime($last_update_day)) != date('Y')) {
2237
			if ($globalStatsResetYear) {
2238
				$reset = true;
2239
				$last_update_day = date('Y').'-01-01 00:00:00';
2240
			}
2241
			$Spotter = new Spotter($this->db);
2242
2243
			if ($globalDebug) echo 'Count all aircraft types...'."\n";
2244
			$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day);
2245
			foreach ($alldata as $number) {
2246
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'','',$reset);
2247
			}
2248
			if ($globalDebug) echo 'Count all airlines...'."\n";
2249
			$alldata = $Spotter->countAllAirlines(false,0,$last_update_day);
2250
			foreach ($alldata as $number) {
2251
				$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],'',$reset);
2252
			}
2253
			if ($globalDebug) echo 'Count all registrations...'."\n";
2254
			$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day);
2255
			foreach ($alldata as $number) {
2256
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'','',$reset);
2257
			}
2258
			if ($globalDebug) echo 'Count all callsigns...'."\n";
2259
			$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day);
2260
			foreach ($alldata as $number) {
2261
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
2262
			}
2263
			if ($globalDebug) echo 'Count all owners...'."\n";
2264
			$alldata = $Spotter->countAllOwners(false,0,$last_update_day);
2265
			foreach ($alldata as $number) {
2266
				$this->addStatOwner($number['owner_name'],$number['owner_count'],'','',$reset);
2267
			}
2268
			if ($globalDebug) echo 'Count all pilots...'."\n";
2269
			$alldata = $Spotter->countAllPilots(false,0,$last_update_day);
2270
			foreach ($alldata as $number) {
2271
				if ($number['pilot_id'] == 0 || $number['pilot_id'] == '') $number['pilot_id'] = $number['pilot_name'];
2272
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'','',$number['format_source'],$reset);
2273
			}
2274
			
2275
			if ($globalDebug) echo 'Count all departure airports...'."\n";
2276
			$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day);
2277
			if ($globalDebug) echo 'Count all detected departure airports...'."\n";
2278
			$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day);
2279
			if ($globalDebug) echo 'Order departure airports...'."\n";
2280
			$alldata = array();
2281
			foreach ($pall as $value) {
2282
				$icao = $value['airport_departure_icao'];
2283
				$alldata[$icao] = $value;
2284
			}
2285
			foreach ($dall as $value) {
2286
				$icao = $value['airport_departure_icao'];
2287
				if (isset($alldata[$icao])) {
2288
					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2289
				} else $alldata[$icao] = $value;
2290
			}
2291
			$count = array();
2292
			foreach ($alldata as $key => $row) {
2293
				$count[$key] = $row['airport_departure_icao_count'];
2294
			}
2295
			array_multisort($count,SORT_DESC,$alldata);
2296
			foreach ($alldata as $number) {
2297
				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);
2298
			}
2299
			if ($globalDebug) echo 'Count all arrival airports...'."\n";
2300
			$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day);
2301
			if ($globalDebug) echo 'Count all detected arrival airports...'."\n";
2302
			$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day);
2303
			if ($globalDebug) echo 'Order arrival airports...'."\n";
2304
			$alldata = array();
2305
			foreach ($pall as $value) {
2306
				$icao = $value['airport_arrival_icao'];
2307
				$alldata[$icao] = $value;
2308
			}
2309
			foreach ($dall as $value) {
2310
				$icao = $value['airport_arrival_icao'];
2311
				if (isset($alldata[$icao])) {
2312
					$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2313
				} else $alldata[$icao] = $value;
2314
			}
2315
			$count = array();
2316
			foreach ($alldata as $key => $row) {
2317
				$count[$key] = $row['airport_arrival_icao_count'];
2318
			}
2319
			array_multisort($count,SORT_DESC,$alldata);
2320
			foreach ($alldata as $number) {
2321
				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);
2322
			}
2323
			if ($Connection->tableExists('countries')) {
2324
				if ($globalDebug) echo 'Count all flights by countries...'."\n";
2325
				//$SpotterArchive = new SpotterArchive();
2326
				//$alldata = $SpotterArchive->countAllFlightOverCountries(false,0,$last_update_day);
2327
				$Spotter = new Spotter($this->db);
2328
				$alldata = $Spotter->countAllFlightOverCountries(false,0,$last_update_day);
2329
				foreach ($alldata as $number) {
2330
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],'','',$reset);
2331
				}
2332
			}
2333
			
2334
			if (isset($globalAccidents) && $globalAccidents) {
2335
				if ($globalDebug) echo 'Count fatalities stats...'."\n";
2336
				$Accident = new Accident($this->db);
2337
				$this->deleteStatsByType('fatalities_byyear');
2338
				$alldata = $Accident->countFatalitiesByYear();
2339
				foreach ($alldata as $number) {
2340
					$this->addStat('fatalities_byyear',$number['count'],date('Y-m-d H:i:s',mktime(0,0,0,1,1,$number['year'])));
2341
				}
2342
				$this->deleteStatsByType('fatalities_bymonth');
2343
				$alldata = $Accident->countFatalitiesLast12Months();
2344
				foreach ($alldata as $number) {
2345
					$this->addStat('fatalities_bymonth',$number['count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month'],1,$number['year'])));
2346
				}
2347
			}
2348
2349
			// Add by month using getstat if month finish...
2350
			//if (date('m',strtotime($last_update_day)) != date('m')) {
2351
			if ($globalDebug) echo 'Count all flights by months...'."\n";
2352
			$last_month = date('Y-m-01 00:00:00', strtotime('-1 month', strtotime($last_update_day)));
2353
			$filter_last_month = array('since_date' => $last_month);
2354
			$Spotter = new Spotter($this->db);
2355
			$alldata = $Spotter->countAllMonths($filter_last_month);
2356
			$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...
2357
			foreach ($alldata as $number) {
2358
				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...
2359
				$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'])));
2360
			}
2361
			if ($globalDebug) echo 'Count all military flights by months...'."\n";
2362
			$alldata = $Spotter->countAllMilitaryMonths($filter_last_month);
2363
			foreach ($alldata as $number) {
2364
				$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'])));
2365
			}
2366
			if ($globalDebug) echo 'Count all owners by months...'."\n";
2367
			$alldata = $Spotter->countAllMonthsOwners($filter_last_month);
2368
			foreach ($alldata as $number) {
2369
				$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'])));
2370
			}
2371
			if ($globalDebug) echo 'Count all pilots by months...'."\n";
2372
			$alldata = $Spotter->countAllMonthsPilots($filter_last_month);
2373
			foreach ($alldata as $number) {
2374
				$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'])));
2375
			}
2376
			if ($globalDebug) echo 'Count all airlines by months...'."\n";
2377
			$alldata = $Spotter->countAllMonthsAirlines($filter_last_month);
2378
			foreach ($alldata as $number) {
2379
				$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'])));
2380
			}
2381
			if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
2382
			$alldata = $Spotter->countAllMonthsAircrafts($filter_last_month);
2383
			foreach ($alldata as $number) {
2384
				$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'])));
2385
			}
2386
			if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
2387
			$alldata = $Spotter->countAllMonthsRealArrivals($filter_last_month);
2388
			foreach ($alldata as $number) {
2389
				$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'])));
2390
			}
2391
			if ($globalDebug) echo 'Airports data...'."\n";
2392
			if ($globalDebug) echo '...Departure'."\n";
2393
			$this->deleteStatAirport('daily');
2394
//			$pall = $Spotter->getLast7DaysAirportsDeparture();
2395
  //      		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
2396
			$pall = $Spotter->getLast7DaysAirportsDeparture();
2397
			$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
2398
			/*
2399
			$alldata = array();
2400
			foreach ($pall as $value) {
2401
				$icao = $value['departure_airport_icao'];
2402
				$alldata[$icao] = $value;
2403
			}
2404
			foreach ($dall as $value) {
2405
				$icao = $value['departure_airport_icao'];
2406
				$ddate = $value['date'];
2407
				if (isset($alldata[$icao])) {
2408
					$alldata[$icao]['departure_airport_count'] = $alldata[$icao]['departure_airport_count'] + $value['departure_airport_count'];
2409
				} else $alldata[$icao] = $value;
2410
			}
2411
			$count = array();
2412
			foreach ($alldata as $key => $row) {
2413
				$count[$key] = $row['departure_airport_count'];
2414
			}
2415
			array_multisort($count,SORT_DESC,$alldata);
2416
			*/
2417
			foreach ($dall as $value) {
2418
				$icao = $value['departure_airport_icao'];
2419
				$ddate = $value['date'];
2420
				$find = false;
2421
				foreach ($pall as $pvalue) {
2422
					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2423
						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2424
						$find = true;
2425
						break;
2426
					}
2427
				}
2428
				if ($find === false) {
2429
					$pall[] = $value;
2430
				}
2431
			}
2432
			$alldata = $pall;
2433
			foreach ($alldata as $number) {
2434
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count']);
2435
			}
2436
			echo '...Arrival'."\n";
2437
			$pall = $Spotter->getLast7DaysAirportsArrival();
2438
			$dall = $Spotter->getLast7DaysDetectedAirportsArrival();
2439
			/*
2440
			$alldata = array();
2441
			foreach ($pall as $value) {
2442
				$icao = $value['arrival_airport_icao'];
2443
				$alldata[$icao] = $value;
2444
			}
2445
			foreach ($dall as $value) {
2446
				$icao = $value['arrival_airport_icao'];
2447
				if (isset($alldata[$icao])) {
2448
					$alldata[$icao]['arrival_airport_icao_count'] = $alldata[$icao]['arrival_airport_count'] + $value['arrival_airport_count'];
2449
				} else $alldata[$icao] = $value;
2450
			}
2451
			$count = array();
2452
			foreach ($alldata as $key => $row) {
2453
				$count[$key] = $row['arrival_airport_count'];
2454
			}
2455
			array_multisort($count,SORT_DESC,$alldata);
2456
			*/
2457
2458
			foreach ($dall as $value) {
2459
				$icao = $value['arrival_airport_icao'];
2460
				$ddate = $value['date'];
2461
				$find = false;
2462
				foreach ($pall as $pvalue) {
2463
					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2464
						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2465
						$find = true;
2466
						break;
2467
					}
2468
				}
2469
				if ($find === false) {
2470
						$pall[] = $value;
2471
				}
2472
			}
2473
			$alldata = $pall;
2474
			foreach ($alldata as $number) {
2475
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count']);
2476
			}
2477
2478
			echo 'Flights data...'."\n";
2479
			$this->deleteStatFlight('month');
2480
			echo '-> countAllDatesLastMonth...'."\n";
2481
			$alldata = $Spotter->countAllDatesLastMonth($filter_last_month);
2482
			foreach ($alldata as $number) {
2483
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
2484
			}
2485
			echo '-> countAllDates...'."\n";
2486
			$previousdata = $this->countAllDates();
2487
			$previousdatabyairlines = $this->countAllDatesByAirlines();
2488
			$this->deleteStatFlight('date');
2489
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter_last_month));
2490
			$values = array();
2491
			foreach ($alldata as $cnt) {
2492
				$values[] = $cnt['date_count'];
2493
			}
2494
			array_multisort($values,SORT_DESC,$alldata);
2495
			array_splice($alldata,11);
2496
			foreach ($alldata as $number) {
2497
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
2498
			}
2499
			
2500
			$this->deleteStatFlight('hour');
2501
			echo '-> countAllHours...'."\n";
2502
			$alldata = $Spotter->countAllHours('hour',$filter_last_month);
2503
			foreach ($alldata as $number) {
2504
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
2505
			}
2506
2507
			// Count by airlines
2508
			echo '--- Stats by airlines ---'."\n";
2509
			if ($Connection->tableExists('countries')) {
2510
				if ($globalDebug) echo 'Count all flights by countries by airlines...'."\n";
2511
				$SpotterArchive = new SpotterArchive($this->db);
2512
				//$Spotter = new Spotter($this->db);
2513
				$alldata = $SpotterArchive->countAllFlightOverCountriesByAirlines(false,0,$last_update_day);
2514
				//$alldata = $Spotter->countAllFlightOverCountriesByAirlines(false,0,$last_update_day);
2515
				foreach ($alldata as $number) {
2516
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],$number['airline_icao'],'',$reset);
2517
				}
2518
			}
2519
			if ($globalDebug) echo 'Count all aircraft types by airlines...'."\n";
2520
			$Spotter = new Spotter($this->db);
2521
			$alldata = $Spotter->countAllAircraftTypesByAirlines(false,0,$last_update_day);
2522
			foreach ($alldata as $number) {
2523
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],$number['airline_icao'],'',$reset);
2524
			}
2525
			if ($globalDebug) echo 'Count all aircraft registrations by airlines...'."\n";
2526
			$alldata = $Spotter->countAllAircraftRegistrationsByAirlines(false,0,$last_update_day);
2527
			foreach ($alldata as $number) {
2528
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],$number['airline_icao'],'',$reset);
2529
			}
2530
			if ($globalDebug) echo 'Count all callsigns by airlines...'."\n";
2531
			$alldata = $Spotter->countAllCallsignsByAirlines(false,0,$last_update_day);
2532
			foreach ($alldata as $number) {
2533
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
2534
			}
2535
			if ($globalDebug) echo 'Count all owners by airlines...'."\n";
2536
			$alldata = $Spotter->countAllOwnersByAirlines(false,0,$last_update_day);
2537
			foreach ($alldata as $number) {
2538
				$this->addStatOwner($number['owner_name'],$number['owner_count'],$number['airline_icao'],'',$reset);
2539
			}
2540
			if ($globalDebug) echo 'Count all pilots by airlines...'."\n";
2541
			$alldata = $Spotter->countAllPilotsByAirlines(false,0,$last_update_day);
2542
			foreach ($alldata as $number) {
2543
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],$number['airline_icao'],'',$number['format_source'],$reset);
2544
			}
2545
			if ($globalDebug) echo 'Count all departure airports by airlines...'."\n";
2546
			$pall = $Spotter->countAllDepartureAirportsByAirlines(false,0,$last_update_day);
2547
			if ($globalDebug) echo 'Count all detected departure airports by airlines...'."\n";
2548
			$dall = $Spotter->countAllDetectedDepartureAirportsByAirlines(false,0,$last_update_day);
2549
			if ($globalDebug) echo 'Order detected departure airports by airlines...'."\n";
2550
			//$alldata = array();
2551
			foreach ($dall as $value) {
2552
				$icao = $value['airport_departure_icao'];
2553
				$dicao = $value['airline_icao'];
2554
				$find = false;
2555
				foreach ($pall as $pvalue) {
2556
					if ($pvalue['airport_departure_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
2557
						$pvalue['airport_departure_icao_count'] = $pvalue['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2558
						$find = true;
2559
						break;
2560
					}
2561
				}
2562
				if ($find === false) {
2563
					$pall[] = $value;
2564
				}
2565
			}
2566
			$alldata = $pall;
2567
			foreach ($alldata as $number) {
2568
				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);
2569
			}
2570
			if ($globalDebug) echo 'Count all arrival airports by airlines...'."\n";
2571
			$pall = $Spotter->countAllArrivalAirportsByAirlines(false,0,$last_update_day);
2572
			if ($globalDebug) echo 'Count all detected arrival airports by airlines...'."\n";
2573
			$dall = $Spotter->countAllDetectedArrivalAirportsByAirlines(false,0,$last_update_day);
2574
			if ($globalDebug) echo 'Order arrival airports by airlines...'."\n";
2575
			//$alldata = array();
2576
			foreach ($dall as $value) {
2577
				$icao = $value['airport_arrival_icao'];
2578
				$dicao = $value['airline_icao'];
2579
				$find = false;
2580
				foreach ($pall as $pvalue) {
2581
					if ($pvalue['airport_arrival_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
2582
						$pvalue['airport_arrival_icao_count'] = $pvalue['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2583
						$find = true;
2584
						break;
2585
					}
2586
				}
2587
				if ($find === false) {
2588
					$pall[] = $value;
2589
				}
2590
			}
2591
			$alldata = $pall;
2592
			foreach ($alldata as $number) {
2593
				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);
2594
			}
2595
			if ($globalDebug) echo 'Count all flights by months by airlines...'."\n";
2596
			$Spotter = new Spotter($this->db);
2597
			$alldata = $Spotter->countAllMonthsByAirlines($filter_last_month);
2598
			$lastyear = false;
2599
			foreach ($alldata as $number) {
2600
				if ($number['year_name'] != date('Y')) $lastyear = true;
2601
				$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']);
2602
			}
2603
			if ($globalDebug) echo 'Count all owners by months by airlines...'."\n";
2604
			$alldata = $Spotter->countAllMonthsOwnersByAirlines($filter_last_month);
2605
			foreach ($alldata as $number) {
2606
				$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']);
2607
			}
2608
			if ($globalDebug) echo 'Count all pilots by months by airlines...'."\n";
2609
			$alldata = $Spotter->countAllMonthsPilotsByAirlines($filter_last_month);
2610
			foreach ($alldata as $number) {
2611
				$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']);
2612
			}
2613
			if ($globalDebug) echo 'Count all aircrafts by months by airlines...'."\n";
2614
			$alldata = $Spotter->countAllMonthsAircraftsByAirlines($filter_last_month);
2615
			foreach ($alldata as $number) {
2616
				$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']);
2617
			}
2618
			if ($globalDebug) echo 'Count all real arrivals by months by airlines...'."\n";
2619
			$alldata = $Spotter->countAllMonthsRealArrivalsByAirlines($filter_last_month);
2620
			foreach ($alldata as $number) {
2621
				$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']);
2622
			}
2623
			if ($globalDebug) echo '...Departure'."\n";
2624
			$pall = $Spotter->getLast7DaysAirportsDepartureByAirlines();
2625
			$dall = $Spotter->getLast7DaysDetectedAirportsDepartureByAirlines();
2626
			foreach ($dall as $value) {
2627
				$icao = $value['departure_airport_icao'];
2628
				$airline = $value['airline_icao'];
2629
				$ddate = $value['date'];
2630
				$find = false;
2631
				foreach ($pall as $pvalue) {
2632
					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] = $airline) {
2633
						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2634
						$find = true;
2635
						break;
2636
					}
2637
				}
2638
				if ($find === false) {
2639
					$pall[] = $value;
2640
				}
2641
			}
2642
			$alldata = $pall;
2643
			foreach ($alldata as $number) {
2644
				$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']);
2645
			}
2646
			if ($globalDebug) echo '...Arrival'."\n";
2647
			$pall = $Spotter->getLast7DaysAirportsArrivalByAirlines();
2648
			$dall = $Spotter->getLast7DaysDetectedAirportsArrivalByAirlines();
2649
			foreach ($dall as $value) {
2650
				$icao = $value['arrival_airport_icao'];
2651
				$airline = $value['airline_icao'];
2652
				$ddate = $value['date'];
2653
				$find = false;
2654
				foreach ($pall as $pvalue) {
2655
					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] == $airline) {
2656
						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2657
						$find = true;
2658
						break;
2659
					}
2660
				}
2661
				if ($find === false) {
2662
					$pall[] = $value;
2663
				}
2664
			}
2665
			$alldata = $pall;
2666
			foreach ($alldata as $number) {
2667
				$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']);
2668
			}
2669
2670
			if ($globalDebug) echo 'Flights data...'."\n";
2671
			if ($globalDebug) echo '-> countAllDatesLastMonth...'."\n";
2672
			$alldata = $Spotter->countAllDatesLastMonthByAirlines($filter_last_month);
2673
			foreach ($alldata as $number) {
2674
				$this->addStatFlight('month',$number['date_name'],$number['date_count'], $number['airline_icao']);
2675
			}
2676
			if ($globalDebug) echo '-> countAllDates...'."\n";
2677
			//$previousdata = $this->countAllDatesByAirlines();
2678
			$alldata = $Common->array_merge_noappend($previousdatabyairlines,$Spotter->countAllDatesByAirlines($filter_last_month));
2679
			$values = array();
2680
			foreach ($alldata as $cnt) {
2681
				$values[] = $cnt['date_count'];
2682
			}
2683
			array_multisort($values,SORT_DESC,$alldata);
2684
			array_splice($alldata,11);
2685
			foreach ($alldata as $number) {
2686
				$this->addStatFlight('date',$number['date_name'],$number['date_count'],$number['airline_icao']);
2687
			}
2688
			
2689
			if ($globalDebug) echo '-> countAllHours...'."\n";
2690
			$alldata = $Spotter->countAllHoursByAirlines('hour',$filter_last_month);
2691
			foreach ($alldata as $number) {
2692
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],$number['airline_icao']);
2693
			}
2694
2695
			// Stats by filters
2696
			if (!isset($globalStatsFilters) || $globalStatsFilters == '') $globalStatsFilters = array();
2697
			foreach ($globalStatsFilters as $name => $filter) {
2698
				if (!empty($filter)) {
2699
					//$filter_name = $filter['name'];
2700
					$filter_name = $name;
2701
					$reset = false;
2702
					$last_update = $this->getLastStatsUpdate('last_update_stats_'.$filter_name);
2703
					if (isset($filter['resetall']) && isset($last_update[0]['value']) && strtotime($filter['resetall']) > strtotime($last_update[0]['value'])) {
2704
						if ($globalDebug) echo '!!! Delete stats for filter '.$filter_name.' !!!'."\n";
2705
						$this->deleteOldStats($filter_name);
2706
						unset($last_update);
2707
					}
2708
					if (isset($last_update[0]['value'])) {
2709
						$last_update_day = $last_update[0]['value'];
2710
					} else {
2711
						$last_update_day = '2012-12-12 12:12:12';
2712
						if (isset($filter['DeleteLastYearStats'])) {
2713
							$last_update_day = date('Y').'-01-01 00:00:00';
2714
						}
2715
					}
2716
					if (isset($filter['DeleteLastYearStats']) && date('Y',strtotime($last_update_day)) != date('Y')) {
2717
						$last_update_day = date('Y').'-01-01 00:00:00';
2718
						$reset = true;
2719
					}
2720
					// Count by filter
2721
					if ($globalDebug) echo '--- Stats for filter '.$filter_name.' ---'."\n";
2722
					$Spotter = new Spotter($this->db);
2723
					if ($globalDebug) echo 'Count all aircraft types...'."\n";
2724
					$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day,$filter);
2725
					foreach ($alldata as $number) {
2726
						$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'',$filter_name,$reset);
2727
					}
2728
					if ($globalDebug) echo 'Count all airlines...'."\n";
2729
					$alldata = $Spotter->countAllAirlines(false,0,$last_update_day,$filter);
2730
					foreach ($alldata as $number) {
2731
						$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],$filter_name,$reset);
2732
					}
2733
					if ($globalDebug) echo 'Count all aircraft registrations...'."\n";
2734
					$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day,$filter);
2735
					foreach ($alldata as $number) {
2736
						$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'',$filter_name,$reset);
2737
					}
2738
					if ($globalDebug) echo 'Count all callsigns...'."\n";
2739
					$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day,$filter);
2740
					foreach ($alldata as $number) {
2741
						$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],'',$filter_name,$reset);
2742
					}
2743
					if ($globalDebug) echo 'Count all owners...'."\n";
2744
					$alldata = $Spotter->countAllOwners(false,0,$last_update_day,$filter);
2745
					foreach ($alldata as $number) {
2746
						$this->addStatOwner($number['owner_name'],$number['owner_count'],'',$filter_name,$reset);
2747
					}
2748
					if ($globalDebug) echo 'Count all pilots...'."\n";
2749
					$alldata = $Spotter->countAllPilots(false,0,$last_update_day,$filter);
2750
					foreach ($alldata as $number) {
2751
						$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'',$filter_name,$number['format_source'],$reset);
2752
					}
2753
					if ($globalDebug) echo 'Count departure airports...'."\n";
2754
					$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day,$filter);
2755
					$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day,$filter);
2756
					$alldata = array();
2757
					foreach ($pall as $value) {
2758
						$icao = $value['airport_departure_icao'];
2759
						$alldata[$icao] = $value;
2760
					}
2761
					foreach ($dall as $value) {
2762
						$icao = $value['airport_departure_icao'];
2763
						if (isset($alldata[$icao])) {
2764
							$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2765
						} else $alldata[$icao] = $value;
2766
					}
2767
					$count = array();
2768
					foreach ($alldata as $key => $row) {
2769
						$count[$key] = $row['airport_departure_icao_count'];
2770
					}
2771
					array_multisort($count,SORT_DESC,$alldata);
2772
					foreach ($alldata as $number) {
2773
						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);
2774
					}
2775
					if ($globalDebug) echo 'Count all arrival airports...'."\n";
2776
					$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day,false,$filter);
2777
					$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day,false,$filter);
2778
					$alldata = array();
2779
					foreach ($pall as $value) {
2780
						$icao = $value['airport_arrival_icao'];
2781
						$alldata[$icao] = $value;
2782
					}
2783
					foreach ($dall as $value) {
2784
						$icao = $value['airport_arrival_icao'];
2785
						if (isset($alldata[$icao])) {
2786
							$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2787
						} else $alldata[$icao] = $value;
2788
					}
2789
					$count = array();
2790
					foreach ($alldata as $key => $row) {
2791
						$count[$key] = $row['airport_arrival_icao_count'];
2792
					}
2793
					array_multisort($count,SORT_DESC,$alldata);
2794
					foreach ($alldata as $number) {
2795
						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);
2796
					}
2797
					if ($globalDebug) echo 'Count all months...'."\n";
2798
					$Spotter = new Spotter($this->db);
2799
					$alldata = $Spotter->countAllMonths($filter);
2800
					$lastyear = false;
2801
					foreach ($alldata as $number) {
2802
						if ($number['year_name'] != date('Y')) $lastyear = true;
2803
						$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);
2804
					}
2805
					if ($globalDebug) echo 'Count all owners by months...'."\n";
2806
					$alldata = $Spotter->countAllMonthsOwners($filter);
2807
					foreach ($alldata as $number) {
2808
						$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);
2809
					}
2810
					if ($globalDebug) echo 'Count all pilots by months...'."\n";
2811
					$alldata = $Spotter->countAllMonthsPilots($filter);
2812
					foreach ($alldata as $number) {
2813
						$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);
2814
					}
2815
					if ($globalDebug) echo 'Count all military by months...'."\n";
2816
					$alldata = $Spotter->countAllMilitaryMonths($filter);
2817
					foreach ($alldata as $number) {
2818
						$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);
2819
					}
2820
					if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
2821
					$alldata = $Spotter->countAllMonthsAircrafts($filter);
2822
				    	foreach ($alldata as $number) {
2823
			    			$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);
2824
					}
2825
					if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
2826
					$alldata = $Spotter->countAllMonthsRealArrivals($filter);
2827
					foreach ($alldata as $number) {
2828
						$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);
2829
					}
2830
					echo '...Departure'."\n";
2831
					$pall = $Spotter->getLast7DaysAirportsDeparture('',$filter);
2832
					$dall = $Spotter->getLast7DaysDetectedAirportsDeparture('',$filter);
2833
					foreach ($dall as $value) {
2834
						$icao = $value['departure_airport_icao'];
2835
						$ddate = $value['date'];
2836
						$find = false;
2837
						foreach ($pall as $pvalue) {
2838
							if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2839
								$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2840
								$find = true;
2841
								break;
2842
							}
2843
						}
2844
						if ($find === false) {
2845
							$pall[] = $value;
2846
						}
2847
					}
2848
					$alldata = $pall;
2849
					foreach ($alldata as $number) {
2850
						$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);
2851
					}
2852
					echo '...Arrival'."\n";
2853
					$pall = $Spotter->getLast7DaysAirportsArrival('',$filter);
2854
					$dall = $Spotter->getLast7DaysDetectedAirportsArrival('',$filter);
2855
					foreach ($dall as $value) {
2856
						$icao = $value['arrival_airport_icao'];
2857
						$ddate = $value['date'];
2858
						$find = false;
2859
						foreach ($pall as $pvalue) {
2860
							if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2861
								$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2862
								$find = true;
2863
								break;
2864
							}
2865
						}
2866
						if ($find === false) {
2867
							$pall[] = $value;
2868
						}
2869
					}
2870
					$alldata = $pall;
2871
					foreach ($alldata as $number) {
2872
						$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);
2873
					}
2874
					echo 'Flights data...'."\n";
2875
					echo '-> countAllDatesLastMonth...'."\n";
2876
					$alldata = $Spotter->countAllDatesLastMonth($filter);
2877
					foreach ($alldata as $number) {
2878
						$this->addStatFlight('month',$number['date_name'],$number['date_count'], '',$filter_name);
2879
					}
2880
					echo '-> countAllDates...'."\n";
2881
					$previousdata = $this->countAllDates('',$filter_name);
2882
					$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter));
2883
					$values = array();
2884
					foreach ($alldata as $cnt) {
2885
						$values[] = $cnt['date_count'];
2886
					}
2887
					array_multisort($values,SORT_DESC,$alldata);
2888
					array_splice($alldata,11);
2889
					foreach ($alldata as $number) {
2890
						$this->addStatFlight('date',$number['date_name'],$number['date_count'],'',$filter_name);
2891
					}
2892
				
2893
					echo '-> countAllHours...'."\n";
2894
					$alldata = $Spotter->countAllHours('hour',$filter);
2895
					foreach ($alldata as $number) {
2896
						$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],'',$filter_name);
2897
					}
2898
					echo 'Insert last stats update date...'."\n";
2899
					date_default_timezone_set('UTC');
2900
					$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y-m-d G:i:s'));
2901
					if (isset($filter['DeleteLastYearStats']) && $filter['DeleteLastYearStats'] == true) {
2902
						if (date('Y',strtotime($last_update_day)) != date('Y')) {
2903
							$this->deleteOldStats($filter_name);
2904
							$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y').'-01-01 00:00:00');
2905
						}
2906
					}
2907
				}
2908
			}
2909
		}
2910
2911
			// Last year stats
2912
			if (isset($lastyear) && $lastyear) {
2913
				echo 'Data from last year...'."\n";
2914
				// SUM all previous month to put as year
2915
				$previous_year = date('Y');
2916
				$previous_year--;
2917
				$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2918
				$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2919
				$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2920
				$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2921
				$allairlines = $this->getAllAirlineNames();
2922
				foreach ($allairlines as $data) {
2923
					$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2924
					$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2925
					$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2926
					$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2927
				}
2928
				
2929
				if (isset($globalArchiveYear) && $globalArchiveYear) {
2930
					if ($globalArchive) {
2931
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
2932
						try {
2933
							$sth = $this->db->prepare($query);
2934
							$sth->execute();
2935
						} catch(PDOException $e) {
2936
							return "error : ".$e->getMessage().' - query : '.$query."\n";
2937
						}
2938
						$query = "INSERT INTO tracker_archive_output SELECT * FROM tracker_output WHERE tracker_output.date < '".date('Y')."-01-01 00:00:00'";
2939
						try {
2940
							$sth = $this->db->prepare($query);
2941
							$sth->execute();
2942
						} catch(PDOException $e) {
2943
							return "error : ".$e->getMessage().' - query : '.$query."\n";
2944
						}
2945
						$query = "INSERT INTO marine_archive_output SELECT * FROM marine_output WHERE marine_output.date < '".date('Y')."-01-01 00:00:00'";
2946
						try {
2947
							$sth = $this->db->prepare($query);
2948
							$sth->execute();
2949
						} catch(PDOException $e) {
2950
							return "error : ".$e->getMessage().' - query : '.$query."\n";
2951
						}
2952
					}
2953
					echo 'Delete old data'."\n";
2954
					if ($globalDBdriver == 'mysql') {
2955
						$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000";
2956
					} else {
2957
						$query = "DELETE FROM spotter_output WHERE spotter_id IN (SELECT spotter_id FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000)";
2958
					}
2959
					try {
2960
						$sth = $this->db->prepare($query);
2961
						$sth->execute();
2962
					} catch(PDOException $e) {
2963
						return "error : ".$e->getMessage().' - query : '.$query."\n";
2964
					}
2965
					if ($globalDBdriver == 'mysql') {
2966
						$query = "DELETE FROM tracker_output WHERE tracker_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000";
2967
					} else {
2968
						$query = "DELETE FROM tracker_output WHERE tracker_id IN (SELECT tracker_id FROM tracker_output WHERE tracker_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000)";
2969
					}
2970
					try {
2971
						$sth = $this->db->prepare($query);
2972
						$sth->execute();
2973
					} catch(PDOException $e) {
2974
						return "error : ".$e->getMessage().' - query : '.$query."\n";
2975
					}
2976
					if ($globalDBdriver == 'mysql') {
2977
						$query = "DELETE FROM marine_output WHERE marine_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000";
2978
					} else {
2979
						$query = "DELETE FROM marine_output WHERE marine_id IN (SELECT marine_id FROM marine_output WHERE marine_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000)";
2980
					}
2981
					try {
2982
						$sth = $this->db->prepare($query);
2983
						$sth->execute();
2984
					} catch(PDOException $e) {
2985
						return "error : ".$e->getMessage().' - query : '.$query."\n";
2986
					}
2987
				}
2988
				if (isset($globalDeleteLastYearStats) && $globalDeleteLastYearStats) {
2989
					$last_update = $this->getLastStatsUpdate('last_update_stats');
2990
					if (date('Y',strtotime($last_update[0]['value'])) != date('Y')) {
2991
						$this->deleteOldStats();
2992
						$this->addLastStatsUpdate('last_update_stats',date('Y').'-01-01 00:00:00');
2993
						$lastyearupdate = true;
2994
					}
2995
				}
2996
			}
2997
			if ($globalArchiveMonths > 0) {
2998
				if ($globalArchive) {
2999
					echo 'Archive old data...'."\n";
3000
					if ($globalDBdriver == 'mysql') {
3001
						//$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3002
						$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)
3003
							    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
3004
							     FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3005
					} else {
3006
						$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)
3007
							     SELECT 
3008
								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
3009
							    FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
3010
					}
3011
					try {
3012
						$sth = $this->db->prepare($query);
3013
						$sth->execute();
3014
					} catch(PDOException $e) {
3015
						return "error : ".$e->getMessage();
3016
					}
3017
					echo 'Archive old tracker data...'."\n";
3018
					if ($globalDBdriver == 'mysql') {
3019
						$query = "INSERT INTO tracker_archive_output (tracker_archive_output_id,famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, comment, type) 
3020
							    SELECT tracker_id,famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, comment, type
3021
							     FROM tracker_output WHERE tracker_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3022
					} else {
3023
						$query = "INSERT INTO tracker_archive_output (tracker_archive_output_id,famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, comment, type) 
3024
							     SELECT tracker_id,famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, comment, type
3025
							    FROM tracker_output WHERE tracker_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
3026
					}
3027
					try {
3028
						$sth = $this->db->prepare($query);
3029
						$sth->execute();
3030
					} catch(PDOException $e) {
3031
						return "error : ".$e->getMessage();
3032
					}
3033
					echo 'Archive old marine data...'."\n";
3034
					if ($globalDBdriver == 'mysql') {
3035
						$query = "INSERT INTO marine_archive_output (marine_archive_output_id,fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, mmsi, type, status,imo,arrival_port_name,arrival_port_date) 
3036
							    SELECT marine_id,fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, mmsi, type, status,imo,arrival_port_name,arrival_port_date 
3037
							     FROM marine_output WHERE marine_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3038
					} else {
3039
						$query = "INSERT INTO marine_archive_output (marine_archive_output_id,fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, mmsi, type, status,imo,arrival_port_name,arrival_port_date) 
3040
							     SELECT marine_id,fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, mmsi, type, status,imo,arrival_port_name,arrival_port_date 
3041
							    FROM marine_output WHERE marine_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
3042
					}
3043
					try {
3044
						$sth = $this->db->prepare($query);
3045
						$sth->execute();
3046
					} catch(PDOException $e) {
3047
						return "error : ".$e->getMessage();
3048
					}
3049
				}
3050
				echo 'Deleting old data...'."\n";
3051
				if ($globalDBdriver == 'mysql') {
3052
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3053
				} else {
3054
					$query = "DELETE FROM spotter_output WHERE spotter_id IN ( SELECT spotter_id FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP))";
3055
				}
3056
				try {
3057
					$sth = $this->db->prepare($query);
3058
					$sth->execute();
3059
				} catch(PDOException $e) {
3060
					return "error : ".$e->getMessage();
3061
				}
3062
				echo 'Deleting old tracker data...'."\n";
3063
				if ($globalDBdriver == 'mysql') {
3064
					$query = "DELETE FROM tracker_output WHERE tracker_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3065
				} else {
3066
					$query = "DELETE FROM tracker_output WHERE tracker_id IN ( SELECT tracker_id FROM tracker_output WHERE tracker_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP))";
3067
				}
3068
				try {
3069
					$sth = $this->db->prepare($query);
3070
					$sth->execute();
3071
				} catch(PDOException $e) {
3072
					return "error : ".$e->getMessage();
3073
				}
3074
				echo 'Deleting old marine data...'."\n";
3075
				if ($globalDBdriver == 'mysql') {
3076
					$query = "DELETE FROM marine_output WHERE marine_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3077
				} else {
3078
					$query = "DELETE FROM marine_output WHERE marine_id IN ( SELECT marine_id FROM marine_output WHERE marine_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP))";
3079
				}
3080
				try {
3081
					$sth = $this->db->prepare($query);
3082
					$sth->execute();
3083
				} catch(PDOException $e) {
3084
					return "error : ".$e->getMessage();
3085
				}
3086
			}
3087
			if (!isset($lastyearupdate)) {
3088
				echo 'Insert last stats update date...'."\n";
3089
				date_default_timezone_set('UTC');
3090
				$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
3091
			}
3092
			if ($globalStatsResetYear) {
3093
				require_once(dirname(__FILE__).'/../install/class.settings.php');
3094
				settings::modify_settings(array('globalStatsResetYear' => 'FALSE'));
3095
			}
3096
		
3097
	}
3098
}
3099
3100
?>