From: Steve Sutton Date: Fri, 22 Sep 2017 20:46:53 +0000 (-0400) Subject: Work on login and add self to registrant list. X-Git-Tag: v1.0.0^2~388 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=f574c6c10fa0bd3705043a7da566b7bb7dc863e1;p=WP-Plugins%2Fglm-member-db-registrations.git Work on login and add self to registrant list. Getting the login form working and setting up a model for the logged in account which can be used to add your self as a registrant for the event class. --- diff --git a/js/frontRegApp.js b/js/frontRegApp.js index 670519d..23db71e 100644 --- a/js/frontRegApp.js +++ b/js/frontRegApp.js @@ -5,9 +5,9 @@ app.Models.Front.Account = Backbone.Model.extend({ // Default registrant values defaults: { + name: 'AppAccount', id: '', email: '', - parent: '', }, url: ajaxUrl+'&glm_action=regFront&collection=account', @@ -100,7 +100,8 @@ app.Models.Front.Login = Backbone.Model.extend({ defaults: { username: '', - password: '' + password: '', + message: '', }, urlRoot: appLoginUrl, @@ -380,52 +381,23 @@ app.Collections.Front.RegRequestRegistrants = Backbone.Collection.extend({ app.Views.Front.Account = Backbone.View.extend({ tagName: 'div', - className: 'glm-reg-level-registrant clearfix', + className: '', template: _.template( jQuery('#account-template').html() ), events: { - //'click label': 'edit', - 'click .glm-reg-level-registrant-delete': 'deleteOne', - 'click .saveRegistrant': 'close', }, initialize: function(){ this.listenTo( this.model, 'change', this.render ); - //this.listenTo( this.model, 'destroy', this.remove ); return this; }, render: function(){ this.$el.html( this.template( this.model.toJSON() ) ); - //this.$nameInput = this.$('.editName'); - this.$emailInput = this.$('.editEmail'); return this; }, - deleteOne: function(){ - //console.log( this.model ); - this.model.destroy(); - this.remove(); - }, - - edit: function(){ - this.$el.addClass( 'editing' ); - this.$emailInput.focus(); - }, - - close: function(){ - var regemail = this.$emailInput.val().trim(); - - this.model.save({ email: regemail }, { validate: true }); - if ( this.model.validationError ) { - alert( this.model.validationError ); - this.render(); - } - - this.$el.removeClass('editing'); - }, - }); // js/views/app.js @@ -441,11 +413,15 @@ app.Views.Front.App = Backbone.View.extend({ this.cart = cart; this.event = new app.Views.Front.RegEvent({ model: regEvent }); this.render(); - // var regRequest = cart.get( 'request' ); - // console.log( regRequest.id ); }, render: function(){ + // Check to see if there's a login + if ( loginAccount ) { + jQuery('.glm-reg-account-login').hide(); + var accountView = new app.Views.Front.Account({ model: loginAccount }); + this.$el.append( accountView.render().el ); + } this.$el.append( this.event.render().el ); return this; }, @@ -453,16 +429,71 @@ app.Views.Front.App = Backbone.View.extend({ events: { 'click #appLogin': 'login', 'click #glm-reg-cart-continue': 'continue', + 'click #accountLogin': 'loginToAccount', + 'click #accountLogout': 'logout', }, continue: function(){ console.log( cart ); // Need to update the cart + }, + logout: function(){ + // Send data to Logout + jQuery.ajax({ + context: this, + url: appAccountUrl, + dataType: 'json', + data: 'option=logout' + }) + .done(function( msg ){ + console.log( msg ); + // Here we can update. + loginAccount = ''; + this.render(); + }) + .fail(function(msg){ + console.log('Fail:'); + console.log( msg ); + }); + }, + + loginToAccount: function(){ + this.$userInput = jQuery('.login'); + this.$pwdInput = jQuery('.password'); + + // Send data to login ajax and verify user login + jQuery.ajax({ + context: this, + url: appAccountUrl, + dataType: 'json', + data: 'option=login&username=' + this.$userInput.val().trim() + '&password=' + this.$pwdInput.val().trim() + }) + .done(function(account){ + if ( account.valid !== false ) { + // Create account model and assign to this.loginAccount + loginAccount = new app.Models.Front.Account({ + id: account.id, + email: account.email + }); + // Destroy the model and remove the view + this.loginModel.destroy(); + this.loginView.remove(); + this.render(); + } else { + console.log( 'Login failed' ); + this.loginModel.set({ message: 'Failed login!' }); + } + }) + .fail(function(msg){ + console.log('Fail:'); + console.log( msg ); + }); }, login: function(){ - this.loginView = new app.Views.Front.Login({ model: new app.Models.Front.Login() }); + this.loginModel = new app.Models.Front.Login(); + this.loginView = new app.Views.Front.Login({ model: this.loginModel }); this.$el.append( this.loginView.render().el ); jQuery('#appLogin').hide(); }, @@ -480,11 +511,11 @@ app.Views.Front.Login = Backbone.View.extend({ template: _.template( jQuery('#eventReg-account-login').html() ), events: { - 'click .accountLogin': 'loginToAccount', 'click #loginCancel': 'closeLoginForm' }, initialize: function(){ + this.listenTo( this.model, 'change', this.render ); return this; }, @@ -494,17 +525,8 @@ app.Views.Front.Login = Backbone.View.extend({ return; }, - loginToAccount: function(){ - // Send data to login ajax and verify user login - jQuery.ajax({ - url: ajaxUrl - }); - }, - render: function(){ this.$el.html( this.template( this.model.toJSON() ) ); - this.$userInput = jQuery('.login'); - this.$pwdInput = jQuery('.password'); return this; }, @@ -572,6 +594,11 @@ app.Views.Front.RegClass = Backbone.View.extend({ }, render: function(){ + console.log( loginAccount ); + if ( loginAccount === '' ) { + console.log( 'hide called for .glm-add-account' ); + this.$('.glm-add-account').hide(); + } this.model.set({ reg_count: this.model.registrants.length }); this.$el.html( this.template( this.model.toJSON() ) ); // Need to see if this class has attendees and render them @@ -591,6 +618,33 @@ app.Views.Front.RegClass = Backbone.View.extend({ events: { 'click .addRegistrant': 'newEntry', + 'click .glm-add-account': 'addAccount', + 'click .glm-add-new-account': 'addNewAccount', + }, + + addAccount: function(){ + console.log( 'Add Account Called' ); + // Check to make sure the loginAccount is not empty + if ( loginAccount !== '' ) { + var regRequest = cart.get( 'request' ); + var accountEmail = loginAccount.get( 'email' ); + var accountId = loginAccount.get( 'id' ); + if ( accountId && accountEmail ) { + this.model.registrants.create({ + option: 'add', + account: accountId, + reg_request: regRequest.id, + reg_event: this.model.get( 'reg_event' ), + reg_class: this.model.get( 'id' ), + reg_rate: this.model.get('reg_rate_id'), + email: accountEmail, + }); + } + } + }, + + addNewAccount: function(){ + console.log( 'Add New Account Called' ) }, toggleClassOpen: function(){ diff --git a/js/models/front/account.js b/js/models/front/account.js index 9542d6a..d39327a 100644 --- a/js/models/front/account.js +++ b/js/models/front/account.js @@ -5,9 +5,9 @@ app.Models.Front.Account = Backbone.Model.extend({ // Default registrant values defaults: { + name: 'AppAccount', id: '', email: '', - parent: '', }, url: ajaxUrl+'&glm_action=regFront&collection=account', diff --git a/js/models/front/login.js b/js/models/front/login.js index 270ad76..6280f38 100644 --- a/js/models/front/login.js +++ b/js/models/front/login.js @@ -5,7 +5,8 @@ app.Models.Front.Login = Backbone.Model.extend({ defaults: { username: '', - password: '' + password: '', + message: '', }, urlRoot: appLoginUrl, diff --git a/js/views/front/account.js b/js/views/front/account.js index 06d08eb..623e5a4 100644 --- a/js/views/front/account.js +++ b/js/views/front/account.js @@ -4,50 +4,21 @@ app.Views.Front.Account = Backbone.View.extend({ tagName: 'div', - className: 'glm-reg-level-registrant clearfix', + className: '', template: _.template( jQuery('#account-template').html() ), events: { - //'click label': 'edit', - 'click .glm-reg-level-registrant-delete': 'deleteOne', - 'click .saveRegistrant': 'close', }, initialize: function(){ this.listenTo( this.model, 'change', this.render ); - //this.listenTo( this.model, 'destroy', this.remove ); return this; }, render: function(){ this.$el.html( this.template( this.model.toJSON() ) ); - //this.$nameInput = this.$('.editName'); - this.$emailInput = this.$('.editEmail'); return this; }, - deleteOne: function(){ - //console.log( this.model ); - this.model.destroy(); - this.remove(); - }, - - edit: function(){ - this.$el.addClass( 'editing' ); - this.$emailInput.focus(); - }, - - close: function(){ - var regemail = this.$emailInput.val().trim(); - - this.model.save({ email: regemail }, { validate: true }); - if ( this.model.validationError ) { - alert( this.model.validationError ); - this.render(); - } - - this.$el.removeClass('editing'); - }, - }); diff --git a/js/views/front/app.js b/js/views/front/app.js index fc363fe..ed16e89 100644 --- a/js/views/front/app.js +++ b/js/views/front/app.js @@ -11,11 +11,15 @@ app.Views.Front.App = Backbone.View.extend({ this.cart = cart; this.event = new app.Views.Front.RegEvent({ model: regEvent }); this.render(); - // var regRequest = cart.get( 'request' ); - // console.log( regRequest.id ); }, render: function(){ + // Check to see if there's a login + if ( loginAccount ) { + jQuery('.glm-reg-account-login').hide(); + var accountView = new app.Views.Front.Account({ model: loginAccount }); + this.$el.append( accountView.render().el ); + } this.$el.append( this.event.render().el ); return this; }, @@ -23,16 +27,71 @@ app.Views.Front.App = Backbone.View.extend({ events: { 'click #appLogin': 'login', 'click #glm-reg-cart-continue': 'continue', + 'click #accountLogin': 'loginToAccount', + 'click #accountLogout': 'logout', }, continue: function(){ console.log( cart ); // Need to update the cart + }, + + logout: function(){ + // Send data to Logout + jQuery.ajax({ + context: this, + url: appAccountUrl, + dataType: 'json', + data: 'option=logout' + }) + .done(function( msg ){ + console.log( msg ); + // Here we can update. + loginAccount = ''; + this.render(); + }) + .fail(function(msg){ + console.log('Fail:'); + console.log( msg ); + }); + }, + + loginToAccount: function(){ + this.$userInput = jQuery('.login'); + this.$pwdInput = jQuery('.password'); + // Send data to login ajax and verify user login + jQuery.ajax({ + context: this, + url: appAccountUrl, + dataType: 'json', + data: 'option=login&username=' + this.$userInput.val().trim() + '&password=' + this.$pwdInput.val().trim() + }) + .done(function(account){ + if ( account.valid !== false ) { + // Create account model and assign to this.loginAccount + loginAccount = new app.Models.Front.Account({ + id: account.id, + email: account.email + }); + // Destroy the model and remove the view + this.loginModel.destroy(); + this.loginView.remove(); + this.render(); + } else { + console.log( 'Login failed' ); + this.loginModel.set({ message: 'Failed login!' }); + } + }) + .fail(function(msg){ + console.log('Fail:'); + console.log( msg ); + }); }, login: function(){ - this.loginView = new app.Views.Front.Login({ model: new app.Models.Front.Login() }); + this.loginModel = new app.Models.Front.Login(); + this.loginView = new app.Views.Front.Login({ model: this.loginModel }); this.$el.append( this.loginView.render().el ); jQuery('#appLogin').hide(); }, diff --git a/js/views/front/login.js b/js/views/front/login.js index 3fbdbd9..f0f7550 100644 --- a/js/views/front/login.js +++ b/js/views/front/login.js @@ -9,11 +9,11 @@ app.Views.Front.Login = Backbone.View.extend({ template: _.template( jQuery('#eventReg-account-login').html() ), events: { - 'click .accountLogin': 'loginToAccount', 'click #loginCancel': 'closeLoginForm' }, initialize: function(){ + this.listenTo( this.model, 'change', this.render ); return this; }, @@ -23,17 +23,8 @@ app.Views.Front.Login = Backbone.View.extend({ return; }, - loginToAccount: function(){ - // Send data to login ajax and verify user login - jQuery.ajax({ - url: ajaxUrl - }); - }, - render: function(){ this.$el.html( this.template( this.model.toJSON() ) ); - this.$userInput = jQuery('.login'); - this.$pwdInput = jQuery('.password'); return this; }, diff --git a/js/views/front/regClass.js b/js/views/front/regClass.js index 1b88b8e..6d00dd4 100644 --- a/js/views/front/regClass.js +++ b/js/views/front/regClass.js @@ -18,6 +18,11 @@ app.Views.Front.RegClass = Backbone.View.extend({ }, render: function(){ + console.log( loginAccount ); + if ( loginAccount === '' ) { + console.log( 'hide called for .glm-add-account' ); + this.$('.glm-add-account').hide(); + } this.model.set({ reg_count: this.model.registrants.length }); this.$el.html( this.template( this.model.toJSON() ) ); // Need to see if this class has attendees and render them @@ -37,6 +42,33 @@ app.Views.Front.RegClass = Backbone.View.extend({ events: { 'click .addRegistrant': 'newEntry', + 'click .glm-add-account': 'addAccount', + 'click .glm-add-new-account': 'addNewAccount', + }, + + addAccount: function(){ + console.log( 'Add Account Called' ); + // Check to make sure the loginAccount is not empty + if ( loginAccount !== '' ) { + var regRequest = cart.get( 'request' ); + var accountEmail = loginAccount.get( 'email' ); + var accountId = loginAccount.get( 'id' ); + if ( accountId && accountEmail ) { + this.model.registrants.create({ + option: 'add', + account: accountId, + reg_request: regRequest.id, + reg_event: this.model.get( 'reg_event' ), + reg_class: this.model.get( 'id' ), + reg_rate: this.model.get('reg_rate_id'), + email: accountEmail, + }); + } + } + }, + + addNewAccount: function(){ + console.log( 'Add New Account Called' ) }, toggleClassOpen: function(){ diff --git a/models/admin/ajax/account.php b/models/admin/ajax/account.php index ed07b62..3a13667 100644 --- a/models/admin/ajax/account.php +++ b/models/admin/ajax/account.php @@ -91,60 +91,73 @@ class GlmMembersAdmin_ajax_account extends GLMDataRegistrationsAccount */ public function modelAction( $actionData = false ) { - //static $accountId = 10; $return = false; $option = filter_var( $_REQUEST['option'], FILTER_SANITIZE_STRING ); switch ( $option ) { + case 'logout': + unset( $_SESSION['LoginAccount'] ); + $return = array( 'valid' => false ); + break; case 'login': + $username = filter_var( $_REQUEST['username'], FILTER_SANITIZE_STRING ); + $password = filter_var( $_REQUEST['password'], FILTER_SANITIZE_STRING ); // Check accounts for ones that match - $returnPassword = $this->wpdb->get_var( + $returnData = $this->wpdb->get_row( $this->wpdb->prepare( - "SELECT password + "SELECT id,password FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "account WHERE email = %s", - $jsonData->username - ) + $username + ), + ARRAY_A ); - if ( $returnPassword == crypt( $jsonData->password, $returnPassword ) ) { - $return = array( 'valid' => true ); + $returnPassword = $returnData['password']; + $returnId = $returnData['id']; + if ( $returnPassword == crypt( $password, $returnPassword ) ) { + $return = array( 'valid' => true, 'email' => $username, 'id' => $returnId ); + // Add the user to the php session + $_SESSION['LoginAccount'] = array( + 'id' => $returnId, + 'email' => $username + ); } else { $return = array( 'valid' => false ); } break; - case 'checkEmail': - break; - } - if ( $email = filter_var( $_REQUEST['email'], FILTER_VALIDATE_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' => true, - 'validEmail' => true, - 'id' => (int)$accountId, - 'email' => $email, + case 'checkEmail': + if ( $email = filter_var( $_REQUEST['email'], FILTER_VALIDATE_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' => true, + 'validEmail' => true, + 'id' => (int)$accountId, + 'email' => $email, + ); + } else { + $return = array( + 'valid' => false, + 'validEmail' => true, + 'email' => $email, + ); + } } else { $return = array( 'valid' => false, - 'validEmail' => true, - 'email' => $email, + 'validEmail' => false, ); } - } else { - $return = array( - 'valid' => false, - 'validEmail' => false, - ); + break; } header('Content-type:application/json;charset=utf-8', true); echo json_encode( $return ); diff --git a/models/front/registrations/registration.php b/models/front/registrations/registration.php index 8b72a46..bf438a2 100644 --- a/models/front/registrations/registration.php +++ b/models/front/registrations/registration.php @@ -37,6 +37,8 @@ public function modelAction($actionData = false) { + $loginAccount = ''; + $scripts = array( //'backbone-local' => 'js/lib/backbone.localStorage.min.js', 'regApp' => 'js/frontRegApp.js', @@ -98,7 +100,7 @@ if ( isset( $cartId ) && filter_var( $cartId, FILTER_VALIDATE_INT ) ) { $cart = $regCart->getRegistrationCart( $cartId ); //echo '
$cart: ' . print_r( $cart, true ) . '
'; - $regRequestJSON = json_encode( $cart['request'] ); + $regRequestJSON = json_encode( $cart['request'], JSON_NUMERIC_CHECK ); } //echo '
$_SESSION: ' . print_r( $_SESSION, true ) . '
'; @@ -126,16 +128,18 @@ $regClass = $RegClass->getList( $where ); //echo '
$regClass: ' . print_r( $regClass, true ) . '
'; $RegRate = new GlmDataRegistrationsRegRate( $this->wpdb, $this->config); - foreach ( $regClass as &$rClass ) { - // grab the reg rate for this class - $rates = $RegRate->getList( "T.reg_class = " . $rClass['id'] ); - //echo '
$rates: ' . print_r( $rates, true ) . '
'; - if ( isset( $rates ) && is_array( $rates ) ) { - foreach ( $rates as $rate ) { - $rClass['reg_rate_id'] = $rate['id']; - $rClass['reg_rate_name'] = $rate['name']; - $rClass['reg_rate_base_price'] = $rate['base_rate']; - $rClass['reg_rate_per_reg'] = $rate['per_registrant']; + if ( isset( $regClass ) && is_array( $regClass ) ) { + foreach ( $regClass as &$rClass ) { + // grab the reg rate for this class + $rates = $RegRate->getList( "T.reg_class = " . $rClass['id'] ); + //echo '
$rates: ' . print_r( $rates, true ) . '
'; + if ( isset( $rates ) && is_array( $rates ) ) { + foreach ( $rates as $rate ) { + $rClass['reg_rate_id'] = $rate['id']; + $rClass['reg_rate_name'] = $rate['name']; + $rClass['reg_rate_base_price'] = $rate['base_rate']; + $rClass['reg_rate_per_reg'] = $rate['per_registrant']; + } } } } @@ -161,21 +165,32 @@ // Build the Class array of json objects for the classes. $jsonClasses = array(); - foreach ( $regClass as $rClass ) { - $jsonClasses[] = json_encode( $rClass ); + if ( isset( $regClass ) && is_array( $regClass ) ) { + foreach ( $regClass as $rClass ) { + $jsonClasses[] = json_encode( $rClass, JSON_NUMERIC_CHECK ); + } } $regClassJSON = '[' . implode( ',', $jsonClasses ) . ']'; $regCartJSON = json_encode( $cart ); + // check to see if there's a user logged in + if ( isset( $_SESSION['LoginAccount'] ) && filter_var( $_SESSION['LoginAccount']['id'], FILTER_VALIDATE_INT ) ) { + $loginAccount = json_encode( $_SESSION['LoginAccount'], JSON_NUMERIC_CHECK ); + //unset( $_SESSION['LoginAccount'] ); + } + + //echo '
$_SESSION: ' . print_r( $_SESSION, true ) . '
'; + // Compile template data $templateData = array( 'entry' => $regEventSample, 'thisJsUrl' => GLM_MEMBERS_REGISTRATIONS_PLUGIN_URL . '/js', - 'regEventJSON' => json_encode( $event ), + 'regEventJSON' => json_encode( $event, JSON_NUMERIC_CHECK ), 'regClassesJSON' => $regClassJSON, 'regCartJSON' => $regCartJSON, 'regRequestJSON' => $regRequestJSON, 'eventData' => $eventData, + 'loginAccount' => $loginAccount, ); // Return status, any suggested view, and any data to controller return array( diff --git a/views/front/registrations/registration.html b/views/front/registrations/registration.html index 3b67f9a..81ac573 100644 --- a/views/front/registrations/registration.html +++ b/views/front/registrations/registration.html @@ -1,6 +1,25 @@ {* Event Registration App - Backbone.js *} {* Underscore Templates for the Event Registration App *} -{literal} +{* Template for Account Login *}{literal} + +{/literal} +{* Template for Logged in Account *}{literal} + +{/literal} +{* Template for the regEvent *}{literal} - - - - - +{/literal} +{* Template for regClass *}{literal} - - - +{/literal} +{* Template for registrant add form *}{literal} {/literal} +{* Template for registrant *}{literal} + +{/literal}