Completed
Push — master ( 302f5a...bb5a03 )
by Yannick
30:38
created

Stats::countAllMarineDatesLastMonth()   B

Complexity

Conditions 5
Paths 18

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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