Completed
Push — master ( 8a4155...eb4f96 )
by Yannick
07:53
created

Stats::countAllDatesLast7Days()   B

Complexity

Conditions 4
Paths 12

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 12
nop 1
dl 0
loc 22
rs 8.9197
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.SpotterArchive.php');
8
require_once(dirname(__FILE__).'/class.Common.php');
9
class Stats {
10
	public $db;
11
	public function __construct($dbc = null) {
12
		$Connection = new Connection($dbc);
13
		$this->db = $Connection->db();
14
        }
15
              
16
	public function addLastStatsUpdate($type,$stats_date) {
17
                $query = "DELETE FROM config WHERE name = :type;
18
            		INSERT INTO config (name,value) VALUES (:type,:stats_date);";
19
                $query_values = array('type' => $type,':stats_date' => $stats_date);
20
                 try {
21
                        $sth = $this->db->prepare($query);
22
                        $sth->execute($query_values);
23
                } catch(PDOException $e) {
24
                        return "error : ".$e->getMessage();
25
                }
26
        }
27
28
	public function getLastStatsUpdate($type = 'last_update_stats') {
29
                $query = "SELECT value FROM config WHERE name = :type";
30
                 try {
31
                        $sth = $this->db->prepare($query);
32
                        $sth->execute(array(':type' => $type));
33
                } catch(PDOException $e) {
34
                        echo "error : ".$e->getMessage();
35
                }
36
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
37
                return $all;
38
        }
39
	public function getAllAirlineNames() {
40
                $query = "SELECT * FROM stats_airline ORDER BY airline_name ASC";
41
                 try {
42
                        $sth = $this->db->prepare($query);
43
                        $sth->execute();
44
                } catch(PDOException $e) {
45
                        echo "error : ".$e->getMessage();
46
                }
47
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
48
                return $all;
49
        }
50
	public function getAllAircraftTypes($stats_airline = '') {
51
                $query = "SELECT * FROM stats_aircraft WHERE stats_airline = :stats_airline ORDER BY aircraft_manufacturer ASC";
52
                 try {
53
                        $sth = $this->db->prepare($query);
54
                        $sth->execute(array(':stats_airline' => $stats_airline));
55
                } catch(PDOException $e) {
56
                        echo "error : ".$e->getMessage();
57
                }
58
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
59
                return $all;
60
        }
61
	public function getAllAirportNames($stats_airline = '') {
62
                $query = "SELECT airport_icao, airport_name,airport_city,airport_country FROM stats_airport WHERE stats_airline = :stats_airline GROUP BY airport_icao,airport_name,airport_city,airport_country ORDER BY airport_city ASC";
63
                 try {
64
                        $sth = $this->db->prepare($query);
65
                        $sth->execute(array(':stats_airline' => $stats_airline));
66
                } catch(PDOException $e) {
67
                        echo "error : ".$e->getMessage();
68
                }
69
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
70
                return $all;
71
        }
72
73
74
	public function countAllAircraftTypes($limit = true, $stats_airline = '') {
75
		if ($limit) $query = "SELECT aircraft_icao, cnt AS aircraft_icao_count, aircraft_name FROM stats_aircraft WHERE aircraft_name <> '' AND aircraft_icao <> '' AND stats_airline = :stats_airline ORDER BY aircraft_icao_count DESC LIMIT 10 OFFSET 0";
76
		else $query = "SELECT aircraft_icao, cnt AS aircraft_icao_count, aircraft_name FROM stats_aircraft WHERE aircraft_name <> '' AND aircraft_icao <> '' AND stats_airline = :stats_airline ORDER BY aircraft_icao_count DESC";
77
                 try {
78
                        $sth = $this->db->prepare($query);
79
                        $sth->execute(array(':stats_airline' => $stats_airline));
80
                } catch(PDOException $e) {
81
                        echo "error : ".$e->getMessage();
82
                }
83
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
84
                if (empty($all)) {
85
            	    $filters = array('airlines' => array($stats_airline));
86
            	    $Spotter = new Spotter($this->db);
87
            	    $all = $Spotter->countAllAircraftTypes($limit,0,'',$filters);
88
                }
89
                return $all;
90
	}
91
	public function countAllAirlineCountries($limit = true) {
92
		if ($limit) $query = "SELECT airlines.country AS airline_country, SUM(stats_airline.cnt) as airline_country_count FROM stats_airline,airlines WHERE stats_airline.airline_icao=airlines.icao AND GROUP BY airline_country ORDER BY airline_country_count DESC LIMIT 10 OFFSET 0";
93
		else $query = "SELECT airlines.country AS airline_country, SUM(stats_airline.cnt) as airline_country_count FROM stats_airline,airlines WHERE stats_airline.airline_icao=airlines.icao GROUP BY airline_country ORDER BY airline_country_count DESC";
94
                 try {
95
                        $sth = $this->db->prepare($query);
96
                        $sth->execute();
97
                } catch(PDOException $e) {
98
                        echo "error : ".$e->getMessage();
99
                }
100
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
101
                if (empty($all)) {
102
            		$Spotter = new Spotter($this->db);
103
            		$all = $Spotter->countAllAirlineCountries($limit);
104
                
105
                }
106
                return $all;
107
	}
108
	public function countAllAircraftManufacturers($limit = true,$stats_airline = '') {
109
		if ($limit) $query = "SELECT aircraft_manufacturer, SUM(stats_aircraft.cnt) as aircraft_manufacturer_count FROM stats_aircraft WHERE stats_airline = :stats_airline GROUP BY aircraft_manufacturer ORDER BY aircraft_manufacturer_count DESC LIMIT 10 OFFSET 0";
110
		else $query = "SELECT aircraft_manufacturer, SUM(stats_aircraft.cnt) as aircraft_manufacturer_count FROM stats_aircraft WHERE stats_airline = :stats_airline GROUP BY aircraft_manufacturer ORDER BY aircraft_manufacturer_count DESC";
111
                 try {
112
                        $sth = $this->db->prepare($query);
113
                        $sth->execute(array(':stats_airline' => $stats_airline));
114
                } catch(PDOException $e) {
115
                        echo "error : ".$e->getMessage();
116
                }
117
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
118
                if (empty($all)) {
119
            		$filters = array('airlines' => array($stats_airline));
120
            		$Spotter = new Spotter($this->db);
121
			$all = $Spotter->countAllAircraftManufacturers($filters);
122
                }
123
                return $all;
124
	}
125
126
	public function countAllArrivalCountries($limit = true, $stats_airline = '') {
127
		if ($limit) $query = "SELECT airport_country AS airport_arrival_country, arrival as airport_arrival_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline LIMIT 10 OFFSET 0";
128
		else $query = "SELECT airport_country AS airport_arrival_country, arrival as airport_arrival_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline";
129
                 try {
130
                        $sth = $this->db->prepare($query);
131
                        $sth->execute(array(':stats_airline' => $stats_airline));
132
                } catch(PDOException $e) {
133
                        echo "error : ".$e->getMessage();
134
                }
135
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
136
                if (empty($all)) {
137
			$filters = array('airlines' => array($stats_airline));
138
			$Spotter = new Spotter($this->db);
139
			$all = $Spotter->countAllArrivalCountries($limit,$filters);
140
                }
141
                return $all;
142
	}
143
	public function countAllDepartureCountries($limit = true, $stats_airline = '') {
144
		if ($limit) $query = "SELECT airport_country AS airport_departure_country, departure as airport_departure_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline LIMIT 10 OFFSET 0";
145
		else $query = "SELECT airport_country AS airport_departure_country, departure as airport_departure_country_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline";
146
                 try {
147
                        $sth = $this->db->prepare($query);
148
                        $sth->execute(array(':stats_airline' => $stats_airline));
149
                } catch(PDOException $e) {
150
                        echo "error : ".$e->getMessage();
151
                }
152
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
153
                if (empty($all)) {
154
			$filters = array('airlines' => array($stats_airline));
155
			$Spotter = new Spotter($this->db);
156
			$all = $Spotter->countAllDepartureCountries($filters);
157
                }
158
                return $all;
159
	}
160
161
	public function countAllAirlines($limit = true) {
162
		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 ORDER BY airline_count DESC LIMIT 10 OFFSET 0";
163
		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 ORDER BY airline_count DESC";
164
                 try {
165
                        $sth = $this->db->prepare($query);
166
                        $sth->execute();
167
                } catch(PDOException $e) {
168
                        echo "error : ".$e->getMessage();
169
                }
170
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
171
                if (empty($all)) {
172
	                $Spotter = new Spotter($this->db);
173
    		        $all = $Spotter->countAllAirlines($limit);
174
                }
175
                return $all;
176
	}
177
	public function countAllAircraftRegistrations($limit = true,$stats_airline = '') {
178
		if ($limit) $query = "SELECT s.aircraft_icao, s.cnt AS aircraft_registration_count, a.type AS aircraft_name, s.registration FROM stats_registration s, aircraft a WHERE s.registration <> '' AND a.icao = s.aircraft_icao AND s.stats_airline = :stats_airline ORDER BY aircraft_registration_count DESC LIMIT 10 OFFSET 0";
179
		else $query = "SELECT s.aircraft_icao, s.cnt AS aircraft_registration_count, a.type AS aircraft_name FROM stats_registration s, aircraft a WHERE s.registration <> '' AND a.icao = s.aircraft_icao AND s.stats_airline = :stats_airline ORDER BY aircraft_registration_count DESC";
180
                 try {
181
                        $sth = $this->db->prepare($query);
182
                        $sth->execute(array(':stats_airline' => $stats_airline));
183
                } catch(PDOException $e) {
184
                        echo "error : ".$e->getMessage();
185
                }
186
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
187
                if (empty($all)) {
188
			$filters = array('airlines' => array($stats_airline));
189
	                $Spotter = new Spotter($this->db);
190
    		        $all = $Spotter->countAllAircraftRegistrations($limit,0,'',$filters);
191
                }
192
                return $all;
193
	}
194
	public function countAllCallsigns($limit = true,$stats_airline = '') {
195
		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.stats_airline = :stats_airline ORDER BY callsign_icao_count DESC LIMIT 10 OFFSET 0";
196
		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.stats_airline = :stats_airline ORDER BY callsign_icao_count DESC";
197
		 try {
198
			$sth = $this->db->prepare($query);
199
			$sth->execute(array(':stats_airline' => $stats_airline));
200
		} catch(PDOException $e) {
201
			echo "error : ".$e->getMessage();
202
		}
203
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
204
		if (empty($all)) {
205
			$filters = array('airlines' => array($stats_airline));
206
			$Spotter = new Spotter($this->db);
207
			$all = $Spotter->countAllCallsigns($limit,0,'',$filters);
208
		}
209
		return $all;
210
	}
211
	public function countAllFlightOverCountries($limit = true, $stats_airline = '') {
212
		$Connection = new Connection();
213
		if ($Connection->tableExists('countries')) {
214
			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 ORDER BY flight_count DESC LIMIT 20 OFFSET 0";
215
			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  ORDER BY flight_count DESC";
216
			 try {
217
				$sth = $this->db->prepare($query);
218
				$sth->execute(array(':stats_airline' => $stats_airline));
219
			} catch(PDOException $e) {
220
				echo "error : ".$e->getMessage();
221
			}
222
			$all = $sth->fetchAll(PDO::FETCH_ASSOC);
223
                /*
224
                if (empty($all)) {
225
	                $Spotter = new Spotter($this->db);
226
    		        $all = $Spotter->countAllFlightOverCountries($limit);
227
                }
228
                */
229
			return $all;
230
		} else {
231
			return array();
232
		}
233
	}
234
	public function countAllPilots($limit = true) {
235
		if ($limit) $query = "SELECT pilot_id, cnt AS pilot_count, pilot_name FROM stats_pilot ORDER BY pilot_count DESC LIMIT 10 OFFSET 0";
236
		else $query = "SELECT pilot_id, cnt AS pilot_count, pilot_name FROM stats_pilot ORDER BY pilot_count DESC";
237
                 try {
238
                        $sth = $this->db->prepare($query);
239
                        $sth->execute();
240
                } catch(PDOException $e) {
241
                        echo "error : ".$e->getMessage();
242
                }
243
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
244
                if (empty($all)) {
245
            		$Spotter = new Spotter($this->db);
246
            		$all = $Spotter->countAllPilots($limit);
247
                }
248
                return $all;
249
	}
250
	public function countAllOwners($limit = true) {
251
		if ($limit) $query = "SELECT owner_name, cnt AS owner_count FROM stats_owner ORDER BY owner_count DESC LIMIT 10 OFFSET 0";
252
		else $query = "SELECT owner_name, cnt AS owner_count FROM stats_owner ORDER BY owner_count DESC";
253
                 try {
254
                        $sth = $this->db->prepare($query);
255
                        $sth->execute();
256
                } catch(PDOException $e) {
257
                        echo "error : ".$e->getMessage();
258
                }
259
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
260
                if (empty($all)) {
261
            		$Spotter = new Spotter($this->db);
262
            		$all = $Spotter->countAllOwners($limit);
263
                }
264
                return $all;
265
	}
266
	public function countAllDepartureAirports($limit = true,$stats_airline = '') {
267
		if ($limit) $query = "SELECT airport_icao AS airport_departure_icao,airport_city AS airport_departure_city,airport_country AS airport_departure_country,departure AS airport_departure_icao_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline LIMIT 10 OFFSET 0";
268
		else $query = "SELECT airport_icao AS airport_departure_icao,airport_city AS airport_departure_city,airport_country AS airport_departure_country,departure AS airport_departure_icao_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline";
269
                 try {
270
                        $sth = $this->db->prepare($query);
271
                        $sth->execute(array(':stats_airline' => $stats_airline));
272
                } catch(PDOException $e) {
273
                        echo "error : ".$e->getMessage();
274
                }
275
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
276
                if (empty($all)) {
277
			$filters = array('airlines' => array($stats_airline));
278
            		$Spotter = new Spotter($this->db);
279
            		$pall = $Spotter->countAllDepartureAirports($limit,0,'',$filters);
280
        		$dall = $Spotter->countAllDetectedDepartureAirports($limit,0,'',$filters);
281
        		$all = array();
282
        		foreach ($pall as $value) {
283
        			$icao = $value['airport_departure_icao'];
284
        			$all[$icao] = $value;
285
        		}
286
        		
287
        		foreach ($dall as $value) {
288
        			$icao = $value['airport_departure_icao'];
289
        			if (isset($all[$icao])) {
290
        				$all[$icao]['airport_departure_icao_count'] = $all[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
291
        			} else $all[$icao] = $value;
292
        		}
293
        		$count = array();
294
        		foreach ($all as $key => $row) {
295
        			$count[$key] = $row['airport_departure_icao_count'];
296
        		}
297
        		array_multisort($count,SORT_DESC,$all);
298
                }
299
                return $all;
300
	}
301
	public function countAllArrivalAirports($limit = true,$stats_airline = '') {
302
		if ($limit) $query = "SELECT airport_icao AS airport_arrival_icao,airport_city AS airport_arrival_city,airport_country AS airport_arrival_country,arrival AS airport_arrival_icao_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline LIMIT 10 OFFSET 0";
303
		else $query = "SELECT airport_icao AS airport_arrival_icao,airport_city AS airport_arrival_city,airport_country AS airport_arrival_country,arrival AS airport_arrival_icao_count FROM stats_airport WHERE stats_type = 'yearly' AND stats_airline = :stats_airline";
304
		try {
305
			$sth = $this->db->prepare($query);
306
			$sth->execute(array(':stats_airline' => $stats_airline));
307
		} catch(PDOException $e) {
308
			echo "error : ".$e->getMessage();
309
		}
310
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
311
		if (empty($all)) {
312
			$filters = array('airlines' => array($stats_airline));
313
			$Spotter = new Spotter($this->db);
314
			$pall = $Spotter->countAllArrivalAirports($limit,0,'',false,$filters);
315
			$dall = $Spotter->countAllDetectedArrivalAirports($limit,0,'',false,$filters);
316
        		$all = array();
317
        		foreach ($pall as $value) {
318
        			$icao = $value['airport_arrival_icao'];
319
        			$all[$icao] = $value;
320
        		}
321
        		
322
        		foreach ($dall as $value) {
323
        			$icao = $value['airport_arrival_icao'];
324
        			if (isset($all[$icao])) {
325
        				$all[$icao]['airport_arrival_icao_count'] = $all[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
326
        			} else $all[$icao] = $value;
327
        		}
328
        		$count = array();
329
        		foreach ($all as $key => $row) {
330
        			$count[$key] = $row['airport_arrival_icao_count'];
331
        		}
332
        		array_multisort($count,SORT_DESC,$all);
333
                }
334
                return $all;
335
	}
336
	public function countAllMonthsLastYear($limit = true,$stats_airline = '') {
337
		global $globalDBdriver;
338
		if ($globalDBdriver == 'mysql') {
339
			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";
340
			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";
341
		} else {
342
			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";
343
			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";
344
		}
345
		$query_data = array(':stats_airline' => $stats_airline);
346
                 try {
347
                        $sth = $this->db->prepare($query);
348
                        $sth->execute($query_data);
349
                } catch(PDOException $e) {
350
                        echo "error : ".$e->getMessage();
351
                }
352
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
353
                if (empty($all)) {
354
			$filters = array('airlines' => array($stats_airline));
355
            		$Spotter = new Spotter($this->db);
356
            		$all = $Spotter->countAllMonthsLastYear($filters);
357
                }
358
                
359
                return $all;
360
	}
361
	
362
	public function countAllDatesLastMonth($stats_airline = '') {
363
		$query = "SELECT flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'month' AND stats_airline = :stats_airline";
364
		$query_data = array(':stats_airline' => $stats_airline);
365
                 try {
366
                        $sth = $this->db->prepare($query);
367
                        $sth->execute($query_data);
368
                } catch(PDOException $e) {
369
                        echo "error : ".$e->getMessage();
370
                }
371
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
372
                if (empty($all)) {
373
			$filters = array('airlines' => array($stats_airline));
374
            		$Spotter = new Spotter($this->db);
375
            		$all = $Spotter->countAllDatesLastMonth($filters);
376
                }
377
                return $all;
378
	}
379
	public function countAllDatesLast7Days($stats_airline = '') {
380
		global $globalDBdriver;
381
		if ($globalDBdriver == 'mysql') {
382
			$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";
383
		} else {
384
			$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";
385
		}
386
		$query_data = array(':stats_airline' => $stats_airline);
387
                 try {
388
                        $sth = $this->db->prepare($query);
389
                        $sth->execute($query_data);
390
                } catch(PDOException $e) {
391
                        echo "error : ".$e->getMessage();
392
                }
393
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
394
                if (empty($all)) {
395
			$filters = array('airlines' => array($stats_airline));
396
            		$Spotter = new Spotter($this->db);
397
            		$all = $Spotter->countAllDatesLast7Days($filters);
398
                }
399
                return $all;
400
	}
401
	public function countAllDates($stats_airline = '') {
402
		$query = "SELECT flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'date' AND stats_airline = :stats_airline";
403
		$query_data = array(':stats_airline' => $stats_airline);
404
                 try {
405
                        $sth = $this->db->prepare($query);
406
                        $sth->execute($query_data);
407
                } catch(PDOException $e) {
408
                        echo "error : ".$e->getMessage();
409
                }
410
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
411
                if (empty($all)) {
412
			$filters = array('airlines' => array($stats_airline));
413
            		$Spotter = new Spotter($this->db);
414
            		$all = $Spotter->countAllDates($filters);
415
                }
416
                return $all;
417
	}
418
	public function countAllDatesByAirlines() {
419
		$query = "SELECT stats_airline as airline_icao, flight_date as date_name, cnt as date_count FROM stats_flight WHERE stats_type = 'date'";
420
		$query_data = array();
421
                 try {
422
                        $sth = $this->db->prepare($query);
423
                        $sth->execute($query_data);
424
                } catch(PDOException $e) {
425
                        echo "error : ".$e->getMessage();
426
                }
427
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
428
                if (empty($all)) {
429
            		$Spotter = new Spotter($this->db);
430
            		$all = $Spotter->countAllDatesByAirlines();
431
                }
432
                return $all;
433
	}
434
	public function countAllMonths($stats_airline = '') {
435
	    	$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";
436
                 try {
437
                        $sth = $this->db->prepare($query);
438
                        $sth->execute(array(':stats_airline' => $stats_airline));
439
                } catch(PDOException $e) {
440
                        echo "error : ".$e->getMessage();
441
                }
442
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
443
                if (empty($all)) {
444
			$filters = array('airlines' => array($stats_airline));
445
            		$Spotter = new Spotter($this->db);
446
            		$all = $Spotter->countAllMonths($filters);
447
                }
448
                return $all;
449
	}
450
	public function countAllMilitaryMonths() {
451
	    	$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'";
452
                 try {
453
                        $sth = $this->db->prepare($query);
454
                        $sth->execute();
455
                } catch(PDOException $e) {
456
                        echo "error : ".$e->getMessage();
457
                }
458
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
459
                if (empty($all)) {
460
            		$Spotter = new Spotter($this->db);
461
            		$all = $Spotter->countAllMilitaryMonths();
462
                }
463
                return $all;
464
	}
465
	public function countAllHours($orderby = 'hour',$limit = true,$stats_airline = '') {
466
		global $globalTimezone, $globalDBdriver;
467
468
		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";
469
		else $query = "SELECT flight_date as hour_name, cnt as hour_count FROM stats_flight WHERE stats_type = 'hour' AND stats_airline = :stats_airline";
470
		if ($orderby == 'hour') {
471
			if ($globalDBdriver == 'mysql') {
472
				$query .= " ORDER BY flight_date ASC";
473
			} else {
474
				$query .= " ORDER BY CAST(flight_date AS integer) ASC";
475
			}
476
		}
477
		if ($orderby == 'count') $query .= " ORDER BY hour_count DESC";
478
                 try {
479
                        $sth = $this->db->prepare($query);
480
                        $sth->execute(array(':stats_airline' => $stats_airline));
481
                } catch(PDOException $e) {
482
                        echo "error : ".$e->getMessage();
483
                }
484
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
485
                if (empty($all)) {
486
			$filters = array('airlines' => array($stats_airline));
487
            		$Spotter = new Spotter($this->db);
488
            		$all = $Spotter->countAllHours($orderby,$filters);
489
                }
490
                return $all;
491
	}
492
	
493
	public function countOverallFlights($stats_airline = '') {
494
		$all = $this->getSumStats('flights_bymonth',date('Y'),$stats_airline);
495
		if (empty($all)) {
496
			$filters = array('airlines' => array($stats_airline));
497
			$Spotter = new Spotter($this->db);
498
			$all = $Spotter->countOverallFlights($filters);
499
		}
500
		return $all;
501
	}
502
	public function countOverallMilitaryFlights() {
503
		$all = $this->getSumStats('military_flights_bymonth',date('Y'));
504
		if (empty($all)) {
505
			$Spotter = new Spotter($this->db);
506
			$all = $Spotter->countOverallMilitaryFlights();
507
		}
508
		return $all;
509
	}
510
	public function countOverallArrival($stats_airline = '') {
511
		$all = $this->getSumStats('realarrivals_bymonth',date('Y'),$stats_airline);
512
		if (empty($all)) {
513
			$filters = array('airlines' => array($stats_airline));
514
			$Spotter = new Spotter($this->db);
515
			$all = $Spotter->countOverallArrival($filters);
516
		}
517
		return $all;
518
	}
519
	public function countOverallAircrafts($stats_airline = '') {
520
		$all = $this->getSumStats('aircrafts_bymonth',date('Y'),$stats_airline);
521
		if (empty($all)) {
522
			$filters = array('airlines' => array($stats_airline));
523
			$Spotter = new Spotter($this->db);
524
			$all = $Spotter->countOverallAircrafts($filters);
525
		}
526
		return $all;
527
	}
528
	public function countOverallAirlines() {
529
		$query = "SELECT COUNT(*) AS nb_airline FROM stats_airline";
530
                 try {
531
                        $sth = $this->db->prepare($query);
532
                        $sth->execute();
533
                } catch(PDOException $e) {
534
                        echo "error : ".$e->getMessage();
535
                }
536
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
537
                $all = $result[0]['nb_airline'];
538
		//$all = $this->getSumStats('airlines_bymonth',date('Y'));
539
		if (empty($all)) {
540
			$Spotter = new Spotter($this->db);
541
			$all = $Spotter->countOverallAirlines();
542
		}
543
		return $all;
544
	}
545
	public function countOverallOwners($stats_airline = '') {
546
		/*
547
		$query = "SELECT COUNT(*) AS nb_owner FROM stats_owner";
548
                 try {
549
                        $sth = $this->db->prepare($query);
550
                        $sth->execute();
551
                } catch(PDOException $e) {
552
                        echo "error : ".$e->getMessage();
553
                }
554
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
555
                $all = $result[0]['nb_owner'];
556
                */
557
		$all = $this->getSumStats('owners_bymonth',date('Y'),$stats_airline);
558
		if (empty($all)) {
559
			$filters = array('airlines' => array($stats_airline));
560
			$Spotter = new Spotter($this->db);
561
			$all = $Spotter->countOverallOwners($filters);
562
		}
563
		return $all;
564
	}
565
	public function countOverallPilots($stats_airline = '') {
566
		$all = $this->getSumStats('pilots_bymonth',date('Y'));
567
		if (empty($all)) {
568
			$filters = array('airlines' => array($stats_airline));
569
			$Spotter = new Spotter($this->db);
570
			$all = $Spotter->countOverallPilots($filters);
571
		}
572
		return $all;
573
	}
574
575
	public function getLast7DaysAirports($airport_icao = '', $stats_airline = '') {
576
		$query = "SELECT * FROM stats_airport WHERE stats_type = 'daily' AND airport_icao = :airport_icao AND stats_airline = :stats_airline ORDER BY date";
577
		$query_values = array(':airport_icao' => $airport_icao,':stats_airline' => $stats_airline);
578
                 try {
579
                        $sth = $this->db->prepare($query);
580
                        $sth->execute($query_values);
581
                } catch(PDOException $e) {
582
                        echo "error : ".$e->getMessage();
583
                }
584
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
585
                return $all;
586
	}
587
	public function getStats($type,$stats_airline = '') {
588
                $query = "SELECT * FROM stats WHERE stats_type = :type AND stats_airline = :stats_airline ORDER BY stats_date";
589
                $query_values = array(':type' => $type,':stats_airline' => $stats_airline);
590
                 try {
591
                        $sth = $this->db->prepare($query);
592
                        $sth->execute($query_values);
593
                } catch(PDOException $e) {
594
                        echo "error : ".$e->getMessage();
595
                }
596
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
597
                return $all;
598
        }
599
	public function getSumStats($type,$year,$stats_airline = '') {
600
    		global $globalArchiveMonths, $globalDBdriver;
601
    		if ($globalDBdriver == 'mysql') {
602
	                $query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND YEAR(stats_date) = :year AND stats_airline = :stats_airline";
603
	        } else {
604
            		$query = "SELECT SUM(cnt) as total FROM stats WHERE stats_type = :type AND EXTRACT(YEAR FROM stats_date) = :year AND stats_airline = :stats_airline";
605
                }
606
                $query_values = array(':type' => $type, ':year' => $year, ':stats_airline' => $stats_airline);
607
                 try {
608
                        $sth = $this->db->prepare($query);
609
                        $sth->execute($query_values);
610
                } catch(PDOException $e) {
611
                        echo "error : ".$e->getMessage();
612
                }
613
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
614
                return $all[0]['total'];
615
        }
616
	public function getStatsTotal($type, $stats_airline = '') {
617
    		global $globalArchiveMonths, $globalDBdriver;
618
    		if ($globalDBdriver == 'mysql') {
619
			$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";
620
		} else {
621
			$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";
622
                }
623
                $query_values = array(':type' => $type, ':stats_airline' => $stats_airline);
624
                 try {
625
                        $sth = $this->db->prepare($query);
626
                        $sth->execute($query_values);
627
                } catch(PDOException $e) {
628
                        echo "error : ".$e->getMessage();
629
                }
630
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
631
                return $all[0]['total'];
632
        }
633
	public function getStatsAircraftTotal($stats_airline = '') {
634
    		global $globalArchiveMonths, $globalDBdriver;
635
    		if ($globalDBdriver == 'mysql') {
636
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline";
637
                } else {
638
			$query = "SELECT SUM(cnt) as total FROM stats_aircraft WHERE stats_airline = :stats_airline";
639
                }
640
                 try {
641
                        $sth = $this->db->prepare($query);
642
                        $sth->execute(array(':stats_airline' => $stats_airline));
643
                } catch(PDOException $e) {
644
                        echo "error : ".$e->getMessage();
645
                }
646
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
647
                return $all[0]['total'];
648
        }
649
	public function getStatsAirlineTotal() {
650
    		global $globalArchiveMonths, $globalDBdriver;
651
    		if ($globalDBdriver == 'mysql') {
652
			$query = "SELECT SUM(cnt) as total FROM stats_airline";
653
                } else {
654
			$query = "SELECT SUM(cnt) as total FROM stats_airline";
655
                }
656
                 try {
657
                        $sth = $this->db->prepare($query);
658
                        $sth->execute();
659
                } catch(PDOException $e) {
660
                        echo "error : ".$e->getMessage();
661
                }
662
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
663
                return $all[0]['total'];
664
        }
665
	public function getStatsOwnerTotal() {
666
    		global $globalArchiveMonths, $globalDBdriver;
667
    		if ($globalDBdriver == 'mysql') {
668
			$query = "SELECT SUM(cnt) as total FROM stats_owner";
669
		} else {
670
			$query = "SELECT SUM(cnt) as total FROM stats_owner";
671
                }
672
                 try {
673
                        $sth = $this->db->prepare($query);
674
                        $sth->execute();
675
                } catch(PDOException $e) {
676
                        echo "error : ".$e->getMessage();
677
                }
678
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
679
                return $all[0]['total'];
680
        }
681
	public function getStatsPilotTotal() {
682
    		global $globalArchiveMonths, $globalDBdriver;
683
    		if ($globalDBdriver == 'mysql') {
684
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot";
685
            	} else {
686
            		$query = "SELECT SUM(cnt) as total FROM stats_pilot";
687
            	}
688
                 try {
689
                        $sth = $this->db->prepare($query);
690
                        $sth->execute();
691
                } catch(PDOException $e) {
692
                        echo "error : ".$e->getMessage();
693
                }
694
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
695
                return $all[0]['total'];
696
        }
697
698
	public function addStat($type,$cnt,$stats_date,$stats_airline = '') {
699
		global $globalDBdriver;
700
		if ($globalDBdriver == 'mysql') {
701
			$query = "INSERT INTO stats (stats_type,cnt,stats_date,stats_airline) VALUES (:type,:cnt,:stats_date,:stats_airline) ON DUPLICATE KEY UPDATE cnt = :cnt";
702
                } else {
703
			$query = "UPDATE stats SET cnt = :cnt WHERE stats_type = :type AND stats_date = :stats_date AND stats_airline = :stats_airline; INSERT INTO stats (stats_type,cnt,stats_date,stats_airline) SELECT :type,:cnt,:stats_date,:stats_airline WHERE NOT EXISTS (SELECT 1 FROM stats WHERE  stats_type = :type AND stats_date = :stats_date AND stats_airline = :stats_airline);"; 
704
		}
705
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date, ':stats_airline' => $stats_airline);
706
                 try {
707
                        $sth = $this->db->prepare($query);
708
                        $sth->execute($query_values);
709
                } catch(PDOException $e) {
710
                        return "error : ".$e->getMessage();
711
                }
712
        }
713
	public function updateStat($type,$cnt,$stats_date) {
714
		global $globalDBdriver;
715
		if ($globalDBdriver == 'mysql') {
716
			$query = "INSERT INTO stats (stats_type,cnt,stats_date) VALUES (:type,:cnt,:stats_date) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
717
		} else {
718
            		//$query = "INSERT INTO stats (stats_type,cnt,stats_date) VALUES (:type,:cnt,:stats_date) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, stats_date = :date";
719
			$query = "UPDATE stats SET cnt = cnt+:cnt WHERE stats_type = :type AND stats_date = :stats_date; INSERT INTO stats (stats_type,cnt,stats_date) SELECT :type,:cnt,:stats_date WHERE NOT EXISTS (SELECT 1 FROM stats WHERE  stats_type = :type AND stats_date = :stats_date);"; 
720
                }
721
                $query_values = array(':type' => $type,':cnt' => $cnt,':stats_date' => $stats_date);
722
                 try {
723
                        $sth = $this->db->prepare($query);
724
                        $sth->execute($query_values);
725
                } catch(PDOException $e) {
726
                        return "error : ".$e->getMessage();
727
                }
728
        }
729
	public function getStatsSource($date,$stats_type = '') {
730
		if ($stats_type == '') {
731
			$query = "SELECT * FROM stats_source WHERE stats_date = :date ORDER BY source_name";
732
			$query_values = array(':date' => $date);
733
		} else {
734
			$query = "SELECT * FROM stats_source WHERE stats_date = :date AND stats_type = :stats_type ORDER BY source_name";
735
			$query_values = array(':date' => $date,':stats_type' => $stats_type);
736
		}
737
                 try {
738
                        $sth = $this->db->prepare($query);
739
                        $sth->execute($query_values);
740
                } catch(PDOException $e) {
741
                        echo "error : ".$e->getMessage();
742
                }
743
                $all = $sth->fetchAll(PDO::FETCH_ASSOC);
744
                return $all;
745
        }
746
747
	public function addStatSource($data,$source_name,$stats_type,$date) {
748
		global $globalDBdriver;
749
		if ($globalDBdriver == 'mysql') {
750
			$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";
751
		} else {
752
			$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);"; 
753
                }
754
                $query_values = array(':data' => $data,':stats_date' => $date,':source_name' => $source_name,':stats_type' => $stats_type);
755
                 try {
756
                        $sth = $this->db->prepare($query);
757
                        $sth->execute($query_values);
758
                } catch(PDOException $e) {
759
                        return "error : ".$e->getMessage();
760
                }
761
        }
762
	public function addStatFlight($type,$date_name,$cnt,$stats_airline = '') {
763
                $query = "INSERT INTO stats_flight (stats_type,flight_date,cnt,stats_airline) VALUES (:type,:flight_date,:cnt,:stats_airline)";
764
                $query_values = array(':type' => $type,':flight_date' => $date_name,':cnt' => $cnt, ':stats_airline' => $stats_airline);
765
                 try {
766
                        $sth = $this->db->prepare($query);
767
                        $sth->execute($query_values);
768
                } catch(PDOException $e) {
769
                        return "error : ".$e->getMessage();
770
                }
771
        }
772
	public function addStatAircraftRegistration($registration,$cnt,$aircraft_icao = '',$airline_icao = '') {
773
		global $globalDBdriver;
774
		if ($globalDBdriver == 'mysql') {
775
			$query = "INSERT INTO stats_registration (aircraft_icao,registration,cnt,stats_airline) VALUES (:aircraft_icao,:registration,:cnt,:stats_airline) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
776
		} else {
777
			$query = "UPDATE stats_registration SET cnt = cnt+:cnt WHERE registration = :registration AND stats_airline = :stats_airline; INSERT INTO stats_registration (aircraft_icao,registration,cnt,stats_airline) SELECT :aircraft_icao,:registration,:cnt,:stats_airline WHERE NOT EXISTS (SELECT 1 FROM stats_registration WHERE registration = :registration AND stats_airline = :stats_airline);"; 
778
		}
779
                $query_values = array(':aircraft_icao' => $aircraft_icao,':registration' => $registration,':cnt' => $cnt,':stats_airline' => $airline_icao);
780
                 try {
781
                        $sth = $this->db->prepare($query);
782
                        $sth->execute($query_values);
783
                } catch(PDOException $e) {
784
                        return "error : ".$e->getMessage();
785
                }
786
        }
787
	public function addStatCallsign($callsign_icao,$cnt,$airline_icao = '') {
788
		global $globalDBdriver;
789
		if ($globalDBdriver == 'mysql') {
790
			$query = "INSERT INTO stats_callsign (callsign_icao,airline_icao,cnt) VALUES (:callsign_icao,:airline_icao,:cnt) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
791
		} else {
792
			$query = "UPDATE stats_callsign SET cnt = cnt+:cnt WHERE callsign_icao = :callsign_icao; INSERT INTO stats_callsign (callsign_icao,airline_icao,cnt) SELECT :callsign_icao,:airline_icao,:cnt WHERE NOT EXISTS (SELECT 1 FROM stats_callsign WHERE callsign_icao = :callsign_icao);"; 
793
		}
794
                $query_values = array(':callsign_icao' => $callsign_icao,':airline_icao' => $airline_icao,':cnt' => $cnt);
795
                 try {
796
                        $sth = $this->db->prepare($query);
797
                        $sth->execute($query_values);
798
                } catch(PDOException $e) {
799
                        return "error : ".$e->getMessage();
800
                }
801
        }
802
	public function addStatCountry($iso2,$iso3,$name,$cnt) {
803
		global $globalDBdriver;
804
		if ($globalDBdriver == 'mysql') {
805
			$query = "INSERT INTO stats_country (iso2,iso3,name,cnt) VALUES (:iso2,:iso3,:name,:cnt) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
806
		} else {
807
			$query = "UPDATE stats_country SET cnt = cnt+:cnt WHERE iso2 = :iso2; INSERT INTO stats_country (iso2,iso3,name,cnt) SELECT :iso2,:iso3,:name,:cnt WHERE NOT EXISTS (SELECT 1 FROM stats_country WHERE iso2 = :iso2);"; 
808
		}
809
                $query_values = array(':iso2' => $iso2,':iso3' => $iso3,':name' => $name,':cnt' => $cnt);
810
                 try {
811
                        $sth = $this->db->prepare($query);
812
                        $sth->execute($query_values);
813
                } catch(PDOException $e) {
814
                        return "error : ".$e->getMessage();
815
                }
816
        }
817
	public function addStatAircraft($aircraft_icao,$cnt,$aircraft_name = '',$aircraft_manufacturer = '', $airline_icao = '') {
818
		global $globalDBdriver;
819
		if ($globalDBdriver == 'mysql') {
820
			$query = "INSERT INTO stats_aircraft (aircraft_icao,aircraft_name,aircraft_manufacturer,cnt,stats_airline) VALUES (:aircraft_icao,:aircraft_name,:aircraft_manufacturer,:cnt,:stats_airline) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, aircraft_name = :aircraft_name, aircraft_manufacturer = :aircraft_manufacturer, stats_airline = :stats_airline";
821
		} else {
822
			$query = "UPDATE stats_aircraft SET cnt = cnt+:cnt, aircraft_name = :aircraft_name, aircraft_manufacturer = :aircraft_manufacturer WHERE aircraft_icao = :aircraft_icao AND stats_airline = :stats_airline; INSERT INTO stats_aircraft (aircraft_icao,aircraft_name,aircraft_manufacturer,cnt,stats_airline) SELECT :aircraft_icao,:aircraft_name,:aircraft_manufacturer,:cnt,:stats_airline WHERE NOT EXISTS (SELECT 1 FROM stats_aircraft WHERE aircraft_icao = :aircraft_icao AND stats_airline = :stats_airline);"; 
823
		}
824
                $query_values = array(':aircraft_icao' => $aircraft_icao,':aircraft_name' => $aircraft_name,':cnt' => $cnt, ':aircraft_manufacturer' => $aircraft_manufacturer,':stats_airline' => $airline_icao);
825
                 try {
826
                        $sth = $this->db->prepare($query);
827
                        $sth->execute($query_values);
828
                } catch(PDOException $e) {
829
                        return "error : ".$e->getMessage();
830
                }
831
        }
832
	public function addStatAirline($airline_icao,$cnt,$airline_name = '') {
833
		global $globalDBdriver;
834
		if ($globalDBdriver == 'mysql') {
835
			$query = "INSERT INTO stats_airline (airline_icao,airline_name,cnt) VALUES (:airline_icao,:airline_name,:cnt) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt,airline_name = :airline_name";
836
		} else {
837
			$query = "UPDATE stats_airline SET cnt = cnt+:cnt WHERE airline_icao = :airline_icao; INSERT INTO stats_airline (airline_icao,airline_name,cnt) SELECT :airline_icao,:airline_name,:cnt WHERE NOT EXISTS (SELECT 1 FROM stats_airline WHERE airline_icao = :airline_icao);"; 
838
		}
839
                $query_values = array(':airline_icao' => $airline_icao,':airline_name' => $airline_name,':cnt' => $cnt);
840
                 try {
841
                        $sth = $this->db->prepare($query);
842
                        $sth->execute($query_values);
843
                } catch(PDOException $e) {
844
                        return "error : ".$e->getMessage();
845
                }
846
        }
847
	public function addStatOwner($owner_name,$cnt) {
848
		global $globalDBdriver;
849
		if ($globalDBdriver == 'mysql') {
850
			$query = "INSERT INTO stats_owner (owner_name,cnt) VALUES (:owner_name,:cnt) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt";
851
		} else {
852
			$query = "UPDATE stats_owner SET cnt = cnt+:cnt WHERE owner_name = :owner_name; INSERT INTO stats_owner (owner_name,cnt) SELECT :owner_name,:cnt WHERE NOT EXISTS (SELECT 1 FROM stats_owner WHERE owner_name = :owner_name);"; 
853
		}
854
                $query_values = array(':owner_name' => $owner_name,':cnt' => $cnt);
855
                 try {
856
                        $sth = $this->db->prepare($query);
857
                        $sth->execute($query_values);
858
                } catch(PDOException $e) {
859
                        return "error : ".$e->getMessage();
860
                }
861
        }
862
	public function addStatPilot($pilot_id,$cnt,$pilot_name) {
863
		global $globalDBdriver;
864
		if ($globalDBdriver == 'mysql') {
865
			$query = "INSERT INTO stats_pilot (pilot_id,cnt,pilot_name) VALUES (:pilot_id,:cnt,:pilot_name) ON DUPLICATE KEY UPDATE cnt = cnt+:cnt, pilot_name = :pilot_name";
866
		} else {
867
			$query = "UPDATE stats_pilot SET cnt = cnt+:cnt, pilot_name = :pilot_name WHERE pilot_id = :pilot_id; INSERT INTO stats_pilot (pilot_id,cnt,pilot_name) SELECT :pilot_id,:cnt,:pilot_name WHERE NOT EXISTS (SELECT 1 FROM stats_pilot WHERE pilot_id = :pilot_id);"; 
868
		}
869
                $query_values = array(':pilot_id' => $pilot_id,':cnt' => $cnt,':pilot_name' => $pilot_name);
870
                 try {
871
                        $sth = $this->db->prepare($query);
872
                        $sth->execute($query_values);
873
                } catch(PDOException $e) {
874
                        return "error : ".$e->getMessage();
875
                }
876
        }
877
	public function addStatDepartureAirports($airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '') {
878
		global $globalDBdriver;
879
		if ($globalDBdriver == 'mysql') {
880
			$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:departure,'yearly',:date,:stats_airline) ON DUPLICATE KEY UPDATE departure = departure+:departure";
881
		} else {
882
			$query = "UPDATE stats_airport SET departure = departure+:departure WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:departure,'yearly',:date, :stats_airline WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline);"; 
883
		}
884
                $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);
