Completed
Push — master ( edc2c3...411a33 )
by Yannick
57:10 queued 24:31
created

Marine::updateLatestMarineData()   B

Complexity

Conditions 5
Paths 24

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 24
nop 11
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
require_once(dirname(__FILE__).'/class.Image.php');
3
$global_marine_query = "SELECT marine_output.* FROM marine_output";
4
5
class Marine{
6
	public $db;
7
	
8
	public function __construct($dbc = null) {
9
		$Connection = new Connection($dbc);
10
		$this->db = $Connection->db();
11
		if ($this->db === null) die('Error: No DB connection. (Marine)');
12
	}
13
14
	/**
15
	* Get SQL query part for filter used
16
	* @param Array $filter the filter
17
	* @return Array the SQL part
18
	*/
19
	
20
	public function getFilter($filter = array(),$where = false,$and = false) {
21
		global $globalFilter, $globalStatsFilters, $globalFilterName, $globalDBdriver;
22
		$filters = array();
23
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
24
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
25
				$filters = $globalStatsFilters[$globalFilterName];
26
			} else {
27
				$filter = array_merge($filter,$globalStatsFilters[$globalFilterName]);
28
			}
29
		}
30
		if (isset($filter[0]['source'])) {
31
			$filters = array_merge($filters,$filter);
32
		}
33
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
34
		$filter_query_join = '';
35
		$filter_query_where = '';
36
		foreach($filters as $flt) {
37
			if (isset($flt['idents']) && !empty($flt['idents'])) {
38
				if (isset($flt['source'])) {
39
					$filter_query_join .= " INNER JOIN (SELECT fammarine_id FROM marine_output WHERE marine_output.ident IN ('".implode("','",$flt['idents'])."') AND spotter_output.format_source IN ('".implode("','",$flt['source'])."')) spfi ON spfi.fammarine_id = marine_output.fammarine_id";
40
				} else {
41
					$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";
42
				}
43
			}
44
		}
45
		if (isset($filter['source']) && !empty($filter['source'])) {
46
			$filter_query_where .= " AND format_source IN ('".implode("','",$filter['source'])."')";
47
		}
48
		if (isset($filter['ident']) && !empty($filter['ident'])) {
49
			$filter_query_where .= " AND ident = '".$filter['ident']."'";
50
		}
51
		if (isset($filter['id']) && !empty($filter['id'])) {
52
			$filter_query_where .= " AND fammarine_id = '".$filter['id']."'";
53
		}
54
		if (isset($filter['mmsi']) && !empty($filter['mmsi'])) {
55
			$filter_query_where .= " AND mmsi = '".$filter['mmsi']."'";
56
		}
57
		if (isset($filter['race']) && !empty($filter['race'])) {
58
			$filter_query_where .= " AND race_id = '".$filter['race']."'";
59
		}
60
		if (isset($filter['year']) && $filter['year'] != '') {
61
			if ($globalDBdriver == 'mysql') {
62
				$filter_query_where .= " AND YEAR(marine_output.date) = '".$filter['year']."'";
63
			} else {
64
				$filter_query_where .= " AND EXTRACT(YEAR FROM marine_output.date) = '".$filter['year']."'";
65
			}
66
		}
67
		if (isset($filter['month']) && $filter['month'] != '') {
68
			if ($globalDBdriver == 'mysql') {
69
				$filter_query_where .= " AND MONTH(marine_output.date) = '".$filter['month']."'";
70
			} else {
71
				$filter_query_where .= " AND EXTRACT(MONTH FROM marine_output.date) = '".$filter['month']."'";
72
			}
73
		}
74
		if (isset($filter['day']) && $filter['day'] != '') {
75
			if ($globalDBdriver == 'mysql') {
76
				$filter_query_where .= " AND DAY(marine_output.date) = '".$filter['day']."'";
77
			} else {
78
				$filter_query_where .= " AND EXTRACT(DAY FROM marine_output.date) = '".$filter['day']."'";
79
			}
80
		}
81
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
82
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
83
		if ($filter_query_where != '') {
84
			$filter_query_where = preg_replace('/^ AND/',' WHERE',$filter_query_where);
85
		}
86
		$filter_query = $filter_query_join.$filter_query_where;
87
		return $filter_query;
88
	}
89
90
	/**
91
	* Executes the SQL statements to get the spotter information
92
	*
93
	* @param String $query the SQL query
94
	* @param Array $params parameter of the query
95
	* @param String $limitQuery the limit query
96
	* @return Array the spotter information
97
	*
98
	*/
99
	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...
100
	{
101
		date_default_timezone_set('UTC');
102
		if (!is_string($query))
103
		{
104
			return false;
105
		}
106
		
107
		if ($limitQuery != "")
108
		{
109
			if (!is_string($limitQuery))
110
			{
111
				return false;
112
			}
113
		}
114
115
		try {
116
			$sth = $this->db->prepare($query.$limitQuery);
117
			$sth->execute($params);
118
		} catch (PDOException $e) {
119
			printf("Invalid query : %s\nWhole query: %s\n",$e->getMessage(), $query.$limitQuery);
120
			exit();
121
		}
122
		
123
		$num_rows = 0;
124
		$spotter_array = array();
125
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
126
		{
127
			$num_rows++;
128
			$temp_array = array();
129
			if (isset($row['marine_live_id'])) {
130
				$temp_array['marine_id'] = $this->getMarineIDBasedOnFamMarineID($row['fammarine_id']);
131
			/*
132
			} elseif (isset($row['spotter_archive_id'])) {
133
				$temp_array['spotter_id'] = $row['spotter_archive_id'];
134
			} elseif (isset($row['spotter_archive_output_id'])) {
135
				$temp_array['spotter_id'] = $row['spotter_archive_output_id'];
136
			*/} 
137
			elseif (isset($row['marineid'])) {
138
				$temp_array['marine_id'] = $row['marineid'];
139
			} else {
140
				$temp_array['marine_id'] = '';
141
			}
142
			if (isset($row['fammarine_id'])) $temp_array['fammarine_id'] = $row['fammarine_id'];
143
			if (isset($row['mmsi'])) $temp_array['mmsi'] = $row['mmsi'];
144
			if (isset($row['type'])) $temp_array['type'] = $row['type'];
145
			if (isset($row['type_id'])) $temp_array['type_id'] = $row['type_id'];
146
			if (isset($row['status'])) $temp_array['status'] = $row['status'];
147
			if (isset($row['status_id'])) $temp_array['status_id'] = $row['status_id'];
148
			if (isset($row['captain_id'])) $temp_array['captain_id'] = $row['captain_id'];
149
			if (isset($row['captain_name'])) $temp_array['captain_name'] = $row['captain_name'];
150
			if (isset($row['race_id'])) $temp_array['race_id'] = $row['race_id'];
151
			if (isset($row['race_name'])) $temp_array['race_name'] = $row['race_name'];
152
			if (isset($row['race_time']) && isset($row['status']) && $row['status'] != 'Racing' && $row['race_time'] > 0) $temp_array['race_time'] = $row['race_time'];
153
			if (isset($row['race_rank'])) $temp_array['race_rank'] = $row['race_rank'];
154
			if (isset($row['ident'])) $temp_array['ident'] = $row['ident'];
155
			if (isset($row['arrival_port_name'])) $temp_array['arrival_port_name'] = $row['arrival_port_name'];
156
			if (isset($row['latitude'])) $temp_array['latitude'] = $row['latitude'];
157
			if (isset($row['longitude'])) $temp_array['longitude'] = $row['longitude'];
158
			if (isset($row['distance']) && $row['distance'] != '') $temp_array['distance'] = $row['distance'];
159
			if (isset($row['format_source'])) $temp_array['format_source'] = $row['format_source'];
160
			if (isset($row['heading'])) {
161
				$temp_array['heading'] = $row['heading'];
162
				$heading_direction = $this->parseDirection($row['heading']);
163
				if (isset($heading_direction[0]['direction_fullname'])) $temp_array['heading_name'] = $heading_direction[0]['direction_fullname'];
164
			}
165
			if (isset($row['ground_speed'])) $temp_array['ground_speed'] = $row['ground_speed'];
166
167
			if(isset($temp_array['mmsi']) && $temp_array['mmsi'] != "")
168
			{
169
				$Image = new Image($this->db);
170
				if (isset($temp_array['ident']) && $temp_array['ident'] != '') $image_array = $Image->getMarineImage($temp_array['mmsi'],'',$temp_array['ident']);
171
				else $image_array = $Image->getMarineImage($temp_array['mmsi']);
172
				unset($Image);
173
				if (count($image_array) > 0) {
174
					$temp_array['image'] = $image_array[0]['image'];
175
					$temp_array['image_thumbnail'] = $image_array[0]['image_thumbnail'];
176
					$temp_array['image_source'] = $image_array[0]['image_source'];
177
					$temp_array['image_source_website'] = $image_array[0]['image_source_website'];
178
					$temp_array['image_copyright'] = $image_array[0]['image_copyright'];
179
				}
180
			}
181
			
182
			if (isset($row['date'])) {
183
				$dateArray = $this->parseDateString($row['date']);
184
				if ($dateArray['seconds'] < 10)
185
				{
186
					$temp_array['date'] = "a few seconds ago";
187
				} elseif ($dateArray['seconds'] >= 5 && $dateArray['seconds'] < 30)
188
				{
189
					$temp_array['date'] = "half a minute ago";
190
				} elseif ($dateArray['seconds'] >= 30 && $dateArray['seconds'] < 60)
191
				{
192
					$temp_array['date'] = "about a minute ago";
193
				} elseif ($dateArray['minutes'] < 5)
194
				{
195
					$temp_array['date'] = "a few minutes ago";
196
				} elseif ($dateArray['minutes'] >= 5 && $dateArray['minutes'] < 60)
197
				{
198
					$temp_array['date'] = "about ".$dateArray['minutes']." minutes ago";
199
				} elseif ($dateArray['hours'] < 2)
200
				{
201
					$temp_array['date'] = "about an hour ago";
202
				} elseif ($dateArray['hours'] >= 2 && $dateArray['hours'] < 24)
203
				{
204
					$temp_array['date'] = "about ".$dateArray['hours']." hours ago";
205
				} else {
206
					$temp_array['date'] = date("M j Y, g:i a",strtotime($row['date']." UTC"));
207
				}
208
				$temp_array['date_minutes_past'] = $dateArray['minutes'];
209
				$temp_array['date_iso_8601'] = date("c",strtotime($row['date']." UTC"));
210
				$temp_array['date_rfc_2822'] = date("r",strtotime($row['date']." UTC"));
211
				$temp_array['date_unix'] = strtotime($row['date']." UTC");
212
				if (isset($row['last_seen']) && $row['last_seen'] != '') {
213
					if (strtotime($row['last_seen']) > strtotime($row['date'])) {
214
						$temp_array['duration'] = strtotime($row['last_seen']) - strtotime($row['date']);
215
						$temp_array['last_seen_date_iso_8601'] = date("c",strtotime($row['last_seen']." UTC"));
216
						$temp_array['last_seen_date_rfc_2822'] = date("r",strtotime($row['last_seen']." UTC"));
217
						$temp_array['last_seen_date_unix'] = strtotime($row['last_seen']." UTC");
218
					}
219
				}
220
			}
221
			
222
			$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...
223
			if (isset($row['source_name']) && $row['source_name'] != '') $temp_array['source_name'] = $row['source_name'];
224
			if (isset($row['over_country']) && $row['over_country'] != '') $temp_array['over_country'] = $row['over_country'];
225
			if (isset($row['distance']) && $row['distance'] != '') $temp_array['distance'] = $row['distance'];
226
			$temp_array['query_number_rows'] = $num_rows;
227
			$spotter_array[] = $temp_array;
228
		}
229
		if ($num_rows == 0) return array();
230
		$spotter_array[0]['query_number_rows'] = $num_rows;
231
		return $spotter_array;
232
	}	
