From 0752ec35ef3aabc7577ff3b49596dfbbc8ce7be5 Mon Sep 17 00:00:00 2001 From: German Correa Date: Sat, 9 Dec 2017 12:12:50 -0300 Subject: [PATCH] Complete registration. - Fix db.php functions - Improve validations and status modal message on registerform.js IS NEEDED SOME MINOR GRAPHICAL MODIFICATIONS IN FRON-PAGE --- db/db.php | 41 ++++++++------ db/ggdworkshop.db | Bin 16384 -> 16384 bytes public/js/registerform.js | 105 ++++++++++++++++++++++-------------- src/routes.php | 59 +++++++++++--------- templates/registration.html | 25 +++++---- 5 files changed, 139 insertions(+), 91 deletions(-) diff --git a/db/db.php b/db/db.php index 7ff3b06..1d975f0 100644 --- a/db/db.php +++ b/db/db.php @@ -1,4 +1,4 @@ -pdo->prepare($sql); + + $stmt = $this->pdo->prepare($sql); $stmt->execute([ ':nombre' => $registro['nombre'], ':apellido' => $registro['apellido'], @@ -27,17 +29,17 @@ ':ciudad' => $registro['ciudad'], ':pais' => $registro['pais'], ':email' => $registro['email'], - ':fechaLlegada' => $registro['fechaLlegada'], - ':fechaPartida' => $registro['fechaPartida'], + ':fechaLlegada' => $registro['bda'], + ':fechaPartida' => $registro['eda'], ':financiacion' => $registro['financiacion'], - ':invitado' => $registro['invitado'], - ':cartaInvitacion' => $registro['cartaInvitacion'], - ':roomingPref' => $registro['roomingPref'], - ':roommate' => $registro['roommate'], - ':fechaRegistro' => $registro['fechaRegistro'] + ':invitado' => $registro['invited'], + ':cartaInvitacion' => $registro['letterinvited'], + ':roomingPref' => $registro['roomtype'], + ':roommate' => $registro['roomate'], + ':fechaRegistro' => $date = date('Y-m-d H:i:s'), ]); return $this->pdo->lastInsertId(); - + } public function getAll(){ $stmt = $this->pdo->prepare("SELECT * FROM Registro"); @@ -45,6 +47,13 @@ return $stmt->fetchAll(\PDO::FETCH_BOTH); } - } + public function findByMail($email){ + $stmt = $this->pdo->prepare("SELECT * FROM Registro WHERE email=:email"); + $stmt->bindParam(":email",$email); + $stmt->execute(); + return $result = $stmt->fetch(\PDO::FETCH_ASSOC); + + } + + } ?> - diff --git a/db/ggdworkshop.db b/db/ggdworkshop.db index ebf65569863332bf1b09234744251fb8d562e79b..daa8ecdf0e0630911c63c13c53b4c42854640ed5 100644 GIT binary patch delta 1104 zcmb`G&2G~`6oq5lw6^nOq7Y~ZaVJU(s!$Sp+$fDVgchlwHmwD;uw;lQuBF7TY$qZT zD9M5~pu7T`EUTzuPbF47MK` zY#R?=2`dMVV^{PhG8*}s)#Sk6b>0X3Mg&Evl#^giECXpxSzA{m`5k*9z3oXem!-S# z`pQM6DEEU5kxr#oGbt&PVHq|RkvCD(XjoOtMotZ>mg9maR&DS3OR#SmZEQP++o@Wn zi`#~Vt$n+tw@`n&m zv4KntJafE{LyjsQse!n3YeTh-HgfT_thlbou%DRs&?FLsdM3lnQuhYby*+p>EZ=}| z@LpKz{u_9a;DP+~JTQ<^w7nw)CRK*;3=Pg@@azQK=$Ys}IsUXUJ_2LlOSX1#420Iw2 zr^87Wgs-Tq75n$+Vu3qx&unSREb%&FwR;V(GgkkDQ&^%ESsHP|Z)%Z?o(1>~{vp4| zFYv?Y8UK_2#k>5cQ4IWuc9-cYrTYOQX(0(h`M0QH;OPuy>FukUCey&)rX}j)d5+`9 qgy}SO@yZr14}0oQgOf`0cj+>9^*;+H>C*YpETg&6uyAmb;{E`nkV9|) delta 63 zcmV-F0Kor%fB}Gj0gxL31d$v=0R*vNqz?)M4hjGdW)8v*3bP>~{0*}T9{K?a0}rVH V52+8#59hNHFq;pvsV`O#0RtYg5uyM9 diff --git a/public/js/registerform.js b/public/js/registerform.js index 59ba031..a072fc4 100644 --- a/public/js/registerform.js +++ b/public/js/registerform.js @@ -1,30 +1,57 @@ +function showMsg(error,msg){ + + var msgTxt = $("#msgModal .modal-body .alert"); + if(error){ + msgTxt.removeClass("alert-success"); + msgTxt.addClass("alert-danger"); + msgTxt.html('' + +'Error!
'+msg); + } + else{ + msgTxt.removeClass("alert-danger"); + msgTxt.addClass("alert-success"); + msgTxt.html('' + +'Success!
'+msg); + } + $("#msgModal").modal({show:true}) +} $().ready(function(){ $("#statusmsg").hide(); - $("#txtbda,#txteda").datepicker({ dateFormat: "dd/mm/yy", minDate:new Date(2018,6,23), maxDate:new Date(2018,6,27) }); - + $("#pais").select2({ data: data.paises, placeholder: "Select Country", width: "100%", + });/*.one('select2-focus', select2Focus).on("select2-blur", function () { + $(this).one('select2-focus', select2Focus) }); - + +function select2Focus() { + var select2 = $(this).data('select2'); + setTimeout(function() { + if (!select2.opened()) { + select2.open(); + } + }, 0); + }*/ + $("#roomtype").select2({ data: data.roomtypes, placeholder: "Select Rooming Preference", width:"100%", minimumResultsForSearch: -1, dropdownCssClass: "bigdrop", - + }); - + $('#registerform').on('submit',function(e) { e.preventDefault(); @@ -47,6 +74,7 @@ $().ready(function(){ invited:{required:true}, financiacion:"required", letterinvited:"required", + "g-recaptcha-response":"required" }, @@ -62,26 +90,38 @@ $().ready(function(){ email: "Please enter a valid e-mail address", invited: "Select one option please", financiacion: "Select one option please", - letterinvited: "Select one option please" + letterinvited: "Select one option please", + 'g-recaptcha-response': "Please validate reCAPTCHA", + - }, submitHandler: function(form){ var formdata = $(form).serializeArray(); formdata.find(item => item.name === "pais").value = $("#pais").select2("data")[0].text; formdata.find(item => item.name === "roomtype").value = $("#roomtype").select2("data")[0].text; var parameters = $.param(formdata); - console.log(parameters); + var post = $.post('api/register', parameters); post.done(function(data){ - console.log(data); - }); - //console.log(parameters); + if(data.success){ - }, + showMsg(false,data.msg); + } + else{ + var errMsg = data.msg+"
    "; + $.each(data.errors, function(k,v){ + errMsg+="
  • "+v+"
  • "; + }); + errMsg+="
"; + console.log(errMsg); + showMsg(true,errMsg); + } + }); + + }, errorElement: "em", errorPlacement: function ( error, element ) { - // Add the `help-block` class to the error element + // Add the `help-block` class to the error element error.addClass("help-block"); var esradio = $(element).prop("type") == "radio"; var padre = element.parents(".input-container"); @@ -89,20 +129,20 @@ $().ready(function(){ padre.append(error); var iconstatus = $(""); if(!$.contains(padre,"span.glyhpicon")){ - error.before(iconstatus); + error.before(iconstatus); } if(esradio){ error.css("display", "inline"); $(iconstatus).css("display", "inline"); $(iconstatus).css("position", "initial"); - } + } }, success: function(label,element){ var esradio = $(element).prop("type") == "radio"; var padre = $(element).parents(".input-container"); - var iconstatus = padre.children("span.glyphicon"); + var iconstatus = padre.children("span.glyphicon"); if(!iconstatus){ - label.before(""); + label.before(""); } }, highlight: function ( element, errorClass, validClass ) { @@ -110,32 +150,19 @@ $().ready(function(){ var iconstatus = padre.children("span.glyphicon"); padre.addClass("has-error").removeClass("has-success"); iconstatus.addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" ); - padre.addClass( "has-error" ).removeClass( "has-success" ); + padre.addClass( "has-error" ).removeClass( "has-success" ); }, unhighlight: function (element, errorClass, validClass) { - var padre = $(element).parents(".input-container"); + var padre = $(element).parents(".input-container"); var iconstatus = padre.children("span.glyphicon"); - padre.addClass("has-success").removeClass("has-error"); + padre.addClass("has-success").removeClass("has-error"); iconstatus.addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" ); - padre.addClass( "has-success" ).removeClass( "has-error" ); - } - }); + padre.addClass( "has-success" ).removeClass( "has-error" ); + }, + //}); + + }); + - var finan; - $('input:radio[name="financiacion"]').change(function(){ - finan=true; - if($(this).is(':checked') && $(this).val() == "si"){ - $("#detallefinan").show(); - $("#detallefinan textarea").addClass("has-error"); - } - else - $("#detallefinan").hide(); - - $('#sifinan').tooltip("hide"); - $('#sifinan').tooltip("destroy"); - }); - - }); - diff --git a/src/routes.php b/src/routes.php index a2dc701..ac4c334 100644 --- a/src/routes.php +++ b/src/routes.php @@ -62,24 +62,24 @@ $app->group('/api', function($app){ }); $app->post('/register', function (Request $request, Response $response, array $args) { - // Sample log message - //$this->logger->debug(var_dump($request->getParsedBody())); $db = new DB($this->db); $data = $request->getParsedBody(); $messages = $this->flash; - if(!isset($data['nombre'])) - $messages->addMessageNow("submit-register-err", "First Name could not be empty"); - if(!isset($data['apellido'])) - $messages->addMessageNow("submit-register-err", "Last Name could not be empty"); - if(!isset($data['email'])) + if(!isset($data['nombre'])||empty($data['nombre'])) + $messages->addMessageNow("submit-register-err", "First name could not be empty"); + if(!isset($data['apellido'])||empty($data['apellido'])) + $messages->addMessageNow("submit-register-err", "Last name could not be empty"); + if(!isset($data['email'])||empty($data['email'])) $messages->addMessageNow("submit-register-err", "E-mail could not be empty"); - if(!isset($data['financiacion'])) + if(!isset($data['financiacion'])||!is_numeric($data['financiacion'])) $messages->addMessageNow("submit-register-err", "Question about financial support must have a selected answer"); - if(!isset($data['invited'])) + if(!isset($data['invited'])||!is_numeric($data['invited'])) $messages->addMessageNow("submit-register-err", "Question about 'invited to participate in this conference' must have a selected answer"); - if(!isset($data['letterinvited'])) + if(!isset($data['letterinvited'])||!is_numeric($data['letterinvited'])) $messages->addMessageNow("submit-register-err", "Question about needing a letter of invitation must have a selected answer"); - if(!isset($data['g-recaptcha-response'])){ + + //ReCaptcha Validation + if(!isset($data['g-recaptcha-response'])||empty($data['g-recaptcha-response'])){ $messages->addMessageNow("submit-register-err", "Please validate captcha!"); } else { @@ -89,27 +89,34 @@ $app->group('/api', function($app){ if(!$resp->isSuccess()){ $messages->addMessageNow("submit-register-err", "ReCaptcha validation error " . implode($resp->getErrorCodes())); } + else { + unset($data['g-recaptcha-response']); + } + } + + try{ + if($db->findByMail($data['email'])) + $messages->addMessageNow("submit-register-err", $data['email']." is already registered"); + else + $db->insert($data); + } + catch (Exception $e){ + $this->logger->debug("Submit register DB error: ".$e->getMessage()); + $messages->addMessageNow("submit-register-err", "DB error: ".$e->getMessage()); } if($messages->hasMessage("submit-register-err")){ - - $arrayresponse = array("success" => false, "errors"=>$messages->getMessage("submit-register-err"), "data"=>$data); - $newres = $response->withJson($arrayresponse); - return $newres; + $arrayresponse = array("success" => false, + "msg"=>"You registration could not be completed:\n", + "errors"=>$messages->getMessage("submit-register-err"), "data"=>$data); } else { - - /*try{ - $db->insert($data); - } - catch (Exception $e){ - $this->logger->debug($e->getMessage()); - return $e->getMessage(); - }*/ - echo "todo ok"; + $arrayresponse = array("success" => true, + "msg"=>"You have been successfully registered to the GGDWorkshop", + "data"=>$data); } - - + $newres = $response->withJson($arrayresponse); + return $newres; }); }); diff --git a/templates/registration.html b/templates/registration.html index a96b84c..67f8f05 100644 --- a/templates/registration.html +++ b/templates/registration.html @@ -163,8 +163,8 @@
-
- +
+
@@ -175,36 +175,41 @@ +
+ +
- + - + {% endblock %}