231 |
jpm |
1 |
<?php
|
|
|
2 |
class OntologiesListeGenerique {
|
|
|
3 |
|
|
|
4 |
private $parametres = null;
|
|
|
5 |
private $ressources = null;
|
|
|
6 |
private $ontologieDao = null;
|
|
|
7 |
private $ontologieFormateur = null;
|
|
|
8 |
|
|
|
9 |
private $listeUrl = null;
|
|
|
10 |
private $nbreTotalTermes = 0;
|
|
|
11 |
private $termes = array();
|
|
|
12 |
|
|
|
13 |
public function __construct(Ressources $ressources, Parametres $parametres, OntologieDAO $ontologieDao, OntologieFormateur $ontologieFormateur) {
|
|
|
14 |
$this->ressources = $ressources;
|
|
|
15 |
$this->parametres = $parametres;
|
|
|
16 |
$this->ontologieDao = $ontologieDao;
|
|
|
17 |
$this->ontologieFormateur = $ontologieFormateur;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
public function setListeUrl($url) {
|
|
|
21 |
$this->listeUrl = $url;
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
public function consulter() {
|
|
|
25 |
$this->rechercher();
|
|
|
26 |
if ($this->avoirResultats()) {
|
|
|
27 |
$this->trierResultats();
|
|
|
28 |
$retour = $this->construireTableauRetour();
|
|
|
29 |
} else {
|
|
|
30 |
$message = "Aucun résultat ne correspond a votre requête !";
|
|
|
31 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
32 |
throw new Exception($message, $code);
|
|
|
33 |
}
|
|
|
34 |
return $retour;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
private function rechercher() {
|
|
|
38 |
$resultats = array();
|
|
|
39 |
$resultats = $this->ontologieDao->rechercher();
|
|
|
40 |
$this->termes = $resultats;
|
|
|
41 |
$this->nbreTotalTermes = $this->ontologieDao->recupererNombreTermesTotal();
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
private function avoirResultats() {
|
|
|
45 |
$resultat = ($this->nbreTotalTermes == 0) ? false : true;
|
|
|
46 |
return $resultat;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
private function trierResultats() {
|
|
|
50 |
$recherche = $this->parametres->get('recherche');
|
|
|
51 |
if ($recherche == 'floue') {
|
|
|
52 |
$this->termes = $this->ontologieDao->trierResultatsFloue($this->termes);
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
private function construireTableauRetour() {
|
|
|
57 |
$retour = array('entete' => array(), 'resultats' => array());
|
|
|
58 |
$retour['resultats'] = $this->construireResultats();
|
|
|
59 |
$retour['entete'] = $this->construireEntete();
|
|
|
60 |
return $retour;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
private function construireResultats() {
|
|
|
64 |
$nomsFormates = array();
|
|
|
65 |
foreach ($this->termes as $terme) {
|
|
|
66 |
$id = $terme['id_terme'];
|
|
|
67 |
$termesFormates[$id] = $this->formaterTerme($terme);
|
|
|
68 |
}
|
|
|
69 |
return $termesFormates;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function formaterTerme($infos) {
|
|
|
73 |
$termeAFormater = new OntologieDO($infos);
|
|
|
74 |
$this->ontologieFormateur->setTermeAFormater($termeAFormater);
|
|
|
75 |
$this->ontologieFormateur->setChampsRetour($this->parametres->getListe('retour.champs'));
|
|
|
76 |
$terme = $this->ontologieFormateur->formaterListe();
|
|
|
77 |
return $terme;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private function construireEntete() {
|
|
|
81 |
$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);
|
|
|
82 |
$entete['masque'] = $this->formaterEnteteMasque();
|
|
|
83 |
$entete['depart'] = (int) $this->parametres->get('navigation.depart');
|
|
|
84 |
$entete['limite'] = (int) $this->parametres->get('navigation.limite');
|
|
|
85 |
$entete['total'] = $this->nbreTotalTermes;
|
|
|
86 |
if ($hrefPrecedent = $this->formaterEnteteHrefPrecedent()) {
|
|
|
87 |
$entete['href.precedent'] = $hrefPrecedent;
|
|
|
88 |
}
|
|
|
89 |
if ($hrefSuivant = $this->formaterEnteteHrefSuivant()) {
|
|
|
90 |
$entete['href.suivant'] = $hrefSuivant;
|
|
|
91 |
}
|
|
|
92 |
return $entete;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
private function formaterEnteteMasque() {
|
|
|
96 |
$masquesStrictes = array('code');
|
|
|
97 |
$paramsMasque = array(
|
|
|
98 |
'' => 'terme',
|
|
|
99 |
'code' => 'id_terme',
|
|
|
100 |
'nom' => 'terme',
|
|
|
101 |
'description' => 'definition');
|
|
|
102 |
$etendre = ($this->parametres->get('recherche') == 'etendue') ? true : false;
|
|
|
103 |
|
|
|
104 |
$masqueComplet = array();
|
|
|
105 |
foreach ($paramsMasque as $masqueType => $champ) {
|
|
|
106 |
$masqueParam = 'masque'.($masqueType != '' ? '.'.$masqueType : $masqueType);
|
|
|
107 |
if ($this->parametres->exister($masqueParam)) {
|
|
|
108 |
$masqueValeur = $this->parametres->get($masqueParam);
|
|
|
109 |
$masque = "$champ=$masqueValeur";
|
|
|
110 |
$masque .= ($etendre && in_array($masqueType, $masquesStrictes) === false) ? '%' : '';
|
|
|
111 |
|
|
|
112 |
$masqueComplet[] = $masque;
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
return implode('&', $masqueComplet);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
private function formaterEnteteHrefPrecedent() {
|
|
|
119 |
$limite = $this->parametres->get('navigation.limite');
|
|
|
120 |
$departActuel = $this->parametres->get('navigation.depart');
|
|
|
121 |
$departPrecedent = $departActuel - $limite;
|
|
|
122 |
$href = null;
|
|
|
123 |
if ($departPrecedent >= 0) {
|
|
|
124 |
$squelette = $this->construireTplHrefNavigation();
|
|
|
125 |
$href = sprintf($squelette, $departPrecedent, $limite);
|
|
|
126 |
}
|
|
|
127 |
return $href;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
private function formaterEnteteHrefSuivant() {
|
|
|
131 |
$limite = $this->parametres->get('navigation.limite');
|
|
|
132 |
$departActuel = $this->parametres->get('navigation.depart');
|
|
|
133 |
$departSuivant = $departActuel + $limite;
|
|
|
134 |
$href = null;
|
|
|
135 |
if ($departSuivant < $this->nbreTotalTermes) {
|
|
|
136 |
$squelette = $this->construireTplHrefNavigation();
|
|
|
137 |
$href = sprintf($squelette, $departSuivant, $limite);
|
|
|
138 |
}
|
|
|
139 |
return $href;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
private function construireTplHrefNavigation() {
|
|
|
143 |
$requetes = array();
|
|
|
144 |
$this->parametres->rewind();
|
|
|
145 |
while (is_null($parametre = $this->parametres->key()) === false) {
|
|
|
146 |
if (strpos($parametre, 'navigation') === false) {
|
|
|
147 |
$valeur = $this->parametres->current();
|
|
|
148 |
$requetes[] = "$parametre=$valeur";
|
|
|
149 |
}
|
|
|
150 |
$this->parametres->next();
|
|
|
151 |
}
|
|
|
152 |
$requetes[] = "navigation.depart=%s";
|
|
|
153 |
$requetes[] = "navigation.limite=%s";
|
|
|
154 |
$tpl = $this->listeUrl.'?'.implode('&', $requetes);
|
|
|
155 |
return $tpl;
|
|
|
156 |
}
|
|
|
157 |
}
|