233
	
234
	
235
	/**
236
	* Gets all the spotter information based on the latest data entry
237
	*
238
	* @return Array the spotter information
239
	*
240
	*/
241
	public function getLatestMarineData($limit = '', $sort = '', $filter = array())
242
	{
243
		global $global_marine_query;
244
		date_default_timezone_set('UTC');
245
		$filter_query = $this->getFilter($filter);
246
		if ($limit != "")
247
		{
248
			$limit_array = explode(",", $limit);
249
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
250
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
251
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
252
			{
253
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
254
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
255
			} else $limit_query = "";
256
		} else $limit_query = "";
257
		if ($sort != "")
258
		{
259
			$search_orderby_array = $this->getOrderBy();
260
			$orderby_query = $search_orderby_array[$sort]['sql'];
261
		} else {
262
			$orderby_query = " ORDER BY marine_output.date DESC";
263
		}
264
		$query  = $global_marine_query.$filter_query." ".$orderby_query;
265
		$spotter_array = $this->getDataFromDB($query, array(),$limit_query,true);
266
		return $spotter_array;
267
	}
268
    
269
	/*
270
	* Gets all the spotter information based on the spotter id
271
	*
272
	* @return Array the spotter information
273
	*
274
	*/
275
	public function getMarineDataByID($id = '')
276
	{
277
		global $global_marine_query;
278
		
279
		date_default_timezone_set('UTC');
280
		if ($id == '') return array();
281
		$additional_query = "marine_output.fammarine_id = :id";
282
		$query_values = array(':id' => $id);
283
		$query  = $global_marine_query." WHERE ".$additional_query." ";
284
		$spotter_array = $this->getDataFromDB($query,$query_values);
285
		return $spotter_array;
286
	}
287
288
	/**
289
	* Gets all the spotter information based on the callsign
290
	*
291
	* @return Array the spotter information
292
	*
293
	*/
294
	public function getMarineDataByIdent($ident = '', $limit = '', $sort = '', $filter = array())
295
	{
296
		global $global_marine_query;
297
		
298
		date_default_timezone_set('UTC');
299
		
300
		$query_values = array();
301
		$limit_query = '';
302
		$additional_query = '';
303
		$filter_query = $this->getFilter($filter,true,true);
304
		if ($ident != "")
305
		{
306
			if (!is_string($ident))
307
			{
308
				return false;
309
			} else {
310
				$additional_query = " AND (marine_output.ident = :ident)";
311
				$query_values = array(':ident' => $ident);
312
			}
313
		}
314
		
315
		if ($limit != "")
316
		{
317
			$limit_array = explode(",", $limit);
318
			
319
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
320
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
321
			
322
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
323
			{
324
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
325
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
326
			}
327
		}
328
329
		if ($sort != "")
330
		{
331
			$search_orderby_array = $this->getOrderBy();
332
			$orderby_query = $search_orderby_array[$sort]['sql'];
333
		} else {
334
			$orderby_query = " ORDER BY marine_output.date DESC";
335
		}
336
337
		$query = $global_marine_query.$filter_query." marine_output.ident <> '' ".$additional_query." ".$orderby_query;
338
		//echo $query."\n";
339
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
340
341
		return $spotter_array;
342
	}
343
344
	/**
345
	* Gets all the marine information based on the type
346
	*
347
	* @return Array the marine information
348
	*
349
	*/
350
	public function getMarineDataByType($type = '', $limit = '', $sort = '', $filter = array())
351
	{
352
		global $global_marine_query;
353
		
354
		date_default_timezone_set('UTC');
355
		
356
		$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...
357
		$limit_query = '';
358
		$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...
359
		$filter_query = $this->getFilter($filter,true,true);
360
		if (!is_string($type))
361
		{
362
			return false;
363
		} else {
364
			$additional_query = " AND marine_output.type_id = :type";
365
			$query_values = array(':type' => $type);
366
		}
367
		
368
		if ($limit != "")
369
		{
370
			$limit_array = explode(",", $limit);
371
			
372
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
373
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
374
			
375
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
376
			{
377
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
378
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
379
			}
380
		}
381
382
		if ($sort != "")
383
		{
384
			$search_orderby_array = $this->getOrderBy();
385
			$orderby_query = $search_orderby_array[$sort]['sql'];
386
		} else {
387
			$orderby_query = " ORDER BY marine_output.date DESC";
388
		}
389
390
		$query = $global_marine_query.$filter_query." marine_output.type <> '' ".$additional_query." ".$orderby_query;
391
		//echo $query."\n";
392
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
393
394
		return $spotter_array;
395
	}
396
	
397
	public function getMarineDataByDate($date = '', $limit = '', $sort = '',$filter = array())
398
	{
399
		global $global_marine_query, $globalTimezone, $globalDBdriver;
400
		
401
		$query_values = array();
402
		$limit_query = '';
403
		$additional_query = '';
404
405
		$filter_query = $this->getFilter($filter,true,true);
406
		
407
		if ($date != "")
408
		{
409
			if ($globalTimezone != '') {
410
				date_default_timezone_set($globalTimezone);
411
				$datetime = new DateTime($date);
412
				$offset = $datetime->format('P');
413
			} else {
414
				date_default_timezone_set('UTC');
415
				$datetime = new DateTime($date);
416
				$offset = '+00:00';
417
			}
418
			if ($globalDBdriver == 'mysql') {
419
				$additional_query = " AND DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date ";
420
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':offset' => $offset);
421
			} elseif ($globalDBdriver == 'pgsql') {
422
				$additional_query = " AND to_char(marine_output.date AT TIME ZONE :timezone,'YYYY-mm-dd') = :date ";
423
				$query_values = array(':date' => $datetime->format('Y-m-d'), ':timezone' => $globalTimezone);
424
			}
425
		}
426
		
427
		if ($limit != "")
428
		{
429
			$limit_array = explode(",", $limit);
430
			
431
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
432
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
433
			
434
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
435
			{
436
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
437
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
438
			}
439
		}
440
441
		if ($sort != "")
442
		{
443
			$search_orderby_array = $this->getOrderBy();
444
			$orderby_query = $search_orderby_array[$sort]['sql'];
445
		} else {
446
			$orderby_query = " ORDER BY marine_output.date DESC";
447
		}
448
449
		$query = $global_marine_query.$filter_query." marine_output.ident <> '' ".$additional_query.$orderby_query;
450
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
451
		return $spotter_array;
452
	}
453
454
	/**
455
	* Gets all the marine information based on the captain
456
	*
457
	* @return Array the marine information
458
	*
459
	*/
460
	public function getMarineDataByCaptain($captain = '', $limit = '', $sort = '', $filter = array())
461
	{
462
		global $global_marine_query;
463
		date_default_timezone_set('UTC');
464
		$query_values = array();
465
		$limit_query = '';
466
		$additional_query = '';
467
		$filter_query = $this->getFilter($filter,true,true);
468
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
469
		if ($captain != "")
470
		{
471
			$additional_query = " AND (marine_output.captain_name = :captain OR marine_output.captain_id = :captain)";
472
			$query_values = array(':captain' => $captain);
473
		}
474
		if ($limit != "")
475
		{
476
			$limit_array = explode(",", $limit);
477
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
478
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
479
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
480
			{
481
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
482
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
483
			}
484
		}
485
		if ($sort != "")
486
		{
487
			$search_orderby_array = $this->getOrderBy();
488
			$orderby_query = $search_orderby_array[$sort]['sql'];
489
		} else {
490
			$orderby_query = " ORDER BY marine_output.date DESC";
491
		}
492
		$query = $global_marine_query.$filter_query." marine_output.captain_name <> '' ".$additional_query." ".$orderby_query;
493
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
494
		return $spotter_array;
495
	}
496
497
	/**
498
	* Gets all the marine information based on the race
499
	*
500
	* @return Array the marine information
501
	*
502
	*/
503
	public function getMarineDataByRace($race = '', $limit = '', $sort = '', $filter = array())
504
	{
505
		global $global_marine_query;
506
		date_default_timezone_set('UTC');
507
		$query_values = array();
508
		$limit_query = '';
509
		$additional_query = '';
510
		$filter_query = $this->getFilter($filter,true,true);
511
		$race = filter_var($race,FILTER_SANITIZE_STRING);
512
		if ($race != "")
513
		{
514
			$additional_query = " AND (marine_output.race_name = :race OR marine_output.race_id = :race)";
515
			$query_values = array(':race' => $race);
516
		}
517
		if ($limit != "")
518
		{
519
			$limit_array = explode(",", $limit);
520
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
521
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
522
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
523
			{
524
				//$limit_query = " LIMIT ".$limit_array[0].",".$limit_array[1];
525
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
526
			}
527
		}
528
		if ($sort != "")
529
		{
530
			$search_orderby_array = $this->getOrderBy();
531
			$orderby_query = $search_orderby_array[$sort]['sql'];
532
		} else {
533
			$orderby_query = " ORDER BY marine_output.race_rank ASC, marine_output.distance";
534
		}
535
		$query = $global_marine_query.$filter_query." marine_output.race_name <> '' ".$additional_query." ".$orderby_query;
536
		$spotter_array = $this->getDataFromDB($query, $query_values, $limit_query);
537
		return $spotter_array;
538
	}
539
540
	/**
541
	* Count races by captain
542
	*
543
	* @return String Duration of all races
544
	*
545
	*/
546
	public function countRacesByCaptain($captain,$filters = array())
547
	{
548
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
549
		$filter_query = $this->getFilter($filters,true,true);
550
		$query  = "SELECT COUNT(*) AS nb 
551
			FROM marine_output".$filter_query." (marine_output.captain_name = :captain OR marine_output.captain_id = :captain)";
552
		$query_values = array();
553
		$query_values = array_merge($query_values,array(':captain' => $captain));
554
		$sth = $this->db->prepare($query);
555
		$sth->execute($query_values);
556
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
557
		return $result[0]['nb'];
558
	}
559
560
	/**
561
	* Count captains by race
562
	*
563
	* @return String Duration of all races
564
	*
565
	*/
566
	public function countCaptainsByRace($race,$filters = array())
567
	{
568
		$race = filter_var($race,FILTER_SANITIZE_STRING);
569
		$filter_query = $this->getFilter($filters,true,true);
570
		$query  = "SELECT COUNT(*) AS nb 
571
			FROM marine_output".$filter_query." (marine_output.race_name = :race OR marine_output.race_id = :race)";
572
		$query_values = array();
573
		$query_values = array_merge($query_values,array(':race' => $race));
574
		$sth = $this->db->prepare($query);
575
		$sth->execute($query_values);
576
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
577
		return $result[0]['nb'];
578
	}