885
                 try {
886
                        $sth = $this->db->prepare($query);
887
                        $sth->execute($query_values);
888
                } catch(PDOException $e) {
889
                        return "error : ".$e->getMessage();
890
                }
891
        }
892
	public function addStatDepartureAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$departure,$airline_icao = '') {
893
		global $globalDBdriver;
894
		if ($globalDBdriver == 'mysql') {
895
			$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:departure,'daily',:date,:stats_airline) ON DUPLICATE KEY UPDATE departure = :departure";
896
		} else {
897
			$query = "UPDATE stats_airport SET departure = departure+:departure WHERE airport_icao = :airport_icao AND stats_type = 'daily' AND date = :date AND stats_airline = :stats_airline; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,departure,stats_type,date,stats_airline) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:departure,'daily',:date,:stats_airline WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'daily' AND date = :date);"; 
898
		}
899
                $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);
900
                 try {
901
                        $sth = $this->db->prepare($query);
902
                        $sth->execute($query_values);
903
                } catch(PDOException $e) {
904
                        return "error : ".$e->getMessage();
905
                }
906
        }
907
	public function addStatArrivalAirports($airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '') {
908
		global $globalDBdriver;
909
		if ($globalDBdriver == 'mysql') {
910
			$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'yearly',:date,:stats_airline) ON DUPLICATE KEY UPDATE arrival = arrival+:arrival";
911
		} else {
912
			$query = "UPDATE stats_airport SET arrival = arrival+:arrival WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'yearly',:date,:stats_airline WHERE NOT EXISTS (SELECT 1 FROM stats_airport WHERE airport_icao = :airport_icao AND stats_type = 'yearly' AND stats_airline = :stats_airline);"; 
913
		}
