Completed
Push — master ( d86a10...79cbb4 )
by Yannick
75:24 queued 40:34
created

Stats::countOverallPilots()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 24
nop 4
dl 0
loc 29
rs 6.7272
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 aircraft_icao <> 'NA' 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 aircraft_icao <> 'NA' 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 aircraft_icao <> 'NA' 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 aircraft_icao <> 'NA' 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 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 registration_count DESC LIMIT 10 OFFSET 0";
434
				else $query = "SELECT s.aircraft_icao, s.cnt AS 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 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 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 registration_count DESC LIMIT 10 OFFSET 0";
446
				else $query = "SELECT s.aircraft_icao, s.cnt AS 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 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
			$Spotter = new Spotter($this->db);
0 ignored issues
show
Unused Code introduced by
$Spotter = new \Spotter($this->db); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
558
			return $Spotter->countAllFlightOverCountries($limit);
559
		} else return array();
560
	}
561
	public function countAllMarineOverCountries($limit = true, $filter_name = '',$year = '',$month = '') {
562
		$Connection = new Connection($this->db);
563
		if ($filter_name == '') $filter_name = $this->filter_name;
564
		if ($Connection->tableExists('countries')) {
565
			if ($year == '' && $month == '') {
566
				if ($limit) $query = "SELECT countries.iso3 as marine_country_iso3, countries.iso2 as marine_country_iso2, countries.name as marine_country, cnt as marine_count, lat as marine_country_latitude, lon as marine_country_longitude FROM stats_marine_country, countries WHERE stats_marine_country.iso2 = countries.iso2 AND filter_name = :filter_name ORDER BY marine_count DESC LIMIT 20 OFFSET 0";
567
				else $query = "SELECT countries.iso3 as marine_country_iso3, countries.iso2 as marine_country_iso2, countries.name as marine_country, cnt as marine_count, lat as marine_country_latitude, lon as marine_country_longitude FROM stats_marine_country, countries WHERE stats_marine_country.iso2 = countries.iso2 AND filter_name = :filter_name ORDER BY marine_count DESC";
568
				 try {
569
					$sth = $this->db->prepare($query);
570
					$sth->execute(array(':filter_name' => $filter_name));
571
				} catch(PDOException $e) {
572
					echo "error : ".$e->getMessage();
573
				}
574
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
575
				return $all;
576
			}
577
			$filters = array();
578
			if ($filter_name != '') {
579
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
0 ignored issues
show
Bug introduced by
The variable $globalStatsFilters does not exist. Did you mean $filters?

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

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

Loading history...
580
			}
581
			$Marine = new Marine($this->db);
582
			return $Marine->countAllMarineOverCountries($limit,$filters,$year,$month);
0 ignored issues
show
Documentation introduced by
$filters is of type array, but the function expects a integer.

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...
Documentation introduced by
$month is of type string, but the function expects a array.

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...
583
		} else return array();
584
	}
585
	public function countAllTrackerOverCountries($limit = true, $filter_name = '',$year = '',$month = '') {
586
		$Connection = new Connection($this->db);
587
		if ($filter_name == '') $filter_name = $this->filter_name;
588
		if ($Connection->tableExists('countries')) {
589
			if ($year == '' && $month == '') {
590
				if ($limit) $query = "SELECT countries.iso3 as tracker_country_iso3, countries.iso2 as tracker_country_iso2, countries.name as tracker_country, cnt as tracker_count, lat as tracker_country_latitude, lon as tracker_country_longitude FROM stats_tracker_country, countries WHERE stats_tracker_country.iso2 = countries.iso2 AND filter_name = :filter_name ORDER BY tracker_count DESC LIMIT 20 OFFSET 0";
591
				else $query = "SELECT countries.iso3 as tracker_country_iso3, countries.iso2 as tracker_country_iso2, countries.name as tracker_country, cnt as tracker_count, lat as tracker_country_latitude, lon as tracker_country_longitude FROM stats_tracker_country, countries WHERE stats_tracker_country.iso2 = countries.iso2 AND filter_name = :filter_name ORDER BY tracker_count DESC";
592
				 try {
593
					$sth = $this->db->prepare($query);
594
					$sth->execute(array(':filter_name' => $filter_name));
595
				} catch(PDOException $e) {
596
					echo "error : ".$e->getMessage();
597
				}
598
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
599
				return $all;
600
			}
601
			$Tracker = new Tracker($this->db);
602
			return $Tracker->countAllTrackerOverCountries($limit,$filters,$year,$month);
0 ignored issues
show
Bug introduced by
The variable $filters does not exist. Did you forget to declare it?

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

Loading history...
Documentation introduced by
$month is of type string, but the function expects a array.

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...
603
		} else return array();
604
	}
605
	public function countAllPilots($limit = true,$stats_airline = '',$filter_name = '', $year = '',$month = '') {
606
		global $globalStatsFilters;
607
		if ($filter_name == '') $filter_name = $this->filter_name;
608
		if ($year == '' && $month == '') {
609
			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";
610
			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";
611
			try {
612
				$sth = $this->db->prepare($query);
613
				$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
614
			} catch(PDOException $e) {
615
				echo "error : ".$e->getMessage();
616
			}
617
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
618
		} else $all = array();
619
		if (empty($all)) {
620
			$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
621
			if ($filter_name != '') {
622
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
623
			}
624
			$Spotter = new Spotter($this->db);
625
			//$all = $Spotter->countAllPilots($limit,0,'',$filters,$year,$month);
626
			$all = $Spotter->countAllPilots($limit,0,'',$filters);
627
		}
628
		return $all;
629
	}
630
631
	public function countAllOwners($limit = true,$stats_airline = '', $filter_name = '',$year = '',$month = '') {
632
		global $globalStatsFilters;
633
		if ($filter_name == '') $filter_name = $this->filter_name;
634
		if (strpos($stats_airline,'alliance_') !== FALSE) {
635
			$Spotter = new Spotter($this->db);
636
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
637
			if ($year == '' && $month == '') {
638
				$alliance_airlines = array();
639
				foreach ($airlines as $airline) {
640
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
641
				}
642
				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";
643
				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";
644
				try {
645
					$sth = $this->db->prepare($query);
646
					$sth->execute(array(':filter_name' => $filter_name));
647
				} catch(PDOException $e) {
648
					echo "error : ".$e->getMessage();
649
				}
650
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
651
			} else $all = array();
652
		} else {
653
			if ($year == '' && $month == '') {
654
				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";
655
				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";
656
				try {
657
					$sth = $this->db->prepare($query);
658
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
659
				} catch(PDOException $e) {
660
					echo "error : ".$e->getMessage();
661
				}
662
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
663
			} else $all = array();
664
		}
665
		if (empty($all)) {
666
			if (strpos($stats_airline,'alliance_') !== FALSE) {
667
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
668
			} else {
669
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
670
			}
671
			if ($filter_name != '') {
672
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
673
			}
674
			$Spotter = new Spotter($this->db);
675
			//$all = $Spotter->countAllOwners($limit,0,'',$filters,$year,$month);
676
			$all = $Spotter->countAllOwners($limit,0,'',$filters);
677
		}
678
		return $all;
679
	}
