| 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 |
}
|
| 2436 |
jpm |
137 |
|
| 1523 |
aurelien |
138 |
public function modifierCodeInseeEtZoneGeo($coordonnees) {
|
| 2528 |
aurelien |
139 |
|
|
|
140 |
$codeP = $this->proteger($coordonnees['code_insee']);
|
|
|
141 |
$codeInseeP = $this->proteger('INSEE-C:'.$coordonnees['code_insee']);
|
|
|
142 |
$nomP = $this->proteger($coordonnees['nom']);
|
|
|
143 |
|
| 1523 |
aurelien |
144 |
$update = "UPDATE cel_obs ".
|
| 2527 |
aurelien |
145 |
"SET ".
|
| 2533 |
aurelien |
146 |
"pays = 'FR', ".
|
| 2528 |
aurelien |
147 |
"code_insee_calcule = ".$codeP.", ".
|
|
|
148 |
"ce_zone_geo = ".$codeInseeP.", ".
|
|
|
149 |
"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
|
| 2527 |
aurelien |
150 |
"WHERE ".
|
|
|
151 |
" latitude = ".$this->proteger($coordonnees['latitude'])." ".
|
|
|
152 |
" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
|
| 1500 |
delphine |
153 |
$this->requeter($update);
|
| 1520 |
aurelien |
154 |
$this->reinitialiserTempsDerniereRequete();
|
| 1500 |
delphine |
155 |
}
|
| 2533 |
aurelien |
156 |
|
|
|
157 |
public function modifierPays($coordonnees) {
|
|
|
158 |
$codePaysP = $this->proteger($coordonnees['code_pays']);
|
|
|
159 |
$nomP = $this->proteger($coordonnees['nom']);
|
|
|
160 |
|
|
|
161 |
$update = "UPDATE cel_obs ".
|
|
|
162 |
"SET ".
|
|
|
163 |
"pays = ".$codePaysP.", ".
|
|
|
164 |
"zone_geo = IF(zone_geo = '' OR zone_geo = NULL, ".$nomP.", zone_geo) ".
|
|
|
165 |
"WHERE ".
|
|
|
166 |
" latitude = ".$this->proteger($coordonnees['latitude'])." ".
|
|
|
167 |
" AND longitude = ".$this->proteger($coordonnees['longitude'])." ";
|
|
|
168 |
$this->requeter($update);
|
|
|
169 |
$this->reinitialiserTempsDerniereRequete();
|
|
|
170 |
}
|
| 2436 |
jpm |
171 |
|
| 1520 |
aurelien |
172 |
// 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
|
|
|
174 |
// aucun département). Pour éviter cela on teste régulièrement la connection
|
|
|
175 |
public function testerActiviteConnection() {
|
|
|
176 |
$temps_courant = microtime(true);
|
|
|
177 |
$temps_depuis_derniere_requete = $temps_courant - $this->temps_derniere_requete;
|
|
|
178 |
if($temps_depuis_derniere_requete >= 18) {
|
|
|
179 |
$this->ping();
|
|
|
180 |
$this->reinitialiserTempsDerniereRequete();
|
|
|
181 |
}
|
|
|
182 |
}
|
| 2436 |
jpm |
183 |
|
| 1520 |
aurelien |
184 |
private function reinitialiserTempsDerniereRequete() {
|
|
|
185 |
$this->temps_derniere_requete = microtime(true);
|
|
|
186 |
}
|
| 2436 |
jpm |
187 |
}
|