Subversion Repositories eFlore/Applications.coel-consultation

Rev

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

Rev Author Line No. Line
9 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Classe Controleur générale de l'application Collection.
5
 *
6
 * @category	php5.2
7
 * @package		Collection
8
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @copyright	2010 Tela-Botanica
10
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
 * @version		SVN: $Id: ColControleur.php 55 2010-04-30 16:18:31Z jpm $
13
 */
11 jpm 14
abstract class ColControleur extends Controleur {
55 jpm 15
 
16
	const FMT_DATE = '%d/%m/%Y';
17
	const FMT_DATE_HEURE = '%d/%m/%Y %H:%i:%s';
18
	const FMT_DATE_HEURE_TXT = '%A %d %B %Y à %H:%M';
19
 
9 jpm 20
	const RENDU_TETE = 'tete';
21
	const RENDU_CORPS = 'corps';
22
	const RENDU_PIED = 'pied';
15 jpm 23
	const TYPE_AUTRE = 'AUTRE';
24
	const TYPE_TOTAL = 'TOTAL';
25
	const SEPARATEUR_TYPE_VALEUR = '##';
26
	const SEPARATEUR_VALEURS = ';;';
27
	const SEPARATEUR_DONNEES = '||';
28
	const VALEUR_NULL = 'NC';
9 jpm 29
 
30
	private $sortie = array();
31
	private $parametres = array();
11 jpm 32
	// FIXME : voir s'il est plus intéressant d'utiliser une méthode dans les classes filles
33
	protected $url = null;
9 jpm 34
 
35
	public function __construct()  {
36
		$registre = Registre::getInstance();
37
		$this->parametres = $registre->get('parametres');
38
		$this->url = $this->parametres['url'];
39
		parent::__construct();
40
	}
41
 
42
	/**
11 jpm 43
	 * Attribue à la bonne position de sortie un contenu.
9 jpm 44
	 */
45
	protected function setSortie($position, $contenu, $fusionner = false) {
11 jpm 46
		if ($this->verifierExistencePosition($position)) {
9 jpm 47
			if ($fusionner) {
48
				$this->sortie[$position] .= $contenu;
49
			} else {
50
				$this->sortie[$position] = $contenu;
51
			}
52
		}
53
	}
11 jpm 54
 
55
	/**
56
	 * Vérifie l'existence de la position indiquée pour son utilisation dans le tableau de sortie.
57
	 * @param string la position à tester.
58
	 * @return bool true si la position est valide, sinon false.
59
	 */
60
	private function verifierExistencePosition($position) {
61
		$existe = true;
62
		if ($position != self::RENDU_TETE &&
63
			$position != self::RENDU_CORPS &&
64
			$position != self::RENDU_PIED) {
65
			trigger_error("La position '$position' n'est pas une valeur prédéfinie.", E_USER_WARNING);
66
			$existe = false;
67
		}
68
		return $existe;
69
	}
70
 
71
	/**
72
	 * Retourne le tableau de sortie à utiliser dans le controleur principal de l'application.
73
	 */
9 jpm 74
	public function getSortie() {
75
		return $this->sortie;
76
	}
77
 
11 jpm 78
	/**
79
	 * Execute l'action d'un module donnée et fusionne le résultat avec le tableau de sortie.
80
	 */
81
	protected function executerAction($ClasseModule, $action) {
82
		$module = new $ClasseModule();
83
		$module->$action();
84
		$this->fusionnerSortie($module->getSortie());
85
 
86
	}
9 jpm 87
 
11 jpm 88
	/**
89
	 * Fusionne un tableau de sortie par défaut avec le tableau passé en paramêtre.
90
	 * @param array le tableau à fusionner
91
	 */
92
	private function fusionnerSortie($sortie) {
93
		$this->sortie = array_merge($this->sortie, $sortie);
94
	}
95
 
16 jpm 96
	protected function construireTxtTruckSimple($chaine_a_analyser) {
97
		return $this->construireTxtTruck($chaine_a_analyser, false, false);
98
	}
99
 
15 jpm 100
	protected function construireTxtTruckSansMajuscule($chaine_a_analyser) {
101
		return $this->construireTxtTruck($chaine_a_analyser, false);
102
	}
103
 
16 jpm 104
	protected function construireTxtTruckSansPointFinal($chaine_a_analyser) {
105
		return $this->construireTxtTruck($chaine_a_analyser, true, false);
106
	}
107
 
108
	protected function construireTxtTruck($chaine_a_analyser, $majuscule = true, $point_final = true) {
15 jpm 109
		$termes = array();
110
		if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != ''))	{
111
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
112
			$nbre_valeurs = count($valeurs);
113
			if ($nbre_valeurs > 0) {
114
				for ($i = 0; $i < $nbre_valeurs; $i++)	{
115
					$valeur = trim($valeurs[$i]);
116
					if ($valeur != '') {
117
						$valeur_formatee = $this->formaterValeurTruck($valeur);
118
						$termes[] = $valeur_formatee;
119
					}
120
				}
121
			}
122
		}