680
	public function countAllDepartureAirports($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
681
		global $globalStatsFilters;
682
		if ($filter_name == '') $filter_name = $this->filter_name;
683
		if (strpos($stats_airline,'alliance_') !== FALSE) {
684
			$Spotter = new Spotter($this->db);
685
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
686
			if ($year == '' && $month == '') {
687
				$alliance_airlines = array();
688
				foreach ($airlines as $airline) {
689
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
690
				}
691
				if ($limit) $query = "SELECT DISTINCT airport_icao AS airport_departure_icao,airport.name AS airport_departure_name,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";
692
				else $query = "SELECT DISTINCT airport_icao AS airport_departure_icao,airport.name AS airport_departure_name,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";
693
				try {
694
					$sth = $this->db->prepare($query);
695
					$sth->execute(array(':filter_name' => $filter_name));
696
				} catch(PDOException $e) {
697
					echo "error : ".$e->getMessage();
698
				}
699
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
700
			} else $all = array();
701
		} else {
702
			if ($year == '' && $month == '') {
703
				if ($limit) $query = "SELECT DISTINCT airport_icao AS airport_departure_icao,airport.name AS airport_departure_name,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";
704
				else $query = "SELECT DISTINCT airport_icao AS airport_departure_icao,airport.name AS airport_departure_name,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";
705
				try {
706
					$sth = $this->db->prepare($query);
707
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
708
				} catch(PDOException $e) {
709
					echo "error : ".$e->getMessage();
710
				}
711
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
712
			} else $all = array();
713
		}
714
		if (empty($all)) {
715
			if (strpos($stats_airline,'alliance_') !== FALSE) {
716
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
717
			} else {
718
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
719
			}
720
			if ($filter_name != '') {
721
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
722
			}
723
			$Spotter = new Spotter($this->db);
724
//            		$pall = $Spotter->countAllDepartureAirports($limit,0,'',$filters,$year,$month);
725
  //      		$dall = $Spotter->countAllDetectedDepartureAirports($limit,0,'',$filters,$year,$month);
726
			$pall = $Spotter->countAllDepartureAirports($limit,0,'',$filters);
727
			$dall = $Spotter->countAllDetectedDepartureAirports($limit,0,'',$filters);
728
			$all = array();
729
			foreach ($pall as $value) {
730
				$icao = $value['airport_departure_icao'];
731
				$all[$icao] = $value;
732
			}
733
			foreach ($dall as $value) {
734
				$icao = $value['airport_departure_icao'];
735
				if (isset($all[$icao])) {
736
					$all[$icao]['airport_departure_icao_count'] = $all[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
737
				} else $all[$icao] = $value;
738
			}
739
			$count = array();
740
			foreach ($all as $key => $row) {
741
				$count[$key] = $row['airport_departure_icao_count'];
742
			}
743
			array_multisort($count,SORT_DESC,$all);
744
		}
745
		return $all;
746
	}
747
	public function countAllArrivalAirports($limit = true,$stats_airline = '',$filter_name = '',$year = '',$month = '') {
748
		global $globalStatsFilters;
749
		if ($filter_name == '') $filter_name = $this->filter_name;
750
		if (strpos($stats_airline,'alliance_') !== FALSE) {
751
			$Spotter = new Spotter($this->db);
752
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
753
			if ($year == '' && $month == '') {
754
				$alliance_airlines = array();
755
				foreach ($airlines as $airline) {
756
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
757
				}
758
				if ($limit) $query = "SELECT DISTINCT airport_icao AS airport_arrival_icao,airport.name AS airport_arrival_name, 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";
759
				else $query = "SELECT DISTINCT airport_icao AS airport_arrival_icao,airport.name AS airport_arrival_name, 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";
760
				try {
761
					$sth = $this->db->prepare($query);
762
					$sth->execute(array(':filter_name' => $filter_name));
763
				} catch(PDOException $e) {
764
					echo "error : ".$e->getMessage();
765
				}
766
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
767
			} else $all = array();
768
		} else {
769
			if ($year == '' && $month == '') {
770
				if ($limit) $query = "SELECT DISTINCT airport_icao AS airport_arrival_icao,airport.name AS airport_arrival_name, 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";
771
				else $query = "SELECT DISTINCT airport_icao AS airport_arrival_icao,airport.name AS airport_arrival_name, 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";
772
				try {
773
					$sth = $this->db->prepare($query);
774
					$sth->execute(array(':stats_airline' => $stats_airline,':filter_name' => $filter_name));
775
				} catch(PDOException $e) {
776
					echo "error : ".$e->getMessage();
777
				}
778
				$all = $sth->fetchAll(PDO::FETCH_ASSOC);
779
			} else $all = array();
780
		}
781
		if (empty($all)) {
782
			if (strpos($stats_airline,'alliance_') !== FALSE) {
783
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
784
			} else {
785
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
786
			}
787
			if ($filter_name != '') {
788
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
789
			}
790
			$Spotter = new Spotter($this->db);
791
//			$pall = $Spotter->countAllArrivalAirports($limit,0,'',false,$filters,$year,$month);
792
//			$dall = $Spotter->countAllDetectedArrivalAirports($limit,0,'',false,$filters,$year,$month);
793
			$pall = $Spotter->countAllArrivalAirports($limit,0,'',false,$filters);
794
			$dall = $Spotter->countAllDetectedArrivalAirports($limit,0,'',false,$filters);
795
			$all = array();
796
			foreach ($pall as $value) {
797
				$icao = $value['airport_arrival_icao'];
798
				$all[$icao] = $value;
799
			}
800
			foreach ($dall as $value) {
801
				$icao = $value['airport_arrival_icao'];
802
				if (isset($all[$icao])) {
803
					$all[$icao]['airport_arrival_icao_count'] = $all[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
804
				} else $all[$icao] = $value;
805
			}
806
			$count = array();
807
			foreach ($all as $key => $row) {
808
				$count[$key] = $row['airport_arrival_icao_count'];
809
			}
810
			array_multisort($count,SORT_DESC,$all);
811
		}
812
		return $all;
813
	}
814
	public function countAllMonthsLastYear($limit = true,$stats_airline = '',$filter_name = '') {
815
		global $globalDBdriver, $globalStatsFilters;
816
		if ($filter_name == '') $filter_name = $this->filter_name;
817
		if (strpos($stats_airline,'alliance_') !== FALSE) {
818
			$Spotter = new Spotter($this->db);
819
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
820
			$alliance_airlines = array();
821
			foreach ($airlines as $airline) {
822
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
823
			}
824
			if ($globalDBdriver == 'mysql') {
825
				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";
826
				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";
827
			} else {
828
				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";
829
				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";
830
			}
831
			$query_data = array(':filter_name' => $filter_name);
832
		} else {
833
			if ($globalDBdriver == 'mysql') {
834
				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";
835
				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";
836
			} else {
837
				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";
838
				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";
839
			}
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->countAllMonthsLastYear($filters);
860
		}
861
		return $all;
862
	}
863
	
864
	public function countAllDatesLastMonth($stats_airline = '',$filter_name = '') {
865
		global $globalStatsFilters;
866
		if ($filter_name == '') $filter_name = $this->filter_name;
867
		if (strpos($stats_airline,'alliance_') !== FALSE) {
868
			$Spotter = new Spotter($this->db);
869
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
870
			$alliance_airlines = array();
871
			foreach ($airlines as $airline) {
872
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
873
			}
874
			$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";
875
			$query_data = array(':filter_name' => $filter_name);
876
		} else {
877
			$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";
878
			$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
879
		}
880
		try {
881
			$sth = $this->db->prepare($query);
882
			$sth->execute($query_data);
883
		} catch(PDOException $e) {
884
			echo "error : ".$e->getMessage();
885
		}
886
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
887
		if (empty($all)) {
888
			if (strpos($stats_airline,'alliance_') !== FALSE) {
889
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
890
			} else {
891
				$filters = array('airlines' => array($stats_airline));
892
			}
893
			if ($filter_name != '') {
894
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
895
			}
896
			$Spotter = new Spotter($this->db);
897
			$all = $Spotter->countAllDatesLastMonth($filters);
898
		}
899
		return $all;
900
	}
901
	public function countAllDatesLast7Days($stats_airline = '',$filter_name = '') {
902
		global $globalDBdriver, $globalStatsFilters;
903
		if ($filter_name == '') $filter_name = $this->filter_name;
904
		if (strpos($stats_airline,'alliance_') !== FALSE) {
905
			$Spotter = new Spotter($this->db);
906
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
907
			$alliance_airlines = array();
908
			foreach ($airlines as $airline) {
909
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
910
			}
911
			if ($globalDBdriver == 'mysql') {
912
				$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";
913
			} else {
914
				$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";
915
			}
916
			$query_data = array(':filter_name' => $filter_name);
917
		} else {
918
			if ($globalDBdriver == 'mysql') {
919
				$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";
920
			} else {
921
				$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";
922
			}
923
			$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
924
		}
925
		try {
926
			$sth = $this->db->prepare($query);
927
			$sth->execute($query_data);
928
		} catch(PDOException $e) {
929
			echo "error : ".$e->getMessage();
930
		}
931
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
932
		if (empty($all)) {
933
			if (strpos($stats_airline,'alliance_') !== FALSE) {
934
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
935
			} else {
936
				$filters = array('airlines' => array($stats_airline));
937
			}
938
			if ($filter_name != '') {
939
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
940
			}
941
			$Spotter = new Spotter($this->db);
942
			$all = $Spotter->countAllDatesLast7Days($filters);
943
		}
944
		return $all;
945
	}
946
	public function countAllDates($stats_airline = '',$filter_name = '') {
947
		global $globalStatsFilters;
948
		if ($filter_name == '') $filter_name = $this->filter_name;
949
		if (strpos($stats_airline,'alliance_') !== FALSE) {
950
			$Spotter = new Spotter($this->db);
951
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
952
			$alliance_airlines = array();
953
			foreach ($airlines as $airline) {
954
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
955
			}
956
			$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";
957
			$query_data = array(':filter_name' => $filter_name);
958
		} else {
959
			$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";
960
			$query_data = array(':stats_airline' => $stats_airline,':filter_name' => $filter_name);
961
		}
962
		try {
963
			$sth = $this->db->prepare($query);
964
			$sth->execute($query_data);
965
		} catch(PDOException $e) {
966
			echo "error : ".$e->getMessage();
967
		}
968
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
969
		if (empty($all)) {
970
			if (strpos($stats_airline,'alliance_') !== FALSE) {
971
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
972
			} else {
973
				$filters = array('airlines' => array($stats_airline));
974
			}
975
			if ($filter_name != '') {
976
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
977
			}
978
			$Spotter = new Spotter($this->db);
979
			$all = $Spotter->countAllDates($filters);
980
		}
981
		return $all;
982
	}
983
	public function countAllDatesMarine($filter_name = '') {
984
		global $globalStatsFilters;
985
		if ($filter_name == '') $filter_name = $this->filter_name;
986
		$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";
987
		$query_data = array(':filter_name' => $filter_name);
988
		try {
989
			$sth = $this->db->prepare($query);
990
			$sth->execute($query_data);
991
		} catch(PDOException $e) {
992
			echo "error : ".$e->getMessage();
993
		}
994
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
995
		if (empty($all)) {
996
			$filters = array();
997
			if ($filter_name != '') {
998
				$filters = $globalStatsFilters[$filter_name];
999
			}
1000
			$Marine = new Marine($this->db);
1001
			$all = $Marine->countAllDates($filters);
1002
		}
1003
		return $all;
1004
	}
1005
	public function countAllDatesTracker($filter_name = '') {
1006
		global $globalStatsFilters;
1007
		if ($filter_name == '') $filter_name = $this->filter_name;
1008
		$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";
1009
		$query_data = array(':filter_name' => $filter_name);
1010
		try {
1011
			$sth = $this->db->prepare($query);
1012
			$sth->execute($query_data);
1013
		} catch(PDOException $e) {
1014
			echo "error : ".$e->getMessage();
1015
		}
1016
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1017
		if (empty($all)) {
1018
			$filters = array();
1019
			if ($filter_name != '') {
1020
				$filters = $globalStatsFilters[$filter_name];
1021
			}
1022
			$Tracker = new Tracker($this->db);
1023
			$all = $Tracker->countAllDates($filters);
1024
		}
1025
		return $all;
1026
	}
1027
	public function countAllDatesByAirlines($filter_name = '') {
1028
		global $globalStatsFilters;
1029
		if ($filter_name == '') $filter_name = $this->filter_name;
1030
		$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";
1031
		$query_data = array('filter_name' => $filter_name);
1032
		try {
1033
			$sth = $this->db->prepare($query);
1034
			$sth->execute($query_data);
1035
		} catch(PDOException $e) {
1036
			echo "error : ".$e->getMessage();
1037
		}
1038
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1039
		if (empty($all)) {
1040
			$filters = array();
1041
			if ($filter_name != '') {
1042
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1043
			}
1044
			$Spotter = new Spotter($this->db);
1045
			$all = $Spotter->countAllDatesByAirlines($filters);
1046
		}
1047
		return $all;
1048
	}
1049
	public function countAllMonths($stats_airline = '',$filter_name = '') {
1050
		global $globalStatsFilters, $globalDBdriver;
1051
		if ($filter_name == '') $filter_name = $this->filter_name;
1052
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1053
			$Spotter = new Spotter($this->db);
1054
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1055
			$alliance_airlines = array();
1056
			foreach ($airlines as $airline) {
1057
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1058
			}
1059
			if ($globalDBdriver == 'mysql') {
1060
				$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";
1061
			} else {
1062
				$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";
1063
			}
1064
			$query_data = array(':filter_name' => $filter_name);
1065
		} else {
1066
			if ($globalDBdriver == 'mysql') {
1067
				$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";
1068
			} else {
1069
				$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";
1070
			}
1071
			$query_data = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1072
		}
1073
		try {
1074
			$sth = $this->db->prepare($query);
1075
			$sth->execute($query_data);
1076
		} catch(PDOException $e) {
1077
			echo "error : ".$e->getMessage();
1078
		}
1079
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1080
		if (empty($all)) {
1081
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1082
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1083
			} else {
1084
				$filters = array('airlines' => array($stats_airline));
1085
			}
1086
			if ($filter_name != '') {
1087
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1088
			}
1089
			$Spotter = new Spotter($this->db);
1090
			$all = $Spotter->countAllMonths($filters);
1091
		}
1092
		return $all;
1093
	}
1094
	public function countFatalitiesLast12Months() {
1095
		global $globalStatsFilters, $globalDBdriver;
1096
		if ($globalDBdriver == 'mysql') {
1097
			$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";
1098
		} else {
1099
			$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";
1100
		}
1101
		try {
1102
			$sth = $this->db->prepare($query);
1103
			$sth->execute();
1104
		} catch(PDOException $e) {
1105
			echo "error : ".$e->getMessage();
1106
		}
1107
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1108
		if (empty($all)) {
1109
			$Accident = new Accident($this->db);
1110
			$all = $Accident->countFatalitiesLast12Months();
1111
		}
1112
		return $all;
1113
	}
1114
	public function countFatalitiesByYear() {
1115
		global $globalStatsFilters, $globalDBdriver;
1116
		if ($globalDBdriver == 'mysql') {
1117
			$query = "SELECT YEAR(stats_date) AS year, cnt as count FROM stats WHERE stats_type = 'fatalities_byyear' ORDER BY stats_date";
1118
		} else {
1119
			$query = "SELECT EXTRACT(YEAR FROM stats_date) AS year, cnt as count FROM stats WHERE stats_type = 'fatalities_byyear' ORDER BY stats_date";
1120
		}
1121
		try {
1122
			$sth = $this->db->prepare($query);
1123
			$sth->execute();
1124
		} catch(PDOException $e) {
1125
			echo "error : ".$e->getMessage();
1126
		}
1127
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1128
		if (empty($all)) {
1129
			$Accident = new Accident($this->db);
1130
			$all = $Accident->countFatalitiesByYear();
1131
		}
1132
		return $all;
1133
	}
1134
	public function countAllMilitaryMonths($filter_name = '') {
1135
		global $globalStatsFilters;
1136
		if ($filter_name == '') $filter_name = $this->filter_name;
1137
		$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";
1138
		try {
1139
			$sth = $this->db->prepare($query);
1140
			$sth->execute(array(':filter_name' => $filter_name));
1141
		} catch(PDOException $e) {
1142
			echo "error : ".$e->getMessage();
1143
		}
1144
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1145
		if (empty($all)) {
1146
			$filters = array();
1147
			if ($filter_name != '') {
1148
					$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1149
			}
1150
			$Spotter = new Spotter($this->db);
1151
			$all = $Spotter->countAllMilitaryMonths($filters);
1152
		}
1153
		return $all;
1154
	}
1155
	public function countAllHours($orderby = 'hour',$limit = true,$stats_airline = '',$filter_name = '') {
1156
		global $globalTimezone, $globalDBdriver, $globalStatsFilters;
1157
		if ($filter_name == '') $filter_name = $this->filter_name;
1158
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1159
			$Spotter = new Spotter($this->db);
1160
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1161
			$alliance_airlines = array();
1162
			foreach ($airlines as $airline) {
1163
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1164
			}
1165
			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";
1166
			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";
1167
			$query_data = array(':filter_name' => $filter_name);
1168
		} else {
1169
			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";
1170
			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";
1171
			$query_data = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1172
		}
1173
		if ($orderby == 'hour') {
1174
			if ($globalDBdriver == 'mysql') {
1175
				$query .= " ORDER BY CAST(flight_date AS UNSIGNED) ASC";
1176
			} else {
1177
				$query .= " ORDER BY CAST(flight_date AS integer) ASC";
1178
			}
1179
		}
1180
		if ($orderby == 'count') $query .= " ORDER BY hour_count DESC";
1181
		try {
1182
			$sth = $this->db->prepare($query);
1183
			$sth->execute($query_data);
1184
		} catch(PDOException $e) {
1185
			echo "error : ".$e->getMessage();
1186
		}
1187
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1188
		if (empty($all)) {
1189
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1190
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1191
			} else {
1192
				$filters = array('airlines' => array($stats_airline));
1193
			}
1194
			if ($filter_name != '') {
1195
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1196
			}
1197
			$Spotter = new Spotter($this->db);
1198
			$all = $Spotter->countAllHours($orderby,$filters);
1199
		}
1200
		return $all;
1201
	}
1202
	public function countOverallFlights($stats_airline = '', $filter_name = '',$year = '',$month = '') {
1203
		global $globalStatsFilters;
1204
		if ($filter_name == '') $filter_name = $this->filter_name;
1205
		if ($year == '') $year = date('Y');
1206
		$all = $this->getSumStats('flights_bymonth',$year,$stats_airline,$filter_name,$month);
1207
		if (empty($all)) {
1208
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1209
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1210
			} else {
1211
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1212
			}
1213
			if ($filter_name != '') {
1214
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1215
			}
1216
			$Spotter = new Spotter($this->db);
1217
			//$all = $Spotter->countOverallFlights($filters,$year,$month);
1218
			$all = $Spotter->countOverallFlights($filters);
1219
		}
1220
		return $all;
1221
	}
1222
	public function countOverallMarine($filter_name = '',$year = '',$month = '') {
1223
		global $globalStatsFilters;
1224
		if ($filter_name == '') $filter_name = $this->filter_name;
1225
		if ($year == '') $year = date('Y');
1226
		$all = $this->getSumStats('marine_bymonth',$year,'',$filter_name,$month);
1227
		if (empty($all)) {
1228
			$filters = array('year' => $year,'month' => $month);
1229
			if ($filter_name != '') {
1230
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1231
			}
1232
			$Marine = new Marine($this->db);
1233
			//$all = $Spotter->countOverallFlights($filters,$year,$month);
1234
			$all = $Marine->countOverallMarine($filters);
1235
		}
1236
		return $all;
1237
	}
