Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1004 | Rev 1068 | 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() {
1060 aurelien 79
		if (Config::get(Registre::get('parametres.referentiel').'.baseImages') != "") {
80
			$this->imagesService->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseImages'));
81
			$nns = $this->extraireNnDesNoms();
82
			$urls = $this->imagesService->getUrlsImagesParIdsNoms($nns);
83
			$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReferentielDesUrls($urls);
84
		}
199 delphine 85
	}
86
 
87
	private function extraireNnDesNoms() {
88
		$nns = array();
89
		foreach ($this->noms as $id => $nom) {
90
			if (array_key_exists('nom_retenu.code', $nom)) {
91
				if (in_array($nom['nom_retenu.code'], $nns) == false) {
931 delphine 92
					$idAAjouter = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
199 delphine 93
					if (is_numeric($idAAjouter)) {
94
						$nns[] = $idAAjouter;
95
					}
96
				}
97
			}
98
		}
99
		return $nns;
100
	}
101
 
931 delphine 102
	private function supprimerCodeReferentielDesUrls($urls) {
313 jpm 103
		$urlsNettoyees = array();
199 delphine 104
		foreach ($urls as $id => $url) {
931 delphine 105
			$id = $this->supprimerCodeReferentiel($id);
199 delphine 106
			$urlsNettoyees[$id] = $url;
107
		}
108
		return $urlsNettoyees;
109
	}
110
 
931 delphine 111
	private function supprimerCodeReferentiel($chaine) {
112
		$codeReferentiel = $this->parametres->referentielCourant.'.';
113
		$chaine = str_replace($codeReferentiel, '', $chaine);
312 jpm 114
		return $chaine;
115
	}
798 mathilde 116
 
931 delphine 117
	private function chargerRepartition() {
1004 aurelien 118
		$numsNomsASynonymes = $this->extraireNumerosNomSynonymes();
931 delphine 119
		if (Config::get(Registre::get('parametres.referentiel').'.baseRepartition') != "") {
120
			$this->apiCartes->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseRepartition'));
121
			$this->apiCartes->setLargeur('108x101');
122
			$urls = array();
123
			foreach ($this->noms as $nom) {
124
				if (array_key_exists('nom_retenu.code', $nom)) {
125
					$id = $nom['nom_retenu.code'];
126
					$id = $this->supprimerCodeReferentielAvecNn($id);
1004 aurelien 127
					$idsNoms = $numsNomsASynonymes[$id];
128
					$this->apiCartes->setId("nn:".implode(',',$idsNoms)."");
931 delphine 129
					if (array_key_exists($id, $urls) == false) {
130
						$urls[$id] = $this->apiCartes->getUrlPng();
131
					}
132
				}
133
			}
134
			$this->infosPourTpl['repartition']['urls'] = $urls;
135
		}
136
	}
137
 
1004 aurelien 138
	private function extraireNumerosNomSynonymes() {
139
		$numNomsASynonyme = array();
140
		//TODO: pourrait surement être bien plus efficace
141
		// et ne faire qu'une seule boucle
142
		foreach ($this->noms as $id => $nom) {
143
			$num_nom_retenu = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
144
			if(!isset($numNomsASynonyme[$num_nom_retenu])) {
145
				$numNomsASynonyme[$num_nom_retenu] = array();
146
			}
147
			$numNomsASynonyme[$num_nom_retenu][] = $id;
148
			if(!in_array($num_nom_retenu,$numNomsASynonyme[$num_nom_retenu])) {
149
				$numNomsASynonyme[$num_nom_retenu][] = $num_nom_retenu;
150
			}
151
		}
152
		foreach($this->noms as $id => $nom) {
153
			$num_nom_retenu = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
154
			if($id != $num_nom_retenu) {
155
				$numNomsASynonyme[$id] = $numNomsASynonyme[$num_nom_retenu];
156
			}
157
		}
158
		return $numNomsASynonyme;
159
	}
160
 
798 mathilde 161
	function supprimerAccents($chaine){
162
		return strtr($chaine,array('à' => 'a','á' => 'a','â' => 'a','ã' => 'a','ä' => 'a',
163
										'ç' => 'c',
164
										'è' => 'e','é' => 'e','ê' => 'e','ë' => 'e',
165
										'ì' => 'i','í' => 'i','î' => 'i','ï' => 'i',
166
										'ñ' => 'n',
167
										'ò' => 'o', 'ó' => 'o' , 'ô' => 'o', 'õ' => 'o', 'ö' => 'o',
168
										'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
169
										'ý' => 'y', 'ÿ' => 'y'));
170
	}
