Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 747 | Rev 798 | 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
 
618 mathilde 37
	private function chargerRepartition() {
747 aurelien 38
		$this->apiCartes->setProjet('chorodep');
39
		$this->apiCartes->setLargeur('108x101');
312 jpm 40
		$urls = array();
41
		foreach ($this->noms as $nom) {
42
			if (array_key_exists('nom_retenu.code', $nom)) {
43
				$id = $nom['nom_retenu.code'];
313 jpm 44
				$id = $this->supprimerCodeReftaxAvecNn($id);
747 aurelien 45
				$this->apiCartes->setId("nn:$id");
312 jpm 46
				if (array_key_exists($id, $urls) == false) {
747 aurelien 47
					$urls[$id] = $this->apiCartes->getUrlPng();
312 jpm 48
				}
49
			}
50
		}
51
		$this->infosPourTpl['repartition']['urls'] = $urls;
282 gduche 52
	}
161 jpm 53
 
54
	public function getTplInfos() {
55
		return $this->infosPourTpl;
56
	}
57
 
58
	public function getTplNom() {
59
		return self::TPL_VUE;
60
	}
61
 
62
	public function formater() {
199 delphine 63
		$this->obtenirUrlsPhotos();
64
		$this->extraireInfosNomsPourTplDetermination();
618 mathilde 65
		$this->obtenirUrlsImagesCoste();
66
		$this->chargerRepartition();
161 jpm 67
	}
618 mathilde 68
 
69
	private function obtenirUrlsImagesCoste() {
70
		$this->extraireInfosTaxons();
71
		$this->imagesService->setProjet('coste');
736 aurelien 72
		$tax = implode(',', $this->infosPourTpl['taxons']);
73
		$this->imagesService->setNnTaxon($tax);
74
		$costeImg = $this->imagesService->getInfosImagesTaxons();
75
		if (!empty($costeImg)) {
76
			foreach ($costeImg as  $infos) {
77
				$num_taxon = $infos['num_taxonomique'];
78
				$images[$num_taxon][] = $infos['binaire.href'];
79
				$this->infosPourTpl['imagesCoste'] = $images;
618 mathilde 80
			}
81
		}
82
	}
83
 
84
	private function extraireInfosTaxons() {
85
		foreach ($this->noms as $id => $nom ) {
86
			if (array_key_exists('num_taxon', $nom)
87
				&& array_key_exists('taxon', $nom)) {
88
				$this->infosPourTpl['taxons'][$nom['taxon']] = $nom['num_taxon'];
89
			}
90
		}
91
	}
161 jpm 92
 
199 delphine 93
	private function obtenirUrlsPhotos() {
94
		$nns = $this->extraireNnDesNoms();
95
		$urls = $this->imagesService->getUrlsImagesParIdsNoms($nns);
96
		$this->infosPourTpl['imagesUrls'] = $this->supprimerCodeReftaxDesUrls($urls);
97
	}
98
 
99
	private function extraireNnDesNoms() {
100
		$nns = array();
101
		foreach ($this->noms as $id => $nom) {
102
			if (array_key_exists('nom_retenu.code', $nom)) {
103
				if (in_array($nom['nom_retenu.code'], $nns) == false) {
313 jpm 104
					$idAAjouter = $this->supprimerCodeReftaxAvecNn($nom['nom_retenu.code']);
199 delphine 105
					if (is_numeric($idAAjouter)) {
106
						$nns[] = $idAAjouter;
107
					}
108
				}
109
			}
110
		}
111
		return $nns;
112
	}
113
 
114
	private function supprimerCodeReftaxDesUrls($urls) {
313 jpm 115
		$urlsNettoyees = array();
199 delphine 116
		foreach ($urls as $id => $url) {
312 jpm 117
			$id = $this->supprimerCodeReftax($id);
199 delphine 118
			$urlsNettoyees[$id] = $url;
119
		}
120
		return $urlsNettoyees;
121
	}
122
 
312 jpm 123
	private function supprimerCodeReftax($chaine) {
124
		$codeReftax = $this->parametres->reftaxCourant.'.';
125
		$chaine = str_replace($codeReftax, '', $chaine);
126
		return $chaine;
127
	}
128
 
129
 
790 mathilde 130
 