914
                $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);
915
                 try {
916
                        $sth = $this->db->prepare($query);
917
                        $sth->execute($query_values);
918
                } catch(PDOException $e) {
919
                        return "error : ".$e->getMessage();
920
                }
921
        }
922
	public function addStatArrivalAirportsDaily($date,$airport_icao,$airport_name,$airport_city,$airport_country,$arrival,$airline_icao = '') {
923
		global $globalDBdriver;
924
		if ($globalDBdriver == 'mysql') {
925
			$query = "INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline) VALUES (:airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'daily',:date,:stats_airline) ON DUPLICATE KEY UPDATE arrival = :arrival";
926
		} else {
927
			$query = "UPDATE stats_airport SET arrival = arrival+:arrival WHERE airport_icao = :airport_icao AND stats_type = 'daily' AND date = :date AND stats_airline = :stats_airline; INSERT INTO stats_airport (airport_icao,airport_name,airport_city,airport_country,arrival,stats_type,date,stats_airline) SELECT :airport_icao,:airport_name,:airport_city,:airport_country,:arrival,'yearly',:date,:stats_airline 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);"; 
928
		}
929
                $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);
930
                 try {
931
                        $sth = $this->db->prepare($query);
932
                        $sth->execute($query_values);
933
                } catch(PDOException $e) {
934
                        return "error : ".$e->getMessage();
935
                }
