Subversion Repositories eFlore/Applications.del

Rev

Rev 1794 | Rev 1922 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1794 Rev 1881
Line 1... Line 1...
1
<?php
1
<?php
2
/**
2
/**
3
 * Le web service plantnet récupère toutes les image de la table v_del_image
3
 * Le web service plantnet récupère toutes les infos de la vue del_plantnet.
4
 * ordonées par date de modification
4
 * Ordonées par date de modification.
5
 * Les images sont regroupées en observations
5
 * Les images sont regroupées en observations.
6
 * Les tags, les votes et les propositions de determinations sont intégrés à l'observation
6
 * Les tags, les votes et les propositions de determinations sont intégrés à l'observation.
7
 *
7
 *
8
 * @category DEL
8
 * @category DEL
9
 * @package Services
9
 * @package Services
10
 * @subpackage Plantnet
10
 * @subpackage Plantnet
11
 * @version 0.1
11
 * @version 0.1
Line 18... Line 18...
18
 * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
18
 * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
19
 */
19
 */
Line 20... Line 20...
20
 
20
 
Line 21... Line -...
21
class Changements {
-
 
22
 
21
class Changements {
23
	private $indexImagesIds = array();
22
 
24
	private $conteneur;
23
	private $conteneur;
-
 
24
	private $navigation;
25
	private $navigation;
25
	private $bdd;
26
	private $bdd;
26
 
27
	private $parametres = array();
27
	private $parametres = array();
-
 
28
	private $ressources = array();
-
 
29
	private $date_defaut = '1900-01-01';
-
 
30
	private $idsObsImg = array();
Line 28... Line 31...
28
	private $ressources = array();
31
	private $infosObsImg = array();
29
	private $date_defaut = '1900-01-01';
32
 
30
 
33
 
31
	public function __construct(Conteneur $conteneur = null) {
34
	public function __construct(Conteneur $conteneur = null) {
Line 32... Line 35...
32
		/* restore_exception_handler(); */
35
		/* restore_exception_handler(); */
33
		/* restore_error_handler(); */
-
 
34
		/* ini_set("display_errors", "1"); */
36
		/* restore_error_handler(); */
35
 
37
		/* ini_set("display_errors", "1"); */
36
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
38
 
Line 37... Line 39...
37
		$this->conteneur->chargerConfiguration('config_plantnet.ini');
39
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
Line 54... Line 56...
54
		if (!isset($parametres['date'])) {
56
		if (!isset($parametres['date'])) {
55
			$this->parametres['date'] = $this->date_defaut;
57
			$this->parametres['date'] = $this->date_defaut;
56
		}
58
		}
Line 57... Line 59...
57
 
59
 
58
		// Lancement du service
60
		// Lancement du service
59
		$liaisons = $this->chargerLiaisons();
61
		$this->idsObsImg = $this->getIdsObsImg();
60
		$images = array();
62
		$infos = array();
-
 
63
		$total = 0;
-
 
64
		if ($this->idsObsImg) {
Line 61... Line -...
61
		$total = 0;
-
 
62
 
-
 
63
		if ($liaisons) {
-
 
64
			$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
-
 
65
			$total = (int) $compte['nbre'];
65
			$total = $this->getTotal();
66
 
66
 
67
			$imgdata = $this->recupererDonneeObs($liaisons);
-
 
68
			$obs = $this->regrouperObs($liaisons, $imgdata);
67
			$this->infosObsImg = $this->recupererInfos();
69
 
68
			$infos = $this->formaterInfos();
70
			$obs = $this->chargerPropositionPlusProbable($obs);
69
			$infos = $this->chargerPropositionPlusProbable($infos);
71
			$obs = $this->orderArray($obs);
-
 
72
		}
70
			$infos = $this->orderArray($infos);
Line 73... Line 71...
73
 
71
		}
74
		$this->navigation->setTotal($total);
72
		$this->navigation->setTotal($total);
75
 
73
 
76
		// Mettre en forme le résultat et l'envoyer pour affichage
-
 
77
		$resultat = new ResultatService();
74
		// Mettre en forme le résultat et l'envoyer pour affichage
78
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $obs);
75
		$resultat = new ResultatService();
Line 79... Line -...
79
 
-
 
80
		return $resultat;
-
 
81
	}
-
 
82
 
-
 
83
 
-
 
84
	private function orderArray(&$obs) {
-
 
85
		$ret = array();
-
 
86
 
-
 
87
		foreach ($obs as $o) {
-
 
88
			$ret[] = $o;
-
 
89
		}
-
 
90
 
-
 
91
		function cmp($a, $b) {
-
 
92
			return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
-
 
93
		}
-
 
94
 
-
 
95
		usort($ret, 'cmp');
76
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $infos);
96
		return $ret;
77
		return $resultat;
97
	}
78
	}
