Subversion Repositories eFlore/Applications.cel

Rev

Rev 2541 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2541 Rev 2545
1
<?php
1
<?php
2
class Dao extends Bdd {
2
class Dao extends Bdd {
3
 
3
 
4
	private $temps_derniere_requete = 0;
4
	private $temps_derniere_requete = 0;
5
 
5
 
6
	public function rechercherCoordonnees($conditions = array()) {
6
	public function rechercherCoordonnees($conditions = array()) {
7
		if(!empty($conditions)) {
7
		if(!empty($conditions)) {
8
			$where = 'WHERE '.implode(' AND ', $conditions);		
8
			$where = 'WHERE '.implode(' AND ', $conditions);		
9
		}
9
		}
10
		
10
		
11
		$requete = "SELECT longitude, latitude ".
11
		$requete = "SELECT longitude, latitude ".
12
			"FROM cel_obs ".
12
			"FROM cel_obs ".
13
			$where.
13
			$where.
14
			"GROUP BY longitude , latitude ";
14
			"GROUP BY longitude , latitude ";
15
		
15
		
16
		$resultat = $this->recupererTous($requete);
16
		$resultat = $this->recupererTous($requete);
17
		echo $requete;exit;
-
 
18
		$this->reinitialiserTempsDerniereRequete();
17
		$this->reinitialiserTempsDerniereRequete();
19
		return $resultat;
18
		return $resultat;
20
	}
19
	}
21
 
20
 
22
	public function rechercherCoordonneesFrancaisesSansCorrespondances() {	
21
	public function rechercherCoordonneesFrancaisesSansCorrespondances() {	
23
		$conditions = array(
22
		$conditions = array(
24
						$this->getConditionCoordonneesValides(),
23
						$this->getConditionCoordonneesValides(),
25
						$this->getConditionInfosGeoIncompletes(),
24
						$this->getConditionInfosGeoIncompletes(),
26
						$this->getConditionlimiteeALaFrance(),
25
						$this->getConditionlimiteeALaFrance(),
27
						$this->getConditionModifObsRecente()
26
						$this->getConditionModifObsRecente()
28
					);
27
					);
29
		
28
		
30
		return $this->rechercherCoordonnees($conditions);
29
		return $this->rechercherCoordonnees($conditions);
31
	}
30
	}
32
	
31
	
33
	public function rechercherCoordonneesFrancaisesSansCorrespondanceDepuisLeDebut() {
32
	public function rechercherCoordonneesFrancaisesSansCorrespondanceDepuisLeDebut() {
34
		$conditions = array(
33
		$conditions = array(
35
						$this->getConditionCoordonneesValides(),
34
						$this->getConditionCoordonneesValides(),
36
						$this->getConditionInfosGeoIncompletes(),
35
						$this->getConditionInfosGeoIncompletes(),
37
						$this->getConditionlimiteeALaFrance()
36
						$this->getConditionlimiteeALaFrance()
38
					);
37
					);
39
		
38
		
40
		return $this->rechercherCoordonnees($conditions);
39
		return $this->rechercherCoordonnees($conditions);
41
	}
40
	}
42
	
41
	
43
	public function rechercherPaysSansCorrespondance() {
42
	public function rechercherPaysSansCorrespondance() {
44
		$conditions = array(
43
		$conditions = array(
45
				$this->getConditionCoordonneesValides(),
44
				$this->getConditionCoordonneesValides(),
46
				$this->getConditionSansPays(),
45
				$this->getConditionSansPays(),
47
				$this->getConditionModifObsRecente()
46
				$this->getConditionModifObsRecente()
48
		);
47
		);
49
		
48
		
50
		return $this->rechercherCoordonnees($conditions);
49
		return $this->rechercherCoordonnees($conditions);
51
	}
50
	}
52
	
51
	
53
	public function rechercherPaysSansCorrespondanceDepuisLeDebut() {
52
	public function rechercherPaysSansCorrespondanceDepuisLeDebut() {
54
		$conditions = array(
53
		$conditions = array(
55
				$this->getConditionCoordonneesValides(),
54
				$this->getConditionCoordonneesValides(),
56
				$this->getConditionSansPays()
55
				$this->getConditionSansPays()
57
		);
56
		);
58
		
57
		
59
		return $this->rechercherCoordonnees($conditions);
58
		return $this->rechercherCoordonnees($conditions);
60
	}
59
	}
61
	
60
	
62
	public function rechercherToutSansCorrespondance() {
61
	public function rechercherToutSansCorrespondance() {
63
		$conditions = array(
62
		$conditions = array(
64
				$this->getConditionCoordonneesValides(),
63
				$this->getConditionCoordonneesValides(),
65
				'('.
64
				'('.
66
					$this->getConditionInfosGeoIncompletes().' OR '.
65
					$this->getConditionInfosGeoIncompletes().' OR '.
67
					$this->getConditionSansPays().
66
					$this->getConditionSansPays().
68
				') ',
67
				') ',
69
				$this->getConditionModifObsRecente()
68
				$this->getConditionModifObsRecente()
70
		);
69
		);
71
		
70
		
72
		return $this->rechercherCoordonnees($conditions);
71
		return $this->rechercherCoordonnees($conditions);
73
	}
72
	}
74
	
73
	
75
	public function rechercherSansCorrespondanceDepuisLeDebut() {
74
	public function rechercherSansCorrespondanceDepuisLeDebut() {
76
		$conditions = array(
75
		$conditions = array(
77
				$this->getConditionCoordonneesValides(),
76
				$this->getConditionCoordonneesValides(),
78
				'('.
77
				'('.
79
				$this->getConditionInfosGeoIncompletes().' OR '.
78
				$this->getConditionInfosGeoIncompletes().' OR '.
80
				$this->getConditionSansPays().
79
				$this->getConditionSansPays().
81
				') '
80
				') '
82
		);
81
		);
83
		return $this->rechercherCoordonnees($conditions);
82
		return $this->rechercherCoordonnees($conditions);
84
	}
83
	}
85
	
84
	
86
	
85
	
87
	private function getConditionModifObsRecente() {
86
	private function getConditionModifObsRecente() {
88
		$condition = 'DATE_ADD(date_modification, INTERVAL 25 HOUR) >= CURDATE() ';
87
		$condition = 'DATE_ADD(date_modification, INTERVAL 25 HOUR) >= CURDATE() ';
89
		return $condition;
88
		return $condition;
90
	}
89
	}
91
	
90
	
92
	private function getConditionInfosGeoIncompletes() {
91
	private function getConditionInfosGeoIncompletes() {
93
		$condition = '('.
92
		$condition = '('.
94
						'ce_zone_geo IS NULL OR ce_zone_geo = "" OR '.
93
						'ce_zone_geo IS NULL OR ce_zone_geo = "" OR '.
95
						'zone_geo IS NULL OR zone_geo = "" '.
94
						'zone_geo IS NULL OR zone_geo = "" '.
96
					 ') ';
95
					 ') ';
97
		return $condition;
96
		return $condition;
98
	}
97
	}
99
	
98
	
100
	private function getConditionlimiteeALaFrance() {
99
	private function getConditionlimiteeALaFrance() {
101
		$condition = '('.
100
		$condition = '('.
102
						'(latitude <= 51.071667 AND latitude >= 41.316667) AND '.
101
						'(latitude <= 51.071667 AND latitude >= 41.316667) AND '.
103
					 	'(longitude <= 9.513333 AND longitude >= -5.140278) '.
102
					 	'(longitude <= 9.513333 AND longitude >= -5.140278) '.
104
					 ') ';
103
					 ') ';
105
		return $condition;
104
		return $condition;
106
	}
105
	}
107
		
106
		
108
	private function getConditionSansPays() {
107
	private function getConditionSansPays() {
109
		$condition = '(pays IS NULL OR pays = "XX" OR pays = "")';
108
		$condition = '(pays IS NULL OR pays = "XX" OR pays = "")';
110
		return $condition;
109
		return $condition;
111
	}
110
	}
112
	
111
	
113
	private function getConditionCoordonneesValides() {
112
	private function getConditionCoordonneesValides() {
114
		$condition = '(latitude IS NOT NULL AND longitude IS NOT NULL '.
113
		$condition = '(latitude IS NOT NULL AND longitude IS NOT NULL '.
115
					 ' AND latitude != 0 AND latitude != "" '.
114
					 ' AND latitude != 0 AND latitude != "" '.
116
					 ' AND longitude != 0 AND longitude != "" '.
115
					 ' AND longitude != 0 AND longitude != "" '.
117
					 ' AND latitude >= -90 AND latitude <= 90 '.
116
					 ' AND latitude >= -90 AND latitude <= 90 '.
118
					 ' AND longitude >= -180 AND longitude <= 180) ';	
117
					 ' AND longitude >= -180 AND longitude <= 180) ';	
119
		return $condition;
118
		return $condition;
120
	}
119
	}
121
 
120
 
122
	public function creerColonneCodeInseeCalcule() {
121
	public function creerColonneCodeInseeCalcule() {
123
		$create = 'ALTER TABLE cel_obs '.
122
		$create = 'ALTER TABLE cel_obs '.
124
			'ADD code_insee_calcule VARCHAR(5) NOT NULL ';
123
			'ADD code_insee_calcule VARCHAR(5) NOT NULL ';
125
		$this->requeter($create);
124
		$this->requeter($create);
126
		$this->reinitialiserTempsDerniereRequete();
125
		$this->reinitialiserTempsDerniereRequete();
127
	}
126
	}
128
 
127
 
129
	public function ajouterCodeInseeCalcule($latitude, $longitude, $code_insee) {
128
	public function ajouterCodeInseeCalcule($latitude, $longitude, $code_insee) {
130
		$insert = 'UPDATE cel_obs '.
129
		$insert = 'UPDATE cel_obs '.
131
			"SET ".
130
			"SET ".
132
			"code_insee_calcule = ".$this->proteger($code_insee)." ".
131
			"code_insee_calcule = ".$this->proteger($code_insee)." ".
133
			"WHERE latitude = ".$this->proteger($latitude)." ".
132
			"WHERE latitude = ".$this->proteger($latitude)." ".
134
			"	AND longitude = ".$this->proteger($longitude)." ";
133
			"	AND longitude = ".$this->proteger($longitude)." ";
135
		$this->requeter($insert);
134
		$this->requeter($insert);
136
		$this->reinitialiserTempsDerniereRequete();
135
		$this->reinitialiserTempsDerniereRequete();
137
	}
136
	}
138
 
137
 
139
	public function modifierCodeInseeEtZoneGeo($coordonnees) {
138
	public function modifierCodeInseeEtZoneGeo($coordonnees) {
140
		
139
		
141
		$codeP = $this->proteger($coordonnees['code_insee']);
140
		$codeP = $this->proteger($coordonnees['code_insee']);
142
		$codeInseeP = $this->proteger('INSEE-C:'.$coordonnees['code_insee']);
141
		$codeInseeP = $this->proteger('INSEE-C:'.$coordonnees['code_insee']);
143
		$nomP = $this->proteger($coordonnees['nom']);
142
		$nomP = $this->proteger($coordonnees['nom']);
144
		
143
		
145
		$update = "UPDATE cel_obs ".
144
		$update = "UPDATE cel_obs ".
146
			"SET ".
145
			"SET ".
147
				"pays = 'FR', ".
146
				"pays = 'FR', ".
148
				"code_insee_calcule = ".$codeP.", ".
147
				"code_insee_calcule = ".$codeP.", ".
149
				"ce_zone_geo = ".$codeInseeP.", ".
148
				"ce_zone_geo = ".$codeInseeP.", ".
150
		    	"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
149
		    	"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
151
			"WHERE ".
150
			"WHERE ".
152
			" latitude = ".$this->proteger($coordonnees['latitude'])." ".
151
			" latitude = ".$this->proteger($coordonnees['latitude'])." ".
153
			" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
152
			" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
154
		$this->requeter($update);
153
		$this->requeter($update);
155
		$this->reinitialiserTempsDerniereRequete();
154
		$this->reinitialiserTempsDerniereRequete();
156
	}
155
	}
157
	
156
	
158
	public function modifierPays($coordonnees) {		
157
	public function modifierPays($coordonnees) {		
159
		$codePaysP = $this->proteger($coordonnees['code_pays']);
158
		$codePaysP = $this->proteger($coordonnees['code_pays']);
160
		$nomP = $this->proteger($coordonnees['nom']);
159
		$nomP = $this->proteger($coordonnees['nom']);
161
		
160
		
162
		$update = "UPDATE cel_obs ".
161
		$update = "UPDATE cel_obs ".
163
				"SET ".
162
				"SET ".
164
				"pays = ".$codePaysP.", ".
163
				"pays = ".$codePaysP.", ".
165
				"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
164
				"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
166
				"WHERE ".
165
				"WHERE ".
167
				" latitude = ".$this->proteger($coordonnees['latitude'])." ".
166
				" latitude = ".$this->proteger($coordonnees['latitude'])." ".
168
				" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
167
				" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
169
		$this->requeter($update);
168
		$this->requeter($update);
170
		$this->reinitialiserTempsDerniereRequete();
169
		$this->reinitialiserTempsDerniereRequete();
171
	}
170
	}
172
 
171
 
173
	// Il peut se passer assez de temps sans qu'aucune requete ne soit effectuée
172
	// Il peut se passer assez de temps sans qu'aucune requete ne soit effectuée
174
	// (cas d'un grand nombre d'enregistrements à la suite pour lesquels on ne trouve
173
	// (cas d'un grand nombre d'enregistrements à la suite pour lesquels on ne trouve
175
	// aucun département). Pour éviter cela on teste régulièrement la connection
174
	// aucun département). Pour éviter cela on teste régulièrement la connection
176
	public function testerActiviteConnection() {
175
	public function testerActiviteConnection() {
177
		$temps_courant = microtime(true);
176
		$temps_courant = microtime(true);
178
		$temps_depuis_derniere_requete = $temps_courant - $this->temps_derniere_requete;
177
		$temps_depuis_derniere_requete = $temps_courant - $this->temps_derniere_requete;
179
		if($temps_depuis_derniere_requete >= 18) {
178
		if($temps_depuis_derniere_requete >= 18) {
180
			$this->ping();
179
			$this->ping();
181
			$this->reinitialiserTempsDerniereRequete();
180
			$this->reinitialiserTempsDerniereRequete();
182
		}
181
		}
183
	}
182
	}
184
 
183
 
185
	private function reinitialiserTempsDerniereRequete() {
184
	private function reinitialiserTempsDerniereRequete() {
186
		$this->temps_derniere_requete = microtime(true);
185
		$this->temps_derniere_requete = microtime(true);
187
	}
186
	}
188
}
187
}