Completed
Push — master ( bb48eb...8e257c )
by Yannick
34:24
created

Marine::getRace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This class is part of FlightAirmap. It's used for marine data
4
 *
5
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
6
 * Licensed under AGPL license.
7
 * For more information see: https://www.flightairmap.com/
8
*/
9
require_once(dirname(__FILE__).'/class.Image.php');
10
$global_marine_query = "SELECT marine_output.* FROM marine_output";
11
12
class Marine{
13
	public $db;
14
	
15
	public function __construct($dbc = null) {
16
		$Connection = new Connection($dbc);
17
		$this->db = $Connection->db();
18
		if ($this->db === null) die('Error: No DB connection. (Marine)');
19
	}
20
21
	/**
22
	* Get SQL query part for filter used
23
	* @param Array $filter the filter
24
	* @return Array the SQL part
25
	*/
26
	
27
	public function getFilter($filter = array(),$where = false,$and = false) {
28
		global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver;
29
		$filters = array();
30
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
31
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
32
				$filters = $globalStatsFilters[$globalFilterName];
33
			} else {
34
				$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]);
35
			}
36
		}
37
		if (isset($filter[0]['source'])) {
38
			$filters = array_merge($filters,$filter);
39
		}
40
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
41
		$filter_query_join = '';
42
		$filter_query_where = '';
43
		foreach($filters as $flt) {
44
			if (isset($flt['idents']) && !empty($flt['idents'])) {
45
				if (isset($flt['source'])) {
46
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."') AND marine_output.format_source IN ('".implode("','",$flt['source'])."')) spfi ON spfi.fammarine_id = marine_output.fammarine_id";
47
				} else {
48
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."')) spfi ON spfi.fammarine_id = marine_output.fammarine_id";
49
				}
50
			}
51
		}
52
		if (isset($filter['source']) && !empty($filter['source'])) {
53
			$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
54
		}
55
		if (isset($filter['ident']) && !empty($filter['ident'])) {
56
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
57
		}
58
		if (isset($filter['id']) && !empty($filter['id'])) {
59
			$filter_query_where .= " AND fammarine_id = '".$filter['id']."'";
60
		}
61
		if (isset($filter['mmsi']) && !empty($filter['mmsi'])) {
62
			$filter_query_where .= " AND mmsi = '".$filter['mmsi']."'";
63
		}
64
		if (isset($filter['race']) && !empty($filter['race'])) {
65
			$filter_query_where .= " AND race_id = '".$filter['race']."'";
66
		}
67
		if (isset($filter['year']) && $filter['year'] != '') {
68
			if ($globalDBdriver == 'mysql') {
69
				$filter_query_where .= " AND YEAR(marine_output.date) = '".$filter['year']."'";
70
			} else {
71
				$filter_query_where .= " AND EXTRACT(YEAR FROM marine_output.date) = '".$filter['year']."'";
72
			}
73
		}
74
		if (isset($filter['month']) && $filter['month'] != '') {
75
			if ($globalDBdriver == 'mysql') {
76
				$filter_query_where .= " AND MONTH(marine_output.date) = '".$filter['month']."'";
77
			} else {
78
				$filter_query_where .= " AND EXTRACT(MONTH FROM marine_output.date) = '".$filter['month']."'";
79
			}
80
		}
81
		if (isset($filter['day']) && $filter['day'] != '') {
82
			if ($globalDBdriver == 'mysql') {
83
				$filter_query_where .= " AND DAY(marine_output.date) = '".$filter['day']."'";
84
			} else {
85
				$filter_query_where .= " AND EXTRACT(DAY FROM marine_output.date) = '".$filter['day']."'";
86
			}
87
		}
88
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
89
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
90
		if ($filter_query_where != '') {
91
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
92
		}
93
		$filter_query = $filter_query_join.$filter_query_where;
94
		return $filter_query;
95
	}
96
97
	/**
98
	* Executes the SQL statements to get the spotter information
99
	*
100
	* @param String $query the SQL query
101
	* @param Array $params parameter of the query
102
	* @param String $limitQuery the limit query
103
	* @return Array the spotter information
104
	*
105
	*/
106
	public function getDataFromDB($query, $params = array(), $limitQuery = '',$schedules = false)
0 ignored issues
show
Unused Code introduced by
The parameter $schedules is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
	{
108
		global $globalVM;
109
		date_default_timezone_set('UTC');
110
		if (!is_string($query))
111
		{
112
			return false;
113
		}
114
		
115
		if ($limitQuery != "")
116
		{
117
			if (!is_string($limitQuery))
118
			{
119
				return false;
120
			}
121
		}
122
123
		try {
124
			$sth = $this->db->prepare($query.$limitQuery);
125
			$sth->execute($params);
126
		} catch (PDOException $e) {
127
			printf("Invalid query : %s\nWhole query: %s\n",$e->getMessage(), $query.$limitQuery);
128
			exit();
129
		}
130
		
131
		$num_rows = 0;
132
		$spotter_array = array();
133
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
134
		{
135
			$num_rows++;
136
			$temp_array = array();
137
			if (isset($row['marine_live_id'])) {
138
				$temp_array['marine_id'] = $this->getMarineIDBasedOnFamMarineID($row['fammarine_id']);
139
			/*
140
			} elseif (isset($row['spotter_archive_id'])) {
141
				$temp_array['spotter_id'] = $row['spotter_archive_id'];
142
			} elseif (isset($row['spotter_archive_output_id'])) {
143
				$temp_array['spotter_id'] = $row['spotter_archive_output_id'];
144
			*/} 
145
			elseif (isset($row['marineid'])) {
146
				$temp_array['marine_id'] = $row['marineid'];
147
			} else {
148
				$temp_array['marine_id'] = '';
149
			}
150
			if (isset($row['fammarine_id'])) $temp_array['fammarine_id'] = $row['fammarine_id'];
151
			if (isset($row['mmsi'])) $temp_array['mmsi'] = $row['mmsi'];
152
			if (isset($row['type'])) $temp_array['type'] = html_entity_decode($row['type'],ENT_QUOTES);
153
			if (isset($row['type_id'])) $temp_array['type_id'] = $row['type_id'];
154
			if (isset($row['status'])) $temp_array['status'] = $row['status'];
155
			if (isset($row['status_id'])) $temp_array['status_id'] = $row['status_id'];
156
			if (isset($row['captain_id'])) $temp_array['captain_id'] = $row['captain_id'];
157
			if (isset($row['captain_name'])) $temp_array['captain_name'] = $row['captain_name'];
158
			if (isset($row['race_id'])) $temp_array['race_id'] = $row['race_id'];
159
			if (isset($row['race_name'])) $temp_array['race_name'] = $row['race_name'];
160
			if (isset($row['race_time']) && isset($row['status']) && $row['status'] != 'Racing' && $row['race_time'] > 0) $temp_array['race_time'] = $row['race_time'];
161
			if (isset($row['race_rank'])) $temp_array['race_rank'] = $row['race_rank'];
162
			if (isset($row['ident'])) $temp_array['ident'] = $row['ident'];
163
			if (isset($row['arrival_port_name'])) $temp_array['arrival_port_name'] = $row['arrival_port_name'];
164
			if (isset($row['latitude'])) $temp_array['latitude'] = $row['latitude'];
165
			if (isset($row['longitude'])) $temp_array['longitude'] = $row['longitude'];
166
			if (isset($row['distance']) && $row['distance'] != '') $temp_array['distance'] = $row['distance'];
167
			if (isset($row['format_source'])) $temp_array['format_source'] = $row['format_source'];
168
			if (isset($row['heading'])) {
169
				$temp_array['heading'] = $row['heading'];
170
				$heading_direction = $this->parseDirection($row['heading']);
171
				if (isset($heading_direction[0]['direction_fullname'])) $temp_array['heading_name'] = $heading_direction[0]['direction_fullname'];
172
			}
173
			if (isset($row['ground_speed'])) $temp_array['ground_speed'] = $row['ground_speed'];
174
175
			if(isset($temp_array['mmsi']) && $temp_array['mmsi'] != "")
176
			{
177
				$Image = new Image($this->db);
178
				if (isset($temp_array['ident']) && $temp_array['ident'] != '') $image_array = $Image->getMarineImage($temp_array['mmsi'],'',$temp_array['ident']);
179
				else $image_array = $Image->getMarineImage($temp_array['mmsi']);
180
				unset($Image);
181
				if (count($image_array) > 0) {
182
					$temp_array['image'] = $image_array[0]['image'];
183
					$temp_array['image_thumbnail'] = $image_array[0]['image_thumbnail'];
184
					$temp_array['image_source'] = $image_array[0]['image_source'];
185
					$temp_array['image_source_website'] = $image_array[0]['image_source_website'];
186
					$temp_array['image_copyright'] = $image_array[0]['image_copyright'];
187
				}
188
			} elseif(isset($temp_array['type']) && $temp_array['type'] != "")
189
			{
190
				$Image = new Image($this->db);
191
				$image_array = $Image->getMarineImage('','','',$temp_array['type']);
192
				unset($Image);
193
				if (count($image_array) > 0) {
194
					$temp_array['image'] = $image_array[0]['image'];
195
					$temp_array['image_thumbnail'] = $image_array[0]['image_thumbnail'];
196
					$temp_array['image_source'] = $image_array[0]['image_source'];
197
					$temp_array['image_source_website'] = $image_array[0]['image_source_website'];
198
					$temp_array['image_copyright'] = $image_array[0]['image_copyright'];
199
				}
200
			}
201
			
202
			if (isset($row['date'])) {
203
				$dateArray = $this->parseDateString($row['date']);
204
				if ($dateArray['seconds'] < 10)
205
				{
206
					$temp_array['date'] = "a few seconds ago";
207
				} elseif ($dateArray['seconds'] >= 5 && $dateArray['seconds'] < 30)
208
				{
209
					$temp_array['date'] = "half a minute ago";
210
				} elseif ($dateArray['seconds'] >= 30 && $dateArray['seconds'] < 60)
211
				{
212
					$temp_array['date'] = "about a minute ago";
213
				} elseif ($dateArray['minutes'] < 5)
214
				{
215
					$temp_array['date'] = "a few minutes ago";
216
				} elseif ($dateArray['minutes'] >= 5 && $dateArray['minutes'] < 60)
217
				{
218
					$temp_array['date'] = "about ".$dateArray['minutes']." minutes ago";
219
				} elseif ($dateArray['hours'] < 2)
220
				{
221
					$temp_array['date'] = "about an hour ago";
222
				} elseif ($dateArray['hours'] >= 2 && $dateArray['hours'] < 24)
223
				{
224
					$temp_array['date'] = "about ".$dateArray['hours']." hours ago";
225
				} else {
226
					$temp_array['date'] = date("M j Y, g:i a",strtotime($row['date']." UTC"));
227
				}
228
				$temp_array['date_minutes_past'] = $dateArray['minutes'];
229
				$temp_array['date_iso_8601'] = date("c",strtotime($row['date']." UTC"));
230
				$temp_array['date_rfc_2822'] = date("r",strtotime($row['date']." UTC"));
231
				$temp_array['date_unix'] = strtotime($row['date']." UTC");
232
				if (isset($row['last_seen']) && $row['last_seen'] != '') {
233
					if (strtotime($row['last_seen']) > strtotime($row['date'])) {
234
						$temp_array['duration'] = strtotime($row['last_seen']) - strtotime($row['date']);
235
						$temp_array['last_seen_date_iso_8601'] = date("c",strtotime($row['last_seen']." UTC"));
236
						$temp_array['last_seen_date_rfc_2822'] = date("r",strtotime($row['last_seen']." UTC"));
237
						$temp_array['last_seen_date_unix'] = strtotime($row['last_seen']." UTC");
238
					}
239
				}
240
			}
241
			
242
			$fromsource = NULL;
0 ignored issues
show
Unused Code introduced by
$fromsource 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...
243
			if (isset($row['source_name']) && $row['source_name'] != '') $temp_array['source_name'] = $row['source_name'];
244
			if (isset($row['over_country']) && $row['over_country'] != '') $temp_array['over_country'] = $row['over_country'];
245
			if (isset($row['distance']) && $row['distance'] != '') $temp_array['distance'] = $row['distance'];
246
			$temp_array['query_number_rows'] = $num_rows;
247
			$spotter_array[] = $temp_array;
248
		}
249
		if ($num_rows == 0) return array();
250
		$spotter_array[0]['query_number_rows'] = $num_rows;
251
		return $spotter_array;
252
	}	
253
	
254
	
255
	/**
256
	* Gets all the spotter information based on the latest data entry
257
	*
258
	* @return Array the spotter information
259
	*
260
	*/
261
	public function getLatestMarineData($limit = '', $sort = '', $filter = array())
262
	{
263
		global $global_marine_query;
264
		date_default_timezone_set('UTC');
265
		$filter_query = $this->getFilter($filter);
266
		if ($limit != "")
267
		{
268
			$limit_array = explode(",", $limit);
269
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
270
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
271
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
272
			{
273
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
274
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
275
			} else $limit_query = "";
276
		} else $limit_query = "";
277
		if ($sort != "")
278
		{
279
			$search_orderby_array = $this->getOrderBy();
280
			$orderby_query = $search_orderby_array[$sort]['sql'];
281
		} else {
282
			$orderby_query = " ORDER BY marine_output.date DESC";
283
		}
284
		$query  = $global_marine_query.$filter_query." ".$orderby_query;
285
		$spotter_array = $this->getDataFromDB($query, array(),$limit_query,true);
286
		return $spotter_array;
287
	}
288
    
289
	/*
290
	* Gets all the spotter information based on the spotter id
291
	*
292
	* @return Array the spotter information
293
	*
294
	*/
