Subversion Repositories eFlore/Applications.cel

Rev

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

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