Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 206 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 206 Rev 208
1
<?php
1
<?php
2
class NomsListeGenerique implements NomsListe {
2
class NomsListeGenerique implements NomsListe {
3
 
3
 
4
	private $parametres = null;
4
	private $parametres = null;
5
	private $ressources = null;
5
	private $ressources = null;
6
	private $bdd = null;
-
 
7
	private $projet = null;
6
	private $nomDao = null;
8
	private $versions = null;
-
 
9
	private $listeUrl = null;
7
	private $nomFormateur = null;
10
	private $detailsHrefTpl = null;
-
 
11
	private $ontologieHrefTpl = null;
-
 
12
	private $champsProjet = array();
-
 
-
 
8
 
13
 
9
	private $listeUrl = null;
14
	private $nbreTotalNoms = 0;
10
	private $nbreTotalNoms = 0;
15
	private $noms = array();
11
	private $noms = array();
16
 
12
 
17
	public function __construct(Ressources $ressources, Parametres $parametres, Bdd $bdd) {
13
	public function __construct(Ressources $ressources, Parametres $parametres, NomDAO $nomDao, NomFormateur $nomFormateur) {
18
		$this->ressources = $ressources;
14
		$this->ressources = $ressources;
19
		$this->parametres = $parametres;
-
 
20
		$this->bdd = $bdd;
-
 
21
	}
-
 
22
 
-
 
23
	public function setProjet($projet) {
15
		$this->parametres = $parametres;
24
		$this->projet = $projet;
-
 
25
	}
-
 
26
 
-
 
27
	public function setVersions($versions) {
-
 
28
		$this->versions = $versions;
-
 
29
	}
-
 
30
 
-
 
31
public function setChampsProjet($champsProjet) {
16
		$this->nomDao = $nomDao;
32
		$this->champsProjet = $champsProjet;
-
 
33
	}
-
 
34
 
-
 
35
	public function setDetailsHrefTpl($tpl) {
-
 
36
		$this->detailsHrefTpl = $tpl;
-
 
37
	}
-
 
38
 
-
 
39
	public function setOntologieHrefTpl($tpl) {
-
 
40
		$this->ontologieHrefTpl = $tpl;
17
		$this->nomFormateur = $nomFormateur;
41
	}
18
	}
42
 
19
 
43
	public function setListeUrl($url) {
20
	public function setListeUrl($url) {
44
		$this->listeUrl = $url;
21
		$this->listeUrl = $url;
45
	}
22
	}
46
 
23
 
47
	public function consulter() {
24
	public function consulter() {
48
		$this->noms = $this->rechercher();
25
		$this->noms = $this->rechercher();
49
		$this->nbreNomsTotal = $this->recupererNombreNomsTotal();
26
		$this->nbreNomsTotal = $this->nomDao->recupererNombreNomsTotal();
50
		$this->trierNoms();
27
		$this->trierNoms();
51
		$retour = $this->construireTableauRetour();
28
		$retour = $this->construireTableauRetour();
52
		return $retour;
29
		return $retour;
53
	}
30
	}
54
 
-
 
55
	private function getTable() {
-
 
56
		$versions = $this->versions->getVersions();
-
 
57
		return $this->projet.'_v'.end($versions);
-
 
58
	}
31
 
59
 
32
 
60
	private function rechercher() {
33
	private function rechercher() {
61
		$resultats = array();
34
		$resultats = array();
62
		$recherche = $this->parametres->get('recherche');
35
		$recherche = $this->parametres->get('recherche');
63
 
36
 
64
		if ($recherche == 'stricte') {
37
		if ($recherche == 'stricte') {
65
			$resultats = $this->rechercherStricte();
38
			$resultats = $this->nomDao->rechercherStricte();
66
		} else if ($recherche == 'etendue') {
39
		} else if ($recherche == 'etendue') {
67
			$resultats = $this->rechercherEtendue();
40
			$resultats = $this->nomDao->rechercherEtendue();
68
		} else if ($recherche == 'floue') {
41
		} else if ($recherche == 'floue') {
69
			$resultats = $this->rechercherFloue();
42
			$resultats = $this->nomDao->rechercherFloue();
70
		}
43
		}
71
 
44
 
72
		return $resultats;
45
		return $resultats;
73
	}
46
	}
74
 
-
 
75
	private function rechercherStricte() {
-
 
76
		$table = $this->getTable();
-
 
77
		$conditions = array();
-
 
78
		if ($masque = $this->parametres->getMasquePourBdd()) {
-
 
79
			$conditions[] = "ns.nom_sci = $masque";
-
 
80
		}
-
 
81
		if ($masqueSg = $this->parametres->getMasquePourBdd('sg')) {
-
 
82
			$conditions[] = "ns.nom_supra_generique = $masqueSg";
-
 
83
		}
-
 
84
		if ($masqueGen = $this->parametres->getMasquePourBdd('gen')) {
-
 
85
			$conditions[] = "ns.genre = $masqueGen";
-
 
86
		}
-
 
87
		if ($masqueSp = $this->parametres->getMasquePourBdd('sp')) {
-
 
88
			$conditions[] = "ns.epithete_sp = $masqueSp";
-
 
89
		}
-
 
90
 
-
 
91
		$requete = 'SELECT SQL_CALC_FOUND_ROWS ns.*,  '.
-
 
92
					'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
-
 
93
					"FROM $table AS ns ".
-
 
94
					"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
-
 
95
					"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
-
 
96
					(count($conditions) > 0 ? 'WHERE ' : '').
-
 
97
					implode(' AND ', $conditions).
-
 
98
					'ORDER BY ns.nom_sci ASC '.
-
 
99
					'LIMIT 0,100';
-
 
100
		$resultats = $this->bdd->recupererTous($requete);
-
 
101
 
-
 
102
		return $resultats;
-
 
103
	}
-
 
104
 
-
 
105
	private function rechercherEtendue() {
-
 
106
		$table = $this->getTable();
-
 
107
		$conditions = array();
-
 
108
		if ($masque = $this->parametres->getMasquePourBdd()) {
-
 
109
			$conditions[] = "ns.nom_sci LIKE $masque";
-
 
110
		}
-
 
111
		if ($masqueSg = $this->parametres->getMasquePourBdd('sg')) {
-
 
112
			$conditions[] = "ns.nom_supra_generique LIKE $masqueSg";
-
 
113
		}
-
 
114
		if ($masqueGen = $this->parametres->getMasquePourBdd('gen')) {
-
 
115
			$conditions[] = "ns.genre LIKE $masqueGen";
-
 
116
		}
-
 
117
		if ($masqueSp = $this->parametres->getMasquePourBdd('sp')) {
-
 
118
			$conditions[] = "ns.epithete_sp LIKE $masqueSp";
-
 
119
		}
-
 
120
		$requete = 'SELECT SQL_CALC_FOUND_ROWS ns.*,  '.
-
 
121
			'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
-
 
122
			"FROM $table AS ns ".
-
 
123
			"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
-
 
124
			"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
-
 
125
			(count($conditions) > 0 ? 'WHERE ' : '').
-
 
126
			implode(' AND ', $conditions).
-
 
127
			'ORDER BY ns.nom_sci ASC '.
-
 
128
			'LIMIT 0,100';
-
 
129
 
-
 
130
		$resultats = $this->bdd->recupererTous($requete);
-
 
131
		return $resultats;
-
 
132
	}
-
 
133
 
-
 
134
	private function rechercherFloue() {
-
 
135
		$table = $this->getTable();
-
 
136
		$masque = $this->parametres->getMasquePourBdd();
-
 
137
		$requete = 'SELECT SQL_CALC_FOUND_ROWS ns.*,  '.
-
 
138
			'	nr.nom_sci AS nr_nom_sci, nb.nom_sci AS nb_nom_sci '.
-
 
139
			"FROM $table AS ns ".
-
 
140
			"	LEFT JOIN $table AS nr ON (ns.num_nom_retenu = nr.num_nom) ".
-
 
141
			"	LEFT JOIN $table AS nb ON (ns.basionyme = nb.num_nom) ".
-
 
142
			($masque ? 'WHERE '.
-
 
143
				"	(SOUNDEX(ns.nom_sci) = SOUNDEX($masque)) ".
-
 
144
				"	OR (SOUNDEX(REVERSE(ns.nom_sci)) = SOUNDEX(REVERSE($masque))) " : '').
-
 
145
			'ORDER BY ns.nom_sci ASC '.
-
 
146
			'LIMIT 0,100';
-
 
147
		$resultats = $this->bdd->recupererTous($requete);
-
 
148
		return $resultats;
-
 
149
	}
-
 
150
 
-
 
151
	private function recupererNombreNomsTotal() {
-
 
152
		$requete = 'SELECT FOUND_ROWS() AS nbre';
-
 
153
		$nombre = $this->bdd->recuperer($requete);
-
 
154
		return (int) $nombre['nbre'];
-
 
155
	}
-
 
156
 
47
 
157
	private function trierNoms() {
48
	private function trierNoms() {
158
		$recherche = $this->parametres->get('recherche');
49
		$recherche = $this->parametres->get('recherche');
159
		if ($recherche == 'floue') {
50
		if ($recherche == 'floue') {
160
			$this->trierRechercheFloue();
51
			$this->trierRechercheFloue();
161
		}
52
		}
162
	}
53
	}
163
 
54
 
164
	public function trierRechercheFloue() {
55
	public function trierRechercheFloue() {
165
		$nomDemande = $this->parametres->get('masque');
56
		$nomDemande = $this->parametres->get('masque');
166
		$nomDemandeSimple = strtolower(Chaine::supprimerAccents($nomDemande));
57
		$nomDemandeSimple = strtolower(Chaine::supprimerAccents($nomDemande));
167
 
58
 
168
		foreach ($this->noms as $id => $nom) {
59
		foreach ($this->noms as $id => $nom) {
169
			$nomFlouSimple = strtolower(Chaine::supprimerAccents($nom['nom_sci']));
60
			$nomFlouSimple = strtolower(Chaine::supprimerAccents($nom['nom_sci']));
170
			// Prime pour la ressemblance globale :
61
			// Prime pour la ressemblance globale :
171
			$score = 500 - levenshtein($nomFlouSimple, $nomDemandeSimple);
62
			$score = 500 - levenshtein($nomFlouSimple, $nomDemandeSimple);
172
			// On affine
63
			// On affine
173
			$score += similar_text($nomDemandeSimple, $nomFlouSimple) * 3;
64
			$score += similar_text($nomDemandeSimple, $nomFlouSimple) * 3;
174
			$this->noms[$id]['score'] = $score;
65
			$this->noms[$id]['score'] = $score;
175
		}
66
		}
176
		$noms = $this->noms;
67
		$noms = $this->noms;
177
		$this->noms = Tableau::trierMD($noms, array('score' => false));
68
		$this->noms = Tableau::trierMD($noms, array('score' => false));
178
		//print_r($this->noms);
69
		//print_r($this->noms);
179
	}
70
	}
180
 
71
 
181
	private function construireTableauRetour() {
72
	private function construireTableauRetour() {
182
		$retour = array('entete' => array(), 'resultats' => array());
73
		$retour = array('entete' => array(), 'resultats' => array());
183
		$retour['resultats'] = $this->construireResultats();
74
		$retour['resultats'] = $this->construireResultats();
184
		$retour['entete'] = $this->construireEntete();
75
		$retour['entete'] = $this->construireEntete();
185
		return $retour;
76
		return $retour;
186
	}
77
	}
187
 
78
 
188
	private function construireResultats() {
79
	private function construireResultats() {
189
		$nomsFormates = array();
80
		$nomsFormates = array();
190
		foreach ($this->noms as $nom) {
81
		foreach ($this->noms as $nom) {
191
			$id = $nom['num_nom'];
82
			$id = $nom['num_nom'];
192
			$nomsFormates[$id] = $this->formaterNom($nom);
83
			$nomsFormates[$id] = $this->formaterNom($nom);
193
		}
84
		}
194
		return $nomsFormates;
85
		return $nomsFormates;
195
	}
86
	}
196
 
87
 
197
	private function formaterNom($infos) {
88
	private function formaterNom($infos) {
198
		$nomAFormater = new NomDO($infos);
89
		$nomAFormater = new NomDO($infos);
199
		$formateur = new NomFormateur($nomAFormater);
90
		$this->nomFormateur->setNomAFormater($nomAFormater);
200
		$formateur->setBdd($this->bdd);
-
 
201
		$formateur->setDetailsHrefTpl($this->detailsHrefTpl);
-
 
202
		$formateur->setChampsRetour($this->parametres->getListe('retour.champs'));
91
		$this->nomFormateur->setChampsRetour($this->parametres->getListe('retour.champs'));
203
		$nom = $formateur->formaterListe();
92
		$nom = $this->nomFormateur->formaterListe();
204
		return $nom;
93
		return $nom;
205
	}
94
	}
206
 
95
 
207
	private function formaterHref($nom) {
96
	private function formaterHref($nom) {
208
		$href = sprintf($this->detailsHrefTpl, $nom['num_nom']);
97
		$href = sprintf($this->detailsHrefTpl, $nom['num_nom']);
209
		return $href;
98
		return $href;
210
	}
99
	}
211
 
100
 
212
	private function construireEntete() {
101
	private function construireEntete() {
213
		$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
102
		$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
214
		$entete['masque'] = $this->formaterEnteteMasque();
103
		$entete['masque'] = $this->formaterEnteteMasque();
215
		$entete['depart'] = (int) $this->parametres->get('navigation.depart');
104
		$entete['depart'] = (int) $this->parametres->get('navigation.depart');
216
		$entete['limite'] = (int) $this->parametres->get('navigation.limite');
105
		$entete['limite'] = (int) $this->parametres->get('navigation.limite');
217
		$entete['total'] = $this->nbreNomsTotal;
106
		$entete['total'] = $this->nbreNomsTotal;
218
		if ($hrefPrecedent = $this->formaterEnteteHrefPrecedent()) {
107
		if ($hrefPrecedent = $this->formaterEnteteHrefPrecedent()) {
219
			$entete['href.precedent'] = $hrefPrecedent;
108
			$entete['href.precedent'] = $hrefPrecedent;
220
		}
109
		}
221
		if ($hrefSuivant = $this->formaterEnteteHrefSuivant()) {
110
		if ($hrefSuivant = $this->formaterEnteteHrefSuivant()) {
222
			$entete['href.suivant'] = $hrefSuivant;
111
			$entete['href.suivant'] = $hrefSuivant;
223
		}
112
		}
224
		return $entete;
113
		return $entete;
225
	}
114
	}
226
 
115
 
227
	private function formaterEnteteMasque() {
116
	private function formaterEnteteMasque() {
228
		$masqueComplet = array();
117
		$masqueComplet = array();
229
		if ($this->parametres->exister('masque')) {
118
		if ($this->parametres->exister('masque')) {
230
			$masque = '';
119
			$masque = '';
231
			$masque .= 'nom_sci='.$this->parametres->get('masque');
120
			$masque .= 'nom_sci='.$this->parametres->get('masque');
232
			if ($this->parametres->get('recherche') == 'etendue') {
121
			if ($this->parametres->get('recherche') == 'etendue') {
233
				$masque .= '%';
122
				$masque .= '%';
234
			}
123
			}
235
			$masqueComplet[] = $masque;
124
			$masqueComplet[] = $masque;
236
		}
125
		}
237
		if ($this->parametres->exister('masque.sg')) {
126
		if ($this->parametres->exister('masque.sg')) {
238
			$masque = '';
127
			$masque = '';
239
			$masque .= 'nom_supra_generique='.$this->parametres->get('masque.sg');
128
			$masque .= 'nom_supra_generique='.$this->parametres->get('masque.sg');
240
			if ($this->parametres->get('recherche') == 'etendue') {
129
			if ($this->parametres->get('recherche') == 'etendue') {
241
				$masque .= '%';
130
				$masque .= '%';
242
			}
131
			}
243
			$masqueComplet[] = $masque;
132
			$masqueComplet[] = $masque;
244
		}
133
		}
245
		if ($this->parametres->exister('masque.gen')) {
134
		if ($this->parametres->exister('masque.gen')) {
246
			$masque = '';
135
			$masque = '';
247
			$masque .= 'genre='.$this->parametres->get('masque.gen');
136
			$masque .= 'genre='.$this->parametres->get('masque.gen');
248
			if ($this->parametres->get('recherche') == 'etendue') {
137
			if ($this->parametres->get('recherche') == 'etendue') {
249
				$masque .= '%';
138
				$masque .= '%';
250
			}
139
			}
251
			$masqueComplet[] = $masque;
140
			$masqueComplet[] = $masque;
252
		}
141
		}
253
		if ($this->parametres->exister('masque.sp')) {
142
		if ($this->parametres->exister('masque.sp')) {
254
			$masque = '';
143
			$masque = '';
255
			$masque .= 'epithete_sp='.$this->parametres->get('masque.sp');
144
			$masque .= 'epithete_sp='.$this->parametres->get('masque.sp');
256
			if ($this->parametres->get('recherche') == 'etendue') {
145
			if ($this->parametres->get('recherche') == 'etendue') {
257
				$masque .= '%';
146
				$masque .= '%';
258
			}
147
			}
259
			$masqueComplet[] = $masque;
148
			$masqueComplet[] = $masque;
260
		}
149
		}
261
		return implode('&', $masqueComplet);
150
		return implode('&', $masqueComplet);
262
	}
151
	}
263
 
152
 
264
	private function formaterEnteteHrefPrecedent() {
153
	private function formaterEnteteHrefPrecedent() {
265
		$limite = $this->parametres->get('navigation.limite');
154
		$limite = $this->parametres->get('navigation.limite');
266
		$departActuel = $this->parametres->get('navigation.depart');
155
		$departActuel = $this->parametres->get('navigation.depart');
267
		$departPrecedent = $departActuel - $limite;
156
		$departPrecedent = $departActuel - $limite;
268
		$href = null;
157
		$href = null;
269
		if ($departPrecedent >= 0) {
158
		if ($departPrecedent >= 0) {
270
			$squelette = $this->construireTplHrefNavigation();
159
			$squelette = $this->construireTplHrefNavigation();
271
			$href = sprintf($squelette, $departPrecedent, $limite);
160
			$href = sprintf($squelette, $departPrecedent, $limite);
272
		}
161
		}
273
		return $href;
162
		return $href;
274
	}
163
	}
275
 
164
 
276
	private function formaterEnteteHrefSuivant() {
165
	private function formaterEnteteHrefSuivant() {
277
		$limite = $this->parametres->get('navigation.limite');
166
		$limite = $this->parametres->get('navigation.limite');
278
		$departActuel = $this->parametres->get('navigation.depart');
167
		$departActuel = $this->parametres->get('navigation.depart');
279
		$departSuivant = $departActuel + $limite;
168
		$departSuivant = $departActuel + $limite;
280
		$href = null;
169
		$href = null;
281
		if ($departSuivant < $this->nbreNomsTotal) {
170
		if ($departSuivant < $this->nbreNomsTotal) {
282
			$squelette = $this->construireTplHrefNavigation();
171
			$squelette = $this->construireTplHrefNavigation();
283
			$href = sprintf($squelette, $departSuivant, $limite);
172
			$href = sprintf($squelette, $departSuivant, $limite);
284
		}
173
		}
285
		return $href;
174
		return $href;
286
	}
175
	}
287
 
176
 
288
	private function construireTplHrefNavigation() {
177
	private function construireTplHrefNavigation() {
289
		$requetes = array();
178
		$requetes = array();
290
		$this->parametres->rewind();
179
		$this->parametres->rewind();
291
		while (is_null($parametre = $this->parametres->key()) === false) {
180
		while (is_null($parametre = $this->parametres->key()) === false) {
292
			if (strpos($parametre, 'navigation') === false) {
181
			if (strpos($parametre, 'navigation') === false) {
293
				$valeur = $this->parametres->current();
182
				$valeur = $this->parametres->current();
294
				$requetes[] = "$parametre=$valeur";
183
				$requetes[] = "$parametre=$valeur";
295
			}
184
			}
296
			$this->parametres->next();
185
			$this->parametres->next();
297
		}
186
		}
298
		$requetes[] = "navigation.depart=%s";
187
		$requetes[] = "navigation.depart=%s";
299
		$requetes[] = "navigation.limite=%s";
188
		$requetes[] = "navigation.limite=%s";
300
		$tpl = $this->listeUrl.'?'.implode('&', $requetes);
189
		$tpl = $this->listeUrl.'?'.implode('&', $requetes);
301
		return $tpl;
190
		return $tpl;
302
	}
191
	}
303
}
192
}
304
?>
193
?>