Marine::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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

Loading history...
2549
	{
2550
		global $globalTimezone, $globalDBdriver;
2551
		date_default_timezone_set('UTC');
2552
		$query_values = array();
2553
		$additional_query = '';
2554
		$filter_query = $this->getFilter($filters,true,true);
2555
		if ($q != "")
2556
		{
2557
			if (!is_string($q))
2558
			{
2559
				return array();
2560
			} else {
2561
				$q_array = explode(" ", $q);
2562
				foreach ($q_array as $q_item){
2563
					$q_item = filter_var($q_item,FILTER_SANITIZE_STRING);
2564
					$additional_query .= " AND (";
2565
					if (is_int($q_item)) $additional_query .= "(marine_output.marine_id = '".$q_item."') OR ";
2566
					if (is_int($q_item)) $additional_query .= "(marine_output.mmsi = '".$q_item."') OR ";
2567
					if (is_int($q_item)) $additional_query .= "(marine_output.imo = '".$q_item."') OR ";
2568
					if (is_int($q_item)) $additional_query .= "(marine_output.captain_id = '".$q_item."') OR ";
2569
					if (is_int($q_item)) $additional_query .= "(marine_output.race_id = '".$q_item."') OR ";
2570
					if (!is_int($q_item)) $additional_query .= "(marine_output.captain_name = '".$q_item."') OR ";
2571
					if (!is_int($q_item)) $additional_query .= "(marine_output.race_name = '".$q_item."') OR ";
2572
					$additional_query .= "(marine_output.ident like '%".$q_item."%')";
2573
					$additional_query .= ")";
2574
				}
2575
			}
2576
		}
2577
		if ($callsign != "")
2578
		{
2579
			$callsign = filter_var($callsign,FILTER_SANITIZE_STRING);
2580
			if (!is_string($callsign))
2581
			{
2582
				return array();
2583
			} else {
2584
				$additional_query .= " AND marine_output.ident = :callsign";
2585
				$query_values = array_merge($query_values,array(':callsign' => $callsign));
2586
			}
2587
		}
2588
		if ($mmsi != "")
2589
		{
2590
			$mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING);
2591
			if (!is_numeric($mmsi))
2592
			{
2593
				return array();
2594
			} else {
2595
				$additional_query .= " AND marine_output.mmsi = :mmsi";
2596
				$query_values = array_merge($query_values,array(':mmsi' => $mmsi));
2597
			}
2598
		}
2599
		if ($imo != "")
2600
		{
2601
			$imo = filter_var($imo,FILTER_SANITIZE_STRING);
2602
			if (!is_numeric($imo))
2603
			{
2604
				return array();
2605
			} else {
2606
				$additional_query .= " AND marine_output.imo = :imo";
2607
				$query_values = array_merge($query_values,array(':imo' => $imo));
2608
			}
2609
		}
2610
		if ($captain_id != "")
2611
		{
2612
			$captain_id = filter_var($captain_id,FILTER_SANITIZE_STRING);
2613
			if (!is_numeric($captain_id))
2614
			{
2615
				return array();
2616
			} else {
2617
				$additional_query .= " AND marine_output.captain_id = :captain_id";
2618
				$query_values = array_merge($query_values,array(':captain_id' => $captain_id));
2619
			}
2620
		}
2621
		if ($race_id != "")
2622
		{
2623
			$race_id = filter_var($race_id,FILTER_SANITIZE_STRING);
2624
			if (!is_numeric($race_id))
2625
			{
2626
				return array();
2627
			} else {
2628
				$additional_query .= " AND marine_output.race_id = :race_id";
2629
				$query_values = array_merge($query_values,array(':race_id' => $race_id));
2630
			}
2631
		}
2632
		if ($captain_name != "")
2633
		{
2634
			$captain_name = filter_var($captain_name,FILTER_SANITIZE_STRING);
2635
			if (!is_string($captain_name))
2636
			{
2637
				return array();
2638
			} else {
2639
				$additional_query .= " AND marine_output.captain_name = :captain_name";
2640
				$query_values = array_merge($query_values,array(':captain_name' => $captain_name));
2641
			}
2642
		}
2643
		if ($race_name != "")
2644
		{
2645
			$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
2646
			if (!is_numeric($race_name))
2647
			{
2648
				return array();
2649
			} else {
2650
				$additional_query .= " AND marine_output.race_name = :race_name";
2651
				$query_values = array_merge($query_values,array(':race_name' => $race_name));
2652
			}
2653
		}
2654
		if ($date_posted != "")
2655
		{
2656
			$date_array = explode(",", $date_posted);
2657
			$date_array[0] = filter_var($date_array[0],FILTER_SANITIZE_STRING);
2658
			$date_array[1] = filter_var($date_array[1],FILTER_SANITIZE_STRING);
2659
			if ($globalTimezone != '') {
2660
				date_default_timezone_set($globalTimezone);
2661
				$datetime = new DateTime();
2662
				$offset = $datetime->format('P');
2663
			} else $offset = '+00:00';
2664
			if ($date_array[1] != "")
2665
			{
2666
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2667
				$date_array[1] = date("Y-m-d H:i:s", strtotime($date_array[1]));
2668
				if ($globalDBdriver == 'mysql') {
2669
					$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]."' ";
2670
				} else {
2671
					$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]."' ";
2672
				}
