Subversion Repositories eFlore/Applications.del

Rev

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