Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
206 jpm 1
<?php
215 jpm 2
class NomsListeGenerique {
206 jpm 3
 
4
	private $parametres = null;
5
	private $ressources = null;
208 jpm 6
	private $nomDao = null;
7
	private $nomFormateur = null;
8
 
206 jpm 9
	private $listeUrl = null;
10
	private $nbreTotalNoms = 0;
11
	private $noms = array();
12
 
208 jpm 13
	public function __construct(Ressources $ressources, Parametres $parametres, NomDAO $nomDao, NomFormateur $nomFormateur) {
206 jpm 14
		$this->ressources = $ressources;
15
		$this->parametres = $parametres;
208 jpm 16
		$this->nomDao = $nomDao;
17
		$this->nomFormateur = $nomFormateur;
206 jpm 18
	}
19
 
20
	public function setListeUrl($url) {
21
		$this->listeUrl = $url;
22
	}
23
 
24
	public function consulter() {
25
		$this->noms = $this->rechercher();
208 jpm 26
		$this->nbreNomsTotal = $this->nomDao->recupererNombreNomsTotal();
206 jpm 27
		$this->trierNoms();
28
		$retour = $this->construireTableauRetour();
29
		return $retour;
30
	}
31
 
32
 
33
	private function rechercher() {
34
		$resultats = array();
35
		$recherche = $this->parametres->get('recherche');
36
 
37
		if ($recherche == 'stricte') {
208 jpm 38
			$resultats = $this->nomDao->rechercherStricte();
206 jpm 39
		} else if ($recherche == 'etendue') {
208 jpm 40
			$resultats = $this->nomDao->rechercherEtendue();
206 jpm 41
		} else if ($recherche == 'floue') {
208 jpm 42
			$resultats = $this->nomDao->rechercherFloue();
206 jpm 43
		}
44
 
45
		return $resultats;
46
	}
47
 
48
	private function trierNoms() {
49
		$recherche = $this->parametres->get('recherche');
50
		if ($recherche == 'floue') {
51
			$this->trierRechercheFloue();
52
		}
53
	}
54
 
55
	public function trierRechercheFloue() {
56
		$nomDemande = $this->parametres->get('masque');
57
		$nomDemandeSimple = strtolower(Chaine::supprimerAccents($nomDemande));
58
 
59
		foreach ($this->noms as $id => $nom) {
60
			$nomFlouSimple = strtolower(Chaine::supprimerAccents($nom['nom_sci']));
61
			// Prime pour la ressemblance globale :
62
			$score = 500 - levenshtein($nomFlouSimple, $nomDemandeSimple);
63
			// On affine
64
			$score += similar_text($nomDemandeSimple, $nomFlouSimple) * 3;
65
			$this->noms[$id]['score'] = $score;
66
		}
67
		$noms = $this->noms;
68
		$this->noms = Tableau::trierMD($noms, array('score' => false));
69
		//print_r($this->noms);
70
	}
71
 
72
	private function construireTableauRetour() {
73
		$retour = array('entete' => array(), 'resultats' => array());
74
		$retour['resultats'] = $this->construireResultats();
75
		$retour['entete'] = $this->construireEntete();
76
		return $retour;
77
	}
78
 
79
	private function construireResultats() {
80
		$nomsFormates = array();
81
		foreach ($this->noms as $nom) {
82
			$id = $nom['num_nom'];
83
			$nomsFormates[$id] = $this->formaterNom($nom);
84
		}
85
		return $nomsFormates;
86
	}
87
 
88
	private function formaterNom($infos) {
89
		$nomAFormater = new NomDO($infos);
208 jpm 90
		$this->nomFormateur->setNomAFormater($nomAFormater);
91
		$this->nomFormateur->setChampsRetour($this->parametres->getListe('retour.champs'));
92
		$nom = $this->nomFormateur->formaterListe();
206 jpm 93
		return $nom;
94
	}
95
 
96
	private function construireEntete() {
97
		$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
98
		$entete['masque'] = $this->formaterEnteteMasque();
99
		$entete['depart'] = (int) $this->parametres->get('navigation.depart');
100
		$entete['limite'] = (int) $this->parametres->get('navigation.limite');
101
		$entete['total'] = $this->nbreNomsTotal;
102
		if ($hrefPrecedent = $this->formaterEnteteHrefPrecedent()) {
103
			$entete['href.precedent'] = $hrefPrecedent;
104
		}
105
		if ($hrefSuivant = $this->formaterEnteteHrefSuivant()) {
106
			$entete['href.suivant'] = $hrefSuivant;
107
		}
108
		return $entete;
109
	}
110
 
111
	private function formaterEnteteMasque() {
112
		$masqueComplet = array();
113
		if ($this->parametres->exister('masque')) {
114
			$masque = '';
115
			$masque .= 'nom_sci='.$this->parametres->get('masque');
116
			if ($this->parametres->get('recherche') == 'etendue') {
117
				$masque .= '%';
118
			}
119
			$masqueComplet[] = $masque;
120
		}
121
		if ($this->parametres->exister('masque.sg')) {
122
			$masque = '';
123
			$masque .= 'nom_supra_generique='.$this->parametres->get('masque.sg');
124
			if ($this->parametres->get('recherche') == 'etendue') {
125
				$masque .= '%';
126
			}
127
			$masqueComplet[] = $masque;
128
		}
129
		if ($this->parametres->exister('masque.gen')) {
130
			$masque = '';
131
			$masque .= 'genre='.$this->parametres->get('masque.gen');
132
			if ($this->parametres->get('recherche') == 'etendue') {
133
				$masque .= '%';
134
			}
135
			$masqueComplet[] = $masque;
136
		}
137
		if ($this->parametres->exister('masque.sp')) {
138
			$masque = '';
139
			$masque .= 'epithete_sp='.$this->parametres->get('masque.sp');
140
			if ($this->parametres->get('recherche') == 'etendue') {
141
				$masque .= '%';
142
			}
143
			$masqueComplet[] = $masque;
144
		}
145
		return implode('&', $masqueComplet);
146
	}
147
 
148
	private function formaterEnteteHrefPrecedent() {
149
		$limite = $this->parametres->get('navigation.limite');
150
		$departActuel = $this->parametres->get('navigation.depart');
151
		$departPrecedent = $departActuel - $limite;
152
		$href = null;
153
		if ($departPrecedent >= 0) {
154
			$squelette = $this->construireTplHrefNavigation();
155
			$href = sprintf($squelette, $departPrecedent, $limite);
156
		}
157
		return $href;
158
	}
159
 
160
	private function formaterEnteteHrefSuivant() {
161
		$limite = $this->parametres->get('navigation.limite');
162
		$departActuel = $this->parametres->get('navigation.depart');
163
		$departSuivant = $departActuel + $limite;
164
		$href = null;
165
		if ($departSuivant < $this->nbreNomsTotal) {
166
			$squelette = $this->construireTplHrefNavigation();
167
			$href = sprintf($squelette, $departSuivant, $limite);
168
		}
169
		return $href;
170
	}
171
 
172
	private function construireTplHrefNavigation() {
173
		$requetes = array();
174
		$this->parametres->rewind();
175
		while (is_null($parametre = $this->parametres->key()) === false) {
176
			if (strpos($parametre, 'navigation') === false) {
177
				$valeur = $this->parametres->current();
178
				$requetes[] = "$parametre=$valeur";
179
			}
180
			$this->parametres->next();
181
		}
182
		$requetes[] = "navigation.depart=%s";
183
		$requetes[] = "navigation.limite=%s";
184
		$tpl = $this->listeUrl.'?'.implode('&', $requetes);
185
		return $tpl;
186
	}
187
}
188
?>