Subversion Repositories eFlore/Applications.coel

Rev

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

Rev Author Line No. Line
1533 jpm 1
<?php
2
/**
3
 * Classe permettant de traiter plus facilement les champs dénomarlisés de la base de données COEL.
4
 *
5
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
6
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
7
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
8
 * @version $Id$
9
 * @copyright 2010
10
 */
11
class UtilTruck {
12
	const TYPE_AUTRE = 'AUTRE';
13
	const TYPE_TOTAL = 'TOTAL';
14
	const SEPARATEUR_TYPE_VALEUR = '##';
15
	const SEPARATEUR_VALEURS = ';;';
16
	const SEPARATEUR_DONNEES = '||';
17
	const VALEUR_NULL = 'NC';
18
 
19
	private $ontologie = null;
20
 
21
	public function __construct() {
22
 
23
	}
24
 
25
	public function __construct(Ontologie $ontologie) {
26
		$this->setOntologie($ontologie);
27
	}
28
 
1535 jpm 29
	public function setOntologie(Ontologie $ontologie) {
1533 jpm 30
		$this->ontologie = $ontologie;
31
	}
32
 
33
	public function construireTxtTruckSimple($chaine_a_analyser) {
34
		return $this->construireTxtTruck($chaine_a_analyser, false, false);
35
	}
36
 
37
	public static function construireTxtTruckSansMajuscule($chaine_a_analyser) {
38
		return $this->construireTxtTruck($chaine_a_analyser, false, true);
39
	}
40
 
41
	public function construireTxtTruckSansPointFinal($chaine_a_analyser) {
42
		return $this->construireTxtTruck($chaine_a_analyser, true, false);
43
	}
44
 
45
	private function construireTxtTruck($chaine_a_analyser, $majuscule = true, $point_final = true) {
46
		$termes = $this->traiterTxtTruck($chaine_a_analyser);
47
		$chaine_a_retourner = self::formaterTableauDeTxt($termes, $majuscule, $point_final);
48
		return $chaine_a_retourner;
49
	}
50
 
51
	public function traiterTxtTruck($chaine_a_analyser) {
52
		$termes = array();
53
		if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != ''))	{
54
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
55
			$nbre_valeurs = count($valeurs);
56
			if ($nbre_valeurs > 0) {
57
				for ($i = 0; $i < $nbre_valeurs; $i++)	{
58
					$valeur = trim($valeurs[$i]);
59
					if ($valeur != '') {
60
						$valeur_formatee = $this->formaterValeurTruck($valeur);
61
						$termes[] = $valeur_formatee;
62
					}
63
				}
64
			}
65
		}
66
		return $termes;
67
	}
68
 
69
	public function getTxtTruckParPosition($chaine_a_analyser, $position = 1) {
70
		$retour = '';
71
		if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != ''))	{
72
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
73
			$nbre_valeurs = count($valeurs);
74
			if ($nbre_valeurs > 0) {
75
				$position = $position - 1;
76
				$valeur = trim($valeurs[$position]);
77
				if ($valeur != '') {
78
					$retour = $this->formaterValeurTruck($valeur);
79
				}
80
			}
81
		}
82
		return $retour;
83
	}
84
 
85
	public function getTableauTruck($chaine_a_analyser) {
86
		$tableau_retour = array();
87
		if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != ''))	{
88
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
89
			$nbre_valeurs = count($valeurs);
90
			if ($nbre_valeurs > 0) {
91
				for ($i = 0; $i < $nbre_valeurs; $i++)	{
92
					$valeur = trim($valeurs[$i]);
93
					if ($valeur != '') {
94
						$tableau_retour[] = $valeur;
95
					}
96
				}
97
			}
98
		}
99
		return $tableau_retour;
100
	}
101
 
102
	private function formaterValeurTruck($valeur) {
103
		$chaine_a_retourner = '';
104
 
105
		if (preg_match('/^[^#]+##[^$]+$/', $valeur))	{
106
			$cle_valeur = explode(self::SEPARATEUR_TYPE_VALEUR, $valeur);
107
			$chaine_a_retourner = (($cle_valeur[1] == '' || $cle_valeur[1] == 'null') ? self::VALEUR_NULL : $cle_valeur[1]);
108
			$chaine_a_retourner .= ' '.$this->formaterParenthese($cle_valeur[0]);
109
		} else if ($valeur != '')	{
110
			$chaine_a_retourner = $valeur;
111
		} else {
112
			trigger_error("Valeur truck posant problème :$valeur", E_USER_NOTICE);
113
		}
114
 
115
		return $chaine_a_retourner;
116
	}
117
 
118
	public function formaterParenthese($chaine_a_afficher) {
119
		if ($chaine_a_afficher != '') {
120
			$chaine_a_afficher = '('.$chaine_a_afficher.')';
121
		}
122
		return $chaine_a_afficher;
123
	}
124
 
125
	public static function formaterTableauDeTxt(Array $tableau_de_txt, $majuscule = true, $point_final = true) {
126
		$chaine_a_afficher = '';
127
		$taille_du_tableau = count($tableau_de_txt);
128
		if ($taille_du_tableau > 0) {
129
			$index_avt_dernier = $taille_du_tableau - 1;
130
			for ($i = 0; $i < $taille_du_tableau; $i++)	{
131
				$mot = $tableau_de_txt[$i];
132
				if ($i != $index_avt_dernier) {
133
					$chaine_a_afficher .= $mot.', ';
134
				} else {
135
					$chaine_a_afficher .= self::nettoyerPointFinal($mot);
136
					if ($point_final) {
137
						$chaine_a_afficher .= '.';
138
					}
139
				}
140
			}
141
		}
142
		if ($majuscule) {
143
			$chaine_a_afficher = ucfirst($chaine_a_afficher);
144
		}
145
		return $chaine_a_afficher;
146
	}
