Subversion Repositories eFlore/Applications.cel

Rev

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

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