Completed
Push — master ( e47bfe...1fed4c )
by Yannick
05:31
created

Accident   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 7 1
F getLatestAccidentData() 0 90 19
1
<?php
2
require_once(dirname(__FILE__).'/class.Connection.php');
3
require_once(dirname(__FILE__).'/class.Spotter.php');
4
//require_once(dirname(__FILE__).'/class.SpotterImport.php');
5
require_once(dirname(__FILE__).'/class.Image.php');
6
require_once(dirname(__FILE__).'/class.Scheduler.php');
7
require_once(dirname(__FILE__).'/class.Translation.php');
8
class Accident {
9
	public $db;
10
11
	public function __construct($dbc = null) {
12
		$Connection = new Connection($dbc);
13
		$this->db = $Connection->db();
14
	}
15
16
17
	public function get() {
18
		$query = 'SELECT DISTINCT registration FROM accidents ORDER BY date DESC';
19
		$sth = $this->db->prepare($query);
20
		$sth->execute();
21
		$result = $sth->fetchAll(PDO::FETCH_ASSOC);
22
		return $result;
23
	}
24
	
25
	/**
26
	* Get Latest ACARS data from DB
27
	*
28
	* @return Array Return ACARS data in array
29
	*/
30
	public function getLatestAccidentData($limit = '',$type = '') {
31
	global $globalURL, $globalDBdriver;
32
	$Image = new Image($this->db);
33
	$Spotter = new Spotter($this->db);
34
	$Translation = new Translation($this->db);
35
	date_default_timezone_set('UTC');
36
	$result = array();
37
	$limit_query = '';
38
	if ($limit != "")
39
	{
40
	    $limit_array = explode(",", $limit);
41
42
	    $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT);
43
	    $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT);
44
45
	    if ($limit_array[0] >= 0 && $limit_array[1] >= 0)
46
	    {
47
		$limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0];
48
	    }
49
	}
50
51
	if ($type != '') {
52
	    $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents WHERE type = :type GROUP BY registration) ORDER BY date DESC".$limit_query;
53
	    $query_values = array(':type' => $type);
54
	} else {
55
	    //$query = "SELECT * FROM accidents GROUP BY registration ORDER BY date DESC".$limit_query;
56
	    $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents GROUP BY registration) ORDER BY date DESC".$limit_query;
57
	    $query_values = array();
58
	}
59
	try {
60
61
	    $sth = $this->db->prepare($query);
62
	    $sth->execute($query_values);
63
	} catch(PDOException $e) {
64
	    return "error : ".$e->getMessage();
65
	}
66
	$i = 0;
67
	while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
68
	    $data = array();
69
	    if ($row['registration'] != '') {
70
//		$row['registration'] = str_replace('.','',$row['registration']);
71
		$image_array = $Image->getSpotterImage($row['registration']);
72
		if (count($image_array) > 0) $data = array_merge($data,array('image' => $image_array[0]['image'],'image_thumbnail' => $image_array[0]['image_thumbnail'],'image_copyright' => $image_array[0]['image_copyright'],'image_source' => $image_array[0]['image_source'],'image_source_website' => $image_array[0]['image_source_website']));
73
		else $data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => ''));
74
		$aircraft_type = $Spotter->getAllAircraftTypeByRegistration($row['registration']);
75
		$aircraft_info = $Spotter->getAllAircraftInfo($aircraft_type);
76
		//echo $row['registration'];
77
		//print_r($aircraft_info);
78
		if (!empty($aircraft_info)) {
79
			//echo 'ok!!!';
80
			$data['aircraft_type'] = $aircraft_info[0]['icao'];
81
			$data['aircraft_name'] = $aircraft_info[0]['type'];
82
			$data['aircraft_manufacturer'] = $aircraft_info[0]['manufacturer'];
83
		} else {
84
			$data = array_merge($data,array('aircraft_type' => 'NA'));
85
		}
86
		$owner_data = $Spotter->getAircraftOwnerByRegistration($row['registration']);
87
		if (!empty($owner_data)) {
88
			$data['aircraft_owner'] = $owner_data['owner'];
89
			$data['aircraft_base'] = $owner_data['base'];
90
			$data['aircraft_date_first_reg'] = $owner_data['date_first_reg'];
91
		}
92
	    } else $data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => ''));
93
	    if ($row['registration'] == '') $row['registration'] = 'NA';
94
	    if ($row['ident'] == '') $row['ident'] = 'NA';
95
	    $identicao = $Spotter->getAllAirlineInfo(substr($row['ident'],0,2));
96
	    if (isset($identicao[0])) {
97
		if (substr($row['ident'],0,2) == 'AF') {
98
		    if (filter_var(substr($row['ident'],2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) $icao = $row['ident'];
99
		    else $icao = 'AFR'.ltrim(substr($row['ident'],2),'0');
100
		} else $icao = $identicao[0]['icao'].ltrim(substr($row['ident'],2),'0');
101
102
		$data = array_merge($data,array('airline_icao' => $identicao[0]['icao'],'airline_name' => $identicao[0]['name']));
103
	    } else $icao = $row['ident'];
104
	    $icao = $Translation->checkTranslation($icao,false);
105
	    //$data = array_merge($data,array('registration' => $row['registration'], 'date' => $row['date'], 'ident' => $icao,'url' => $row['url']));
106
	    $data = array_merge($row,$data);
107
	    if ($data['ident'] == null) $data['ident'] = $icao;
108
	    if ($data['title'] == null) {
109
		$data['message'] = $row['type'].' of '.$row['registration'].' at '.$row['place'].','.$row['country'];
110
	    } else $data['message'] = strtolower($data['title']);
111
	    $result[] = $data;
112
	    $i++;
113
	}
114
	if (isset($result)) {
115
	    $result[0]['query_number_rows'] = $i;
116
	    return $result;
117
	}
118
	else return array();
119
    }
120
}
121
?>