295
	public function getMarineDataByID($id = '')
296
	{
297
		global $global_marine_query;
298
		
299
		date_default_timezone_set('UTC');
300
		if ($id == '') return array();
301
		$additional_query = "marine_output.fammarine_id = :id";
302
		$query_values = array(':id' => $id);
303
		$query  = $global_marine_query." WHERE ".$additional_query." ";
304
		$spotter_array = $this->getDataFromDB($query,$query_values);
305
		return $spotter_array;
306
	}
307
308
	/**
309
	* Gets all the spotter information based on the callsign
310
	*
311
	* @return Array the spotter information
312
	*
313
	*/
314
	public function getMarineDataByIdent($ident = '', $limit = '', $sort = '', $filter = array())
315
	{
316
		global $global_marine_query;
317
		
318
		date_default_timezone_set('UTC');
319
		
320
		$query_values = array();
321
		$limit_query = '';
322
		$additional_query = '';
323
		$filter_query = $this->getFilter($filter,true,true);
324
		if ($ident != "")
325
		{
326
			if (!is_string($ident))
327
			{
328
				return false;
329
			} else {
330
				$additional_query = " AND marine_output.ident = :ident";
331
				$query_values = array(':ident' => $ident);
332
			}
333
		}
334
		
335
		if ($limit != "")
336
		{
337
			$limit_array = explode(",", $limit);
338
			
339
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
340
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
341
			
342
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
343
			{
344
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
345
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
346
			}
347
		}
348
349
		if ($sort != "")
350
		{
351
			$search_orderby_array = $this->getOrderBy();
352
			$orderby_query = $search_orderby_array[$sort]['sql'];
353
		} else {
354
			$orderby_query = " ORDER BY marine_output.date DESC";
355
		}
356
357
		$query = $global_marine_query.$filter_query." ".$additional_query." ".$orderby_query;
358
		//echo $query."\n";
359
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
360
361
		return $spotter_array;
362
	}
363
364
	/**
365
	* Gets all the marine information based on the type
366
	*
367
	* @return Array the marine information
368
	*
369
	*/
370
	public function getMarineDataByType($type = '', $limit = '', $sort = '', $filter = array())
371
	{
372
		global $global_marine_query;
373
		
374
		date_default_timezone_set('UTC');
375
		
376
		$query_values = array();
0 ignored issues
show
Unused Code introduced by
$query_values 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...
377
		$limit_query = '';
378
		$additional_query = '';
0 ignored issues
show
Unused Code introduced by
$additional_query 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...
379
		$filter_query = $this->getFilter($filter,true,true);
380
		if (!is_string($type))
381
		{
382
			return false;
383
		} else {
384
			$additional_query = " AND marine_output.type_id = :type";
385
			$query_values = array(':type' => $type);
386
		}
387
		
388
		if ($limit != "")
389
		{
390
			$limit_array = explode(",", $limit);
391
			
392
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
393
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
394
			
395
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
396
			{
397
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
398
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
399
			}
400
		}
401
402
		if ($sort != "")
403
		{
404
			$search_orderby_array = $this->getOrderBy();
405
			$orderby_query = $search_orderby_array[$sort]['sql'];
406
		} else {
407
			$orderby_query = " ORDER BY marine_output.date DESC";
408
		}
409
410
		$query = $global_marine_query.$filter_query." marine_output.type <> '' ".$additional_query." ".$orderby_query;
411
		//echo $query."\n";
412
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
413
414
		return $spotter_array;
415
	}
416
	
417
	public function getMarineDataByDate($date = '', $limit = '', $sort = '',$filter = array())
418
	{
419
		global $global_marine_query, $globalTimezone, $globalDBdriver;
420
		
421
		$query_values = array();
422
		$limit_query = '';
423
		$additional_query = '';
424
425
		$filter_query = $this->getFilter($filter,true,true);
426
		
427
		if ($date != "")
428
		{
429
			if ($globalTimezone != '') {
430
				date_default_timezone_set($globalTimezone);
431
				$datetime = new DateTime($date);
432
				$offset = $datetime->format('P');
433
			} else {
434
				date_default_timezone_set('UTC');
435
				$datetime = new DateTime($date);
436
				$offset = '+00:00';
437
			}
438
			if ($globalDBdriver == 'mysql') {
439
				$additional_query = " AND DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date ";
440
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':offset' => $offset);
441
			} elseif ($globalDBdriver == 'pgsql') {
442
				$additional_query = " AND to_char(marine_output.date AT TIME ZONE :timezone,'YYYY-mm-dd') = :date ";
443
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':timezone' => $globalTimezone);
444
			}
445
		}
446
		
447
		if ($limit != "")
448
		{
449
			$limit_array = explode(",", $limit);
450
			
451
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
452
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
453
			
454
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
455
			{
456
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
457
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
458
			}
459
		}
460
461
		if ($sort != "")
462
		{
463
			$search_orderby_array = $this->getOrderBy();
464
			$orderby_query = $search_orderby_array[$sort]['sql'];
465
		} else {
466
			$orderby_query = " ORDER BY marine_output.date DESC";
467
		}
468
469
		$query = $global_marine_query.$filter_query." marine_output.ident <> '' ".$additional_query.$orderby_query;
470
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
471
		return $spotter_array;
472
	}
473
474
	/**
475
	* Gets all the marine information based on the captain
476
	*
477
	* @return Array the marine information
478
	*
479
	*/
480
	public function getMarineDataByCaptain($captain = '', $limit = '', $sort = '', $filter = array())
481
	{
482
		global $global_marine_query;
483
		date_default_timezone_set('UTC');
484
		$query_values = array();
485
		$limit_query = '';
486
		$additional_query = '';
487
		$filter_query = $this->getFilter($filter,true,true);
488
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
489
		if ($captain != "")
490
		{
491
			$additional_query = " AND (marine_output.captain_name = :captain OR marine_output.captain_id = :captain)";
492
			$query_values = array(':captain' => $captain);
493
		}
494
		if ($limit != "")
495
		{
496
			$limit_array = explode(",", $limit);
497
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
498
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
499
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
500
			{
501
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
502
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
503
			}
504
		}
505
		if ($sort != "")
506
		{
507
			$search_orderby_array = $this->getOrderBy();
508
			$orderby_query = $search_orderby_array[$sort]['sql'];
509
		} else {
510
			$orderby_query = " ORDER BY marine_output.date DESC";
511
		}
512
		$query = $global_marine_query.$filter_query." marine_output.captain_name <> '' ".$additional_query." ".$orderby_query;
513
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
514
		return $spotter_array;
515
	}
516
517
	/**
518
	* Gets all the marine information based on the race
519
	*
520
	* @return Array the marine information
521
	*
522
	*/
523
	public function getMarineDataByRace($race = '', $limit = '', $sort = '', $filter = array())
524
	{
525
		global $global_marine_query,$globalDBdriver;
526
		date_default_timezone_set('UTC');
527
		$query_values = array();
528
		$limit_query = '';
529
		$additional_query = '';
530
		$filter_query = $this->getFilter($filter,true,true);
531
		$race = filter_var($race,FILTER_SANITIZE_STRING);
532
		if ($race != "")
533
		{
534
			$additional_query = " AND (marine_output.race_name = :race OR marine_output.race_id = :race)";
535
			$query_values = array(':race' => $race);
536
		}
537
		if ($limit != "")
538
		{
539
			$limit_array = explode(",", $limit);
540
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
541
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
542
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
543
			{
544
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
545
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
546
			}
547
		}
548
		if ($sort != "")
549
		{
550
			$search_orderby_array = $this->getOrderBy();
551
			$orderby_query = $search_orderby_array[$sort]['sql'];
552
		} else {
553
			if ($globalDBdriver == 'mysql') {
554
				$orderby_query = " ORDER BY -marine_output.race_rank DESC, marine_output.distance ASC";
555
			} else {
556
				$orderby_query = " ORDER BY marine_output.race_rank ASC, marine_output.distance ASC";
557
			}
558
		}
559
		$query = $global_marine_query.$filter_query." marine_output.race_name <> '' ".$additional_query." ".$orderby_query;
560
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
561
		return $spotter_array;
562
	}
563
564
	/**
565
	* Count races by captain
566
	*
567
	* @return Integer number of race for a captain
568
	*
569
	*/
570
	public function countRacesByCaptain($captain,$filters = array())
571
	{
572
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
573
		$filter_query = $this->getFilter($filters,true,true);
574
		$query  = "SELECT COUNT(*) AS nb 
575
			FROM marine_output".$filter_query." (marine_output.captain_name = :captain OR marine_output.captain_id = :captain)";
576
		$query_values = array();
577
		$query_values = array_merge($query_values,array(':captain' => $captain));
578
		$sth = $this->db->prepare($query);
579
		$sth->execute($query_values);
580
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
581
		return $result[0]['nb'];
582
	}
583
584
	/**
585
	* Count captains by race
586
	*
587
	* @return String Duration of all races
588
	*
589
	*/
590
	public function countCaptainsByRace($race,$filters = array())
591
	{
592
		$race = filter_var($race,FILTER_SANITIZE_STRING);
593
		$filter_query = $this->getFilter($filters,true,true);
594
		$query  = "SELECT COUNT(*) AS nb 
595
			FROM marine_output".$filter_query." (marine_output.race_name = :race OR marine_output.race_id = :race)";
596
		$query_values = array();
597
		$query_values = array_merge($query_values,array(':race' => $race));
598
		$sth = $this->db->prepare($query);
599
		$sth->execute($query_values);
600
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
601
		return $result[0]['nb'];
602
	}
603
604
	/**
605
	* Gets all boat types that have been used by a captain
606
	*
607
	* @return Array the boat list
608
	*
609
	*/
610
	public function countAllBoatTypesByCaptain($captain,$filters = array(),$year = '',$month = '',$day = '')
611
	{
612
		global $globalDBdriver;
613
		$filter_query = $this->getFilter($filters,true,true);
614
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
615
		$query  = "SELECT DISTINCT marine_output.type, COUNT(marine_output.type) AS type_count
616
			FROM marine_output".$filter_query." (marine_output.captain_id = :captain OR marine_output.captain_name = :captain)";
617
		$query_values = array();
618
		if ($year != '') {
619
			if ($globalDBdriver == 'mysql') {
620
				$query .= " AND YEAR(marine_output.date) = :year";
621
				$query_values = array_merge($query_values,array(':year' => $year));
622
			} else {
623
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
624
				$query_values = array_merge($query_values,array(':year' => $year));
625
			}
626
		}
627
		if ($month != '') {
628
			if ($globalDBdriver == 'mysql') {
629
				$query .= " AND MONTH(marine_output.date) = :month";
630
				$query_values = array_merge($query_values,array(':month' => $month));
631
			} else {
632
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
633
				$query_values = array_merge($query_values,array(':month' => $month));
634
			}
635
		}
636
		if ($day != '') {
637
			if ($globalDBdriver == 'mysql') {
638
				$query .= " AND DAY(marine_output.date) = :day";
639
				$query_values = array_merge($query_values,array(':day' => $day));
640
			} else {
641
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
642
				$query_values = array_merge($query_values,array(':day' => $day));
643
			}
644
		}
645
		$query .= " GROUP BY marine_output.type
646
			ORDER BY type_count DESC";
647
		$query_values = array_merge($query_values,array(':captain' => $captain));
648
		$sth = $this->db->prepare($query);
649
		$sth->execute($query_values);
650
		return $sth->fetchAll(PDO::FETCH_ASSOC);
651
	}
652
653
	/**
654
	* Gets all boat types that have been used on a race
655
	*
656
	* @return Array the boat list
657
	*
658
	*/
659
	public function countAllBoatTypesByRace($race,$filters = array(),$year = '',$month = '',$day = '')
660
	{
661
		global $globalDBdriver;
662
		$filter_query = $this->getFilter($filters,true,true);
663
		$race = filter_var($race,FILTER_SANITIZE_STRING);
664
		$query  = "SELECT DISTINCT marine_output.type, COUNT(marine_output.type) AS type_count
665
			FROM marine_output".$filter_query." (marine_output.race_id = :race OR marine_output.race_name = :race)";
666
		$query_values = array();
667
		if ($year != '') {
668
			if ($globalDBdriver == 'mysql') {
669
				$query .= " AND YEAR(marine_output.date) = :year";
670
				$query_values = array_merge($query_values,array(':year' => $year));
671
			} else {
672
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
673
				$query_values = array_merge($query_values,array(':year' => $year));
674
			}
675
		}
676
		if ($month != '') {
677
			if ($globalDBdriver == 'mysql') {
678
				$query .= " AND MONTH(marine_output.date) = :month";
679
				$query_values = array_merge($query_values,array(':month' => $month));
680
			} else {
681
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
682
				$query_values = array_merge($query_values,array(':month' => $month));
683
			}
684
		}
685
		if ($day != '') {
686
			if ($globalDBdriver == 'mysql') {
687
				$query .= " AND DAY(marine_output.date) = :day";
688
				$query_values = array_merge($query_values,array(':day' => $day));
689
			} else {
690
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
691
				$query_values = array_merge($query_values,array(':day' => $day));
692
			}
693
		}
694
		$query .= " GROUP BY marine_output.type
695
			ORDER BY type_count DESC";
696
		$query_values = array_merge($query_values,array(':race' => $race));
697
		$sth = $this->db->prepare($query);
698
		$sth->execute($query_values);
699
		return $sth->fetchAll(PDO::FETCH_ASSOC);
700
	}
701
702
	/**
703
	* Gets race duration by captain
704
	*
705
	* @return String Duration of all race
706
	*
707
	*/
708
	public function getRaceDurationByCaptain($captain,$filters = array(),$year = '',$month = '',$day = '')
