Completed
Push — master ( e17801...597a61 )
by Yannick
25:55
created

Translation::ident2icao()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 7
nop 1
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/settings.php');
3
require_once(dirname(__FILE__).'/class.Connection.php');
4
require_once(dirname(__FILE__).'/class.Spotter.php');
5
require_once(dirname(__FILE__).'/class.Common.php');
6
require_once(dirname(__FILE__).'/libs/uagent/uagent.php');
7
8
9
class Translation {
10
	public $db;
11
	public function __construct($dbc = null) {
12
		$Connection = new Connection($dbc);
13
		$this->db = $Connection->db();
14
	}
15
16
17
	/**
18
	* Change IATA to ICAO value for ident
19
	* 
20
	* @param String $ident ident
21
	* @return String the icao
22
	*/
23
	public function ident2icao($ident) {
24
		$Spotter = new Spotter();
25
		if (!is_numeric(substr($ident, 0, 3))) {
26
			if (is_numeric(substr(substr($ident, 0, 3), -1, 1))) {
27
				$airline_icao = substr($ident, 0, 2);
28
			} elseif (is_numeric(substr(substr($ident, 0, 4), -1, 1))) {
29
				//$airline_icao = substr($ident, 0, 3);
30
				return $ident;
31
			} else return $ident;
32
		} else return $ident;
33
		if ($airline_icao == 'AF') {
34
			if (filter_var(substr($ident,2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) $icao = $ident;
35
			else $icao = 'AFR'.ltrim(substr($ident,2),'0');
36
		} else {
37
			$identicao = $Spotter->getAllAirlineInfo($airline_icao);
38
			if (isset($identicao[0])) {
39
				$icao = $identicao[0]['icao'].ltrim(substr($ident,2),'0');
40
			} else $icao = $ident;
41
		}
42
		return $icao;
43
	}
44
45
	public function getOperator($ident) {
46
		$query = "SELECT * FROM translation WHERE Operator = :ident LIMIT 1";
47
		$query_values = array(':ident' => $ident);
48
		try {
49
			$sth = $this->db->prepare($query);
50
			$sth->execute($query_values);
51
		} catch(PDOException $e) {
52
			return "error : ".$e->getMessage();
53
		}
54
		$row = $sth->fetch(PDO::FETCH_ASSOC);
55
		$sth->closeCursor();
56
		if (count($row) > 0) {
57
			return $row['operator_correct'];
58
		} else return $ident;
59
	}
60
61
	public function addOperator($ident,$correct_ident,$source) {
62
		$query = "INSERT INTO translation (Operator,Operator_correct,Source) VALUES (:ident,:correct_ident,:source)";
63
		$query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source);
64
		try {
65
			$sth = $this->db->prepare($query);
66
			$sth->execute($query_values);
67
		} catch(PDOException $e) {
68
			return "error : ".$e->getMessage();
69
		}
70
	}
71
72
	public function updateOperator($ident,$correct_ident,$source) {
73
		$query = "UPDATE translation SET Operator_correct = :correct_ident,Source = :source WHERE Operator = :ident";
74
		$query_values = array(':ident' => $ident,':correct_ident' => $correct_ident, ':source' => $source);
75
		try {
76
			$sth = $this->db->prepare($query);
77
			$sth->execute($query_values);
78
		} catch(PDOException $e) {
79
			return "error : ".$e->getMessage();
80
		}
81
	}
82
83
	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...
84
		global $globalTranslationSources, $globalTranslationFetch;
85
		//if (!isset($globalTranslationSources)) $globalTranslationSources = array('planefinder');
86
		$globalTranslationSources = array();
87
		if (!isset($globalTranslationFetch)) $globalTranslationFetch = TRUE;
88
		//echo "Check Translation for ".$ident."...";
89
		$correct = $this->getOperator($ident);
90
		if ($correct != '' && $correct != $ident) {
91
			//echo "Found in DB !\n";
92
			return $correct;
93
		}
94
		 /*
95
    	    elseif ($web && $globalTranslationFetch) {
96
    		if (! is_numeric(substr($ident,-4))) {
97
    		    if (count($globalTranslationSources) > 0) {
98
    			$correct = $this->fromPlanefinder($ident);
99
    			if ($correct != '') {
100
    			    $correct = $this->ident2icao($correct);
101
    			    if ($correct != $ident) {
102
    				$this->addOperator($ident,$correct,'planefinder');
103
    				//echo "Add to DB ! (".$correct.") \n";
104
    				return $correct;
105
    			    }
106
    			}
107
    		    }
108
    		}
109
    	    }
110
    	    */
111
		return $this->ident2icao($ident);
112
	}
113
114
/*  
115
    function fromPlanefinder($icao) {
116
	$url = 'http://planefinder.net/data/endpoints/search_ajax.php?searchText='.$icao;
117
	$Common = new Common();
118
	$json = $Common->getData($url);
119
	$parsed_json = json_decode($json);
120
	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;
121
	else return '';
122
    }
123
*/
124
}
125
//echo Translation->checkTranslation('EZY268X');
126
//Translation->fromPlanefinder('EZY268X');
127
?>