147
 
148
	private static function formaterAutre($chaine_a_afficher) {
149
		if ($chaine_a_afficher != '') {
150
			$chaine_a_afficher = ' [Autre : '.$chaine_a_afficher.']';
151
		}
152
		return $chaine_a_afficher;
153
	}
154
 
155
	private static function formaterOuiNon($chaine_a_formater) {
156
		$txt_a_retourner = '';
157
		if ($chaine_a_formater == '0') {
158
			$txt_a_retourner = 'non';
159
		} else if ($chaine_a_formater == '1') {
160
			$txt_a_retourner = 'oui';
161
		}
162
		return $txt_a_retourner;
163
	}
164
 
165
	private static function nettoyerPointFinal($mot) {
166
		$mot = preg_replace('/[.]$/', '', $mot);
167
		return $mot;
168
	}
169
 
170
	public function construireTxtListeOntologie($chaineAAnalyser, $valeurEstOntologie = true, $typeEstOntologie = true, $donneeEstOntologie = false) {
171
		$termes = array();
172
		$autres = array();
173
		$chaineAAnalyser = trim($chaineAAnalyser);
174
		if ($chaineAAnalyser != '') {
175
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaineAAnalyser);
176
			$nbreValeurs = count($valeurs);
177
			if ($nbreValeurs > 0)	{
178
				for ($i = 0; $i < $nbreValeurs; $i++)	{
179
					$valeur = $valeurs[$i];
180
 
181
					// VALEUR SANS TYPE
182
					// La valeur sans type est une entrée de l'ontologie
183
					if ($valeurEstOntologie && preg_match('/^[0-9]+$/u', $valeur)) {
184
						if ($valeur == '0') {
185
							$valeur = '';
186
						} else {
187
							if (isset($this->ontologie)) {
188
								$valeurOntologie = $this->ontologie->getValeur($valeur);
189
								if (isset($valeurOntologie)) {
190
									$valeur = $valeurOntologie['nom'];
191
								}
192
							} else {
1535 jpm 193
								$e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
1533 jpm 194
								trigger_error($e, E_USER_WARNING);
195
							}
196
						}
197
					}
198
 
199
					// VALEUR AVEC TYPE
200
					// Type : AUTRE
201
					$valeurTypeAutre = self::TYPE_AUTRE.self::SEPARATEUR_TYPE_VALEUR;
202
					if (preg_match('/^'.$valeurTypeAutre.'.+$/u', $valeur)) {
203
						$txtAutre = preg_replace('/^'.$valeurTypeAutre.'/u', '', $valeur);
204
						if ($txtAutre != '') {
205
							$autres[] = $txtAutre;
206
						}
207
						$valeur = '';
208
					}
209
					// Type correspondant à une entrée de l'ontologie
210
					if ($typeEstOntologie) {
211
						$valeurTypeOntologie = '([0-9]+)'.self::SEPARATEUR_TYPE_VALEUR;
212
						$valeurTypeAutre = '([[:alnum:]]+)'.self::SEPARATEUR_TYPE_VALEUR;
213
						if (preg_match('/^'.$valeurTypeOntologie.'.*$/u', $valeur, $match)) {// Cas type : réf. numérique
214
							$type = $match[1];
1535 jpm 215
							if (isset($this->ontologie)) {
1533 jpm 216
								$valeurOntologieNom = $this->ontologie->getValeurNom($type);
217
								if (isset($valeurOntologieNom)) {
218
									$valeurOntologieNom .= ' : ';
219
									$valeur = preg_replace('/^'.$type.'/u', $valeurOntologieNom, $valeur);
220
								}
221
							} else {
1535 jpm 222
								$e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
1533 jpm 223
								trigger_error($e, E_USER_WARNING);
224
							}
225
						} else if (preg_match('/^'.$valeurTypeAutre.'.*$/u', $valeur, $match)) {// Cas type : AUTRE
226
							$type = $match[1];
227
							$valeur = preg_replace('/^'.$type.'/u', $type.' : ', $valeur);
228
						}
229
					}
230
					// Donnée correspondant à une entrée de l'ontologie
231
					if ($donneeEstOntologie) {
232
						$donneeOntologie = self::SEPARATEUR_TYPE_VALEUR.'([0-9]+)';
233
						if (preg_match('/^.+'.$donneeOntologie.'$/u', $valeur, $match)) {
234
							$donnee = $match[1];
235
							$donnee = str_replace(self::SEPARATEUR_TYPE_VALEUR, '', $donnee);
236
							if (isset($this->ontologie)) {
237
								$valeurOntologieNom = $this->ontologie->getValeurNom($donnee);
238
								if (isset($valeurOntologieNom)) {
239
									$valeur = preg_replace('/'.$donnee.'$/u', $valeurOntologieNom, $valeur);
240
								}
241
							} else {
1535 jpm 242
								$e = "Veuillez définir l'ontologie à utiliser en employant la méthode setOntologie().";
1533 jpm 243
								trigger_error($e, E_USER_WARNING);
244
							}
245
						}
246
					}
247
 
248
					// Nettoyage final
249
					$valeur = preg_replace('/'.self::SEPARATEUR_TYPE_VALEUR.'/', '', $valeur);
250
 
251
					if ($valeur != '') {
252
						$termes[] = $valeur;
253
					}
254
				}
255
			}
256
		}
257
 
258
		$chaineTermes = self::formaterTableauDeTxt($termes);
259
		$chaineAutres = self::formaterTableauDeTxt($autres);
260
		$chaineARetourner = $chaineTermes.self::formaterAutre($chaineAutres);
261
 
262
		return $chaineARetourner;
263
	}
264
}