709
	{
710
		global $globalDBdriver;
711
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
712
		$filter_query = $this->getFilter($filters,true,true);
713
		$query  = "SELECT SUM(last_seen - date) AS duration 
714
		    FROM marine_output".$filter_query." (marine_output.captain_name = :captain OR marine_output.captain_id = :captain) 
715
		    AND last_seen > date";
716
		$query_values = array();
717
		if ($year != '') {
718
			if ($globalDBdriver == 'mysql') {
719
				$query .= " AND YEAR(marine_output.date) = :year";
720
				$query_values = array_merge($query_values,array(':year' => $year));
721
			} else {
722
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
723
				$query_values = array_merge($query_values,array(':year' => $year));
724
			}
725
		}
726
		if ($month != '') {
727
			if ($globalDBdriver == 'mysql') {
728
				$query .= " AND MONTH(marine_output.date) = :month";
729
				$query_values = array_merge($query_values,array(':month' => $month));
730
			} else {
731
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
732
				$query_values = array_merge($query_values,array(':month' => $month));
733
			}
734
		}
735
		if ($day != '') {
736
			if ($globalDBdriver == 'mysql') {
737
				$query .= " AND DAY(marine_output.date) = :day";
738
				$query_values = array_merge($query_values,array(':day' => $day));
739
			} else {
740
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
741
				$query_values = array_merge($query_values,array(':day' => $day));
742
			}
743
		}
744
		$query_values = array_merge($query_values,array(':captain' => $captain));
745
		$sth = $this->db->prepare($query);
746
		$sth->execute($query_values);
747
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
748
		if (is_int($result[0]['duration'])) return gmdate('H:i:s',$result[0]['duration']);
749
		else return $result[0]['duration'];
750
	}
751
752
	/**
753
	* Gets race duration by captains
754
	*
755
	* @return String Duration of all race
756
	*
757
	*/
758
	public function getRaceDurationByCaptains($limit = true,$filters = array(),$year = '',$month = '',$day = '')
759
	{
760
		global $globalDBdriver;
761
		$filter_query = $this->getFilter($filters,true,true);
762
		$query  = "SELECT SUM(last_seen - date) AS duration, captain_id, captain_name 
763
		    FROM marine_output".$filter_query." last_seen > date";
764
		$query_values = array();
765
		if ($year != '') {
766
			if ($globalDBdriver == 'mysql') {
767
				$query .= " AND YEAR(marine_output.date) = :year";
768
				$query_values = array_merge($query_values,array(':year' => $year));
769
			} else {
770
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
771
				$query_values = array_merge($query_values,array(':year' => $year));
772
			}
773
		}
774
		if ($month != '') {
775
			if ($globalDBdriver == 'mysql') {
776
				$query .= " AND MONTH(marine_output.date) = :month";
777
				$query_values = array_merge($query_values,array(':month' => $month));
778
			} else {
779
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
780
				$query_values = array_merge($query_values,array(':month' => $month));
781
			}
782
		}
783
		if ($day != '') {
784
			if ($globalDBdriver == 'mysql') {
785
				$query .= " AND DAY(marine_output.date) = :day";
786
				$query_values = array_merge($query_values,array(':day' => $day));
787
			} else {
788
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
789
				$query_values = array_merge($query_values,array(':day' => $day));
790
			}
791
		}
792
		$query .= " GROUP BY marine_output.captain_id,marine_output.captain_name ORDER BY duration DESC";
793
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
794
		$sth = $this->db->prepare($query);
795
		$sth->execute($query_values);
796
		//if (is_int($result[0]['duration'])) return gmdate('H:i:s',$result[0]['duration']);
797
		//else return $result[0]['duration'];
798
		$duration_array = array();
799
		$temp_array = array();
800
        
801
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
802
		{
803
			if ($row['duration'] != '') {
804
				$temp_array['marine_duration_days'] = $row['duration'];
805
				//$temp_array['marine_duration'] = strtotime($row['duration']);
806
				$temp_array['marine_captain_id'] = $row['captain_id'];
807
				$temp_array['marine_captain_name'] = $row['captain_name'];
808
				$duration_array[] = $temp_array;
809
			}
810
		}
811
		return $duration_array;
812
813
	}
814
815
	/**
816
	* Gets a list of all captain names and captain ids
817
	*
818
	* @return Array list of captain names and captain ids
819
	*
820
	*/
821
	public function getAllCaptainNames($filters = array())
822
	{
823
		$filter_query = $this->getFilter($filters,true,true);
824
		$query  = "SELECT DISTINCT marine_output.captain_name, marine_output.captain_id
825
			FROM marine_output".$filter_query." marine_output.captain_name <> '' 
826
			ORDER BY marine_output.captain_name ASC";
827
	
828
		$sth = $this->db->prepare($query);
829
		$sth->execute();
830
		return $sth->fetchAll(PDO::FETCH_ASSOC);
831
	} 
832
833
	/**
834
	* Gets a list of all race names and race ids
835
	*
836
	* @return Array list of race names and race ids
837
	*
838
	*/
839
	public function getAllRaceNames($filters = array())
840
	{
841
		$filter_query = $this->getFilter($filters,true,true);
842
		$query  = "SELECT DISTINCT marine_output.race_name, marine_output.race_id
843
			FROM marine_output".$filter_query." marine_output.race_name <> '' 
844
			ORDER BY marine_output.race_name ASC";
845
	
846
		$sth = $this->db->prepare($query);
847
		$sth->execute();
848
		return $sth->fetchAll(PDO::FETCH_ASSOC);
849
	} 
850
851
852
	/**
853
	* Gets all source name
854
	*
855
	* @param String type format of source
856
	* @return Array list of source name
857
	*
858
	*/
859
	public function getAllSourceName($type = '',$filters = array())
860
	{
861
		$filter_query = $this->getFilter($filters,true,true);
862
		$query_values = array();
863
		$query  = "SELECT DISTINCT marine_output.source_name 
864
				FROM marine_output".$filter_query." marine_output.source_name <> ''";
865
		if ($type != '') {
866
			$query_values = array(':type' => $type);
867
			$query .= " AND format_source = :type";
868
		}
869
		$query .= " ORDER BY marine_output.source_name ASC";
870
871
		$sth = $this->db->prepare($query);
872
		if (!empty($query_values)) $sth->execute($query_values);
873
		else $sth->execute();
874
875
		$source_array = array();
876
		$temp_array = array();
877
		
878
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
879
		{
880
			$temp_array['source_name'] = $row['source_name'];
881
			$source_array[] = $temp_array;
882
		}
883
		return $source_array;
884
	}
885
886
887
	/**
888
	* Gets a list of all idents/callsigns
889
	*
890
	* @return Array list of ident/callsign names
891
	*
892
	*/
893
	public function getAllIdents($filters = array())
894
	{
895
		$filter_query = $this->getFilter($filters,true,true);
896
		$query  = "SELECT DISTINCT marine_output.ident
897
								FROM marine_output".$filter_query." marine_output.ident <> '' 
898
								ORDER BY marine_output.date ASC LIMIT 700 OFFSET 0";
899
900
		$sth = $this->db->prepare($query);
901
		$sth->execute();
902
    
903
		$ident_array = array();
904
		$temp_array = array();
905
		
906
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
907
		{
908
			$temp_array['ident'] = $row['ident'];
909
			$ident_array[] = $temp_array;
910
		}
911
912
		return $ident_array;
913
	}
914
915
	/**
916
	* Gets all info from a mmsi
917
	*
918
	* @return Array ident
919
	*
920
	*/
921
	public function getIdentity($mmsi)
922
	{
923
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT);
924
		$query  = "SELECT * FROM marine_identity WHERE mmsi = :mmsi LIMIT 1";
925
		$sth = $this->db->prepare($query);
926
		$sth->execute(array(':mmsi' => $mmsi));
927
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
928
		if (isset($result[0])) return $result[0];
929
		else return array();
930
	}
931
932
	/**
933
	* Add identity
934
	*
935
	*/
936
	public function addIdentity($mmsi,$imo,$ident,$callsign,$type)
937
	{
938
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT);
939
		if ($mmsi != '') {
940
			$imo = filter_var($imo,FILTER_SANITIZE_NUMBER_INT);
941
			$ident = filter_var($ident,FILTER_SANITIZE_STRING);
942
			$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
943
			$type = filter_var($type,FILTER_SANITIZE_STRING);
944
			$identinfo = $this->getIdentity($mmsi);
945
			if (empty($identinfo)) {
946
				$query  = "INSERT INTO marine_identity (mmsi,imo,call_sign,ship_name,type) VALUES (:mmsi,:imo,:call_sign,:ship_name,:type)";
947
				$sth = $this->db->prepare($query);
948
				$sth->execute(array(':mmsi' => $mmsi,':imo' => $imo,':call_sign' => $callsign,':ship_name' => $ident,':type' => $type));
949
			} elseif ($ident != '' && $identinfo['ship_name'] != $ident) {
950
				$query  = "UPDATE marine_identity SET ship_name = :ship_name,type = :type WHERE mmsi = :mmsi";
951
				$sth = $this->db->prepare($query);
952
				$sth->execute(array(':mmsi' => $mmsi,':ship_name' => $ident,':type' => $type));
953
			}
954
		}
955
	}
956
957
	/*
958
	* Gets a list of all dates
959
	*
960
	* @return Array list of date names
961
	*
962
	*/
963
	public function getAllDates()
964
	{
965
		global $globalTimezone, $globalDBdriver;
966
		if ($globalTimezone != '') {
967
			date_default_timezone_set($globalTimezone);
968
			$datetime = new DateTime();
969
			$offset = $datetime->format('P');
970
		} else $offset = '+00:00';
971
972
		if ($globalDBdriver == 'mysql') {
973
			$query  = "SELECT DISTINCT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) as date
974
								FROM marine_output
975
								WHERE marine_output.date <> '' 
976
								ORDER BY marine_output.date ASC LIMIT 0,100";
977
		} else {
978
			$query  = "SELECT DISTINCT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') as date
979
								FROM marine_output
980
								WHERE marine_output.date <> '' 
981
								ORDER BY marine_output.date ASC LIMIT 0,100";
982
		}
983
		
984
		$sth = $this->db->prepare($query);
985
		$sth->execute(array(':offset' => $offset));
986
    
987
		$date_array = array();
988
		$temp_array = array();
989
		
990
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
991
		{
992
			$temp_array['date'] = $row['date'];
993
994
			$date_array[] = $temp_array;
995
		}
996
997
		return $date_array;
998
	}
999
1000
	/**
1001
	* Update ident tracker data
1002
	*
1003
	* @param String $fammarine_id the ID
1004
	* @param String $ident the marine ident
1005
	* @return String success or false
1006
	*
1007
	*/
1008
	public function updateIdentMarineData($fammarine_id = '', $ident = '',$fromsource = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $fromsource is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1009
	{
1010
		$query = 'UPDATE marine_output SET ident = :ident WHERE fammarine_id = :fammarine_id';
1011
		$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident);
1012
		try {
1013
			$sth = $this->db->prepare($query);
1014
			$sth->execute($query_values);
1015
		} catch (PDOException $e) {
1016
			return "error : ".$e->getMessage();
1017
		}
1018
		return "success";
1019
	}
1020
1021
	/**
1022
	* Update arrival marine data
1023
	*
1024
	* @param String $fammarine_id the ID
1025
	* @param String $arrival_code the marine ident
1026
	* @return String success or false
1027
	*
1028
	*/
1029
	public function updateArrivalPortNameMarineData($fammarine_id = '', $arrival_code = '',$fromsource = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $fromsource is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1030
	{
1031
		$query = 'UPDATE marine_output SET arrival_port_name = :arrival_code WHERE fammarine_id = :fammarine_id';
1032
		$query_values = array(':fammarine_id' => $fammarine_id,':arrival_code' => $arrival_code);
1033
		try {
1034
			$sth = $this->db->prepare($query);
1035
			$sth->execute($query_values);
1036
		} catch (PDOException $e) {
1037
			return "error : ".$e->getMessage();
1038
		}
1039
		return "success";
1040
	}
1041
1042
	/**
1043
	* Update Status data
1044
	*
1045
	* @param String $fammarine_id the ID
1046
	* @param String $status_id the marine status id
1047
	* @param String $status the marine status
1048
	* @return String success or false
1049
	*
1050
	*/
1051
	public function updateStatusMarineData($fammarine_id = '', $status_id = '',$status = '')
1052
	{
1053
1054
		$query = 'UPDATE marine_output SET status = :status, status_id = :status_id WHERE fammarine_id = :fammarine_id';
1055
                $query_values = array(':fammarine_id' => $fammarine_id,':status' => $status,':status_id' => $status_id);
1056
1057
		try {
1058
			$sth = $this->db->prepare($query);
1059
			$sth->execute($query_values);
1060
		} catch (PDOException $e) {
1061
			return "error : ".$e->getMessage();
1062
		}
1063
		
1064
		return "success";
1065
1066
	}
1067
	/**
1068
	* Update latest marine data
1069
	*
1070
	* @param String $fammarine_id the ID
1071
	* @param String $ident the marine ident
1072
	* @return String success or false
1073
	*
1074
	*/	
1075
	public function updateLatestMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $groundspeed = NULL, $date = '',$distance = NULL,$race_rank = NULL, $race_time = NULL, $status = '', $race_begin = '')
