Subversion Repositories eFlore/Applications.cel

Rev

Rev 2131 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2131 Rev 2163
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Service permettant de récupérer toutes les informations d'une observation publique.
4
 * Service permettant de récupérer toutes les informations d'une observation publique.
5
 * Encodage en entrée : utf8
5
 * Encodage en entrée : utf8
6
 * Encodage en sortie : utf8
6
 * Encodage en sortie : utf8
7
 *
7
 *
8
 * Cas d'utilisation :
8
 * Cas d'utilisation :
9
 * GET /CelObs/[id] : oû id est l'identifiant d'une observation publique
9
 * GET /CelObs/[id] : oû id est l'identifiant d'une observation publique
10
 *
10
 *
11
 * @author Jean-Pascal MILCENT <jpm@clapas.org>
11
 * @author Jean-Pascal MILCENT <jpm@clapas.org>
12
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
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>
13
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
 * @version $Id$
14
 * @version $Id$
15
 * @copyright © 2013, Jean-Pascal MILCENT
15
 * @copyright © 2013, Jean-Pascal MILCENT
16
 */
16
 */
17
class CelObs extends Cel {
17
class CelObs extends Cel {
18
 
-
 
19
	private $rechercheObs = null;
18
	private $rechercheObs = null;
20
	private $chpsEtendus = null;
19
	private $chpsEtendus = null;
-
 
20
	private $donnees = null;
21
 
21
 
22
	public function __construct($config) {
22
	public function __construct($config) {
23
		parent::__construct($config);
23
		parent::__construct($config);
24
		$this->rechercheObs = new RechercheObservation($config);
24
		$this->rechercheObs = new RechercheObservation($config);
25
		$this->chpsEtendus = new GestionChampsEtendus($config, 'obs');
25
		$this->chpsEtendus = new GestionChampsEtendus($config, 'obs');
-
 
26
 
-
 
27
		$this->chargerNomsTablesReferentiels();
-
 
28
	}
-
 
29
 
-
 
30
	private function chargerNomsTablesReferentiels() {
-
 
31
		// Créé des attributs avec le code du référentiel : bdtfx, bdtxa, apd, isfan
-
 
32
		foreach ( $this->config['referentiels'] as $referentiel => $table) {
-
 
33
			$this->$referentiel = $table;
-
 
34
		}
26
	}
35
	}
27
 
36
 
28
	function getElement($ressources){
37
	function getElement($ressources){
29
		$retour = false;
38
		$retour = false;
30
		$idObs = $ressources[0];
39
		$idObs = $ressources[0];
31
		if (isset($idObs) && preg_match('/^[0-9]+$/', $idObs)) {
40
		if (isset($idObs) && preg_match('/^[0-9]+$/', $idObs)) {
32
 
41
 
33
			$criteres = array('id_observation' => $idObs, 'transmission' => 1);
42
			$criteres = array('id_observation' => $idObs, 'transmission' => 1);
34
			$obsTrouvee = $this->rechercheObs->rechercherObservations(null, $criteres, 0, 1)->get();
43
			$obsTrouvee = $this->rechercheObs->rechercherObservations(null, $criteres, 0, 1)->get();
35
 
44
 
36
			$observation = array();
45
			$observation = array();
37
			if (is_array($obsTrouvee) && count($obsTrouvee) > 0) {
46
			if (is_array($obsTrouvee) && count($obsTrouvee) > 0) {
38
				$observation = $obsTrouvee[0];
47
				$observation = $obsTrouvee[0];
39
			}
48
			}
40
			$observation = $this->preparerChamps($observation);
49
			$observation = $this->preparerChamps($observation);
41
			$observation = $this->selectionnerChamps($observation);
50
			$observation = $this->selectionnerChamps($observation);
42
			$observation = $this->formaterClePourJs($observation);
51
			$observation = $this->formaterClePourJs($observation);
43
 
52
 
44
			$champsEtendus = $this->chpsEtendus->consulter($idObs);
53
			$champsEtendus = $this->chpsEtendus->consulter($idObs);
45
			if (is_array($champsEtendus) && count($champsEtendus) > 0) {
54
			if (is_array($champsEtendus) && count($champsEtendus) > 0) {
46
				$champsEtendus = $this->preparerChampsEtendus($champsEtendus);
55
				$champsEtendus = $this->preparerChampsEtendus($champsEtendus);
47
				$observation['extension'] = $champsEtendus;
56
				$observation['extension'] = $champsEtendus;
48
			}
57
			}
49
 
58
 
50
			$this->envoyerJson($observation);
59
			$this->envoyerJson($observation);
51
			$retour = true;
60
			$retour = true;
52
		}
61
		}
53
		return $retour;
62
		return $retour;
54
	}
63
	}
55
 
64
 
56
	private function preparerChamps($champs) {
65
	private function preparerChamps($champs) {
57
		if (isset($champs['date_observation'])) {
66
		if (isset($champs['date_observation'])) {
58
			$date = explode(' ', $champs['date_observation']);
67
			$date = explode(' ', $champs['date_observation']);
59
			$champs['date_observation'] = $date[0];
68
			$champs['date_observation'] = $date[0];
60
		}
69
		}
61
		return $champs;
70
		return $champs;
62
	}
71
	}
63
 
72
 
64
	private function selectionnerChamps($observation) {
73
	private function selectionnerChamps($observation) {
65
		$champs = array('id_observation', 'nom_sel', 'nom_ret', 'nom_ret_nn', 'nt', 'famille',
74
		$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',
75
			'nom_referentiel', 'ce_zone_geo', 'zone_geo', 'lieudit', 'station', 'milieu', 'latitude', 'longitude',
67
			'geodatum', 'date_observation', 'mots_cles_texte', 'commentaire', 'date_creation', 'date_modification',
76
			'geodatum', 'date_observation', 'mots_cles_texte', 'commentaire', 'date_creation', 'date_modification',
68
			'date_transmission', 'code_insee_calcule', 'abondance', 'certitude', 'phenologie', 'altitude');
77
			'date_transmission', 'code_insee_calcule', 'abondance', 'certitude', 'phenologie', 'altitude');
69
		$selection = array();
78
		$selection = array();
70
		foreach ($champs as $chp) {
79
		foreach ($champs as $chp) {
71
			if (isset($observation[$chp])) {
80
			if (isset($observation[$chp])) {
72
				$selection[$chp] = $observation[$chp];
81
				$selection[$chp] = $observation[$chp];
73
			}
82
			}
74
		}
83
		}
75
		return $selection;
84
		return $selection;
76
	}
85
	}
77
 
86
 
78
	private function formaterClePourJs(Array $tableau) {
87
	private function formaterClePourJs(Array $tableau) {
79
		$tableauJs = array();
88
		$tableauJs = array();
80
		foreach ($tableau as $cle => $valeur) {
89
		foreach ($tableau as $cle => $valeur) {
81
			if ($cle == 'ce_zone_geo') {
90
			if ($cle == 'ce_zone_geo') {
82
				$cle = 'codeZoneGeo';
91
				$cle = 'codeZoneGeo';
83
			} else {
92
			} else {
84
				$cle = str_replace(' ', '', ucwords(str_replace('_', ' ', $cle)));
93
				$cle = str_replace(' ', '', ucwords(str_replace('_', ' ', $cle)));
85
				$cle{0} = strtolower($cle{0});
94
				$cle{0} = strtolower($cle{0});
86
			}
95
			}
87
			$tableauJs[$cle] = $valeur;
96
			$tableauJs[$cle] = $valeur;
88
		}
97
		}
89
		return $tableauJs;
98
		return $tableauJs;
90
	}
99
	}
91
 
100
 
92
	private function preparerChampsEtendus($champs) {
101
	private function preparerChampsEtendus($champs) {
93
		$retour = array();
102
		$retour = array();
94
		foreach ($champs as $chp) {
103
		foreach ($champs as $chp) {
95
			$retour[$chp['cle']] = array('valeur' => $chp['valeur'], 'label' => $chp['label']);
104
			$retour[$chp['cle']] = array('valeur' => $chp['valeur'], 'label' => $chp['label']);
96
		}
105
		}
97
		return $retour;
106
		return $retour;
98
	}
107
	}
-
 
108
 
-
 
109
	/**
-
 
110
	 * Méthode appelée avec une requête de type POST et un identifiant d'observation.
-
 
111
	 * Modifie une observation en fonction des informations envoyées en POST.
-
 
112
	 * Utilisé par:
-
 
113
	 * - service:del:0.1/determinations/ : ValiderDetermination.php::modifierObservationParDetermination()
-
 
114
	 * - service:del:0.1/observations/#idObs [POST] : pour dépublier une observation
-
 
115
	 *
-
 
116
	 * @param $uid array	$uid[0] (int) : identifiant observation
-
 
117
	 * @param pairs array	tableau contenant les champs à modifier sous la forme : nom_du_champ=nouvelle_valeur
-
 
118
	 */
-
 
119
	public function updateElement($ressources, $donnees) {
-
 
120
		$this->donnees = $donnees;
-
 
121
		if ($this->controlerAccessibiliteWs()) {
-
 
122
			if ($this->controleAppelIpAutorisee()) {
-
 
123
				$idObs = isset($ressources[0]) ? $ressources[0] : '';
-
 
124
				$this->verifierIdentifiantObs($idObs);
-
 
125
 
-
 
126
				if (count($this->donnees) == 1) {
-
 
127
					$donneesObligatoires = array('transmission');
-
 
128
					if ($this->verifierDonneesObligatoires($donneesObligatoires)) {
-
 
129
						$this->depublierObs($idObs);
-
 
130
					}
-
 
131
				} else if (count($this->donnees) == 4) {
-
 
132
					$donneesObligatoires = array('id_observation', 'nom_sel_nn', 'nom_referentiel', 'id_utilisateur');
-
 
133
					if ($this->verifierDonneesObligatoires($donneesObligatoires)) {
-
 
134
						$this->accepterPropositionDEL($idObs);
-
 
135
					}
-
 
136
				} else {
-
 
137
					$msg = "La modification complète d'une observation n'est pas implémentée.";
-
 
138
					$this->envoyerMessageErreur(501, $msg);
-
 
139
				}
-
 
140
 
-
 
141
				$this->envoyer('ok');
-
 
142
			}
-
 
143
		}
-
 
144
	}
-
 
145
 
-
 
146
	private function verifierIdentifiantObs($chaine) {
-
 
147
		$ok = preg_match('/^[0-9]+$/', $chaine);
-
 
148
		if ($ok == false) {
-
 
149
			$msg = "Indiquer un seul identifiant numérique d'observation.";
-
 
150
			$this->envoyerMessageErreur(412, $msg);
-
 
151
		}
-
 
152
		return $ok;
-
 
153
	}
-
 
154
 
-
 
155
	private function verifierDonneesObligatoires($champsObligatoires) {
-
 
156
		foreach ($champsObligatoires as $param) {
-
 
157
			if (! isset($this->donnees[$param])) {
-
 
158
				$msg = sprintf("Paramètre %s manquant (parmi %s)", $param, implode(', ', $champsObligatoires));
-
 
159
				$this->envoyerMessageErreur(412, $msg);
-
 
160
			}
-
 
161
		}
-
 
162
		return true;
-
 
163
	}
-
 
164
 
-
 
165
	private function depublierObs($idObs) {
-
 
166
		$gestionnaireObs = new GestionObservation($this->config);
-
 
167
		$depublication = $gestionnaireObs->modifierTransmissionObservation($idObs, false);
-
 
168
		if ($depublication === false) {
-
 
169
			$msg = "Un problème est survenu (voir log). Les observations n'ont pas pu être dépubliées.";
-
 
170
			$this->envoyerMessageErreur(304, $msg);
-
 
171
		}
-
 
172
	}
-
 
173
 
-
 
174
	/**
-
 
175
	 * Modifie une observation aveec les infos d'une proposition :
-
 
176
	 * Nous complétons les données de la proposition acceptée ici car:
-
 
177
	 * 1) la table tb_del.del_commentaire ne contient pas toutes les informations nécessaires
-
 
178
	 * 2) la table tb_del.del_commentaire ne *devrait* pas contenir beaucoup plus que nom_sel et nom_sel_nn
-
 
179
	 * 3) la génération de ces données ici, au moment de l'UPDATE, est le meilleur garant de leur fiabilité
-
 
180
	 */
-
 
181
	private function accepterPropositionDEL($idObs) {
-
 
182
		$gestion_observation = new GestionObservation($this->config);
-
 
183
		$donnees = array_map('trim', $this->donnees);
-
 
184
		$idUtilisateur = $donnees['id_utilisateur'];
-
 
185
		$donneesAModifier = array(
-
 
186
			'certitude' => 'Certaine',
-
 
187
			'nom_referentiel' => $donnees['nom_referentiel'],
-
 
188
		);
-
 
189
		// TODO : la récupération des infos du nom est aussi effectué par la suite voir ce qu'il faut garder
-
 
190
		$infosNoms = $this->getNomInfos($donnees['nom_sel_nn'], $donnees['nom_referentiel']);
-
 
191
		if ($infosNoms) {
-
 
192
			$donneesAModifier = array_merge($donneesAModifier, $infosNoms);
-
 
193
		}
-
 
194
 
-
 
195
		$modification = $gestion_observation->modifierObservationPublique($idObs, $donneesAModifier);
-
 
196
 
-
 
197
		if ($modification) {
-
 
198
			// supression des éventuelles liaison de l'obs avec le mot clé contenu dans obsKeywordDelete
-
 
199
			$gestionMotsClesObs = new GestionMotsClesChemin($this->config, 'obs');
-
 
200
			$supp_liaison_mot_cle = $gestionMotsClesObs->supprimerLiaisonPourMotCleEtIdElementLie('aDeterminer', $idObs, $idUtilisateur);
-
 
201
		} else {
-
 
202
			$msg = "Impossible de modifier l'observation associée à cet identifiant. Erreur mysql : " . mysql_error();
-
 
203
			$this->envoyerMessageErreur(500, $msg);// Internal Server Error
-
 
204
		}
-
 
205
	}
-
 
206
 
-
 
207
	// TODO : cette méthode et celles qui en dépendent sont peut être inutiles au vue de la méthode traiterEspece() de GestionObservation
-
 
208
	private function getNomInfos($id_nom, $code_referentiel) {
-
 
209
		$retour = false;
-
 
210
		if ($id_nom && $code_referentiel) {
-
 
211
			switch ($code_referentiel) {
-
 
212
				case 'bdtfx' :
-
 
213
					$retour = $this->getInfosBdtfx($id_nom);
-
 
214
					break;
-
 
215
				case 'bdtxa' :
-
 
216
					$retour = $this->getInfosBdtxa($id_nom);
-
 
217
					break;
-
 
218
				case 'isfan' :
-
 
219
					$retour = $this->getInfosIsfan($id_nom);
-
 
220
					break;
-
 
221
				case 'apd' :
-
 
222
					$retour = $this->getInfosApd($id_nom);
-
 
223
					break;
-
 
224
			}
-
 
225
		}
-
 
226
		return $retour;
-
 
227
	}
-
 
228
 
-
 
229
	private function getInfosBdtfx($id_nom) {
-
 
230
		$idNomP = CEL::db()->proteger($id_nom);
-
 
231
		$requete = "SELECT o.num_nom_retenu AS nom_ret_nn, o.num_taxonomique AS nt, o.famille, ".
-
 
232
			"	CONCAT(o.nom_sci, ' ', o.auteur) AS nom_sel, ".
-
 
233
			"	CONCAT(ret.nom_sci, ' ', ret.auteur) AS nom_ret ".
-
 
234
			"FROM {$this->bdtfx} AS o ".
-
 
235
			"	LEFT JOIN {$this->bdtfx} AS ret ON (o.num_nom_retenu != 0 AND o.num_nom_retenu = ret.num_nom) ".
-
 
236
			"WHERE o.num_nom = $idNomP ".
-
 
237
			' -- '.__FILE__.' : '.__LINE__;
-
 
238
		$resultat = Cel::db()->requeterLigne($requete);
-
 
239
		return $resultat;
-
 
240
	}
-
 
241
 
-
 
242
	private function getInfosBdtxa($id_nom) {
-
 
243
		$idNomP = CEL::db()->proteger($id_nom);
-
 
244
		// Champ "num_tax" au lieu de "num_taxonomique"
-
 
245
		$requete = "SELECT o.num_nom_retenu AS nom_ret_nn, o.num_tax AS nt, o.famille, ".
-
 
246
			"	CONCAT(o.nom_sci, ' ', o.auteur) AS nom_sel, ".
-
 
247
			"	CONCAT(ret.nom_sci, ' ', ret.auteur) AS nom_ret ".
-
 
248
			"FROM {$this->bdtxa} AS o ".
-
 
249
			"	LEFT JOIN {$this->bdtxa} AS ret ON (o.num_nom_retenu != 0 AND o.num_nom_retenu = ret.num_nom) ".
-
 
250
			"WHERE o.num_nom = $idNomP ".
-
 
251
			' -- '.__FILE__.' : '.__LINE__;
-
 
252
		$resultat = Cel::db()->requeterLigne($requete);
-
 
253
		return $resultat;
-
 
254
	}
-
 
255
 
-
 
256
	private function getInfosIsfan($id_nom) {
-
 
257
		$idNomP = CEL::db()->proteger($id_nom);
-
 
258
		// Champ "num_tax" au lieu de "num_taxonomique"
-
 
259
		$requete = "SELECT o.num_nom_retenu AS nom_ret_nn, o.num_taxonomique AS nt, o.famille, ".
-
 
260
			"	CONCAT(o.nom_sci, ' ', o.auteur) AS nom_sel, ".
-
 
261
			"	CONCAT(ret.nom_sci, ' ', ret.auteur) AS nom_ret ".
-
 
262
			"FROM {$this->isfan} AS o ".
-
 
263
			"	LEFT JOIN {$this->isfan} AS ret ON (o.num_nom_retenu != 0 AND o.num_nom_retenu = ret.num_nom) ".
-
 
264
			"WHERE o.num_nom = $idNomP ".
-
 
265
			' -- '.__FILE__.' : '.__LINE__;
-
 
266
		$resultat = Cel::db()->requeterLigne($requete);
-
 
267
		return $resultat;
-
 
268
	}
-
 
269
 
-
 
270
	private function getInfosApd($id_nom) {
-
 
271
		$idNomP = CEL::db()->proteger($id_nom);
-
 
272
		// Champ "num_tax" au lieu de "num_taxonomique"
-
 
273
		$requete = "SELECT o.num_nom_retenu AS nom_ret_nn, o.num_taxonomique AS nt, o.famille, ".
-
 
274
			"	CONCAT(o.nom_sci, ' ', o.auteur) AS nom_sel, ".
-
 
275
			"	CONCAT(ret.nom_sci, ' ', ret.auteur) AS nom_ret ".
-
 
276
			"FROM {$this->apd} AS o ".
-
 
277
			"	LEFT JOIN {$this->apd} AS ret ON (o.num_nom_retenu != 0 AND o.num_nom_retenu = ret.num_nom) ".
-
 
278
			"WHERE o.num_nom = $idNomP ".
-
 
279
			' -- '.__FILE__.' : '.__LINE__;
-
 
280
		$resultat = Cel::db()->requeterLigne($requete);
-
 
281
		return $resultat;
-
 
282
	}
99
}
283
}
100
284