Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
584 mathilde 1
<?php
2
/**
3
*
4
*  fonctions et paramètres communs aux traitement des syntaxons
5
*   - traitement de l'ontologie
6
*   - fonction utiles pour les paramètres de configuration entre autres
7
*   - traitement des tables et de leur version
8
*   - traitement de la requête
9
*
10
* @package eflore-projets
11
* @author mathilde Salthun-Lassalle
12
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
* @version 1.0
15
* @copyright 1999-2012 Tela Botanica (accueil@tela-botanica.org)
16
*/
17
 
960 raphael 18
require_once(dirname(__FILE__) . '/../../baseflor/Informations.php');
19
 
584 mathilde 20
class SyntaxonsCommun  extends Commun {
21
 
22
 
23
	protected $table = "";
24
	protected $format_reponse = 'syntaxons';
25
	protected $serviceNom = 'syntaxons';
26
	protected $Bdd;
27
	protected $syn;
28
	protected $niv;
29
	protected $limite_requete = array('depart' => 0, 'limite' => 100);
960 raphael 30
	protected $requete_condition = array();
584 mathilde 31
	protected $total_resultat;
32
	protected $champs_recherches = ' * ';
33
	protected $table_version;
34
	protected $version_projet = '+' ;
960 raphael 35
	static $cache = array();
584 mathilde 36
 
37
 
38
	public function __construct(Conteneur $conteneur) {
39
		$this->Bdd = $conteneur->getBdd();
960 raphael 40
		$this->syn = $this->getParametreTableauSpe('Paramètres.synonymes');
41
		$this->niv = $this->getParametreTableauSpe('Paramètres.niveaux');
584 mathilde 42
	}
43
 
44
 
45
 
46
 
47
 
48
	//+---------------------------------------------------outils------------------------------------------------//
49
	protected function getParametreTableauSpe($cle) {
50
		$tableau = array();
51
		$parametre = Config::get($cle);
52
		if (empty($parametre) === false) {
53
			$tableauPartiel = explode(',', $parametre);
54
			$tableauPartiel = array_map('trim', $tableauPartiel);
55
			foreach ($tableauPartiel as $champ) {
56
				$tableau[] = trim($champ,"'");// spécificité pour le = ? des synonymes
57
			}
58
		}
59
		return $tableau;
60
	}
61
 
62
	//la fonction php array_filters ne marche pas sur le serveur
63
	protected function enleverValeursVides($tableau) {
64
		$tab = array();
65
		foreach ($tableau as $cle => $valeur) {
66
			if ($valeur != '' && is_int($cle)) {
67
				$tab[] = $valeur;
68
			} elseif ($valeur != '' && is_string($cle)) {
69
				$tab[$cle] = $valeur;
70
			}
71
		}
72
		return $tab;
73
	}
74
 
75
	//+++------------------------------traitement des versions----------------------------------------++
76
 
77
	protected function traiterVersion($valeur) {
78
		if (preg_match('/^[0-9]+(?:[._][0-9]+)*$/', $valeur) || $valeur == '+' || $valeur == '*') {
79
			$this->version_projet = $valeur;
80
		} else {
81
			$e = "Erreur : La version est inconnue.";
82
			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
83
		}
84
	}
85
 
86
	protected function definirTables() {
87
		$table_num_version = $this->recupererVersionDisponible();
88
		$prefixe_table = config::get('bdd_table');
89
		if ( in_array($this->version_projet,$table_num_version) ) {
90
			$this->table_version[] = $prefixe_table.'_v'.$this->version_projet;
91
		} elseif ($this->version_projet == '+') {
92
			$derniere_version = $table_num_version[count($table_num_version) - 1];
93
			$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $derniere_version);
94
		} elseif ($this->version_projet == '*') {
95
			foreach ($table_num_version as $num_version) {
96
				$this->table_version[] = $prefixe_table.'_v'.str_replace('.', '_', $num_version);
97
			}
98
		} else {
99
			$e = "Erreur : La version est inconnue.";
100
			throw new Exception($e, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
101
		}
102
	}
103
 
104
	//+------------------------------------ontologies----------------------------------------------------+
105
 
106
	protected function recupererOntologiesNiveau($valeur, $code){
107
		$ontologie = array();
108
		$url = Config::get('url_service_base').Config::get('nom_projet').
109
							'/ontologies/'.$code.':'.urlencode(urlencode($valeur));
960 raphael 110
		if(array_key_exists($url, self::$cache)) {
111
			$val = self::$cache[$url];
901 raphael 112
		} else {
113
			$val = $this->consulterHref($url);
960 raphael 114
			self::$cache[$url] = $val;
901 raphael 115
		}
584 mathilde 116
		$ontologie['niveau.code'] = $valeur;
117
		$ontologie['niveau.libelle'] = $val->nom;
118
		$ontologie['niveau.href'] = $url;
119
		return $ontologie;
120
	}
121
 
122
	protected  function recupererOntologiesNiveauSyn($niveau){
123
		$ontologie = array();
124
		$synonymes = explode(' ',$niveau[1]);
125
		$libelle = array();
126
		$detail = array();
127
		foreach ($synonymes as $syn) {
128
			if ($syn == '=') {
129
				$syn = 'equi';
130
			} elseif ($syn == '= ?') {
131
				$syn = 'prob';
132
			}
133
			if ($syn != '' && $syn != '?') {
134
				$detail[] = $this->recupererOntologiesNiveau($syn, 'syn');
135
				$dernier = end($detail);
136
				$libelle[] = $dernier['niveau.libelle'].' ';
137
			}
138
		}
139
		$ontologie['niveau.code'] = $niveau[0];
140
		$ontologie['niveau.libelle'] = 'synonyme '.implode(' - ',$libelle);
950 raphael 141
		if (isset($niveau[2])) {
584 mathilde 142
			$ontologie['niveau.libelle'] .= ' '.$niveau[2];
143
		}
144
		$ontologie['niveau.detail'] = $detail;
145
		return $ontologie;
146
	}
147
 
148
	protected function traiterOntologieNiveau($niveau) {
149
		$ontologie = array();
960 raphael 150
		if ( preg_match('/syn {0,1}(['.implode($this->syn).']*)( *\[.+\])*/',$niveau,$retour)) {
584 mathilde 151
 
152
			$ontologie = $this->recupererOntologiesNiveauSyn($retour);
153
 
154
		} else {
155
			$ontologie = $this->recupererOntologiesNiveau($niveau, 'UP');
156
		}
157
		return $ontologie;
158
	}
159
 
160
	//+--------------------------FONCTIONS D'ASSEMBLAGE DE LA REQUETE-------------------------------------------+
161
 
162
	protected function assemblerLaRequete() {
163
		$requete = 	' SELECT '.$this->champs_recherches.' FROM '.$this->table.' '
960 raphael 164
            .Informations::retournerRequeteCondition($this->requete_condition).' '.$this->delimiterResultatsRequete();
584 mathilde 165
 
166
		return $requete;
167
	}
168
 
169
	protected function delimiterResultatsRequete() {
960 raphael 170
		$this->total_resultat = Informations::calculerTotalResultat(
171
            $this->getBdd(),
172
            $this->table,
173
            Array(),
174
            $this->requete_condition);
175
 
584 mathilde 176
		$requete_limite = '';
177
		if (($this->limite_requete['depart'] <=  $this->total_resultat) ){
178
			if (($this->limite_requete['limite'] + $this->limite_requete['depart'] )
179
			< $this->total_resultat  ){
180
				$requete_limite = 'LIMIT '.$this->limite_requete['depart'].', '
181
				.$this->limite_requete['limite'];
182
			}
183
		} else {
184
			$e = "Erreur : la valeur pour le paramètre navigation.départ est supérieure".
185
										" au nombre total de résultats.";
186
			throw new Exception($e, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
187
		}
188
		return $requete_limite;
189
	}
190
 
191
 
192
 
193
 
194
}
195
?>