1076
	{
1077
		if ($latitude == '') $latitude = NULL;
1078
		if ($longitude == '') $longitude = NULL;
1079
		$groundspeed = round($groundspeed);
1080
		if ($race_begin != '') {
1081
			$query = 'UPDATE marine_output SET ident = :ident, last_latitude = :last_latitude, last_longitude = :last_longitude, last_seen = :last_seen, last_ground_speed = :last_ground_speed, distance = :distance, race_rank = :race_rank, race_time = :race_time, status = :status, date = :race_begin WHERE fammarine_id = :fammarine_id';
1082
			$query_values = array(':fammarine_id' => $fammarine_id,':last_latitude' => $latitude,':last_longitude' => $longitude, ':last_ground_speed' => $groundspeed,':last_seen' => $date,':ident' => $ident,':distance' => $distance,':race_rank' => $race_rank,':race_time' => $race_time,':status' => $status,':race_begin' => $race_begin);
1083
		} else {
1084
			$query = 'UPDATE marine_output SET ident = :ident, last_latitude = :last_latitude, last_longitude = :last_longitude, last_seen = :last_seen, last_ground_speed = :last_ground_speed, distance = :distance, race_rank = :race_rank, race_time = :race_time, status = :status WHERE fammarine_id = :fammarine_id';
1085
			$query_values = array(':fammarine_id' => $fammarine_id,':last_latitude' => $latitude,':last_longitude' => $longitude, ':last_ground_speed' => $groundspeed,':last_seen' => $date,':ident' => $ident,':distance' => $distance,':race_rank' => $race_rank,':race_time' => $race_time,':status' => $status);
1086
		}
1087
		try {
1088
			$sth = $this->db->prepare($query);
1089
			$sth->execute($query_values);
1090
		} catch (PDOException $e) {
1091
			echo "error : ".$e->getMessage();
1092
			return "error : ".$e->getMessage();
1093
		}
1094
		
1095
		return "success";
1096
1097
	}
1098
1099
	/**
1100
	* Adds a new marine data
1101
	*
1102
	* @param String $fammarine_id the ID
1103
	* @param String $ident the marine ident
1104
	* @param String $departure_airport_icao the departure airport
0 ignored issues
show
Bug introduced by
There is no parameter named $departure_airport_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1105
	* @param String $arrival_airport_icao the arrival airport
0 ignored issues
show
Bug introduced by
There is no parameter named $arrival_airport_icao. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1106
	* @param String $latitude latitude of flight
1107
	* @param String $longitude latitude of flight
1108
	* @param String $waypoints waypoints of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $waypoints. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1109
	* @param String $heading heading of flight
1110
	* @param String $groundspeed speed of flight
1111
	* @param String $date date of flight
1112
	* @param String $departure_airport_time departure time of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $departure_airport_time. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1113
	* @param String $arrival_airport_time arrival time of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $arrival_airport_time. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1114
	* @param String $squawk squawk code of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $squawk. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1115
	* @param String $route_stop route stop of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $route_stop. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1116
	* @param String $highlight highlight or not
0 ignored issues
show
Bug introduced by
There is no parameter named $highlight. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1117
	* @param String $ModeS ModesS code of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $ModeS. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1118
	* @param String $registration registration code of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $registration. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1119
	* @param String $pilot_id pilot id of flight (for virtual airlines)
0 ignored issues
show
Bug introduced by
There is no parameter named $pilot_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1120
	* @param String $pilot_name pilot name of flight (for virtual airlines)
0 ignored issues
show
Bug introduced by
There is no parameter named $pilot_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1121
	* @param String $verticalrate vertival rate of flight
0 ignored issues
show
Bug introduced by
There is no parameter named $verticalrate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1122
	* @return String success or false
1123
	*/
1124
	public function addMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $heading = '', $groundspeed = '', $date = '', $mmsi = '',$type = '',$typeid = '',$imo = '',$callsign = '',$arrival_code = '',$arrival_date = '',$status = '',$statusid = '',$format_source = '', $source_name = '', $captain_id = '',$captain_name = '',$race_id = '', $race_name = '', $distance = '',$race_rank = '', $race_time = '')
1125
	{
1126
		global $globalURL, $globalMarineImageFetch;
1127
		
1128
		//$Image = new Image($this->db);
1129
		$Common = new Common();
1130
		
1131
		date_default_timezone_set('UTC');
1132
		
1133
		//getting the registration
1134
		if ($fammarine_id != "")
1135
		{
1136
			if (!is_string($fammarine_id))
1137
			{
1138
				return false;
1139
			}
1140
		}
1141
		$fromsource = NULL;
0 ignored issues
show
Unused Code introduced by
$fromsource 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...
1142
		//getting the airline information
1143
		if ($ident != "")
1144
		{
1145
			if (!is_string($ident))
1146
			{
1147
				return false;
1148
			}
1149
		}
1150
1151
		if ($latitude != "")
1152
		{
1153
			if (!is_numeric($latitude))
1154
			{
1155
				return false;
1156
			}
1157
		}
1158
		
1159
		if ($longitude != "")
1160
		{
1161
			if (!is_numeric($longitude))
1162
			{
1163
				return false;
1164
			}
1165
		}
1166
		
1167
		if ($heading != "")
1168
		{
1169
			if (!is_numeric($heading))
1170
			{
1171
				return false;
1172
			}
1173
		}
1174
		if ($mmsi != "")
1175
		{
1176
			if (!is_numeric($mmsi))
1177
			{
1178
				return false;
1179
			}
1180
		}
1181
		
1182
		if ($groundspeed != "")
1183
		{
1184
			if (!is_numeric($groundspeed))
1185
			{
1186
				return false;
1187
			}
1188
		}
1189
1190
    
1191
		if ($date == "")
1192
		{
1193
			$date = date("Y-m-d H:i:s", time());
1194
		}
1195
1196
		$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING);
1197
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1198
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1199
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1200
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
1201
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1202
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
1203
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
1204
		$type = filter_var($type,FILTER_SANITIZE_STRING);
1205
		$status = filter_var($status,FILTER_SANITIZE_STRING);
1206
		$type_id = filter_var($typeid,FILTER_SANITIZE_NUMBER_INT);
1207
		$status_id = filter_var($statusid,FILTER_SANITIZE_NUMBER_INT);
1208
		$imo = filter_var($imo,FILTER_SANITIZE_STRING);
1209
		$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
0 ignored issues
show
Unused Code introduced by
$callsign 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...
1210
		$arrival_code = filter_var($arrival_code,FILTER_SANITIZE_STRING);
1211
		$arrival_date = filter_var($arrival_date,FILTER_SANITIZE_STRING);
1212
		$captain_id = filter_var($captain_id,FILTER_SANITIZE_STRING);
1213
		$captain_name = filter_var($captain_name,FILTER_SANITIZE_STRING);
1214
		$race_id = filter_var($race_id,FILTER_SANITIZE_STRING);
1215
		$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
1216
		$race_rank = filter_var($race_rank,FILTER_SANITIZE_NUMBER_INT);
1217
		$race_time = filter_var($race_time,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1218
		$distance = filter_var($distance,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1219
		if (isset($globalMarineImageFetch) && $globalMarineImageFetch === TRUE) {
1220
			$Image = new Image($this->db);
1221
			$image_array = $Image->getMarineImage($mmsi,$imo,$ident);
1222
			if (!isset($image_array[0]['mmsi'])) {
1223
				$Image->addMarineImage($mmsi,$imo,$ident);
1224
			}
1225
			unset($Image);
1226
		}
1227
		if ($latitude == '' && $longitude == '') {
1228
			$latitude = 0;
1229
			$longitude = 0;
1230
		}
1231
		if ($type_id == '') $type_id = NULL;
1232
		if ($status_id == '') $status_id = NULL;
1233
		if ($distance == '') $distance = NULL;
1234
		if ($race_rank == '') $race_rank = NULL;
1235
		if ($race_time == '') $race_time = NULL;
1236
		if ($heading == '' || $Common->isInteger($heading) === false) $heading = 0;
1237
		//if ($groundspeed == '' || $Common->isInteger($groundspeed) === false) $groundspeed = 0;
1238
		if ($arrival_date == '') $arrival_date = NULL;
1239
		$query  = "INSERT INTO marine_output (fammarine_id, ident, latitude, longitude, heading, ground_speed, date, format_source, source_name, mmsi, type, type_id, status,status_id,imo,arrival_port_name,arrival_port_date,captain_id,captain_name,race_id,race_name, distance, race_rank,race_time) 
1240
		    VALUES (:fammarine_id,:ident,:latitude,:longitude,:heading,:speed,:date,:format_source, :source_name,:mmsi,:type,:type_id,:status,:status_id,:imo,:arrival_port_name,:arrival_port_date,:captain_id,:captain_name,:race_id,:race_name, :distance, :race_rank,:race_time)";
1241
1242
		$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident,':latitude' => $latitude,':longitude' => $longitude,':heading' => $heading,':speed' => $groundspeed,':date' => $date,':format_source' => $format_source, ':source_name' => $source_name,':mmsi' => $mmsi,':type' => $type,':type_id' => $type_id,':status' => $status,':status_id' => $status_id,':imo' => $imo,':arrival_port_name' => $arrival_code,':arrival_port_date' => $arrival_date,':captain_id' => $captain_id,':captain_name' => $captain_name,':race_id' => $race_id,':race_name' => $race_name,':distance' => $distance,':race_rank' => $race_rank,':race_time' => $race_time);
1243
		try {
1244
			$sth = $this->db->prepare($query);
1245
			$sth->execute($query_values);
1246
			$this->db = null;
1247
		} catch (PDOException $e) {
1248
			return "error : ".$e->getMessage();
1249
		}
1250
		
1251
		return "success";
1252
1253
	}
1254
	
1255
  
1256
	/**
1257
	* Gets the aircraft ident within the last hour
1258
	*
1259
	* @return String the ident
1260
	*
1261
	*/
1262
	public function getIdentFromLastHour($ident)
1263
	{
1264
		global $globalDBdriver, $globalTimezone;
1265
		if ($globalDBdriver == 'mysql') {
1266
			$query  = "SELECT marine_output.ident FROM marine_output 
1267
								WHERE marine_output.ident = :ident 
1268
								AND marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
1269
								AND marine_output.date < UTC_TIMESTAMP()";
1270
			$query_data = array(':ident' => $ident);
1271
		} else {
1272
			$query  = "SELECT marine_output.ident FROM marine_output 
1273
								WHERE marine_output.ident = :ident 
1274
								AND marine_output.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
1275
								AND marine_output.date < now() AT TIME ZONE 'UTC'";
1276
			$query_data = array(':ident' => $ident);
1277
    		}
1278
		
1279
		$sth = $this->db->prepare($query);
1280
		$sth->execute($query_data);
1281
    		$ident_result='';
1282
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1283
		{
1284
			$ident_result = $row['ident'];
1285
		}
1286
1287
		return $ident_result;
1288
	}
1289
	
1290
	
1291
	/**
1292
	* Gets the aircraft data from the last 20 seconds
1293
	*
1294
	* @return Array the marine data
1295
	*
1296
	*/
1297
	public function getRealTimeData($q = '')
1298
	{
1299
		global $globalDBdriver;
1300
		$additional_query = '';
1301
		if ($q != "")
1302
		{
1303
			if (!is_string($q))
1304
			{
1305
				return false;
1306
			} else {
1307
				$q_array = explode(" ", $q);
1308
				foreach ($q_array as $q_item){
1309
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
1310
					$additional_query .= " AND (";
1311
					$additional_query .= "(marine_output.ident like '%".$q_item."%')";
1312
					$additional_query .= ")";
1313
				}
1314
			}
1315
		}
1316
		if ($globalDBdriver == 'mysql') {
1317
			$query  = "SELECT marine_output.* FROM marine_output 
1318
				WHERE marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 SECOND) ".$additional_query." 
1319
				AND marine_output.date < UTC_TIMESTAMP()";
1320
		} else {
1321
			$query  = "SELECT marine_output.* FROM marine_output 
1322
				WHERE marine_output.date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 SECONDS' ".$additional_query." 
1323
				AND marine_output.date::timestamp < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";
1324
		}
1325
		$marine_array = $this->getDataFromDB($query, array());
1326
1327
		return $marine_array;
1328
	}
1329
	
1330
	
1331
	
1332
1333
	/**
1334
	* Gets all number of flight over countries
1335
	*
1336
	* @return Array the airline country list
1337
	*
1338
	*/
1339
1340
	public function countAllMarineOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array())
1341
	{
1342
		global $globalDBdriver, $globalArchive;
1343
		//$filter_query = $this->getFilter($filters,true,true);
1344
		$Connection= new Connection($this->db);
1345
		if (!$Connection->tableExists('countries')) return array();
1346
		require_once('class.SpotterLive.php');
1347
		if (!isset($globalArchive) || $globalArchive !== TRUE) {
1348
			$MarineLive = new MarineLive($this->db);
1349
			$filter_query = $MarineLive->getFilter($filters,true,true);
1350
			$filter_query .= " over_country IS NOT NULL AND over_country <> ''";
1351
			if ($olderthanmonths > 0) {
1352
				if ($globalDBdriver == 'mysql') {
1353
					$filter_query .= ' AND marine_live.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1354
				} else {
1355
					$filter_query .= " AND marine_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1356
				}
1357
			}
1358
			if ($sincedate != '') {
1359
				if ($globalDBdriver == 'mysql') {
1360
					$filter_query .= " AND marine_live.date > '".$sincedate."' ";
1361
				} else {
1362
					$filter_query .= " AND marine_live.date > CAST('".$sincedate."' AS TIMESTAMP)";
1363
				}
1364
			}
1365
			$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT fammarine_id,over_country FROM marine_live".$filter_query.") l ON c.iso2 = l.over_country ";
1366
		} else {
1367
			require_once(dirname(__FILE__)."/class.MarineArchive.php");
1368
			$MarineArchive = new MarineArchive($this->db);
1369
			$filter_query = $MarineArchive->getFilter($filters,true,true);
1370
			$filter_query .= " over_country <> ''";
1371
			if ($olderthanmonths > 0) {
1372
				if ($globalDBdriver == 'mysql') {
1373
					$filter_query .= ' AND marine_archive.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1374
				} else {
1375
					$filter_query .= " AND marine_archive.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1376
				}
1377
			}
1378
			if ($sincedate != '') {
1379
				if ($globalDBdriver == 'mysql') {
1380
					$filter_query .= " AND marine_archive.date > '".$sincedate."' ";
1381
				} else {
1382
					$filter_query .= " AND marine_archive.date > CAST('".$sincedate."' AS TIMESTAMP)";
1383
				}
1384
			}
1385
			$filter_query .= " LIMIT 200 OFFSET 0";
1386
			$query = "SELECT c.name, c.iso3, c.iso2, count(c.name) as nb FROM countries c INNER JOIN (SELECT DISTINCT fammarine_id,over_country FROM marine_archive".$filter_query.") l ON c.iso2 = l.over_country ";
1387
		}
