Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
1582 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Service permettant de récupérer toutes les informations d'une observation publique.
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
7
 *
8
 * Cas d'utilisation :
9
 * GET /CelObs/[id] : oû id est l'identifiant d'une observation publique
10
 *
11
 * @author Jean-Pascal MILCENT <jpm@clapas.org>
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 $Id$
15
 * @copyright © 2013, Jean-Pascal MILCENT
16
 */
17
class CelObs extends Cel {
18
 
19
	private $rechercheObs = null;
20
	private $chpsEtendus = null;
21
 
22
	public function __construct($config) {
23
		parent::__construct($config);
24
		$this->rechercheObs = new RechercheObservation($config);
25
		$this->chpsEtendus = new GestionChampsEtendus($config, 'obs');
26
	}
27
 
28
	function getElement($ressources){
29
		$retour = false;
30
		$idObs = $ressources[0];
31
		if (isset($idObs) && preg_match('/^[0-9]+$/', $idObs)) {
32
 
33
			$criteres = array('id_observation' => $idObs, 'transmission' => 1);
1631 raphael 34
			$obsTrouvee = $this->rechercheObs->rechercherObservations(null, $criteres, 0, 1)->get();
1582 jpm 35
 
36
			$observation = array();
37
			if (is_array($obsTrouvee) && count($obsTrouvee) > 0) {
38
				$observation = $obsTrouvee[0];
39
			}
40
			$observation = $this->preparerChamps($observation);
41
			$observation = $this->selectionnerChamps($observation);
42
			$observation = $this->formaterClePourJs($observation);
43
 
44
			$champsEtendus = $this->chpsEtendus->consulter($idObs);
45
			if (is_array($champsEtendus) && count($champsEtendus) > 0) {
46
				$champsEtendus = $this->preparerChampsEtendus($champsEtendus);
47
				$observation['extension'] = $champsEtendus;
48
			}
49
 
50
			$this->envoyerJson($observation);
51
			$retour = true;
52
		}
53
		return $retour;
54
	}
55
 
56
	private function preparerChamps($champs) {
57
		if (isset($champs['date_observation'])) {
58
			$date = explode(' ', $champs['date_observation']);
59
			$champs['date_observation'] = $date[0];
60
		}
61
		return $champs;
62
	}
63
 
64
	private function selectionnerChamps($observation) {
65
		$champs = array('id_observation', 'nom_sel', 'nom_ret', 'nom_ret_nn', 'nt', 'famille',
66
			'nom_referentiel', 'ce_zone_geo', 'zone_geo', 'lieudit', 'station', 'milieu', 'latitude', 'longitude',
67
			'geodatum', 'date_observation', 'mots_cles_texte', 'commentaire', 'date_creation', 'date_modification',
68
			'date_transmission', 'code_insee_calcule', 'abondance', 'certitude', 'phenologie', 'altitude');
69
		$selection = array();
70
		foreach ($champs as $chp) {
71
			if (isset($observation[$chp])) {
72
				$selection[$chp] = $observation[$chp];
73
			}
74
		}
75
		return $selection;
76
	}
77
 
78
	private function formaterClePourJs(Array $tableau) {
79
		$tableauJs = array();
80
		foreach ($tableau as $cle => $valeur) {
81
			if ($cle == 'ce_zone_geo') {
82
				$cle = 'codeZoneGeo';
83
			} else {
84
				$cle = str_replace(' ', '', ucwords(str_replace('_', ' ', $cle)));
85
				$cle{0} = strtolower($cle{0});
86
			}
87
			$tableauJs[$cle] = $valeur;
88
		}
89
		return $tableauJs;
90
	}
91
 
92
	private function preparerChampsEtendus($champs) {
93
		$retour = array();
94
		foreach ($champs as $chp) {
95
			$retour[$chp['cle']] = array('valeur' => $chp['valeur'], 'label' => $chp['label']);
96
		}
97
		return $retour;
98
	}
99
}