Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2212 arthur 1
<?php
2
/**
3
 * Le web service plantnet récupère toutes les infos de la vue del_plantnet.
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.
7
 *
8
 * @category DEL
9
 * @package Services
10
 * @subpackage Plantnet
11
 * @version 0.1
12
 * @author Mathias CHOUET <mathias@tela-botanica.org>
13
 * @author Samuel DUFOUR-KOWALSKI <samuel.dufour@cirad.fr>
14
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
15
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
16
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
17
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
18
 * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
19
 */
20
 
21
class Changements {
22
 
23
	private $conteneur;
24
	private $navigation;
25
	private $bdd;
26
 
27
	private $parametres = array();
28
	private $ressources = array();
29
	private $date_defaut = '1900-01-01';
30
	private $ordre_defaut = 'asc';
31
	private $idsObsImg = array();
32
	private $infosObsImg = array();
33
 
34
 
35
	public function __construct(Conteneur $conteneur = null) {
36
		/* restore_exception_handler(); */
37
		/* restore_error_handler(); */
38
		/* ini_set("display_errors", "1"); */
39
 
40
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
41
		$this->navigation = $conteneur->getNavigation();
42
		$this->bdd = $this->conteneur->getBdd();
43
	}
44
 
45
	/**
46
	 * Méthode principale de la classe.
47
	 * Lance la récupération des images dans la base et les place dans un objet ResultatService
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
		// initialiserRessourcesEtParametres()
54
		$this->ressources = $ressources;
55
		$this->parametres = $parametres;
56
 
57
		if (!isset($parametres['date'])) {
58
			$this->parametres['date'] = $this->date_defaut;
59
		}
60
 
61
		if (! isset($parametres['ordre'])) {
62
			$this->parametres['ordre'] = $this->ordre_defaut;
63
		} else {
64
 			$parametres['ordre'] = strtolower($parametres['ordre']);
65
 			if (! in_array($parametres['ordre'], array('asc', 'desc'))) {
66
 				$this->parametres['ordre'] = $this->ordre_defaut;
67
			}
68
		}
69
 
70
		// Lancement du service
71
		$this->idsObsImg = $this->getIdsObsImg();
72
		$infos = array();
73
		$total = 0;
74
		if ($this->idsObsImg) {
75
			$total = $this->getTotal();
76
 
77
			$this->infosObsImg = $this->recupererInfos();
78
			$infos = $this->formaterInfos();
79
			$infos = $this->chargerPropositionPlusProbable($infos);
80
			$infos = $this->orderArray($infos);
81
		}
82
		$this->navigation->setTotal($total);
83
 
84
		// Mettre en forme le résultat et l'envoyer pour affichage
85
		$resultat = new ResultatService();
86
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $infos);
87
		return $resultat;
88
	}
89
 
90
	/*-------------------------------------------------------------------------------
91
								CHARGEMENT DES IMAGES
92
	--------------------------------------------------------------------------------*/
93
 
94
	private function getIdsObsImg() {
95
		$date_debut = "'{$this->parametres['date']}'";
96
		$limite = @min(intval($this->parametres['navigation.limite']), 1000);
97
		$limite = $limite ? $limite : 100; // 0 => 10
98
		$depart = intval(@$this->parametres['navigation.depart']);
99
		$ordre = $this->parametres['ordre'];
100
 
101
		$requete =
102
			'SELECT SQL_CALC_FOUND_ROWS p.id_observation, p.id_image, '.
103
			'GROUP_CONCAT(iv.valeur) AS votes, '.
104
			'GROUP_CONCAT(DISTINCT tag) AS tags, '.
105
			'modif_date '.
106
 
107
			'FROM del_plantnet AS p '.
108
			'	JOIN del_observation_modif_date '.
109
			'		ON (p.id_observation = del_observation_modif_date.id_observation '.
110
			'		AND modif_date >= '.$date_debut.') '.
111
			'	LEFT JOIN del_image_vote AS iv '.
112
			'		ON (id_image = iv.ce_image AND iv.ce_protocole = 3) '.
113
			'	LEFT JOIN del_image_tag AS it '.
114
			'		ON (id_image = it.ce_image AND it.actif = 1) '.
115
			'	LEFT JOIN del_commentaire AS c '.
116
			'		ON (p.id_observation = c.ce_observation) '.
117
			'	LEFT JOIN del_commentaire_vote AS cv '.
118
			'		ON (c.id_commentaire = cv.ce_proposition) '.
119
			'GROUP BY id_image, p.id_observation '.
120
			'ORDER BY modif_date ' . $ordre . ' '.
121
			'LIMIT '.$depart.', '.$limite.
122
			' -- '.__FILE__.':'.__LINE__;
123
 
124
		return $this->bdd->recupererTous($requete);
125
	}
126
 
127
	private function getTotal() {
128
		$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
129
		$total = (int) $compte['nbre'];
130
		return $total;
131
	}
132
 
133
	// recupere les donnée associées (fait en 2 requetes pour optimiser)
134
	private function recupererInfos() {
135
		// recuperer les ids
136
		$idsImg = array();
137
		foreach ($this->idsObsImg as $ids) {
138
			$id = $ids['id_image'];
139
			$idsImg[] = $id;
140
		}
141
		$idsImgConcat = implode(',', $idsImg);
142
 
143
		$requete = 'SELECT '.
144
			'p.id_observation, p.id_image, '.
145
			'p.nom_sel, '.
146
			'p.nom_referentiel, p.nom_ret, p.famille, '.
147
			'p.zone_geo, p.latitude, p.longitude, '.
148
			'p.date_observation, p.date_creation, p.date_transmission, '.
149
			'p.mots_cles_texte, '.
150
			'p.ce_utilisateur, p.prenom_utilisateur, p.nom_utilisateur, '.
151
			'p.i_mots_cles_texte AS mots_cles_texte_image, p.nom_original AS nom_image, '.
152
			'cp.id_plantnet '.
153
			'FROM del_plantnet AS p '.
154
			'LEFT JOIN tb_cel_test.cel_plantnet AS cp ON p.id_observation = cp.id_observation '.
155
			"WHERE id_image IN ($idsImgConcat) ".
156
			' -- '.__FILE__.':'.__LINE__;
157
		// recuperer les donnees
158
		$resultats = $this->bdd->recupererTous($requete);
159
 
160
		// regroupe les données par id_image
161
		$img_data = array();
162
		foreach ($resultats as $infos) {
163
			$idImg = $infos['id_image'];
164
			$img_data[$idImg] = $infos;
165
		}
166
		return $img_data;
167
	}
168
 
169
	/**
170
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
171
	* @param $liaisons les liaisons de la table del_obs_images
172
	*/
173
	private function formaterInfos() {
174
		// regroupe les observations
175
		$obs = array();
176
		$imgCelTpl = $this->conteneur->getParametre('cel_img_url_tpl');
177
		foreach ($this->idsObsImg as $ids) {
178
			$idobs = $ids['id_observation'];
179
			$idimg = $ids['id_image'];
180
 
181
			$imgdata = $this->infosObsImg[$idimg];
182
 
183
			if (!isset($obs[$idobs])) {
184
				$obs[$idobs] = array();
185
			}
186
 
187
			$obs[$idobs]['id_observation'] = $idobs;
188
			$obs[$idobs]['id_plantnet'] = $imgdata['id_plantnet'];
189
			$obs[$idobs]['auteur_id'] = $imgdata['ce_utilisateur'];
190
			$obs[$idobs]['auteur_prenom'] = $imgdata['prenom_utilisateur'];
191
			$obs[$idobs]['auteur_nom'] = $imgdata['nom_utilisateur'];
192
 
193
			$obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($imgdata['mots_cles_texte']);
194
 
195
			$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
196
			$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
197
			$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
198
			$obs[$idobs]['date_changement'] = $ids['modif_date'];
199
 
200
			$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
201
			$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
202
			$obs[$idobs]['nom_ret'] = $imgdata['nom_ret'];
203
			//$obs[$idobs]['nn'] = $imgdata['nom_ret_nn'];
204
			//$obs[$idobs]['nt'] = $imgdata['nt'];
205
			$obs[$idobs]['famille'] = $imgdata['famille'];
206
 
207
			$obs[$idobs]['zone_geo'] = $imgdata['zone_geo'];
208
			$obs[$idobs]['latitude'] = floatval($imgdata['latitude']);
209
			$obs[$idobs]['longitude'] = floatval($imgdata['longitude']);
210
 
211
			if (!isset($obs[$idobs]['images'])) {
212
				$obs[$idobs]['images'] = array();
213
			}
214
 
215
			$img_obj = array(
216
				'id_image' => $idimg,
217
				'nom_image' => $imgdata['nom_image'],
218
				'url' => sprintf($imgCelTpl, $idimg, 'O'),
219
				'votes' => array_map('intval', explode(',', $ids['votes'])),
220
				'tags' => explode(',', $ids['tags']),
221
				'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
222
				);
223
			// push
224
			$obs[$idobs]['images'][] = $img_obj;
225
		}
226
		return $obs;
227
	}
228
 
229
	/**
230
	 * Charger les votes pour chaque image
231
	 */
232
	private function chargerPropositionPlusProbable(&$obs) {
233
		$obsIds = array_keys($obs);
234
		$idsObsConcat = implode(',', $obsIds);
235
 
236
		$requete = 'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, cv.ce_utilisateur '.
237
			'FROM del_commentaire_vote AS cv, del_commentaire AS c '.
238
			"WHERE ce_observation IN ($idsObsConcat) ".
239
			'AND nom_sel IS NOT NULL '.
240
			'AND c.id_commentaire = cv.ce_proposition '.
241
			' -- '.__FILE__.':'.__LINE__;
242
		$resultats = $this->bdd->recupererTous($requete);
243
 
244
		// calcul des votes
245
		// un vote identifié a un facteur de 3
246
		// additionne tous les vote par ce_proposition
247
		$votes = array(); // map ce_proposition -> score
248
		foreach ($resultats as $vote) {
249
			if (!isset($votes[$vote['id_commentaire']])) {
250
				$votes[$vote['id_commentaire']] = 0;
251
			}
252
			$valeur = ($vote['valeur'] == 1) ? 1 : -1;
253
			$votes[$vote['id_commentaire']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
254
		}
255
 
256
		foreach ($resultats as $vote) {
257
			$idobs = $vote['ce_observation'];
258
 
259
			if (!isset($obs[$idobs]['determinations'])) {
260
				$obs[$idobs]['determinations'] = array();
261
			}
262
 
263
			$obs[$idobs]['determinations'][$vote['id_commentaire']] =
264
			array('nom_sel' => $vote['nom_sel'],
265
				'nom_ret' => $vote['nom_ret'],
266
				'score' => $votes[$vote['id_commentaire']],
267
				'nn' => $vote['nom_sel_nn']);
268
		}
269
		return $obs;
270
	}
271
 
272
	private function orderArray(&$obs) {
273
		$ret = array();
274
		foreach ($obs as $o) {
275
			$ret[] = $o;
276
		}
277
 
278
		function cmpDesc($a, $b) {
279
			return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
280
		}
281
		function cmpAsc($a, $b) {
282
			return cmpDesc($b, $a);
283
		}
284
 
285
		if ($this->parametres['ordre'] == 'desc') {
286
			usort($ret, 'cmpDesc');
287
		} else {
288
			usort($ret, 'cmpAsc');
289
		}
290
		return $ret;
291
	}
292
 
293
	/**
294
	 * Formater les mots clés du cel en n'affichant que ceux faisant partie
295
	 * d'une liste définie dans le fichier de configuration
296
	 * @param $chaineMotCleCel la chaine de mots clés du cel
297
	 * @return string la chaine filtrée
298
	 */
299
	private function formaterMotsClesCel($chaineMotCleCel) {
300
		$mots_cles_cel_affiches = "fleur,fleurs,feuille,feuilles,ecorce,fruit,fruits,port,plantnet,plantscan_new,plantnet-mobile";
301
 
302
		$result = array_intersect(
303
			explode(',', $mots_cles_cel_affiches), // $tabMotsClesAffiches
304
			explode(',', $chaineMotCleCel)); // $tabMotsClesCel
305
 
306
		if (count($result) === 0) {
307
		  return array();
308
		}
309
		$ret = explode(',', implode(',', $result));
310
		return $ret;
311
	}
312
 
313
}