ATC::deleteById()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * This class is part of FlightAirmap. It's used to support ATC (used by virtual airlines).
4
 *
5
 * Copyright (c) Ycarus (Yannick Chabanois) at Zugaina <[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
12
class ATC {
13
	public $db;
14
15
	/*
16
	 * Initialize DB connection
17
	*/
18
	public function __construct($dbc = null) {
19
		$Connection = new Connection($dbc);
20
		$this->db = $Connection->db;
21
		if ($this->db === null) die('Error: No DB connection. (ATC)');
22
	}
23
24
    /**
25
     * Get SQL query part for filter used
26
     * @param array $filter the filter
27
     * @param bool $where
28
     * @param bool $and
29
     * @return String the SQL part
30
     */
31
	public function getFilter($filter = array(),$where = false,$and = false) {
32
		global $globalFilter, $globalStatsFilters, $globalFilterName;
33
		if (is_array($globalStatsFilters) && isset($globalStatsFilters[$globalFilterName])) {
34
			if (isset($globalStatsFilters[$globalFilterName][0]['source'])) {
35
				foreach($globalStatsFilters[$globalFilterName] as $source) {
36
					if (isset($source['source'])) $filter['source'][] = $source['source'];
37
				}
38
			} else {
39
				$filter = $globalStatsFilters[$globalFilterName];
40
			}
41
		}
42
		if (is_array($globalFilter)) $filter = array_merge($filter,$globalFilter);
43
		$filter_query_join = '';
44
		$filter_query_where = '';
45
		if (isset($filter['source']) && !empty($filter['source'])) {
46
			$filter_query_where = " WHERE format_source IN ('".implode("','",$filter['source'])."')";
47
		}
48
		if ($filter_query_where == '' && $where) $filter_query_where = ' WHERE';
49
		elseif ($filter_query_where != '' && $and) $filter_query_where .= ' AND';
50
		$filter_query = $filter_query_join.$filter_query_where;
51
		return $filter_query;
52
	}
53
54
	/*
55
	 * Get all ATC from atc table
56
	 * @return array Return all ATC
57
     */
58
    public function getAll() {
59
		$filter_query = $this->getFilter(array());
60
		$query = "SELECT * FROM atc".$filter_query;
61
		$query_values = array();
62
		try {
63
			$sth = $this->db->prepare($query);
64
			$sth->execute($query_values);
65
		} catch(PDOException $e) {
66
			return "error : ".$e->getMessage();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'error : ' . $e->getMessage(); (string) is incompatible with the return type documented by ATC::getAll of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
		}
68
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
69
		return $all;
70
	}
71
72
	/*
73
	 * Get ATC from atc table by id
74
	 * @param Integer ATC id
75
	 * @return Array Return ATC
76
	*/
77
	public function getById($id) {
78
		$filter_query = $this->getFilter(array(),true,true);
79
		$query = "SELECT * FROM atc".$filter_query." atc_id = :id";
80
		$query_values = array(':id' => $id);
81
		try {
82
			$sth = $this->db->prepare($query);
83
			$sth->execute($query_values);
84
		} catch(PDOException $e) {
85
			return "error : ".$e->getMessage();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'error : ' . $e->getMessage(); (string) is incompatible with the return type documented by ATC::getById of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
86
		}
87
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
88
		return $all;
89
	}
90
91
	/*
92
	 * Get ATC from atc table by ident
93
	 * @param String $ident Flight ident
94
	 * @param String $format_source Format source
95
	 * @return Array Return ATC
96
	*/
97
	public function getByIdent($ident,$format_source = '') {
98
		$filter_query = $this->getFilter(array(),true,true);
99
		if ($format_source == '') {
100
			$query = "SELECT * FROM atc".$filter_query." ident = :ident";
101
			$query_values = array(':ident' => $ident);
102
		} else {
103
			$query = "SELECT * FROM atc".$filter_query." ident = :ident AND format_source = :format_source";
104
			$query_values = array(':ident' => $ident,':format_source' => $format_source);
105
		}
106
		try {
107
			$sth = $this->db->prepare($query);
108
			$sth->execute($query_values);
109
		} catch(PDOException $e) {
110
			return "error : ".$e->getMessage();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'error : ' . $e->getMessage(); (string) is incompatible with the return type documented by ATC::getByIdent of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
111
		}
112
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
113
		return $all;
114
	}
115
116
	/*
117
	 * Add ATC in atc table
118
	 * @param String $ident Flight ident
119
	 * @param String $frequency Frequency
120
	 * @param Float $latitude Latitude
121
	 * @param Float $longitude Longitude
122
	 * @param Float $range Range
123
	 * @param String $info ATC info
124
	 * @param String $date ATC date
125
	 * @param String $type ATC type
126
	 * @param Integer $ivao_id ATC IVAO id
127
	 * @param String $ivao_name ATC IVAO name
128
	 * @param String $format_source Format source
129
	 * @param String $source_name Source name
130
	*/
131
	public function add($ident,$frequency,$latitude,$longitude,$range,$info,$date,$type = '',$ivao_id = '',$ivao_name = '',$format_source = '',$source_name = '') {
132
		$info = preg_replace('/[^(\x20-\x7F)]*/','',$info);
133
		$info = str_replace('^','<br />',$info);
134
		$info = str_replace('&amp;sect;','',$info);
135
		$info = str_replace('"','',$info);
136
		if ($type == '') $type = NULL;
137
		$query = "INSERT INTO atc (ident,frequency,latitude,longitude,atc_range,info,atc_lastseen,type,ivao_id,ivao_name,format_source,source_name) VALUES (:ident,:frequency,:latitude,:longitude,:range,:info,:date,:type,:ivao_id,:ivao_name,:format_source,:source_name)";
138
		$query_values = array(':ident' => $ident,':frequency' => $frequency,':latitude' => $latitude,':longitude' => $longitude,':range' => $range,':info' => $info,':date' => $date,':ivao_id' => $ivao_id,':ivao_name' => $ivao_name, ':type' => $type,':format_source' => $format_source,':source_name' => $source_name);
139
		try {
140
			$sth = $this->db->prepare($query);
141
			$sth->execute($query_values);
142
		} catch(PDOException $e) {
143
			return "error : ".$e->getMessage();
144
		}
145
		return '';
146
	}
147
148
	/*
149
	 * Update in atc table
150
	 * @param String $ident Flight ident
151
	 * @param String $frequency Frequency
152
	 * @param Float $latitude Latitude
153
	 * @param Float $longitude Longitude
154
	 * @param Float $range Range
155
	 * @param String $info ATC info
156
	 * @param String $date ATC date
157
	 * @param String $type ATC type
158
	 * @param Integer $ivao_id ATC IVAO id
159
	 * @param String $ivao_name ATC IVAO name
160
	 * @param String $format_source Format source
161
	 * @param String $source_name Source name
162
	*/
163
	public function update($ident,$frequency,$latitude,$longitude,$range,$info,$date,$type = '',$ivao_id = '',$ivao_name = '',$format_source = '',$source_name = '') {
164
		$info = preg_replace('/[^(\x20-\x7F)]*/','',$info);
165
		$info = str_replace('^','<br />',$info);
166
		$info = str_replace('&amp;sect;','',$info);
167
		$info = str_replace('"','',$info);
168
		if ($type == '') $type = NULL;
169
		$query = "UPDATE atc SET frequency = :frequency,latitude = :latitude,longitude = :longitude,atc_range = :range,info = :info,atc_lastseen = :date,type = :type,ivao_id = :ivao_id,ivao_name = :ivao_name WHERE ident = :ident AND format_source = :format_source AND source_name = :source_name";
170
		$query_values = array(':ident' => $ident,':frequency' => $frequency,':latitude' => $latitude,':longitude' => $longitude,':range' => $range,':info' => $info,':date' => $date,':ivao_id' => $ivao_id,':ivao_name' => $ivao_name, ':type' => $type,':format_source' => $format_source,':source_name' => $source_name);
171
		try {
172
			$sth = $this->db->prepare($query);
173
			$sth->execute($query_values);
174
		} catch(PDOException $e) {
175
			return "error : ".$e->getMessage();
176
		}
177
		return '';
178
	}
179
180
	/*
181
	 * Delete ATC from atc table by id
182
	 * @param Integer ATC id
183
	*/
184
	public function deleteById($id) {
185
		$query = "DELETE FROM atc WHERE atc_id = :id";
186
		$query_values = array(':id' => $id);
187
		try {
188
			$sth = $this->db->prepare($query);
189
			$sth->execute($query_values);
190
		} catch(PDOException $e) {
191
			return "error : ".$e->getMessage();
192
		}
193
		return '';
194
	}
195
196
	/*
197
	 * Delete ATC from atc table by ident
198
	 * @param String $ident Flight ident
199
	 * @param String $format_source Format source
200
	*/
201
	public function deleteByIdent($ident,$format_source) {
202
		$query = "DELETE FROM atc WHERE ident = :ident AND format_source = :format_source";
203
		$query_values = array(':ident' => $ident,':format_source' => $format_source);
204
		try {
205
			$sth = $this->db->prepare($query);
206
			$sth->execute($query_values);
207
		} catch(PDOException $e) {
208
			return "error : ".$e->getMessage();
209
		}
210
		return '';
211
	}
212
213
	/*
214
	 * Delete all ATC from atc table
215
	*/
216
	public function deleteAll() {
217
		$query = "DELETE FROM atc";
218
		$query_values = array();
219
		try {
220
			$sth = $this->db->prepare($query);
221
			$sth->execute($query_values);
222
		} catch(PDOException $e) {
223
			return "error : ".$e->getMessage();
224
		}
225
		return '';
226
	}
227
228
	/*
229
	 * Delete ATC from atc table older than 1 hour
230
	*/
231
	public function deleteOldATC() {
232
		global $globalDBdriver;
233
		if ($globalDBdriver == 'mysql') {
234
			$query  = "DELETE FROM atc WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 HOUR) >= atc.atc_lastseen";
235
		} else {
236
			$query  = "DELETE FROM atc WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 HOUR' >= atc.atc_lastseen";
237
		}
238
		try {
239
			$sth = $this->db->prepare($query);
240
			$sth->execute();
241
		} catch(PDOException $e) {
242
			return "error";
243
		}
244
		return "success";
245
	}
246
}
247
?>