579
580
	/**
581
	* Gets all boat types that have been used by a captain
582
	*
583
	* @return Array the boat list
584
	*
585
	*/
586
	public function countAllBoatTypesByCaptain($captain,$filters = array(),$year = '',$month = '',$day = '')
587
	{
588
		global $globalDBdriver;
589
		$filter_query = $this->getFilter($filters,true,true);
590
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
591
		$query  = "SELECT DISTINCT marine_output.type, COUNT(marine_output.type) AS type_count
592
			FROM marine_output".$filter_query." (marine_output.captain_id = :captain OR marine_output.captain_name = :captain)";
593
		$query_values = array();
594
		if ($year != '') {
595
			if ($globalDBdriver == 'mysql') {
596
				$query .= " AND YEAR(marine_output.date) = :year";
597
				$query_values = array_merge($query_values,array(':year' => $year));
598
			} else {
599
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
600
				$query_values = array_merge($query_values,array(':year' => $year));
601
			}
602
		}
603
		if ($month != '') {
604
			if ($globalDBdriver == 'mysql') {
605
				$query .= " AND MONTH(marine_output.date) = :month";
606
				$query_values = array_merge($query_values,array(':month' => $month));
607
			} else {
608
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
609
				$query_values = array_merge($query_values,array(':month' => $month));
610
			}
611
		}
612
		if ($day != '') {
613
			if ($globalDBdriver == 'mysql') {
614
				$query .= " AND DAY(marine_output.date) = :day";
615
				$query_values = array_merge($query_values,array(':day' => $day));
616
			} else {
617
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
618
				$query_values = array_merge($query_values,array(':day' => $day));
619
			}
620
		}
621
		$query .= " GROUP BY marine_output.type
622
			ORDER BY type_count DESC";
623
		$query_values = array_merge($query_values,array(':captain' => $captain));
624
		$sth = $this->db->prepare($query);
625
		$sth->execute($query_values);
626
		return $sth->fetchAll(PDO::FETCH_ASSOC);
627
	}
628
629
	/**
630
	* Gets all boat types that have been used on a race
631
	*
632
	* @return Array the boat list
633
	*
634
	*/
635
	public function countAllBoatTypesByRace($race,$filters = array(),$year = '',$month = '',$day = '')
636
	{
637
		global $globalDBdriver;
638
		$filter_query = $this->getFilter($filters,true,true);
639
		$race = filter_var($race,FILTER_SANITIZE_STRING);
640
		$query  = "SELECT DISTINCT marine_output.type, COUNT(marine_output.type) AS type_count
641
			FROM marine_output".$filter_query." (marine_output.race_id = :race OR marine_output.race_name = :race)";
642
		$query_values = array();
643
		if ($year != '') {
644
			if ($globalDBdriver == 'mysql') {
645
				$query .= " AND YEAR(marine_output.date) = :year";
646
				$query_values = array_merge($query_values,array(':year' => $year));
647
			} else {
648
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
649
				$query_values = array_merge($query_values,array(':year' => $year));
650
			}
651
		}
652
		if ($month != '') {
653
			if ($globalDBdriver == 'mysql') {
654
				$query .= " AND MONTH(marine_output.date) = :month";
655
				$query_values = array_merge($query_values,array(':month' => $month));
656
			} else {
657
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
658
				$query_values = array_merge($query_values,array(':month' => $month));
659
			}
660
		}
661
		if ($day != '') {
662
			if ($globalDBdriver == 'mysql') {
663
				$query .= " AND DAY(marine_output.date) = :day";
664
				$query_values = array_merge($query_values,array(':day' => $day));
665
			} else {
666
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
667
				$query_values = array_merge($query_values,array(':day' => $day));
668
			}
669
		}
670
		$query .= " GROUP BY marine_output.type
671
			ORDER BY type_count DESC";
672
		$query_values = array_merge($query_values,array(':race' => $race));
673
		$sth = $this->db->prepare($query);
674
		$sth->execute($query_values);
675
		return $sth->fetchAll(PDO::FETCH_ASSOC);
676
	}
677
678
	/**
679
	* Gets race duration by captain
680
	*
681
	* @return String Duration of all race
682
	*
683
	*/
684
	public function getRaceDurationByCaptain($captain,$filters = array(),$year = '',$month = '',$day = '')
685
	{
686
		global $globalDBdriver;
687
		$captain = filter_var($captain,FILTER_SANITIZE_STRING);
688
		$filter_query = $this->getFilter($filters,true,true);
689
		$query  = "SELECT SUM(last_seen - date) AS duration 
690
		    FROM marine_output".$filter_query." (marine_output.captain_name = :captain OR marine_output.captain_id = :captain) 
691
		    AND last_seen > date";
692
		$query_values = array();
693
		if ($year != '') {
694
			if ($globalDBdriver == 'mysql') {
695
				$query .= " AND YEAR(marine_output.date) = :year";
696
				$query_values = array_merge($query_values,array(':year' => $year));
697
			} else {
698
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
699
				$query_values = array_merge($query_values,array(':year' => $year));
700
			}
701
		}
702
		if ($month != '') {
703
			if ($globalDBdriver == 'mysql') {
704
				$query .= " AND MONTH(marine_output.date) = :month";
705
				$query_values = array_merge($query_values,array(':month' => $month));
706
			} else {
707
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
708
				$query_values = array_merge($query_values,array(':month' => $month));
709
			}
710
		}
711
		if ($day != '') {
712
			if ($globalDBdriver == 'mysql') {
713
				$query .= " AND DAY(marine_output.date) = :day";
714
				$query_values = array_merge($query_values,array(':day' => $day));
715
			} else {
716
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
717
				$query_values = array_merge($query_values,array(':day' => $day));
718
			}
719
		}
720
		$query_values = array_merge($query_values,array(':captain' => $captain));
721
		$sth = $this->db->prepare($query);
722
		$sth->execute($query_values);
723
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
724
		if (is_int($result[0]['duration'])) return gmdate('H:i:s',$result[0]['duration']);
725
		else return $result[0]['duration'];
726
	}
727
728
	/**
729
	* Gets a list of all captain names and captain ids
730
	*
731
	* @return Array list of captain names and captain ids
732
	*
733
	*/
734
	public function getAllCaptainNames($filters = array())
735
	{
736
		$filter_query = $this->getFilter($filters,true,true);
737
		$query  = "SELECT DISTINCT marine_output.captain_name, marine_output.captain_id
738
			FROM marine_output".$filter_query." marine_output.captain_name <> '' 
739
			ORDER BY marine_output.captain_name ASC";
740
	
741
		$sth = $this->db->prepare($query);
742
		$sth->execute();
743
		return $sth->fetchAll(PDO::FETCH_ASSOC);
744
	} 
745
746
	/**
747
	* Gets a list of all race names and race ids
748
	*
749
	* @return Array list of race names and race ids
750
	*
751
	*/
752
	public function getAllRaceNames($filters = array())
753
	{
754
		$filter_query = $this->getFilter($filters,true,true);
755
		$query  = "SELECT DISTINCT marine_output.race_name, marine_output.race_id
756
			FROM marine_output".$filter_query." marine_output.race_name <> '' 
757
			ORDER BY marine_output.race_name ASC";
758
	
759
		$sth = $this->db->prepare($query);
760
		$sth->execute();
761
		return $sth->fetchAll(PDO::FETCH_ASSOC);
762
	} 
763
764
	/**
765
	* Gets all source name
766
	*
767
	* @param String type format of source
768
	* @return Array list of source name
769
	*
770
	*/
771
	public function getAllSourceName($type = '',$filters = array())
772
	{
773
		$filter_query = $this->getFilter($filters,true,true);
774
		$query_values = array();
775
		$query  = "SELECT DISTINCT marine_output.source_name 
776
				FROM marine_output".$filter_query." marine_output.source_name <> ''";
777
		if ($type != '') {
778
			$query_values = array(':type' => $type);
779
			$query .= " AND format_source = :type";
780
		}
781
		$query .= " ORDER BY marine_output.source_name ASC";
782
783
		$sth = $this->db->prepare($query);
784
		if (!empty($query_values)) $sth->execute($query_values);
785
		else $sth->execute();
786
787
		$source_array = array();
788
		$temp_array = array();
789
		
790
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
791
		{
792
			$temp_array['source_name'] = $row['source_name'];
793
			$source_array[] = $temp_array;
794
		}
795
		return $source_array;
796
	}
797
798
799
	/**
800
	* Gets a list of all idents/callsigns
801
	*
802
	* @return Array list of ident/callsign names
803
	*
804
	*/
805
	public function getAllIdents($filters = array())
806
	{
807
		$filter_query = $this->getFilter($filters,true,true);
808
		$query  = "SELECT DISTINCT marine_output.ident
809
								FROM marine_output".$filter_query." marine_output.ident <> '' 
810
								ORDER BY marine_output.date ASC LIMIT 700 OFFSET 0";
811
812
		$sth = $this->db->prepare($query);
813
		$sth->execute();
814
    
815
		$ident_array = array();
816
		$temp_array = array();
817
		
818
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
819
		{
820
			$temp_array['ident'] = $row['ident'];
821
			$ident_array[] = $temp_array;
822
		}
823
824
		return $ident_array;
825
	}
826
827
	/**
828
	* Gets all info from a mmsi
829
	*
830
	* @return Array ident
831
	*
832
	*/
833
	public function getIdentity($mmsi)
834
	{
835
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT);
836
		$query  = "SELECT * FROM marine_identity WHERE mmsi = :mmsi LIMIT 1";
837
		$sth = $this->db->prepare($query);
838
		$sth->execute(array(':mmsi' => $mmsi));
839
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
840
		if (isset($result[0])) return $result[0];
841
		else return array();
842
	}
843
844
	/**
845
	* Add identity
846
	*
847
	*/
848
	public function addIdentity($mmsi,$imo,$ident,$callsign,$type)
849
	{
850
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_NUMBER_INT);
851
		if ($mmsi != '') {
852
			$imo = filter_var($imo,FILTER_SANITIZE_NUMBER_INT);
853
			$ident = filter_var($ident,FILTER_SANITIZE_STRING);
854
			$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
855
			$type = filter_var($type,FILTER_SANITIZE_STRING);
856
			$identinfo = $this->getIdentity($mmsi);
857
			if (empty($identinfo)) {
858
				$query  = "INSERT INTO marine_identity (mmsi,imo,call_sign,ship_name,type) VALUES (:mmsi,:imo,:call_sign,:ship_name,:type)";
859
				$sth = $this->db->prepare($query);
860
				$sth->execute(array(':mmsi' => $mmsi,':imo' => $imo,':call_sign' => $callsign,':ship_name' => $ident,':type' => $type));
861
			} elseif ($ident != '' && $identinfo['ship_name'] != $ident) {
862
				$query  = "UPDATE marine_identity SET ship_name = :ship_name,type = :type WHERE mmsi = :mmsi";
863
				$sth = $this->db->prepare($query);
864
				$sth->execute(array(':mmsi' => $mmsi,':ship_name' => $ident,':type' => $type));
865
			}
866
		}
867
	}
868
869
	/*
870
	* Gets a list of all dates
871
	*
872
	* @return Array list of date names
873
	*
874
	*/
875
	public function getAllDates()
