Subversion Repositories Sites.obs-saisons.fr

Rev

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

<?php

class OdsStation extends GestionTriple {

        /**
     * Méthode appelée avec une requête de type GET.
     *
     */
    function getElement($param = array()) {
        
        if(isset($param[0])) {
                $id_participant = $param[0];
        } else {
                return;
        }
       
                if($param[1] == "*") {
        
                        $info = $this->obtenirListeStationPourParticipant($id_participant);
                        
                } else if(is_numeric($param[1])) {
                        
                        $id_station = $param[1];
                        
                        $info = $this->obtenirInformationsStation($id_station);
                }
       
        // Envoi sur la sortie standard
        $this->envoyer($info);
    
    }
   
    /**
     * Méthode appelée pour ajouter un élément.
     */
    public function createElement($params) {
        
        $elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu');
        $erreurs = array();
        
        foreach($elements_requis as $requis) {
                if(!isset($params[$requis])) {
                        //$erreurs[$requis] = 'erreur ';
                }
        }
        
        if(!empty($erreurs)) {
                $this->envoyer($erreurs);
        }
        
        $station['alt'] = '0';
        
        $id_participant = $params['id_participant'];
        
        $requete_creation_station = 'INSERT INTO ods_stations '.
                                        '(os_ce_participant, os_nom, os_ce_commune, os_latitude, os_longitude, os_altitude, os_ce_environnement, os_commentaire) '.
                                        'VALUES ('.
                                                        $this->bdd->quote($id_participant).','.
                                                        $this->bdd->quote($params['station_nom']).','.
                                                        $this->bdd->quote($params['station_commune']).','.
                                                        $this->bdd->quote($params['station_lat']).','.
                                                        $this->bdd->quote($params['station_lon']).','.
                                                        $this->bdd->quote($params['station_alt']).','.
                                                        $this->bdd->quote($params['station_milieu']).','.
                                                        $this->bdd->quote($params['station_description']).')';          
                                                        
                $creation_station = $this->executerRequeteSimple($requete_creation_station);
                
                if(!$creation_station) {
                // TODO: comment gère t'on les erreurs ?
        }
        
        $this->envoyer();
        
    }
   
    /**
     * Méthode appelée pour mettre à jour un élément
     */
    public function updateElement($uid, $params)    {
        
            if(!isset($uid[0])) {
                $id_participant = $uid[0];
        } else {
                return;
        }
        
        if(!isset($uid[1])) {
                $id_station = $uid[1];
        } else {
                return;
        }
        
        $requete_modification_station = 'UPDATE ods_stations '.
                        'SET '.
                        'os_nom ='.$this->bdd->quote($params['station_nom']).','.
                'os_latitude ='.$this->bdd->quote($params['station_commune']).','.
                        'os_latitude ='.$this->bdd->quote($params['station_lat']).','.
                        'os_longitude ='.$this->bdd->quote($params['station_lon']).','.
                        'os_altitude ='.$this->bdd->quote($params['station_alt']).','.
                        'os_ce_environnement ='.$this->bdd->quote($params['station_milieu']).','.
                        'os_commentaire ='.$this->bdd->quote($params['station_description']).' '.
                        'WHERE os_ce_particant = '.$this->bdd->quote($id_participant).' '.
                'AND os_id_station = '.$this->bdd->quote($id_station);
                                                        
                $modification_station = $this->executerRequeteSimple($requete_creation_station);
                
                if(!$modification_station) {
                // TODO: comment gère t'on les erreurs ?
        }
        
        $this->envoyer();
    }
   
    /**
     * Méthode appelée pour supprimer un élément
     */
    public function deleteElement($uid) {
        
        // Pour le moment, pas de suppression des stations
        return ;
                
        if(!isset($uid[0])) {
                $id_participant = $uid[0];
        } else {
                return;
        }
        
        if(!isset($uid[1])) {
                $id_station = $uid[1];
        } else {
                return;
        }
        
        $requete_suppression_station = 'DELETE FROM ods_stations '.
        'WHERE os_ce_particant = '.$this->bdd->quote($id_participant).' '.
                'AND os_id_station = '.$this->bdd->quote($id_station);
        
        // TODO : supprimer également tout ce qui est associé à la station (observations, etc...)
        $suppression_station = $this->executerRequeteSimple($requete_suppression_station);
        
        if(!$suppression_station) {
                // TODO: comment gère t'on les erreurs ?
        }
        
        $this->envoyer();
        
    }
    
    
    // +---------------------------------------------------------------------------------------------------------------+
    // METHODES D'ACCES A LA BASE DE DONNEES
        private function obtenirListeStationPourParticipant($id_participant) {
                
        $requete_liste_station = 'SELECT * FROM ods_stations WHERE os_ce_participant = '.$this->bdd->quote($id_participant);

        $liste_station = $this->executerRequete($requete_liste_station);
        
        $liste_station_formatees = array();
                
        foreach($liste_station as $indice => $station) {
                        
                $station_champs_formates = $this->formaterChampsStationPourEnvoi($station);
                    $liste_station_formatees[$station['os_id_station']] = $station_champs_formates;
        }
        
        return $liste_station_formatees;
        }
    
    private function obtenirInformationsStation($id_station) {
        
        $requete_infos_station = 'SELECT * FROM ods_stations WHERE os_id_station = '.$this->bdd->quote($id_station);

        $infos_station = $this->executerRequete($requete_infos_station);
        
        $infos_station_formatees = array();
        
        if(!empty($infos_station)) {
                $infos_station = $infos_station[0];
                $infos_station_formatees = $this->formaterChampsStationPourEnvoi($infos_station);
        }
        
        return $infos_station_formatees;
    }
    
    private function formaterChampsStationPourEnvoi($station) {
        
        $station_champs_formates = array(
                        'id' => $station['os_id_station'],
                        'nom' => $station['os_nom'],
                                'id_commune' => $station['os_ce_commune'],
                        'commune' => 'Montpellier',//$this->obtenirInformationsCommuneParCodeInsee($station['os_ce_commune']),
                                'milieu' => 'Urbain',//$this->obtenirInformationsMilieuParAbreviation($station['os_milieu']),
                                'latitude' => $station['os_latitude'],
                                'longitude' => $station['os_longitude'],
                        'altitude' => $station['os_altitude'],
                                'description' => $station['os_commentaire']
                    );
                    
                return $station_champs_formates;
    }
    
    private function obtenirInformationsCommuneParId($id_commune) {
        
        $requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_id_commune = '.$this->bdd->quote($id_commune);
        $infos_commune = $this->executerRequete($requete_infos_commune);
        
        return $infos_commune;
    }
    
        private function obtenirInformationsCommuneParCodeInsee($code_insee_commune) {
        
        $requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_code_insee = '.$this->bdd->quote($code_insee_commune);
        $infos_commune = $this->executerRequete($requete_infos_commune);
        
        return $infos_commune;
    }
    
    private function obtenirInformationsMilieuParAbreviation($abreviation_milieu) {
        
        $informations_milieu = $this->obtenirValeurTripleParAbreviation($abreviation_milieu);
        return $informations_milieu;
        
    }
}
?>