Subversion Repositories eFlore/Applications.cel

Rev

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

<?php
// declare(encoding='UTF-8');
/**
 * Service CRUD sur une observation.
 *
 * @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 Inventory extends Cel {

        public function getRessource() {

        }

        public function getElement($uid){
                // Controle detournement utilisateur
                $this->controleUtilisateur($uid[0]);
                if (!isset($uid[0])) {
                        return;
                }

                $parametres_recherche = array(array('ce_utilisateur',$uid[0]),array('ordre',$uid[1]));

                $chercheur_observations = new RechercheObservation($this->config);
                $retour_recherche = $chercheur_observations->rechercherObservations($uid[0], $parametres_recherche, 0, 1)->get();

                $observation = array();
                if (is_array($retour_recherche) && count($retour_recherche) > 0) {
                        $observation = $retour_recherche[0];
                }
                $observation = $this->formaterObservationVersTableauSequentiel($observation);
                $sortie = json_encode($observation);
                $this->envoyerJson($sortie);
                return true;
        }

        private function formaterObservationVersTableauSequentiel(&$observation) {
                if ($observation['date_observation']!="0000-00-00 00:00:00") {
                        list($year,$month,$day)= explode('-',$observation['date_observation']);
                        list($day)= explode(' ',$day);
                        $observation['date_observation']=$day."/".$month."/".$year;
                }

                $observation = array($observation['nom_sel'], $observation['nom_sel_nn'],
                                        $observation['nom_ret'], $observation['nom_ret_nn'],
                                        $observation['nt'], $observation['famille'], $observation['zone_geo'],
                                        $observation['ce_zone_geo'], $observation['ordre'],
                                        $observation['date_observation'], $observation['lieudit'],
                                        $observation['station'], $observation['milieu'],
                                        $observation['commentaire'], $observation['latitude'],
                                        $observation['longitude']);
                return $observation;
        }

        public function updateElement($uid,$pairs) {
                // Controle detournement utilisateur
                $this->controleUtilisateur($uid[0]);

                if (!isset($uid[1])) {
                        //TODO: envoyer un message d'erreur
                        return;
                }

                $uid[1] = rtrim($uid[1],',');

                $champs_etendus = isset($pairs['obs_etendue']) ? json_decode(stripslashes($pairs['obs_etendue']),true) : null;
                unset($pairs['obs_etendue']);

                if (isset($pairs['date_observation']) && !empty($pairs['date_observation'])) {
                        $time = strtotime(str_replace("/", "-", $pairs['date_observation']));
                        if(!$time || $time > time()) {
                                throw new Exception('Date invalide');
                        }
                }

                $gestionnaire_observation = new GestionObservation($this->config);
                $modification_observation = $gestionnaire_observation->modifierObservation($uid[0],$uid[1],$pairs);

                if ($champs_etendus != null) {
                        $gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
                        $id_obs = $gestionnaire_observation->renvoyerIdPourOrdre($uid[0],$uid[1]);
                        foreach($champs_etendus as &$champ_etendu) {
                                $objet_champ_etendu = new ChampEtendu();
                                $objet_champ_etendu->id = $id_obs;
                                if($this->doitGenererCleChampEtendu($champ_etendu)) {
                                        $champ_etendu['cle'] = $gestion_champs_etendus->transformerLabelEnCle($champ_etendu['label']);
                                }
                                $objet_champ_etendu->cle = $champ_etendu['cle'];
                                $objet_champ_etendu->valeur = $champ_etendu['valeur'];
                                $champ_etendu = $objet_champ_etendu;
                        }
                        // ajouterParLots modifie les champs et ajoute ceux qui existent déjà
                        $champs_supp = $gestion_champs_etendus->ajouterParLots($champs_etendus);
                }
                return true;
        }

        public function createElement($pairs) {
                // Controle detournement utilisateur
                $this->controleUtilisateur($pairs['ce_utilisateur']);

                $champs_etendus = isset($pairs['obs_etendue']) ? json_decode(stripslashes($pairs['obs_etendue']),true) : null;
                unset($pairs['obs_etendue']);

                if (isset($pairs['date_observation']) && !empty($pairs['date_observation'])) {
                        $time = strtotime(str_replace("/", "-", $pairs['date_observation']));
                        if(!$time || $time > time()) {
                                throw new Exception('Date invalide');
                        }
                }

                $gestionnaire_observation = new GestionObservation($this->config);
                $id_nouvelle_obs = $gestionnaire_observation->ajouterObservation($pairs['ce_utilisateur'], $pairs);

                if ($champs_etendus != null) {
                        $gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
                        foreach($champs_etendus as &$champ_etendu) {
                                $objet_champ_etendu = new ChampEtendu();
                                $objet_champ_etendu->id = $id_nouvelle_obs;
                                if($this->doitGenererCleChampEtendu($champ_etendu)) {
                                        $champ_etendu['cle'] = $gestion_champs_etendus->transformerLabelEnCle($champ_etendu['label']);
                                }
                                $objet_champ_etendu->cle = $champ_etendu['cle'];
                                $objet_champ_etendu->valeur = $champ_etendu['valeur'];
                                $champ_etendu = $objet_champ_etendu;
                        }
                        $champs_supp = $gestion_champs_etendus->ajouterParLots($champs_etendus);
                }
                return true;
        }

        /**
        * Supprime une observation
        *
        * uid[0] : utilisateur obligatoire
        * uid[1] : ordres de l'observation à supprimer
        */
        public function deleteElement($uid){
                // Controle detournement utilisateur
                $this->controleUtilisateur($uid[0]);

                if (!isset($uid[1])) {
                        //TODO: envoyer un message d'erreur
                        return;
                }

                $uid[1] = rtrim($uid[1],',');

                $gestionnaire_observation = new GestionObservation($this->config);
                $id_obs = $gestionnaire_observation->renvoyerIdPourOrdre($uid[0],$uid[1]);
                $suppression_observation = $gestionnaire_observation->supprimerObservation($uid[0],$uid[1]);

                $gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
                $champs_supp = $gestion_champs_etendus->vider($id_obs);
                
                $gestionnaire_images = new GestionImage($this->config);
                $gestionnaire_images->modifierTransmissionParObs($id_obs, false);

                if ($suppression_observation) {
                        echo "OK";
                }
                exit();
        }

        private function doitGenererCleChampEtendu($champ_etendu) {
                return !isset($champ_etendu['cle']) ||
                        trim($champ_etendu['cle'] == "" ||
                        strpos($champ_etendu['cle'],'tempid_') !== false);
        }
}