876
	{
877
		global $globalTimezone, $globalDBdriver;
878
		if ($globalTimezone != '') {
879
			date_default_timezone_set($globalTimezone);
880
			$datetime = new DateTime();
881
			$offset = $datetime->format('P');
882
		} else $offset = '+00:00';
883
884
		if ($globalDBdriver == 'mysql') {
885
			$query  = "SELECT DISTINCT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) as date
886
								FROM marine_output
887
								WHERE marine_output.date <> '' 
888
								ORDER BY marine_output.date ASC LIMIT 0,100";
889
		} else {
890
			$query  = "SELECT DISTINCT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') as date
891
								FROM marine_output
892
								WHERE marine_output.date <> '' 
893
								ORDER BY marine_output.date ASC LIMIT 0,100";
894
		}
895
		
896
		$sth = $this->db->prepare($query);
897
		$sth->execute(array(':offset' => $offset));
898
    
899
		$date_array = array();
900
		$temp_array = array();
901
		
902
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
903
		{
904
			$temp_array['date'] = $row['date'];
905
906
			$date_array[] = $temp_array;
907
		}
908
909
		return $date_array;
910
	}
911
912
	/**
913
	* Update ident tracker data
914
	*
915
	* @param String $fammarine_id the ID
916
	* @param String $ident the marine ident
917
	* @return String success or false
918
	*
919
	*/
920
	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...
921
	{
922
		$query = 'UPDATE marine_output SET ident = :ident WHERE fammarine_id = :fammarine_id';
923
		$query_values = array(':fammarine_id' => $fammarine_id,':ident' => $ident);
924
		try {
925
			$sth = $this->db->prepare($query);
926
			$sth->execute($query_values);
927
		} catch (PDOException $e) {
928
			return "error : ".$e->getMessage();
929
		}
930
		return "success";
931
	}
932
933
	/**
934
	* Update arrival marine data
935
	*
936
	* @param String $fammarine_id the ID
937
	* @param String $arrival_code the marine ident
938
	* @return String success or false
939
	*
940
	*/
941
	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...
942
	{
943
		$query = 'UPDATE marine_output SET arrival_port_name = :arrival_code WHERE fammarine_id = :fammarine_id';
944
		$query_values = array(':fammarine_id' => $fammarine_id,':arrival_code' => $arrival_code);
945
		try {
946
			$sth = $this->db->prepare($query);
947
			$sth->execute($query_values);
948
		} catch (PDOException $e) {
949
			return "error : ".$e->getMessage();
950
		}
951
		return "success";
952
	}
953
954
	/**
955
	* Update Status data
956
	*
957
	* @param String $fammarine_id the ID
958
	* @param String $status_id the marine status id
959
	* @param String $status the marine status
960
	* @return String success or false
961
	*
962
	*/
963
	public function updateStatusMarineData($fammarine_id = '', $status_id = '',$status = '')
964
	{
965
966
		$query = 'UPDATE marine_output SET status = :status, status_id = :status_id WHERE fammarine_id = :fammarine_id';
967
                $query_values = array(':fammarine_id' => $fammarine_id,':status' => $status,':status_id' => $status_id);
968
969
		try {
970
			$sth = $this->db->prepare($query);
971
			$sth->execute($query_values);
972
		} catch (PDOException $e) {
973
			return "error : ".$e->getMessage();
974
		}
975
		
976
		return "success";
977
978
	}
979
	/**
980
	* Update latest marine data
981
	*
982
	* @param String $fammarine_id the ID
983
	* @param String $ident the marine ident
984
	* @return String success or false
985
	*
986
	*/	
987
	public function updateLatestMarineData($fammarine_id = '', $ident = '', $latitude = '', $longitude = '', $groundspeed = NULL, $date = '',$distance = NULL,$race_rank = NULL, $race_time = NULL, $status = '', $race_begin = '')
988
	{
989
		if ($latitude == '') $latitude = NULL;
990
		if ($longitude == '') $longitude = NULL;
991
		$groundspeed = round($groundspeed);
992
		if ($race_begin != '') {
993
			$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';
994
			$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);
995
		} else {
996
			$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';
997
			$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);
998
		}
999
		try {
1000
			$sth = $this->db->prepare($query);
1001
			$sth->execute($query_values);
1002
		} catch (PDOException $e) {
1003
			echo "error : ".$e->getMessage();
1004
			return "error : ".$e->getMessage();
1005
		}
1006
		
1007
		return "success";
1008
1009
	}
1010
1011
	/**
1012
	* Adds a new marine data
1013
	*
1014
	* @param String $fammarine_id the ID
1015
	* @param String $ident the marine ident
1016
	* @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...
1017
	* @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...
1018
	* @param String $latitude latitude of flight
1019
	* @param String $longitude latitude of flight
1020
	* @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...
1021
	* @param String $heading heading of flight
1022
	* @param String $groundspeed speed of flight
1023
	* @param String $date date of flight
1024
	* @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...
1025
	* @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...
1026
	* @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...
1027
	* @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...
1028
	* @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...
1029
	* @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...
1030
	* @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...
1031
	* @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...
1032
	* @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...
1033
	* @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...
1034
	* @return String success or false
1035
	*/
1036
	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 = '')
1037
	{
1038
		global $globalURL, $globalMarineImageFetch;
1039
		
1040
		//$Image = new Image($this->db);
1041
		$Common = new Common();
1042
		
1043
		date_default_timezone_set('UTC');
1044
		
1045
		//getting the registration
1046
		if ($fammarine_id != "")
1047
		{
1048
			if (!is_string($fammarine_id))
1049
			{
1050
				return false;
1051
			}
1052
		}
1053
		$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...
1054
		//getting the airline information
1055
		if ($ident != "")
1056
		{
1057
			if (!is_string($ident))
1058
			{
1059
				return false;
1060
			}
1061
		}
1062
1063
		if ($latitude != "")
1064
		{
1065
			if (!is_numeric($latitude))
1066
			{
1067
				return false;
1068
			}
1069
		}
1070
		
1071
		if ($longitude != "")
1072
		{
1073
			if (!is_numeric($longitude))
1074
			{
1075
				return false;
1076
			}
1077
		}
1078
		
1079
		if ($heading != "")
1080
		{
1081
			if (!is_numeric($heading))
1082
			{
1083
				return false;
1084
			}
1085
		}
1086
		if ($mmsi != "")
1087
		{
1088
			if (!is_numeric($mmsi))
1089
			{
1090
				return false;
1091
			}
1092
		}
1093
		
1094
		if ($groundspeed != "")
1095
		{
1096
			if (!is_numeric($groundspeed))
1097
			{
1098
				return false;
1099
			}
1100
		}
1101
1102
    
1103
		if ($date == "")
1104
		{
1105
			$date = date("Y-m-d H:i:s", time());
1106
		}
1107
1108
		$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING);
1109
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1110
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1111
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1112
		$heading = filter_var($heading,FILTER_SANITIZE_NUMBER_INT);
1113
		$groundspeed = filter_var($groundspeed,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1114
		$format_source = filter_var($format_source,FILTER_SANITIZE_STRING);
1115
		$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
1116
		$type = filter_var($type,FILTER_SANITIZE_STRING);
1117
		$status = filter_var($status,FILTER_SANITIZE_STRING);
1118
		$type_id = filter_var($typeid,FILTER_SANITIZE_NUMBER_INT);
1119
		$status_id = filter_var($statusid,FILTER_SANITIZE_NUMBER_INT);
1120
		$imo = filter_var($imo,FILTER_SANITIZE_STRING);
1121
		$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...
1122
		$arrival_code = filter_var($arrival_code,FILTER_SANITIZE_STRING);
1123
		$arrival_date = filter_var($arrival_date,FILTER_SANITIZE_STRING);
1124
		$captain_id = filter_var($captain_id,FILTER_SANITIZE_STRING);
1125
		$captain_name = filter_var($captain_name,FILTER_SANITIZE_STRING);
1126
		$race_id = filter_var($race_id,FILTER_SANITIZE_STRING);
1127
		$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
1128
		$race_rank = filter_var($race_rank,FILTER_SANITIZE_NUMBER_INT);
1129
		$race_time = filter_var($race_time,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
1130
		if (isset($globalMarineImageFetch) && $globalMarineImageFetch === TRUE) {
1131
			$Image = new Image($this->db);
1132
			$image_array = $Image->getMarineImage($mmsi,$imo,$ident);
1133
			if (!isset($image_array[0]['mmsi'])) {
1134
				$Image->addMarineImage($mmsi,$imo,$ident);
1135
			}
1136
			unset($Image);
1137
		}
1138
		if ($latitude == '' && $longitude == '') {
1139
			$latitude = 0;
1140
			$longitude = 0;
1141
		}
1142
		if ($type_id == '') $type_id = NULL;
1143
		if ($status_id == '') $status_id = NULL;
1144
		if ($distance == '') $distance = NULL;
1145
		if ($race_rank == '') $race_rank = NULL;
1146
		if ($race_time == '') $race_time = NULL;
1147
		if ($heading == '' || $Common->isInteger($heading) === false) $heading = 0;
1148
		//if ($groundspeed == '' || $Common->isInteger($groundspeed) === false) $groundspeed = 0;
1149
		if ($arrival_date == '') $arrival_date = NULL;
1150
		$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) 
1151
		    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)";
1152
1153
		$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);
1154
		try {
1155
			$sth = $this->db->prepare($query);
1156
			$sth->execute($query_values);
1157
			$this->db = null;
1158
		} catch (PDOException $e) {
1159
			return "error : ".$e->getMessage();
1160
		}
1161
		
1162
		return "success";
1163
1164
	}
1165
	
1166
  
1167
	/**
1168
	* Gets the aircraft ident within the last hour
1169
	*
1170
	* @return String the ident
1171
	*
1172
	*/
1173
	public function getIdentFromLastHour($ident)
1174
	{
1175
		global $globalDBdriver, $globalTimezone;
1176
		if ($globalDBdriver == 'mysql') {
1177
			$query  = "SELECT marine_output.ident FROM marine_output 
1178
								WHERE marine_output.ident = :ident 
1179
								AND marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) 
1180
								AND marine_output.date < UTC_TIMESTAMP()";
1181
			$query_data = array(':ident' => $ident);
1182
		} else {
1183
			$query  = "SELECT marine_output.ident FROM marine_output 
1184
								WHERE marine_output.ident = :ident 
1185
								AND marine_output.date >= now() AT TIME ZONE 'UTC' - INTERVAL '1 HOURS'
1186
								AND marine_output.date < now() AT TIME ZONE 'UTC'";
1187
			$query_data = array(':ident' => $ident);
1188
    		}
1189
		
1190
		$sth = $this->db->prepare($query);
1191
		$sth->execute($query_data);
1192
    		$ident_result='';
1193
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1194
		{
1195
			$ident_result = $row['ident'];
1196
		}
1197
1198
		return $ident_result;
1199
	}
1200
	
1201
	
1202
	/**
1203
	* Gets the aircraft data from the last 20 seconds
1204
	*
1205
	* @return Array the marine data
1206
	*
1207
	*/
1208
	public function getRealTimeData($q = '')
