Completed
Push — master ( 247ee0...bc6eda )
by Yannick
17:03
created

Source::deleteOldLocationByType()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 6
nop 1
dl 0
loc 15
rs 9.4285
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
	}
11
12
	public function getAllLocationInfo() {
13
		$query = "SELECT * FROM source_location";
14
		$query_values = array();
15
		try {
16
			$sth = $this->db->prepare($query);
17
			$sth->execute($query_values);
18
		} catch(PDOException $e) {
19
			return "error : ".$e->getMessage();
20
		}
21
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
22
		return $all;
23
	}
24
25
	public function getLocationInfobyName($name) {
26
		$query = "SELECT * FROM source_location WHERE name = :name";
27
		$query_values = array(':name' => $name);
28
		try {
29
			$sth = $this->db->prepare($query);
30
			$sth->execute($query_values);
31
		} catch(PDOException $e) {
32
			return "error : ".$e->getMessage();
33
		}
34
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
35
		return $all;
36
	}
37
38
	public function getLocationInfobySourceName($name) {
39
		$query = "SELECT * FROM source_location WHERE source = :name";
40
		$query_values = array(':name' => $name);
41
		try {
42
			$sth = $this->db->prepare($query);
43
			$sth->execute($query_values);
44
		} catch(PDOException $e) {
45
			return "error : ".$e->getMessage();
46
		}
47
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
48
		return $all;
49
	}
50
51
	public function getLocationInfoByType($type) {
52
		$query = "SELECT * FROM source_location WHERE type = :type";
53
		$query_values = array(':type' => $type);
54
		try {
55
			$sth = $this->db->prepare($query);
56
			$sth->execute($query_values);
57
		} catch(PDOException $e) {
58
			return "error : ".$e->getMessage();
59
		}
60
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
61
		return $all;
62
	}
63
64
	public function getLocationInfoByLocationID($location_id) {
65
		$query = "SELECT * FROM source_location WHERE location_id = :location_id";
66
		$query_values = array(':location_id' => $location_id);
67
		try {
68
			$sth = $this->db->prepare($query);
69
			$sth->execute($query_values);
70
		} catch(PDOException $e) {
71
			return "error : ".$e->getMessage();
72
		}
73
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
74
		return $all;
75
	}
76
77
	public function getLocationInfoByID($id) {
78
		$query = "SELECT * FROM source_location WHERE id = :id";
79
		$query_values = array(':id' => $id);
80
		try {
81
			$sth = $this->db->prepare($query);
82
			$sth->execute($query_values);
83
		} catch(PDOException $e) {
84
			return "error : ".$e->getMessage();
85
		}
86
		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
87
		return $all;
88
	}
89
90
	public function addLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '') {
91
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
92
		$query = "INSERT INTO source_location (name,latitude,longitude,altitude,country,city,logo,source,type,source_id,last_seen,location_id) VALUES (:name,:latitude,:longitude,:altitude,:country,:city,:logo,:source,:type,:source_id,:last_seen,:location_id)";
93
		$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);
94
		try {
95
			$sth = $this->db->prepare($query);
96
			$sth->execute($query_values);
97
		} catch(PDOException $e) {
98
			echo "error : ".$e->getMessage();
99
		}
100
	}
101
102
	public function updateLocation($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0,$location_id = 0,$last_seen = '') {
103
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
104
		$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 WHERE name = :name AND source = :source";
105
		$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);
106
		try {
107
			$sth = $this->db->prepare($query);
108
			$sth->execute($query_values);
109
		} catch(PDOException $e) {
110
			return "error : ".$e->getMessage();
111
		}
112
	}
113
114
	public function updateLocationByLocationID($name,$latitude,$longitude,$altitude,$city,$country,$source,$logo = 'antenna.png',$type = '',$source_id = 0, $location_id,$last_seen = '') {
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...
115
		if ($last_seen == '') $last_seen = date('Y-m-d H:i:s');
116
		$query = "UPDATE source_location SET latitude = :latitude,longitude = :longitude,altitude = :altitude,country = :country,city = :city,logo = :logo,type = :type, last_seen = :last_seen WHERE location_id = :location_id AND source = :source AND source_id = :source_id";
117
		$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);
118
		try {
119
			$sth = $this->db->prepare($query);
120
			$sth->execute($query_values);
121
		} catch(PDOException $e) {
122
			echo "error : ".$e->getMessage();
123
		}
124
	}
125
126
	public function deleteLocation($id) {
127
		$query = "DELETE FROM source_location WHERE id = :id";
128
		$query_values = array(':id' => $id);
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 deleteLocationByType($type) {
138
		$query = "DELETE FROM source_location WHERE type = :type";
139
		$query_values = array(':type' => $type);
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 deleteAllLocation() {
149
		$query = "DELETE FROM source_location";
150
		try {
151
			$sth = $this->db->prepare($query);
152
			$sth->execute();
153
		} catch(PDOException $e) {
154
			return "error : ".$e->getMessage();
155
		}
156
	}
157
158
	public function deleteOldLocationByType($type) {
159
		global $globalDBdriver;
160
		if ($globalDBdriver == 'mysql') {
161
			$query  = "DELETE FROM source_location WHERE DATE_SUB(UTC_TIMESTAMP(),INTERVAL 1 WEEK) >= source_location.last_seen AND type = :type";
162
		} else {
163
			$query  = "DELETE FROM source_location WHERE NOW() AT TIME ZONE 'UTC' - INTERVAL '1 WEEK' >= source_location.last_seen AND type = :type";
164
		}
165
		try {
166
			$sth = $this->db->prepare($query);
167
			$sth->execute(array(':type' => $type));
168
		} catch(PDOException $e) {
169
			return "error";
170
		}
171
		return "success";
172
	}
173
}
174
?>