1238
	public function countOverallTracker($filter_name = '',$year = '',$month = '') {
1239
		global $globalStatsFilters;
1240
		if ($filter_name == '') $filter_name = $this->filter_name;
1241
		if ($year == '') $year = date('Y');
1242
		$all = $this->getSumStats('tracker_bymonth',$year,'',$filter_name,$month);
1243
		if (empty($all)) {
1244
			$filters = array('year' => $year,'month' => $month);
1245
			if ($filter_name != '') {
1246
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1247
			}
1248
			$Tracker = new Tracker($this->db);
1249
			//$all = $Spotter->countOverallFlights($filters,$year,$month);
1250
			$all = $Tracker->countOverallTracker($filters);
1251
		}
1252
		return $all;
1253
	}
1254
	public function countOverallMilitaryFlights($filter_name = '',$year = '', $month = '') {
1255
		global $globalStatsFilters;
1256
		if ($filter_name == '') $filter_name = $this->filter_name;
1257
		if ($year == '') $year = date('Y');
1258
		$all = $this->getSumStats('military_flights_bymonth',$year,'',$filter_name,$month);
1259
		if (empty($all)) {
1260
			$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...
1261
			$filters = array('year' => $year,'month' => $month);
1262
			if ($filter_name != '') {
1263
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1264
			}
1265
			$Spotter = new Spotter($this->db);
1266
			//$all = $Spotter->countOverallMilitaryFlights($filters,$year,$month);
1267
			$all = $Spotter->countOverallMilitaryFlights($filters);
1268
		}
1269
		return $all;
1270
	}
1271
	public function countOverallArrival($stats_airline = '',$filter_name = '', $year = '', $month = '') {
1272
		global $globalStatsFilters;
1273
		if ($filter_name == '') $filter_name = $this->filter_name;
1274
		if ($year == '') $year = date('Y');
1275
		$all = $this->getSumStats('realarrivals_bymonth',$year,$stats_airline,$filter_name,$month);
1276
		if (empty($all)) {
1277
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1278
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1279
			} else {
1280
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1281
			}
1282
			if ($filter_name != '') {
1283
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1284
			}
1285
			$Spotter = new Spotter($this->db);
1286
			//$all = $Spotter->countOverallArrival($filters,$year,$month);
1287
			$all = $Spotter->countOverallArrival($filters);
1288
		}
1289
		return $all;
1290
	}
1291
	public function countOverallAircrafts($stats_airline = '',$filter_name = '',$year = '', $month = '') {
1292
		global $globalStatsFilters;
1293
		if ($filter_name == '') $filter_name = $this->filter_name;
1294
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1295
			$Spotter = new Spotter($this->db);
1296
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1297
			if ($year == '' && $month == '') {
1298
				$alliance_airlines = array();
1299
				foreach ($airlines as $airline) {
1300
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1301
				}
1302
				$query = "SELECT COUNT(*) AS nb FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1303
				try {
1304
					$sth = $this->db->prepare($query);
1305
					$sth->execute(array(':filter_name' => $filter_name));
1306
				} catch(PDOException $e) {
1307
					echo "error : ".$e->getMessage();
1308
				}
1309
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1310
				$all = $result[0]['nb'];
1311
			} else $all = $this->getSumStats('aircrafts_bymonth',$year,$stats_airline,$filter_name,$month);
1312
		} else {
1313
			if ($year == '' && $month == '') {
1314
				$query = "SELECT COUNT(*) AS nb FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1315
				try {
1316
					$sth = $this->db->prepare($query);
1317
					$sth->execute(array(':filter_name' => $filter_name,':stats_airline' => $stats_airline));
1318
				} catch(PDOException $e) {
1319
					echo "error : ".$e->getMessage();
1320
				}
1321
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1322
				$all = $result[0]['nb'];
1323
			} else $all = $this->getSumStats('aircrafts_bymonth',$year,$stats_airline,$filter_name,$month);
1324
		}
1325
		if (empty($all)) {
1326
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1327
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1328
			} else {
1329
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1330
			}
1331
			if ($filter_name != '') {
1332
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1333
			}
1334
			$Spotter = new Spotter($this->db);
1335
			//$all = $Spotter->countOverallAircrafts($filters,$year,$month);
1336
			$all = $Spotter->countOverallAircrafts($filters);
1337
		}
1338
		return $all;
1339
	}
1340
	public function countOverallAirlines($filter_name = '',$year = '',$month = '') {
1341
		global $globalStatsFilters;
1342
		if ($filter_name == '') $filter_name = $this->filter_name;
1343
		if ($year == '' && $month == '') {
1344
			$query = "SELECT COUNT(*) AS nb_airline FROM stats_airline WHERE filter_name = :filter_name";
1345
			try {
1346
				$sth = $this->db->prepare($query);
1347
				$sth->execute(array(':filter_name' => $filter_name));
1348
			} catch(PDOException $e) {
1349
				echo "error : ".$e->getMessage();
1350
			}
1351
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1352
			$all = $result[0]['nb_airline'];
1353
		} else $all = $this->getSumStats('airlines_bymonth',$year,'',$filter_name,$month);
1354
		if (empty($all)) {
1355
			$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...
1356
			$filters = array('year' => $year,'month' => $month);
1357
			if ($filter_name != '') {
1358
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1359
			}
1360
			$Spotter = new Spotter($this->db);
1361
			//$all = $Spotter->countOverallAirlines($filters,$year,$month);
1362
			$all = $Spotter->countOverallAirlines($filters);
1363
		}
1364
		return $all;
1365
	}
1366
	public function countOverallOwners($stats_airline = '',$filter_name = '',$year = '', $month = '') {
1367
		global $globalStatsFilters;
1368
		if ($filter_name == '') $filter_name = $this->filter_name;
1369
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1370
			$Spotter = new Spotter($this->db);
1371
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1372
			if ($year == '' && $month == '') {
1373
				$alliance_airlines = array();
1374
				foreach ($airlines as $airline) {
1375
					$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1376
				}
1377
				$query = "SELECT count(*) as nb FROM stats_owner WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1378
				$query_values = array(':filter_name' => $filter_name);
1379
				try {
1380
					$sth = $this->db->prepare($query);
1381
					$sth->execute($query_values);
1382
				} catch(PDOException $e) {
1383
					echo "error : ".$e->getMessage();
1384
				}
1385
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1386
				$all = $result[0]['nb'];
1387
			} else {
1388
				$all = $this->getSumStats('owners_bymonth',$year,$stats_airline,$filter_name,$month);
1389
			}
1390
		} else {
1391
			if ($year == '' && $month == '') {
1392
				$query = "SELECT count(*) as nb FROM stats_owner WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1393
				$query_values = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1394
				try {
1395
					$sth = $this->db->prepare($query);
1396
					$sth->execute($query_values);
1397
				} catch(PDOException $e) {
1398
					echo "error : ".$e->getMessage();
1399
				}
1400
				$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1401
				$all = $result[0]['nb'];
1402
			} else {
1403
				$all = $this->getSumStats('owners_bymonth',$year,$stats_airline,$filter_name,$month);
1404
			}
1405
		}
1406
		if (empty($all)) {
1407
			if (strpos($stats_airline,'alliance_') !== FALSE) {
1408
				$filters = array('alliance' => str_replace('_',' ',str_replace('alliance_','',$stats_airline)),'year' => $year,'month' => $month);
1409
			} else {
1410
				$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1411
			}
1412
			if ($filter_name != '') {
1413
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1414
			}
1415
			$Spotter = new Spotter($this->db);
1416
			//$all = $Spotter->countOverallOwners($filters,$year,$month);
1417
			$all = $Spotter->countOverallOwners($filters);
1418
		}
1419
		return $all;
1420
	}
1421
	public function countOverallPilots($stats_airline = '',$filter_name = '',$year = '',$month = '') {
1422
		global $globalStatsFilters;
1423
		if ($filter_name == '') $filter_name = $this->filter_name;
1424
		//if ($year == '') $year = date('Y');
1425
		if ($year == '' && $month == '') {
1426
			$query = "SELECT count(*) as nb FROM stats_pilot WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1427
			$query_values = array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1428
			try {
1429
				$sth = $this->db->prepare($query);
1430
				$sth->execute($query_values);
1431
			} catch(PDOException $e) {
1432
				echo "error : ".$e->getMessage();
1433
			}
1434
			$result = $sth->fetchAll(PDO::FETCH_ASSOC);
1435
			$all = $result[0]['nb'];
1436
		} else {
1437
			$all = $this->getSumStats('pilots_bymonth',$year,$stats_airline,$filter_name,$month);
1438
		}
1439
		if (empty($all)) {
1440
			$filters = array('airlines' => array($stats_airline),'year' => $year,'month' => $month);
1441
			if ($filter_name != '') {
1442
				$filters = array_merge($filters,$globalStatsFilters[$filter_name]);
1443
			}
1444
			$Spotter = new Spotter($this->db);
1445
			//$all = $Spotter->countOverallPilots($filters,$year,$month);
1446
			$all = $Spotter->countOverallPilots($filters);
1447
		}
1448
		return $all;
1449
	}
1450
1451
	public function getLast7DaysAirports($airport_icao = '', $stats_airline = '',$filter_name = '') {
1452
		if ($filter_name == '') $filter_name = $this->filter_name;
1453
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1454
			$Spotter = new Spotter($this->db);
1455
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1456
			$alliance_airlines = array();
1457
			foreach ($airlines as $airline) {
1458
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1459
			}
1460
			$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";
1461
			$query_values = array(':airport_icao' => $airport_icao,':filter_name' => $filter_name);
1462
		} else {
1463
			$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";
1464
			$query_values = array(':airport_icao' => $airport_icao,':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1465
		}
1466
		try {
1467
			$sth = $this->db->prepare($query);
1468
			$sth->execute($query_values);
1469
		} catch(PDOException $e) {
1470
			echo "error : ".$e->getMessage();
1471
		}
1472
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1473
		return $all;
1474
	}
1475
	public function getStats($type,$stats_airline = '', $filter_name = '') {
1476
		if ($filter_name == '') $filter_name = $this->filter_name;
1477
		$query = "SELECT * FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline AND filter_name = :filter_name ORDER BY stats_date";
1478
		$query_values = array(':type' => $type,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1479
		try {
1480
			$sth = $this->db->prepare($query);
1481
			$sth->execute($query_values);
1482
		} catch(PDOException $e) {
1483
			echo "error : ".$e->getMessage();
1484
		}
1485
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1486
		return $all;
1487
	}
1488
	public function deleteStatsByType($type,$stats_airline = '', $filter_name = '') {
1489
		if ($filter_name == '') $filter_name = $this->filter_name;
1490
		$query = "DELETE FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline AND filter_name = :filter_name";
1491
		$query_values = array(':type' => $type,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1492
		try {
1493
			$sth = $this->db->prepare($query);
1494
			$sth->execute($query_values);
1495
		} catch(PDOException $e) {
1496
			echo "error : ".$e->getMessage();
1497
		}
1498
	}
1499
	public function getSumStats($type,$year,$stats_airline = '',$filter_name = '',$month = '') {
1500
		if ($filter_name == '') $filter_name = $this->filter_name;
1501
		global $globalArchiveMonths, $globalDBdriver;
1502
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1503
			$Spotter = new Spotter($this->db);
1504
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1505
			$alliance_airlines = array();
1506
			foreach ($airlines as $airline) {
1507
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1508
			}
1509
			if ($globalDBdriver == 'mysql') {
1510
				if ($month == '') {
1511
					$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";
1512
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name);
1513
				} else {
1514
					$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";
1515
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name,':month' => $month);
1516
				}
1517
			} else {
1518
				if ($month == '') {
1519
					$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";
1520
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name);
1521
				} else {
1522
					$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";
1523
					$query_values = array(':type' => $type, ':year' => $year, ':filter_name' => $filter_name,':month' => $month);
1524
				}
1525
			}
1526
		} else {
1527
			if ($globalDBdriver == 'mysql') {
1528
				if ($month == '') {
1529
					$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";
1530
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1531
				} else {
1532
					$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";
1533
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
1534
				}
1535
			} else {
1536
				if ($month == '') {
1537
					$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";
1538
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1539
				} else {
1540
					$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";
1541
					$query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline,':filter_name' => $filter_name,':month' => $month);
1542
				}
1543
			}
1544
		}
1545
		try {
1546
			$sth = $this->db->prepare($query);
1547
			$sth->execute($query_values);
1548
		} catch(PDOException $e) {
1549
			echo "error : ".$e->getMessage();
1550
		}
1551
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1552
		return $all[0]['total'];
1553
	}
1554
	public function getStatsTotal($type, $stats_airline = '', $filter_name = '') {
1555
		global $globalArchiveMonths, $globalDBdriver;
1556
		if ($filter_name == '') $filter_name = $this->filter_name;
1557
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1558
			$Spotter = new Spotter($this->db);
1559
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1560
			$alliance_airlines = array();
1561
			foreach ($airlines as $airline) {
1562
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1563
			}
1564
			if ($globalDBdriver == 'mysql') {
1565
				$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";
1566
			} else {
1567
				$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";
1568
			}
1569
			$query_values = array(':type' => $type, ':filter_name' => $filter_name);
1570
		} else {
1571
			if ($globalDBdriver == 'mysql') {
1572
				$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";
1573
			} else {
1574
				$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";
1575
			}
1576
			$query_values = array(':type' => $type, ':stats_airline' => $stats_airline, ':filter_name' => $filter_name);
1577
		}