1209
	{
1210
		global $globalDBdriver;
1211
		$additional_query = '';
1212
		if ($q != "")
1213
		{
1214
			if (!is_string($q))
1215
			{
1216
				return false;
1217
			} else {
1218
				$q_array = explode(" ", $q);
1219
				foreach ($q_array as $q_item){
1220
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
1221
					$additional_query .= " AND (";
1222
					$additional_query .= "(marine_output.ident like '%".$q_item."%')";
1223
					$additional_query .= ")";
1224
				}
1225
			}
1226
		}
1227
		if ($globalDBdriver == 'mysql') {
1228
			$query  = "SELECT marine_output.* FROM marine_output 
1229
				WHERE marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 SECOND) ".$additional_query." 
1230
				AND marine_output.date < UTC_TIMESTAMP()";
1231
		} else {
1232
			$query  = "SELECT marine_output.* FROM marine_output 
1233
				WHERE marine_output.date::timestamp >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '20 SECONDS' ".$additional_query." 
1234
				AND marine_output.date::timestamp < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";
1235
		}
1236
		$marine_array = $this->getDataFromDB($query, array());
1237
1238
		return $marine_array;
1239
	}
1240
	
1241
	
1242
	
1243
1244
	/**
1245
	* Gets all number of flight over countries
1246
	*
1247
	* @return Array the airline country list
1248
	*
1249
	*/
1250
1251
	public function countAllMarineOverCountries($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array())
1252
	{
1253
		global $globalDBdriver, $globalArchive;
1254
		//$filter_query = $this->getFilter($filters,true,true);
1255
		$Connection= new Connection($this->db);
1256
		if (!$Connection->tableExists('countries')) return array();
1257
		require_once('class.SpotterLive.php');
1258
		if (!isset($globalArchive) || $globalArchive !== TRUE) {
1259
			$MarineLive = new MarineLive($this->db);
1260
			$filter_query = $MarineLive->getFilter($filters,true,true);
1261
			$filter_query .= " over_country IS NOT NULL AND over_country <> ''";
1262
			if ($olderthanmonths > 0) {
1263
				if ($globalDBdriver == 'mysql') {
1264
					$filter_query .= ' AND marine_live.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1265
				} else {
1266
					$filter_query .= " AND marine_live.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1267
				}
1268
			}
1269
			if ($sincedate != '') {
1270
				if ($globalDBdriver == 'mysql') {
1271
					$filter_query .= " AND marine_live.date > '".$sincedate."' ";
1272
				} else {
1273
					$filter_query .= " AND marine_live.date > CAST('".$sincedate."' AS TIMESTAMP)";
1274
				}
1275
			}
1276
			$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 ";
1277
		} else {
1278
			require_once(dirname(__FILE__)."/class.MarineArchive.php");
1279
			$MarineArchive = new MarineArchive($this->db);
1280
			$filter_query = $MarineArchive->getFilter($filters,true,true);
1281
			$filter_query .= " over_country <> ''";
1282
			if ($olderthanmonths > 0) {
1283
				if ($globalDBdriver == 'mysql') {
1284
					$filter_query .= ' AND marine_archive.date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH) ';
1285
				} else {
1286
					$filter_query .= " AND marine_archive.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1287
				}
1288
			}
1289
			if ($sincedate != '') {
1290
				if ($globalDBdriver == 'mysql') {
1291
					$filter_query .= " AND marine_archive.date > '".$sincedate."' ";
1292
				} else {
1293
					$filter_query .= " AND marine_archive.date > CAST('".$sincedate."' AS TIMESTAMP)";
1294
				}
1295
			}
1296
			$filter_query .= " LIMIT 200 OFFSET 0";
1297
			$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 ";
1298
		}
1299
		$query .= "GROUP BY c.name,c.iso3,c.iso2 ORDER BY nb DESC";
1300
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1301
1302
		$sth = $this->db->prepare($query);
1303
		$sth->execute();
1304
 
1305
		$flight_array = array();
1306
		$temp_array = array();
1307
        
1308
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1309
		{
1310
			$temp_array['marine_count'] = $row['nb'];
1311
			$temp_array['marine_country'] = $row['name'];
1312
			$temp_array['marine_country_iso3'] = $row['iso3'];
1313
			$temp_array['marine_country_iso2'] = $row['iso2'];
1314
			$flight_array[] = $temp_array;
1315
		}
1316
		return $flight_array;
1317
	}
1318
	
1319
	
1320
	
1321
	/**
1322
	* Gets all callsigns that have flown over
1323
	*
1324
	* @return Array the callsign list
1325
	*
1326
	*/
1327
	public function countAllCallsigns($limit = true, $olderthanmonths = 0, $sincedate = '',$filters = array(),$year = '', $month = '', $day = '')
1328
	{
1329
		global $globalDBdriver;
1330
		$filter_query = $this->getFilter($filters,true,true);
1331
		$query  = "SELECT DISTINCT marine_output.ident, COUNT(marine_output.ident) AS callsign_icao_count 
1332
                    FROM marine_output".$filter_query." marine_output.ident <> ''";
1333
		 if ($olderthanmonths > 0) {
1334
			if ($globalDBdriver == 'mysql') $query .= ' AND date < DATE_SUB(UTC_TIMESTAMP(),INTERVAL '.$olderthanmonths.' MONTH)';
1335
			else $query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
1336
		}
1337
		if ($sincedate != '') {
1338
			if ($globalDBdriver == 'mysql') $query .= " AND marine_output.date > '".$sincedate."'";
1339
			else $query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
1340
		}
1341
		$query_values = array();
1342
		if ($year != '') {
1343
			if ($globalDBdriver == 'mysql') {
1344
				$query .= " AND YEAR(marine_output.date) = :year";
1345
				$query_values = array_merge($query_values,array(':year' => $year));
1346
			} else {
1347
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1348
				$query_values = array_merge($query_values,array(':year' => $year));
1349
			}
1350
		}
1351
		if ($month != '') {
1352
			if ($globalDBdriver == 'mysql') {
1353
				$query .= " AND MONTH(marine_output.date) = :month";
1354
				$query_values = array_merge($query_values,array(':month' => $month));
1355
			} else {
1356
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1357
				$query_values = array_merge($query_values,array(':month' => $month));
1358
			}
1359
		}
1360
		if ($day != '') {
1361
			if ($globalDBdriver == 'mysql') {
1362
				$query .= " AND DAY(marine_output.date) = :day";
1363
				$query_values = array_merge($query_values,array(':day' => $day));
1364
			} else {
1365
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
1366
				$query_values = array_merge($query_values,array(':day' => $day));
1367
			}
1368
		}
1369
		$query .= " GROUP BY marine_output.ident ORDER BY callsign_icao_count DESC";
1370
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
1371
      		
1372
		$sth = $this->db->prepare($query);
1373
		$sth->execute($query_values);
1374
      
1375
		$callsign_array = array();
1376
		$temp_array = array();
1377
        
1378
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1379
		{
1380
			$temp_array['callsign_icao'] = $row['ident'];
1381
			$temp_array['airline_name'] = $row['airline_name'];
1382
			$temp_array['airline_icao'] = $row['airline_icao'];
1383
			$temp_array['callsign_icao_count'] = $row['callsign_icao_count'];
1384
          
1385
			$callsign_array[] = $temp_array;
1386
		}
1387
1388
		return $callsign_array;
1389
	}
1390
1391
1392
	/**
1393
	* Counts all dates
1394
	*
1395
	* @return Array the date list
1396
	*
1397
	*/
1398
	public function countAllDates($filters = array())
1399
	{
1400
		global $globalTimezone, $globalDBdriver;
1401
		if ($globalTimezone != '') {
1402
			date_default_timezone_set($globalTimezone);
1403
			$datetime = new DateTime();
1404
			$offset = $datetime->format('P');
1405
		} else $offset = '+00:00';
1406
1407
		if ($globalDBdriver == 'mysql') {
1408
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1409
								FROM marine_output";
1410
			$query .= $this->getFilter($filters);
1411
			$query .= " GROUP BY date_name 
1412
								ORDER BY date_count DESC
1413
								LIMIT 10 OFFSET 0";
1414
		} else {
1415
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1416
								FROM marine_output";
1417
			$query .= $this->getFilter($filters);
1418
			$query .= " GROUP BY date_name 
1419
								ORDER BY date_count DESC
1420
								LIMIT 10 OFFSET 0";
1421
		}
1422
      
1423
		
1424
		$sth = $this->db->prepare($query);
1425
		$sth->execute(array(':offset' => $offset));
1426
      
1427
		$date_array = array();
1428
		$temp_array = array();
1429
        
1430
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1431
		{
1432
			$temp_array['date_name'] = $row['date_name'];
1433
			$temp_array['date_count'] = $row['date_count'];
1434
1435
			$date_array[] = $temp_array;
1436
		}
1437
1438
		return $date_array;
1439
	}
1440
	
1441
	
1442
	/**
1443
	* Counts all dates during the last 7 days
1444
	*
1445
	* @return Array the date list
1446
	*
1447
	*/
1448
	public function countAllDatesLast7Days($filters = array())
1449
	{
1450
		global $globalTimezone, $globalDBdriver;
1451
		if ($globalTimezone != '') {
1452
			date_default_timezone_set($globalTimezone);
1453
			$datetime = new DateTime();
1454
			$offset = $datetime->format('P');
1455
		} else $offset = '+00:00';
1456
		$filter_query = $this->getFilter($filters,true,true);
1457
		if ($globalDBdriver == 'mysql') {
1458
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1459
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 7 DAY)";
1460
			$query .= " GROUP BY date_name 
1461
								ORDER BY marine_output.date ASC";
1462
			$query_data = array(':offset' => $offset);
1463
		} else {
1464
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1465
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '7 DAYS'";
1466
			$query .= " GROUP BY date_name 
1467
								ORDER BY date_name ASC";
1468
			$query_data = array(':offset' => $offset);
1469
    		}
1470
		
1471
		$sth = $this->db->prepare($query);
1472
		$sth->execute($query_data);
1473
      
1474
		$date_array = array();
1475
		$temp_array = array();
1476
        
1477
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1478
		{
1479
			$temp_array['date_name'] = $row['date_name'];
1480
			$temp_array['date_count'] = $row['date_count'];
1481
          
1482
			$date_array[] = $temp_array;
1483
		}
1484
1485
		return $date_array;
1486
	}
1487
1488
	/**
1489
	* Counts all dates during the last month
1490
	*
1491
	* @return Array the date list
1492
	*
1493
	*/
1494
	public function countAllDatesLastMonth($filters = array())
1495
	{
1496
		global $globalTimezone, $globalDBdriver;
1497
		if ($globalTimezone != '') {
1498
			date_default_timezone_set($globalTimezone);
1499
			$datetime = new DateTime();
1500
			$offset = $datetime->format('P');
1501
		} else $offset = '+00:00';
1502
		$filter_query = $this->getFilter($filters,true,true);
1503
		if ($globalDBdriver == 'mysql') {
1504
			$query  = "SELECT DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS date_name, count(*) as date_count
1505
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 MONTH)";
1506
			$query .= " GROUP BY date_name 
1507
								ORDER BY marine_output.date ASC";
1508
			$query_data = array(':offset' => $offset);
1509
		} else {
1510
			$query  = "SELECT to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') AS date_name, count(*) as date_count
1511
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 MONTHS'";
1512
			$query .= " GROUP BY date_name 
1513
								ORDER BY date_name ASC";
1514
			$query_data = array(':offset' => $offset);
1515
    		}
1516
		
1517
		$sth = $this->db->prepare($query);