936
        }
937
938
	public function deleteStat($id) {
939
                $query = "DELETE FROM stats WHERE stats_id = :id";
940
                $query_values = array(':id' => $id);
941
                 try {
942
                        $sth = $this->db->prepare($query);
943
                        $sth->execute($query_values);
944
                } catch(PDOException $e) {
945
                        return "error : ".$e->getMessage();
946
                }
947
        }
948
	public function deleteStatFlight($type) {
949
                $query = "DELETE FROM stats_flight WHERE stats_type = :type";
950
                $query_values = array(':type' => $type);
951
                 try {
952
                        $sth = $this->db->prepare($query);
953
                        $sth->execute($query_values);
954
                } catch(PDOException $e) {
955
                        return "error : ".$e->getMessage();
956
                }
957
        }
958
	public function deleteStatAirport($type) {
959
                $query = "DELETE FROM stats_airport WHERE stats_type = :type";
960
                $query_values = array(':type' => $type);
961
                 try {
962
                        $sth = $this->db->prepare($query);
963
                        $sth->execute($query_values);
964
                } catch(PDOException $e) {
965
                        return "error : ".$e->getMessage();
966
                }
967
        }
968
        
969
        public function addOldStats() {
970
    		global $globalArchiveMonths, $globalArchive, $globalArchiveYear, $globalDBdriver;
971
    		$Common = new Common();
972
    		$Connection = new Connection();
973
    		date_default_timezone_set('UTC');
974
    		$last_update = $this->getLastStatsUpdate('last_update_stats');
975
		//print_r($last_update);
976
		/*
977
		$flightsbymonth = $this->getStats('flights_by_month');
978
    		if (empty($last_update) && empty($flightsbymonth)) {
979
			// Initial update
980
			$Spotter = new Spotter($this->db);
981
			$alldata = $Spotter->countAllMonths();
982
			$lastyear = false;
983
			foreach ($alldata as $number) {
984
				if ($number['year_name'] != date('Y')) $lastyear = true;
985
				$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'])));
986
			}
987
			$alldata = $Spotter->countAllMilitaryMonths();
988
			//$lastyear = false;
989
			foreach ($alldata as $number) {
990
				if ($number['year_name'] != date('Y')) $lastyear = true;
991
				$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'])));
992
			}
993
			$alldata = $Spotter->countAllMonthsOwners();
994
			foreach ($alldata as $number) {
995
				$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'])));
996
			}
997
			$alldata = $Spotter->countAllMonthsPilots();
998
			foreach ($alldata as $number) {
999
				$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'])));
1000
			}
1001
			$alldata = $Spotter->countAllMonthsAirlines();
1002
			foreach ($alldata as $number) {
1003
				$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'])));
1004
			}
1005
			$alldata = $Spotter->countAllMonthsAircrafts();
1006
			foreach ($alldata as $number) {
1007
				$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'])));
1008
			}
1009
			$alldata = $Spotter->countAllMonthsRealArrivals();
1010
			foreach ($alldata as $number) {
1011
				$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'])));
1012
			}
1013
			$this->deleteStatFlight('month');
1014
			$alldata = $Spotter->countAllDatesLastMonth();
1015
			foreach ($alldata as $number) {
1016
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
1017
			}
1018
			$previousdata = $this->countAllDates();
1019
			$this->deleteStatFlight('date');
1020
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates());
1021
			$values = array();
1022
			foreach ($alldata as $cnt) {
1023
				$values[] = $cnt['date_count'];
1024
			}
1025
			array_multisort($values,SORT_DESC,$alldata);
1026
			array_splice($alldata,11);
1027
			foreach ($alldata as $number) {
1028
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
1029
			}
1030
			$this->deleteStatFlight('hour');
1031
			$alldata = $Spotter->countAllHours('hour');
1032
			foreach ($alldata as $number) {
1033
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
1034
			}
1035
			if ($lastyear) {
1036
				$monthsSinceLastYear = date('n');
1037
				$alldata = $Spotter->countAllAircraftTypes(false,$monthsSinceLastYear);
1038
				foreach ($alldata as $number) {
1039
					$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer']);
1040
				}
1041
				$alldata = $Spotter->countAllAirlines(false,$monthsSinceLastYear);
1042
				foreach ($alldata as $number) {
1043
					$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name']);
1044
				}
1045
				if ($Connection->tableExists('countries')) {
1046
					$alldata = $Spotter->countAllFlightOverCountries(false,$monthsSinceLastYear);
1047
					foreach ($alldata as $number) {
1048
						$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count']);
1049
					}
1050
				}
1051
				$alldata = $Spotter->countAllOwners(false,$monthsSinceLastYear);
1052
				foreach ($alldata as $number) {
1053
					$this->addStatOwner($number['owner_name'],$number['owner_count']);
1054
				}
1055
				$alldata = $Spotter->countAllPilots(false,$monthsSinceLastYear);
1056
				foreach ($alldata as $number) {
1057
					$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name']);
1058
				}
1059
				$previous_year = date('Y');
1060
				$previous_year--;
1061
				$this->addStat('aircrafts_byyear',$this->getStatsAircraftTotal(),$previous_year.'-01-01 00:00:00');
1062
				$this->addStat('airlines_byyear',$this->getStatsAirlineTotal(),$previous_year.'-01-01 00:00:00');
1063
				$this->addStat('owner_byyear',$this->getStatsOwnerTotal(),$previous_year.'-01-01 00:00:00');
1064
				$this->addStat('pilot_byyear',$this->getStatsPilotTotal(),$previous_year.'-01-01 00:00:00');
1065
				
1066
				if (isset($globalArchiveYear) && $globalArchiveYear) {
1067
					if ($globalArchive) {
1068
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
1069
						//echo $query;
1070
						try {
1071
							$sth = $this->db->prepare($query);
1072
							$sth->execute();
1073
						} catch(PDOException $e) {
1074
							return "error : ".$e->getMessage().' - query : '.$query."\n";
1075
						}
1076
					}
1077
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
1078
					try {
1079
						$sth = $this->db->prepare($query);
1080
						$sth->execute();
1081
					} catch(PDOException $e) {
1082
						return "error : ".$e->getMessage().' - query : '.$query."\n";
1083
					}
1084
				}
1085
			}
1086
			if (!isset($globalArchiveMonths) || $globalArchiveMonths == '') $globalArchiveMonths = 2;
1087
			if ($globalArchiveMonths > 0) {
1088
				$alldata = $Spotter->countAllAircraftTypes(false,$globalArchiveMonths);
1089
				foreach ($alldata as $number) {
1090
					$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer']);
1091
				}
1092
				$alldata = $Spotter->countAllAirlines(false,$globalArchiveMonths);
1093
				foreach ($alldata as $number) {
1094
					$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name']);
1095
				}
1096
				$alldata = $Spotter->countAllAircraftRegistrations(false,$globalArchiveMonths);
1097
				foreach ($alldata as $number) {
1098
					$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao']);
1099
				}
1100
				$alldata = $Spotter->countAllCallsigns(false,$globalArchiveMonths);
1101
				foreach ($alldata as $number) {
1102
					$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao']);
1103
				}
1104
				$alldata = $Spotter->countAllOwners(false,$globalArchiveMonths);
1105
				foreach ($alldata as $number) {
1106
					$this->addStatOwner($number['owner_name'],$number['owner_count']);
1107
				}
1108
				if ($Connection->tableExists('countries')) {
1109
					$alldata = $Spotter->countAllFlightOverCountries(false,$globalArchiveMonths);
1110
					foreach ($alldata as $number) {
1111
						$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count']);
1112
					}
1113
				}
1114
				$alldata = $Spotter->countAllPilots(false,$globalArchiveMonths);
1115
				foreach ($alldata as $number) {
1116
					$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name']);
1117
				}
1118
				$pall = $Spotter->countAllDepartureAirports(false,$globalArchiveMonths);
1119
        			$dall = $Spotter->countAllDetectedDepartureAirports(false,$globalArchiveMonths);
1120
	        		$alldata = array();
1121
    				foreach ($pall as $value) {
1122
	        			$icao = $value['airport_departure_icao'];
1123
    					$alldata[$icao] = $value;
1124
	        		}
1125
	        		foreach ($dall as $value) {
1126
    					$icao = $value['airport_departure_icao'];
1127
        				if (isset($alldata[$icao])) {                                                           
1128
        					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1129
	        			} else $alldata[$icao] = $value;
1130
    				}
1131
        			$count = array();
1132
        			foreach ($alldata as $key => $row) {
1133
        				$count[$key] = $row['airport_departure_icao_count'];
1134
	        		}
1135
    				array_multisort($count,SORT_DESC,$alldata);
1136
1137
				//print_r($alldate);
1138
				foreach ($alldata as $number) {
1139
					$this->addStatDepartureAirports($number['airport_departure_icao'],$number['airport_departure_name'],$number['airport_departure_city'],$number['airport_departure_country'],$number['airport_departure_icao_count']);
1140
				}
1141
				$pdata = $Spotter->countAllArrivalAirports(false,$globalArchiveMonths);
1142
        			$dall = $Spotter->countAllDetectedArrivalAirports(false,$globalArchiveMonths);
1143
	        		$alldata = array();
1144
    				foreach ($pdata as $value) {
1145
	        			$icao = $value['airport_arrival_icao'];
1146
    					$alldata[$icao] = $value;
1147
	        		}
1148
	        		foreach ($dall as $value) {
1149
    					$icao = $value['airport_arrival_icao'];
1150
        				if (isset($alldata[$icao])) {                                                           
1151
        					$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1152
	        			} else $alldata[$icao] = $value;
1153
    				}
1154
        			$count = array();
1155
        			foreach ($alldata as $key => $row) {
1156
        				$count[$key] = $row['airport_arrival_icao_count'];
1157
	        		}
1158
    				array_multisort($count,SORT_DESC,$alldata);
1159
				foreach ($alldata as $number) {
1160
					$this->addStatArrivalAirports($number['airport_arrival_icao'],$number['airport_arrival_name'],$number['airport_arrival_city'],$number['airport_arrival_country'],$number['airport_arrival_icao_count']);
1161
				}
1162
				$this->addStat('aircrafts_byyear',$this->getStatsAircraftTotal(),date('Y').'-01-01 00:00:00');
1163
				$this->addStat('airlines_byyear',$this->getStatsAirlineTotal(),date('Y').'-01-01 00:00:00');
1164
				$this->addStat('owner_byyear',$this->getStatsOwnerTotal(),date('Y').'-01-01 00:00:00');
1165
				$this->addStat('pilot_byyear',$this->getStatsPilotTotal(),date('Y').'-01-01 00:00:00');
1166
			
1167
				if ($globalArchive) {
1168
					if ($globalDBdriver == 'mysql') {
1169
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1170
					} else {
1171
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
1172
					}
1173
					try {
1174
						$sth = $this->db->prepare($query);
1175
						$sth->execute();
1176
					} catch(PDOException $e) {
1177
						return "error : ".$e->getMessage().' - query : '.$query."\n";
1178
					}
1179
				}
1180
	
1181
				//$query = 'DELETE FROM spotter_output WHERE spotter_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveMonths.' MONTH)';
1182
				if ($globalDBdriver == 'mysql') {
1183
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1184
				} else {
1185
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS, 'YYYY/mm/01') AS TIMESTAMP)";
1186
				}
1187
				try {
1188
					$sth = $this->db->prepare($query);
1189
					$sth->execute();
1190
				} catch(PDOException $e) {
1191
					return "error : ".$e->getMessage().' - query : '.$query."\n";
1192
				}
1193
			}
1194
			$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
1195
		} else {
1196
		*/
1197
			echo 'Update stats !'."\n";
1198
			if (isset($last_update[0]['value'])) {
1199
				$last_update_day = $last_update[0]['value'];
1200
			} else $last_update_day = '2012-12-12 12:12:12';
1201
			$Spotter = new Spotter($this->db);
1202
			$alldata = $Spotter->countAllAircraftTypes(false,0,$last_update_day);
1203
			foreach ($alldata as $number) {
1204
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer']);
1205
			}
1206
			$alldata = $Spotter->countAllAirlines(false,0,$last_update_day);
1207
			foreach ($alldata as $number) {
1208
				$this->addStatAirline($number['airline_icao'],$number['airline_count'],$number['airline_name']);
1209
			}
1210
			$alldata = $Spotter->countAllAircraftRegistrations(false,0,$last_update_day);
1211
			foreach ($alldata as $number) {
1212
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao']);
1213
			}
1214
			$alldata = $Spotter->countAllCallsigns(false,0,$last_update_day);
1215
			foreach ($alldata as $number) {
1216
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao']);
1217
			}
1218
			$alldata = $Spotter->countAllOwners(false,0,$last_update_day);
1219
			foreach ($alldata as $number) {
1220
				$this->addStatOwner($number['owner_name'],$number['owner_count']);
1221
			}
1222
			$alldata = $Spotter->countAllPilots(false,0,$last_update_day);
1223
			foreach ($alldata as $number) {
1224
				$this->addStatPilot($number['pilot_id'],$number['pilot_count'],$number['pilot_name']);
1225
			}
1226
			
1227
			$pall = $Spotter->countAllDepartureAirports(false,0,$last_update_day);
1228
        		$dall = $Spotter->countAllDetectedDepartureAirports(false,0,$last_update_day);
1229
	        	$alldata = array();
1230
	        	
1231
    			foreach ($pall as $value) {
1232
	        		$icao = $value['airport_departure_icao'];
1233
    				$alldata[$icao] = $value;
1234
	        	}
1235
	        	foreach ($dall as $value) {
1236
    				$icao = $value['airport_departure_icao'];
1237
        			if (isset($alldata[$icao])) {
1238
    					$alldata[$icao]['airport_departure_icao_count'] = $alldata[$icao]['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1239
        			} else $alldata[$icao] = $value;
1240
			}
1241
    			$count = array();
1242
    			foreach ($alldata as $key => $row) {
1243
    				$count[$key] = $row['airport_departure_icao_count'];
1244
        		}
1245
			array_multisort($count,SORT_DESC,$alldata);
1246
			foreach ($alldata as $number) {
1247
				echo $this->addStatDepartureAirports($number['airport_departure_icao'],$number['airport_departure_name'],$number['airport_departure_city'],$number['airport_departure_country'],$number['airport_departure_icao_count']);
1248
			}
1249
			$pall = $Spotter->countAllArrivalAirports(false,0,$last_update_day);
1250
        		$dall = $Spotter->countAllDetectedArrivalAirports(false,0,$last_update_day);
1251
	        	$alldata = array();
1252
    			foreach ($pall as $value) {
1253
	        		$icao = $value['airport_arrival_icao'];
1254
    				$alldata[$icao] = $value;
1255
	        	}
1256
	        	foreach ($dall as $value) {
1257
    				$icao = $value['airport_arrival_icao'];
1258
        			if (isset($alldata[$icao])) {                                                           
1259
        				$alldata[$icao]['airport_arrival_icao_count'] = $alldata[$icao]['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1260
	        		} else $alldata[$icao] = $value;
1261
    			}
1262
        		$count = array();
1263
        		foreach ($alldata as $key => $row) {
1264
        			$count[$key] = $row['airport_arrival_icao_count'];
1265
	        	}
1266
    			array_multisort($count,SORT_DESC,$alldata);
1267
                        foreach ($alldata as $number) {
1268
				echo $this->addStatArrivalAirports($number['airport_arrival_icao'],$number['airport_arrival_name'],$number['airport_arrival_city'],$number['airport_arrival_country'],$number['airport_arrival_icao_count']);
1269
			}
1270
			if ($Connection->tableExists('countries')) {
1271
				$SpotterArchive = new SpotterArchive();
1272
				$alldata = $SpotterArchive->countAllFlightOverCountries(false,0,$last_update_day);
1273
				foreach ($alldata as $number) {
1274
					$this->addStatCountry($number['flight_country_iso2'],$number['flight_country_iso3'],$number['flight_country'],$number['flight_count']);
1275
				}
1276
			}
1277
			
1278
1279
			// Add by month using getstat if month finish...
1280
1281
			//if (date('m',strtotime($last_update_day)) != date('m')) {
1282
			$Spotter = new Spotter($this->db);
1283
			$alldata = $Spotter->countAllMonths();
1284
			$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...
1285
			foreach ($alldata as $number) {
1286
				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...
1287
				$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'])));
1288
			}
1289
			$alldata = $Spotter->countAllMilitaryMonths();
1290
			foreach ($alldata as $number) {
1291
				$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'])));
1292
			}
1293
			$alldata = $Spotter->countAllMonthsOwners();
1294
			foreach ($alldata as $number) {
1295
				$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'])));
1296
			}
1297
			$alldata = $Spotter->countAllMonthsPilots();
1298
			foreach ($alldata as $number) {
1299
				$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'])));
1300
			}
1301
			$alldata = $Spotter->countAllMonthsAirlines();
1302
			foreach ($alldata as $number) {
1303
				$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'])));
1304
			}
1305
			$alldata = $Spotter->countAllMonthsAircrafts();
1306
			foreach ($alldata as $number) {
1307
				$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'])));
1308
			}
1309
			$alldata = $Spotter->countAllMonthsRealArrivals();
1310
			foreach ($alldata as $number) {
1311
				$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'])));
1312
			}
1313
			echo 'Airports data...'."\n";
1314
			echo '...Departure'."\n";
1315
			$this->deleteStatAirport('daily');
1316
//			$pall = $Spotter->getLast7DaysAirportsDeparture();
1317
  //      		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1318
			$pall = $Spotter->getLast7DaysAirportsDeparture();
1319
        		$dall = $Spotter->getLast7DaysDetectedAirportsDeparture();
1320
        		/*
1321
	        	$alldata = array();
1322
    			foreach ($pall as $value) {
1323
	        		$icao = $value['departure_airport_icao'];
1324
    				$alldata[$icao] = $value;
1325
	        	}
1326
	        	foreach ($dall as $value) {
1327
    				$icao = $value['departure_airport_icao'];
1328
    				$ddate = $value['date'];
1329
        			if (isset($alldata[$icao])) {
1330
        				$alldata[$icao]['departure_airport_count'] = $alldata[$icao]['departure_airport_count'] + $value['departure_airport_count'];
1331
	        		} else $alldata[$icao] = $value;
1332
    			}
1333
        		$count = array();
1334
        		foreach ($alldata as $key => $row) {
1335
        			$count[$key] = $row['departure_airport_count'];
1336
	        	}
1337
    			array_multisort($count,SORT_DESC,$alldata);
1338
    			*/
1339
    			foreach ($dall as $value) {
1340
    				$icao = $value['departure_airport_icao'];
1341
    				$ddate = $value['date'];
1342
    				$find = false;
1343
    				foreach ($pall as $pvalue) {
1344
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1345
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1346
    						$find = true;
1347
    						break;
1348
    					}
1349
    				}
1350
    				if ($find === false) {
1351
    					$pall[] = $value;
1352
    				}
1353
    			}
1354
    			$alldata = $pall;
1355
			foreach ($alldata as $number) {
1356
				$this->addStatDepartureAirportsDaily($number['date'],$number['departure_airport_icao'],$number['departure_airport_name'],$number['departure_airport_city'],$number['departure_airport_country'],$number['departure_airport_count']);
1357
			}
1358
			echo '...Arrival'."\n";
1359
			$pall = $Spotter->getLast7DaysAirportsArrival();
1360
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrival();
1361
        		/*
1362
	        	$alldata = array();
1363
    			foreach ($pall as $value) {
1364
	        		$icao = $value['arrival_airport_icao'];
1365
    				$alldata[$icao] = $value;
1366
	        	}
1367
	        	foreach ($dall as $value) {
1368
    				$icao = $value['arrival_airport_icao'];
1369
        			if (isset($alldata[$icao])) {
1370
        				$alldata[$icao]['arrival_airport_icao_count'] = $alldata[$icao]['arrival_airport_count'] + $value['arrival_airport_count'];
1371
	        		} else $alldata[$icao] = $value;
1372
    			}
1373
        		$count = array();
1374
        		foreach ($alldata as $key => $row) {
1375
        			$count[$key] = $row['arrival_airport_count'];
1376
	        	}
1377
    			array_multisort($count,SORT_DESC,$alldata);
1378
    			*/
1379
1380
1381
    			foreach ($dall as $value) {
1382
    				$icao = $value['arrival_airport_icao'];
1383
    				$ddate = $value['date'];
1384
    				$find = false;
1385
    				foreach ($pall as $pvalue) {
1386
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate) {
1387
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1388
    						$find = true;
1389
    						break;
1390
    					}
1391
    				}
1392
    				if ($find === false) {
1393
    					$pall[] = $value;
1394
    				}
1395
    			}
1396
    			$alldata = $pall;
1397
			foreach ($alldata as $number) {
1398
				$this->addStatArrivalAirportsDaily($number['date'],$number['arrival_airport_icao'],$number['arrival_airport_name'],$number['arrival_airport_city'],$number['arrival_airport_country'],$number['arrival_airport_count']);
1399
			}
1400
1401
			echo 'Flights data...'."\n";
1402
			$this->deleteStatFlight('month');
1403
			echo '-> countAllDatesLastMonth...'."\n";
1404
			$alldata = $Spotter->countAllDatesLastMonth();
1405
			foreach ($alldata as $number) {
1406
				$this->addStatFlight('month',$number['date_name'],$number['date_count']);
1407
			}
1408
			echo '-> countAllDates...'."\n";
1409
			$previousdata = $this->countAllDates();
1410
			$this->deleteStatFlight('date');
1411
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDates());
1412
			$values = array();
