Completed
Push — master ( 83bb91...7edaba )
by Yannick
36:33
created

Marine::countAllHoursByIdent()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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