1518
		$sth->execute($query_data);
1519
      
1520
		$date_array = array();
1521
		$temp_array = array();
1522
        
1523
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1524
		{
1525
			$temp_array['date_name'] = $row['date_name'];
1526
			$temp_array['date_count'] = $row['date_count'];
1527
          
1528
			$date_array[] = $temp_array;
1529
		}
1530
1531
		return $date_array;
1532
	}
1533
1534
1535
1536
	/**
1537
	* Counts all month
1538
	*
1539
	* @return Array the month list
1540
	*
1541
	*/
1542
	public function countAllMonths($filters = array())
1543
	{
1544
		global $globalTimezone, $globalDBdriver;
1545
		if ($globalTimezone != '') {
1546
			date_default_timezone_set($globalTimezone);
1547
			$datetime = new DateTime();
1548
			$offset = $datetime->format('P');
1549
		} else $offset = '+00:00';
1550
1551
		if ($globalDBdriver == 'mysql') {
1552
			$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
1553
								FROM marine_output";
1554
			$query .= $this->getFilter($filters);
1555
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1556
		} else {
1557
			$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
1558
								FROM marine_output";
1559
			$query .= $this->getFilter($filters);
1560
			$query .= " GROUP BY year_name, month_name ORDER BY date_count DESC";
1561
		}
1562
      
1563
		
1564
		$sth = $this->db->prepare($query);
1565
		$sth->execute(array(':offset' => $offset));
1566
      
1567
		$date_array = array();
1568
		$temp_array = array();
1569
        
1570
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1571
		{
1572
			$temp_array['month_name'] = $row['month_name'];
1573
			$temp_array['year_name'] = $row['year_name'];
1574
			$temp_array['date_count'] = $row['date_count'];
1575
1576
			$date_array[] = $temp_array;
1577
		}
1578
1579
		return $date_array;
1580
	}
1581
1582
	
1583
	
1584
1585
	/**
1586
	* Counts all dates during the last year
1587
	*
1588
	* @return Array the date list
1589
	*
1590
	*/
1591
	public function countAllMonthsLastYear($filters)
1592
	{
1593
		global $globalTimezone, $globalDBdriver;
1594
		if ($globalTimezone != '') {
1595
			date_default_timezone_set($globalTimezone);
1596
			$datetime = new DateTime();
1597
			$offset = $datetime->format('P');
1598
		} else $offset = '+00:00';
1599
		$filter_query = $this->getFilter($filters,true,true);
1600
		if ($globalDBdriver == 'mysql') {
1601
			$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
1602
								FROM marine_output".$filter_query." marine_output.date >= DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 YEAR)";
1603
			$query .= " GROUP BY year_name, month_name
1604
								ORDER BY year_name, month_name ASC";
1605
			$query_data = array(':offset' => $offset);
1606
		} else {
1607
			$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
1608
								FROM marine_output".$filter_query." marine_output.date >= CURRENT_TIMESTAMP AT TIME ZONE INTERVAL :offset - INTERVAL '1 YEARS'";
1609
			$query .= " GROUP BY year_name, month_name
1610
								ORDER BY year_name, month_name ASC";
1611
			$query_data = array(':offset' => $offset);
1612
    		}
1613
		
1614
		$sth = $this->db->prepare($query);
1615
		$sth->execute($query_data);
1616
      
1617
		$date_array = array();
1618
		$temp_array = array();
1619
        
1620
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1621
		{
1622
			$temp_array['year_name'] = $row['year_name'];
1623
			$temp_array['month_name'] = $row['month_name'];
1624
			$temp_array['date_count'] = $row['date_count'];
1625
          
1626
			$date_array[] = $temp_array;
1627
		}
1628
1629
		return $date_array;
1630
	}
1631
	
1632
	
1633
	
1634
	/**
1635
	* Counts all hours
1636
	*
1637
	* @return Array the hour list
1638
	*
1639
	*/
1640
	public function countAllHours($orderby,$filters = array())
1641
	{
1642
		global $globalTimezone, $globalDBdriver;
1643
		if ($globalTimezone != '') {
1644
			date_default_timezone_set($globalTimezone);
1645
			$datetime = new DateTime();
1646
			$offset = $datetime->format('P');
1647
		} else $offset = '+00:00';
1648
1649
		$orderby_sql = '';
1650
		if ($orderby == "hour")
1651
		{
1652
			$orderby_sql = "ORDER BY hour_name ASC";
1653
		}
1654
		if ($orderby == "count")
1655
		{
1656
			$orderby_sql = "ORDER BY hour_count DESC";
1657
		}
1658
		
1659
		if ($globalDBdriver == 'mysql') {
1660
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1661
								FROM marine_output";
1662
			$query .= $this->getFilter($filters);
1663
			$query .= " GROUP BY hour_name 
1664
								".$orderby_sql;
1665
1666
/*		$query  = "SELECT HOUR(marine_output.date) AS hour_name, count(*) as hour_count
1667
								FROM marine_output 
1668
								GROUP BY hour_name 
1669
								".$orderby_sql."
1670
								LIMIT 10 OFFSET 00";
1671
  */    
1672
		$query_data = array(':offset' => $offset);
1673
		} else {
1674
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1675
								FROM marine_output";
1676
			$query .= $this->getFilter($filters);
1677
			$query .= " GROUP BY hour_name 
1678
								".$orderby_sql;
1679
			$query_data = array(':offset' => $offset);
1680
		}
1681
		
1682
		$sth = $this->db->prepare($query);
1683
		$sth->execute($query_data);
1684
      
1685
		$hour_array = array();
1686
		$temp_array = array();
1687
        
1688
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1689
		{
1690
			$temp_array['hour_name'] = $row['hour_name'];
1691
			$temp_array['hour_count'] = $row['hour_count'];
1692
          
1693
			$hour_array[] = $temp_array;
1694
		}
1695
1696
		return $hour_array;
1697
	}
1698
	
1699
	
1700
	
1701
	/**
1702
	* Counts all hours by date
1703
	*
1704
	* @return Array the hour list
1705
	*
1706
	*/
1707
	public function countAllHoursByDate($date, $filters = array())
1708
	{
1709
		global $globalTimezone, $globalDBdriver;
1710
		$filter_query = $this->getFilter($filters,true,true);
1711
		$date = filter_var($date,FILTER_SANITIZE_STRING);
1712
		if ($globalTimezone != '') {
1713
			date_default_timezone_set($globalTimezone);
1714
			$datetime = new DateTime($date);
1715
			$offset = $datetime->format('P');
1716
		} else $offset = '+00:00';
1717
1718
		if ($globalDBdriver == 'mysql') {
1719
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1720
								FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = :date
1721
								GROUP BY hour_name 
1722
								ORDER BY hour_name ASC";
1723
		} else {
1724
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1725
								FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset, 'YYYY-mm-dd') = :date
1726
								GROUP BY hour_name 
1727
								ORDER BY hour_name ASC";
1728
		}
1729
		
1730
		$sth = $this->db->prepare($query);
1731
		$sth->execute(array(':date' => $date, ':offset' => $offset));
1732
      
1733
		$hour_array = array();
1734
		$temp_array = array();
1735
        
1736
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1737
		{
1738
			$temp_array['hour_name'] = $row['hour_name'];
1739
			$temp_array['hour_count'] = $row['hour_count'];
1740
          
1741
			$hour_array[] = $temp_array;
1742
		}
1743
1744
		return $hour_array;
1745
	}
1746
	
1747
	
1748
	
1749
	/**
1750
	* Counts all hours by a ident/callsign
1751
	*
1752
	* @return Array the hour list
1753
	*
1754
	*/
1755
	public function countAllHoursByIdent($ident, $filters = array())
1756
	{
1757
		global $globalTimezone, $globalDBdriver;
1758
		$filter_query = $this->getFilter($filters,true,true);
1759
		$ident = filter_var($ident,FILTER_SANITIZE_STRING);
1760
		if ($globalTimezone != '') {
1761
			date_default_timezone_set($globalTimezone);
1762
			$datetime = new DateTime();
1763
			$offset = $datetime->format('P');
1764
		} else $offset = '+00:00';
1765
1766
		if ($globalDBdriver == 'mysql') {
1767
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1768
								FROM marine_output".$filter_query." marine_output.ident = :ident 
1769
								GROUP BY hour_name 
1770
								ORDER BY hour_name ASC";
1771
		} else {
1772
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1773
								FROM marine_output".$filter_query." marine_output.ident = :ident 
1774
								GROUP BY hour_name 
1775
								ORDER BY hour_name ASC";
1776
		}
1777
      
1778
		
1779
		$sth = $this->db->prepare($query);
1780
		$sth->execute(array(':ident' => $ident,':offset' => $offset));
1781
      
1782
		$hour_array = array();
1783
		$temp_array = array();
1784
        
1785
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1786
		{
1787
			$temp_array['hour_name'] = $row['hour_name'];
1788
			$temp_array['hour_count'] = $row['hour_count'];
1789
          
1790
			$hour_array[] = $temp_array;
1791
		}
1792
1793
		return $hour_array;
1794
	}
1795
	
1796
	
1797
	
1798
	/**
1799
	* Counts all vessels
1800
	*
1801
	* @return Integer the number of vessels
1802
	*
1803
	*/
1804
	public function countOverallMarine($filters = array(),$year = '',$month = '')
1805
	{
1806
		global $globalDBdriver;
1807
		//$queryi  = "SELECT COUNT(marine_output.marine_id) AS flight_count FROM marine_output";
1808
		$queryi  = "SELECT COUNT(DISTINCT marine_output.mmsi) AS flight_count FROM marine_output";
1809
		$query_values = array();
1810
		$query = '';
1811
		if ($year != '') {
1812
			if ($globalDBdriver == 'mysql') {
1813
				$query .= " AND YEAR(marine_output.date) = :year";
1814
				$query_values = array_merge($query_values,array(':year' => $year));
1815
			} else {
1816
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1817
				$query_values = array_merge($query_values,array(':year' => $year));
1818
			}
1819
		}
1820
		if ($month != '') {
1821
			if ($globalDBdriver == 'mysql') {
1822
				$query .= " AND MONTH(marine_output.date) = :month";
1823
				$query_values = array_merge($query_values,array(':month' => $month));
1824
			} else {
1825
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1826
				$query_values = array_merge($query_values,array(':month' => $month));
1827
			}
1828
		}
1829
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1830
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1831
		
1832
		$sth = $this->db->prepare($queryi);
1833
		$sth->execute($query_values);
1834
		return $sth->fetchColumn();
1835
	}
1836
	
1837
	/**
1838
	* Counts all vessel type
1839
	*
1840
	* @return Integer the number of vessels
1841
	*
1842
	*/
1843
	public function countOverallMarineTypes($filters = array(),$year = '',$month = '')
