Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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);
1115 mathias 58
			$costeImg = $this->imagesService->setApi(Eflore::API_EFLORE)->getInfosImagesTaxons();
931 delphine 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();
1115 mathias 82
			$urls = $this->imagesService->setApi(Eflore::API_EFLORE)->getUrlsImagesParIdsNoms($nns);
1060 aurelien 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() {
1068 raphael 118
		// $numsNomsASynonymes = $this->extraireNumerosNomSynonymes();
119
		if (Config::get(Registre::get('parametres.referentiel').'.baseRepartition') == "") {
120
			return;
121
		}
122
 
123
		$this->apiCartes->setProjet(Config::get(Registre::get('parametres.referentiel').'.baseRepartition'));
124
		$this->apiCartes->setLargeur('108x101');
125
		$urls = array();
126
		foreach ($this->noms as $nom) {
127
			if (array_key_exists('nom_retenu.code', $nom)) {
128
				$id = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
129
				$this->apiCartes->setId("nn:" . $id); //  . ',' . implode(',', array_keys($numsNomsASynonymes[$id])));
130
				if (array_key_exists($id, $urls) == false) {
131
					$urls[$id] = $this->apiCartes->getUrlPng();
931 delphine 132
				}
133
			}
134
		}
1068 raphael 135
		$this->infosPourTpl['repartition']['urls'] = $urls;
931 delphine 136
	}
137
 
1004 aurelien 138
	private function extraireNumerosNomSynonymes() {
139
		$numNomsASynonyme = array();
140
		foreach ($this->noms as $id => $nom) {
141
			$num_nom_retenu = $this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']);
1068 raphael 142
			// faux
143
			$numNomsASynonyme[$num_nom_retenu][$id] = TRUE;
1004 aurelien 144
		}
145
		return $numNomsASynonyme;
146
	}
147
 
199 delphine 148
	private function extraireInfosNomsPourTplDetermination() {
149
		$taxons = array();
150
		foreach ($this->noms as $idNomCourant => $nom) {
1068 raphael 151
			$nn = intval($this->supprimerCodeReferentielAvecNn($nom['nom_retenu.code']));
152
			$nomV = iconv('UTF-8', 'ASCII//TRANSLIT', strtolower($nom['nom_vernaculaire']));
153
			if (preg_match('/^'.strtolower($this->parametres->masqueRecherche).'(?: |$)/', $nomV)) {
154
				// au moins un debute par la requête
798 mathilde 155
				if (isset($taxons) && array_key_exists($nn, $taxons[0]) == false) {
790 mathilde 156
					$taxons[0][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
157
				}
158
				$taxons[0][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
159
			} else {
1068 raphael 160
				// contient
931 delphine 161
				if (isset($taxons) && (!isset($taxons[1]) || array_key_exists($nn, $taxons[1]) == false)) {
790 mathilde 162
					$taxons[1][$nn] = $this->renvoyerInfosTaxon($nom['taxon'], $nn);
163
				}
164
				$taxons[1][$nn]['nomVerna'][] = $this->renvoyerInfosNomVerna($nom);
199 delphine 165
			}
166
		}
790 mathilde 167
		$this->infosPourTpl['noms'] = isset($taxons)  ? $taxons : false;
199 delphine 168
	}
790 mathilde 169
 
170
	private function renvoyerInfosNomVerna($valeurs) {
171
		$nom_verna = array();
172
		$nom_verna['nn'] = $valeurs['id'];
173
		$nom_verna['nom_vernaculaire'] = $valeurs['nom_vernaculaire'];
174
		return $nom_verna;
175
	}
176
 
177
	private function renvoyerInfosTaxon($nomSci, $nn) {
178
		$taxon = array();
179
		$taxon['nomSci'] = $nomSci;
180
		$taxon['urlFiche'] = $this->urls->obtenirUrlFiche($nn, $this->parametres->typeNom, $this->parametres->masqueRecherche);
1068 raphael 181
		$this->chargerRepartition($nn);
182
		$taxon['repartition_vignette'] = $this->infosPourTpl['repartition']['urls'];
790 mathilde 183
		return $taxon;
184
	}
312 jpm 185
 
931 delphine 186
	private function supprimerCodeReferentielAvecNn($nn) {
1068 raphael 187
		return str_replace($this->parametres->referentielCourant.'.nn:', '', $nn);
199 delphine 188
	}
189
 
790 mathilde 190
	//tri alphabétique des noms scientifiques par catégorie (débute par , contient )
161 jpm 191
	public function trier() {
790 mathilde 192
		$verna = array();
193
		foreach ($this->infosPourTpl['noms'] as $categorie => $valeurs) {
194
			$verna += $this->classerAlphabetiquement('nomSci', $valeurs);
195
		}
196
		$this->infosPourTpl['noms'] = $verna;
161 jpm 197
	}
790 mathilde 198
 
199
	private function classerAlphabetiquement($champs, $valeurs) {
200
		$this->trieur->setTableau($valeurs);
931 delphine 201
		$this->trieur->setChampsEtOrdres(array($champs => 'nat'));
790 mathilde 202
		return $this->trieur->trier();
203
	}
204
 
798 mathilde 205
 
161 jpm 206
 
207
	public function surligner() {
202 delphine 208
		$this->definirMotsASurligner();
209
		foreach ($this->infosPourTpl['noms'] as $idNom => $nom) {
210
			foreach ($nom['nomVerna'] as $idVerna => $nomVerna) {
211
				$nom['nomVerna'][$idVerna]['nom_vernaculaire'] = $this->surlignerMotsMasqueRecherche($nomVerna['nom_vernaculaire']);
212
			}
213
			$this->infosPourTpl['noms'][$idNom] = $nom;
214
		}
161 jpm 215
	}
312 jpm 216
 
202 delphine 217
	private function definirMotsASurligner() {
207 delphine 218
		$this->motsASurligner = explode(' ', $this->parametres->masqueRecherche);
798 mathilde 219
 
202 delphine 220
	}
790 mathilde 221
 
202 delphine 222
	private function surlignerMotsMasqueRecherche($nom) {
223
		$this->surligneur->setTexte($nom);
224
		$nom = $this->surligneur->surlignerMots($this->motsASurligner);
225
		return $nom;
226
	}
161 jpm 227
}
228
?>