2673
			} else {
2674
				$date_array[0] = date("Y-m-d H:i:s", strtotime($date_array[0]));
2675
				if ($globalDBdriver == 'mysql') {
2676
					$additional_query .= " AND TIMESTAMP(CONVERT_TZ(marine_output.date,'+00:00', '".$offset."')) >= '".$date_array[0]."' ";
2677
				} else {
2678
					$additional_query .= " AND CAST(marine_output.date AT TIME ZONE INTERVAL ".$offset." AS TIMESTAMP) >= '".$date_array[0]."' ";
2679
				}
2680
			}
2681
		}
2682
		if ($limit != "")
2683
		{
2684
			$limit_array = explode(",", $limit);
2685
			$limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
2686
			$limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
2687
			if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
2688
			{
2689
				$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
2690
			} else $limit_query = "";
2691
		} else $limit_query = "";
2692
		if ($sort != "")
2693
		{
2694
			$search_orderby_array = $this->getOrderBy();
2695
			$orderby_query = $search_orderby_array[$sort]['sql'];
2696
		} else {
2697
			if ($origLat != "" && $origLon != "" && $dist != "") {
2698
				$orderby_query = " ORDER BY distance ASC";
2699
			} else {
2700
				$orderby_query = " ORDER BY marine_output.race_rank,marine_output.date DESC";
2701
			}
2702
		}
2703
		if ($origLat != "" && $origLon != "" && $dist != "") {
2704
			$dist = number_format($dist*0.621371,2,'.',''); // convert km to mile
2705
			if ($globalDBdriver == 'mysql') {
2706
				$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 
2707
				    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)) 
2708
				    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;
2709
			} else {
2710
				$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 
2711
				    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)) 
2712
				    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;
2713
			}
2714
		} else {
2715
			$query  = "SELECT marine_output.* FROM marine_output".$filter_query." marine_output.ident <> '' 
2716
			    ".$additional_query."
2717
			    ".$orderby_query;
2718
		}
2719
		$marine_array = $this->getDataFromDB($query, $query_values,$limit_query);
2720
		return $marine_array;
2721
	}
2722
2723
    /**
2724
     * Check marine by id
2725
     *
2726
     * @param $id
2727
     * @return String the ident
2728
     */
2729
	public function checkId($id)
2730
	{
2731
		$query  = 'SELECT marine_output.ident, marine_output.fammarine_id FROM marine_output WHERE marine_output.fammarine_id = :id';
2732
		$query_data = array(':id' => $id);
2733
		$sth = $this->db->prepare($query);
2734
		$sth->execute($query_data);
2735
		$ident_result='';
2736
		while($row = $sth->fetch(PDO::FETCH_ASSOC))
2737
		{
2738
			$ident_result = $row['fammarine_id'];
2739
		}
2740
		return $ident_result;
2741
	}
2742
2743
    /**
2744
     * Gets all info from a race
2745
     *
2746
     * @param $race_name
2747
     * @return array race
2748
     */
2749
	public function getRaceByName($race_name)
2750
	{
2751
		$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
2752
		$query  = "SELECT * FROM marine_race WHERE race_name = :race_name LIMIT 1";
2753
		$sth = $this->db->prepare($query);
2754
		$sth->execute(array(':race_name' => $race_name));
2755
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
2756
		if (isset($result[0])) return $result[0];
2757
		else return array();
2758
	}
2759
2760
    /**
2761
     * Gets all info from a race
2762
     *
2763
     * @param $race_id
2764
     * @return array race
2765
     */
2766
	public function getRace($race_id)
2767
	{
2768
		$race_id = filter_var($race_id,FILTER_SANITIZE_NUMBER_INT);
2769
		$query  = "SELECT * FROM marine_race WHERE race_id = :race_id LIMIT 1";
2770
		$sth = $this->db->prepare($query);
2771
		$sth->execute(array(':race_id' => $race_id));
2772
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
2773
		if (isset($result[0])) return $result[0];
2774
		else return array();
2775
	}
2776
2777
    /**
2778
     * Add race
2779
     * @param $race_id
2780
     * @param $race_name
2781
     * @param $race_creator
2782
     * @param $race_desc
2783
     * @param $race_startdate
2784
     * @param $race_markers
2785
     */
2786
	public function addRace($race_id,$race_name,$race_creator,$race_desc,$race_startdate,$race_markers)
2787
	{
2788
		$race_id = filter_var($race_id,FILTER_SANITIZE_NUMBER_INT);
2789
		if ($race_id != '') {
2790
			$race_name = filter_var($race_name,FILTER_SANITIZE_STRING);
2791
			$race_creator = filter_var($race_creator,FILTER_SANITIZE_STRING);
2792
			$race_desc = filter_var($race_desc,FILTER_SANITIZE_STRING);
2793
			$race_startdate = filter_var($race_startdate,FILTER_SANITIZE_STRING);
2794
			//$race_markers = filter_var($race_markers,FILTER_SANITIZE_STRING);
2795
			$allrace = $this->getRace($race_id);
2796
			if (empty($allrace)) {
2797
				$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)";
2798
				$sth = $this->db->prepare($query);
2799
				$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));
2800
			} elseif ($race_id != '') {
2801
				$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";
2802
				$sth = $this->db->prepare($query);
2803
				$sth->execute(array(':race_id' => $race_id,':race_name' => $race_name,':race_desc' => $race_desc,':race_startdate' => $race_startdate,':race_markers' => $race_markers));
2804
			}
2805
		}
2806
	}
2807
2808
2809
2810
	public function getOrderBy()
2811
	{
2812
		$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"));
2813
		return $orderby;
2814
	}
2815
    
2816
}
2817
?>
2818