1844
	{
1845
		global $globalDBdriver;
1846
		$queryi  = "SELECT COUNT(DISTINCT marine_output.type) AS marine_count FROM marine_output";
1847
		$query_values = array();
1848
		$query = '';
1849
		if ($year != '') {
1850
			if ($globalDBdriver == 'mysql') {
1851
				$query .= " AND YEAR(marine_output.date) = :year";
1852
				$query_values = array_merge($query_values,array(':year' => $year));
1853
			} else {
1854
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
1855
				$query_values = array_merge($query_values,array(':year' => $year));
1856
			}
1857
		}
1858
		if ($month != '') {
1859
			if ($globalDBdriver == 'mysql') {
1860
				$query .= " AND MONTH(marine_output.date) = :month";
1861
				$query_values = array_merge($query_values,array(':month' => $month));
1862
			} else {
1863
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
1864
				$query_values = array_merge($query_values,array(':month' => $month));
1865
			}
1866
		}
1867
		if (empty($query_values)) $queryi .= $this->getFilter($filters);
1868
		else $queryi .= $this->getFilter($filters,true,true).substr($query,4);
1869
		
1870
		$sth = $this->db->prepare($queryi);
1871
		$sth->execute($query_values);
1872
		return $sth->fetchColumn();
1873
	}
1874
	
1875
  
1876
	/**
1877
	* Counts all hours of today
1878
	*
1879
	* @return Array the hour list
1880
	*
1881
	*/
1882
	public function countAllHoursFromToday($filters = array())
1883
	{
1884
		global $globalTimezone, $globalDBdriver;
1885
		$filter_query = $this->getFilter($filters,true,true);
1886
		if ($globalTimezone != '') {
1887
			date_default_timezone_set($globalTimezone);
1888
			$datetime = new DateTime();
1889
			$offset = $datetime->format('P');
1890
		} else $offset = '+00:00';
1891
1892
		if ($globalDBdriver == 'mysql') {
1893
			$query  = "SELECT HOUR(CONVERT_TZ(marine_output.date,'+00:00', :offset)) AS hour_name, count(*) as hour_count
1894
								FROM marine_output".$filter_query." DATE(CONVERT_TZ(marine_output.date,'+00:00', :offset)) = CURDATE()
1895
								GROUP BY hour_name 
1896
								ORDER BY hour_name ASC";
1897
		} else {
1898
			$query  = "SELECT EXTRACT(HOUR FROM marine_output.date AT TIME ZONE INTERVAL :offset) AS hour_name, count(*) as hour_count
1899
								FROM marine_output".$filter_query." to_char(marine_output.date AT TIME ZONE INTERVAL :offset,'YYYY-mm-dd') = CAST(NOW() AS date)
1900
								GROUP BY hour_name 
1901
								ORDER BY hour_name ASC";
1902
		}
1903
		
1904
		$sth = $this->db->prepare($query);
1905
		$sth->execute(array(':offset' => $offset));
1906
      
1907
		$hour_array = array();
1908
		$temp_array = array();
1909
        
1910
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1911
		{
1912
			$temp_array['hour_name'] = $row['hour_name'];
1913
			$temp_array['hour_count'] = $row['hour_count'];
1914
			$hour_array[] = $temp_array;
1915
		}
1916
1917
		return $hour_array;
1918
	}
1919
    
1920
    
1921
     /**
1922
	* Gets the Barrie Spotter ID based on the FlightAware ID
1923
	*
1924
	* @return Integer the Barrie Spotter ID
1925
q	*
1926
	*/
1927
	public function getMarineIDBasedOnFamMarineID($fammarine_id)
1928
	{
1929
		$fammarine_id = filter_var($fammarine_id,FILTER_SANITIZE_STRING);
1930
1931
		$query  = "SELECT marine_output.marine_id
1932
				FROM marine_output 
1933
				WHERE marine_output.fammarine_id = '".$fammarine_id."'";
1934
        
1935
		
1936
		$sth = $this->db->prepare($query);
1937
		$sth->execute();
1938
1939
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
1940
		{
1941
			return $row['marine_id'];
1942
		}
1943
	}
1944
  
1945
 
1946
	/**
1947
	* Parses a date string
1948
	*
1949
	* @param String $dateString the date string
1950
	* @param String $timezone the timezone of a user
1951
	* @return Array the time information
1952
	*
1953
	*/
1954
	public function parseDateString($dateString, $timezone = '')