1388
		$query .= "GROUP BY c.name,c.iso3,c.iso2 ORDER BY nb DESC";
1389
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1390
1391
		$sth = $this->db->prepare($query);
1392
		$sth->execute();
1393
 
1394
		$flight_array = array();
1395
		$temp_array = array();
1396
        
1397
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1398
		{
1399
			$temp_array['marine_count'] = $row['nb'];
1400
			$temp_array['marine_country'] = $row['name'];
1401
			$temp_array['marine_country_iso3'] = $row['iso3'];
1402
			$temp_array['marine_country_iso2'] = $row['iso2'];
1403
			$flight_array[] = $temp_array;
1404
		}
1405
		return $flight_array;
1406
	}
1407
	
1408
	
1409
	
1410
	/**
1411
	* Gets all callsigns that have flown over
1412
	*
1413
	* @return Array the callsign list
1414
	*
1415
	*/
1416
	public function countAllCallsigns($limit = true, $olderthanmonths = 0, $sincedate = '',$filters = array(),$year = '', $month = '', $day = '')
1417
	{
1418
		global $globalDBdriver;
1419
		$filter_query = $this->getFilter($filters,true,true);
1420
		$query  = "SELECT DISTINCT marine_output.ident, COUNT(marine_output.ident) AS callsign_icao_count 
1421
                    FROM marine_output".$filter_query." marine_output.ident <> ''";
1422
		 if ($olderthanmonths > 0) {
1423
			if ($globalDBdriver == 'mysql') $query .= ' AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH)';
1424
			else $query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1425
		}
1426
		if ($sincedate != '') {
1427
			if ($globalDBdriver == 'mysql') $query .= " AND marine_output.date > '".$sincedate."'";
1428
			else $query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
1429
		}
1430
		$query_values = array();
1431
		if ($year != '') {
1432
			if ($globalDBdriver == 'mysql') {
1433
				$query .= " AND YEAR(marine_output.date) = :year";
1434
				$query_values = array_merge($query_values,array(':year' => $year));
1435
			} else {
1436
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1437
				$query_values = array_merge($query_values,array(':year' => $year));
1438
			}
1439
		}
1440
		if ($month != '') {
1441
			if ($globalDBdriver == 'mysql') {
1442
				$query .= " AND MONTH(marine_output.date) = :month";
1443
				$query_values = array_merge($query_values,array(':month' => $month));
1444
			} else {
1445
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1446
				$query_values = array_merge($query_values,array(':month' => $month));
1447
			}
1448
		}
1449
		if ($day != '') {
1450
			if ($globalDBdriver == 'mysql') {
1451
				$query .= " AND DAY(marine_output.date) = :day";
1452
				$query_values = array_merge($query_values,array(':day' => $day));
1453
			} else {
1454
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
1455
				$query_values = array_merge($query_values,array(':day' => $day));
1456
			}
1457
		}
1458
		$query .= " GROUP BY marine_output.ident ORDER BY callsign_icao_count DESC";
1459
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1460
      		
1461
		$sth = $this->db->prepare($query);
1462
		$sth->execute($query_values);
1463
      
1464
		$callsign_array = array();
1465
		$temp_array = array();
1466
        
1467
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1468
		{
1469
			$temp_array['callsign_icao'] = $row['ident'];
1470
			$temp_array['airline_name'] = $row['airline_name'];
1471
			$temp_array['airline_icao'] = $row['airline_icao'];
1472
			$temp_array['callsign_icao_count'] = $row['callsign_icao_count'];
1473
          
1474
			$callsign_array[] = $temp_array;
1475
		}
1476
1477
		return $callsign_array;
1478
	}
1479
1480
1481
	/**
1482
	* Counts all dates
1483
	*
1484
	* @return Array the date list
1485
	*
1486
	*/
1487
	public function countAllDates($filters = array())
1488
	{
1489
		global $globalTimezone, $globalDBdriver;
1490
		if ($globalTimezone != '') {
1491
			date_default_timezone_set($globalTimezone);
1492
			$datetime = new DateTime();
1493
			$offset = $datetime->format('P');
1494
		} else $offset = '+00:00';
1495
1496
		if ($globalDBdriver == 'mysql') {
1497
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1498
								FROM marine_output";
1499
			$query .= $this->getFilter($filters);
1500
			$query .= " GROUP BY date_name 
1501
								ORDER BY date_count DESC
1502
								LIMIT 10 OFFSET 0";
1503
		} else {
1504
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1505
								FROM marine_output";
1506
			$query .= $this->getFilter($filters);
1507
			$query .= " GROUP BY date_name 
1508
								ORDER BY date_count DESC
1509
								LIMIT 10 OFFSET 0";
1510
		}
1511
      
1512
		
1513
		$sth = $this->db->prepare($query);
1514
		$sth->execute(array(':offset' => $offset));
1515
      
1516
		$date_array = array();
1517
		$temp_array = array();
1518
        
1519
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1520
		{
1521
			$temp_array['date_name'] = $row['date_name'];
1522
			$temp_array['date_count'] = $row['date_count'];
1523
1524
			$date_array[] = $temp_array;
1525
		}
1526
1527
		return $date_array;
1528
	}
1529
	
1530
	
1531
	/**
1532
	* Counts all dates during the last 7 days
1533
	*
1534
	* @return Array the date list
1535
	*
1536
	*/
1537
	public function countAllDatesLast7Days($filters = array())
1538
	{
1539
		global $globalTimezone, $globalDBdriver;
1540
		if ($globalTimezone != '') {
1541
			date_default_timezone_set($globalTimezone);
1542
			$datetime = new DateTime();
1543
			$offset = $datetime->format('P');
1544
		} else $offset = '+00:00';
1545
		$filter_query = $this->getFilter($filters,true,true);
1546
		if ($globalDBdriver == 'mysql') {
1547
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1548
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY)";
1549
			$query .= " GROUP BY date_name 
1550
								ORDER BY marine_output.date ASC";
1551
			$query_data = array(':offset' => $offset);
1552
		} else {
1553
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1554
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '7 DAYS'";
1555
			$query .= " GROUP BY date_name 
1556
								ORDER BY date_name ASC";
1557
			$query_data = array(':offset' => $offset);
1558
    		}
1559
		
1560
		$sth = $this->db->prepare($query);
1561
		$sth->execute($query_data);
1562
      
1563
		$date_array = array();
1564
		$temp_array = array();
1565
        
1566
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1567
		{
1568
			$temp_array['date_name'] = $row['date_name'];
1569
			$temp_array['date_count'] = $row['date_count'];
1570
          
1571
			$date_array[] = $temp_array;
1572
		}
1573
1574
		return $date_array;
1575
	}
1576
1577
	/**
1578
	* Counts all dates during the last month
1579
	*
1580
	* @return Array the date list
1581
	*
1582
	*/
1583
	public function countAllDatesLastMonth($filters = array())
1584
	{
1585
		global $globalTimezone, $globalDBdriver;
1586
		if ($globalTimezone != '') {
1587
			date_default_timezone_set($globalTimezone);
1588
			$datetime = new DateTime();
1589
			$offset = $datetime->format('P');
1590
		} else $offset = '+00:00';
1591
		$filter_query = $this->getFilter($filters,true,true);
1592
		if ($globalDBdriver == 'mysql') {
1593
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1594
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MONTH)";
1595
			$query .= " GROUP BY date_name 
1596
								ORDER BY marine_output.date ASC";
1597
			$query_data = array(':offset' => $offset);
1598
		} else {
1599
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1600
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 MONTHS'";
1601
			$query .= " GROUP BY date_name 
1602
								ORDER BY date_name ASC";
1603
			$query_data = array(':offset' => $offset);
1604
    		}
1605
		
1606
		$sth = $this->db->prepare($query);
1607
		$sth->execute($query_data);
1608
      
1609
		$date_array = array();
1610
		$temp_array = array();
1611
        
1612
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1613
		{
1614
			$temp_array['date_name'] = $row['date_name'];
1615
			$temp_array['date_count'] = $row['date_count'];
1616
          
1617
			$date_array[] = $temp_array;
1618
		}
1619
1620
		return $date_array;
1621
	}
1622
1623
1624
1625
	/**
1626
	* Counts all month
1627
	*
1628
	* @return Array the month list
1629
	*
1630
	*/
1631
	public function countAllMonths($filters = array())
1632
	{
1633
		global $globalTimezone, $globalDBdriver;
1634
		if ($globalTimezone != '') {
1635
			date_default_timezone_set($globalTimezone);
1636
			$datetime = new DateTime();
1637
			$offset = $datetime->format('P');
1638
		} else $offset = '+00:00';
1639
1640
		if ($globalDBdriver == 'mysql') {
1641
			$query  = "SELECT YEAR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS year_name,MONTH(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS month_name, count(*) as date_count
1642
								FROM marine_output";
1643
			$query .= $this->getFilter($filters);
1644
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1645
		} else {
1646
			$query  = "SELECT EXTRACT(YEAR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS year_name,EXTRACT(MONTH FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS month_name, count(*) as date_count
1647
								FROM marine_output";
1648
			$query .= $this->getFilter($filters);
1649
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1650
		}
1651
      
1652
		
1653
		$sth = $this->db->prepare($query);
1654
		$sth->execute(array(':offset' => $offset));
1655
      
1656
		$date_array = array();
1657
		$temp_array = array();
1658
        
1659
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1660
		{
1661
			$temp_array['month_name'] = $row['month_name'];
1662
			$temp_array['year_name'] = $row['year_name'];
1663
			$temp_array['date_count'] = $row['date_count'];
1664
1665
			$date_array[] = $temp_array;
1666
		}
1667
1668
		return $date_array;
1669
	}
1670
1671
	
1672
	
1673
1674
	/**
1675
	* Counts all dates during the last year
1676
	*
1677
	* @return Array the date list
1678
	*
1679
	*/
1680
	public function countAllMonthsLastYear($filters)
1681
	{
1682
		global $globalTimezone, $globalDBdriver;
1683
		if ($globalTimezone != '') {
1684
			date_default_timezone_set($globalTimezone);
1685
			$datetime = new DateTime();
1686
			$offset = $datetime->format('P');
1687
		} else $offset = '+00:00';
1688
		$filter_query = $this->getFilter($filters,true,true);
1689
		if ($globalDBdriver == 'mysql') {
1690
			$query  = "SELECT MONTH(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS month_name, YEAR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS year_name, count(*) as date_count
1691
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)";
1692
			$query .= " GROUP BY year_name, month_name
1693
								ORDER BY year_name, month_name ASC";
1694
			$query_data = array(':offset' => $offset);
1695
		} else {
1696
			$query  = "SELECT EXTRACT(MONTH FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS month_name, EXTRACT(YEAR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS year_name, count(*) as date_count
1697
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'";
1698
			$query .= " GROUP BY year_name, month_name
1699
								ORDER BY year_name, month_name ASC";
1700
			$query_data = array(':offset' => $offset);
1701
    		}
1702
		
1703
		$sth = $this->db->prepare($query);
1704
		$sth->execute($query_data);
1705
      
1706
		$date_array = array();
1707
		$temp_array = array();
1708
        
1709
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1710
		{
1711
			$temp_array['year_name'] = $row['year_name'];
1712
			$temp_array['month_name'] = $row['month_name'];
1713
			$temp_array['date_count'] = $row['date_count'];
1714
          
1715
			$date_array[] = $temp_array;
1716
		}
1717
1718
		return $date_array;
1719
	}
1720
	
1721
	
1722
	
1723
	/**
1724
	* Counts all hours
1725
	*
1726
	* @return Array the hour list
1727
	*
1728
	*/
1729
	public function countAllHours($orderby,$filters = array())
1730
	{
1731
		global $globalTimezone, $globalDBdriver;
1732
		if ($globalTimezone != '') {
1733
			date_default_timezone_set($globalTimezone);
1734
			$datetime = new DateTime();
1735
			$offset = $datetime->format('P');
1736
		} else $offset = '+00:00';
1737
1738
		$orderby_sql = '';
1739
		if ($orderby == "hour")
1740
		{
1741
			$orderby_sql = "ORDER BY hour_name ASC";
1742
		}
1743
		if ($orderby == "count")
1744
		{
1745
			$orderby_sql = "ORDER BY hour_count DESC";
1746
		}
1747
		
1748
		if ($globalDBdriver == 'mysql') {
1749
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1750
								FROM marine_output";
1751
			$query .= $this->getFilter($filters);
1752
			$query .= " GROUP BY hour_name 
1753
								".$orderby_sql;
1754
1755
/*		$query  = "SELECT HOUR(marine_output.date) AS hour_name, count(*) as hour_count
1756
								FROM marine_output 
1757
								GROUP BY hour_name 
1758
								".$orderby_sql."
1759
								LIMIT 10 OFFSET 00";
1760
  */    
1761
		$query_data = array(':offset' => $offset);
1762
		} else {
1763
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1764
								FROM marine_output";
1765
			$query .= $this->getFilter($filters);
1766
			$query .= " GROUP BY hour_name 
1767
								".$orderby_sql;
1768
			$query_data = array(':offset' => $offset);
1769
		}
1770
		
1771
		$sth = $this->db->prepare($query);
1772
		$sth->execute($query_data);
1773
      
1774
		$hour_array = array();
1775
		$temp_array = array();
1776
        
1777
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1778
		{
1779
			$temp_array['hour_name'] = $row['hour_name'];
1780
			$temp_array['hour_count'] = $row['hour_count'];
1781
          
1782
			$hour_array[] = $temp_array;
1783
		}
1784
1785
		return $hour_array;
1786
	}