123
 
16 jpm 124
		$chaine_a_retourner = $this->formaterTableauDeTxt($termes, $majuscule, $point_final);
15 jpm 125
		return $chaine_a_retourner;
126
	}
127
 
55 jpm 128
	protected function getTxtTruckParPosition($chaine_a_analyser, $position = 1) {
129
		$retour = '';
130
		if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != ''))	{
131
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
132
			$nbre_valeurs = count($valeurs);
133
			if ($nbre_valeurs > 0) {
134
				$position = $position - 1;
135
				$valeur = trim($valeurs[$position]);
136
				if ($valeur != '') {
137
					$retour = $this->formaterValeurTruck($valeur);
138
				}
139
			}
140
		}
141
		return $retour;
142
	}
143
 
144
	protected function getTableauTruck($chaine_a_analyser) {
145
		$tableau_retour = array();
146
		if ((!is_null($chaine_a_analyser)) && (trim($chaine_a_analyser) != ''))	{
147
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaine_a_analyser);
148
			$nbre_valeurs = count($valeurs);
149
			if ($nbre_valeurs > 0) {
150
				for ($i = 0; $i < $nbre_valeurs; $i++)	{
151
					$valeur = trim($valeurs[$i]);
152
					if ($valeur != '') {
153
						$tableau_retour[] = $valeur;
154
					}
155
				}
156
			}
157
		}
158
		return $tableau_retour;
159
	}
160
 
15 jpm 161
	private function formaterValeurTruck($valeur) {
162
		$chaine_a_retourner = '';
163
 
164
		if (preg_match('/^[^#]+##[^$]+$/', $valeur))	{
165
			$cle_valeur = explode(self::SEPARATEUR_TYPE_VALEUR, $valeur);
166
			$chaine_a_retourner = (($cle_valeur[1] == '' || $cle_valeur[1] == 'null') ? self::VALEUR_NULL : $cle_valeur[1]);
167
			$chaine_a_retourner .= ' '.$this->formaterParenthese($cle_valeur[0]);
168
		} else if ($valeur != '')	{
169
			$chaine_a_retourner = $valeur;
170
		} else {
171
			trigger_error("Valeur truck posant problème :$valeur", E_USER_NOTICE);
172
		}
173
 
174
		return $chaine_a_retourner;
175
	}
176
 
177
	protected function formaterParenthese($chaine_a_afficher) {
178
		if ($chaine_a_afficher != '') {
179
			$chaine_a_afficher = '('.$chaine_a_afficher.')';
180
		}
181
		return $chaine_a_afficher;
182
	}
183
 
184
	protected function formaterSautDeLigne($chaine_a_formater) {
185
		$txt_a_retourner = preg_replace('/\n/', '<br />', $chaine_a_formater);
186
		return $txt_a_retourner;
187
	}
188
 