1578
		try {
1579
			$sth = $this->db->prepare($query);
1580
			$sth->execute($query_values);
1581
		} catch(PDOException $e) {
1582
			echo "error : ".$e->getMessage();
1583
		}
1584
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1585
		return $all[0]['total'];
1586
	}
1587
	public function getStatsAircraftTotal($stats_airline = '', $filter_name = '') {
1588
		global $globalArchiveMonths, $globalDBdriver;
1589
		if ($filter_name == '') $filter_name = $this->filter_name;
1590
		if (strpos($stats_airline,'alliance_') !== FALSE) {
1591
			$Spotter = new Spotter($this->db);
1592
			$airlines = $Spotter->getAllAirlineNamesByAlliance(str_replace('_',' ',str_replace('alliance_','',$stats_airline)));
1593
			$alliance_airlines = array();
1594
			foreach ($airlines as $airline) {
1595
				$alliance_airlines = array_merge($alliance_airlines,array($airline['airline_icao']));
1596
			}
1597
			if ($globalDBdriver == 'mysql') {
1598
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1599
			} else {
1600
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline IN ('".implode("','",$alliance_airlines)."') AND filter_name = :filter_name";
1601
			}
1602
		} else {
1603
			if ($globalDBdriver == 'mysql') {
1604
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1605
			} else {
1606
				$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline AND filter_name = :filter_name";
1607
			}
1608
		}
1609
		try {
1610
			$sth = $this->db->prepare($query);
1611
			$sth->execute(array(':stats_airline' => $stats_airline, ':filter_name' => $filter_name));
1612
		} catch(PDOException $e) {
1613
			echo "error : ".$e->getMessage();
1614
		}
1615
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1616
		return $all[0]['total'];
1617
	}
1618
	public function getStatsAirlineTotal($filter_name = '') {
1619
		global $globalArchiveMonths, $globalDBdriver;
1620
		if ($filter_name == '') $filter_name = $this->filter_name;
1621
		if ($globalDBdriver == 'mysql') {
1622
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
1623
		} else {
1624
			$query = "SELECT SUM(cnt) as total FROM stats_airline WHERE filter_name = :filter_name";
1625
		}
1626
		try {
1627
			$sth = $this->db->prepare($query);
1628
			$sth->execute(array(':filter_name' => $filter_name));
1629
		} catch(PDOException $e) {
1630
			echo "error : ".$e->getMessage();
1631
		}
1632
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1633
		return $all[0]['total'];
1634
	}
1635
	public function getStatsOwnerTotal($filter_name = '') {
1636
		global $globalArchiveMonths, $globalDBdriver;
1637
		if ($filter_name == '') $filter_name = $this->filter_name;
1638
		if ($globalDBdriver == 'mysql') {
1639
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
1640
		} else {
1641
			$query = "SELECT SUM(cnt) as total FROM stats_owner WHERE filter_name = :filter_name";
1642
		}
1643
		try {
1644
			$sth = $this->db->prepare($query);
1645
			$sth->execute(array(':filter_name' => $filter_name));
1646
		} catch(PDOException $e) {
1647
			echo "error : ".$e->getMessage();
1648
		}
1649
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1650
		return $all[0]['total'];
1651
	}
1652
	public function getStatsOwner($owner_name,$filter_name = '') {
1653
		global $globalArchiveMonths, $globalDBdriver;
1654
		if ($filter_name == '') $filter_name = $this->filter_name;
1655
		$query = "SELECT cnt FROM stats_owner WHERE filter_name = :filter_name AND owner_name = :owner_name";
1656
		try {
1657
			$sth = $this->db->prepare($query);
1658
			$sth->execute(array(':filter_name' => $filter_name,':owner_name' => $owner_name));
1659
		} catch(PDOException $e) {
1660
			echo "error : ".$e->getMessage();
1661
		}
1662
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1663
		if (isset($all[0]['cnt'])) return $all[0]['cnt'];
1664
		else return 0;
1665
	}
1666
	public function getStatsPilotTotal($filter_name = '') {
1667
		global $globalArchiveMonths, $globalDBdriver;
1668
		if ($filter_name == '') $filter_name = $this->filter_name;
1669
		if ($globalDBdriver == 'mysql') {
1670
			$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
1671
		} else {
1672
			$query = "SELECT SUM(cnt) as total FROM stats_pilot WHERE filter_name = :filter_name";
1673
		}
1674
		try {
1675
			$sth = $this->db->prepare($query);
1676
			$sth->execute(array(':filter_name' => $filter_name));
1677
		} catch(PDOException $e) {
1678
			echo "error : ".$e->getMessage();
1679
		}
1680
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1681
		return $all[0]['total'];
1682
	}
1683
	public function getStatsPilot($pilot,$filter_name = '') {
1684
		global $globalArchiveMonths, $globalDBdriver;
1685
		if ($filter_name == '') $filter_name = $this->filter_name;
1686
		$query = "SELECT cnt FROM stats_pilot WHERE filter_name = :filter_name AND (pilot_name = :pilot OR pilot_id = :pilot)";
1687
		try {
1688
			$sth = $this->db->prepare($query);
1689
			$sth->execute(array(':filter_name' => $filter_name,':pilot' => $pilot));
1690
		} catch(PDOException $e) {
1691
			echo "error : ".$e->getMessage();
1692
		}
1693
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1694
		if (isset($all[0]['cnt'])) return $all[0]['cnt'];
1695
		else return 0;
1696
	}
1697
1698
	public function addStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
1699
		global $globalDBdriver;
1700
		if ($filter_name == '') $filter_name = $this->filter_name;
1701
		if ($globalDBdriver == 'mysql') {
1702
			$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";
1703
		} else {
1704
			$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);"; 
1705
		}
1706
		$query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1707
		try {
1708
			$sth = $this->db->prepare($query);
1709
			$sth->execute($query_values);
1710
		} catch(PDOException $e) {
1711
			return "error : ".$e->getMessage();
1712
		}
1713
	}
1714
	public function updateStat($type,$cnt,$stats_date,$stats_airline = '',$filter_name = '') {
1715
		global $globalDBdriver;
1716
		if ($filter_name == '') $filter_name = $this->filter_name;
1717
		if ($globalDBdriver == 'mysql') {
1718
			$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";
1719
		} else {
1720
			//$query = "INSERT INTO stats (stats_type,cnt,stats_date) VALUES (:type,:cnt,:stats_date) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
1721
			$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);"; 
1722
		}
1723
		$query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1724
		try {
1725
			$sth = $this->db->prepare($query);
1726
			$sth->execute($query_values);
1727
		} catch(PDOException $e) {
1728
			return "error : ".$e->getMessage();
1729
		}
1730
	}
1731
        /*
1732
	public function getStatsSource($date,$stats_type = '') {
1733
		if ($stats_type == '') {
1734
			$query = "SELECT * FROM stats_source WHERE stats_date = :date ORDER BY source_name";
1735
			$query_values = array(':date' => $date);
1736
		} else {
1737
			$query = "SELECT * FROM stats_source WHERE stats_date = :date AND stats_type = :stats_type ORDER BY source_name";
1738
			$query_values = array(':date' => $date,':stats_type' => $stats_type);
1739
		}
1740
                 try {
1741
                        $sth = $this->db->prepare($query);
1742
                        $sth->execute($query_values);
1743
                } catch(PDOException $e) {
1744
                        echo "error : ".$e->getMessage();
1745
                }
1746
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
1747
                return $all;
1748
        }
1749
        */
1750
1751
	public function getStatsSource($stats_type,$year = '',$month = '',$day = '') {
1752
		global $globalDBdriver;
1753
		$query = "SELECT * FROM stats_source WHERE stats_type = :stats_type";
1754
		$query_values = array();
1755
		if ($globalDBdriver == 'mysql') {
1756
			if ($year != '') {
1757
				$query .= ' AND YEAR(stats_date) = :year';
1758
				$query_values = array_merge($query_values,array(':year' => $year));
1759
			}
1760
			if ($month != '') {
1761
				$query .= ' AND MONTH(stats_date) = :month';
1762
				$query_values = array_merge($query_values,array(':month' => $month));
1763
			}
1764
			if ($day != '') {
1765
				$query .= ' AND DAY(stats_date) = :day';
1766
				$query_values = array_merge($query_values,array(':day' => $day));
1767
			}
1768
		} else {
1769
			if ($year != '') {
1770
				$query .= ' AND EXTRACT(YEAR FROM stats_date) = :year';
1771
				$query_values = array_merge($query_values,array(':year' => $year));
1772
			}
1773
			if ($month != '') {
1774
				$query .= ' AND EXTRACT(MONTH FROM stats_date) = :month';
1775
				$query_values = array_merge($query_values,array(':month' => $month));
1776
			}
1777
			if ($day != '') {
1778
				$query .= ' AND EXTRACT(DAY FROM stats_date) = :day';
1779
				$query_values = array_merge($query_values,array(':day' => $day));
1780
			}
1781
		}
1782
		$query .= " ORDER BY source_name";
1783
		$query_values = array_merge($query_values,array(':stats_type' => $stats_type));
1784
		try {
1785
			$sth = $this->db->prepare($query);
1786
			$sth->execute($query_values);
1787
		} catch(PDOException $e) {
1788
			echo "error : ".$e->getMessage();
1789
		}
1790
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
1791
		return $all;
1792
	}
1793
1794
	public function addStatSource($data,$source_name,$stats_type,$date) {
1795
		global $globalDBdriver;
1796
		if ($globalDBdriver == 'mysql') {
1797
			$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";
1798
		} else {
1799
			$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);"; 
1800
		}
1801
		$query_values = array(':data' => $data,':stats_date' => $date,':source_name' => $source_name,':stats_type' => $stats_type);
1802
		try {
1803
			$sth = $this->db->prepare($query);
1804
			$sth->execute($query_values);
1805
		} catch(PDOException $e) {
1806
			return "error : ".$e->getMessage();
1807
		}
1808
	}
1809
	public function addStatFlight($type,$date_name,$cnt,$stats_airline = '',$filter_name = '') {
1810
		$query = "INSERT INTO stats_flight (stats_type,flight_date,cnt,stats_airline,filter_name) VALUES (:type,:flight_date,:cnt,:stats_airline,:filter_name)";
1811
		$query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt, ':stats_airline' => $stats_airline,':filter_name' => $filter_name);
1812
		try {
1813
			$sth = $this->db->prepare($query);
1814
			$sth->execute($query_values);
1815
		} catch(PDOException $e) {
1816
			return "error : ".$e->getMessage();
1817
		}
1818
	}
1819
	public function addStatMarine($type,$date_name,$cnt,$filter_name = '') {
1820
		$query = "INSERT INTO stats_marine (stats_type,marine_date,cnt,filter_name) VALUES (:type,:flight_date,:cnt,:filter_name)";
1821
		$query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt,':filter_name' => $filter_name);
1822
		try {
1823
			$sth = $this->db->prepare($query);
1824
			$sth->execute($query_values);
1825
		} catch(PDOException $e) {
1826
			return "error : ".$e->getMessage();
1827
		}
1828
	}
1829
	public function addStatTracker($type,$date_name,$cnt,$filter_name = '') {
1830
		$query = "INSERT INTO stats_tracker (stats_type,tracker_date,cnt,filter_name) VALUES (:type,:flight_date,:cnt,:filter_name)";
1831
		$query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt,':filter_name' => $filter_name);
1832
		try {
1833
			$sth = $this->db->prepare($query);
1834
			$sth->execute($query_values);
1835
		} catch(PDOException $e) {
1836
			return "error : ".$e->getMessage();
1837
		}
1838
	}
1839
	public function addStatAircraftRegistration($registration,$cnt,$aircraft_icao = '',$airline_icao = '',$filter_name = '',$reset = false) {
1840
		global $globalDBdriver;
1841
		if ($globalDBdriver == 'mysql') {
1842
			if ($reset) {
1843
				$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";
1844
			} else {
1845
				$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";
1846
			}
1847
		} else {
1848
			if ($reset) {
1849
				$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);"; 
1850
			} else {
1851
				$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);"; 
1852
			}
1853
		}
1854
		$query_values = array(':aircraft_icao' => $aircraft_icao,':registration' => $registration,':cnt' => $cnt,':stats_airline' => $airline_icao, ':filter_name' => $filter_name);
1855
		try {
1856
			$sth = $this->db->prepare($query);
1857
			$sth->execute($query_values);
1858
		} catch(PDOException $e) {
1859
			return "error : ".$e->getMessage();
1860
		}
1861
	}
1862
	public function addStatCallsign($callsign_icao,$cnt,$airline_icao = '', $filter_name = '', $reset = false) {
1863
		global $globalDBdriver;
1864
		if ($globalDBdriver == 'mysql') {
1865
			if ($reset) {
1866
				$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";
1867
			} else {
1868
				$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";
1869
			}
1870
		} else {
1871
			if ($reset) {
1872
				$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);"; 
1873
			} else {
1874
				$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);"; 
1875
			}
1876
		}
1877
		$query_values = array(':callsign_icao' => $callsign_icao,':airline_icao' => $airline_icao,':cnt' => $cnt, ':filter_name' => $filter_name);
1878
		try {
1879
			$sth = $this->db->prepare($query);
1880
			$sth->execute($query_values);
1881
		} catch(PDOException $e) {
1882
			return "error : ".$e->getMessage();
1883
		}
1884
	}
1885
	public function addStatCountry($iso2,$iso3,$name,$cnt,$airline_icao = '',$filter_name = '',$reset = false) {
1886
		global $globalDBdriver;
1887
		if ($globalDBdriver == 'mysql') {
1888
			if ($reset) {
1889
				$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";
1890
			} else {
1891
				$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";
1892
			}
1893
		} else {
1894
			if ($reset) {
1895
				$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);"; 
1896
			} else {
1897
				$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);"; 
1898
			}
