From b145b13dea075d758fcb84a68220bd02b7c5a4d7 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 14 Dec 2015 08:35:20 +0100 Subject: [PATCH] =?UTF-8?q?Preparation=20=C3=A0=20l'AJAXisation=20des=20co?= =?UTF-8?q?mmunications?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Consultations.php | 3 +- Dashboard.php | 3 +- Medecins.php | 3 +- Patients.php | 3 +- js/lib/API.js | 77 ++++++++++++++++++++++++++++++++++++++ js/{ => lib}/adjust.js | 0 js/medecins.js | 5 ++- managers/Manager.class.php | 2 +- 8 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 js/lib/API.js rename js/{ => lib}/adjust.js (100%) diff --git a/Consultations.php b/Consultations.php index 3553add..ebac30d 100755 --- a/Consultations.php +++ b/Consultations.php @@ -23,7 +23,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null; - + + diff --git a/Dashboard.php b/Dashboard.php index 4eb7d67..8144dd3 100755 --- a/Dashboard.php +++ b/Dashboard.php @@ -18,7 +18,8 @@ if(!Authentification::checkUser(0)){ - + + diff --git a/Medecins.php b/Medecins.php index 4a0bd2b..faa031d 100755 --- a/Medecins.php +++ b/Medecins.php @@ -24,7 +24,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null; - + + diff --git a/Patients.php b/Patients.php index d2f342b..7832167 100755 --- a/Patients.php +++ b/Patients.php @@ -24,7 +24,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null; - + + diff --git a/js/lib/API.js b/js/lib/API.js new file mode 100644 index 0000000..12beffe --- /dev/null +++ b/js/lib/API.js @@ -0,0 +1,77 @@ +/* classe API */ +function APIClass(){}; + +APIClass.prototype = { + xhr: [], // tableau d'objets pour les requêtes ajax + + + + /* transaction avec le serveur (managers) + * + * @param pRequest l'objet passé en JSON à API.php + * @param pHandler fonction qui s'éxécutera lors de la réponse (1 argument -> réponse) + * + * @return answer l'objet retourné par API.php via pHandler (1er argument) + * + *************************************************************************************************** + * + * @usecase + * 1. var answerObject = sendRequest( + * 2. { var1: "exemple", var2: 198294 }, + * 3. function(rep){ alert(rep); } + * 4. ); + * @explain + * 1. on appelle la fonction <=> on créé la requête + * 2. on passe l'objet qui sera envoyé + * 3. on passe une fonction qui utilise un argument (sera la réponse de API.php) (sous forme d'objet) + * + */ + send: function(pRequest, pHandler){ + + // on efface les requêtes qui sont terminées (toutes celles de this.xhr) + for( var i = 0 ; i < this.xhr.length ; i++ ){ + if( this.xhr[i].readyState == 4 ) // si terminée + this.xhr = this.xhr.slice(0,i-1).concat(this.xhr.slice(i,this.xhr.length-1)); // suppression entrée + } + + // on créé une nouvelle entrée + this.xhr.push(null); + i = this.xhr.length-1; + + // création de l'objet AJAX + if(window.XMLHttpRequest) // IE7+, Firefox, Chrome, Opera, Safari + this.xhr[i] = new XMLHttpRequest(); + else // IE5, IE6 + this.xhr[i] = new ActiveXObject('Microsoft.XMLHttpRequest'); + + console.log(pRequest); + + var ptrAPI = this; + this.xhr[i].onreadystatechange = function(){ + if( ptrAPI.xhr[i].readyState == 4 ){ // si la requête est terminée + + /* DEBUG : affiche la réponse BRUTE de API.php */ + console.log('managers/ => '+ptrAPI.xhr[i].responseText); + console.log( JSON.parse(ptrAPI.xhr[i].responseText) ); + + /* si success de requête */ + if( [0,200].indexOf(ptrAPI.xhr[i].status) > -1 ){ // si fichier existe et reçu + try{ pHandler( JSON.parse(ptrAPI.xhr[i].responseText) ); } // si on peut parser, on envoie + catch(e){ pHandler({status:'corrupted'}); } // sinon on envoie obj.status = 'corrupted' + } + /* sinon retourne obj.status = 'unreachable' */ + else + pHandler({status: 'unreachable'}); + + } + } + + // on créé un formulaire POST (virtuel) + var form = new FormData(); + form.append('json', JSON.stringify(pRequest) ); // on créé la variable $_POST['json']=>request + + this.xhr[i].open('POST', 'managers/', true); + this.xhr[i].send( form ); + + } +}; diff --git a/js/adjust.js b/js/lib/adjust.js similarity index 100% rename from js/adjust.js rename to js/lib/adjust.js diff --git a/js/medecins.js b/js/medecins.js index b2e280e..711feb6 100755 --- a/js/medecins.js +++ b/js/medecins.js @@ -66,6 +66,9 @@ sbCreer.addEventListener('click', function(e){ formElements[i].value = ''; // on vide }} - if( checker ) // si tout es ok uniquement, on submit() + if( checker ){ // si tout es ok uniquement, on submit() sbCreer.parentNode.submit(); + }else{ // sinon on affiche l'erreur + notif('error', 'Oups!', 'Certains champs sont requis ou incorrects.'); + } }, false); \ No newline at end of file diff --git a/managers/Manager.class.php b/managers/Manager.class.php index ba0a316..90466fd 100755 --- a/managers/Manager.class.php +++ b/managers/Manager.class.php @@ -50,7 +50,7 @@ class Manager{ public function dispatch($params){ if(($this->managerFound instanceof Authentification) or (Authentification::checkUser($this->commandFound['role'],$this->commandFound['strict']))){ if(method_exists($this->managerFound,$this->commandFound['method'])){ - $evalCommand = '$this->managerFound->'.$this->commandFound['method'].'($params)?>'; + $evalCommand = '$this->managerFound->'.$this->commandFound['method'].'($params); ?>'; eval($evalCommand); }else{ Response::quickResponse(500,json_encode("La méthode: ".$this->commandFound['method'].' n\'est pas présente dans le manager'));