171
 
312 jpm 172
 
199 delphine 173
	private function extraireInfosNomsPourTplDetermination() {
174
		$taxons = array();
175
		foreach ($this->noms as $idNomCourant => $nom) {
931 delphine 176
			$nn = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
798 mathilde 177
			$nom_min = strtolower($nom['nom_vernaculaire']);
178
			$nom_ss_accent = $this->supprimerAccents($nom_min);
179
			if (preg_match('/^'.strtolower($this->parametres->masqueRecherche).' |^'.strtolower($this->parametres->masqueRecherche).'$/', $nom_ss_accent )) {
180
				//au moins un debute par la requête
181
				if (isset($taxons) && array_key_exists($nn, $taxons[0]) == false) {
790 mathilde 182
					$taxons[0][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
183
				}
184
				$taxons[0][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
185
			} else {
186
				//contient
931 delphine 187
				if (isset($taxons) && (!isset($taxons[1]) || array_key_exists($nn, $taxons[1]) == false)) {
790 mathilde 188
					$taxons[1][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
189
				}
190
				$taxons[1][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
199 delphine 191
			}
192
		}
790 mathilde 193
		$this->infosPourTpl['noms'] = isset($taxons)  ? $taxons : false;
199 delphine 194
	}
790 mathilde 195
 
196
	private function renvoyerInfosNomVerna($valeurs) {
197
		$nom_verna = array();
198
		$nom_verna['nn'] = $valeurs['id'];
199
		$nom_verna['nom_vernaculaire'] = $valeurs['nom_vernaculaire'];
200
		return $nom_verna;
201
	}
202
 
203
	private function renvoyerInfosTaxon($nomSci, $nn) {
204
		$taxon = array();
205
		$taxon['nomSci'] = $nomSci;
206
		$taxon['urlFiche'] = $this->urls->obtenirUrlFiche($nn, $this->parametres->typeNom, $this->parametres->masqueRecherche);
207
		$taxon['repartition_vignette'] = $this->chargerRepartition($nn);
208
		return $taxon;
209
	}
312 jpm 210
 
931 delphine 211
	private function supprimerCodeReferentielAvecNn($nn) {
212
		$codeReferentiel = $this->parametres->referentielCourant.'.nn:';
213
		return str_replace($codeReferentiel, '', $nn);
199 delphine 214
	}
215
 
790 mathilde 216
	//tri alphabétique des noms scientifiques par catégorie (débute par , contient )
161 jpm 217
	public function trier() {
790 mathilde 218
		$verna = array();
219
		foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs) {
220
			$verna += $this->classerAlphabetiquement('nomSci', $valeurs);
221
		}
222
		$this->infosPourTpl['noms'] = $verna;
161 jpm 223
	}
790 mathilde 224
 
225
	private function classerAlphabetiquement($champs, $valeurs) {
226
		$this->trieur->setTableau($valeurs);
931 delphine 227
		$this->trieur->setChampsEtOrdres(array($champs => 'nat'));
790 mathilde 228
		return $this->trieur->trier();
229
	}
230
 
798 mathilde 231
 
161 jpm 232
 
233
	public function surligner() {
202 delphine 234
		$this->definirMotsASurligner();
235
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
236
			foreach ($nom['nomVerna'] as $idVerna => $nomVerna) {
237
				$nom['nomVerna'][$idVerna]['nom_vernaculaire'] = $this->surlignerMotsMasqueRecherche($nomVerna['nom_vernaculaire']);
238
			}
239
			$this->infosPourTpl['noms'][$idNom] = $nom;
240
		}
161 jpm 241
	}
312 jpm 242
 
202 delphine 243
	private function definirMotsASurligner() {
207 delphine 244
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
798 mathilde 245
 
202 delphine 246
	}
790 mathilde 247
 
202 delphine 248
	private function surlignerMotsMasqueRecherche($nom) {
249
		$this->surligneur->setTexte($nom);
250
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
251
		return $nom;
252
	}
161 jpm 253
}
254
?>