1899
		}
1900
		$query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt,':filter_name' => $filter_name,':airline' => $airline_icao);
1901
		try {
1902
			$sth = $this->db->prepare($query);
1903
			$sth->execute($query_values);
1904
		} catch(PDOException $e) {
1905
			return "error : ".$e->getMessage();
1906
		}
1907
	}
1908
	public function addStatCountryMarine($iso2,$iso3,$name,$cnt,$filter_name = '',$reset = false) {
1909
		global $globalDBdriver;
1910
		if ($globalDBdriver == 'mysql') {
1911
			if ($reset) {
1912
				$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";
1913
			} else {
1914
				$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";
1915
			}
1916
		} else {
1917
			if ($reset) {
1918
				$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);"; 
1919
			} else {
1920
				$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);"; 
1921
			}
1922
		}
1923
		$query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt,':filter_name' => $filter_name);
1924
		try {
1925
			$sth = $this->db->prepare($query);
1926
			$sth->execute($query_values);
1927
		} catch(PDOException $e) {
1928
			return "error : ".$e->getMessage();
1929
		}
1930
	}
1931
	public function addStatCountryTracker($iso2,$iso3,$name,$cnt,$filter_name = '',$reset = false) {
1932
		global $globalDBdriver;
1933
		if ($globalDBdriver == 'mysql') {
1934
			if ($reset) {
1935
				$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";
1936
			} else {
1937
				$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";
1938
			}
1939
		} else {
1940
			if ($reset) {
1941
				$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);"; 
1942
			} else {
1943
				$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);"; 
1944
			}
1945
		}
1946
		$query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt,':filter_name' => $filter_name);
1947
		try {
1948
			$sth = $this->db->prepare($query);
1949
			$sth->execute($query_values);
1950
		} catch(PDOException $e) {
1951
			return "error : ".$e->getMessage();
1952
		}
1953
	}
1954
	public function addStatAircraft($aircraft_icao,$cnt,$aircraft_name = '',$aircraft_manufacturer = '', $airline_icao = '', $filter_name = '', $reset = false) {
1955
		global $globalDBdriver;
1956
		if ($globalDBdriver == 'mysql') {
1957
			if ($reset) {
1958
				$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";
1959
			} else {
1960
				$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";
1961
			}
1962
		} else {
1963
			if ($reset) {
1964
				$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);"; 
1965
			} else {
1966
				$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);"; 
1967
			}
1968
		}
1969
		$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);
1970
		try {
1971
			$sth = $this->db->prepare($query);
1972
			$sth->execute($query_values);
1973
		} catch(PDOException $e) {
1974
			return "error : ".$e->getMessage();
1975
		}
1976
	}
1977
	public function addStatAirline($airline_icao,$cnt,$airline_name = '',$filter_name = '', $reset = false) {
1978
		global $globalDBdriver;
1979
		if ($globalDBdriver == 'mysql') {
1980
			if ($reset) {
1981
				$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";
1982
			} else {
1983
				$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";
1984
			}
1985
		} else {
1986
			if ($reset) {
1987
				$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);"; 
1988
			} else {
1989
				$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);"; 
1990
			}
1991
		}
1992
		$query_values = array(':airline_icao' => $airline_icao,':airline_name' => $airline_name,':cnt' => $cnt,':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
	public function addStatOwner($owner_name,$cnt,$stats_airline = '', $filter_name = '', $reset = false) {
2001
		global $globalDBdriver;
2002
		if ($globalDBdriver == 'mysql') {
2003
			if ($reset) {
2004
				$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";
2005
			} else {
2006
				$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";
2007
			}
2008
		} else {
2009
			if ($reset) {
2010
				$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);"; 
2011
			} else {
2012
				$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);"; 
2013
			}
2014
		}
2015
		$query_values = array(':owner_name' => $owner_name,':cnt' => $cnt,':stats_airline' => $stats_airline,':filter_name' => $filter_name);
2016
		try {
2017
			$sth = $this->db->prepare($query);
2018
			$sth->execute($query_values);
2019
		} catch(PDOException $e) {
2020
			return "error : ".$e->getMessage();
2021
		}
2022
	}
2023
	public function addStatPilot($pilot_id,$cnt,$pilot_name,$stats_airline = '',$filter_name = '',$format_source = '',$reset = false) {
2024
		global $globalDBdriver;
2025
		if ($globalDBdriver == 'mysql') {
2026
			if ($reset) {
2027
				$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";
2028
			} else {
2029
				$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";
2030
			}
2031
		} else {
2032
			if ($reset) {
2033
				$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);"; 
2034
			} else {
2035
				$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);"; 
2036
			}
2037
		}
2038
		$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);
2039
		try {
2040
			$sth = $this->db->prepare($query);
2041
			$sth->execute($query_values);
2042
		} catch(PDOException $e) {
2043
			return "error : ".$e->getMessage();
2044
		}
2045
	}
2046
	public function addStatDepartureAirports($airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '',$reset = false) {
2047
		global $globalDBdriver;
2048
		if ($airport_icao != '') {
2049
			if ($globalDBdriver == 'mysql') {
2050
				if ($reset) {
2051
					$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";
2052
				} else {
2053
					$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";
2054
				}
2055
			} else {
2056
				if ($reset) {
2057
					$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);"; 
2058
				} else {
2059
					$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);"; 
2060
				}
2061
			}
2062
			$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);
2063
			try {
2064
				$sth = $this->db->prepare($query);
2065
				$sth->execute($query_values);
2066
			} catch(PDOException $e) {
2067
				return "error : ".$e->getMessage();
2068
			}
2069
		}
2070
	}
2071
	public function addStatDepartureAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '',$filter_name = '') {
2072
		global $globalDBdriver;
2073
		if ($airport_icao != '') {
2074
			if ($globalDBdriver == 'mysql') {
2075
				$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";
2076
			} else {
2077
				$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);"; 
2078
			}
2079
			$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);
2080
			 try {
2081
				$sth = $this->db->prepare($query);
2082
				$sth->execute($query_values);
2083
			} catch(PDOException $e) {
2084
				return "error : ".$e->getMessage();
2085
			}
2086
		}
2087
	}
2088
	public function addStatArrivalAirports($airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '',$reset = false) {
2089
		global $globalDBdriver;
2090
		if ($airport_icao != '') {
2091
			if ($globalDBdriver == 'mysql') {
2092
				if ($reset) {
2093
					$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";
2094
				} else {
2095
					$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";
2096
				}
2097
			} else {
2098
				if ($reset) {
2099
					$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);"; 
2100
				} else {
2101
					$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);"; 
2102
				}
2103
			}
2104
			$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);
2105
			try {
2106
				$sth = $this->db->prepare($query);
2107
				$sth->execute($query_values);
2108
			} catch(PDOException $e) {
2109
				return "error : ".$e->getMessage();
2110
			}
2111
		}
2112
	}
2113
	public function addStatArrivalAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '',$filter_name = '') {
2114
		global $globalDBdriver;
2115
		if ($airport_icao != '') {
2116
			if ($globalDBdriver == 'mysql') {
2117
				$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";
2118
			} else {
2119
				$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);"; 
2120
			}
2121
			$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);
2122
			try {
2123
				$sth = $this->db->prepare($query);
2124
				$sth->execute($query_values);
2125
			} catch(PDOException $e) {
2126
				return "error : ".$e->getMessage();
2127
			}
2128
		}
2129
	}
2130
2131
	public function deleteStat($id) {
2132
		$query = "DELETE FROM stats WHERE stats_id = :id";
2133
		$query_values = array(':id' => $id);
2134
		try {
2135
			$sth = $this->db->prepare($query);
2136
			$sth->execute($query_values);
2137
		} catch(PDOException $e) {
2138
			return "error : ".$e->getMessage();
2139
		}
2140
	}
2141
	public function deleteStatFlight($type) {
2142
		$query = "DELETE FROM stats_flight WHERE stats_type = :type";
2143
		$query_values = array(':type' => $type);
2144
		try {
2145
			$sth = $this->db->prepare($query);
2146
			$sth->execute($query_values);
2147
		} catch(PDOException $e) {
2148
			return "error : ".$e->getMessage();
2149
		}
2150
	}
2151
	public function deleteStatMarine($type) {
2152
		$query = "DELETE FROM stats_marine WHERE stats_type = :type";
2153
		$query_values = array(':type' => $type);
2154
		try {
2155
			$sth = $this->db->prepare($query);
2156
			$sth->execute($query_values);
2157
		} catch(PDOException $e) {
2158
			return "error : ".$e->getMessage();
2159
		}
2160
	}
2161
	public function deleteStatTracker($type) {
2162
		$query = "DELETE FROM stats_tracker WHERE stats_type = :type";
2163
		$query_values = array(':type' => $type);
2164
		try {
2165
			$sth = $this->db->prepare($query);
2166
			$sth->execute($query_values);
2167
		} catch(PDOException $e) {
2168
			return "error : ".$e->getMessage();
2169
		}
2170
	}
2171
	public function deleteStatAirport($type) {
2172
		$query = "DELETE FROM stats_airport WHERE stats_type = :type";
2173
		$query_values = array(':type' => $type);
2174
		try {
2175
			$sth = $this->db->prepare($query);
2176
			$sth->execute($query_values);
2177
		} catch(PDOException $e) {
2178
			return "error : ".$e->getMessage();
2179
		}
2180
	}
2181
2182
	public function addOldStats() {
2183
		global $globalMasterServer, $globalAircraft, $globalMarine, $globalTracker, $globalDebug, $globalArchiveMonths, $globalArchive, $globalArchiveYear, $globalDBdriver, $globalStatsFilters,$globalDeleteLastYearStats,$globalStatsReset,$globalStatsResetYear, $globalAccidents;
2184
		$Common = new Common();
2185
		$Connection = new Connection($this->db);
2186
		date_default_timezone_set('UTC');
2187
		if ((isset($globalMarine) && $globalMarine) || (isset($globalMasterServer) && $globalMasterServer)) {
2188
			$last_update = $this->getLastStatsUpdate('last_update_stats_marine');
2189
			if ($globalDebug) echo '!!! Update Marine stats !!!'."\n";
2190
			if (isset($last_update[0]['value'])) {
2191
				$last_update_day = $last_update[0]['value'];
2192
			} else $last_update_day = '2012-12-12 12:12:12';
2193
			$reset = false;
2194
			$Marine = new Marine($this->db);
2195
			$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...
2196
			if ($Connection->tableExists('countries')) {
2197
				if ($globalDebug) echo 'Count all vessels by countries...'."\n";
2198
				$alldata = $Marine->countAllMarineOverCountries(false,0,$last_update_day);
2199
				foreach ($alldata as $number) {
2200
					$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...
2201
				}
2202
			}
2203
			if ($globalDebug) echo 'Count all vessels by months...'."\n";
2204
			$last_month = date('Y-m-01 00:00:00', strtotime('-1 month', strtotime($last_update_day)));
2205
			$filter_last_month = array('since_date' => $last_month);
2206
			$alldata = $Marine->countAllMonths($filter_last_month);
2207
			$lastyear = false;
2208
			foreach ($alldata as $number) {
2209
				if ($number['year_name'] != date('Y')) $lastyear = true;
2210
				$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'])));
2211
			}
2212
			echo 'Marine data...'."\n";
2213
			$this->deleteStatMarine('month');
2214
			echo '-> countAllDatesLastMonth...'."\n";
2215
			$alldata = $Marine->countAllDatesLastMonth($filter_last_month);
2216
			foreach ($alldata as $number) {
2217
				$this->addStatMarine('month',$number['date_name'],$number['date_count']);
2218
			}
2219
			echo '-> countAllDates...'."\n";
2220
			$previousdata = $this->countAllDatesMarine();
2221
			$this->deleteStatMarine('date');
2222
			$alldata = $Common->array_merge_noappend($previousdata,$Marine->countAllDates($filter_last_month));
2223
			$values = array();
2224
			foreach ($alldata as $cnt) {
2225
				$values[] = $cnt['date_count'];
2226
			}
2227
			array_multisort($values,SORT_DESC,$alldata);
2228
			array_splice($alldata,11);
2229
			foreach ($alldata as $number) {
2230
				$this->addStatMarine('date',$number['date_name'],$number['date_count']);
2231
			}
2232
			
2233
			$this->deleteStatMarine('hour');
2234
			echo '-> countAllHours...'."\n";
2235
			$alldata = $Marine->countAllHours('hour',$filter_last_month);
2236
			foreach ($alldata as $number) {
2237
				$this->addStatMarine('hour',$number['hour_name'],$number['hour_count']);
2238
			}
2239
			echo 'Insert last stats update date...'."\n";
2240
			date_default_timezone_set('UTC');
2241
			$this->addLastStatsUpdate('last_update_stats_marine',date('Y-m-d G:i:s'));
2242
		}
2243
		if ((isset($globalTracker) && $globalTracker) || (isset($globalMasterServer) && $globalMasterServer)) {
2244
			$last_update = $this->getLastStatsUpdate('last_update_stats_tracker');
2245
			if ($globalDebug) echo '!!! Update tracker stats !!!'."\n";
2246
			if (isset($last_update[0]['value'])) {
2247
				$last_update_day = $last_update[0]['value'];
2248
			} else $last_update_day = '2012-12-12 12:12:12';
2249
			$reset = false;
2250
			$Tracker = new Tracker($this->db);
2251
			if ($Connection->tableExists('countries')) {
2252
				if ($globalDebug) echo 'Count all trackers by countries...'."\n";
2253
				$alldata = $Tracker->countAllTrackerOverCountries(false,0,$last_update_day);
2254
				foreach ($alldata as $number) {
2255
					$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...
2256
				}
2257
			}
2258
			if ($globalDebug) echo 'Count all vessels by months...'."\n";
2259
			$last_month = date('Y-m-01 00:00:00', strtotime('-1 month', strtotime($last_update_day)));
2260
			$filter_last_month = array('since_date' => $last_month);
2261
			$alldata = $Tracker->countAllMonths($filter_last_month);
2262
			$lastyear = false;
2263
			foreach ($alldata as $number) {
2264
				if ($number['year_name'] != date('Y')) $lastyear = true;
2265
				$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'])));
2266
			}
2267
			echo 'Tracker data...'."\n";
2268
			$this->deleteStatTracker('month');
2269
			echo '-> countAllDatesLastMonth...'."\n";
2270
			$alldata = $Tracker->countAllDatesLastMonth($filter_last_month);
2271
			foreach ($alldata as $number) {
2272
				$this->addStatTracker('month',$number['date_name'],$number['date_count']);
2273
			}
2274
			echo '-> countAllDates...'."\n";
2275
			$previousdata = $this->countAllDatesTracker();
2276
			$this->deleteStatTracker('date');
2277
			$alldata = $Common->array_merge_noappend($previousdata,$Tracker->countAllDates($filter_last_month));
2278
			$values = array();
2279
			foreach ($alldata as $cnt) {
2280
				$values[] = $cnt['date_count'];
2281
			}
2282
			array_multisort($values,SORT_DESC,$alldata);
2283
			array_splice($alldata,11);
2284
			foreach ($alldata as $number) {
2285
				$this->addStatTracker('date',$number['date_name'],$number['date_count']);
2286
			}
2287
			
2288
			$this->deleteStatTracker('hour');
2289
			echo '-> countAllHours...'."\n";
2290
			$alldata = $Tracker->countAllHours('hour',$filter_last_month);
2291
			foreach ($alldata as $number) {
2292
				$this->addStatTracker('hour',$number['hour_name'],$number['hour_count']);
2293
			}
2294
			echo 'Insert last stats update date...'."\n";
2295
			date_default_timezone_set('UTC');
2296
			$this->addLastStatsUpdate('last_update_stats_tracker',date('Y-m-d G:i:s'));
2297
		}
2298
2299
		if (!isset($globalAircraft) || (isset($globalAircraft) && $globalAircraft) || (isset($globalMasterServer) && $globalMasterServer)) {
2300
			$last_update = $this->getLastStatsUpdate('last_update_stats');
2301
			if ($globalDebug) echo '!!! Update aicraft stats !!!'."\n";
2302
			if (isset($last_update[0]['value'])) {
2303
				$last_update_day = $last_update[0]['value'];
2304
			} else $last_update_day = '2012-12-12 12:12:12';
2305
			$reset = false;
2306
			//if ($globalStatsResetYear && date('Y',strtotime($last_update_day)) != date('Y')) {
2307
			if ($globalStatsResetYear) {
2308
				$reset = true;
2309
				$last_update_day = date('Y').'-01-01 00:00:00';
2310
			}
2311
			$Spotter = new Spotter($this->db);
2312
2313
			if ($globalDebug) echo 'Count all aircraft types...'."\n";
2314
			$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day);
2315
			foreach ($alldata as $number) {
2316
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'','',$reset);
2317
			}
