Translation::ident2icao()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 1
dl 0
loc 21
rs 8.6506
c 0
b 0
f 0
1
<?php
2
/**
3
 * This class is part of FlightAirmap. It's used to do flight ident translation
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__).'/settings.php');
10
require_once(dirname(__FILE__).'/class.Connection.php');
11
require_once(dirname(__FILE__).'/class.Spotter.php');
12
require_once(dirname(__FILE__).'/class.Common.php');
13
require_once(dirname(__FILE__).'/libs/uagent/uagent.php');
14
15
16
class Translation {
17
	public $db;
18
	
19
	/*
20
	* Initialize DB connection
21
	*/
22
	public function __construct($dbc = null) {
23
		$Connection = new Connection($dbc);
24
		$this->db = $Connection->db();
25
	}
26
27
	/**
28
	* Change IATA to ICAO value for ident
29
	* 
30
	* @param string $ident ident
31
	* @return string the icao
32
	*/
33
	public function ident2icao($ident) {
34
		$Spotter = new Spotter();
35
		if (!is_numeric(substr($ident, 0, 3))) {
36
			if (is_numeric(substr(substr($ident, 0, 3), -1, 1))) {
37
				$airline_icao = substr($ident, 0, 2);
38
			} elseif (is_numeric(substr(substr($ident, 0, 4), -1, 1))) {
39
				//$airline_icao = substr($ident, 0, 3);
40
				return $ident;
41
			} else return $ident;
42
		} else return $ident;
43
		if ($airline_icao == 'AF') {
44
			if (filter_var(substr($ident,2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) $icao = $ident;
45
			else $icao = 'AFR'.ltrim(substr($ident,2),'0');
46
		} else {
47
			$identicao = $Spotter->getAllAirlineInfo($airline_icao);
48
			if (isset($identicao[0])) {
49
				$icao = $identicao[0]['icao'].ltrim(substr($ident,2),'0');
50
			} else $icao = $ident;
51
		}
52
		return $icao;
53
	}
54
55
    /**
56
     * @param $ident
57
     * @return string
58
     */
59
    public function getOperator($ident) {
60
		$query = "SELECT * FROM translation WHERE Operator = :ident LIMIT 1";
61
		$query_values = array(':ident' => $ident);
62
		try {
63
			$sth = $this->db->prepare($query);
64
			$sth->execute($query_values);
65
		} catch(PDOException $e) {
66
			return "error : ".$e->getMessage();
67
		}
68
		$row = $sth->fetch(PDO::FETCH_ASSOC);
69
		$sth->closeCursor();
70
		if (isset($row['operator_correct'])) {
71
			return $row['operator_correct'];
72
		} else return $ident;
73
	}
74
75
    /**
76
     * @param $ident
77
     * @param $correct_ident
78
     * @param $source
79
     * @return string
80
     */
81
    public function addOperator($ident, $correct_ident, $source) {
82
		$query = "INSERT INTO translation (Operator,Operator_correct,Source) VALUES (:ident,:correct_ident,:source)";
83
		$query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source);
84
		try {
85
			$sth = $this->db->prepare($query);
86
			$sth->execute($query_values);
87
		} catch(PDOException $e) {
88
			return "error : ".$e->getMessage();
89
		}
90
	}
91
92
    /**
93
     * @param $ident
94
     * @param $correct_ident
95
     * @param $source
96
     * @return string
97
     */
98
    public function updateOperator($ident, $correct_ident, $source) {
99
		$query = "UPDATE translation SET Operator_correct = :correct_ident,Source = :source WHERE Operator = :ident";
100
		$query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source);
101
		try {
102
			$sth = $this->db->prepare($query);
103
			$sth->execute($query_values);
104
		} catch(PDOException $e) {
105
			return "error : ".$e->getMessage();
106
		}
107
	}
108
109
    /**
110
     * @param $ident
111
     * @param bool $web
112
     * @return string
113
     */
114
    public function checkTranslation($ident, $web = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $web 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...
115
		global $globalTranslationSources, $globalTranslationFetch;
116
		//if (!isset($globalTranslationSources)) $globalTranslationSources = array('planefinder');
117
		$globalTranslationSources = array();
118
		if (!isset($globalTranslationFetch)) $globalTranslationFetch = TRUE;
119
		//echo "Check Translation for ".$ident."...";
120
		$correct = $this->getOperator($ident);
121
		if ($correct != '' && $correct != $ident) {
122
			//echo "Found in DB !\n";
123
			return $correct;
124
		}
125
		/*
126
		elseif ($web && $globalTranslationFetch) {
127
			if (! is_numeric(substr($ident,-4))) {
128
				if (count($globalTranslationSources) > 0) {
129
					$correct = $this->fromPlanefinder($ident);
130
					if ($correct != '') {
131
						$correct = $this->ident2icao($correct);
132
						if ($correct != $ident) {
133
							$this->addOperator($ident,$correct,'planefinder');
134
							//echo "Add to DB ! (".$correct.") \n";
135
							return $correct;
136
						}
137
					}
138
				}
139
			}
140
		}
141
		*/
142
		return $this->ident2icao($ident);
143
	}
144
145
/*  
146
	// Disabled as Planefinder request
147
	function fromPlanefinder($icao) {
148
		$url = 'http://planefinder.net/data/endpoints/search_ajax.php?searchText='.$icao;
149
		$Common = new Common();
150
		$json = $Common->getData($url);
151
		$parsed_json = json_decode($json);
152
		if (isset($parsed_json->flights[0]->title) && isset($parsed_json->flights[0]->subtitle) && $parsed_json->flights[0]->subtitle == $icao) return $parsed_json->flights[0]->title;
153
		else return '';
154
	}
155
*/
156
}
157
//echo Translation->checkTranslation('EZY268X');
158
//Translation->fromPlanefinder('EZY268X');
159
?>