Line 98... Line 79...
98
 
79
 
99
	/*-------------------------------------------------------------------------------
80
	/*-------------------------------------------------------------------------------
100
								CHARGEMENT DES IMAGES
-
 
101
	--------------------------------------------------------------------------------*/
81
								CHARGEMENT DES IMAGES
102
 
82
	--------------------------------------------------------------------------------*/
103
	private function chargerLiaisons() {
83
 
Line 104... Line 84...
104
		$date_debut = $this->parametres['date'];
84
	private function getIdsObsImg() {
105
		$date_debut = '\'' . $date_debut . '\'';
85
		$date_debut = "'{$this->parametres['date']}'";
106
		$limite = @min(intval($this->parametres['navigation.limite']), 1000);
-
 
-
 
86
		$limite = @min(intval($this->parametres['navigation.limite']), 1000);
107
		$limite = $limite ? $limite : 10; // 0 => 10
87
		$limite = $limite ? $limite : 10; // 0 => 10
108
		$depart = intval(@$this->parametres['navigation.depart']);
-
 
109
 
88
		$depart = intval(@$this->parametres['navigation.depart']);
110
		$requete_sql =
89
 
111
			'select SQL_CALC_FOUND_ROWS vdi.id_observation, vdi.id_image, '.
90
		$requete =
112
 
91
			'SELECT SQL_CALC_FOUND_ROWS p.id_observation, p.id_image, '.
113
			'GROUP_CONCAT(del_image_vote.valeur) as votes, GROUP_CONCAT(DISTINCT tag) as tags, '.
92
			'GROUP_CONCAT(iv.valeur) AS votes, '.
114
 
93
			'GROUP_CONCAT(DISTINCT tag) AS tags, '.
115
			'GREATEST('.
94
			'GREATEST('.
116
			'IFNULL(vdi.date_creation, \''.$this->date_defaut.'\'), '.
95
			'	IFNULL(p.date_creation, \''.$this->date_defaut.'\'), '.
117
			'IFNULL(vdi.date_modification, \''.$this->date_defaut.'\'), '.
96
			'	IFNULL(p.date_modification, \''.$this->date_defaut.'\'), '.
-
 
97
			'	IFNULL(MAX(it.date), \''.$this->date_defaut.'\'), '.
118
			'IFNULL(MAX(del_image_tag.date), \''.$this->date_defaut.'\'), '.
98
			'	IFNULL(MAX(it.date_modification), \''.$this->date_defaut.'\'), '.
119
			'IFNULL(MAX(del_image_tag.date_modification), \''.$this->date_defaut.'\'), '.
-
 
120
			'IFNULL(MAX(del_image_vote.date), \''.$this->date_defaut.'\'), '.
99
			'	IFNULL(MAX(iv.date), \''.$this->date_defaut.'\'), '.
-
 
100
			'	IFNULL(MAX(c.date), \''.$this->date_defaut.'\'), '.
121
			'IFNULL(MAX(del_commentaire.date), \''.$this->date_defaut.'\'), '.
101
			'	IFNULL(MAX(cv.date), \''.$this->date_defaut.'\')) AS modif_date '.
-
 
102
 
122
			'IFNULL(MAX(del_commentaire_vote.date), \''.$this->date_defaut.'\')) as modif_date '.
103
			'FROM del_plantnet AS p '.
-
 
104
			'	LEFT JOIN del_image_vote AS iv '.
123
 
105
			'		ON (id_image = iv.ce_image AND iv.ce_protocole = 3) '.
124
			'from v_del_image as vdi '.
-
 
125
 
106
			'	LEFT JOIN del_image_tag AS it '.
126
			'left join del_image_vote on del_image_vote.ce_image=id_image and del_image_vote.ce_protocole=3 '.
-
 
127
			'left join del_image_tag on del_image_tag.ce_image=id_image and del_image_tag.actif=1 '.
107
			'		ON (id_image = it.ce_image AND it.actif = 1) '.
128
			'left join del_commentaire on del_commentaire.ce_observation=id_observation '.
108
			'	LEFT JOIN del_commentaire AS c '.
129
			'left join del_commentaire_vote on del_commentaire_vote.ce_proposition=del_commentaire.id_commentaire '.
109
			'		ON (id_observation = c.ce_observation) '.
130
 
110
			'	LEFT JOIN del_commentaire_vote AS cv '.
131
			'group by id_image, id_observation '.
111
			'		ON (c.id_commentaire = cv.ce_proposition) '.
132
 
112
			'GROUP BY id_image, id_observation '.
133
			'having MAX(vdi.date_creation) >= '.$date_debut.' or '.
113
			'HAVING MAX(p.date_creation) >= '.$date_debut.' '.
134
			'       MAX(vdi.date_modification) >= '.$date_debut.' or '.
114
			'	OR MAX(p.date_modification) >= '.$date_debut.' '.
-
 
115
			'	OR MAX(it.date) >= '.$date_debut.' '.
135
			'       MAX(del_image_tag.date) >= '.$date_debut.' or '.
116
			'	OR MAX(it.date_modification) >= '.$date_debut.' '.
136
			'       MAX(del_image_tag.date_modification) >= '.$date_debut.' or '.
-
 
137
			'       MAX(del_image_vote.date) >= '.$date_debut.' or '.
117
			'	OR MAX(iv.date) >= '.$date_debut.' '.
138
			'       MAX(del_commentaire.date) >= '.$date_debut.' or '.
-
 
139
			'       MAX(del_commentaire_vote.date) >= '.$date_debut.' '.
-
 
140
			'order by modif_date DESC '.
-
 
141
			'limit '.$depart.', '.$limite.' -- '.__FILE__.':'.__LINE__;
118
			'	OR MAX(c.date) >= '.$date_debut.' '.
142
 
119
			'	OR MAX(cv.date) >= '.$date_debut.' '.
143
		//echo $requete_sql; exit;
120
			'ORDER BY modif_date DESC '.
-
 
121
			'LIMIT '.$depart.', '.$limite.
-
 
122
			' -- '.__FILE__.':'.__LINE__;
-
 
123
		//echo $requete; exit;
-
 
124
		// GROUP BY (très couteux) car multiples observations associées à une image
-
 
125
		// charlie est ici :-)
-
 
126
		// eg: 16150,16185,16245,16246,118995,129989
-
 
127
		return $this->bdd->recupererTous($requete);
144
 
128
	}
Line 145... Line 129...
145
		return $this->bdd->recupererTous($requete_sql);
129
 
146
 
130
	private function getTotal() {
147
		// GROUP BY (très couteux) car multiples observations associées à une image
131
		$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
148
		// charlie est ici :-)
132
		$total = (int) $compte['nbre'];
149
		// eg: 16150,16185,16245,16246,118995,129989
133
		return $total;
150
	}
134
	}
