Completed
Push — master ( 83b404...19202b )
by Yannick
29:38
created

Source::getLocationInfoByType()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 12
nop 3
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/settings.php');
3
require_once(dirname(__FILE__).'/class.Connection.php');
4
5
class Source {
6
	public $db;
7
	public function __construct($dbc = null) {
8
		$Connection = new Connection($dbc);
9
		$this->db = $Connection->db;
10
		if ($this->db === null) die('Error: No DB connection.');
11
	}
12
13
	public function getAllLocationInfo() {
14
		$query = "SELECT * FROM source_location";
15
		$query_values = array();
16
		try {
17
			$sth = $this->db->prepare($query);
18
			$sth->execute($query_values);
19
		} catch(PDOException $e) {
20
			return "error : ".$e->getMessage();
21
		}
22
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
23
		return $all;
24
	}
25
26
	public function getLocationInfobyName($name) {
27
		$query = "SELECT * FROM source_location WHERE name = :name";
28
		$query_values = array(':name' => $name);
29
		try {
30
			$sth = $this->db->prepare($query);
31
			$sth->execute($query_values);
32
		} catch(PDOException $e) {
33
			return "error : ".$e->getMessage();
34
		}
35
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
36
		return $all;
37
	}
38
39
	public function getLocationInfobyNameType($name,$type) {
40
		$query = "SELECT * FROM source_location WHERE name = :name AND type = :type";
41
		$query_values = array(':name' => $name,':type' => $type);
42
		try {
43
			$sth = $this->db->prepare($query);
44
			$sth->execute($query_values);
45
		} catch(PDOException $e) {
46
			return "error : ".$e->getMessage();
47
		}
48
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
49
		return $all;
50
	}
51
52
	public function getLocationInfobySourceName($name) {
53
		$query = "SELECT * FROM source_location WHERE source = :name";
54
		$query_values = array(':name' => $name);
55
		try {
56
			$sth = $this->db->prepare($query);
57
			$sth->execute($query_values);
58
		} catch(PDOException $e) {
59
			return "error : ".$e->getMessage();
60
		}
61
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
62
		return $all;
63
	}
64
65
	public function getLocationInfoByType($type, $coord = array(), $limit = false) {
66
		$query = "SELECT * FROM source_location WHERE type = :type";
67
		if (is_array($coord) && !empty($coord)) {
68
			$minlong = filter_var($coord[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
69
			$minlat = filter_var($coord[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
70
			$maxlong = filter_var($coord[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
71
			$maxlat = filter_var($coord[3],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
72
			$query .= " AND source_location.latitude BETWEEN ".$minlat." AND ".$maxlat." AND source_location.longitude BETWEEN ".$minlong." AND ".$maxlong." AND source_location.latitude <> 0 AND source_location.longitude <> 0";
73
		}
74
		$query .= " ORDER BY last_seen DESC";
75
		if ($limit) $query .= " LIMIT 200";
76
		$query_values = array(':type' => $type);
77
		try {
78
			$sth = $this->db->prepare($query);
79
			$sth->execute($query_values);
80
		} catch(PDOException $e) {
81
			return "error : ".$e->getMessage();
82
		}
83
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
84
		return $all;
85
	}
86
87
	public function getLocationInfoByLocationID($location_id) {
88
		$query = "SELECT * FROM source_location WHERE location_id = :location_id";
89
		$query_values = array(':location_id' => $location_id);
90
		try {
91
			$sth = $this->db->prepare($query);
92
			$sth->execute($query_values);
93
		} catch(PDOException $e) {
94
			return "error : ".$e->getMessage();
95
		}
96
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
97
		return $all;
98
	}
99
100
	public function getLocationInfoByID($id) {
101
		$query = "SELECT * FROM source_location WHERE id = :id";
102
		$query_values = array(':id' => $id);
103
		try {
104
			$sth = $this->db->prepare($query);
105
			$sth->execute($query_values);
106
		} catch(PDOException $e) {
107
			return "error : ".$e->getMessage();
108
		}
109
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
110
		return $all;
111
	}
112
113
	public function addLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '', $description = '') {
114
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
115
		$query = "INSERT INTO source_location (name,latitude,longitude,altitude,country,city,logo,source,type,source_id,last_seen,location_id,description) VALUES (:name,:latitude,:longitude,:altitude,:country,:city,:logo,:source,:type,:source_id,:last_seen,:location_id,:description)";
116
		$query_values = array(':name' => $name,':latitude' => $latitude, ':longitude' => $longitude,':altitude' => $altitude,':city' => $city,':country' => $country,':logo' => $logo,':source' => $source,':type' => $type,':source_id' => $source_id,':last_seen' => $last_seen,':location_id' => $location_id,':description' => $description);
117
		try {
118
			$sth = $this->db->prepare($query);
119
			$sth->execute($query_values);
120
		} catch(PDOException $e) {
121
			echo "error : ".$e->getMessage();
122
		}
123
	}
124
125
	public function updateLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '',$description = '') {
126
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
127
		$query = "UPDATE source_location SET latitude = :latitude,longitude = :longitude,altitude = :altitude,country = :country,city = :city,logo = :logo,type = :type, source_id = :source_id, last_seen = :last_seen,location_id = :location_id, description = :description WHERE name = :name AND source = :source";
128
		$query_values = array(':name' => $name,':latitude' => $latitude, ':longitude' => $longitude,':altitude' => $altitude,':city' => $city,':country' => $country,':logo' => $logo,':source' => $source,':type' => $type,':source_id' => $source_id,':last_seen' => $last_seen,':location_id' => $location_id,':description' => $description);
129
		try {
130
			$sth = $this->db->prepare($query);
131
			$sth->execute($query_values);
132
		} catch(PDOException $e) {
133
			return "error : ".$e->getMessage();
134
		}
135
	}
136
137
	public function updateLocationDescByName($name,$source,$source_id = 0,$description = '') {
138
		$query = "UPDATE source_location SET description = :description WHERE source_id = :source_id AND name = :name AND source = :source";
139
		$query_values = array(':name' => $name,':source' => $source,':source_id' => $source_id,':description' => $description);
140
		try {
141
			$sth = $this->db->prepare($query);
142
			$sth->execute($query_values);
143
		} catch(PDOException $e) {
144
			return "error : ".$e->getMessage();
145
		}
146
	}
147
148
	public function updateLocationByLocationID($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0, $location_id,$last_seen = '',$description = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $name 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...
149
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
150
		$query = "UPDATE source_location SET latitude = :latitude,longitude = :longitude,altitude = :altitude,country = :country,city = :city,logo = :logo,type = :type, last_seen = :last_seen, description = :description WHERE location_id = :location_id AND source = :source AND source_id = :source_id";
151
		$query_values = array(':source_id' => $source_id,':latitude' => $latitude, ':longitude' => $longitude,':altitude' => $altitude,':city' => $city,':country' => $country,':logo' => $logo,':source' => $source,':type' => $type,':last_seen' => $last_seen,':location_id' => $location_id,':description' => $description);
152
		try {
153
			$sth = $this->db->prepare($query);
154
			$sth->execute($query_values);
155
		} catch(PDOException $e) {
156
			echo "error : ".$e->getMessage();
157
		}
158
	}
159
160
	public function deleteLocation($id) {
161
		$query = "DELETE FROM source_location WHERE id = :id";
162
		$query_values = array(':id' => $id);
163
		try {
164
			$sth = $this->db->prepare($query);
165
			$sth->execute($query_values);
166
		} catch(PDOException $e) {
167
			return "error : ".$e->getMessage();
168
		}
169
	}
170
171
	public function deleteLocationByType($type) {
172
		$query = "DELETE FROM source_location WHERE type = :type";
173
		$query_values = array(':type' => $type);
174
		try {
175
			$sth = $this->db->prepare($query);
176
			$sth->execute($query_values);
177
		} catch(PDOException $e) {
178
			return "error : ".$e->getMessage();
179
		}
180
	}
181
182
	public function deleteLocationBySource($source) {
183
		$query = "DELETE FROM source_location WHERE source = :source";
184
		$query_values = array(':source' => $source);
185
		try {
186
			$sth = $this->db->prepare($query);
187
			$sth->execute($query_values);
188
		} catch(PDOException $e) {
189
			return "error : ".$e->getMessage();
190
		}
191
	}
192
193
	public function deleteAllLocation() {
194
		$query = "DELETE FROM source_location";
195
		try {
196
			$sth = $this->db->prepare($query);
197
			$sth->execute();
198
		} catch(PDOException $e) {
199
			return "error : ".$e->getMessage();
200
		}
201
	}
202
203
	public function deleteOldLocationByType($type) {
204
		global $globalDBdriver;
205
		if ($type == 'wx') {
206
			if ($globalDBdriver == 'mysql') {
207
				$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 DAY) >= source_location.last_seen AND type = :type";
208
			} else {
209
				$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 DAY' >= source_location.last_seen AND type = :type";
210
			}
211
		} elseif ($type == 'lightning') {
212
			if ($globalDBdriver == 'mysql') {
213
				$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 20 MINUTE) >= source_location.last_seen AND type = :type";
214
			} else {
215
				$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '20 MINUTE' >= source_location.last_seen AND type = :type";
216
			}
217
		} else {
218
			if ($globalDBdriver == 'mysql') {
219
				$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 WEEK) >= source_location.last_seen AND type = :type";
220
			} else {
221
				$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 WEEK' >= source_location.last_seen AND type = :type";
222
			}
223
		}
224
		try {
225
			$sth = $this->db->prepare($query);
226
			$sth->execute(array(':type' => $type));
227
		} catch(PDOException $e) {
228
			return "error";
229
		}
230
		return "success";
231
	}
232
}
233
?>