16 jpm 189
	protected function formaterTableauDeTxt($tableau_de_txt, $majuscule = true, $point_final = true) {
15 jpm 190
		$chaine_a_afficher = '';
191
		$taille_du_tableau = count($tableau_de_txt);
192
		if ($taille_du_tableau > 0) {
193
			$index_avt_dernier = $taille_du_tableau - 1;
194
			for ($i = 0; $i < $taille_du_tableau; $i++)	{
195
				$mot = $tableau_de_txt[$i];
196
				if ($i != $index_avt_dernier) {
197
					$chaine_a_afficher .= $mot.', ';
198
				} else {
16 jpm 199
					$chaine_a_afficher .= $this->nettoyerPointFinal($mot);
200
					if ($point_final) {
201
						$chaine_a_afficher .= '.';
202
					}
15 jpm 203
				}
204
			}
205
		}
206
		if ($majuscule) {
207
			$chaine_a_afficher = ucfirst($chaine_a_afficher);
208
		}
209
		return $chaine_a_afficher;
210
	}
211
 
212
	protected function formaterAutre($chaine_a_afficher) {
213
		if ($chaine_a_afficher != '') {
214
			$chaine_a_afficher = ' [Autre : '.$chaine_a_afficher.']';
215
		}
216
		return $chaine_a_afficher;
217
	}
218
 
219
	protected function formaterOuiNon($chaine_a_formater) {
220
		$txt_a_retourner = '';
221
		if ($chaine_a_formater == '0') {
222
			$txt_a_retourner = 'non';
223
		} else if ($chaine_a_formater == '1') {
224
			$txt_a_retourner = 'oui';
225
		}
226
		return $txt_a_retourner;
227
	}
228
 
55 jpm 229
	protected function formaterDate($date, $format = self::FMT_DATE_HEURE) {
17 jpm 230
		if ($date == '' || $date == '0000-00-00' || $date == '0000-00-00 00:00:00') {
231
			$date = 'Inconnue';
55 jpm 232
		} else {
233
			$timestamp = strtotime($date);
234
			$date = strftime($format, $timestamp);
17 jpm 235
		}
236
		return $date;
237
	}
238
 
15 jpm 239
	protected function nettoyerPointFinal($mot) {
240
		$mot = preg_replace('/[.]$/', '', $mot);
241
		return $mot;
242
	}
243
 
