New file |
0,0 → 1,218 |
<?php |
// declare(encoding='UTF-8'); |
/** |
* Service générique de modification des observations. |
* |
* Cas d'utilisation : |
* GET /CelObs/[id] : oû id est l'identifiant d'une observation publique |
* POST /CelObs/[id] : oû id est l'identifiant d'une observation publique |
* si les data du POST contienent : |
* - 1 données : |
* - 'transmission' : publier ou dépublier une obs |
* - 3 données : |
* - 'id_observation', 'nom_sel_nn', 'nom_referentiel' : permet d'accepter une proposition (DEL) |
* |
* @internal Mininum PHP version : 5.2 |
* @category CEL |
* @package Services |
* @subpackage Observations |
* @version 0.1 |
* @author Mathias CHOUET <mathias@tela-botanica.org> |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org> |
* @author Aurelien PERONNET <aurelien@tela-botanica.org> |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt> |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt> |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org> |
*/ |
class CelObs extends Cel { |
private $rechercheObs = null; |
private $chpsEtendus = null; |
private $donnees = null; |
|
public function __construct($config) { |
parent::__construct($config); |
$this->rechercheObs = new RechercheObservationExport($config, array("standard"=>"0")); |
$this->chpsEtendus = new GestionChampsEtendus($config, 'obs'); |
|
$this->chargerNomsTablesReferentiels(); |
} |
|
private function chargerNomsTablesReferentiels() { |
// Créé des attributs avec le code du référentiel : bdtfx, bdtxa, apd, isfan |
foreach ( $this->config['referentiels'] as $referentiel => $table) { |
$this->$referentiel = $table; |
} |
} |
|
public function getElement($ressources){ |
$retour = false; |
$idObs = $ressources[0]; |
if (isset($idObs) && preg_match('/^[0-9]+$/', $idObs)) { |
|
$criteres = array('id_observation' => $idObs, 'transmission' => 1, 'standard' => 0); |
$obsTrouvee = $this->rechercheObs->rechercherObservations(null, $criteres, 0, 1)->get(); |
|
$observation = array(); |
if (is_array($obsTrouvee) && count($obsTrouvee) > 0) { |
$observation = $obsTrouvee[0]; |
} |
$observation = $this->preparerChamps($observation); |
$observation = $this->selectionnerChamps($observation); |
$observation = $this->formaterClePourJs($observation); |
|
$champsEtendus = $this->chpsEtendus->consulter($idObs); |
if (is_array($champsEtendus) && count($champsEtendus) > 0) { |
$champsEtendus = $this->preparerChampsEtendus($champsEtendus); |
$observation['extension'] = $champsEtendus; |
} |
|
$this->envoyerJson($observation); |
$retour = true; |
} |
return $retour; |
} |
|
private function preparerChamps($champs) { |
if (isset($champs['date_observation'])) { |
$date = explode(' ', $champs['date_observation']); |
$champs['date_observation'] = $date[0]; |
} |
return $champs; |
} |
|
private function selectionnerChamps($observation) { |
$champs = array('id_observation', 'nom_sel', 'nom_ret', 'nom_ret_nn', 'nt', 'famille', |
'nom_referentiel', 'ce_zone_geo', 'zone_geo', 'lieudit', 'station', 'milieu', 'latitude', 'longitude', |
'geodatum', 'date_observation', 'mots_cles_texte', 'commentaire', 'date_creation', 'date_modification', |
'date_transmission', 'code_insee_calcule', 'abondance', 'certitude', 'phenologie', 'altitude'); |
$selection = array(); |
foreach ($champs as $chp) { |
if (isset($observation[$chp])) { |
$selection[$chp] = $observation[$chp]; |
} |
} |
return $selection; |
} |
|
private function formaterClePourJs(Array $tableau) { |
$tableauJs = array(); |
foreach ($tableau as $cle => $valeur) { |
if ($cle == 'ce_zone_geo') { |
$cle = 'codeZoneGeo'; |
} else { |
$cle = str_replace(' ', '', ucwords(str_replace('_', ' ', $cle))); |
$cle{0} = strtolower($cle{0}); |
} |
$tableauJs[$cle] = $valeur; |
} |
return $tableauJs; |
} |
|
private function preparerChampsEtendus($champs) { |
$retour = array(); |
foreach ($champs as $chp) { |
$retour[$chp['cle']] = array('valeur' => $chp['valeur']); |
} |
return $retour; |
} |
|
/** |
* Méthode appelée avec une requête de type POST et un identifiant d'observation. |
* Modifie une observation en fonction des informations envoyées en POST. |
* Utilisé par: |
* - service:del:0.1/determinations/ : ValiderDetermination.php::modifierObservationParDetermination() |
* - service:del:0.1/observations/#idObs [POST] : pour dépublier une observation |
* |
* @param $uid array $uid[0] (int) : identifiant observation |
* @param pairs array tableau contenant les champs à modifier sous la forme : nom_du_champ=nouvelle_valeur |
*/ |
public function updateElement($ressources, $donnees) { |
$this->donnees = $donnees; |
if ($this->controlerAccessibiliteWs()) { |
if ($this->controleAppelIpAutorisee()) { |
$idObs = isset($ressources[0]) ? $ressources[0] : ''; |
$this->verifierIdentifiantObs($idObs); |
|
if (count($this->donnees) == 1) { |
$donneesObligatoires = array('transmission'); |
if ($this->verifierDonneesObligatoires($donneesObligatoires)) { |
$this->depublierObs($idObs); |
} |
} else if (count($this->donnees) == 3) { |
$donneesObligatoires = array('id_observation', 'nom_sel_nn', 'nom_referentiel'); |
if ($this->verifierDonneesObligatoires($donneesObligatoires)) { |
$this->accepterPropositionDEL($idObs); |
} |
} else { |
$msg = "La modification complète d'une observation n'est pas implémentée."; |
$this->envoyerMessageErreur(501, $msg); |
} |
|
$this->envoyer('ok'); |
} |
} |
return true; |
} |
|
private function verifierIdentifiantObs($chaine) { |
$ok = preg_match('/^[0-9]+$/', $chaine); |
if ($ok == false) { |
$msg = "Indiquer un seul identifiant numérique d'observation."; |
$this->envoyerMessageErreur(412, $msg); |
} |
return $ok; |
} |
|
private function verifierDonneesObligatoires($champsObligatoires) { |
foreach ($champsObligatoires as $param) { |
if (! isset($this->donnees[$param])) { |
$msg = sprintf("Paramètre %s manquant (parmi %s)", $param, implode(', ', $champsObligatoires)); |
$this->envoyerMessageErreur(412, $msg); |
} |
} |
return true; |
} |
|
private function depublierObs($idObs) { |
$gestionnaireObs = new GestionObservation($this->config); |
$depublication = $gestionnaireObs->modifierTransmissionObservation($idObs, false); |
if ($depublication === false) { |
$msg = "Un problème est survenu (voir log). L'observation « $idObs » n'a pas pu être dépubliée."; |
$this->envoyerMessageErreur(304, $msg); |
} |
} |
|
|
|
/** |
* Modifie une observation aveec les infos d'une proposition : |
* Nous complétons les données de la proposition acceptée ici car: |
* 1) la table tb_del.del_commentaire ne contient pas toutes les informations nécessaires |
* 2) la table tb_del.del_commentaire ne *devrait* pas contenir beaucoup plus que nom_sel et nom_sel_nn |
* 3) la génération de ces données ici, au moment de l'UPDATE, est le meilleur garant de leur fiabilité |
*/ |
private function accepterPropositionDEL($idObs) { |
$gestion_observation = new GestionObservation($this->config); |
$donnees = array_map('trim', $this->donnees); |
$donneesAModifier = array( |
'certitude' => 'certain', |
'nom_referentiel' => $donnees['nom_referentiel'], |
'nom_sel_nn' => $donnees['nom_sel_nn'], |
); |
|
$modification = $gestion_observation->modifierObservationPublique($idObs, $donneesAModifier); |
|
if ($modification) { |
// TODO: en modifiant bien la classe de gestion mots clés, on aurait peut être pas besoin de l'id |
// utilisateur (car l'id de l'obs est déjà sans ambiguité) |
$idUtilisateur = $this->rechercheObs->obtenirIdUtilisateurPourIdObs($idObs); |
|
// supression des éventuelles liaison de l'obs avec le mot clé contenu dans obsKeywordDelete |
$gestionMotsClesObs = new GestionMotsClesChemin($this->config, 'obs'); |
$supp_liaison_mot_cle = $gestionMotsClesObs->supprimerLiaisonPourMotCleEtIdElementLie('aDeterminer', $idObs, $idUtilisateur); |
} else { |
$msg = "Impossible de modifier l'observation associée à cet identifiant. Erreur mysql : " . mysql_error(); |
$this->envoyerMessageErreur(500, $msg);// Internal Server Error |
} |
} |
} |