1413
			foreach ($alldata as $cnt) {
1414
				$values[] = $cnt['date_count'];
1415
			}
1416
			array_multisort($values,SORT_DESC,$alldata);
1417
			array_splice($alldata,11);
1418
			foreach ($alldata as $number) {
1419
				$this->addStatFlight('date',$number['date_name'],$number['date_count']);
1420
			}
1421
			
1422
			$this->deleteStatFlight('hour');
1423
			echo '-> countAllHours...'."\n";
1424
			$alldata = $Spotter->countAllHours('hour');
1425
			foreach ($alldata as $number) {
1426
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count']);
1427
			}
1428
			
1429
			// Count by airlines
1430
			echo '--- Stats by airlines ---'."\n";
1431
			$Spotter = new Spotter($this->db);
1432
			$alldata = $Spotter->countAllAircraftTypesByAirlines(false,0,$last_update_day);
1433
			foreach ($alldata as $number) {
1434
				$this->addStatAircraft($number['aircraft_icao'],$number['aircraft_icao_count'],$number['aircraft_name'],$number['aircraft_manufacturer'],$number['airline_icao']);
1435
			}
1436
			$alldata = $Spotter->countAllAircraftRegistrationsByAirlines(false,0,$last_update_day);
1437
			foreach ($alldata as $number) {
1438
				$this->addStatAircraftRegistration($number['registration'],$number['aircraft_registration_count'],$number['aircraft_icao'],$number['airline_icao']);
1439
			}
