Completed
Push — master ( 79cbb4...2c41c0 )
by Yannick
30:50
created

Stats::addOldStats()   F

Complexity

Conditions 284
Paths > 20000

Size

Total Lines 992
Code Lines 741

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 284
eloc 741
nc 429496.7295
nop 0
dl 0
loc 992
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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