1955
	{
1956
		$time_array = array();
1957
	
1958
		if ($timezone != "")
1959
		{
1960
			date_default_timezone_set($timezone);
1961
		}
1962
		
1963
		$current_date = date("Y-m-d H:i:s");
1964
		$date = date("Y-m-d H:i:s",strtotime($dateString." UTC"));
1965
		
1966
		$diff = abs(strtotime($current_date) - strtotime($date));
1967
1968
		$time_array['years'] = floor($diff / (365*60*60*24)); 
1969
		$years = $time_array['years'];
1970
		
1971
		$time_array['months'] = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
1972
		$months = $time_array['months'];
1973
		
1974
		$time_array['days'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
1975
		$days = $time_array['days'];
1976
		$time_array['hours'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
1977
		$hours = $time_array['hours'];
1978
		$time_array['minutes'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
1979
		$minutes = $time_array['minutes'];
1980
		$time_array['seconds'] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));  
1981
		
1982
		return $time_array;
1983
	}
1984
	
1985
	/**
1986
	* Parses the direction degrees to working
1987
	*
1988
	* @param Float $direction the direction in degrees
1989
	* @return Array the direction information
1990
	*
1991
	*/
1992
	public function parseDirection($direction = 0)
1993
	{
1994
		if ($direction == '') $direction = 0;
1995
		$direction_array = array();
1996
		$temp_array = array();
1997
1998
		if ($direction == 360 || ($direction >= 0 && $direction < 22.5))
1999
		{
2000
			$temp_array['direction_degree'] = $direction;
2001
			$temp_array['direction_shortname'] = "N";
2002
			$temp_array['direction_fullname'] = "North";
2003
		} elseif ($direction >= 22.5 && $direction < 45){
2004
			$temp_array['direction_degree'] = $direction;
2005
			$temp_array['direction_shortname'] = "NNE";
2006
			$temp_array['direction_fullname'] = "North-Northeast";
2007
		} elseif ($direction >= 45 && $direction < 67.5){
2008
			$temp_array['direction_degree'] = $direction;
2009
			$temp_array['direction_shortname'] = "NE";
2010
			$temp_array['direction_fullname'] = "Northeast";
2011
		} elseif ($direction >= 67.5 && $direction < 90){
2012
			$temp_array['direction_degree'] = $direction;
2013
			$temp_array['direction_shortname'] = "ENE";
2014
			$temp_array['direction_fullname'] = "East-Northeast";
2015
		} elseif ($direction >= 90 && $direction < 112.5){
2016
			$temp_array['direction_degree'] = $direction;
2017
			$temp_array['direction_shortname'] = "E";
2018
			$temp_array['direction_fullname'] = "East";
2019
		} elseif ($direction >= 112.5 && $direction < 135){
2020
			$temp_array['direction_degree'] = $direction;
2021
			$temp_array['direction_shortname'] = "ESE";
2022
			$temp_array['direction_fullname'] = "East-Southeast";
2023
		} elseif ($direction >= 135 && $direction < 157.5){
2024
			$temp_array['direction_degree'] = $direction;
2025
			$temp_array['direction_shortname'] = "SE";
2026
			$temp_array['direction_fullname'] = "Southeast";
2027
		} elseif ($direction >= 157.5 && $direction < 180){
2028
			$temp_array['direction_degree'] = $direction;
2029
			$temp_array['direction_shortname'] = "SSE";
2030
			$temp_array['direction_fullname'] = "South-Southeast";
2031
		} elseif ($direction >= 180 && $direction < 202.5){
2032
			$temp_array['direction_degree'] = $direction;
2033
			$temp_array['direction_shortname'] = "S";
2034
			$temp_array['direction_fullname'] = "South";
2035
		} elseif ($direction >= 202.5 && $direction < 225){
2036
			$temp_array['direction_degree'] = $direction;
2037
			$temp_array['direction_shortname'] = "SSW";
2038
			$temp_array['direction_fullname'] = "South-Southwest";
2039
		} elseif ($direction >= 225 && $direction < 247.5){
2040
			$temp_array['direction_degree'] = $direction;
2041
			$temp_array['direction_shortname'] = "SW";
2042
			$temp_array['direction_fullname'] = "Southwest";
2043
		} elseif ($direction >= 247.5 && $direction < 270){
2044
			$temp_array['direction_degree'] = $direction;
2045
			$temp_array['direction_shortname'] = "WSW";
2046
			$temp_array['direction_fullname'] = "West-Southwest";
2047
		} elseif ($direction >= 270 && $direction < 292.5){
2048
			$temp_array['direction_degree'] = $direction;
2049
			$temp_array['direction_shortname'] = "W";
2050
			$temp_array['direction_fullname'] = "West";
2051
		} elseif ($direction >= 292.5 && $direction < 315){
2052
			$temp_array['direction_degree'] = $direction;
2053
			$temp_array['direction_shortname'] = "WNW";
2054
			$temp_array['direction_fullname'] = "West-Northwest";
2055
		} elseif ($direction >= 315 && $direction < 337.5){
2056
			$temp_array['direction_degree'] = $direction;
2057
			$temp_array['direction_shortname'] = "NW";
2058
			$temp_array['direction_fullname'] = "Northwest";
2059
		} elseif ($direction >= 337.5 && $direction < 360){
2060
			$temp_array['direction_degree'] = $direction;
2061
			$temp_array['direction_shortname'] = "NNW";
2062
			$temp_array['direction_fullname'] = "North-Northwest";
2063
		}
2064
		$direction_array[] = $temp_array;
2065
		return $direction_array;
2066
	}
2067
	
2068
	
2069
	/**
2070
	* Gets Country from latitude/longitude
2071
	*
2072
	* @param Float $latitude latitute of the flight
2073
	* @param Float $longitude longitute of the flight
2074
	* @return String the countrie
2075
	*/
2076
	public function getCountryFromLatitudeLongitude($latitude,$longitude)
2077
	{
2078
		global $globalDBdriver, $globalDebug;
2079
		$latitude = filter_var($latitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
2080
		$longitude = filter_var($longitude,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
2081
	
2082
		$Connection = new Connection($this->db);
2083
		if (!$Connection->tableExists('countries')) return '';
2084
	
2085
		try {
2086
			/*
2087
			if ($globalDBdriver == 'mysql') {
2088
				//$query  = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(:latitude :longitude)'), ogc_geom) LIMIT 1";
2089
				$query = "SELECT name, iso2, iso3 FROM countries WHERE Within(GeomFromText('POINT(".$longitude.' '.$latitude.")'), ogc_geom) LIMIT 1";
2090
			}
2091
			*/
2092
			// This query seems to work both for MariaDB and PostgreSQL
2093
			$query = "SELECT name,iso2,iso3 FROM countries WHERE ST_Within(ST_GeomFromText('POINT(".$longitude." ".$latitude.")',4326), ogc_geom) LIMIT 1";
2094
		
2095
			$sth = $this->db->prepare($query);
2096
			//$sth->execute(array(':latitude' => $latitude,':longitude' => $longitude));
2097
			$sth->execute();
2098
    
2099
			$row = $sth->fetch(PDO::FETCH_ASSOC);
2100
			$sth->closeCursor();
2101
			if (count($row) > 0) {
2102
				return $row;
2103
			} else return '';
2104
		} catch (PDOException $e) {
2105
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
2106
			return '';
2107
		}
2108
	
2109
	}
2110
2111
	/**
2112
	* Gets Country from iso2
2113
	*
2114
	* @param String $iso2 ISO2 country code
2115
	* @return String the countrie
2116
	*/
2117
	public function getCountryFromISO2($iso2)
2118
	{
2119
		global $globalDBdriver, $globalDebug;
2120
		$iso2 = filter_var($iso2,FILTER_SANITIZE_STRING);
2121
	
2122
		$Connection = new Connection($this->db);
2123
		if (!$Connection->tableExists('countries')) return '';
2124
	
2125
		try {
2126
			$query = "SELECT name,iso2,iso3 FROM countries WHERE iso2 = :iso2 LIMIT 1";
2127
		
2128
			$sth = $this->db->prepare($query);
2129
			$sth->execute(array(':iso2' => $iso2));
2130
    
2131
			$row = $sth->fetch(PDO::FETCH_ASSOC);
2132
			$sth->closeCursor();
2133
			if (count($row) > 0) {
2134
				return $row;
2135
			} else return '';
2136
		} catch (PDOException $e) {
2137
			if (isset($globalDebug) && $globalDebug) echo 'Error : '.$e->getMessage()."\n";
2138
			return '';
2139
		}
2140
	
2141
	}
2142
2143
	
2144
	/**
2145
	* Gets the short url from bit.ly
2146
	*
2147
	* @param String $url the full url
2148
	* @return String the bit.ly url
2149
	*
2150
	*/
2151
	public function getBitlyURL($url)
2152
	{
2153
		global $globalBitlyAccessToken;
2154
		
2155
		if ($globalBitlyAccessToken == '') return $url;
2156
        
2157
		$google_url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$globalBitlyAccessToken.'&longUrl='.$url;
2158
		
2159
		$ch = curl_init();
2160
		curl_setopt($ch, CURLOPT_HEADER, 0);
2161
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
2162
		curl_setopt($ch, CURLOPT_URL, $google_url);
2163
		$bitly_data = curl_exec($ch);
2164
		curl_close($ch);
2165
		
2166
		$bitly_data = json_decode($bitly_data);
2167
		$bitly_url = '';
2168
		if ($bitly_data->status_txt = "OK"){
2169
			$bitly_url = $bitly_data->data->url;
2170
		}
2171
2172
		return $bitly_url;
2173
	}
2174
2175
	
2176
	/**
2177
	* Gets all vessels types that have flown over
2178
	*
2179
	* @return Array the vessel type list
2180
	*
2181
	*/
2182
	public function countAllMarineTypes($limit = true,$olderthanmonths = 0,$sincedate = '',$filters = array(),$year = '',$month = '',$day = '')
2183
	{
2184
		global $globalDBdriver;
2185
		$filter_query = $this->getFilter($filters,true,true);
2186
		$query  = "SELECT marine_output.type AS marine_type, COUNT(marine_output.type) AS marine_type_count, marine_output.type_id AS marine_type_id 
2187
		    FROM marine_output ".$filter_query." marine_output.type <> '' AND marine_output.type_id IS NOT NULL";
2188
		if ($olderthanmonths > 0) {
2189
			if ($globalDBdriver == 'mysql') {
2190
				$query .= ' AND marine_output.date < DATE_SUB(UTC_TIMESTAMP(), INTERVAL '.$olderthanmonths.' MONTH)';
2191
			} else {
2192
				$query .= " AND marine_output.date < CURRENT_TIMESTAMP AT TIME ZONE 'UTC' - INTERVAL '".$olderthanmonths." MONTHS'";
2193
			}
2194
		}
2195
		if ($sincedate != '') {
2196
			if ($globalDBdriver == 'mysql') {
2197
				$query .= " AND marine_output.date > '".$sincedate."'";
2198
			} else {
2199
				$query .= " AND marine_output.date > CAST('".$sincedate."' AS TIMESTAMP)";
2200
			}
2201
		}
2202
		$query_values = array();
2203
		if ($year != '') {
2204
			if ($globalDBdriver == 'mysql') {
2205
				$query .= " AND YEAR(marine_output.date) = :year";
2206
				$query_values = array_merge($query_values,array(':year' => $year));
2207
			} else {
2208
				$query .= " AND EXTRACT(YEAR FROM marine_output.date) = :year";
2209
				$query_values = array_merge($query_values,array(':year' => $year));
2210
			}
2211
		}
2212
		if ($month != '') {
2213
			if ($globalDBdriver == 'mysql') {
2214
				$query .= " AND MONTH(marine_output.date) = :month";
2215
				$query_values = array_merge($query_values,array(':month' => $month));
2216
			} else {
2217
				$query .= " AND EXTRACT(MONTH FROM marine_output.date) = :month";
2218
				$query_values = array_merge($query_values,array(':month' => $month));
2219
			}
2220
		}
2221
		if ($day != '') {
2222
			if ($globalDBdriver == 'mysql') {
2223
				$query .= " AND DAY(marine_output.date) = :day";
2224
				$query_values = array_merge($query_values,array(':day' => $day));
2225
			} else {
2226
				$query .= " AND EXTRACT(DAY FROM marine_output.date) = :day";
2227
				$query_values = array_merge($query_values,array(':day' => $day));
2228
			}
2229
		}
2230
		$query .= " GROUP BY marine_output.type, marine_output.type_id ORDER BY marine_type_count DESC";
2231
		if ($limit) $query .= " LIMIT 10 OFFSET 0";
2232
		$sth = $this->db->prepare($query);
2233
		$sth->execute($query_values);
2234
		$marine_array = array();
2235
		$temp_array = array();
2236
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
2237
		{
2238
			$temp_array['marine_type'] = $row['marine_type'];
2239
			$temp_array['marine_type_id'] = $row['marine_type_id'];
2240
			$temp_array['marine_type_count'] = $row['marine_type_count'];
2241
			$marine_array[] = $temp_array;
2242
		}
2243
		return $marine_array;
2244
	}
2245
2246
	/**
2247
	* Gets all the tracker information
2248
	*
2249
	* @return Array the tracker information
2250
	*
2251
	*/
2252
	public function searchMarineData($q = '', $callsign = '',$mmsi = '', $imo = '', $date_posted = '', $limit = '', $sort = '', $includegeodata = '',$origLat = '',$origLon = '',$dist = '',$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...
2253
	{
2254
		global $globalTimezone, $globalDBdriver;
2255
		date_default_timezone_set('UTC');
2256
		$query_values = array();
2257
		$additional_query = '';
2258
		$filter_query = $this->getFilter($filters,true,true);
2259
		if ($q != "")
2260
		{
2261
			if (!is_string($q))
2262
			{
2263
				return false;
2264
			} else {
2265
				$q_array = explode(" ", $q);
2266
				foreach ($q_array as $q_item){
2267
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
2268
					$additional_query .= " AND (";
2269
					if (is_int($q_item)) $additional_query .= "(marine_output.marine_id = '".$q_item."') OR ";
2270
					if (is_int($q_item)) $additional_query .= "(marine_output.mmsi = '".$q_item."') OR ";
2271
					if (is_int($q_item)) $additional_query .= "(marine_output.imo = '".$q_item."') OR ";
2272
					$additional_query .= "(marine_output.ident like '%".$q_item."%') OR ";
2273
					$additional_query .= ")";
2274
				}
2275
			}
2276
		}
2277
		if ($callsign != "")
2278
		{
2279
			$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
2280
			if (!is_string($callsign))
2281
			{
2282
				return false;
2283
			} else {
2284
				$additional_query .= " AND marine_output.ident = :callsign";
2285
				$query_values = array_merge($query_values,array(':callsign' => $callsign));
2286
			}
2287
		}
2288
		if ($mmsi != "")
2289
		{
2290
			$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
2291
			if (!is_numeric($mmsi))
2292
			{
2293
				return false;
2294
			} else {
2295
				$additional_query .= " AND marine_output.mmsi = :mmsi";
2296
				$query_values = array_merge($query_values,array(':mmsi' => $mmsi));
2297
			}
2298
		}
2299
		if ($imo != "")
2300
		{
2301
			$imo = filter_var($imo,FILTER_SANITIZE_STRING);
2302
			if (!is_numeric($imo))
2303
			{
2304
				return false;
2305
			} else {
2306
				$additional_query .= " AND marine_output.imo = :imo";
2307
				$query_values = array_merge($query_values,array(':imo' => $imo));
2308
			}
2309
		}
2310
		if ($date_posted != "")
2311
		{
2312
			$date_array = explode(",", $date_posted);
2313
			$date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING);
2314
			$date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING);
2315
			if ($globalTimezone != '') {
2316
				date_default_timezone_set($globalTimezone);
2317
				$datetime = new DateTime();
2318
				$offset = $datetime->format('P');
2319
			} else $offset = '+00:00';
2320
			if ($date_array[1] != "")
2321
			{
2322
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2323
				$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1]));
2324
				if ($globalDBdriver == 'mysql') {
2325
					$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]."' ";
2326
				} else {
2327
					$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]."' ";
2328
				}
2329
			} else {
2330
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2331
				if ($globalDBdriver == 'mysql') {
2332
					$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' ";
2333
				} else {
2334
					$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' ";
2335
				}
2336
			}
2337
		}
2338
		if ($limit != "")
2339
		{
2340
			$limit_array = explode(",", $limit);
2341
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
2342
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
2343
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
2344
			{
2345
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
2346
			} else $limit_query = "";
2347
		} else $limit_query = "";
2348
		if ($sort != "")
2349
		{
2350
			$search_orderby_array = $this->getOrderBy();
2351
			$orderby_query = $search_orderby_array[$sort]['sql'];
2352
		} else {
2353
			if ($origLat != "" && $origLon != "" && $dist != "") {
2354
				$orderby_query = " ORDER BY distance ASC";
2355
			} else {
2356
				$orderby_query = " ORDER BY marine_output.date DESC";
2357
			}
2358
		}
2359
		if ($origLat != "" && $origLon != "" && $dist != "") {
2360
			$dist = number_format($dist*0.621371,2,'.',''); // convert km to mile
2361
			if ($globalDBdriver == 'mysql') {
2362
				$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 
2363
				    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)) 
2364
				    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;
2365
			} else {
2366
				$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 
2367
				    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)) 
2368
				    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;
2369
			}
2370
		} else {
2371
			$query  = "SELECT marine_output.* FROM marine_output".$filter_query." marine_output.ident <> '' 
2372
			    ".$additional_query."
2373
			    ".$orderby_query;
2374
		}
2375
		$marine_array = $this->getDataFromDB($query, $query_values,$limit_query);
2376
		return $marine_array;
2377
	}
2378
2379
	/**
2380
	* Check marine by id
2381
	*
2382
	* @return String the ident
2383
	*
2384
	*/
2385
	public function checkId($id)
2386
	{
2387
		global $globalDBdriver, $globalTimezone;
2388
		$query  = 'SELECT marine_output.ident, marine_output.fammarine_id FROM marine_output WHERE marine_output.fammarine_id = :id';
2389
		$query_data = array(':id' => $id);
2390
		$sth = $this->db->prepare($query);
2391
		$sth->execute($query_data);
2392
		$ident_result='';
2393
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
2394
		{
2395
			$ident_result = $row['fammarine_id'];
2396
		}
2397
		return $ident_result;
2398
	}
2399
2400
	public function getOrderBy()
2401
	{
2402
		$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"));
2403
		
2404
		return $orderby;
2405
		
2406
	}
2407
    
2408
}
2409
?>