2318
			if ($globalDebug) echo 'Count all airlines...'."\n";
2319
			$alldata = $Spotter->countAllAirlines(false,0,$last_update_day);
2320
			foreach ($alldata as $number) {
2321
				$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],'',$reset);
2322
			}
2323
			if ($globalDebug) echo 'Count all registrations...'."\n";
2324
			$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day);
2325
			foreach ($alldata as $number) {
2326
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'','',$reset);
2327
			}
2328
			if ($globalDebug) echo 'Count all callsigns...'."\n";
2329
			$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day);
2330
			foreach ($alldata as $number) {
2331
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
2332
			}
2333
			if ($globalDebug) echo 'Count all owners...'."\n";
2334
			$alldata = $Spotter->countAllOwners(false,0,$last_update_day);
2335
			foreach ($alldata as $number) {
2336
				$this->addStatOwner($number['owner_name'],$number['owner_count'],'','',$reset);
2337
			}
2338
			if ($globalDebug) echo 'Count all pilots...'."\n";
2339
			$alldata = $Spotter->countAllPilots(false,0,$last_update_day);
2340
			foreach ($alldata as $number) {
2341
				if ($number['pilot_id'] == 0 || $number['pilot_id'] == '') $number['pilot_id'] = $number['pilot_name'];
2342
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'','',$number['format_source'],$reset);
2343
			}
2344
			
2345
			if ($globalDebug) echo 'Count all departure airports...'."\n";
2346
			$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day);
2347
			if ($globalDebug) echo 'Count all detected departure airports...'."\n";
2348
			$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day);
2349
			if ($globalDebug) echo 'Order departure airports...'."\n";
2350
			$alldata = array();
2351
			foreach ($pall as $value) {
2352
				$icao = $value['airport_departure_icao'];
2353
				$alldata[$icao] = $value;
2354
			}
2355
			foreach ($dall as $value) {
2356
				$icao = $value['airport_departure_icao'];
2357
				if (isset($alldata[$icao])) {
2358
					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2359
				} else $alldata[$icao] = $value;
2360
			}
2361
			$count = array();
2362
			foreach ($alldata as $key => $row) {
2363
				$count[$key] = $row['airport_departure_icao_count'];
2364
			}
2365
			array_multisort($count,SORT_DESC,$alldata);
2366
			foreach ($alldata as $number) {
2367
				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);
2368
			}
2369
			if ($globalDebug) echo 'Count all arrival airports...'."\n";
2370
			$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day);
2371
			if ($globalDebug) echo 'Count all detected arrival airports...'."\n";
2372
			$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day);
2373
			if ($globalDebug) echo 'Order arrival airports...'."\n";
2374
			$alldata = array();
2375
			foreach ($pall as $value) {
2376
				$icao = $value['airport_arrival_icao'];
2377
				$alldata[$icao] = $value;
2378
			}
2379
			foreach ($dall as $value) {
2380
				$icao = $value['airport_arrival_icao'];
2381
				if (isset($alldata[$icao])) {
2382
					$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2383
				} else $alldata[$icao] = $value;
2384
			}
2385
			$count = array();
2386
			foreach ($alldata as $key => $row) {
2387
				$count[$key] = $row['airport_arrival_icao_count'];
2388
			}
2389
			array_multisort($count,SORT_DESC,$alldata);
2390
			foreach ($alldata as $number) {
2391
				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);
2392
			}
2393
			if ($Connection->tableExists('countries')) {
2394
				if ($globalDebug) echo 'Count all flights by countries...'."\n";
2395
				//$SpotterArchive = new SpotterArchive();
2396
				//$alldata = $SpotterArchive->countAllFlightOverCountries(false,0,$last_update_day);
2397
				$Spotter = new Spotter($this->db);
2398
				$alldata = $Spotter->countAllFlightOverCountries(false,0,$last_update_day);
2399
				foreach ($alldata as $number) {
2400
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],'','',$reset);
2401
				}
2402
			}
2403
			
2404
			if (isset($globalAccidents) && $globalAccidents) {
2405
				if ($globalDebug) echo 'Count fatalities stats...'."\n";
2406
				$Accident = new Accident($this->db);
2407
				$this->deleteStatsByType('fatalities_byyear');
2408
				$alldata = $Accident->countFatalitiesByYear();
2409
				foreach ($alldata as $number) {
2410
					$this->addStat('fatalities_byyear',$number['count'],date('Y-m-d H:i:s',mktime(0,0,0,1,1,$number['year'])));
2411
				}
2412
				$this->deleteStatsByType('fatalities_bymonth');
2413
				$alldata = $Accident->countFatalitiesLast12Months();
2414
				foreach ($alldata as $number) {
2415
					$this->addStat('fatalities_bymonth',$number['count'],date('Y-m-d H:i:s',mktime(0,0,0,$number['month'],1,$number['year'])));
2416
				}
2417
			}
2418
2419
			// Add by month using getstat if month finish...
2420
			//if (date('m',strtotime($last_update_day)) != date('m')) {
2421
			if ($globalDebug) echo 'Count all flights by months...'."\n";
2422
			$last_month = date('Y-m-01 00:00:00', strtotime('-1 month', strtotime($last_update_day)));
2423
			$filter_last_month = array('since_date' => $last_month);
2424
			$Spotter = new Spotter($this->db);
2425
			$alldata = $Spotter->countAllMonths($filter_last_month);
2426
			$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...
2427
			foreach ($alldata as $number) {
2428
				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...
2429
				$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'])));
2430
			}
2431
			if ($globalDebug) echo 'Count all military flights by months...'."\n";
2432
			$alldata = $Spotter->countAllMilitaryMonths($filter_last_month);
2433
			foreach ($alldata as $number) {
2434
				$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'])));
2435
			}
2436
			if ($globalDebug) echo 'Count all owners by months...'."\n";
2437
			$alldata = $Spotter->countAllMonthsOwners($filter_last_month);
2438
			foreach ($alldata as $number) {
2439
				$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'])));
2440
			}
2441
			if ($globalDebug) echo 'Count all pilots by months...'."\n";
2442
			$alldata = $Spotter->countAllMonthsPilots($filter_last_month);
2443
			foreach ($alldata as $number) {
2444
				$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'])));
2445
			}
2446
			if ($globalDebug) echo 'Count all airlines by months...'."\n";
2447
			$alldata = $Spotter->countAllMonthsAirlines($filter_last_month);
2448
			foreach ($alldata as $number) {
2449
				$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'])));
2450
			}
2451
			if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
2452
			$alldata = $Spotter->countAllMonthsAircrafts($filter_last_month);
2453
			foreach ($alldata as $number) {
2454
				$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'])));
2455
			}
2456
			if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
2457
			$alldata = $Spotter->countAllMonthsRealArrivals($filter_last_month);
2458
			foreach ($alldata as $number) {
2459
				$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'])));
2460
			}
2461
			if ($globalDebug) echo 'Airports data...'."\n";
2462
			if ($globalDebug) echo '...Departure'."\n";
2463
			$this->deleteStatAirport('daily');
2464
//			$pall = $Spotter->getLast7DaysAirportsDeparture();
2465
  //      		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
2466
			$pall = $Spotter->getLast7DaysAirportsDeparture();
2467
			$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
2468
			/*
2469
			$alldata = array();
2470
			foreach ($pall as $value) {
2471
				$icao = $value['departure_airport_icao'];
2472
				$alldata[$icao] = $value;
2473
			}
2474
			foreach ($dall as $value) {
2475
				$icao = $value['departure_airport_icao'];
2476
				$ddate = $value['date'];
2477
				if (isset($alldata[$icao])) {
2478
					$alldata[$icao]['departure_airport_count'] = $alldata[$icao]['departure_airport_count'] + $value['departure_airport_count'];
2479
				} else $alldata[$icao] = $value;
2480
			}
2481
			$count = array();
2482
			foreach ($alldata as $key => $row) {
2483
				$count[$key] = $row['departure_airport_count'];
2484
			}
2485
			array_multisort($count,SORT_DESC,$alldata);
2486
			*/
2487
			foreach ($dall as $value) {
2488
				$icao = $value['departure_airport_icao'];
2489
				$ddate = $value['date'];
2490
				$find = false;
2491
				foreach ($pall as $pvalue) {
2492
					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2493
						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2494
						$find = true;
2495
						break;
2496
					}
2497
				}
2498
				if ($find === false) {
2499
					$pall[] = $value;
2500
				}
2501
			}
2502
			$alldata = $pall;
2503
			foreach ($alldata as $number) {
2504
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count']);
2505
			}
2506
			echo '...Arrival'."\n";
2507
			$pall = $Spotter->getLast7DaysAirportsArrival();
2508
			$dall = $Spotter->getLast7DaysDetectedAirportsArrival();
2509
			/*
2510
			$alldata = array();
2511
			foreach ($pall as $value) {
2512
				$icao = $value['arrival_airport_icao'];
2513
				$alldata[$icao] = $value;
2514
			}
2515
			foreach ($dall as $value) {
2516
				$icao = $value['arrival_airport_icao'];
2517
				if (isset($alldata[$icao])) {
2518
					$alldata[$icao]['arrival_airport_icao_count'] = $alldata[$icao]['arrival_airport_count'] + $value['arrival_airport_count'];
2519
				} else $alldata[$icao] = $value;
2520
			}
2521
			$count = array();
2522
			foreach ($alldata as $key => $row) {
2523
				$count[$key] = $row['arrival_airport_count'];
2524
			}
2525
			array_multisort($count,SORT_DESC,$alldata);
2526
			*/
2527
2528
			foreach ($dall as $value) {
2529
				$icao = $value['arrival_airport_icao'];
2530
				$ddate = $value['date'];
2531
				$find = false;
2532
				foreach ($pall as $pvalue) {
2533
					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2534
						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2535
						$find = true;
2536
						break;
2537
					}
2538
				}
2539
				if ($find === false) {
2540
						$pall[] = $value;
2541
				}
2542
			}
2543
			$alldata = $pall;
2544
			foreach ($alldata as $number) {
2545
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count']);
2546
			}
