From a231011372eb040194f56b359debfb3f9eb9f477 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 30 Apr 2016 16:50:46 +0200 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20de=20la=20cr=C3=A9ation?= =?UTF-8?q?=20de=20sujet=20finie.=20+=20Cr=C3=A9ation=20de=20sujet=20fonct?= =?UTF-8?q?ionnelle=20depuis=20`input/phone`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/modules.json | 4 +- js/lib/api-min.js | 4 +- js/lib/api.js | 2 +- manager/Database.php | 2 +- manager/ModuleResponse.php | 2 +- manager/module/input.php | 4 -- manager/repo/subject.php | 81 +++++++++++++++----------------------- view/js/input.js | 2 +- 8 files changed, 39 insertions(+), 62 deletions(-) diff --git a/config/modules.json b/config/modules.json index 3b16e01..2777f4a 100755 --- a/config/modules.json +++ b/config/modules.json @@ -99,7 +99,7 @@ "description": "Recupere le contenu d'un fichier XML de journal d'appel s'il a été importé au préalable.", "permissions": [], "parameters": { - "phone_number": { "description": "Numéro de téléphone de l'interrogé.", "type": "phone_number" } + "phone_number": { "description": "Numéro de téléphone de l'interrogé.", "type": "number" } } } }, @@ -189,7 +189,7 @@ "phone": { "description": "Enregistre les données d'une enquête téléphonique.", - "permissions": [], + "permissions": ["admin"], "parameters": { "subject": { "description": "Données sur le sujet de l'enquête.", "type": "json" }, "contacts": { "description": "Données des contacts de l'enquête.", "type": "json" }, diff --git a/js/lib/api-min.js b/js/lib/api-min.js index 4516cca..e116455 100644 --- a/js/lib/api-min.js +++ b/js/lib/api-min.js @@ -1,3 +1,3 @@ function APIClass(b){this.target=b} -APIClass.prototype={xhr:[],send:function(b,d,g){b.hasOwnProperty("path")||d({ModuleError:4});for(var a=0;a "+e.xhr[a].responseText), --1<[0,200].indexOf(e.xhr[a].status))try{d(JSON.parse(e.xhr[a].responseText))}catch(b){d({ModuleError:1})}else d({ModuleError:3})};var f=new FormData,c;for(c in b)"path"!=c?f.append(c,JSON.stringify(b[c])):f.append(c,b[c]);this.xhr[a].open("POST",this.target,!0);null!=g&&this.xhr[a].setRequestHeader("Authorization","Digest "+g);this.xhr[a].send(f)}}; +APIClass.prototype={xhr:[],send:function(b,d,g){b.hasOwnProperty("path")||d({ModuleError:4});for(var a=0;a '+ptrAPI.xhr[i].responseText); + // console.log('http://host/api/ => '+ptrAPI.xhr[i].responseText); // console.log( JSON.parse(ptrAPI.xhr[i].responseText) ); /* si success de requête */ diff --git a/manager/Database.php b/manager/Database.php index b715d6f..74b921f 100755 --- a/manager/Database.php +++ b/manager/Database.php @@ -247,7 +247,7 @@ break; // Numéro de téléphone - case 'phone_number': + case 'number': return $checker && is_string($value) && preg_match('/^(?:0|\+33 ?|0?0?33 ?|)([1-9] ?(?:[0-9] ?){8})$/i', $value); break; diff --git a/manager/ModuleResponse.php b/manager/ModuleResponse.php index fc5648b..f908d01 100755 --- a/manager/ModuleResponse.php +++ b/manager/ModuleResponse.php @@ -139,7 +139,7 @@ // On rajoute l'erreur au message $returnData = array_merge( array( - 'ModuleError' => $this->error, + 'ModuleError' => $this->error, 'ErrorDescription' => ManagerError::explicit($this->error) ), $this->data diff --git a/manager/module/input.php b/manager/module/input.php index 66537c3..869f734 100644 --- a/manager/module/input.php +++ b/manager/module/input.php @@ -25,9 +25,6 @@ extract($params); - return array('ModulError' => 100); - - /* [1] On crée le sujet de l'enquête =========================================================*/ /* (1) On rédige la requête */ @@ -35,7 +32,6 @@ 'username' => $subject['username'], 'firstname' => $subject['firstname'], 'lastname' => $subject['lastname'], - 'id_facebook' => null, 'number' => $subject['number'] )); diff --git a/manager/repo/subject.php b/manager/repo/subject.php index fe732b9..61fa77d 100644 --- a/manager/repo/subject.php +++ b/manager/repo/subject.php @@ -90,77 +90,58 @@ $checkInput = $checkInput && Database::check('varchar(0,30)', $lastname); $checkInput = $checkInput && !!strlen($username.$firstname.$lastname); // Pseudo, prénom, ou nom, au moins un n'est pas vide $checkInput = $checkInput && ( Database::check('id', $id_facebook) || is_null($id_facebook) ); - $checkInput = $checkInput && ( Database::check('number', $number) || is_null($number) ); + $checkInput = $checkInput && ( Database::check('number', $number) || is_null($number) ); // Si erreur en entree, on retourne FAUX if( !$checkInput ) return false; /* [1] On écrit la requête =========================================================*/ - $request_create = 'INSERT INTO sujets(idSujet, pseudo, nom, prenom, id_facebook, telephone) '; - $request_create .= 'VALUES ('; - $request_create .= 'DEFAULT, '; // idPersone - $request_create .= strlen($username) ? ':pseudo, ' : 'NULL, '; // pseudo - $request_create .= strlen($lastname) ? ':nom, ' : 'NULL, '; // nom - $request_create .= strlen($firstname) ? ':prenom, ' : 'NULL, '; // prenom - $request_create .= !is_null($id_facebook) ? ':id_facebook, ' : 'NULL, '; // Id facebook - $request_create .= !is_null($number) ? ':number, ' : 'NULL, '; // Numéro de tél - $request_create .= ')'; + $request_create_string = 'INSERT INTO sujets(idSujet, pseudo, nom, prenom, id_facebook, telephone) '; + $request_create_string .= 'VALUES ('; + $request_create_string .= 'DEFAULT, '; // idPersone + $request_create_string .= strlen($username) ? ':pseudo, ' : 'NULL, '; // pseudo + $request_create_string .= strlen($lastname) ? ':nom, ' : 'NULL, '; // nom + $request_create_string .= strlen($firstname) ? ':prenom, ' : 'NULL, '; // prenom + $request_create_string .= !is_null($id_facebook) ? ':id_facebook, ' : 'NULL, '; // Id facebook + $request_create_string .= !is_null($number) ? ':number, ' : 'NULL, '; // Numéro de tél + // On retire la dernière virgule + $request_create_string = preg_replace('/, ?$/', '', $request_create_string); + $request_create_string .= ')'; - /* (1) Si id_facebook NULL */ - if( $reference == null ){ + // On crée la requête + $request_create = Database::getPDO()->prepare($request_create_string); - // TODO: Finir l'implémentation - $create = Database::getPDO()->prepare("INSERT INTO subjects(id_subject, login, password, mail, reference, permission) - VALUES(DEFAULT, :login, :password, :mail, NULL, :permission)"); - $create->execute(array( - ':login' => $login, - ':password' => $password, - ':mail' => $mail, - ':permission' => $permission - )); - /* (2) Si reference est defini */ - }else{ + /* [2] On exécute la requête avec les valeurs + =========================================================*/ + /* (1) On ajoute les paramètres à ajouter */ + $variables = array(); + if( strlen($username) ) $variables[':pseudo'] = $username; + if( strlen($lastname) ) $variables[':nom'] = $lastname; + if( strlen($firstname) ) $variables[':prenom'] = $firstname; + if( !is_null($id_facebook) ) $variables[':id_facebook'] = $id_facebook; + if( !is_null($number) ) $variables[':number'] = $number; - $create = Database::getPDO()->prepare("INSERT INTO subjects(id_subject, login, password, mail, reference, permission) - VALUES(DEFAULT, :login, :password, :mail, :reference, :permission)"); - $create->execute(array( - ':login' => $login, - ':password' => $password, - ':mail' => $mail, - ':reference' => (int) $reference, - ':permission' => $permission - )); + /* (2) On exécute la requête avec leurs variables utiles uniquement */ + $status_created = $request_create->execute($variables); - } /* [3] Verification de la creation + recuperation id =========================================================*/ - $checkCreate = Database::getPDO()->prepare("SELECT id_subject - FROM subjects - WHERE login = :login - AND password = :password - AND mail = :mail - AND ( reference = :reference OR reference is NULL ) - AND permission = :permission"); - $checkCreate->execute(array( - ':login' => $login, - ':password' => $password, - ':mail' => $mail, - ':reference' => (int) $reference, - ':permission' => $permission - )); + // Si erreur de création + if( $status_created === false ) return false; + // On recupere l'id du sujet - $id_subject = $checkCreate->fetch(); + $id_subject = Database::getPDO()->lastInsertId(); // Si erreur, on retourne FALSE - if( $id_subject === false ) return false; + if( $id_subject == null ) return false; /* [4] Gestion du retour =========================================================*/ - return $id_subject['id_subject']; + return $id_subject; } diff --git a/view/js/input.js b/view/js/input.js index 6d9c2ad..cb1d683 100644 --- a/view/js/input.js +++ b/view/js/input.js @@ -309,7 +309,7 @@ include('/js/includes/input-phone-fiche.js', function(){ -}) }) }) }); +}); }); }); });