1440
			$alldata = $Spotter->countAllCallsignsByAirlines(false,0,$last_update_day);
1441
			foreach ($alldata as $number) {
1442
				$this->addStatCallsign($number['callsign_icao'],$number['callsign_icao_count'],$number['airline_icao']);
1443
			}
1444
			
1445
			$pall = $Spotter->countAllDepartureAirportsByAirlines(false,0,$last_update_day);
1446
       			$dall = $Spotter->countAllDetectedDepartureAirportsByAirlines(false,0,$last_update_day);
1447
	        	//$alldata = array();
1448
    			foreach ($dall as $value) {
1449
    				$icao = $value['airport_departure_icao'];
1450
    				$dicao = $value['airline_icap'];
1451
    				$find = false;
1452
    				foreach ($pall as $pvalue) {
1453
    					if ($pvalue['airport_departure_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1454
    						$pvalue['airport_departure_icao_count'] = $pvalue['airport_departure_icao_count'] + $value['airport_departure_icao_count'];
1455
    						$find = true;
1456
    						break;
1457
    					}
1458
    				}
1459
    				if ($find === false) {
1460
    					$pall[] = $value;
1461
    				}
1462
    			}
1463
    			$alldata = $pall;
1464
			foreach ($alldata as $number) {
1465
				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']);
1466
			}
1467
			$pall = $Spotter->countAllArrivalAirportsByAirlines(false,0,$last_update_day);
1468
        		$dall = $Spotter->countAllDetectedArrivalAirportsByAirlines(false,0,$last_update_day);
1469
	        	//$alldata = array();
1470
    			foreach ($dall as $value) {
1471
    				$icao = $value['airport_arrival_icao'];
1472
    				$dicao = $value['airline_icao'];
1473
    				$find = false;
1474
    				foreach ($pall as $pvalue) {
1475
    					if ($pvalue['airport_arrival_icao'] == $icao && $pvalue['airline_icao'] = $dicao) {
1476
    						$pvalue['airport_arrival_icao_count'] = $pvalue['airport_arrival_icao_count'] + $value['airport_arrival_icao_count'];
1477
    						$find = true;
1478
    						break;
1479
    					}
1480
    				}
1481
    				if ($find === false) {
1482
    					$pall[] = $value;
1483
    				}
1484
    			}
1485
    			$alldata = $pall;
1486
                        foreach ($alldata as $number) {
1487
				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']);
1488
			}
1489
			$Spotter = new Spotter($this->db);
1490
			$alldata = $Spotter->countAllMonthsByAirlines();
1491
			$lastyear = false;
1492
			foreach ($alldata as $number) {
1493
				if ($number['year_name'] != date('Y')) $lastyear = true;
1494
				$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']);
1495
			}
1496
			$alldata = $Spotter->countAllMonthsPilotsByAirlines();
1497
			foreach ($alldata as $number) {
1498
				$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']);
1499
			}
1500
			$alldata = $Spotter->countAllMonthsAircraftsByAirlines();
1501
			foreach ($alldata as $number) {
1502
				$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']);
1503
			}
1504
			$alldata = $Spotter->countAllMonthsRealArrivalsByAirlines();
1505
			foreach ($alldata as $number) {
1506
				$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']);
1507
			}
1508
			echo '...Departure'."\n";
1509
			$pall = $Spotter->getLast7DaysAirportsDepartureByAirlines();
1510
        		$dall = $Spotter->getLast7DaysDetectedAirportsDepartureByAirlines();
1511
    			foreach ($dall as $value) {
1512
    				$icao = $value['departure_airport_icao'];
1513
    				$airline = $value['airline_icao'];
1514
    				$ddate = $value['date'];
1515
    				$find = false;
1516
    				foreach ($pall as $pvalue) {
1517
    					if ($pvalue['departure_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] = $airline) {
1518
    						$pvalue['departure_airport_count'] = $pvalue['departure_airport_count'] + $value['departure_airport_count'];
1519
    						$find = true;
1520
    						break;
1521
    					}
1522
    				}
1523
    				if ($find === false) {
1524
    					$pall[] = $value;
1525
    				}
1526
    			}
1527
    			$alldata = $pall;
1528
			foreach ($alldata as $number) {
1529
				$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']);
1530
			}
1531
			echo '...Arrival'."\n";
1532
			$pall = $Spotter->getLast7DaysAirportsArrivalByAirlines();
1533
        		$dall = $Spotter->getLast7DaysDetectedAirportsArrivalByAirlines();
1534
    			foreach ($dall as $value) {
1535
    				$icao = $value['arrival_airport_icao'];
1536
    				$airline = $value['airline_icao'];
1537
    				$ddate = $value['date'];
1538
    				$find = false;
1539
    				foreach ($pall as $pvalue) {
1540
    					if ($pvalue['arrival_airport_icao'] == $icao && $pvalue['date'] == $ddate && $pvalue['airline_icao'] == $airline) {
1541
    						$pvalue['arrival_airport_count'] = $pvalue['arrival_airport_count'] + $value['arrival_airport_count'];
1542
    						$find = true;
1543
    						break;
1544
    					}
1545
    				}
1546
    				if ($find === false) {
1547
    					$pall[] = $value;
1548
    				}
1549
    			}
1550
    			$alldata = $pall;
1551
			foreach ($alldata as $number) {
1552
				$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']);
1553
			}
1554
1555
			echo 'Flights data...'."\n";
1556
			echo '-> countAllDatesLastMonth...'."\n";
1557
			$alldata = $Spotter->countAllDatesLastMonthByAirlines();
1558
			foreach ($alldata as $number) {
1559
				$this->addStatFlight('month',$number['date_name'],$number['date_count'], $number['airline_icao']);
1560
			}
1561
			echo '-> countAllDates...'."\n";
1562
			$previousdata = $this->countAllDatesByAirlines();
1563
			$alldata = $Common->array_merge_noappend($previousdata,$Spotter->countAllDatesByAirlines());
1564
			$values = array();
1565
			foreach ($alldata as $cnt) {
1566
				$values[] = $cnt['date_count'];
1567
			}
1568
			array_multisort($values,SORT_DESC,$alldata);
1569
			array_splice($alldata,11);
1570
			foreach ($alldata as $number) {
1571
				$this->addStatFlight('date',$number['date_name'],$number['date_count'],$number['airline_icao']);
1572
			}
1573
			
1574
			echo '-> countAllHours...'."\n";
1575
			$alldata = $Spotter->countAllHoursByAirlines('hour');
1576
			foreach ($alldata as $number) {
1577
				$this->addStatFlight('hour',$number['hour_name'],$number['hour_count'],$number['airline_icao']);
1578
			}
1579
			
1580
1581
			
1582
			
1583
			if ($lastyear) {
1584
				echo 'Data from last year...'."\n";
1585
				// SUM all previous month to put as year
1586
				$previous_year = date('Y');
1587
				$previous_year--;
1588
				$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1589
				$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1590
				$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1591
				$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year),$previous_year.'-01-01 00:00:00');
1592
				$allairlines = $this->getAllAirlineNames();
1593
				foreach ($allairlines as $data) {
1594
					$this->addStat('aircrafts_byyear',$this->getSumStats('aircrafts_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1595
					$this->addStat('airlines_byyear',$this->getSumStats('airlines_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1596
					$this->addStat('owner_byyear',$this->getSumStats('owner_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1597
					$this->addStat('pilot_byyear',$this->getSumStats('pilot_bymonth',$previous_year,$data['airline_icao']),$previous_year.'-01-01 00:00:00',$data['airline_icao']);
1598
				}
1599
				
1600
				if (isset($globalArchiveYear) && $globalArchiveYear) {
1601
					if ($globalArchive) {
1602
						$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
1603
						try {
1604
							$sth = $this->db->prepare($query);
1605
							$sth->execute();
1606
						} catch(PDOException $e) {
1607
							return "error : ".$e->getMessage().' - query : '.$query."\n";
1608
						}
1609
					}
1610
					echo 'Delete old data'."\n";
1611
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < '".date('Y')."-01-01 00:00:00'";
1612
					try {
1613
						$sth = $this->db->prepare($query);
1614
						$sth->execute();
1615
					} catch(PDOException $e) {
1616
						return "error : ".$e->getMessage().' - query : '.$query."\n";
1617
					}
1618
				}
1619
			}
1620
			if ($globalArchiveMonths > 0) {
1621
				if ($globalArchive) {
1622
					echo 'Archive old data...'."\n";
1623
					if ($globalDBdriver == 'mysql') {
1624
						//$query = "INSERT INTO spotter_archive_output SELECT * FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1625
						$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)
1626
							    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
1627
	    						     FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1628
					} else {
1629
						$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)
1630
							     SELECT 
1631
								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
1632
							    FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
1633
					}
1634
					try {
1635
						$sth = $this->db->prepare($query);
1636
						$sth->execute();
1637
					} catch(PDOException $e) {
1638
						return "error : ".$e->getMessage();
1639
					}
1640
				}
1641
				echo 'Deleting old data...'."\n";
1642
				//$query = 'DELETE FROM spotter_output WHERE spotter_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$globalArchiveMonths.' MONTH)';
1643
				if ($globalDBdriver == 'mysql') {
1644
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < DATE_FORMAT(UTC_TIMESTAMP() - INTERVAL ".$globalArchiveMonths." MONTH, '%Y/%m/01')";
1645
				} else {
1646
					$query = "DELETE FROM spotter_output WHERE spotter_output.date < CAST(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$globalArchiveMonths." MONTHS', 'YYYY/mm/01') AS TIMESTAMP)";
1647
				}
1648
				try {
1649
					$sth = $this->db->prepare($query);
1650
					$sth->execute();
1651
				} catch(PDOException $e) {
1652
					return "error : ".$e->getMessage();
1653
				}
1654
			}
1655
			echo 'Insert last stats update date...'."\n";
1656
			date_default_timezone_set('UTC');
1657
			$this->addLastStatsUpdate('last_update_stats',date('Y-m-d G:i:s'));
1658
		//}
1659
	}
1660
}
1661
1662
?>