Subversion Repositories eFlore/Applications.cel

Rev

Rev 2545 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1500 delphine 1
<?php
2
class Dao extends Bdd {
2436 jpm 3
 
1520 aurelien 4
	private $temps_derniere_requete = 0;
2436 jpm 5
 
2527 aurelien 6
	public function rechercherCoordonnees($conditions = array()) {
7
		if(!empty($conditions)) {
8
			$where = 'WHERE '.implode(' AND ', $conditions);
9
		}
10
 
1523 aurelien 11
		$requete = "SELECT longitude, latitude ".
12
			"FROM cel_obs ".
2527 aurelien 13
			$where.
1523 aurelien 14
			"GROUP BY longitude , latitude ";
2527 aurelien 15
 
1500 delphine 16
		$resultat = $this->recupererTous($requete);
1520 aurelien 17
		$this->reinitialiserTempsDerniereRequete();
1500 delphine 18
		return $resultat;
19
	}
2436 jpm 20
 
2533 aurelien 21
	public function rechercherCoordonneesFrancaisesSansCorrespondances() {
2527 aurelien 22
		$conditions = array(
23
						$this->getConditionCoordonneesValides(),
24
						$this->getConditionInfosGeoIncompletes(),
25
						$this->getConditionlimiteeALaFrance(),
26
						$this->getConditionModifObsRecente()
27
					);
28
 
29
		return $this->rechercherCoordonnees($conditions);
1517 aurelien 30
	}
2525 aurelien 31
 
2533 aurelien 32
	public function rechercherCoordonneesFrancaisesSansCorrespondanceDepuisLeDebut() {
2527 aurelien 33
		$conditions = array(
34
						$this->getConditionCoordonneesValides(),
35
						$this->getConditionInfosGeoIncompletes(),
36
						$this->getConditionlimiteeALaFrance()
37
					);
38
 
39
		return $this->rechercherCoordonnees($conditions);
2525 aurelien 40
	}
2527 aurelien 41
 
2533 aurelien 42
	public function rechercherPaysSansCorrespondance() {
43
		$conditions = array(
44
				$this->getConditionCoordonneesValides(),
45
				$this->getConditionSansPays(),
46
				$this->getConditionModifObsRecente()
47
		);
48
 
49
		return $this->rechercherCoordonnees($conditions);
50
	}
2527 aurelien 51
 
2533 aurelien 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
	}
84
 
85
 
2527 aurelien 86
	private function getConditionModifObsRecente() {
87
		$condition = 'DATE_ADD(date_modification, INTERVAL 25 HOUR) >= CURDATE() ';
88
		return $condition;
89
	}
90
 
91
	private function getConditionInfosGeoIncompletes() {
92
		$condition = '('.
2528 aurelien 93
						'ce_zone_geo IS NULL OR ce_zone_geo = "" OR '.
94
						'zone_geo IS NULL OR zone_geo = "" '.
2527 aurelien 95
					 ') ';
96
		return $condition;
97
	}
98
 
99
	private function getConditionlimiteeALaFrance() {
100
		$condition = '('.
101
						'(latitude <= 51.071667 AND latitude >= 41.316667) AND '.
102
					 	'(longitude <= 9.513333 AND longitude >= -5.140278) '.
103
					 ') ';
104
		return $condition;
105
	}
2533 aurelien 106
 
107
	private function getConditionSansPays() {
108
		$condition = '(pays IS NULL OR pays = "XX" OR pays = "")';
109
		return $condition;
110
	}
2527 aurelien 111
 
112
	private function getConditionCoordonneesValides() {
113
		$condition = '(latitude IS NOT NULL AND longitude IS NOT NULL '.
114
					 ' AND latitude != 0 AND latitude != "" '.
2541 aurelien 115
					 ' AND longitude != 0 AND longitude != "" '.
116
					 ' AND latitude >= -90 AND latitude <= 90 '.
117
					 ' AND longitude >= -180 AND longitude <= 180) ';
2527 aurelien 118
		return $condition;
119
	}
2436 jpm 120
 
1523 aurelien 121
	public function creerColonneCodeInseeCalcule() {
122
		$create = 'ALTER TABLE cel_obs '.
123
			'ADD code_insee_calcule VARCHAR(5) NOT NULL ';
1500 delphine 124
		$this->requeter($create);
1520 aurelien 125
		$this->reinitialiserTempsDerniereRequete();
1500 delphine 126
	}
2436 jpm 127
 
1523 aurelien 128
	public function ajouterCodeInseeCalcule($latitude, $longitude, $code_insee) {
129
		$insert = 'UPDATE cel_obs '.
2527 aurelien 130
			"SET ".
131
			"code_insee_calcule = ".$this->proteger($code_insee)." ".
1523 aurelien 132
			"WHERE latitude = ".$this->proteger($latitude)." ".
133
			"	AND longitude = ".$this->proteger($longitude)." ";
1500 delphine 134
		$this->requeter($insert);
1520 aurelien 135
		$this->reinitialiserTempsDerniereRequete();
1500 delphine 136
	}
2559 aurelien 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
	}
2436 jpm 147
 
1523 aurelien 148
	public function modifierCodeInseeEtZoneGeo($coordonnees) {
2528 aurelien 149
 
150
		$codeP = $this->proteger($coordonnees['code_insee']);
151
		$codeInseeP = $this->proteger('INSEE-C:'.$coordonnees['code_insee']);
152
		$nomP = $this->proteger($coordonnees['nom']);
153
 
1523 aurelien 154
		$update = "UPDATE cel_obs ".
2527 aurelien 155
			"SET ".
2533 aurelien 156
				"pays = 'FR', ".
2528 aurelien 157
				"code_insee_calcule = ".$codeP.", ".
158
				"ce_zone_geo = ".$codeInseeP.", ".
159
		    	"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
2527 aurelien 160
			"WHERE ".
161
			" latitude = ".$this->proteger($coordonnees['latitude'])." ".
162
			" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
1500 delphine 163
		$this->requeter($update);
1520 aurelien 164
		$this->reinitialiserTempsDerniereRequete();
1500 delphine 165
	}
2533 aurelien 166
 
167
	public function modifierPays($coordonnees) {
168
		$codePaysP = $this->proteger($coordonnees['code_pays']);
169
		$nomP = $this->proteger($coordonnees['nom']);
170
 
171
		$update = "UPDATE cel_obs ".
172
				"SET ".
173
				"pays = ".$codePaysP.", ".
174
				"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
175
				"WHERE ".
176
				" latitude = ".$this->proteger($coordonnees['latitude'])." ".
177
				" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
178
		$this->requeter($update);
179
		$this->reinitialiserTempsDerniereRequete();
180
	}
2436 jpm 181
 
1520 aurelien 182
	// Il peut se passer assez de temps sans qu'aucune requete ne soit effectuée
183
	// (cas d'un grand nombre d'enregistrements à la suite pour lesquels on ne trouve
184
	// aucun département). Pour éviter cela on teste régulièrement la connection
185
	public function testerActiviteConnection() {
186
		$temps_courant = microtime(true);
187
		$temps_depuis_derniere_requete = $temps_courant - $this->temps_derniere_requete;
188
		if($temps_depuis_derniere_requete >= 18) {
189
			$this->ping();
190
			$this->reinitialiserTempsDerniereRequete();
191
		}
192
	}
2436 jpm 193
 
1520 aurelien 194
	private function reinitialiserTempsDerniereRequete() {
195
		$this->temps_derniere_requete = microtime(true);
196
	}
2436 jpm 197
}