Subversion Repositories Applications.dictionnaire

Rev

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

<?php
class Dictionnaire extends RestService {

//+----------------------------------------------------------------------------------------------------------------+    
// Consulter
        public function consulter($ressources, $parametres) {
        $entete_http = RestServeur::HTTP_CODE_CONTENU_REQUIS;
        $corps_http = null;
        
        if (isset($ressources[0])) {
                
                $methode = $ressources[0];
                
                switch($methode) {
                        
                        case 'mots':
                                $retour = $this->getMots();
                        break;

                        case 'zglossary':
                                $retour = $this->getMotsZglossary();
                        break;

                        case 'autoabbr':
                                $retour = $this->getMotsAutoAbbr();
                        break;
                        
                        case 'def':
                                if(isset($ressources[1])) {
                                                $retour = $this->getDefinition($ressources[1]);
                                        } else {
                                                $retour = $this->getToutesDefinitions();
                                        }
                                break;
                                case 'defs':
                                        $retour = $this->getToutesDefinitions();
                        break;
                        
                        default:
                                $retour = 'Le service requiert un nom de méthode';
                        break;
                }
                        
                        $entete_http = RestServeur::HTTP_CODE_OK;
                        $corps_http = json_encode($retour);     
                        
        } else {
                $entete_http = RestServeur::HTTP_CODE_CONTENU_REQUIS;
        }
        
        RestServeur::envoyerEnteteStatutHttp($entete_http);
        header('Content-type: application/json; charset=UTF-8');
        echo $corps_http; 
        exit;
    }
    
    private function getDefinition($mot) {
        $requete_selection_definition = 'SELECT valeur FROM definitions WHERE cle = "'. self::simplifier($mot).'"';   
        $definition = $this->bdd->recuperer($requete_selection_definition);
        
                return $definition; 
    }
    
    private function getToutesDefinitions() {
                return $this->bdd->recupererTous('SELECT valeur FROM definitions');
    }
    

    private function getMots() {
                $requete = 'SELECT TRIM(cle) as cle FROM definitions';  // certaines cles ont des espaces
                $assoc = $this->bdd->recupererTous($requete);

                array_walk($assoc, function(&$item) { $item = $item['cle']; });
                return $assoc;
    }

    private function getMotsZglossary() {
                $assoc = $this->bdd->recupererTous('SELECT cle as term, 0 as type, valeur as definition FROM definitions' .
                                                                                   ' WHERE valeur != ""');
                return $assoc;
    }

    private function getMotsAutoAbbr() {
                $assoc = $this->bdd->recupererTous("SELECT CONCAT(cle, '*') as cle FROM definitions WHERE valeur != ''");
                $assoc2 = Array();
                foreach($assoc as $v) {
                        $assoc2[$v['cle']] = true;
                }
                return $assoc2;
    }
    
    static function simplifier($chaine){
                return trim(strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $chaine)), " \t\n\r\0\x0b*");
        }
}
?>