Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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

Rev Author Line No. Line
161 jpm 1
<?php
2
class DeterminationVernaFormateur implements Formateur {
3
 
4
	const TPL_VUE = 'determination_verna';
5
 
6
	private $parametres = null;
7
	private $surligneur = null;
8
	private $trieur = null;
9
	private $urls = null;
10
	private $fusioneur = null;
11
	private $manipulateurDeChaine = null;
12
	private $imagesService = null;
13
 
14
	private $motsASurligner = array();
15
	private $noms = array();
16
	private $infosPourTpl = array();
747 aurelien 17
 
18
	private $conteneur = null;
19
	private $apiCartes = null;
161 jpm 20
 
21
	public function __construct(ParametresResultats $parametres, Array $resultats,
22
		Surligneur $surligneur = null, Trieur $trieur = null, AppUrls $urls = null,
23
		ChaineManipulateur $manipulateurDeChaine = null, Images $imagesService = null) {
24
 
25
		$this->parametres = $parametres;
26
		$this->noms = $resultats['resultat'];
27
		$this->surligneur = (is_null($surligneur)) ? new Surligneur() : $surligneur;
28
		$this->trieur = (is_null($trieur)) ? new Trieur() : $trieur;
29
		$this->urls = (is_null($urls)) ? new AppUrls() : $urls;
30
		$this->manipulateurDeChaine = is_null($manipulateurDeChaine) ? new ChaineManipulateur() : $manipulateurDeChaine;
31
		$this->imagesService = is_null($imagesService) ? new Images($this->parametres->projetImg) : $imagesService;
747 aurelien 32
 
33
		$this->conteneur = new Conteneur();
34
		$this->apiCartes = $this->conteneur->getApiCartes();
161 jpm 35
	}
312 jpm 36
 
161 jpm 37
	public function getTplInfos() {
38
		return $this->infosPourTpl;
39
	}
40
 
41
	public function getTplNom() {
42
		return self::TPL_VUE;
43
	}
44
 
45
	public function formater() {
931 delphine 46
		$this->obtenirUrlsDessins();
199 delphine 47
		$this->obtenirUrlsPhotos();
931 delphine 48
		$this->chargerRepartition();
199 delphine 49
		$this->extraireInfosNomsPourTplDetermination();
161 jpm 50
	}
618 mathilde 51
 
931 delphine 52
	private function obtenirUrlsDessins() {
53
		if (Config::get(Registre::get('parametres.referentiel').'.baseDessins') != "") {
54
			$this->extraireInfosTaxons();
55
			$this->imagesService->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseDessins'));
56
			$tax = implode(',', $this->infosPourTpl['taxons']);
57
			$this->imagesService->setNnTaxon($tax);
58
			$costeImg = $this->imagesService->getInfosImagesTaxons();
59
			if (!empty($costeImg)) {
60
				foreach ($costeImg as  $infos) {
61
					$num_taxon = $infos['num_taxonomique'];
62
					$images[$num_taxon][] = $infos['binaire.href'];
63
					$this->infosPourTpl['imagesCoste'] = $images;
64
				}
618 mathilde 65
			}
66
		}
67
	}
68
 
69
	private function extraireInfosTaxons() {
70
		foreach ($this->noms as $id => $nom ) {
71
			if (array_key_exists('num_taxon', $nom)
72
				&& array_key_exists('taxon', $nom)) {
73
				$this->infosPourTpl['taxons'][$nom['taxon']] = $nom['num_taxon'];
74
			}
75
		}
76
	}
161 jpm 77
 
199 delphine 78
	private function obtenirUrlsPhotos() {
79
		$nns = $this->extraireNnDesNoms();
80
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($nns);
931 delphine 81
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReferentielDesUrls($urls);
199 delphine 82
	}
83
 
84
	private function extraireNnDesNoms() {
85
		$nns = array();
86
		foreach ($this->noms as $id => $nom) {
87
			if (array_key_exists('nom_retenu.code', $nom)) {
88
				if (in_array($nom['nom_retenu.code'], $nns) == false) {
931 delphine 89
					$idAAjouter = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
199 delphine 90
					if (is_numeric($idAAjouter)) {
91
						$nns[] = $idAAjouter;
92
					}
93
				}
94
			}
95
		}
96
		return $nns;
97
	}
98
 
931 delphine 99
	private function supprimerCodeReferentielDesUrls($urls) {
313 jpm 100
		$urlsNettoyees = array();
199 delphine 101
		foreach ($urls as $id => $url) {
931 delphine 102
			$id = $this->supprimerCodeReferentiel($id);
199 delphine 103
			$urlsNettoyees[$id] = $url;
104
		}
105
		return $urlsNettoyees;
106
	}
107
 
931 delphine 108
	private function supprimerCodeReferentiel($chaine) {
109
		$codeReferentiel = $this->parametres->referentielCourant.'.';
110
		$chaine = str_replace($codeReferentiel, '', $chaine);
312 jpm 111
		return $chaine;
112
	}
798 mathilde 113
 
931 delphine 114
	private function chargerRepartition() {
1004 aurelien 115
		$numsNomsASynonymes = $this->extraireNumerosNomSynonymes();
931 delphine 116
		if (Config::get(Registre::get('parametres.referentiel').'.baseRepartition') != "") {
117
			$this->apiCartes->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseRepartition'));
118
			$this->apiCartes->setLargeur('108x101');
119
			$urls = array();
120
			foreach ($this->noms as $nom) {
121
				if (array_key_exists('nom_retenu.code', $nom)) {
122
					$id = $nom['nom_retenu.code'];
123
					$id = $this->supprimerCodeReferentielAvecNn($id);
1004 aurelien 124
					$idsNoms = $numsNomsASynonymes[$id];
125
					$this->apiCartes->setId("nn:".implode(',',$idsNoms)."");
931 delphine 126
					if (array_key_exists($id, $urls) == false) {
127
						$urls[$id] = $this->apiCartes->getUrlPng();
128
					}
129
				}
130
			}
131
			$this->infosPourTpl['repartition']['urls'] = $urls;
132
		}
133
	}
134
 
1004 aurelien 135
	private function extraireNumerosNomSynonymes() {
136
		$numNomsASynonyme = array();
137
		//TODO: pourrait surement être bien plus efficace
138
		// et ne faire qu'une seule boucle
139
		foreach ($this->noms as $id => $nom) {
140
			$num_nom_retenu = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
141
			if(!isset($numNomsASynonyme[$num_nom_retenu])) {
142
				$numNomsASynonyme[$num_nom_retenu] = array();
143
			}
144
			$numNomsASynonyme[$num_nom_retenu][] = $id;
145
			if(!in_array($num_nom_retenu,$numNomsASynonyme[$num_nom_retenu])) {
146
				$numNomsASynonyme[$num_nom_retenu][] = $num_nom_retenu;
147
			}
148
		}
149
		foreach($this->noms as $id => $nom) {
150
			$num_nom_retenu = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
151
			if($id != $num_nom_retenu) {
152
				$numNomsASynonyme[$id] = $numNomsASynonyme[$num_nom_retenu];
153
			}
154
		}
155
		return $numNomsASynonyme;
156
	}
157
 
798 mathilde 158
	function supprimerAccents($chaine){
159
		return strtr($chaine,array('à' => 'a','á' => 'a','â' => 'a','ã' => 'a','ä' => 'a',
160
										'ç' => 'c',
161
										'è' => 'e','é' => 'e','ê' => 'e','ë' => 'e',
162
										'ì' => 'i','í' => 'i','î' => 'i','ï' => 'i',
163
										'ñ' => 'n',
164
										'ò' => 'o', 'ó' => 'o' , 'ô' => 'o', 'õ' => 'o', 'ö' => 'o',
165
										'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
166
										'ý' => 'y', 'ÿ' => 'y'));
167
	}
168
 
312 jpm 169
 
199 delphine 170
	private function extraireInfosNomsPourTplDetermination() {
171
		$taxons = array();
172
		foreach ($this->noms as $idNomCourant => $nom) {
931 delphine 173
			$nn = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
798 mathilde 174
			$nom_min = strtolower($nom['nom_vernaculaire']);
175
			$nom_ss_accent = $this->supprimerAccents($nom_min);
176
			if (preg_match('/^'.strtolower($this->parametres->masqueRecherche).' |^'.strtolower($this->parametres->masqueRecherche).'$/', $nom_ss_accent )) {
177
				//au moins un debute par la requête
178
				if (isset($taxons) && array_key_exists($nn, $taxons[0]) == false) {
790 mathilde 179
					$taxons[0][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
180
				}
181
				$taxons[0][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
182
			} else {
183
				//contient
931 delphine 184
				if (isset($taxons) && (!isset($taxons[1]) || array_key_exists($nn, $taxons[1]) == false)) {
790 mathilde 185
					$taxons[1][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
186
				}
187
				$taxons[1][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
199 delphine 188
			}
189
		}
790 mathilde 190
		$this->infosPourTpl['noms'] = isset($taxons)  ? $taxons : false;
199 delphine 191
	}
790 mathilde 192
 
193
	private function renvoyerInfosNomVerna($valeurs) {
194
		$nom_verna = array();
195
		$nom_verna['nn'] = $valeurs['id'];
196
		$nom_verna['nom_vernaculaire'] = $valeurs['nom_vernaculaire'];
197
		return $nom_verna;
198
	}
199
 
200
	private function renvoyerInfosTaxon($nomSci, $nn) {
201
		$taxon = array();
202
		$taxon['nomSci'] = $nomSci;
203
		$taxon['urlFiche'] = $this->urls->obtenirUrlFiche($nn, $this->parametres->typeNom, $this->parametres->masqueRecherche);
204
		$taxon['repartition_vignette'] = $this->chargerRepartition($nn);
205
		return $taxon;
206
	}
312 jpm 207
 
931 delphine 208
	private function supprimerCodeReferentielAvecNn($nn) {
209
		$codeReferentiel = $this->parametres->referentielCourant.'.nn:';
210
		return str_replace($codeReferentiel, '', $nn);
199 delphine 211
	}
212
 
790 mathilde 213
	//tri alphabétique des noms scientifiques par catégorie (débute par , contient )
161 jpm 214
	public function trier() {
790 mathilde 215
		$verna = array();
216
		foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs) {
217
			$verna += $this->classerAlphabetiquement('nomSci', $valeurs);
218
		}
219
		$this->infosPourTpl['noms'] = $verna;
161 jpm 220
	}
790 mathilde 221
 
222
	private function classerAlphabetiquement($champs, $valeurs) {
223
		$this->trieur->setTableau($valeurs);
931 delphine 224
		$this->trieur->setChampsEtOrdres(array($champs => 'nat'));
790 mathilde 225
		return $this->trieur->trier();
226
	}
227
 
798 mathilde 228
 
161 jpm 229
 
230
	public function surligner() {
202 delphine 231
		$this->definirMotsASurligner();
232
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
233
			foreach ($nom['nomVerna'] as $idVerna => $nomVerna) {
234
				$nom['nomVerna'][$idVerna]['nom_vernaculaire'] = $this->surlignerMotsMasqueRecherche($nomVerna['nom_vernaculaire']);
235
			}
236
			$this->infosPourTpl['noms'][$idNom] = $nom;
237
		}
161 jpm 238
	}
312 jpm 239
 
202 delphine 240
	private function definirMotsASurligner() {
207 delphine 241
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
798 mathilde 242
 
202 delphine 243
	}
790 mathilde 244
 
202 delphine 245
	private function surlignerMotsMasqueRecherche($nom) {
246
		$this->surligneur->setTexte($nom);
247
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
248
		return $nom;
249
	}
161 jpm 250
}
251
?>