Subversion Repositories eFlore/Applications.del

Rev

Rev 2124 | Rev 2147 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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