151
 
135
 
152
	// recupere les donnée associées (fait en 2 requetes pour optimiser)
136
	// recupere les donnée associées (fait en 2 requetes pour optimiser)
-
 
137
	private function recupererInfos() {
Line -... Line 138...
-
 
138
		// recuperer les ids
-
 
139
		$idsImg = array();
-
 
140
		foreach ($this->idsObsImg as $ids) {
-
 
141
			$id = $ids['id_image'];
-
 
142
			$idsImg[] = $id;
-
 
143
		}
-
 
144
		$idsImgConcat = implode(',', $idsImg);
-
 
145
 
-
 
146
		$requete = 'SELECT '.
-
 
147
			'id_observation, id_image, '.
-
 
148
			'nom_sel, '.
-
 
149
			'nom_referentiel, nom_ret, nom_ret_nn, nt, famille, '.
153
	private function recupererDonneeObs(&$liaisons) {
150
			'zone_geo, latitude, longitude, '.
154
		// recuperer les ids
151
			'date_observation, date_creation, date_transmission, '.
155
		$ids = array();
-
 
156
		foreach ($liaisons as $img) {
-
 
157
			$id = $img['id_image'];
-
 
158
			$ids[] = $id;
-
 
159
		}
-
 
160
 
-
 
161
		// recuperer les donnees
-
 
162
		$resultats = $this->bdd->recupererTous(sprintf(
-
 
163
			'SELECT  '.
-
 
164
			'vdi.id_observation, vdi.id_image, '.
-
 
165
			'vdi.nom_sel, '.
-
 
166
			'vdi.nom_referentiel, vdi.nom_ret, vdi.nom_ret_nn, vdi.nt, vdi.famille, '.
-
 
167
			'vdi.zone_geo, vdi.latitude, vdi.longitude, '.
-
 
168
			'vdi.date_observation, vdi.date_creation, vdi.date_transmission, '.
-
 
169
			'vdi.mots_cles_texte as mots_cles_texte, '.
-
 
Line 170... Line 152...
170
			'vdi.i_mots_cles_texte as mots_cles_texte_image, '.
152
			'mots_cles_texte, '.
171
 
153
			'ce_utilisateur, prenom_utilisateur, nom_utilisateur, courriel_utilisateur, '.
172
			'vdi.ce_utilisateur as ce_utilisateur, '.
154
			'i_mots_cles_texte AS mots_cles_texte_image, nom_original AS nom_image '.
173
			'vdi.prenom_utilisateur, vdi.courriel_utilisateur, vdi.nom_utilisateur, vdi.nom_original as nom_image '.
155
			'FROM del_plantnet AS p '.
174
 
156
			"WHERE id_image IN ($idsImgConcat) ".
175
			'FROM v_del_image as vdi '.
157
			' -- '.__FILE__.':'.__LINE__;
176
			'WHERE vdi.id_image IN (%s) '.
158
		// recuperer les donnees
177
			'', implode(',', $ids)));
159
		$resultats = $this->bdd->recupererTous($requete);
Line 178... Line 160...
178
 
160
 
179
		// regroupe les données par id_image
161
		// regroupe les données par id_image
180
		$img_data = array();
162
		$img_data = array();
181
		foreach ($resultats as $img) {
163
		foreach ($resultats as $infos) {
182
			$id = $img['id_image'];
164
			$idImg = $infos['id_image'];
183
			$img_data[$id] = $img;
165
			$img_data[$idImg] = $infos;
184
		}
166
		}
185
		return $img_data;
167
		return $img_data;
186
	}
168
	}