1787
	
1788
	
1789
	
1790
	/**
1791
	* Counts all hours by date
1792
	*
1793
	* @return Array the hour list
1794
	*
1795
	*/
1796
	public function countAllHoursByDate($date, $filters = array())
1797
	{
1798
		global $globalTimezone, $globalDBdriver;
1799
		$filter_query = $this->getFilter($filters,true,true);
1800
		$date = filter_var($date,FILTER_SANITIZE_STRING);
1801
		if ($globalTimezone != '') {
1802
			date_default_timezone_set($globalTimezone);
1803
			$datetime = new DateTime($date);
1804
			$offset = $datetime->format('P');
1805
		} else $offset = '+00:00';
1806
1807
		if ($globalDBdriver == 'mysql') {
1808
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1809
								FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date
1810
								GROUP BY hour_name 
1811
								ORDER BY hour_name ASC";
1812
		} else {
1813
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1814
								FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset, 'YYYY-mm-dd') = :date
1815
								GROUP BY hour_name 
1816
								ORDER BY hour_name ASC";
1817
		}
1818
		
1819
		$sth = $this->db->prepare($query);
1820
		$sth->execute(array(':date' => $date, ':offset' => $offset));
1821
      
1822
		$hour_array = array();
1823
		$temp_array = array();
1824
        
1825
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1826
		{
1827
			$temp_array['hour_name'] = $row['hour_name'];
1828
			$temp_array['hour_count'] = $row['hour_count'];
1829
          
1830
			$hour_array[] = $temp_array;
1831
		}
1832
1833
		return $hour_array;
1834
	}
1835
	
1836
	
1837
	
1838
	/**
1839
	* Counts all hours by a ident/callsign
1840
	*
1841
	* @return Array the hour list
1842
	*
1843
	*/
1844
	public function countAllHoursByIdent($ident, $filters = array())
1845
	{
1846
		global $globalTimezone, $globalDBdriver;
1847
		$filter_query = $this->getFilter($filters,true,true);
1848
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1849
		if ($globalTimezone != '') {
1850
			date_default_timezone_set($globalTimezone);
1851
			$datetime = new DateTime();
1852
			$offset = $datetime->format('P');
1853
		} else $offset = '+00:00';
1854
1855
		if ($globalDBdriver == 'mysql') {
1856
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1857
								FROM marine_output".$filter_query." marine_output.ident = :ident 
1858
								GROUP BY hour_name 
1859
								ORDER BY hour_name ASC";
1860
		} else {
1861
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1862
								FROM marine_output".$filter_query." marine_output.ident = :ident 
1863
								GROUP BY hour_name 
1864
								ORDER BY hour_name ASC";
1865
		}
1866
      
1867
		
1868
		$sth = $this->db->prepare($query);
1869
		$sth->execute(array(':ident' => $ident,':offset' => $offset));
1870
      
1871
		$hour_array = array();
1872
		$temp_array = array();
1873
        
1874
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1875
		{
1876
			$temp_array['hour_name'] = $row['hour_name'];
1877
			$temp_array['hour_count'] = $row['hour_count'];
1878
          
1879
			$hour_array[] = $temp_array;
1880
		}
1881
1882
		return $hour_array;
1883
	}
1884
	
1885
	/**
1886
	* Gets all aircraft registrations that have flown over
1887
	*
1888
	* @return Array the aircraft list
1889
	*
1890
	*/
1891
	public function countAllCaptainsByRaces($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array())
1892
	{
1893
		global $globalDBdriver;
1894
		$filter_query = $this->getFilter($filters,true,true);
1895
		$query  = "SELECT DISTINCT marine_output.race_id, marine_output.race_name, COUNT(marine_output.captain_id) AS captain_count 
1896
			FROM marine_output".$filter_query." race_id IS NOT NULL";
1897
		if ($olderthanmonths > 0) {
1898
			if ($globalDBdriver == 'mysql') {
1899
				$query .= ' AND marine_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$olderthanmonths.' MONTH)';
1900
			} else {
1901
				$query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1902
			}
1903
		}
1904
		if ($sincedate != '') {
1905
			if ($globalDBdriver == 'mysql') {
1906
				$query .= " AND marine_output.date > '".$sincedate."'";
1907
			} else {
1908
				$query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
1909
			}
1910
		}
1911
		$query .= " GROUP BY marine_output.race_id,marine_output.race_name ORDER BY captain_count DESC";
1912
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1913
		$sth = $this->db->prepare($query);
1914
		$sth->execute();
1915
		$marine_array = array();
1916
		$temp_array = array();
1917
        
1918
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1919
		{
1920
			$temp_array['marine_race_id'] = $row['race_id'];
1921
			$temp_array['marine_race_name'] = $row['race_name'];
1922
			$temp_array['marine_captain_count'] = $row['captain_count'];
1923
			$marine_array[] = $temp_array;
1924
		}
1925
		return $marine_array;
1926
	}
1927
	
1928
	/**
1929
	* Counts all vessels
1930
	*
1931
	* @return Integer the number of vessels
1932
	*
1933
	*/
1934
	public function countOverallMarine($filters = array(),$year = '',$month = '')
1935
	{
1936
		global $globalDBdriver;
1937
		//$queryi  = "SELECT COUNT(marine_output.marine_id) AS flight_count FROM marine_output";
1938
		$queryi  = "SELECT COUNT(DISTINCT marine_output.mmsi) AS flight_count FROM marine_output";
1939
		$query_values = array();
1940
		$query = '';
1941
		if ($year != '') {
1942
			if ($globalDBdriver == 'mysql') {
1943
				$query .= " AND YEAR(marine_output.date) = :year";
1944
				$query_values = array_merge($query_values,array(':year' => $year));
1945
			} else {
1946
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1947
				$query_values = array_merge($query_values,array(':year' => $year));
1948
			}
1949
		}
1950
		if ($month != '') {
1951
			if ($globalDBdriver == 'mysql') {
1952
				$query .= " AND MONTH(marine_output.date) = :month";
1953
				$query_values = array_merge($query_values,array(':month' => $month));
1954
			} else {
1955
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1956
				$query_values = array_merge($query_values,array(':month' => $month));
1957
			}
1958
		}
1959
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1960
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1961
		
1962
		$sth = $this->db->prepare($queryi);
1963
		$sth->execute($query_values);
1964
		return $sth->fetchColumn();
1965
	}
1966
	
1967
	/**
1968
	* Counts all vessel type
1969
	*
1970
	* @return Integer the number of vessels
1971
	*
1972
	*/
1973
	public function countOverallMarineTypes($filters = array(),$year = '',$month = '')
1974
	{
1975
		global $globalDBdriver;
1976
		$queryi  = "SELECT COUNT(DISTINCT marine_output.type) AS marine_count FROM marine_output";
1977
		$query_values = array();
1978
		$query = '';
1979
		if ($year != '') {
1980
			if ($globalDBdriver == 'mysql') {
1981
				$query .= " AND YEAR(marine_output.date) = :year";
1982
				$query_values = array_merge($query_values,array(':year' => $year));
1983
			} else {
1984
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1985
				$query_values = array_merge($query_values,array(':year' => $year));
1986
			}
1987
		}
1988
		if ($month != '') {
1989
			if ($globalDBdriver == 'mysql') {
1990
				$query .= " AND MONTH(marine_output.date) = :month";
1991
				$query_values = array_merge($query_values,array(':month' => $month));
1992
			} else {
1993
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1994
				$query_values = array_merge($query_values,array(':month' => $month));
1995
			}
1996
		}
1997
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1998
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1999
		
2000
		$sth = $this->db->prepare($queryi);
2001
		$sth->execute($query_values);
2002
		return $sth->fetchColumn();
2003
	}
2004
	
2005
	/**
2006
	* Gets a number of all race
2007
	*
2008
	* @return Integer number of races
2009
	*
2010
	*/
2011
	public function countOverallMarineRaces($filters = array(),$year = '',$month = '')
2012
	{
2013
		global $globalDBdriver;
2014
		$queryi  = "SELECT COUNT(DISTINCT marine_output.race_id) AS marine_count FROM marine_output";
2015
		$query_values = array();
2016
		$query = '';
2017
		if ($year != '') {
2018
			if ($globalDBdriver == 'mysql') {
2019
				$query .= " AND YEAR(marine_output.date) = :year";
2020
				$query_values = array_merge($query_values,array(':year' => $year));
2021
			} else {
2022
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
2023
				$query_values = array_merge($query_values,array(':year' => $year));
2024
			}
2025
		}
2026
		if ($month != '') {
2027
			if ($globalDBdriver == 'mysql') {
2028
				$query .= " AND MONTH(marine_output.date) = :month";
2029
				$query_values = array_merge($query_values,array(':month' => $month));
2030
			} else {
2031
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
2032
				$query_values = array_merge($query_values,array(':month' => $month));
2033
			}
2034
		}
2035
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
2036
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
2037
		
2038
		$sth = $this->db->prepare($queryi);
2039
		$sth->execute($query_values);
2040
		return $sth->fetchColumn();
2041
	}
2042
  
2043
	/**
2044
	* Gets a number of all captain
2045
	*
2046
	* @return Integer number of captain
2047
	*
2048
	*/
2049
	public function countOverallMarineCaptains($filters = array(),$year = '',$month = '')
2050
	{
2051
		global $globalDBdriver;
2052
		$queryi  = "SELECT COUNT(DISTINCT marine_output.captain_id) AS marine_count FROM marine_output";
2053
		$query_values = array();
2054
		$query = '';
2055
		if ($year != '') {
2056
			if ($globalDBdriver == 'mysql') {
2057
				$query .= " AND YEAR(marine_output.date) = :year";
2058
				$query_values = array_merge($query_values,array(':year' => $year));
2059
			} else {
2060
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
2061
				$query_values = array_merge($query_values,array(':year' => $year));
2062
			}
2063
		}
2064
		if ($month != '') {
2065
			if ($globalDBdriver == 'mysql') {
2066
				$query .= " AND MONTH(marine_output.date) = :month";
2067
				$query_values = array_merge($query_values,array(':month' => $month));
2068
			} else {
2069
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
2070
				$query_values = array_merge($query_values,array(':month' => $month));
2071
			}
2072
		}
2073
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
2074
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
2075
		
2076
		$sth = $this->db->prepare($queryi);
2077
		$sth->execute($query_values);
2078
		return $sth->fetchColumn();
2079
	}
2080
  
2081
	/**
2082
	* Counts all hours of today
2083
	*
2084
	* @return Array the hour list
2085
	*
2086
	*/
2087
	public function countAllHoursFromToday($filters = array())
2088
	{
2089
		global $globalTimezone, $globalDBdriver;
2090
		$filter_query = $this->getFilter($filters,true,true);
2091
		if ($globalTimezone != '') {
2092
			date_default_timezone_set($globalTimezone);
2093
			$datetime = new DateTime();
2094
			$offset = $datetime->format('P');
2095
		} else $offset = '+00:00';
2096
2097
		if ($globalDBdriver == 'mysql') {
2098
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
2099
								FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = CURDATE()
2100
								GROUP BY hour_name 
2101
								ORDER BY hour_name ASC";
2102
		} else {
2103
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
2104
								FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') = CAST(NOW() AS date)
2105
								GROUP BY hour_name 
2106
								ORDER BY hour_name ASC";
2107
		}
2108
		
2109
		$sth = $this->db->prepare($query);
2110
		$sth->execute(array(':offset' => $offset));
2111
      
2112
		$hour_array = array();
2113
		$temp_array = array();
2114
        
2115
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
2116
		{
2117
			$temp_array['hour_name'] = $row['hour_name'];
2118
			$temp_array['hour_count'] = $row['hour_count'];
2119
			$hour_array[] = $temp_array;
2120
		}
2121
2122
		return $hour_array;
2123
	}
2124
    
2125
    
2126
     /**
2127
	* Gets the Barrie Spotter ID based on the FlightAware ID
2128
	*
2129
	* @return Integer the Barrie Spotter ID
2130
q	*
2131
	*/
2132
	public function getMarineIDBasedOnFamMarineID($fammarine_id)
2133
	{
2134
		$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING);
2135
2136
		$query  = "SELECT marine_output.marine_id
2137
				FROM marine_output 
2138
				WHERE marine_output.fammarine_id = '".$fammarine_id."'";
2139
        
2140
		
2141
		$sth = $this->db->prepare($query);
2142
		$sth->execute();
2143
2144
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
2145
		{
2146
			return $row['marine_id'];
2147
		}
2148
	}
2149
  
2150
 
2151
	/**
2152
	* Parses a date string
2153
	*
2154
	* @param String $dateString the date string
2155
	* @param String $timezone the timezone of a user
2156
	* @return Array the time information
2157
	*
2158
	*/
