Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
231 jpm 1
<?php
2
class OntologieDAO {
3
	private $bdd = null;
4
	private $versions = null;
5
	private $requeteNbreTermesTotal = null;
6
	private $masquesStrictes = array();
7
	private $paramsMasque = array();
8
 
9
	public function __construct(Ressources $ressources, Parametres $parametres, Bdd $bdd, Versions $versions) {
10
		$this->ressources = $ressources;
11
		$this->parametres = $parametres;
12
		$this->bdd = $bdd;
13
		$this->versions = $versions;
14
		$this->masquesStrictes = array('code');
15
		$this->paramsMasque = array(
16
					'' => 'nom',
17
					'code' => 'id_terme',
18
					'nom' => $this->getChampPourLangue('nom'),
19
					'description' => $this->getChampPourLangue('description'));
20
	}
21
 
22
	public function getDetails() {
23
		$table = $this->getTableTerme();
24
		$tableType = $this->getTableType();
25
		$detailsId = $this->ressources->getDetailsId();
26
		$detailsId = $this->bdd->proteger($detailsId);
27
		$requete =
28
				'SELECT o.*, type '.
29
				"FROM $table AS o ".
30
				"	LEFT JOIN $tableType ON (ce_type = id_type) ".
31
				"WHERE id_terme = $detailsId ";
32
		$resultats = $this->bdd->recuperer($requete);
33
		$terme = new OntologieDO($resultats);
34
		return $terme;
35
	}
36
 
37
	public function rechercher() {
38
		$clause = $this->getClauseSelectSpeciale();
39
		$table = $this->getTableTerme();
40
		$tableType = $this->getTableType();
41
		$tablePublication = $this->getTablePublication();
42
		$tableAuteur = $this->getTableAuteur();
43
		$tableImage = $this->getTableImage();
44
		$conditions = $this->getConditions();
45
		$where = $this->getWhere($conditions);
46
		$ordre = $this->getOrdre();
47
		$navigation = $this->getNavigation();
48
 
49
		$requete = "SELECT $clause o.*, t.type, p.*,  a.*,  i.* ".
50
			"FROM $table AS o ".
51
			"	LEFT JOIN $tableType AS t ON (ce_type = id_type) ".
52
			"	LEFT JOIN $tablePublication AS p ON (ce_publication = id_publication) ".
53
			"	LEFT JOIN $tableAuteur AS a ON (ce_auteur = id_auteur) ".
54
			"	LEFT JOIN $tableImage AS i ON (ce_image = id_image) ".
55
			$where.' '.$conditions.' '.$ordre.' '.
56
			"LIMIT $navigation ";
57
		//die($requete);
58
		$this->requeteNbreNomsTotal = $this->transformerRequetePourNbreTermesTotal($requete);
59
		$resultats = $this->bdd->recupererTous($requete);
60
 
61
		return $resultats;
62
	}
63
 
64
	private function getClauseSelectSpeciale() {
65
		$clause = (Config::get('bdd_protocole') == 'mysql') ? 'SQL_CALC_FOUND_ROWS' : '';
66
		return $clause;
67
	}
68
 
69
	private function getTableTerme() {
70
		return sprintf($this->getNomTableTpl(), 'terme');
71
	}
72
 
73
	private function getTableType() {
74
		return sprintf($this->getNomTableTpl(), 'type');
75
	}
76
 
77
	private function getTablePublication() {
78
		return sprintf($this->getNomTableTpl(), 'publication');
79
	}
80
 
81
	private function getTableAuteur() {
82
		return sprintf($this->getNomTableTpl(), 'auteur');
83
	}
84
 
85
	private function getTableImage() {
86
		return sprintf($this->getNomTableTpl(), 'image');
87
	}
88
 
89
	private function getTableHierarchie() {
90
		return sprintf($this->getNomTableTpl(), 'hierarchie');
91
	}
92
 
93
	private function getNomTableTpl() {
94
		$versions = $this->versions->getVersions();
95
		$derniereVersion = end($versions);
96
		$projetNom = strtolower($this->ressources->getProjetNom());
97
		return $projetNom.'_ontologies_%s_v'.$derniereVersion;
98
	}
99
 
100
	private function getConditions() {
101
		$operateurParDefaut = $this->getOperateurCondition();
102
		$conditionsSql = array();
103
		foreach ($this->paramsMasque as $typeMasque => $champ) {
104
			$operateur = in_array($typeMasque, $this->masquesStrictes) ? '=' : $operateurParDefaut;
105
			if ($valeurMasque = $this->parametres->getMasquePourBdd($typeMasque)) {
106
				if ($operateur == 'SOUNDEX') {
107
					$tpl = '(SOUNDEX(%s) = SOUNDEX(%s)) OR (SOUNDEX(REVERSE(%s)) = SOUNDEX(REVERSE(%s))) ';
108
					$conditionsSql[] = sprintf($tpl, $champ, $valeurMasque, $champ, $valeurMasque);
109
				} else {
110
					$conditionsSql[] = "$champ $operateur $valeurMasque";
111
				}
112
			}
113
		}
114
		return implode(' AND ', $conditionsSql);
115
	}
116
 
117
	private function getOperateurCondition() {
118
		$operateur = '';
119
		$recherche = $this->parametres->get('recherche');
120
		if ($recherche == 'stricte') {
121
			$operateur = '=';
122
		} else if ($recherche == 'etendue') {
123
			$operateur = 'LIKE';
124
		} else if ($recherche == 'floue') {
125
			$operateur = 'SOUNDEX';
126
		}
127
		return $operateur;
128
	}
129
 
130
	private function getWhere($conditions = '') {
131
		$where = '';
132
		if ($conditions != '') {
133
			$where = 'WHERE ';
134
		}
135
		return $where;
136
	}
137
 
138
	private function getOrdre() {
139
		$champNom = $this->getChampPourLangue('nom');
140
		$ordre = "ORDER BY $champNom ASC ";
141
		return $ordre;
142
	}
143
 
144
	private function getChampPourLangue($champ) {
145
		$lg = $this->parametres->get('retour.langue');
146
		if ($lg == 'en') {
147
			$champ .= '_en';
148
		}
149
		return $champ;
150
	}
151
 
152
	private function getNavigation() {
153
		$debut = (int) $this->parametres->get('navigation.depart');
154
		$nbre = $this->parametres->get('navigation.limite');
155
		$navigation = "$debut,$nbre";
156
		return $navigation;
157
	}
158
 
159
	private function transformerRequetePourNbreTermesTotal($requete) {
160
		$requete = preg_replace('/SELECT .* FROM/', 'SELECT COUNT(*) AS nbre FROM', $requete);
161
		$requete = preg_replace('/LIMIT [0-9]+,[0-9]+/', '', $requete);
162
		return $requete;
163
	}
164
 
165
	public function trierResultatsFloue($termes) {
166
		foreach ($this->paramsMasque as $typeMasque => $champ) {
167
			if ($termeDemande = $this->parametres->getMasquePourBdd($typeMasque)) {
168
				$termeDemandeSimple = strtolower(Chaine::supprimerAccents($termeDemande));
169
 
170
				foreach ($termes as $id => $terme) {
171
					$termeFlouSimple = strtolower(Chaine::supprimerAccents($terme[$this->getChampPourLangue('nom')]));
172
					// Prime pour la ressemblance globale :
173
					$leven = levenshtein($termeFlouSimple, $termeDemandeSimple);
174
					// On affine
175
					$similar = similar_text($termeDemandeSimple, $termeFlouSimple) * 3;
176
					// Prime Soundex
177
					$soundex = (soundex($termeDemandeSimple) == soundex($termeFlouSimple)) ? 1000 : 0;
178
					// Calcul du score
179
					$score = 500 - $leven + $similar + $soundex;
180
 
181
					$termes[$id]['score'] = $score;
182
					$termes[$id]['score_calcul'] = "$termeDemandeSimple / $termeFlouSimple : 500 - $leven + $similar + $soundex = $score";
183
				}
184
				$termes = Tableau::trierMD($termes, array('score' => SORT_DESC));
185
			}
186
		}
187
		//echo 'ici<pre>'.print_r($termes, true).'</pre>';die();
188
		return $termes;
189
	}
190
 
191
	public function recupererNombreTermesTotal() {
192
		if (Config::get('bdd_protocole') == 'mysql') {
193
			$requete = 'SELECT FOUND_ROWS() AS nbre';
194
		} else {
195
			$requete = $this->requeteNbreNomsTotal;
196
		}
197
 
198
		$nombre = $this->bdd->recuperer($requete);
199
		return (int) $nombre['nbre'];
200
	}
201
}
202
?>