Subversion Repositories eFlore/Applications.cel

Rev

Rev 3365 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3318 idir 1
 <?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Service affichant les dernières photo publiques du CEL ouvrable sous forme de diaporama.
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
7
 *
8
 * Cas d'utilisation et documentation :
9
 * @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetPhoto
10
 *
11
 * Paramètres :
12
 * ===> extra = booléen (1 ou 0)  [par défaut : 1]
13
 * Affiche / Cache la vignette en taille plus importante au bas du widget.
14
 * ===> vignette = [0-9]+,[0-9]+  [par défaut : 4,3]
15
 * Indique le nombre de vignette par ligne et le nombre de ligne.
16
 *
17
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @license	GPL v3 <http://www.gnu.org/licenses/gpl.txt>
19
 * @license	CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
20
 * @version	$Id$
21
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
22
 */
23
class Streets extends WidgetCommun {
24
 
25
	const DS = DIRECTORY_SEPARATOR;
26
	const SERVICE_DEFAUT = 'saisie';
27
	const WS_SAISIE = 'CelWidgetSaisie';
28
	const WS_UPLOAD = 'CelWidgetUploadImageTemp';
29
	const WS_IMG_LIST = 'celImage';
30
	const WS_OBS = 'CelObs';
31
	const WS_OBS_LIST = 'InventoryObservationList';
32
	const LANGUE_DEFAUT = 'fr';
33
	const PROJET_DEFAUT = 'streets';
34
	const WS_NOM = 'noms';
35
	const EFLORE_API_VERSION = '0.1';
36
	private $cel_url_tpl = null;
37
	/** Si spécifié, on ajoute une barre de navigation inter-applications */
38
	private $bar;
39
	//private $parametres_autorises = array('projet', 'type', 'langue', 'order');
40
	private $parametres_autorises = array(
41
			'projet' => 'projet',
42
			'type' => 'type',
43
			'langue' => 'langue',
44
			'order' => 'order'
45
	);
46
	/**
47
	 * Méthode appelée par défaut pour charger ce widget.
48
	 */
49
	public function executer() {
50
		$retour = null;
51
		// Pour la création de l'id du cache nous ne tenons pas compte du paramètre de l'url callback
52
		unset($this->parametres['callback']);
53
		extract($this->parametres);
54
		$this->bar = (isset($bar)) ? $bar : false;
55
		/* Le fichier Framework.php du Framework de Tela Botanica doit être appelé avant tout autre chose dans l'application.
56
		 Sinon, rien ne sera chargé.
57
		 L'emplacement du Framework peut varier en fonction de l'environnement (test, prod...). Afin de faciliter la configuration
58
		 de l'emplacement du Framework, un fichier framework.defaut.php doit être renommé en framework.php et configuré pour chaque installation de
59
		 l'application.
60
		 Chemin du fichier chargeant le framework requis */
61
		$framework = dirname(__FILE__).'/framework.php';
62
		if (!file_exists($framework)) {
63
			$e = "Veuillez paramêtrer l'emplacement et la version du Framework dans le fichier $framework";
64
			trigger_error($e, E_USER_ERROR);
65
		} else {
66
			// Inclusion du Framework
67
			require_once $framework;
68
			// Ajout d'information concernant cette application
69
			Framework::setCheminAppli(__FILE__);// Obligatoire
70
			Framework::setInfoAppli(Config::get('info'));// Optionnel
71
 
72
		}
73
		$langue = (isset($langue)) ? $langue : self::LANGUE_DEFAUT;
74
		$this->langue = I18n::setLangue($langue);
75
		$this->parametres['langue'] = $langue;
76
 
77
		if (!isset($mode)) {
78
			$mode = self::SERVICE_DEFAUT;
79
		}
80
		if (!isset($projet)) {
81
			$this->parametres['projet'] = self::PROJET_DEFAUT;
82
		}
83
 
84
		$methode = $this->traiterNomMethodeExecuter($mode);
85
		if (method_exists($this, $methode)) {
86
			$retour = $this->$methode();
87
		} else {
88
			$this->messages[] = "Ce type de service '$methode' n'est pas disponible.";
89
		}
90
 
91
		$contenu = '';
92
		if (is_null($retour)) {
93
			$this->messages[] = 'La ressource demandée a retourné une valeur nulle.';
94
		} else {
95
			if (isset($retour['donnees'])) {
96
				$retour['donnees']['conf_mode'] = $this->config['parametres']['modeServeur'];
97
				$retour['donnees']['prod'] = ($this->config['parametres']['modeServeur'] == 'prod');
98
				$retour['donnees']['bar'] = $this->bar;
99
				$retour['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], $this->config['streets']['cheminDos']);
100
				$retour['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/');
101
				$retour['donnees']['url_ws_streets'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE);
102
				$retour['donnees']['url_ws_obs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS);
103
				$retour['donnees']['url_ws_obs_list'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_OBS_LIST);
104
				$retour['donnees']['url_ws_upload'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_UPLOAD);
105
				$retour['donnees']['url_ws_cel_imgs'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_IMG_LIST) . '/liste-ids?obsId=';
106
				$retour['donnees']['url_ws_cel_img_url'] = str_replace ( '%s.jpg', '{id}XS', $this->config['chemins']['celImgUrlTpl'] );
107
				$retour['donnees']['mode'] = $mode;
108
				$retour['donnees']['langue'] = $langue;
109
				if( isset( $this->parametres['squelette'] ) ) {
110
					$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS. $this->parametres['squelette'].'.tpl.html';
111
				} else {
112
					$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$retour['squelette'].'.tpl.html';
113
				}
114
				$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
115
			} else {
116
				$this->messages[] = 'Les données à transmettre au squelette sont nulles.';
117
			}
118
		}
119
		$this->envoyer($contenu);
120
	}
121
 
122
 
123
	private function executerSaisie() {
124
		$retour = array();
125
		$retour['squelette'] = 'streets';
126
		$retour['donnees']['general'] = I18n::get('General');
127
		$retour['donnees']['observateur'] = I18n::get('Observateur');
128
        $retour['donnees']['observation'] = I18n::get('Observation');
129
        $retour['donnees']['arbres'] = I18n::get('Arbres');
130
        $retour['donnees']['image'] = I18n::get('Image');
131
        $retour['donnees']['plantes'] = I18n::get('Plantes');
132
        $retour['donnees']['chpsupp'] = I18n::get('Chpsupp');
133
        $retour['donnees']['resume'] = I18n::get('Resume');
134
		$retour['donnees']['widget'] = $this->rechercherProjet();
135
		return $retour;
136
	}
137
 
138
	/* Recherche si le projet existe sinon va chercher les infos de base */
139
	private function rechercherProjet() {
140
		$tab = array();
141
		$url = $this->config['streets']['celUrlTpl'].'?projet='.$this->parametres['projet'].'&langue='.$this->parametres['langue'];
142
		$json = $this->getDao()->consulter($url);
143
		$tableau = json_decode($json, true);
3365 idir 144
		$tableau = $this->traiterParametres($tableau[0]);
3318 idir 145
		if (isset($this->parametres['squelette']) && $this->parametres['squelette'] === 'plantes') {
146
			$tableau['type_especes'] = 'liste';
147
		}
148
		$tableau['especes'] = $this->rechercherInfosEspeces($tableau);
149
		if ($tableau['milieux'] != "") {
150
			$tableau['milieux'] = explode(";", $tableau['milieux']);
151
		} else {
152
			$tableau['milieux'] = array();
153
		}
154
		if (isset($tableau['motscles'])) {
155
		    $tableau['tag-obs'] = $tableau['tag-img'] = $tableau['motscles'];
156
		}
157
		$tableau['chpSupp'] = $tab;
158
		$langue_projet_url = ( isset ( $this->parametres['langue'] ) && $this->parametres['langue'] !== 'fr' ) ? '_' . $this->parametres['langue'] : '';
159
		$tableau['chemin_fichiers'] = sprintf( $this->config['chemins']['baseURLAbsoluDyn'], $this->config['streets']['imgProjet'] . '/' );
160
		return $tableau;
161
	}
162
 
163
	// remplace certains parametres définis en bd par les parametres définis dans l'url
3365 idir 164
	private function traiterParametres($tableau) {
3318 idir 165
		$criteres = array('tag-obs', 'tag-img', 'projet', 'titre', 'logo');
166
		foreach($this->parametres as $nom_critere => $valeur_critere) {
3365 idir 167
			if (in_array($nom_critere, $criteres)) {
3318 idir 168
				$tableau[$nom_critere] = $valeur_critere;
169
			}
170
		}
171
		return $tableau;
172
	}
173
 
174
	private function rechercherInfosEspeces( $infos_projets ) { //print_r($infos_projets);exit;
175
		$retour = array();
176
		$referentiel = $infos_projets['referentiel'];
177
		$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl'];
178
		$retour['url_ws_autocompletion_ns'] = sprintf( $urlWsNsTpl, self::EFLORE_API_VERSION, $referentiel, self::WS_NOM );;
179
		$retour['url_ws_autocompletion_ns_tpl'] = sprintf( $urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM );
180
		$retour['ns_referentiel'] = $referentiel;
181
 
182
		if ( isset( $infos_projets['type_especes'] ) ) {
183
 
184
			switch ( $infos_projets['type_especes'] ) {
185
				case 'fixe' :
186
					$retour = $this->chargerInfosTaxon( $infos_projets['referentiel'], $infos_projets['especes'] );
187
					break;
188
				case 'referentiel' : break;
189
				case 'liste' :
190
					$referentiel = $infos_projets['referentiel'];
191
					$retour['taxons'] = $this->recupererListeNomsSci();
192
					break;
193
			}
194
		} else if ( isset( $infos_projets['referentiel'] ) ) {
195
			$referentiel = $infos_projets['referentiel'];
196
			if ( isset($infos_projets['num_nom'] ) ) {
197
				$retour = $this->chargerInfosTaxon( $infos_projets['referentiel'], $infos_projets['num_nom'] );
198
			}
199
		}
200
		return $retour;
201
	}
202
 
203
	/**
204
	 * Consulte un webservice pour obtenir des informations sur le taxon dont le
205
	 * numéro nomenclatural est $num_nom (ce sont donc plutôt des infos sur le nom
206
	 * et non le taxon?)
207
	 * @param string|int $num_nom
208
	 * @return array
209
	 */
210
	protected function chargerInfosTaxon( $referentiel, $num_nom ) {
211
		$url_service_infos = sprintf( $this->config['chemins']['infosTaxonUrl'], $referentiel, $num_nom );
212
		$infos = json_decode( file_get_contents( $url_service_infos ) );
213
		// trop de champs injectés dans les infos espèces peuvent
214
		// faire planter javascript
215
		$champs_a_garder = array( 'id', 'nom_sci','nom_sci_complet', 'nom_complet', 'famille','nom_retenu.id', 'nom_retenu_complet', 'num_taxonomique' );
216
		$resultat = array();
217
		$retour = array();
218
		if ( isset( $infos ) && !empty( $infos ) ) {
219
			$infos = (array) $infos;
220
			if ( isset( $infos['nom_sci'] ) && $infos['nom_sci'] !== '' ) {
221
				$resultat = array_intersect_key( $infos, array_flip($champs_a_garder ) );
222
				$resultat['retenu'] = ( $infos['id'] == $infos['nom_retenu.id'] ) ? 'true' : 'false';
223
				$retour['espece_imposee'] = true;
224
				$retour['nn_espece_defaut'] = $nnEspeceImposee;
225
				$retour['nom_sci_espece_defaut'] = $resultat['nom_complet'];
226
				$retour['infos_espece'] = $this->array2js( $resultat, true );
227
			}
228
		}
229
		return $retour;
230
	}
231
 
232
	protected function getReferentielImpose() {
233
		$referentiel_impose = true;
234
		if (!empty($_GET['referentiel']) && $_GET['referentiel'] !== 'autre') {
235
			$this->ns_referentiel = $_GET['referentiel'];
236
		} else if (isset($this->configProjet['referentiel'])) {
237
			$this->ns_referentiel = $this->configProjet['referentiel'];
238
		} else if (isset($this->configMission['referentiel'])) {
239
			$this->ns_referentiel = $this->configMission['referentiel'];
240
		} else {
241
			$referentiel_impose = false;
242
		}
243
		return $referentiel_impose;
244
	}
245
 
246
	/**
247
	 * Trie par nom français les taxons lus dans le fichier tsv
248
	 */
249
	protected function recupererListeNomsSci() {
250
		$taxons = $this->recupererListeTaxon();
251
		if (is_array($taxons)) {
252
			$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC));
253
		}
254
		return $taxons;
255
	}
256
 
257
	/**
258
	 * @TODO documenter
259
	 * @return array
260
	 */
261
	protected function recupererListeNoms() {
262
		$taxons = $this->recupererListeTaxon();
263
		$nomsAAfficher = array();
264
		$nomsSpeciaux = array();
265
		if (is_array($taxons)) {
266
			foreach ($taxons as $taxon) {
267
				$nomSciTitle = $taxon['nom_ret'].
268
				($taxon['nom_fr'] != '' ? ' - '.$taxon['nom_fr'] : '' ).
269
				($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
270
				$nomFrTitle = $taxon['nom_sel'].
271
				($taxon['nom_ret'] != $taxon['nom_sel']? ' - '.$taxon['nom_ret'] : '' ).
272
				($taxon['nom_fr_autre'] != '' ? ' - '.$taxon['nom_fr_autre'] : '' );
273
 
274
				if ($taxon['groupe'] == 'special') {
275
					$nomsSpeciaux[] = array(
276
							'num_nom' => $taxon['num_nom_sel'],
277
							'nom_a_afficher' => $taxon['nom_fr'],
278
							'nom_a_sauver' => $taxon['nom_sel'],
279
							'nom_title' => $nomSciTitle,
280
							'nom_type' => 'nom-special');
281
				} else {
282
					$nomsAAfficher[] = array(
283
							'num_nom' => $taxon['num_nom_sel'],
284
							'nom_a_afficher' => $taxon['nom_sel'],
285
							'nom_a_sauver' => $taxon['nom_sel'],
286
							'nom_title' => $nomSciTitle,
287
							'nom_type' => 'nom-sci');
288
					$nomsAAfficher[] = array(
289
							'num_nom' => $taxon['num_nom_sel'],
290
							'nom_a_afficher' => $taxon['nom_fr'],
291
							'nom_a_sauver' => $taxon['nom_fr'],
292
							'nom_title' => $nomFrTitle,
293
							'nom_type' => 'nom-fr');
294
				}
295
			}
296
			$nomsAAfficher = self::trierTableauMd($nomsAAfficher, array('nom_a_afficher' => SORT_ASC));
297
			$nomsSpeciaux = self::trierTableauMd($nomsSpeciaux, array('nom_a_afficher' => SORT_ASC));
298
		}
299
		return array('speciaux' => $nomsSpeciaux, 'sci-et-fr' => $nomsAAfficher);
300
	}
301
 
302
	/**
303
	 * Lit une liste de taxons depuis un fichier tsv fourni
304
	 */
305
	protected function recupererListeTaxon() {
306
		$taxons = array();
3335 idir 307
		$fichier_tsv = dirname(__FILE__) . self::DS . 'configurations' . self::DS . 'sauvages_taxons.tsv';
3318 idir 308
 
309
		if ( file_exists( $fichier_tsv ) && is_readable( $fichier_tsv ) ) {
310
			$taxons = $this->decomposerFichierTsv( $fichier_tsv );
311
		} else {
312
			$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'.";
313
		}
314
		return $taxons;
315
	}
316
 
317
	/**
318
	 * Découpe un fihcier csv
319
	 */
320
	protected function decomposerFichierTsv($fichier, $delimiter = "\t"){
321
		$header = null;
322
		$data = array();
323
		if (($handle = fopen($fichier, "r")) !== FALSE) {
324
			while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
325
				if (!$header) {
326
					$header = $row;
327
				} else {
328
					$data[] = array_combine($header, $row);
329
				}
330
			}
331
			fclose($handle);
332
		}
333
		return $data;
334
	}
335
 
336
	/**
337
	 * Convertit un tableau PHP en Javascript - @WTF pourquoi ne pas faire un json_encode ?
338
	 * @param array $array
339
	 * @param boolean $show_keys
340
	 * @return une portion de JSON représentant le tableau
341
	 */
342
	protected function array2js($array,$show_keys) {
343
		$tableauJs = '{}';
344
		if (!empty($array)) {
345
			$total = count($array) - 1;
346
			$i = 0;
347
			$dimensions = array();
348
			foreach ($array as $key => $value) {
349
				if (is_array($value)) {
350
					$dimensions[$i] = array2js($value,$show_keys);
351
					if ($show_keys) {
352
						$dimensions[$i] = '\"'.$key.'\":'.$dimensions[$i];
353
					}
354
				} else {
355
					$dimensions[$i] = '\"'.addslashes($value).'\"';
356
					if ($show_keys) {
357
						$dimensions[$i] = '\"'.$key.'\":'.$dimensions[$i];
358
					}
359
				}
360
				if ($i == 0) {
361
					$dimensions[$i] = '{'.$dimensions[$i];
362
				}
363
				if ($i == $total) {
364
					$dimensions[$i].= '}';
365
				}
366
				$i++;
367
			}
368
			$tableauJs = implode(',', $dimensions);
369
		}
370
		return $tableauJs;
371
	}
372
}
373
?>