Subversion Repositories eFlore/Applications.cel

Rev

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

<?php
// declare(encoding='UTF-8');
/**
 * Service permettant de récupérer toutes les informations d'une observation publique.
 * Encodage en entrée : utf8
 * Encodage en sortie : utf8
 *
 * Cas d'utilisation :
 * GET /CelObs/[id] : oû id est l'identifiant d'une observation publique
 *
 * @author Jean-Pascal MILCENT <jpm@clapas.org>
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 * @version $Id$
 * @copyright © 2013, Jean-Pascal MILCENT
 */
class CelObs extends Cel {

        private $rechercheObs = null;
        private $chpsEtendus = null;

        public function __construct($config) {
                parent::__construct($config);
                $this->rechercheObs = new RechercheObservation($config);
                $this->chpsEtendus = new GestionChampsEtendus($config, 'obs');
        }

        function getElement($ressources){
                $retour = false;
                $idObs = $ressources[0];
                if (isset($idObs) && preg_match('/^[0-9]+$/', $idObs)) {

                        $criteres = array('id_observation' => $idObs, 'transmission' => 1);
                        $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'], 'label' => $chp['label']);
                }
                return $retour;
        }
}