244
	public function construireTxtListeOntologie($chaineAAnalyser, $valeurEstOntologie = true, $typeEstOntologie = true, $donneeEstOntologie = false) {
245
		$termes = array();
246
		$autres = array();
247
		$chaineAAnalyser = trim($chaineAAnalyser);
248
		if ($chaineAAnalyser != '') {
249
			$valeurs = explode(self::SEPARATEUR_VALEURS, $chaineAAnalyser);
250
			$nbreValeurs = count($valeurs);
251
			if ($nbreValeurs > 0)	{
252
				for ($i = 0; $i < $nbreValeurs; $i++)	{
253
					$valeur = $valeurs[$i];
254
 
255
					// VALEUR SANS TYPE
256
					// La valeur sans type est une entrée de l'ontologie
257
					if ($valeurEstOntologie && preg_match('/^[0-9]+$/', $valeur)) {
258
						if ($valeur == '0') {
259
							$valeur = '';
260
						} else {
261
							$valeurOntologie = Ontologie::getValeur($valeur);
262
							if ($valeurOntologie != '') {
263
								$valeur = $valeurOntologie['nom'];
264
							}
265
						}
266
					}
267
 
268
					// VALEUR AVEC TYPE
269
					// Type : AUTRE
270
					$valeurTypeAutre = self::TYPE_AUTRE.self::SEPARATEUR_TYPE_VALEUR;
271
					if (preg_match('/^'.$valeurTypeAutre.'.+$/', $valeur)) {
272
						$txtAutre = preg_replace('/^'.$valeurTypeAutre.'/', '', $valeur);
273
						if ($txtAutre != '') {
274
							$autres[] = $txtAutre;
275
						}
276
						$valeur = '';
277
					}
278
					// Type correspondant à une entrée de l'ontologie
279
					if ($typeEstOntologie) {
19 jpm 280
						$valeurTypeOntologie = '([0-9]+)'.self::SEPARATEUR_TYPE_VALEUR;
281
						if (preg_match('/^'.$valeurTypeOntologie.'.*$/', $valeur, $match)) {
282
							$type = $match[1];
283
							$valeurOntologieNom = Ontologie::getValeurNom($type);
284
							if ($valeurOntologieNom !== false) {
15 jpm 285
								$valeur = preg_replace('/^'.$type.'/', $valeurOntologieNom.': ', $valeur);
286
							}
287
						}
288
					}
289
					// Donnée correspondant à une entrée de l'ontologie
290
					if ($donneeEstOntologie) {
291
						$donneeOntologie = self::SEPARATEUR_TYPE_VALEUR.'([0-9]+)';
292
						if (preg_match('/^.+'.$donneeOntologie.'$/', $valeur, $match)) {
293
							$donnee = $match[1];
294
							$donnee = str_replace(self::SEPARATEUR_TYPE_VALEUR, '', $donnee);
19 jpm 295
							$valeurOntologieNom = Ontologie::getValeurNom($donnee);
296
							if ($valeurOntologieNom !== false) {
15 jpm 297
								$valeur = preg_replace('/'.$donnee.'$/', $valeurOntologieNom, $valeur);
298
							}
299
						}
300
					}
301
 
302
					// Nettoyage final
303
					$valeur = preg_replace('/'.self::SEPARATEUR_TYPE_VALEUR.'/', '', $valeur);
304
 
305
					if ($valeur != '') {
306
						$termes[] = $valeur;
307
					}
308
				}
309
			}
310
		}
311
 
312
		$chaineTermes = $this->formaterTableauDeTxt($termes);
313
		$chaineAutres = $this->formaterTableauDeTxt($autres);
314
		$chaineARetourner = $chaineTermes.$this->formaterAutre($chaineAutres);
315
 
316
		return $chaineARetourner;
317
	}
318
 
18 jpm 319
	protected function obtenirUrlFicheStructure($id_structure) {
320
		$this->url->setVariableRequete('module', 'Fiche');
321
		$this->url->setVariableRequete('action', 'afficherStructure');
322
		$this->url->setVariableRequete('id', $id_structure);
323
		$url = $this->url->getURL();
324
		$this->url->unsetVariablesRequete(array('module', 'action', 'id'));
325
		return $url;
326
	}
327
 
55 jpm 328
	protected function obtenirUrlFicheCollection($id_collection) {
18 jpm 329
		$this->url->setVariableRequete('module', 'Fiche');
330
		$this->url->setVariableRequete('action', 'afficherCollection');
331
		$this->url->setVariableRequete('id', $id_collection);
332
		$url = $this->url->getURL();
333
		$this->url->unsetVariablesRequete(array('module', 'action', 'id'));
334
		return $url;
335
	}
19 jpm 336
 
55 jpm 337
	protected function obtenirUrlFichePersonne($id_personne) {
338
		$this->url->setVariableRequete('module', 'Fiche');
339
		$this->url->setVariableRequete('action', 'afficherPersonne');
340
		$this->url->setVariableRequete('id', $id_personne);
341
		$url = $this->url->getURL();
342
		$this->url->unsetVariablesRequete(array('module', 'action', 'id'));
343
		return $url;
344
	}
345
 
19 jpm 346
	protected function postraiterDonnees(&$tableau) {
347
		if (count($tableau) > 0) {
348
			foreach ($tableau as $cle => &$valeur) {
349
				if ($valeur == '') {
350
					$valeur = '&nbsp;';
351
				} else if (is_string($valeur)) {
352
					$valeur = preg_replace('/&(?!amp;)/i', '&amp;', $valeur, -1);
353
				} else if (is_array($valeur)) {
354
					$this->postraiterDonnees($valeur);
355
				}
356
			}
357
		}
358
	}
9 jpm 359
}