199 delphine 131
	private function extraireInfosNomsPourTplDetermination() {
132
		$taxons = array();
133
		foreach ($this->noms as $idNomCourant => $nom) {
313 jpm 134
			$nn = $this->supprimerCodeReftaxAvecNn($nom['nom_retenu.code']);
790 mathilde 135
			if (preg_match('/^'. strtolower($this->parametres->masqueRecherche).' |^'. strtolower($this->parametres->masqueRecherche).'$/',  strtolower($nom['nom_vernaculaire']))) {
136
				//debute par
137
				if (array_key_exists($nn, $taxons[0]) == false) {
138
					$taxons[0][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
139
				}
140
				$taxons[0][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
141
			} else {
142
				//contient
143
				if (array_key_exists($nn, $taxons[1]) == false) {
144
					$taxons[1][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
145
				}
146
				$taxons[1][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
199 delphine 147
			}
148
		}
790 mathilde 149
		ksort($taxons);
150
 
151
		$this->infosPourTpl['noms'] = isset($taxons)  ? $taxons : false;
199 delphine 152
	}
790 mathilde 153
 
154
	private function renvoyerInfosNomVerna($valeurs) {
155
		$nom_verna = array();
156
		$nom_verna['nn'] = $valeurs['id'];
157
		$nom_verna['nom_vernaculaire'] = $valeurs['nom_vernaculaire'];
158
		return $nom_verna;
159
	}
160
 
161
	private function renvoyerInfosTaxon($nomSci, $nn) {
162
		$taxon = array();
163
		$taxon['nomSci'] = $nomSci;
164
		$taxon['urlFiche'] = $this->urls->obtenirUrlFiche($nn, $this->parametres->typeNom, $this->parametres->masqueRecherche);
165
		$taxon['repartition_vignette'] = $this->chargerRepartition($nn);
166
		return $taxon;
167
	}
312 jpm 168
 
313 jpm 169
	private function supprimerCodeReftaxAvecNn($nn) {
199 delphine 170
		$codeReftax = $this->parametres->reftaxCourant.'.nn:';
171
		return str_replace($codeReftax, '', $nn);
172
	}
173
 
790 mathilde 174
	//tri alphabétique des noms scientifiques par catégorie (débute par , contient )
161 jpm 175
	public function trier() {
790 mathilde 176
		$verna = array();
177
		foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs) {
178
			$verna += $this->classerAlphabetiquement('nomSci', $valeurs);
179
		}
180
		$this->infosPourTpl['noms'] = $verna;
161 jpm 181
	}
790 mathilde 182
 
183
	private function classerAlphabetiquement($champs, $valeurs) {
184
		$this->trieur->setTableau($valeurs);
185
		$this->trieur->setChampsEtOrdres(array($champs => SORT_NATURAL));
186
		return $this->trieur->trier();
187
	}
188
 
189
	public function diviserResultats() {
190
		foreach ($this->infosPourTpl['noms'] as $nt => $valeurs) {
191
 
192
		}
193
	}
161 jpm 194
 
195
	public function surligner() {
202 delphine 196
		$this->definirMotsASurligner();
197
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
198
			foreach ($nom['nomVerna'] as $idVerna => $nomVerna) {
199
				$nom['nomVerna'][$idVerna]['nom_vernaculaire'] = $this->surlignerMotsMasqueRecherche($nomVerna['nom_vernaculaire']);
200
			}
201
			$this->infosPourTpl['noms'][$idNom] = $nom;
202
		}
161 jpm 203
	}
312 jpm 204
 
202 delphine 205
	private function definirMotsASurligner() {
207 delphine 206
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
790 mathilde 207
		foreach ($this->motsASurligner as $mot) {
208
			if (preg_match_all('/[aeiou]+/', $mot, $retour)) {
209
				$retour = array_unique($retour);
210
				$this->motsASurligner = $this->ajouterAccents($mot,$retour);
211
			}
212
		}
202 delphine 213
	}
790 mathilde 214
 
312 jpm 215
 
790 mathilde 216
 
217
	//pour le surlignage si présence d'accents
218
	private function ajouterAccents($mot, $retour) {
219
		$mots = array($mot);
220
		$accents = array('a' => array('à', 'â', 'ä'),
221
						 'e' => array('é', 'è', 'ë', 'ê'),
222
						 'i' => array('î' , 'ï'),
223
						 'o' => array('ô', 'ö'),
224
						 'u' => array('û', 'ü')
225
						);
226
		foreach($accents as $voyelle => $accents) {
227
			$lettre = array_shift($retour[0]);
228
			if ($lettre == $voyelle ) {
229
				foreach ($accents as $ac) {
230
					$mots[] = str_replace($lettre, $ac, $mot);
231
				}
232
			}
233
			if (empty($retour[0])) {
234
				break;
235
			}
236
		}
237
		return $mots;
238
	}
239
 
202 delphine 240
	private function surlignerMotsMasqueRecherche($nom) {
241
		$this->surligneur->setTexte($nom);
242
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
243
		return $nom;
244
	}
161 jpm 245
}
246
?>