1593 |
samuel |
1 |
<?php
|
|
|
2 |
/**
|
1793 |
jpm |
3 |
* Le web service plantnet récupère toutes les image de la table v_del_image
|
1663 |
samuel |
4 |
* ordonées par date de modification
|
|
|
5 |
* Les images sont regroupées en observations
|
|
|
6 |
* Les tags, les votes et les propositions de determinations sont intégrés à l'observation
|
1593 |
samuel |
7 |
*
|
1793 |
jpm |
8 |
*
|
1593 |
samuel |
9 |
* @category php 5.2
|
1663 |
samuel |
10 |
* @author Samuel Dufour-Kowalski <samuel.dufour@cirad.fr>
|
|
|
11 |
* @author Mathias Chouet <mathias@tela-botanica.org>
|
|
|
12 |
* @author Raphaël Droz <raphael@tela-botanica.org>
|
|
|
13 |
* @author Aurélien Peronnet <aurelien@tela-botanica.org>
|
|
|
14 |
|
|
|
15 |
|
1593 |
samuel |
16 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
17 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
|
|
18 |
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=ApiIdentiplante01Images
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
class Changements {
|
1793 |
jpm |
22 |
|
1593 |
samuel |
23 |
private $indexImagesIds = array();
|
|
|
24 |
private $conteneur;
|
|
|
25 |
private $navigation;
|
|
|
26 |
private $masque;
|
|
|
27 |
private $bdd;
|
|
|
28 |
private $parametres = array();
|
|
|
29 |
private $ressources = array();
|
|
|
30 |
private $date_defaut = '1900-01-01';
|
1793 |
jpm |
31 |
|
1593 |
samuel |
32 |
public function __construct(Conteneur $conteneur = null) {
|
1793 |
jpm |
33 |
|
1593 |
samuel |
34 |
/* restore_exception_handler(); */
|
|
|
35 |
/* restore_error_handler(); */
|
|
|
36 |
/* ini_set("display_errors", "1"); */
|
1793 |
jpm |
37 |
|
1593 |
samuel |
38 |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
|
|
|
39 |
$this->conteneur->chargerConfiguration('config_plantnet.ini');
|
|
|
40 |
$this->navigation = $conteneur->getNavigation();
|
|
|
41 |
$this->masque = $conteneur->getMasque();
|
1793 |
jpm |
42 |
$this->bdd = $this->conteneur->getBdd();
|
1593 |
samuel |
43 |
}
|
1793 |
jpm |
44 |
|
1593 |
samuel |
45 |
/**
|
|
|
46 |
* Méthode principale de la classe.
|
1793 |
jpm |
47 |
* Lance la récupération des images dans la base et les place dans un objet ResultatService
|
1593 |
samuel |
48 |
* pour l'afficher.
|
|
|
49 |
* @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
|
|
|
50 |
* @param array $parametres les paramètres situés après le ? dans l'url
|
|
|
51 |
* */
|
|
|
52 |
public function consulter($ressources, $parametres) {
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
// initialiserRessourcesEtParametres()
|
|
|
56 |
$this->ressources = $ressources;
|
|
|
57 |
$this->parametres = $parametres;
|
1793 |
jpm |
58 |
|
|
|
59 |
|
1593 |
samuel |
60 |
if(!isset($parametres['date'])) {
|
|
|
61 |
$this->parametres['date'] = $this->date_defaut;
|
|
|
62 |
}
|
1793 |
jpm |
63 |
|
|
|
64 |
|
1593 |
samuel |
65 |
// Lancement du service
|
|
|
66 |
$liaisons = $this->chargerLiaisons();
|
|
|
67 |
$images = array();
|
|
|
68 |
$total = 0;
|
|
|
69 |
|
|
|
70 |
if($liaisons) {
|
|
|
71 |
$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
|
|
|
72 |
$total = (int) $compte['nbre'];
|
1793 |
jpm |
73 |
|
1681 |
samuel |
74 |
$imgdata = $this->recupererDonneeObs($liaisons);
|
|
|
75 |
$obs = $this->regrouperObs($liaisons, $imgdata);
|
|
|
76 |
|
1640 |
samuel |
77 |
$obs = $this->chargerPropositionPlusProbable($obs);
|
1643 |
samuel |
78 |
$obs = $this->orderArray($obs);
|
1593 |
samuel |
79 |
}
|
1793 |
jpm |
80 |
|
1593 |
samuel |
81 |
$this->navigation->setTotal($total);
|
1793 |
jpm |
82 |
|
1593 |
samuel |
83 |
// Mettre en forme le résultat et l'envoyer pour affichage
|
|
|
84 |
$resultat = new ResultatService();
|
|
|
85 |
//$resultat->corps = array('entete' => $this->conteneur->getEntete(), 'resultats' => $images);
|
|
|
86 |
$resultat->corps = array('entete' => $this->conteneur->getEntete(), 'resultats' => $obs);
|
1793 |
jpm |
87 |
|
1593 |
samuel |
88 |
return $resultat;
|
|
|
89 |
}
|
1793 |
jpm |
90 |
|
|
|
91 |
|
1643 |
samuel |
92 |
private function orderArray(&$obs) {
|
|
|
93 |
|
|
|
94 |
$ret = array();
|
|
|
95 |
|
|
|
96 |
foreach ($obs as $o) {
|
|
|
97 |
$ret[] = $o;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
function cmp($a, $b) {
|
|
|
101 |
return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
|
|
|
102 |
}
|
1793 |
jpm |
103 |
|
1643 |
samuel |
104 |
usort($ret, 'cmp');
|
|
|
105 |
return $ret;
|
|
|
106 |
}
|
1593 |
samuel |
107 |
/*-------------------------------------------------------------------------------
|
1793 |
jpm |
108 |
CHARGEMENT DES IMAGES
|
1593 |
samuel |
109 |
--------------------------------------------------------------------------------*/
|
|
|
110 |
private function chargerLiaisons() {
|
|
|
111 |
|
|
|
112 |
$date_debut = $this->parametres['date'];
|
1642 |
samuel |
113 |
$date_debut = '\'' . $date_debut . '\'';
|
1593 |
samuel |
114 |
$limite = @min(intval($this->parametres['navigation.limite']), 1000);
|
|
|
115 |
$limite = $limite ? $limite : 10; // 0 => 10
|
|
|
116 |
$depart = intval(@$this->parametres['navigation.depart']);
|
|
|
117 |
|
1642 |
samuel |
118 |
|
1793 |
jpm |
119 |
|
|
|
120 |
$requete_sql =
|
1593 |
samuel |
121 |
'select SQL_CALC_FOUND_ROWS vdi.id_observation, vdi.id_image, '.
|
|
|
122 |
|
|
|
123 |
'GROUP_CONCAT(del_image_vote.valeur) as votes, GROUP_CONCAT(DISTINCT tag) as tags, '.
|
|
|
124 |
|
1701 |
samuel |
125 |
'GREATEST('.
|
|
|
126 |
'IFNULL(vdi.date_creation, \''.$this->date_defaut.'\'), '.
|
|
|
127 |
'IFNULL(vdi.date_modification, \''.$this->date_defaut.'\'), '.
|
|
|
128 |
'IFNULL(MAX(del_image_tag.date), \''.$this->date_defaut.'\'), '.
|
|
|
129 |
'IFNULL(MAX(del_image_tag.date_modification), \''.$this->date_defaut.'\'), '.
|
|
|
130 |
'IFNULL(MAX(del_image_vote.date), \''.$this->date_defaut.'\'), '.
|
|
|
131 |
'IFNULL(MAX(del_commentaire.date), \''.$this->date_defaut.'\'), '.
|
|
|
132 |
'IFNULL(MAX(del_commentaire_vote.date), \''.$this->date_defaut.'\')) as modif_date '.
|
1593 |
samuel |
133 |
|
|
|
134 |
'from v_del_image as vdi '.
|
|
|
135 |
|
1701 |
samuel |
136 |
'left join del_image_vote on del_image_vote.ce_image=id_image and del_image_vote.ce_protocole=3 '.
|
|
|
137 |
'left join del_image_tag on del_image_tag.ce_image=id_image and del_image_tag.actif=1 '.
|
1593 |
samuel |
138 |
'left join del_commentaire on del_commentaire.ce_observation=id_observation '.
|
|
|
139 |
'left join del_commentaire_vote on del_commentaire_vote.ce_proposition=del_commentaire.id_commentaire '.
|
|
|
140 |
|
|
|
141 |
'group by id_image, id_observation '.
|
|
|
142 |
|
|
|
143 |
'having MAX(vdi.date_creation) >= '.$date_debut.' or '.
|
|
|
144 |
' MAX(vdi.date_modification) >= '.$date_debut.' or '.
|
|
|
145 |
' MAX(del_image_tag.date) >= '.$date_debut.' or '.
|
1793 |
jpm |
146 |
' MAX(del_image_tag.date_modification) >= '.$date_debut.' or '.
|
1593 |
samuel |
147 |
' MAX(del_image_vote.date) >= '.$date_debut.' or '.
|
|
|
148 |
' MAX(del_commentaire.date) >= '.$date_debut.' or '.
|
|
|
149 |
' MAX(del_commentaire_vote.date) >= '.$date_debut.' '.
|
|
|
150 |
'order by modif_date DESC '.
|
|
|
151 |
'limit '.$depart.', '.$limite.' -- '.__FILE__.':'.__LINE__;
|
|
|
152 |
|
1643 |
samuel |
153 |
//echo $requete_sql; exit;
|
|
|
154 |
|
1593 |
samuel |
155 |
return $this->bdd->recupererTous($requete_sql);
|
|
|
156 |
|
|
|
157 |
|
1793 |
jpm |
158 |
|
1663 |
samuel |
159 |
// GROUP BY (très couteux) car multiples observations associées à une image
|
1793 |
jpm |
160 |
// charlie est ici :-)
|
1663 |
samuel |
161 |
// eg: 16150,16185,16245,16246,118995,129989
|
1793 |
jpm |
162 |
|
1593 |
samuel |
163 |
}
|
1681 |
samuel |
164 |
|
1793 |
jpm |
165 |
|
1681 |
samuel |
166 |
// recupere les donnée associées (fait en 2 requetes pour optimiser)
|
|
|
167 |
private function recupererDonneeObs(&$liaisons) {
|
|
|
168 |
|
|
|
169 |
// recuperer les ids
|
|
|
170 |
$ids = array();
|
|
|
171 |
foreach ($liaisons as $img) {
|
|
|
172 |
$id = $img['id_image'];
|
|
|
173 |
$ids[] = $id;
|
|
|
174 |
}
|
1793 |
jpm |
175 |
|
1681 |
samuel |
176 |
// recuperer les donnees
|
|
|
177 |
$resultats = $this->bdd->recupererTous(sprintf(
|
|
|
178 |
'SELECT '.
|
|
|
179 |
'vdi.id_observation, vdi.id_image, '.
|
|
|
180 |
'vdi.nom_sel, '.
|
|
|
181 |
'vdi.nom_referentiel, vdi.nom_ret, vdi.nom_ret_nn, vdi.nt, vdi.famille, '.
|
1793 |
jpm |
182 |
'vdi.zone_geo, vdi.latitude, vdi.longitude, '.
|
|
|
183 |
'vdi.date_observation, vdi.date_creation, vdi.date_transmission, '.
|
|
|
184 |
'vdi.mots_cles_texte as mots_cles_texte, '.
|
|
|
185 |
'vdi.i_mots_cles_texte as mots_cles_texte_image, '.
|
|
|
186 |
|
1681 |
samuel |
187 |
'vdi.ce_utilisateur as ce_utilisateur, '.
|
1793 |
jpm |
188 |
'vdi.prenom_utilisateur, vdi.courriel_utilisateur, vdi.nom_utilisateur, vdi.nom_original as nom_image '.
|
1681 |
samuel |
189 |
|
|
|
190 |
'FROM v_del_image as vdi '.
|
|
|
191 |
'WHERE vdi.id_image IN (%s) '.
|
|
|
192 |
'', implode(',', $ids)));
|
|
|
193 |
|
|
|
194 |
// regroupe les données par id_image
|
|
|
195 |
$img_data = array();
|
|
|
196 |
foreach ($resultats as $img) {
|
|
|
197 |
$id = $img['id_image'];
|
|
|
198 |
$img_data[$id] = $img;
|
|
|
199 |
}
|
|
|
200 |
return $img_data;
|
|
|
201 |
}
|
|
|
202 |
|
1593 |
samuel |
203 |
/**
|
|
|
204 |
* Retourner un tableau d'images formaté en fonction des liaisons trouvées
|
|
|
205 |
* @param $liaisons les liaisons de la table del_obs_images
|
|
|
206 |
* */
|
1681 |
samuel |
207 |
private function regrouperObs(&$liaisons, &$imgdatas) {
|
1593 |
samuel |
208 |
|
|
|
209 |
// regroupe les observations
|
|
|
210 |
$obs = array();
|
|
|
211 |
foreach ($liaisons as $img) {
|
|
|
212 |
$idobs = $img['id_observation'];
|
1681 |
samuel |
213 |
$idimg = $img['id_image'];
|
1593 |
samuel |
214 |
|
1681 |
samuel |
215 |
$imgdata = $imgdatas[$idimg];
|
1793 |
jpm |
216 |
|
1593 |
samuel |
217 |
if (!isset($obs[$idobs])) {
|
|
|
218 |
$obs[$idobs] = array();
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
$obs[$idobs]['id_observation'] = $idobs;
|
1681 |
samuel |
222 |
$obs[$idobs]['auteur_id'] = $imgdata['ce_utilisateur'];
|
|
|
223 |
$obs[$idobs]['auteur_prenom'] = $imgdata['prenom_utilisateur'];
|
|
|
224 |
$obs[$idobs]['auteur_nom'] = $imgdata['nom_utilisateur'];
|
|
|
225 |
$obs[$idobs]['auteur_courriel'] = $imgdata['courriel_utilisateur'];
|
1593 |
samuel |
226 |
|
1681 |
samuel |
227 |
$obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($imgdata['mots_cles_texte']);
|
1793 |
jpm |
228 |
|
1681 |
samuel |
229 |
$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
|
|
|
230 |
$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
|
|
|
231 |
$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
|
1640 |
samuel |
232 |
$obs[$idobs]['date_changement'] = $img['modif_date'];
|
|
|
233 |
|
1681 |
samuel |
234 |
$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
|
|
|
235 |
$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
|
|
|
236 |
$obs[$idobs]['nom_ret'] = $imgdata['nom_ret'];
|
|
|
237 |
$obs[$idobs]['nn'] = $imgdata['nom_ret_nn'];
|
|
|
238 |
$obs[$idobs]['nt'] = $imgdata['nt'];
|
|
|
239 |
$obs[$idobs]['famille'] = $imgdata['famille'];
|
1640 |
samuel |
240 |
|
1681 |
samuel |
241 |
$obs[$idobs]['zone_geo'] = $imgdata['zone_geo'];
|
|
|
242 |
$obs[$idobs]['latitude'] = floatval($imgdata['latitude']);
|
|
|
243 |
$obs[$idobs]['longitude'] = floatval($imgdata['longitude']);
|
1640 |
samuel |
244 |
|
1793 |
jpm |
245 |
|
1593 |
samuel |
246 |
if (!isset($obs[$idobs]['images'])) {
|
|
|
247 |
$obs[$idobs]['images'] = array();
|
|
|
248 |
}
|
|
|
249 |
|
1640 |
samuel |
250 |
$img_obj = array(
|
|
|
251 |
'id_image' => $img['id_image'],
|
1681 |
samuel |
252 |
'nom_image' => $imgdata['nom_image'],
|
1652 |
samuel |
253 |
'url' => sprintf('http://api.tela-botanica.org/img:%09dO.jpg', $img['id_image']),
|
1640 |
samuel |
254 |
'votes' => array_map('intval', explode(',', $img['votes'])),
|
|
|
255 |
'tags' => explode(',', $img['tags']),
|
1681 |
samuel |
256 |
'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
|
1640 |
samuel |
257 |
);
|
|
|
258 |
// push
|
|
|
259 |
$obs[$idobs]['images'][] = $img_obj;
|
1593 |
samuel |
260 |
|
|
|
261 |
}
|
1793 |
jpm |
262 |
|
1593 |
samuel |
263 |
return $obs;
|
1793 |
jpm |
264 |
|
1593 |
samuel |
265 |
}
|
1793 |
jpm |
266 |
|
1593 |
samuel |
267 |
/**
|
1640 |
samuel |
268 |
* Charger les votes pour chaque image
|
|
|
269 |
**/
|
|
|
270 |
private function chargerPropositionPlusProbable(&$obs) {
|
1593 |
samuel |
271 |
|
1640 |
samuel |
272 |
$obsIds = array_keys($obs);
|
1593 |
samuel |
273 |
|
1640 |
samuel |
274 |
$resultats = $this->bdd->recupererTous(sprintf(
|
1657 |
samuel |
275 |
'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, del_commentaire_vote.ce_utilisateur '.
|
1640 |
samuel |
276 |
'FROM del_commentaire_vote, del_commentaire '.
|
|
|
277 |
'WHERE ce_observation IN (%s) '.
|
|
|
278 |
'AND nom_sel IS NOT NULL '.
|
|
|
279 |
'AND del_commentaire.id_commentaire=del_commentaire_vote.ce_proposition '.
|
|
|
280 |
'', implode(',', $obsIds)));
|
1593 |
samuel |
281 |
|
1793 |
jpm |
282 |
|
1593 |
samuel |
283 |
$votes = array(); // map ce_proposition -> score
|
1793 |
jpm |
284 |
|
1593 |
samuel |
285 |
// calcul des votes
|
|
|
286 |
// un vote identifié a un facteur de 3
|
1640 |
samuel |
287 |
// additionne tous les vote par ce_proposition
|
|
|
288 |
foreach($resultats as $vote) {
|
|
|
289 |
if(!isset($votes[$vote['id_commentaire']])) {
|
|
|
290 |
$votes[$vote['id_commentaire']] = 0;
|
1593 |
samuel |
291 |
}
|
|
|
292 |
$valeur = ($vote['valeur'] == 1) ? 1 : -1;
|
1640 |
samuel |
293 |
$votes[$vote['id_commentaire']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
|
1593 |
samuel |
294 |
}
|
1793 |
jpm |
295 |
|
1640 |
samuel |
296 |
foreach($resultats as $vote) {
|
|
|
297 |
$idobs = $vote['ce_observation'];
|
1793 |
jpm |
298 |
|
1640 |
samuel |
299 |
if(!isset($obs[$idobs]['determinations'])) {
|
|
|
300 |
$obs[$idobs]['determinations'] = array();
|
1593 |
samuel |
301 |
}
|
|
|
302 |
|
1793 |
jpm |
303 |
$obs[$idobs]['determinations'][$vote['id_commentaire']] =
|
1640 |
samuel |
304 |
array('nom_sel' => $vote['nom_sel'],
|
1657 |
samuel |
305 |
'nom_ret' => $vote['nom_ret'],
|
1640 |
samuel |
306 |
'score' => $votes[$vote['id_commentaire']],
|
|
|
307 |
'nn' => $vote['nom_sel_nn']
|
|
|
308 |
);
|
1593 |
samuel |
309 |
}
|
|
|
310 |
|
1793 |
jpm |
311 |
|
1640 |
samuel |
312 |
return $obs;
|
1593 |
samuel |
313 |
}
|
1793 |
jpm |
314 |
|
|
|
315 |
|
1593 |
samuel |
316 |
/*-------------------------------------------------------------------------------
|
|
|
317 |
FORMATER ET METTRE EN FORME
|
|
|
318 |
--------------------------------------------------------------------------------*/
|
|
|
319 |
/**
|
|
|
320 |
* Formater les mots clés du cel en n'affichant que ceux faisant partie
|
|
|
321 |
* d'une liste définie dans le fichier de configuration
|
|
|
322 |
* @param $chaineMotCleCel la chaine de mots clés du cel
|
|
|
323 |
* @return string la chaine filtrée
|
|
|
324 |
* */
|
|
|
325 |
private function formaterMotsClesCel($chaineMotCleCel) {
|
1652 |
samuel |
326 |
|
|
|
327 |
$mots_cles_cel_affiches = "fleur,fleurs,feuille,feuilles,ecorce,fruit,fruits,port,plantnet,plantscan_new";
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
$result = array_intersect(
|
|
|
331 |
explode(',', $mots_cles_cel_affiches), // $tabMotsClesAffiches
|
|
|
332 |
explode(',', $chaineMotCleCel)); // $tabMotsClesCel
|
|
|
333 |
|
1681 |
samuel |
334 |
if (count($result) === 0) {
|
|
|
335 |
return array();
|
|
|
336 |
}
|
|
|
337 |
$ret = explode(',', implode(',', $result));
|
|
|
338 |
return $ret;
|
1593 |
samuel |
339 |
}
|
1793 |
jpm |
340 |
|
1593 |
samuel |
341 |
}
|