2159
	public function parseDateString($dateString, $timezone = '')
2160
	{
2161
		$time_array = array();
2162
	
2163
		if ($timezone != "")
2164
		{
2165
			date_default_timezone_set($timezone);
2166
		}
2167
		
2168
		$current_date = date("Y-m-d H:i:s");
2169
		$date = date("Y-m-d H:i:s",strtotime($dateString." UTC"));
2170
		
2171
		$diff = abs(strtotime($current_date) - strtotime($date));
2172
2173
		$time_array['years'] = floor($diff / (365*60*60*24)); 
2174
		$years = $time_array['years'];
2175
		
2176
		$time_array['months'] = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
2177
		$months = $time_array['months'];
2178
		
2179
		$time_array['days'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
2180
		$days = $time_array['days'];
2181
		$time_array['hours'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
2182
		$hours = $time_array['hours'];
2183
		$time_array['minutes'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
2184
		$minutes = $time_array['minutes'];
2185
		$time_array['seconds'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));  
2186
		
2187
		return $time_array;
2188
	}
2189
	
2190
	/**
2191
	* Parses the direction degrees to working
2192
	*
2193
	* @param Float $direction the direction in degrees
2194
	* @return Array the direction information
2195
	*
2196
	*/
2197
	public function parseDirection($direction = 0)
2198
	{
2199
		if ($direction == '') $direction = 0;
2200
		$direction_array = array();
2201
		$temp_array = array();
2202
2203
		if ($direction == 360 || ($direction >= 0 && $direction < 22.5))
2204
		{
2205
			$temp_array['direction_degree'] = $direction;
2206
			$temp_array['direction_shortname'] = "N";
2207
			$temp_array['direction_fullname'] = "North";
2208
		} elseif ($direction >= 22.5 && $direction < 45){
2209
			$temp_array['direction_degree'] = $direction;
2210
			$temp_array['direction_shortname'] = "NNE";
2211
			$temp_array['direction_fullname'] = "North-Northeast";
2212
		} elseif ($direction >= 45 && $direction < 67.5){
2213
			$temp_array['direction_degree'] = $direction;
2214
			$temp_array['direction_shortname'] = "NE";
2215
			$temp_array['direction_fullname'] = "Northeast";
2216
		} elseif ($direction >= 67.5 && $direction < 90){
2217
			$temp_array['direction_degree'] = $direction;
2218
			$temp_array['direction_shortname'] = "ENE";
2219
			$temp_array['direction_fullname'] = "East-Northeast";
2220
		} elseif ($direction >= 90 && $direction < 112.5){
2221
			$temp_array['direction_degree'] = $direction;
2222
			$temp_array['direction_shortname'] = "E";
2223
			$temp_array['direction_fullname'] = "East";
2224
		} elseif ($direction >= 112.5 && $direction < 135){
2225
			$temp_array['direction_degree'] = $direction;
2226
			$temp_array['direction_shortname'] = "ESE";
2227
			$temp_array['direction_fullname'] = "East-Southeast";
2228
		} elseif ($direction >= 135 && $direction < 157.5){
2229
			$temp_array['direction_degree'] = $direction;
2230
			$temp_array['direction_shortname'] = "SE";
2231
			$temp_array['direction_fullname'] = "Southeast";
2232
		} elseif ($direction >= 157.5 && $direction < 180){
2233
			$temp_array['direction_degree'] = $direction;
2234
			$temp_array['direction_shortname'] = "SSE";
2235
			$temp_array['direction_fullname'] = "South-Southeast";
2236
		} elseif ($direction >= 180 && $direction < 202.5){
2237
			$temp_array['direction_degree'] = $direction;
2238
			$temp_array['direction_shortname'] = "S";
2239
			$temp_array['direction_fullname'] = "South";
2240
		} elseif ($direction >= 202.5 && $direction < 225){
2241
			$temp_array['direction_degree'] = $direction;
2242
			$temp_array['direction_shortname'] = "SSW";
2243
			$temp_array['direction_fullname'] = "South-Southwest";
2244
		} elseif ($direction >= 225 && $direction < 247.5){
2245
			$temp_array['direction_degree'] = $direction;
2246
			$temp_array['direction_shortname'] = "SW";
2247
			$temp_array['direction_fullname'] = "Southwest";
2248
		} elseif ($direction >= 247.5 && $direction < 270){
2249
			$temp_array['direction_degree'] = $direction;
2250
			$temp_array['direction_shortname'] = "WSW";
2251
			$temp_array['direction_fullname'] = "West-Southwest";
2252
		} elseif ($direction >= 270 && $direction < 292.5){
2253
			$temp_array['direction_degree'] = $direction;
2254
			$temp_array['direction_shortname'] = "W";
2255
			$temp_array['direction_fullname'] = "West";
2256
		} elseif ($direction >= 292.5 && $direction < 315){
2257
			$temp_array['direction_degree'] = $direction;
2258
			$temp_array['direction_shortname'] = "WNW";
2259
			$temp_array['direction_fullname'] = "West-Northwest";
2260
		} elseif ($direction >= 315 && $direction < 337.5){
2261
			$temp_array['direction_degree'] = $direction;
2262
			$temp_array['direction_shortname'] = "NW";
2263
			$temp_array['direction_fullname'] = "Northwest";
2264
		} elseif ($direction >= 337.5 && $direction < 360){
2265
			$temp_array['direction_degree'] = $direction;
2266
			$temp_array['direction_shortname'] = "NNW";
2267
			$temp_array['direction_fullname'] = "North-Northwest";
2268
		}
2269
		$direction_array[] = $temp_array;
2270
		return $direction_array;
2271
	}
2272
	
2273
	
2274
	/**
2275
	* Gets Country from latitude/longitude
2276
	*
2277
	* @param Float $latitude latitute of the flight
2278
	* @param Float $longitude longitute of the flight
2279
	* @return String the countrie
2280
	*/
2281
	public function getCountryFromLatitudeLongitude($latitude,$longitude)
2282
	{
2283
		global $globalDBdriver, $globalDebug;
2284
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
2285
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
2286
	
2287
		$Connection = new Connection($this->db);
2288
		if (!$Connection->tableExists('countries')) return '';
2289
	
2290
		try {
2291
			/*
2292
			if ($globalDBdriver == 'mysql') {
2293
				//$query  = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(:latitude :longitude)'), ogc_geom) LIMIT 1";
2294
				$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(".$longitude.' '.$latitude.")'), ogc_geom) LIMIT 1";
2295
			}
2296
			*/
2297
			// This query seems to work both for MariaDB and PostgreSQL
2298
			$query = "SELECT name,iso2,iso3 FROM countries WHERE ST_Within(ST_GeomFromText('POINT(".$longitude." ".$latitude.")',4326), ogc_geom) LIMIT 1";
2299
		
2300
			$sth = $this->db->prepare($query);
2301
			//$sth->execute(array(':latitude' => $latitude,':longitude' => $longitude));
2302
			$sth->execute();
2303
    
2304
			$row = $sth->fetch(PDO::FETCH_ASSOC);
2305
			$sth->closeCursor();
2306
			if (count($row) > 0) {
2307
				return $row;
2308
			} else return '';
2309
		} catch (PDOException $e) {
2310
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
2311
			return '';
2312
		}
2313
	
2314
	}
2315
2316
	/**
2317
	* Gets Country from iso2
2318
	*
2319
	* @param String $iso2 ISO2 country code
2320
	* @return String the countrie
2321
	*/
2322
	public function getCountryFromISO2($iso2)
2323
	{
2324
		global $globalDBdriver, $globalDebug;
2325
		$iso2 = filter_var($iso2,FILTER_SANITIZE_STRING);
2326
	
2327
		$Connection = new Connection($this->db);
2328
		if (!$Connection->tableExists('countries')) return '';
2329
	
2330
		try {
2331
			$query = "SELECT name,iso2,iso3 FROM countries WHERE iso2 = :iso2 LIMIT 1";
2332
		
2333
			$sth = $this->db->prepare($query);
2334
			$sth->execute(array(':iso2' => $iso2));
2335
    
2336
			$row = $sth->fetch(PDO::FETCH_ASSOC);
2337
			$sth->closeCursor();
2338
			if (count($row) > 0) {
2339
				return $row;
2340
			} else return '';
2341
		} catch (PDOException $e) {
2342
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
2343
			return '';
2344
		}
2345
	
2346
	}
2347
2348
	
2349
	/**
2350
	* Gets the short url from bit.ly
2351
	*
2352
	* @param String $url the full url
2353
	* @return String the bit.ly url
2354
	*
2355
	*/
2356
	public function getBitlyURL($url)
2357
	{
2358
		global $globalBitlyAccessToken;
2359
		
2360
		if ($globalBitlyAccessToken == '') return $url;
2361
        
2362
		$google_url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$globalBitlyAccessToken.'&longUrl='.$url;
2363
		
2364
		$ch = curl_init();
2365
		curl_setopt($ch, CURLOPT_HEADER, 0);
2366
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
2367
		curl_setopt($ch, CURLOPT_URL, $google_url);
2368
		$bitly_data = curl_exec($ch);
2369
		curl_close($ch);
2370
		
2371
		$bitly_data = json_decode($bitly_data);
2372
		$bitly_url = '';
2373
		if ($bitly_data->status_txt = "OK"){
2374
			$bitly_url = $bitly_data->data->url;
2375
		}
2376
2377
		return $bitly_url;
2378
	}
2379
2380
	
2381
	/**
2382
	* Gets all vessels types that have flown over
2383
	*
2384
	* @return Array the vessel type list
2385
	*
2386
	*/
2387
	public function countAllMarineTypes($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array(),$year = '',$month = '',$day = '')
2388
	{
2389
		global $globalDBdriver;
2390
		$filter_query = $this->getFilter($filters,true,true);
2391
		$query  = "SELECT marine_output.type AS marine_type, COUNT(marine_output.type) AS marine_type_count, marine_output.type_id AS marine_type_id 
2392
		    FROM marine_output ".$filter_query." marine_output.type <> '' AND marine_output.type_id IS NOT NULL";
2393
		if ($olderthanmonths > 0) {
2394
			if ($globalDBdriver == 'mysql') {
2395
				$query .= ' AND marine_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$olderthanmonths.' MONTH)';
2396
			} else {
2397
				$query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
2398
			}
2399
		}
2400
		if ($sincedate != '') {
2401
			if ($globalDBdriver == 'mysql') {
2402
				$query .= " AND marine_output.date > '".$sincedate."'";
2403
			} else {
2404
				$query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
2405
			}
2406
		}
2407
		$query_values = array();
2408
		if ($year != '') {
2409
			if ($globalDBdriver == 'mysql') {
2410
				$query .= " AND YEAR(marine_output.date) = :year";
2411
				$query_values = array_merge($query_values,array(':year' => $year));
2412
			} else {
2413
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
2414
				$query_values = array_merge($query_values,array(':year' => $year));
2415
			}
2416
		}
2417
		if ($month != '') {
2418
			if ($globalDBdriver == 'mysql') {
2419
				$query .= " AND MONTH(marine_output.date) = :month";
2420
				$query_values = array_merge($query_values,array(':month' => $month));
2421
			} else {
2422
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
2423
				$query_values = array_merge($query_values,array(':month' => $month));
2424
			}
2425
		}
2426
		if ($day != '') {
2427
			if ($globalDBdriver == 'mysql') {
2428
				$query .= " AND DAY(marine_output.date) = :day";
2429
				$query_values = array_merge($query_values,array(':day' => $day));
2430
			} else {
2431
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
2432
				$query_values = array_merge($query_values,array(':day' => $day));
2433
			}
2434
		}
2435
		$query .= " GROUP BY marine_output.type, marine_output.type_id ORDER BY marine_type_count DESC";
2436
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
2437
		$sth = $this->db->prepare($query);
2438
		$sth->execute($query_values);
2439
		$marine_array = array();
2440
		$temp_array = array();
2441
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
2442
		{
2443
			$temp_array['marine_type'] = html_entity_decode($row['marine_type'],ENT_QUOTES);
2444
			$temp_array['marine_type_id'] = $row['marine_type_id'];
2445
			$temp_array['marine_type_count'] = $row['marine_type_count'];
2446
			$marine_array[] = $temp_array;
2447
		}
2448
		return $marine_array;
2449
	}
2450
2451
	/**
2452
	* Gets all the tracker information
2453
	*
2454
	* @return Array the tracker information
2455
	*
2456
	*/
2457
	public function searchMarineData($q = '', $callsign = '',$mmsi = '', $imo = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '',$captain_id = '',$captain_name = '',$race_id = '',$race_name = '',$filters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $includegeodata is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2458
	{
2459
		global $globalTimezone, $globalDBdriver;
2460
		date_default_timezone_set('UTC');
2461
		$query_values = array();
2462
		$additional_query = '';
2463
		$filter_query = $this->getFilter($filters,true,true);
2464
		if ($q != "")
2465
		{
2466
			if (!is_string($q))
2467
			{
2468
				return false;
2469
			} else {
2470
				$q_array = explode(" ", $q);
2471
				foreach ($q_array as $q_item){
2472
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
2473
					$additional_query .= " AND (";
2474
					if (is_int($q_item)) $additional_query .= "(marine_output.marine_id = '".$q_item."') OR ";
2475
					if (is_int($q_item)) $additional_query .= "(marine_output.mmsi = '".$q_item."') OR ";
2476
					if (is_int($q_item)) $additional_query .= "(marine_output.imo = '".$q_item."') OR ";
2477
					if (is_int($q_item)) $additional_query .= "(marine_output.captain_id = '".$q_item."') OR ";
2478
					if (is_int($q_item)) $additional_query .= "(marine_output.race_id = '".$q_item."') OR ";
2479
					if (!is_int($q_item)) $additional_query .= "(marine_output.captain_name = '".$q_item."') OR ";
2480
					if (!is_int($q_item)) $additional_query .= "(marine_output.race_name = '".$q_item."') OR ";
2481
					$additional_query .= "(marine_output.ident like '%".$q_item."%')";
2482
					$additional_query .= ")";
2483
				}
2484
			}
2485
		}
2486
		if ($callsign != "")
2487
		{
2488
			$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
2489
			if (!is_string($callsign))
2490
			{
2491
				return false;
2492
			} else {
2493
				$additional_query .= " AND marine_output.ident = :callsign";
2494
				$query_values = array_merge($query_values,array(':callsign' => $callsign));
2495
			}
2496
		}
2497
		if ($mmsi != "")
2498
		{
2499
			$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
2500
			if (!is_numeric($mmsi))
2501
			{
2502
				return false;
2503
			} else {
2504
				$additional_query .= " AND marine_output.mmsi = :mmsi";
2505
				$query_values = array_merge($query_values,array(':mmsi' => $mmsi));
2506
			}
2507
		}
2508
		if ($imo != "")
2509
		{
2510
			$imo = filter_var($imo,FILTER_SANITIZE_STRING);
2511
			if (!is_numeric($imo))
2512
			{
2513
				return false;
2514
			} else {
2515
				$additional_query .= " AND marine_output.imo = :imo";
2516
				$query_values = array_merge($query_values,array(':imo' => $imo));
2517
			}
2518
		}
2519
		if ($captain_id != "")
2520
		{
2521
			$captain_id = filter_var($captain_id,FILTER_SANITIZE_STRING);
2522
			if (!is_numeric($captain_id))
2523
			{
2524
				return false;
2525
			} else {
2526
				$additional_query .= " AND marine_output.captain_id = :captain_id";
2527
				$query_values = array_merge($query_values,array(':captain_id' => $captain_id));
2528
			}
2529
		}
2530
		if ($race_id != "")
2531
		{
2532
			$race_id = filter_var($race_id,FILTER_SANITIZE_STRING);
2533
			if (!is_numeric($race_id))
2534
			{
2535
				return false;
2536
			} else {
2537
				$additional_query .= " AND marine_output.race_id = :race_id";
2538
				$query_values = array_merge($query_values,array(':race_id' => $race_id));
2539
			}
2540
		}
2541
		if ($captain_name != "")
2542
		{
2543
			$captain_id = filter_var($captain_name,FILTER_SANITIZE_STRING);
0 ignored issues
show
Unused Code introduced by
$captain_id 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...
2544
			if (!is_string($captain_name))
2545
			{
2546
				return false;
2547
			} else {
2548
				$additional_query .= " AND marine_output.captain_name = :captain_name";
2549
				$query_values = array_merge($query_values,array(':captain_name' => $captain_name));
2550
			}
2551
		}
2552
		if ($race_name != "")
2553
		{
2554
			$race_id = filter_var($race_name,FILTER_SANITIZE_STRING);
0 ignored issues
show
Unused Code introduced by
$race_id 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...
2555
			if (!is_numeric($race_name))
2556
			{
2557
				return false;
2558
			} else {
2559
				$additional_query .= " AND marine_output.race_name = :race_name";
2560
				$query_values = array_merge($query_values,array(':race_name' => $race_name));
2561
			}
2562
		}
2563
		if ($date_posted != "")
2564
		{
2565
			$date_array = explode(",", $date_posted);
2566
			$date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING);
2567
			$date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING);
2568
			if ($globalTimezone != '') {
2569
				date_default_timezone_set($globalTimezone);
2570
				$datetime = new DateTime();
2571
				$offset = $datetime->format('P');
2572
			} else $offset = '+00:00';
2573
			if ($date_array[1] != "")
2574
			{
2575
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2576
				$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1]));