187
 
169
 
Line 188... Line 170...
188
	/**
170
	/**
Line 189... Line 171...
189
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
171
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
190
	* @param $liaisons les liaisons de la table del_obs_images
172
	* @param $liaisons les liaisons de la table del_obs_images
191
	*/
173
	*/
Line 211... Line 193...
211
			$obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($imgdata['mots_cles_texte']);
193
			$obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($imgdata['mots_cles_texte']);
Line 212... Line 194...
212
 
194
 
213
			$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
195
			$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
214
			$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
196
			$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
215
			$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
197
			$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
Line 216... Line 198...
216
			$obs[$idobs]['date_changement'] = $img['modif_date'];
198
			$obs[$idobs]['date_changement'] = $ids['modif_date'];
217
 
199
 
218
			$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
200
			$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
219
			$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
201
			$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
Line 229... Line 211...
229
			if (!isset($obs[$idobs]['images'])) {
211
			if (!isset($obs[$idobs]['images'])) {
230
				$obs[$idobs]['images'] = array();
212
				$obs[$idobs]['images'] = array();
231
			}
213
			}
Line 232... Line 214...
232
 
214
 
233
			$img_obj = array(
215
			$img_obj = array(
234
				'id_image' => $img['id_image'],
216
				'id_image' => $idimg,
235
				'nom_image' => $imgdata['nom_image'],
217
				'nom_image' => $imgdata['nom_image'],
236
				'url' => sprintf('http://api.tela-botanica.org/img:%09dO.jpg', $img['id_image']),
218
				'url' => sprintf('http://api.tela-botanica.org/img:%09dO.jpg', $idimg),
237
				'votes' => array_map('intval', explode(',', $img['votes'])),
219
				'votes' => array_map('intval', explode(',', $ids['votes'])),
238
				'tags' => explode(',', $img['tags']),
220
				'tags' => explode(',', $ids['tags']),
239
				'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
221
				'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
240
				);
222
				);
