Subversion Repositories Sites.obs-saisons.fr

Rev

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

<?php

class OdsStation extends OdsTriple {

        /**
     * Méthode appelée avec une requête de type GET.
     *
     */
    function getElement($param = array()) {
        
        if(isset($param[0]) && isset($param[1])) {
                $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','station_alt');
        $erreurs = array();
        
        foreach($elements_requis as $requis) {
                if(!isset($params[$requis])) {
                        //$erreurs[$requis] = 'erreur ';
                }
        }
        
        if(!empty($erreurs)) {
                $this->envoyer($erreurs);
        }
        
        $id_participant = $params['id_participant'];
        
        if(isset($params['station_code_insee']) && trim($params['station_code_insee']) != '') {
                
                $commune = $params['station_code_insee'];
                        if(strlen($commune) == 4) {
                                $commune = '0'.$commune;
                        }
        } else {
                $commune = $params['station_commune'];
        }
        
        $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->proteger($id_participant).','.
                                                        $this->proteger($params['station_nom']).','.
                                                        $this->proteger($commune).','.
                                                        $this->proteger($params['station_lat']).','.
                                                        $this->proteger($params['station_lon']).','.
                                                        $this->proteger($params['station_alt']).','.
                                                        $this->proteger($params['station_milieu']).','.
                                                        $this->proteger($params['station_description']).')';            
                                                        
                $creation_station = $this->executerRequeteSimple($requete_creation_station);
                
                if(!$creation_station) {
                $retour['erreurs'] = 'erreur d\'insertion';
        } else {
                $retour['id_nouvelle_station'] = $this->renvoyerDernierIdInsere();
        }
        
        $this->envoyer($retour);
        
    }
   
    /**
     * Méthode appelée pour mettre à jour un élément
     */
    public function updateElement($uid, $params)    {
        
            $elements_requis = array('id_participant','station_nom', 'station_commune', 'station_lat', 'station_lon','station_milieu','station_alt');
        $erreurs = array();
        
        foreach($elements_requis as $requis) {
                if(!isset($params[$requis])) {
                        $erreurs[$requis] = 'erreur ';
                }
        }
        
        if(!empty($erreurs)) {
                $this->envoyer($erreurs);
        }
        
        $id_participant = $params['id_participant'];
        
        if(!isset($uid[0])) {
                return;
        } else {
                $id_station = $uid[0];;
        }
        
        if(isset($params['station_code_insee']) && trim($params['station_code_insee']) != '') {
                
                $commune = $params['station_code_insee'];
                        if(strlen($commune) == 4) {
                                $commune = '0'.$commune;
                        }
        } else {
                $commune = $params['station_commune'];
        }
        
        $requete_modification_station = 'UPDATE ods_stations '.
                        'SET '.
                        'os_nom ='.$this->proteger($params['station_nom']).','.
                        'os_ce_commune ='.$this->proteger($commune).','.
                        'os_latitude ='.$this->proteger($params['station_lat']).','.
                        'os_longitude ='.$this->proteger($params['station_lon']).','.
                        'os_altitude ='.$this->proteger($params['station_alt']).','.
                        'os_ce_environnement ='.$this->proteger($params['station_milieu']).','.
                        'os_commentaire ='.$this->proteger($params['station_description']).' '.
                        'WHERE os_ce_participant = '.$this->proteger($id_participant).' '.
                'AND os_id_station = '.$this->proteger($id_station);
                                                        
                $modification_station = $this->executerRequeteSimple($requete_modification_station);
                
                $retour = array();
                
                if(!$modification_station) {
                $retour['erreurs'] = 'Erreur lors de la modification de la station';
        } else {
                $retour['reponse'] = 'OK';
        }

        $this->envoyer($retour);
    }
   
    /**
     * Méthode appelée pour supprimer un élément
     */
    public function deleteElement($uid) {
                
        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_participant = '.$this->proteger($id_participant).' '.
                'AND os_id_station = '.$this->proteger($id_station);
                
        $suppression_station = $this->executerRequeteSimple($requete_suppression_station);
        
        if(!$suppression_station) {
                // TODO: comment gère t'on les erreurs ?
        }
        
        $this->envoyer();
        
    }
    
        private function obtenirListeStationPourParticipant($id_participant) {
                
        $requete_liste_station = 'SELECT * FROM ods_stations WHERE os_ce_participant = '.$this->proteger($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->proteger($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) {
        
        if(is_numeric($station['os_ce_commune'])) {
                $commune = $this->obtenirInformationsCommuneParCodeInsee($station['os_ce_commune']);
        } else {
                $commune = $station['os_ce_commune'];
        }
        
        $station_champs_formates = array(
                        'id' => $station['os_id_station'],
                        'nom' => $station['os_nom'],
                                'code_insee' => $station['os_ce_commune'],
                        'commune' => $commune,
                                'id_milieu' => $station['os_ce_environnement'],
                        'milieu' => $this->obtenirInformationsMilieuParId($station['os_ce_environnement']),
                                'latitude' => $station['os_latitude'],
                                'longitude' => $station['os_longitude'],
                        'altitude' => $station['os_altitude'],
                                'description' => $station['os_commentaire']
                    );
                    
                return $station_champs_formates;
    }
    
        private function obtenirInformationsCommuneParCodeInsee($code_insee_commune) {
        
        $requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_code_insee = '.$this->proteger($code_insee_commune);
        $infos_commune = $this->executerRequete($requete_infos_commune);
        
                //TODO: en attendant de stocker les ids
                return $infos_commune[0]['oc_nom'];
                
        //return $infos_commune;
    }
    
    private function obtenirInformationsMilieuParId($id_milieu) {
        
        $informations_milieu = $this->obtenirValeurTripleParId($id_milieu);
        return $informations_milieu;
        
    }
}
?>