2547
2548
			echo 'Flights data...'."\n";
2549
			$this->deleteStatFlight('month');
2550
			echo '-> countAllDatesLastMonth...'."\n";
2551
			$alldata = $Spotter->countAllDatesLastMonth($filter_last_month);
2552
			foreach ($alldata as $number) {
2553
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
2554
			}
2555
			echo '-> countAllDates...'."\n";
2556
			$previousdata = $this->countAllDates();
2557
			$previousdatabyairlines = $this->countAllDatesByAirlines();
2558
			$this->deleteStatFlight('date');
2559
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter_last_month));
2560
			$values = array();
2561
			foreach ($alldata as $cnt) {
2562
				$values[] = $cnt['date_count'];
2563
			}
2564
			array_multisort($values,SORT_DESC,$alldata);
2565
			array_splice($alldata,11);
2566
			foreach ($alldata as $number) {
2567
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
2568
			}
2569
			
2570
			$this->deleteStatFlight('hour');
2571
			echo '-> countAllHours...'."\n";
2572
			$alldata = $Spotter->countAllHours('hour',$filter_last_month);
2573
			foreach ($alldata as $number) {
2574
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
2575
			}
2576
2577
			// Count by airlines
2578
			echo '--- Stats by airlines ---'."\n";
2579
			if ($Connection->tableExists('countries')) {
2580
				if ($globalDebug) echo 'Count all flights by countries by airlines...'."\n";
2581
				$SpotterArchive = new SpotterArchive($this->db);
2582
				//$Spotter = new Spotter($this->db);
2583
				$alldata = $SpotterArchive->countAllFlightOverCountriesByAirlines(false,0,$last_update_day);
2584
				//$alldata = $Spotter->countAllFlightOverCountriesByAirlines(false,0,$last_update_day);
2585
				foreach ($alldata as $number) {
2586
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count'],$number['airline_icao'],'',$reset);
2587
				}
2588
			}
2589
			if ($globalDebug) echo 'Count all aircraft types by airlines...'."\n";
2590
			$Spotter = new Spotter($this->db);
2591
			$alldata = $Spotter->countAllAircraftTypesByAirlines(false,0,$last_update_day);
2592
			foreach ($alldata as $number) {
2593
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],$number['airline_icao'],'',$reset);
2594
			}
2595
			if ($globalDebug) echo 'Count all aircraft registrations by airlines...'."\n";
2596
			$alldata = $Spotter->countAllAircraftRegistrationsByAirlines(false,0,$last_update_day);
2597
			foreach ($alldata as $number) {
2598
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],$number['airline_icao'],'',$reset);
2599
			}
2600
			if ($globalDebug) echo 'Count all callsigns by airlines...'."\n";
2601
			$alldata = $Spotter->countAllCallsignsByAirlines(false,0,$last_update_day);
2602
			foreach ($alldata as $number) {
2603
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao'],'',$reset);
2604
			}
2605
			if ($globalDebug) echo 'Count all owners by airlines...'."\n";
2606
			$alldata = $Spotter->countAllOwnersByAirlines(false,0,$last_update_day);
2607
			foreach ($alldata as $number) {
2608
				$this->addStatOwner($number['owner_name'],$number['owner_count'],$number['airline_icao'],'',$reset);
2609
			}
2610
			if ($globalDebug) echo 'Count all pilots by airlines...'."\n";
2611
			$alldata = $Spotter->countAllPilotsByAirlines(false,0,$last_update_day);
2612
			foreach ($alldata as $number) {
2613
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],$number['airline_icao'],'',$number['format_source'],$reset);
2614
			}
2615
			if ($globalDebug) echo 'Count all departure airports by airlines...'."\n";
2616
			$pall = $Spotter->countAllDepartureAirportsByAirlines(false,0,$last_update_day);
2617
			if ($globalDebug) echo 'Count all detected departure airports by airlines...'."\n";
2618
			$dall = $Spotter->countAllDetectedDepartureAirportsByAirlines(false,0,$last_update_day);
2619
			if ($globalDebug) echo 'Order detected departure airports by airlines...'."\n";
2620
			//$alldata = array();
2621
			foreach ($dall as $value) {
2622
				$icao = $value['airport_departure_icao'];
2623
				$dicao = $value['airline_icao'];
2624
				$find = false;
2625
				foreach ($pall as $pvalue) {
2626
					if ($pvalue['airport_departure_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
2627
						$pvalue['airport_departure_icao_count'] = $pvalue['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2628
						$find = true;
2629
						break;
2630
					}
2631
				}
2632
				if ($find === false) {
2633
					$pall[] = $value;
2634
				}
2635
			}
2636
			$alldata = $pall;
2637
			foreach ($alldata as $number) {
2638
				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);
2639
			}
2640
			if ($globalDebug) echo 'Count all arrival airports by airlines...'."\n";
2641
			$pall = $Spotter->countAllArrivalAirportsByAirlines(false,0,$last_update_day);
2642
			if ($globalDebug) echo 'Count all detected arrival airports by airlines...'."\n";
2643
			$dall = $Spotter->countAllDetectedArrivalAirportsByAirlines(false,0,$last_update_day);
2644
			if ($globalDebug) echo 'Order arrival airports by airlines...'."\n";
2645
			//$alldata = array();
2646
			foreach ($dall as $value) {
2647
				$icao = $value['airport_arrival_icao'];
2648
				$dicao = $value['airline_icao'];
2649
				$find = false;
2650
				foreach ($pall as $pvalue) {
2651
					if ($pvalue['airport_arrival_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
2652
						$pvalue['airport_arrival_icao_count'] = $pvalue['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2653
						$find = true;
2654
						break;
2655
					}
2656
				}
2657
				if ($find === false) {
2658
					$pall[] = $value;
2659
				}
2660
			}
2661
			$alldata = $pall;
2662
			foreach ($alldata as $number) {
2663
				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);
2664
			}
2665
			if ($globalDebug) echo 'Count all flights by months by airlines...'."\n";
2666
			$Spotter = new Spotter($this->db);
2667
			$alldata = $Spotter->countAllMonthsByAirlines($filter_last_month);
2668
			$lastyear = false;
2669
			foreach ($alldata as $number) {
2670
				if ($number['year_name'] != date('Y')) $lastyear = true;
2671
				$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']);
2672
			}
2673
			if ($globalDebug) echo 'Count all owners by months by airlines...'."\n";
2674
			$alldata = $Spotter->countAllMonthsOwnersByAirlines($filter_last_month);
2675
			foreach ($alldata as $number) {
2676
				$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']);
2677
			}
2678
			if ($globalDebug) echo 'Count all pilots by months by airlines...'."\n";
2679
			$alldata = $Spotter->countAllMonthsPilotsByAirlines($filter_last_month);
2680
			foreach ($alldata as $number) {
2681
				$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']);
2682
			}
2683
			if ($globalDebug) echo 'Count all aircrafts by months by airlines...'."\n";
2684
			$alldata = $Spotter->countAllMonthsAircraftsByAirlines($filter_last_month);
2685
			foreach ($alldata as $number) {
2686
				$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']);
2687
			}
2688
			if ($globalDebug) echo 'Count all real arrivals by months by airlines...'."\n";
2689
			$alldata = $Spotter->countAllMonthsRealArrivalsByAirlines($filter_last_month);
2690
			foreach ($alldata as $number) {
2691
				$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']);
2692
			}
2693
			if ($globalDebug) echo '...Departure'."\n";
2694
			$pall = $Spotter->getLast7DaysAirportsDepartureByAirlines();
2695
			$dall = $Spotter->getLast7DaysDetectedAirportsDepartureByAirlines();
2696
			foreach ($dall as $value) {
2697
				$icao = $value['departure_airport_icao'];
2698
				$airline = $value['airline_icao'];
2699
				$ddate = $value['date'];
2700
				$find = false;
2701
				foreach ($pall as $pvalue) {
2702
					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] = $airline) {
2703
						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2704
						$find = true;
2705
						break;
2706
					}
2707
				}
2708
				if ($find === false) {
2709
					$pall[] = $value;
2710
				}
2711
			}
2712
			$alldata = $pall;
2713
			foreach ($alldata as $number) {
2714
				$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']);
2715
			}
2716
			if ($globalDebug) echo '...Arrival'."\n";
2717
			$pall = $Spotter->getLast7DaysAirportsArrivalByAirlines();
2718
			$dall = $Spotter->getLast7DaysDetectedAirportsArrivalByAirlines();
2719
			foreach ($dall as $value) {
2720
				$icao = $value['arrival_airport_icao'];
2721
				$airline = $value['airline_icao'];
2722
				$ddate = $value['date'];
2723
				$find = false;
2724
				foreach ($pall as $pvalue) {
2725
					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] == $airline) {
2726
						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2727
						$find = true;
2728
						break;
2729
					}
2730
				}
2731
				if ($find === false) {
2732
					$pall[] = $value;
2733
				}
2734
			}
2735
			$alldata = $pall;
2736
			foreach ($alldata as $number) {
2737
				$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']);
2738
			}
2739
2740
			if ($globalDebug) echo 'Flights data...'."\n";
2741
			if ($globalDebug) echo '-> countAllDatesLastMonth...'."\n";
2742
			$alldata = $Spotter->countAllDatesLastMonthByAirlines($filter_last_month);
2743
			foreach ($alldata as $number) {
2744
				$this->addStatFlight('month',$number['date_name'],$number['date_count'], $number['airline_icao']);
2745
			}
2746
			if ($globalDebug) echo '-> countAllDates...'."\n";
2747
			//$previousdata = $this->countAllDatesByAirlines();
2748
			$alldata = $Common->array_merge_noappend($previousdatabyairlines,$Spotter->countAllDatesByAirlines($filter_last_month));
2749
			$values = array();
2750
			foreach ($alldata as $cnt) {
2751
				$values[] = $cnt['date_count'];
2752
			}
2753
			array_multisort($values,SORT_DESC,$alldata);
2754
			array_splice($alldata,11);
2755
			foreach ($alldata as $number) {
2756
				$this->addStatFlight('date',$number['date_name'],$number['date_count'],$number['airline_icao']);
2757
			}
2758
			
2759
			if ($globalDebug) echo '-> countAllHours...'."\n";
2760
			$alldata = $Spotter->countAllHoursByAirlines('hour',$filter_last_month);
2761
			foreach ($alldata as $number) {
2762
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],$number['airline_icao']);
2763
			}
2764
2765
			// Stats by filters
2766
			if (!isset($globalStatsFilters) || $globalStatsFilters == '') $globalStatsFilters = array();
2767
			foreach ($globalStatsFilters as $name => $filter) {
2768
				if (!empty($filter)) {
2769
					//$filter_name = $filter['name'];
2770
					$filter_name = $name;
2771
					$reset = false;
2772
					$last_update = $this->getLastStatsUpdate('last_update_stats_'.$filter_name);
2773
					if (isset($filter['resetall']) && isset($last_update[0]['value']) && strtotime($filter['resetall']) > strtotime($last_update[0]['value'])) {
2774
						if ($globalDebug) echo '!!! Delete stats for filter '.$filter_name.' !!!'."\n";
2775
						$this->deleteOldStats($filter_name);
2776
						unset($last_update);
2777
					}
2778
					if (isset($last_update[0]['value'])) {
2779
						$last_update_day = $last_update[0]['value'];
2780
					} else {
2781
						$last_update_day = '2012-12-12 12:12:12';
2782
						if (isset($filter['DeleteLastYearStats'])) {
2783
							$last_update_day = date('Y').'-01-01 00:00:00';
2784
						}
2785
					}
2786
					if (isset($filter['DeleteLastYearStats']) && date('Y',strtotime($last_update_day)) != date('Y')) {
2787
						$last_update_day = date('Y').'-01-01 00:00:00';
2788
						$reset = true;
2789
					}
2790
					// Count by filter
2791
					if ($globalDebug) echo '--- Stats for filter '.$filter_name.' ---'."\n";
2792
					$Spotter = new Spotter($this->db);
2793
					if ($globalDebug) echo 'Count all aircraft types...'."\n";
2794
					$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day,$filter);
2795
					foreach ($alldata as $number) {
2796
						$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],'',$filter_name,$reset);
2797
					}
2798
					if ($globalDebug) echo 'Count all airlines...'."\n";
2799
					$alldata = $Spotter->countAllAirlines(false,0,$last_update_day,$filter);
2800
					foreach ($alldata as $number) {
2801
						$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name'],$filter_name,$reset);
2802
					}
2803
					if ($globalDebug) echo 'Count all aircraft registrations...'."\n";
2804
					$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day,$filter);
2805
					foreach ($alldata as $number) {
2806
						$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],'',$filter_name,$reset);
2807
					}
2808
					if ($globalDebug) echo 'Count all callsigns...'."\n";
2809
					$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day,$filter);
2810
					foreach ($alldata as $number) {
2811
						$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],'',$filter_name,$reset);
2812
					}
2813
					if ($globalDebug) echo 'Count all owners...'."\n";
2814
					$alldata = $Spotter->countAllOwners(false,0,$last_update_day,$filter);
2815
					foreach ($alldata as $number) {
2816
						$this->addStatOwner($number['owner_name'],$number['owner_count'],'',$filter_name,$reset);
2817
					}
2818
					if ($globalDebug) echo 'Count all pilots...'."\n";
2819
					$alldata = $Spotter->countAllPilots(false,0,$last_update_day,$filter);
2820
					foreach ($alldata as $number) {
2821
						$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name'],'',$filter_name,$number['format_source'],$reset);
2822
					}