241
			// push
223
			// push
242
			$obs[$idobs]['images'][] = $img_obj;
224
			$obs[$idobs]['images'][] = $img_obj;
Line 247... Line 229...
247
	/**
229
	/**
248
	 * Charger les votes pour chaque image
230
	 * Charger les votes pour chaque image
249
	 */
231
	 */
250
	private function chargerPropositionPlusProbable(&$obs) {
232
	private function chargerPropositionPlusProbable(&$obs) {
251
		$obsIds = array_keys($obs);
233
		$obsIds = array_keys($obs);
-
 
234
		$idsObsConcat = implode(',', $obsIds);
Line 252... Line -...
252
 
-
 
253
		$resultats = $this->bdd->recupererTous(sprintf(
235
 
254
			'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, del_commentaire_vote.ce_utilisateur '.
236
		$requete = 'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, cv.ce_utilisateur '.
255
			'FROM del_commentaire_vote, del_commentaire '.
237
			'FROM del_commentaire_vote AS cv, del_commentaire AS c '.
256
			'WHERE ce_observation IN (%s) '.
238
			"WHERE ce_observation IN ($idsObsConcat) ".
257
			'AND nom_sel IS NOT NULL '.
239
			'AND nom_sel IS NOT NULL '.
258
			'AND del_commentaire.id_commentaire=del_commentaire_vote.ce_proposition '.
240
			'AND c.id_commentaire = cv.ce_proposition '.
259
			'', implode(',', $obsIds)));
-
 
260
 
241
			' -- '.__FILE__.':'.__LINE__;
Line 261... Line 242...
261
		$votes = array(); // map ce_proposition -> score
242
		$resultats = $this->bdd->recupererTous($requete);
262
 
243
 
263
		// calcul des votes
244
		// calcul des votes
-
 
245
		// un vote identifié a un facteur de 3
264
		// un vote identifié a un facteur de 3
246
		// additionne tous les vote par ce_proposition
265
		// additionne tous les vote par ce_proposition
247
		$votes = array(); // map ce_proposition -> score
266
		foreach ($resultats as $vote) {
248
		foreach ($resultats as $vote) {
267
			if (!isset($votes[$vote['id_commentaire']])) {
249
			if (!isset($votes[$vote['id_commentaire']])) {
268
				$votes[$vote['id_commentaire']] = 0;
250
				$votes[$vote['id_commentaire']] = 0;
Line 285... Line 267...
285
				'nn' => $vote['nom_sel_nn']);
267
				'nn' => $vote['nom_sel_nn']);
286
		}
268
		}
287
		return $obs;
269
		return $obs;
288
	}
270
	}
Line -... Line 271...
-
 
271
 
-
 
272
	private function orderArray(&$obs) {
-
 
273
		$ret = array();
-
 
274
		foreach ($obs as $o) {
-
 
275
			$ret[] = $o;
-
 
276
		}
-
 
277
 
-
 
278
		function cmp($a, $b) {
-
 
279
			return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
-
 
280
		}
-
 
281
 
-
 
282
		usort($ret, 'cmp');
-
 
283
		return $ret;
-
 
284
	}
-
 
285
 
289
 
286
 
290
	/*-------------------------------------------------------------------------------
287
	/*-------------------------------------------------------------------------------
291
								FORMATER ET METTRE EN FORME
288
								FORMATER ET METTRE EN FORME
Line 292... Line 289...
292
	--------------------------------------------------------------------------------*/
289
	--------------------------------------------------------------------------------*/