Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 880 | 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() {
115
		if (Config::get(Registre::get('parametres.referentiel').'.baseRepartition') != "") {
116
			$this->apiCartes->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseRepartition'));
117
			$this->apiCartes->setLargeur('108x101');
118
			$urls = array();
119
			foreach ($this->noms as $nom) {
120
				if (array_key_exists('nom_retenu.code', $nom)) {
121
					$id = $nom['nom_retenu.code'];
122
					$id = $this->supprimerCodeReferentielAvecNn($id);
123
					$this->apiCartes->setId("nn:$id");
124
					if (array_key_exists($id, $urls) == false) {
125
						$urls[$id] = $this->apiCartes->getUrlPng();
126
					}
127
				}
128
			}
129
			$this->infosPourTpl['repartition']['urls'] = $urls;
130
		}
131
	}
132
 
798 mathilde 133
	function supprimerAccents($chaine){
134
		return strtr($chaine,array('à' => 'a','á' => 'a','â' => 'a','ã' => 'a','ä' => 'a',
135
										'ç' => 'c',
136
										'è' => 'e','é' => 'e','ê' => 'e','ë' => 'e',
137
										'ì' => 'i','í' => 'i','î' => 'i','ï' => 'i',
138
										'ñ' => 'n',
139
										'ò' => 'o', 'ó' => 'o' , 'ô' => 'o', 'õ' => 'o', 'ö' => 'o',
140
										'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
141
										'ý' => 'y', 'ÿ' => 'y'));
142
	}
143
 
312 jpm 144
 
199 delphine 145
	private function extraireInfosNomsPourTplDetermination() {
146
		$taxons = array();
147
		foreach ($this->noms as $idNomCourant => $nom) {
931 delphine 148
			$nn = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
798 mathilde 149
			$nom_min = strtolower($nom['nom_vernaculaire']);
150
			$nom_ss_accent = $this->supprimerAccents($nom_min);
151
			if (preg_match('/^'.strtolower($this->parametres->masqueRecherche).' |^'.strtolower($this->parametres->masqueRecherche).'$/', $nom_ss_accent )) {
152
				//au moins un debute par la requête
153
				if (isset($taxons) && array_key_exists($nn, $taxons[0]) == false) {
790 mathilde 154
					$taxons[0][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
155
				}
156
				$taxons[0][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
157
			} else {
158
				//contient
931 delphine 159
				if (isset($taxons) && (!isset($taxons[1]) || array_key_exists($nn, $taxons[1]) == false)) {
790 mathilde 160
					$taxons[1][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
161
				}
162
				$taxons[1][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
199 delphine 163
			}
164
		}
790 mathilde 165
		$this->infosPourTpl['noms'] = isset($taxons)  ? $taxons : false;
199 delphine 166
	}
790 mathilde 167
 
168
	private function renvoyerInfosNomVerna($valeurs) {
169
		$nom_verna = array();
170
		$nom_verna['nn'] = $valeurs['id'];
171
		$nom_verna['nom_vernaculaire'] = $valeurs['nom_vernaculaire'];
172
		return $nom_verna;
173
	}
174
 
175
	private function renvoyerInfosTaxon($nomSci, $nn) {
176
		$taxon = array();
177
		$taxon['nomSci'] = $nomSci;
178
		$taxon['urlFiche'] = $this->urls->obtenirUrlFiche($nn, $this->parametres->typeNom, $this->parametres->masqueRecherche);
179
		$taxon['repartition_vignette'] = $this->chargerRepartition($nn);
180
		return $taxon;
181
	}
312 jpm 182
 
931 delphine 183
	private function supprimerCodeReferentielAvecNn($nn) {
184
		$codeReferentiel = $this->parametres->referentielCourant.'.nn:';
185
		return str_replace($codeReferentiel, '', $nn);
199 delphine 186
	}
187
 
790 mathilde 188
	//tri alphabétique des noms scientifiques par catégorie (débute par , contient )
161 jpm 189
	public function trier() {
790 mathilde 190
		$verna = array();
191
		foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs) {
192
			$verna += $this->classerAlphabetiquement('nomSci', $valeurs);
193
		}
194
		$this->infosPourTpl['noms'] = $verna;
161 jpm 195
	}
790 mathilde 196
 
197
	private function classerAlphabetiquement($champs, $valeurs) {
198
		$this->trieur->setTableau($valeurs);
931 delphine 199
		$this->trieur->setChampsEtOrdres(array($champs => 'nat'));
790 mathilde 200
		return $this->trieur->trier();
201
	}
202
 
798 mathilde 203
 
161 jpm 204
 
205
	public function surligner() {
202 delphine 206
		$this->definirMotsASurligner();
207
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
208
			foreach ($nom['nomVerna'] as $idVerna => $nomVerna) {
209
				$nom['nomVerna'][$idVerna]['nom_vernaculaire'] = $this->surlignerMotsMasqueRecherche($nomVerna['nom_vernaculaire']);
210
			}
211
			$this->infosPourTpl['noms'][$idNom] = $nom;
212
		}
161 jpm 213
	}
312 jpm 214
 
202 delphine 215
	private function definirMotsASurligner() {
207 delphine 216
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
798 mathilde 217
 
202 delphine 218
	}
790 mathilde 219
 
202 delphine 220
	private function surlignerMotsMasqueRecherche($nom) {
221
		$this->surligneur->setTexte($nom);
222
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
223
		return $nom;
224
	}
161 jpm 225
}
226
?>