2823
					if ($globalDebug) echo 'Count departure airports...'."\n";
2824
					$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day,$filter);
2825
					$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day,$filter);
2826
					$alldata = array();
2827
					foreach ($pall as $value) {
2828
						$icao = $value['airport_departure_icao'];
2829
						$alldata[$icao] = $value;
2830
					}
2831
					foreach ($dall as $value) {
2832
						$icao = $value['airport_departure_icao'];
2833
						if (isset($alldata[$icao])) {
2834
							$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
2835
						} else $alldata[$icao] = $value;
2836
					}
2837
					$count = array();
2838
					foreach ($alldata as $key => $row) {
2839
						$count[$key] = $row['airport_departure_icao_count'];
2840
					}
2841
					array_multisort($count,SORT_DESC,$alldata);
2842
					foreach ($alldata as $number) {
2843
						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);
2844
					}
2845
					if ($globalDebug) echo 'Count all arrival airports...'."\n";
2846
					$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day,false,$filter);
2847
					$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day,false,$filter);
2848
					$alldata = array();
2849
					foreach ($pall as $value) {
2850
						$icao = $value['airport_arrival_icao'];
2851
						$alldata[$icao] = $value;
2852
					}
2853
					foreach ($dall as $value) {
2854
						$icao = $value['airport_arrival_icao'];
2855
						if (isset($alldata[$icao])) {
2856
							$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
2857
						} else $alldata[$icao] = $value;
2858
					}
2859
					$count = array();
2860
					foreach ($alldata as $key => $row) {
2861
						$count[$key] = $row['airport_arrival_icao_count'];
2862
					}
2863
					array_multisort($count,SORT_DESC,$alldata);
2864
					foreach ($alldata as $number) {
2865
						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);
2866
					}
2867
					if ($globalDebug) echo 'Count all months...'."\n";
2868
					$Spotter = new Spotter($this->db);
2869
					$alldata = $Spotter->countAllMonths($filter);
2870
					$lastyear = false;
2871
					foreach ($alldata as $number) {
2872
						if ($number['year_name'] != date('Y')) $lastyear = true;
2873
						$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);
2874
					}
2875
					if ($globalDebug) echo 'Count all owners by months...'."\n";
2876
					$alldata = $Spotter->countAllMonthsOwners($filter);
2877
					foreach ($alldata as $number) {
2878
						$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);
2879
					}
2880
					if ($globalDebug) echo 'Count all pilots by months...'."\n";
2881
					$alldata = $Spotter->countAllMonthsPilots($filter);
2882
					foreach ($alldata as $number) {
2883
						$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);
2884
					}
2885
					if ($globalDebug) echo 'Count all military by months...'."\n";
2886
					$alldata = $Spotter->countAllMilitaryMonths($filter);
2887
					foreach ($alldata as $number) {
2888
						$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);
2889
					}
2890
					if ($globalDebug) echo 'Count all aircrafts by months...'."\n";
2891
					$alldata = $Spotter->countAllMonthsAircrafts($filter);
2892
				    	foreach ($alldata as $number) {
2893
			    			$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);
2894
					}
2895
					if ($globalDebug) echo 'Count all real arrivals by months...'."\n";
2896
					$alldata = $Spotter->countAllMonthsRealArrivals($filter);
2897
					foreach ($alldata as $number) {
2898
						$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);
2899
					}
2900
					echo '...Departure'."\n";
2901
					$pall = $Spotter->getLast7DaysAirportsDeparture('',$filter);
2902
					$dall = $Spotter->getLast7DaysDetectedAirportsDeparture('',$filter);
2903
					foreach ($dall as $value) {
2904
						$icao = $value['departure_airport_icao'];
2905
						$ddate = $value['date'];
2906
						$find = false;
2907
						foreach ($pall as $pvalue) {
2908
							if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2909
								$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
2910
								$find = true;
2911
								break;
2912
							}
2913
						}
2914
						if ($find === false) {
2915
							$pall[] = $value;
2916
						}
2917
					}
2918
					$alldata = $pall;
2919
					foreach ($alldata as $number) {
2920
						$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);
2921
					}
2922
					echo '...Arrival'."\n";
2923
					$pall = $Spotter->getLast7DaysAirportsArrival('',$filter);
2924
					$dall = $Spotter->getLast7DaysDetectedAirportsArrival('',$filter);
2925
					foreach ($dall as $value) {
2926
						$icao = $value['arrival_airport_icao'];
2927
						$ddate = $value['date'];
2928
						$find = false;
2929
						foreach ($pall as $pvalue) {
2930
							if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
2931
								$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
2932
								$find = true;
2933
								break;
2934
							}
2935
						}
2936
						if ($find === false) {
2937
							$pall[] = $value;
2938
						}
2939
					}
2940
					$alldata = $pall;
2941
					foreach ($alldata as $number) {
2942
						$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);
2943
					}
2944
					echo 'Flights data...'."\n";
2945
					echo '-> countAllDatesLastMonth...'."\n";
2946
					$alldata = $Spotter->countAllDatesLastMonth($filter);
2947
					foreach ($alldata as $number) {
2948
						$this->addStatFlight('month',$number['date_name'],$number['date_count'], '',$filter_name);
2949
					}
2950
					echo '-> countAllDates...'."\n";
2951
					$previousdata = $this->countAllDates('',$filter_name);
2952
					$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates($filter));
2953
					$values = array();
2954
					foreach ($alldata as $cnt) {
2955
						$values[] = $cnt['date_count'];
2956
					}
2957
					array_multisort($values,SORT_DESC,$alldata);
2958
					array_splice($alldata,11);
2959
					foreach ($alldata as $number) {
2960
						$this->addStatFlight('date',$number['date_name'],$number['date_count'],'',$filter_name);
2961
					}
2962
				
2963
					echo '-> countAllHours...'."\n";
2964
					$alldata = $Spotter->countAllHours('hour',$filter);
2965
					foreach ($alldata as $number) {
2966
						$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],'',$filter_name);
2967
					}
2968
					echo 'Insert last stats update date...'."\n";
2969
					date_default_timezone_set('UTC');
2970
					$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y-m-d G:i:s'));
2971
					if (isset($filter['DeleteLastYearStats']) && $filter['DeleteLastYearStats'] == true) {
2972
						if (date('Y',strtotime($last_update_day)) != date('Y')) {
2973
							$this->deleteOldStats($filter_name);
2974
							$this->addLastStatsUpdate('last_update_stats_'.$filter_name,date('Y').'-01-01 00:00:00');
2975
						}
2976
					}
2977
				}
2978
			}
2979
		}
2980
2981
			// Last year stats
2982
			if (isset($lastyear) && $lastyear) {
2983
				echo 'Data from last year...'."\n";
2984
				// SUM all previous month to put as year
2985
				$previous_year = date('Y');
2986
				$previous_year--;
2987
				$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2988
				$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2989
				$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2990
				$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
2991
				$allairlines = $this->getAllAirlineNames();
2992
				foreach ($allairlines as $data) {
2993
					$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2994
					$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2995
					$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2996
					$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
2997
				}
2998
				
2999
				if (isset($globalArchiveYear) && $globalArchiveYear) {
3000
					if ($globalArchive) {
3001
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
3002
						try {
3003
							$sth = $this->db->prepare($query);
3004
							$sth->execute();
3005
						} catch(PDOException $e) {
3006
							return "error : ".$e->getMessage().' - query : '.$query."\n";
3007
						}
3008
						$query = "INSERT INTO tracker_archive_output SELECT * FROM tracker_output WHERE tracker_output.date < '".date('Y')."-01-01 00:00:00'";
3009
						try {
3010
							$sth = $this->db->prepare($query);
3011
							$sth->execute();
3012
						} catch(PDOException $e) {
3013
							return "error : ".$e->getMessage().' - query : '.$query."\n";
3014
						}
3015
						$query = "INSERT INTO marine_archive_output SELECT * FROM marine_output WHERE marine_output.date < '".date('Y')."-01-01 00:00:00'";
3016
						try {
3017
							$sth = $this->db->prepare($query);
3018
							$sth->execute();
3019
						} catch(PDOException $e) {
3020
							return "error : ".$e->getMessage().' - query : '.$query."\n";
3021
						}
3022
					}
3023
					echo 'Delete old data'."\n";
3024
					if ($globalDBdriver == 'mysql') {
3025
						$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000";
3026
					} else {
3027
						$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)";
3028
					}
3029
					try {
3030
						$sth = $this->db->prepare($query);
3031
						$sth->execute();
3032
					} catch(PDOException $e) {
3033
						return "error : ".$e->getMessage().' - query : '.$query."\n";
3034
					}
3035
					if ($globalDBdriver == 'mysql') {
3036
						$query = "DELETE FROM tracker_output WHERE tracker_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000";
3037
					} else {
3038
						$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)";
3039
					}
3040
					try {
3041
						$sth = $this->db->prepare($query);
3042
						$sth->execute();
3043
					} catch(PDOException $e) {
3044
						return "error : ".$e->getMessage().' - query : '.$query."\n";
3045
					}
3046
					if ($globalDBdriver == 'mysql') {
3047
						$query = "DELETE FROM marine_output WHERE marine_output.date < '".date('Y')."-01-01 00:00:00' LIMIT 10000";
3048
					} else {
3049
						$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)";
3050
					}
3051
					try {
3052
						$sth = $this->db->prepare($query);
3053
						$sth->execute();
3054
					} catch(PDOException $e) {
3055
						return "error : ".$e->getMessage().' - query : '.$query."\n";
3056
					}
3057
				}
3058
				if (isset($globalDeleteLastYearStats) && $globalDeleteLastYearStats) {
3059
					$last_update = $this->getLastStatsUpdate('last_update_stats');
3060
					if (date('Y',strtotime($last_update[0]['value'])) != date('Y')) {
3061
						$this->deleteOldStats();
3062
						$this->addLastStatsUpdate('last_update_stats',date('Y').'-01-01 00:00:00');
3063
						$lastyearupdate = true;
3064
					}
3065
				}
3066
			}
3067
			if ($globalArchiveMonths > 0) {
3068
				if ($globalArchive) {
3069
					echo 'Archive old data...'."\n";
3070
					if ($globalDBdriver == 'mysql') {
3071
						//$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3072
						$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)
3073
							    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
3074
							     FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3075
					} else {
3076
						$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)
3077
							     SELECT 
3078
								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
3079
							    FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
3080
					}
3081
					try {
3082
						$sth = $this->db->prepare($query);
3083
						$sth->execute();
3084
					} catch(PDOException $e) {
3085
						return "error : ".$e->getMessage();
3086
					}
3087
					echo 'Archive old tracker data...'."\n";
3088
					if ($globalDBdriver == 'mysql') {
3089
						$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) 
3090
							    SELECT tracker_id,famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, comment, type
3091
							     FROM tracker_output WHERE tracker_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3092
					} else {
3093
						$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) 
3094
							     SELECT tracker_id,famtrackid, ident, latitude, longitude, altitude, heading, ground_speed, date, format_source, source_name, comment, type
3095
							    FROM tracker_output WHERE tracker_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
3096
					}
3097
					try {
3098
						$sth = $this->db->prepare($query);
3099
						$sth->execute();
3100
					} catch(PDOException $e) {
3101
						return "error : ".$e->getMessage();
3102
					}
3103
					echo 'Archive old marine data...'."\n";
3104
					if ($globalDBdriver == 'mysql') {
3105
						$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) 
3106
							    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 
3107
							     FROM marine_output WHERE marine_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3108
					} else {
3109
						$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) 
3110
							     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 
3111
							    FROM marine_output WHERE marine_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
3112
					}
3113
					try {
3114
						$sth = $this->db->prepare($query);
3115
						$sth->execute();
3116
					} catch(PDOException $e) {
3117
						return "error : ".$e->getMessage();
3118
					}
3119
				}
3120
				echo 'Deleting old data...'."\n";
3121
				if ($globalDBdriver == 'mysql') {
3122
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3123
				} else {
3124
					$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))";
3125
				}
3126
				try {
3127
					$sth = $this->db->prepare($query);
3128
					$sth->execute();
3129
				} catch(PDOException $e) {
3130
					return "error : ".$e->getMessage();
3131
				}
3132
				echo 'Deleting old tracker data...'."\n";
3133
				if ($globalDBdriver == 'mysql') {
3134
					$query = "DELETE FROM tracker_output WHERE tracker_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3135
				} else {
3136
					$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))";
3137
				}
3138
				try {
3139
					$sth = $this->db->prepare($query);
3140
					$sth->execute();
3141
				} catch(PDOException $e) {
3142
					return "error : ".$e->getMessage();
3143
				}
3144
				echo 'Deleting old marine data...'."\n";
3145
				if ($globalDBdriver == 'mysql') {
3146
					$query = "DELETE FROM marine_output WHERE marine_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
3147
				} else {
3148
					$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))";
3149
				}
3150
				try {
3151
					$sth = $this->db->prepare($query);
3152
					$sth->execute();
3153
				} catch(PDOException $e) {
3154
					return "error : ".$e->getMessage();
3155
				}
3156
			}
3157
			if (!isset($lastyearupdate)) {
3158
				echo 'Insert last stats update date...'."\n";
3159
				date_default_timezone_set('UTC');
3160
				$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
3161
			}
3162
			if ($globalStatsResetYear) {
3163
				require_once(dirname(__FILE__).'/../install/class.settings.php');
3164
				settings::modify_settings(array('globalStatsResetYear' => 'FALSE'));
3165
			}
3166
		
3167
	}
3168
}
3169
3170
?>