2577
				if ($globalDBdriver == 'mysql') {
2578
					$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) <= '".$date_array[1]."' ";
2579
				} else {
2580
					$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) <= '".$date_array[1]."' ";
2581
				}
2582
			} else {
2583
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2584
				if ($globalDBdriver == 'mysql') {
2585
					$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' ";
2586
				} else {
2587
					$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' ";
2588
				}
2589
			}
2590
		}
2591
		if ($limit != "")
2592
		{
2593
			$limit_array = explode(",", $limit);
2594
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
2595
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
2596
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
2597
			{
2598
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
2599
			} else $limit_query = "";
2600
		} else $limit_query = "";
2601
		if ($sort != "")
2602
		{
2603
			$search_orderby_array = $this->getOrderBy();
2604
			$orderby_query = $search_orderby_array[$sort]['sql'];
2605
		} else {
2606
			if ($origLat != "" && $origLon != "" && $dist != "") {
2607
				$orderby_query = " ORDER BY distance ASC";
2608
			} else {
2609
				$orderby_query = " ORDER BY marine_output.race_rank,marine_output.date DESC";
2610
			}
2611
		}
2612
		if ($origLat != "" && $origLon != "" && $dist != "") {
2613
			$dist = number_format($dist*0.621371,2,'.',''); // convert km to mile
2614
			if ($globalDBdriver == 'mysql') {
2615
				$query="SELECT marine_output.*, 1.60935*3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - marine_archive.latitude)*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(marine_archive.latitude*pi()/180)*POWER(SIN(($origLon-marine_archive.longitude)*pi()/180/2),2))) as distance 
2616
				    FROM marine_archive,marine_output".$filter_query." marine_output.fammarine_id = marine_archive.fammarine_id AND marine_output.ident <> '' ".$additional_query."AND marine_archive.longitude between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon+$dist/cos(radians($origLat)*69)) and marine_archive.latitude between ($origLat-($dist/69)) and ($origLat+($dist/69)) 
2617
				    AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - marine_archive.latitude)*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(marine_archive.latitude*pi()/180)*POWER(SIN(($origLon-marine_archive.longitude)*pi()/180/2),2)))) < $dist".$orderby_query;
2618
			} else {
2619
				$query="SELECT marine_output.*, 1.60935 * 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - CAST(marine_archive.latitude as double precision))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(CAST(marine_archive.latitude as double precision)*pi()/180)*POWER(SIN(($origLon-CAST(marine_archive.longitude as double precision))*pi()/180/2),2))) as distance 
2620
				    FROM marine_archive,marine_output".$filter_query." marine_output.fammarine_id = marine_archive.fammarine_id AND marine_output.ident <> '' ".$additional_query."AND CAST(marine_archive.longitude as double precision) between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon+$dist/cos(radians($origLat))*69) and CAST(marine_archive.latitude as double precision) between ($origLat-($dist/69)) and ($origLat+($dist/69)) 
2621
				    AND (3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - CAST(marine_archive.latitude as double precision))*pi()/180/2),2)+COS( $origLat *pi()/180)*COS(CAST(marine_archive.latitude as double precision)*pi()/180)*POWER(SIN(($origLon-CAST(marine_archive.longitude as double precision))*pi()/180/2),2)))) < $dist".$filter_query.$orderby_query;
2622
			}
2623
		} else {
2624
			$query  = "SELECT marine_output.* FROM marine_output".$filter_query." marine_output.ident <> '' 
2625
			    ".$additional_query."
2626
			    ".$orderby_query;
2627
		}
2628
		$marine_array = $this->getDataFromDB($query, $query_values,$limit_query);
2629
		return $marine_array;
2630
	}
2631
2632
	/**
2633
	* Check marine by id
2634
	*
2635
	* @return String the ident
2636
	*
2637
	*/
2638
	public function checkId($id)
2639
	{
2640
		global $globalDBdriver, $globalTimezone;
2641
		$query  = 'SELECT marine_output.ident, marine_output.fammarine_id FROM marine_output WHERE marine_output.fammarine_id = :id';
2642
		$query_data = array(':id' => $id);
2643
		$sth = $this->db->prepare($query);
2644
		$sth->execute($query_data);
2645
		$ident_result='';
2646
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
2647
		{
2648
			$ident_result = $row['fammarine_id'];
2649
		}
2650
		return $ident_result;
2651
	}
2652
2653
	/**
2654
	* Gets all info from a race
2655
	*
2656
	* @return Array race
2657
	*
2658
	*/
2659
	public function getRaceByName($race_name)
2660
	{
2661
		$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
2662
		$query  = "SELECT * FROM marine_race WHERE race_name = :race_name LIMIT 1";
2663
		$sth = $this->db->prepare($query);
2664
		$sth->execute(array(':race_name' => $race_name));
2665
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
2666
		if (isset($result[0])) return $result[0];
2667
		else return array();
2668
	}
2669
2670
	/**
2671
	* Gets all info from a race
2672
	*
2673
	* @return Array race
2674
	*
2675
	*/
2676
	public function getRace($race_id)
2677
	{
2678
		$race_id = filter_var($race_id,FILTER_SANITIZE_NUMBER_INT);
2679
		$query  = "SELECT * FROM marine_race WHERE race_id = :race_id LIMIT 1";
2680
		$sth = $this->db->prepare($query);
2681
		$sth->execute(array(':race_id' => $race_id));
2682
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
2683
		if (isset($result[0])) return $result[0];
2684
		else return array();
2685
	}
2686
2687
	/**
2688
	* Add race
2689
	*
2690
	*/
2691
	public function addRace($race_id,$race_name,$race_creator,$race_desc,$race_startdate,$race_markers)
2692
	{
2693
		$race_id = filter_var($race_id,FILTER_SANITIZE_NUMBER_INT);
2694
		if ($race_id != '') {
2695
			$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
2696
			$race_creator = filter_var($race_creator,FILTER_SANITIZE_STRING);
2697
			$race_desc = filter_var($race_desc,FILTER_SANITIZE_STRING);
2698
			$race_startdate = filter_var($race_startdate,FILTER_SANITIZE_STRING);
2699
			//$race_markers = filter_var($race_markers,FILTER_SANITIZE_STRING);
2700
			$allrace = $this->getRace($race_id);
2701
			if (empty($allrace)) {
2702
				$query  = "INSERT INTO marine_race (race_id,race_name,race_creator,race_desc,race_startdate,race_markers) VALUES (:race_id,:race_name,:race_creator,:race_desc,:race_startdate,:race_markers)";
2703
				$sth = $this->db->prepare($query);
2704
				$sth->execute(array(':race_id' => $race_id,':race_name' => $race_name,':race_creator' => $race_creator,':race_desc' => $race_desc,':race_startdate' => $race_startdate,':race_markers' => $race_markers));
2705
			} elseif ($race_id != '') {
2706
				$query  = "UPDATE marine_race SET race_name = :race_name,race_desc = :race_desc,race_startdate = :race_startdate,race_markers = :race_markers WHERE race_id = :race_id";
2707
				$sth = $this->db->prepare($query);
2708
				$sth->execute(array(':race_id' => $race_id,':race_name' => $race_name,':race_desc' => $race_desc,':race_startdate' => $race_startdate,':race_markers' => $race_markers));
2709
			}
2710
		}
2711
	}
2712
2713
2714
2715
	public function getOrderBy()
2716
	{
2717
		$orderby = array("type_asc" => array("key" => "type_asc", "value" => "Type - ASC", "sql" => "ORDER BY marine_output.type ASC"), "type_desc" => array("key" => "type_desc", "value" => "Type - DESC", "sql" => "ORDER BY marine_output.type DESC"),"manufacturer_asc" => array("key" => "manufacturer_asc", "value" => "Aircraft Manufacturer - ASC", "sql" => "ORDER BY marine_output.aircraft_manufacturer ASC"), "manufacturer_desc" => array("key" => "manufacturer_desc", "value" => "Aircraft Manufacturer - DESC", "sql" => "ORDER BY marine_output.aircraft_manufacturer DESC"),"airline_name_asc" => array("key" => "airline_name_asc", "value" => "Airline Name - ASC", "sql" => "ORDER BY marine_output.airline_name ASC"), "airline_name_desc" => array("key" => "airline_name_desc", "value" => "Airline Name - DESC", "sql" => "ORDER BY marine_output.airline_name DESC"), "ident_asc" => array("key" => "ident_asc", "value" => "Ident - ASC", "sql" => "ORDER BY marine_output.ident ASC"), "ident_desc" => array("key" => "ident_desc", "value" => "Ident - DESC", "sql" => "ORDER BY marine_output.ident DESC"), "airport_departure_asc" => array("key" => "airport_departure_asc", "value" => "Departure port - ASC", "sql" => "ORDER BY marine_output.departure_port_city ASC"), "airport_departure_desc" => array("key" => "airport_departure_desc", "value" => "Departure Airport - DESC", "sql" => "ORDER BY marine_output.departure_airport_city DESC"), "airport_arrival_asc" => array("key" => "airport_arrival_asc", "value" => "Arrival Airport - ASC", "sql" => "ORDER BY marine_output.arrival_airport_city ASC"), "airport_arrival_desc" => array("key" => "airport_arrival_desc", "value" => "Arrival Airport - DESC", "sql" => "ORDER BY marine_output.arrival_airport_city DESC"), "date_asc" => array("key" => "date_asc", "value" => "Date - ASC", "sql" => "ORDER BY marine_output.date ASC"), "date_desc" => array("key" => "date_desc", "value" => "Date - DESC", "sql" => "ORDER BY marine_output.date DESC"),"distance_asc" => array("key" => "distance_asc","value" => "Distance - ASC","sql" => "ORDER BY distance ASC"),"distance_desc" => array("key" => "distance_desc","value" => "Distance - DESC","sql" => "ORDER BY distance DESC"));
2718
		
2719
		return $orderby;
2720
		
2721
	}
2722
    
2723
}
2724
?>