Form for adding new account
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 28 Sep 2017 20:00:17 +0000 (16:00 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 28 Sep 2017 20:00:17 +0000 (16:00 -0400)
Register form.
Creates a new account.
Issues: not setting the password.

css/front.css
js/frontRegApp.js
js/models/front/register.js [new file with mode: 0644]
js/views/front/app.js
js/views/front/login.js
js/views/front/register.js [new file with mode: 0644]
models/admin/ajax/account.php
views/front/registrations/registration.html

index 4b3a7d5..061ddd9 100644 (file)
@@ -111,16 +111,33 @@ label.registrant-label {
     width: 80%;
 }
 div.glm-reg-login {
-    background-color: grey;
+    background-color: white;
     border: solid 1px black;
     padding: 2rem;
     width: 30rem;
     position: absolute;
     top: 10vh;
-    left: 30vw;
+    left: 10vw;
 }
 div.glm-reg-login h4 {
-    color: white;
+    color: black;
+}
+div.glm-reg-register {
+    background-color: white;
+    border: solid 1px black;
+    padding: 2rem;
+    width: 30rem;
+    position: absolute;
+    top: 10vh;
+    left: 10vw;
+}
+div.glm-reg-register div {
+    clear: both;
+}
+div.glm-reg-register input {
+    width: 48% !important;
+    margin-right: 5px;
+    float: left;
 }
 .reg-class-detail {
     /* display: none; */
index bfb4979..2bb8115 100644 (file)
@@ -272,6 +272,33 @@ app.Models.Front.RegRequestRegistrant = Backbone.Model.extend({
     // },
 });
 
+app.Models.Front.Register = Backbone.Model.extend({
+    defaults: {
+        validEmail: false,
+        name: 'register',
+        email: '',
+        password: '',
+        fname: '',
+        lname: '',
+        org: '',
+        title: '',
+        addr1: '',
+        addr2: '',
+        city: '',
+        state: '',
+        zip: '',
+        country: '',
+        phone: '',
+        fax: '',
+    },
+
+    urlRoot: ajaxUrl + 'glm_action=register',
+
+    validate: function(){
+        // Validation stuff here
+    },
+});
+
 app.Models.Front.Request = Backbone.Model.extend({
     defaults: {
         name: 'request',
@@ -449,7 +476,8 @@ app.Views.Front.App = Backbone.View.extend({
     render: function(){
         // Check to see if there's a login
         if ( loginAccount ) {
-            jQuery('.glm-reg-account-login').hide();
+            jQuery('#appLogin').hide();
+            jQuery('#register').hide();
             this.accountView = new app.Views.Front.Account({ model: loginAccount });
             this.$el.append( this.accountView.render().el );
         }
@@ -461,14 +489,11 @@ app.Views.Front.App = Backbone.View.extend({
         'click #appLogin': 'login',
         'click #glm-reg-cart-continue': 'continue',
         'click #accountLogin': 'loginToAccount',
+        'click #accountRegister': 'registerNewAccount',
         'click #accountLogout': 'logout',
         'click #register': 'register',
     },
 
-    register: function(){
-        // Have to create new view and model for the register
-    },
-
     continue: function(){
         console.log( cart );
         // Confirm they are done and goto the next page
@@ -531,16 +556,77 @@ app.Views.Front.App = Backbone.View.extend({
             }
         })
         .fail(function(msg){
-            console.log('Fail:');
+            console.log('Fail: ' );
+            console.log( msg );
+        });
+    },
+
+    registerNewAccount: function(){
+        this.$email    = this.$('.account_email').val().trim();
+        this.$password = this.$('.account_password').val().trim();
+        this.$fname    = this.$('.account_fname').val().trim();
+        this.$lname    = this.$('.account_lname').val().trim();
+        this.$addr1    = this.$('.account_addr1').val().trim();
+        this.$addr2    = this.$('.account_addr2').val().trim();
+        this.$city     = this.$('.account_city').val().trim();
+        this.$state    = this.$('.account_state').val().trim();
+        this.$zip      = this.$('.account_zip').val().trim();
+        this.$country  = this.$('.account_country').val().trim();
+
+        // Send request to backend
+        jQuery.ajax({
+            context: this,
+            url: appAccountUrl,
+            dataType: 'json',
+            data:{
+                option: 'register',
+                email: this.$email,
+                password: this.$password,
+                fname: this.$fname,
+                lname: this.$lname,
+                addr1: this.$addr1,
+                addr2: this.$addr2,
+                city: this.$city,
+                state: this.$state,
+                zip: this.$zip,
+                country: this.$country,
+            }
+        })
+        .done(function(account){
+            console.log('return from ajax for register new');
+            console.log(account);
+            if ( account.id != undefined && account.id !== '' ) {
+                // Account was created.
+                // Now use this account for login.
+                loginAccount = new app.Models.Front.Account({
+                    id: account.id,
+                    email: account.email,
+                    fname: account.fname,
+                    lname: account.lname
+                });
+                this.registerModel.destroy();
+                this.registerView.remove();
+                this.render();
+            }
+        })
+        .fail(function(msg){
+            console.log('Fail: ' );
             console.log( msg );
         });
     },
 
     login: function(){
         this.loginModel = new app.Models.Front.Login();
-        this.loginView = new app.Views.Front.Login({ model: this.loginModel });
+        this.loginView  = new app.Views.Front.Login({ model: this.loginModel });
         this.$el.append( this.loginView.render().el );
-        jQuery('#appLogin').hide();
+        jQuery('#accountHeader').hide();
+    },
+
+    register: function(){
+        this.registerModel = new app.Models.Front.Register();
+        this.registerView  = new app.Views.Front.Register({ model: this.registerModel });
+        this.$el.append( this.registerView.render().el );
+        jQuery('#accountHeader').hide();
     },
 
 });
@@ -556,7 +642,8 @@ app.Views.Front.Login = Backbone.View.extend({
     template: _.template( jQuery('#eventReg-account-login').html() ),
 
     events: {
-        'click #loginCancel': 'closeLoginForm'
+        'click #loginCancel': 'closeLoginForm',
+        'keydown': 'keyAction',
     },
 
     initialize: function(){
@@ -566,15 +653,22 @@ app.Views.Front.Login = Backbone.View.extend({
 
     closeLoginForm: function(){
         this.remove();
-        jQuery('#appLogin').show();
+        jQuery('#accountHeader').show();
         return;
     },
 
     render: function(){
         this.$el.html( this.template( this.model.toJSON() ) );
+        //jQuery('.login').focus();
         return this;
     },
 
+    keyAction: function(e){
+        if ( e.which === 13 ) {
+            console.log( 'Enter key used' );
+        }
+    },
+
 });
 
 app.Views.Front.NewRegistrant = Backbone.View.extend({
@@ -865,3 +959,67 @@ app.Views.Front.RegRequestRegistrant = Backbone.View.extend({
     },
 
 });
+
+app.Views.Front.Register = Backbone.View.extend({
+    tagName: 'div',
+
+    className: 'glm-reg-register',
+
+    template: _.template( jQuery('#register-new-account').html() ),
+
+    events: {
+        'click #registerCancel': 'closeForm',
+        'keydown': 'keyAction',
+    },
+
+    initialize: function(){
+    },
+
+    render: function(){
+        this.$el.html( this.template( this.model.toJSON() ) );
+        return this;
+    },
+
+    closeForm: function(){
+        this.remove();
+        jQuery('#accountHeader').show();
+        return;
+    },
+
+    keyAction: function(e){
+        if ( e.which === 13 ) {
+            console.log( 'Enter key used' );
+        } else if ( e.which === 9 ) {
+            console.log( 'Tab key used' );
+            // if they entered email then test for existing email
+            if ( this.model.get( 'validEmail' ) === true ){
+                return;
+            }
+            var email = this.$('.account_email').val().trim();
+            console.log( email );
+            jQuery.ajax({
+                context: this,
+                url: ajaxUrl + '&glm_action=account&option=checkEmail',
+                dataType: 'json',
+                data: 'email=' + email
+            })
+            .done(function(account){
+                if ( account.validEmail !== true ) {
+                    alert('Must be a valid email address!');
+                    jQuery('.account_email').focus();
+                } else if ( account.id !== undefined && account.email !== undefined ) {
+                    alert('There is an account already with that email address.\nPlease use another one!');
+                    jQuery('.account_email').val('');
+                    jQuery('.account_email').focus();
+                } else {
+                    // email is not existing account and valid
+                    this.model.set({ validEmail: true });
+                }
+            })
+            .fail(function(msg){
+                console.log('Fail: ' + msg);
+            });
+        }
+    },
+
+});
diff --git a/js/models/front/register.js b/js/models/front/register.js
new file mode 100644 (file)
index 0000000..3612c74
--- /dev/null
@@ -0,0 +1,26 @@
+app.Models.Front.Register = Backbone.Model.extend({
+    defaults: {
+        validEmail: false,
+        name: 'register',
+        email: '',
+        password: '',
+        fname: '',
+        lname: '',
+        org: '',
+        title: '',
+        addr1: '',
+        addr2: '',
+        city: '',
+        state: '',
+        zip: '',
+        country: '',
+        phone: '',
+        fax: '',
+    },
+
+    urlRoot: ajaxUrl + 'glm_action=register',
+
+    validate: function(){
+        // Validation stuff here
+    },
+});
index 14cdf6e..fcca729 100644 (file)
@@ -16,7 +16,8 @@ app.Views.Front.App = Backbone.View.extend({
     render: function(){
         // Check to see if there's a login
         if ( loginAccount ) {
-            jQuery('.glm-reg-account-login').hide();
+            jQuery('#appLogin').hide();
+            jQuery('#register').hide();
             this.accountView = new app.Views.Front.Account({ model: loginAccount });
             this.$el.append( this.accountView.render().el );
         }
@@ -28,14 +29,11 @@ app.Views.Front.App = Backbone.View.extend({
         'click #appLogin': 'login',
         'click #glm-reg-cart-continue': 'continue',
         'click #accountLogin': 'loginToAccount',
+        'click #accountRegister': 'registerNewAccount',
         'click #accountLogout': 'logout',
         'click #register': 'register',
     },
 
-    register: function(){
-        // Have to create new view and model for the register
-    },
-
     continue: function(){
         console.log( cart );
         // Confirm they are done and goto the next page
@@ -98,16 +96,77 @@ app.Views.Front.App = Backbone.View.extend({
             }
         })
         .fail(function(msg){
-            console.log('Fail:');
+            console.log('Fail: ' );
+            console.log( msg );
+        });
+    },
+
+    registerNewAccount: function(){
+        this.$email    = this.$('.account_email').val().trim();
+        this.$password = this.$('.account_password').val().trim();
+        this.$fname    = this.$('.account_fname').val().trim();
+        this.$lname    = this.$('.account_lname').val().trim();
+        this.$addr1    = this.$('.account_addr1').val().trim();
+        this.$addr2    = this.$('.account_addr2').val().trim();
+        this.$city     = this.$('.account_city').val().trim();
+        this.$state    = this.$('.account_state').val().trim();
+        this.$zip      = this.$('.account_zip').val().trim();
+        this.$country  = this.$('.account_country').val().trim();
+
+        // Send request to backend
+        jQuery.ajax({
+            context: this,
+            url: appAccountUrl,
+            dataType: 'json',
+            data:{
+                option: 'register',
+                email: this.$email,
+                password: this.$password,
+                fname: this.$fname,
+                lname: this.$lname,
+                addr1: this.$addr1,
+                addr2: this.$addr2,
+                city: this.$city,
+                state: this.$state,
+                zip: this.$zip,
+                country: this.$country,
+            }
+        })
+        .done(function(account){
+            console.log('return from ajax for register new');
+            console.log(account);
+            if ( account.id != undefined && account.id !== '' ) {
+                // Account was created.
+                // Now use this account for login.
+                loginAccount = new app.Models.Front.Account({
+                    id: account.id,
+                    email: account.email,
+                    fname: account.fname,
+                    lname: account.lname
+                });
+                this.registerModel.destroy();
+                this.registerView.remove();
+                this.render();
+            }
+        })
+        .fail(function(msg){
+            console.log('Fail: ' );
             console.log( msg );
         });
     },
 
     login: function(){
         this.loginModel = new app.Models.Front.Login();
-        this.loginView = new app.Views.Front.Login({ model: this.loginModel });
+        this.loginView  = new app.Views.Front.Login({ model: this.loginModel });
         this.$el.append( this.loginView.render().el );
-        jQuery('#appLogin').hide();
+        jQuery('#accountHeader').hide();
+    },
+
+    register: function(){
+        this.registerModel = new app.Models.Front.Register();
+        this.registerView  = new app.Views.Front.Register({ model: this.registerModel });
+        this.$el.append( this.registerView.render().el );
+        jQuery('#accountHeader').hide();
     },
 
 });
index f0f7550..a3d0be3 100644 (file)
@@ -9,7 +9,8 @@ app.Views.Front.Login = Backbone.View.extend({
     template: _.template( jQuery('#eventReg-account-login').html() ),
 
     events: {
-        'click #loginCancel': 'closeLoginForm'
+        'click #loginCancel': 'closeLoginForm',
+        'keydown': 'keyAction',
     },
 
     initialize: function(){
@@ -19,13 +20,20 @@ app.Views.Front.Login = Backbone.View.extend({
 
     closeLoginForm: function(){
         this.remove();
-        jQuery('#appLogin').show();
+        jQuery('#accountHeader').show();
         return;
     },
 
     render: function(){
         this.$el.html( this.template( this.model.toJSON() ) );
+        //jQuery('.login').focus();
         return this;
     },
 
+    keyAction: function(e){
+        if ( e.which === 13 ) {
+            console.log( 'Enter key used' );
+        }
+    },
+
 });
diff --git a/js/views/front/register.js b/js/views/front/register.js
new file mode 100644 (file)
index 0000000..9c9909b
--- /dev/null
@@ -0,0 +1,63 @@
+app.Views.Front.Register = Backbone.View.extend({
+    tagName: 'div',
+
+    className: 'glm-reg-register',
+
+    template: _.template( jQuery('#register-new-account').html() ),
+
+    events: {
+        'click #registerCancel': 'closeForm',
+        'keydown': 'keyAction',
+    },
+
+    initialize: function(){
+    },
+
+    render: function(){
+        this.$el.html( this.template( this.model.toJSON() ) );
+        return this;
+    },
+
+    closeForm: function(){
+        this.remove();
+        jQuery('#accountHeader').show();
+        return;
+    },
+
+    keyAction: function(e){
+        if ( e.which === 13 ) {
+            console.log( 'Enter key used' );
+        } else if ( e.which === 9 ) {
+            console.log( 'Tab key used' );
+            // if they entered email then test for existing email
+            if ( this.model.get( 'validEmail' ) === true ){
+                return;
+            }
+            var email = this.$('.account_email').val().trim();
+            console.log( email );
+            jQuery.ajax({
+                context: this,
+                url: ajaxUrl + '&glm_action=account&option=checkEmail',
+                dataType: 'json',
+                data: 'email=' + email
+            })
+            .done(function(account){
+                if ( account.validEmail !== true ) {
+                    alert('Must be a valid email address!');
+                    jQuery('.account_email').focus();
+                } else if ( account.id !== undefined && account.email !== undefined ) {
+                    alert('There is an account already with that email address.\nPlease use another one!');
+                    jQuery('.account_email').val('');
+                    jQuery('.account_email').focus();
+                } else {
+                    // email is not existing account and valid
+                    this.model.set({ validEmail: true });
+                }
+            })
+            .fail(function(msg){
+                console.log('Fail: ' + msg);
+            });
+        }
+    },
+
+});
index b68c9a1..5cbcb79 100644 (file)
@@ -37,7 +37,7 @@ require_once GLM_MEMBERS_REGISTRATIONS_PLUGIN_CLASS_PATH . '/data/dataAccount.ph
  * an AJAX call that goes through the WorPress AJAX Handler.
  *
  */
-class GlmMembersAdmin_ajax_account extends GLMDataRegistrationsAccount
+class GlmMembersAdmin_ajax_account extends GlmDataRegistrationsAccount
 {
 
     /**
@@ -96,6 +96,69 @@ class GlmMembersAdmin_ajax_account extends GLMDataRegistrationsAccount
         $option = filter_var( $_REQUEST['option'], FILTER_SANITIZE_STRING );
 
         switch ( $option ) {
+        case 'register':
+            // trigger_error(print_r($_REQUEST,1));
+            $email    = filter_var( $_REQUEST['email'], FILTER_VALIDATE_EMAIL );
+            $password = filter_var( $_REQUEST['password'], FILTER_SANITIZE_STRING );
+            $fname    = filter_var( $_REQUEST['fname'], FILTER_SANITIZE_STRING );
+            $lname    = filter_var( $_REQUEST['lname'], FILTER_SANITIZE_STRING );
+            $addr1    = filter_var( $_REQUEST['addr1'], FILTER_SANITIZE_STRING );
+            $addr2    = filter_var( $_REQUEST['addr2'], FILTER_SANITIZE_STRING );
+            $city     = filter_var( $_REQUEST['city'], FILTER_SANITIZE_STRING );
+            $state    = filter_var( $_REQUEST['state'], FILTER_SANITIZE_STRING );
+            $zip      = filter_var( $_REQUEST['zip'], FILTER_SANITIZE_STRING );
+            $country  = filter_var( $_REQUEST['country'], FILTER_SANITIZE_STRING );
+            // Verify that the email is not registered already
+            if ( $email  ) {
+                $accountId = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                          FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "account
+                         WHERE email = %s",
+                        $email
+                    )
+                );
+                if ( $accountId ) {
+                    $return = array(
+                        'valid'      => false,
+                        'validEmail' => false,
+                    );
+                    break;
+                }
+                // If they reach here then check create a new account record
+                $_POST = $_REQUEST;
+                // Note: When using insertEntry() it is not setting the
+                // password. Not sure why!
+                $account = $this->insertEntry();
+                trigger_error(print_r($account,E_USER_NOTICE));
+                IF ( $account['status'] ) {
+                    $return = array(
+                        'id'       => $account['fieldData']['id'],
+                        'email'    => $email,
+                        'fname'    => $fname,
+                        'lname'    => $lname,
+                        'addr1'    => $addr1,
+                        'addr2'    => $addr2,
+                        'city'     => $city,
+                        'state'    => $state,
+                        'zip'      => $zip,
+                        'country'  => $country,
+                    );
+                } else {
+                    $return = array(
+                        'valid'      => false,
+                        'validEmail' => false,
+                    );
+                }
+            } else {
+                $return = array(
+                    'valid'      => false,
+                    'validEmail' => false,
+                );
+                break;
+            }
+
+            break;
         case 'logout':
             unset( $_SESSION['LoginAccount'] );
             $return = array( 'valid' => false );
@@ -108,7 +171,8 @@ class GlmMembersAdmin_ajax_account extends GLMDataRegistrationsAccount
                 $this->wpdb->prepare(
                     "SELECT id,password,fname,lname
                        FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX .  "account
-                      WHERE email = %s",
+                      WHERE email = %s
+                        AND active = true",
                     $username
                 ),
                 ARRAY_A
index cd886c4..7dd3c61 100644 (file)
@@ -2,7 +2,7 @@
 {* Underscore Templates for the Event Registration App *}
 {* Template for Account Login *}{literal}
 <script type="text/template" id="eventReg-account-login">
-    <a class="glm-reg-account-login" id="loginCancel">Close</a>
+    <a id="loginCancel">Close</a>
     <h4>Login</h4>
     <% if ( message ) { %>
         <span style="color: red;"><%= message %></span>
@@ -10,7 +10,6 @@
     <input class="login" placeholder="Email Address" type="email">
     <input class="password" placeholder="Password" type="password">
     <input type="submit" id="accountLogin" value="Login">
-    <a id="register">Register</a>
     <a id="forgotLogin">Forgot Password</a>
 </script>
 {/literal}
 {/literal}
 {* Template for Register New Account *}{literal}
 <script type="text/template" id="register-new-account">
-<h1>New Account</h1>
+    <a id="registerCancel">Close</a>
+    <h3>Register New Account</h3>
+    <div>
+        {/literal}{$terms.reg_term_contact_information}{literal}
+    </div>
+    <div>
+        <input class="account_email" placeholder="*Email Address">
+        <input class="account_password" placeholder="*Password">
+    </div>
+    <div>
+        <input class="account_fname" placeholder="*First Name">
+        <input class="account_lname" placeholder="*Last Name">
+    </div>
+    <div>
+        <input class="account_addr1" placeholder="Address 1">
+        <input class="account_addr2" placeholder="Address 2">
+    </div>
+    <div>
+        <input class="account_city" placeholder="City">
+        <input class="account_state" placeholder="State">
+    </div>
+    <div>
+        <input class="account_zip" placeholder="Zip/Postal Code">
+        <input class="account_country" placeholder="Country">
+    </div>
+    <div>
+        <input type="submit" id="accountRegister" value="Register">
+    </div>
 </script>
 {/literal}
 {* Template for the regEvent *}{literal}
 {/literal}
 <div class="glm-reg-event-list" id="regApp">
     <h1>{$terms.reg_term_registrations_name}</h1>
-    <div class="glm-reg-account">
-        <a class="glm-reg-account-login" id="appLogin">Login</a>
+    <div id="accountHeader">
+        <a id="appLogin">Login</a>
+        <a id="register">Register</a>
     </div>
 </div>
 {* Bootstrap the models needed on page load *}