From 8909491a12cdebdbfc1307232b9329ba79822676 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Wed, 30 Oct 2013 17:53:15 +0100 Subject: [PATCH] new membership and account creation process : modal and form is in a separated page --- js/materio_user.js | 134 +++++++++++++++++++++++++++++++++++++++++ js/materio_user.min.js | 1 + materio_user.module | 99 +++++++++++++++--------------- materio_user.pages.inc | 55 +++++++++++++++++ 4 files changed, 239 insertions(+), 50 deletions(-) create mode 100755 js/materio_user.js create mode 100755 js/materio_user.min.js create mode 100644 materio_user.pages.inc diff --git a/js/materio_user.js b/js/materio_user.js new file mode 100755 index 00000000..12153782 --- /dev/null +++ b/js/materio_user.js @@ -0,0 +1,134 @@ +// @codekit-prepend "gui.js" +// @koala-prepend "gui.js" + + +(function($) { + +MaterioUser = function(){ + + var _isAdhesion = $('body').is('.page-node-11187') || $('body').is('.page-node-11186'); + + function init() { + //trace('MaterioUser init compile test 3'); + if(_isAdhesion) + initAdhesion(); + }; + + function initAdhesion(){ + //trace('initAdhesion'); + $('.get-link a').bind('click', function(event) { + + // do not show the registration form if already logged-in + if($('body').is('.logged-in')) + return true; + + // else show the ajaxified registration form + event.preventDefault(); + + var $this = $(this), + href = $this.attr("href"), + destination = href.match('/\?destination=([^,]+)'); + + destination = "/" + destination[1].replace('%23', '#'); + trace('destination', destination); + + loadRegistrationBlock(destination); + return false; + }); + }; + + function loadRegistrationBlock(destination){ + //trace('loadRegistrationBlock :: dest = '+dest); + $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_user/register', + // {'types':types,'current_path':document.location.href, 'keys':keys, 'searchmode':searchmode}, + function(json){ + //trace('json', json); + + var $modal = $('
').addClass('modal-content').append(json.block); + + // $.event.trigger({ + // type:"form-loaded", + // context:"modal-content" + // }); + + var id,label, description; + $('input.form-text', $modal).each(function(i) { + id = $(this).attr('id'); + label = $('label[for='+id+']').hide().text(); + description = $(this).parent().find('.description').hide().text(); + $(this).attr('placeholder', label).attr('title', description); + }); + + // $('.homepage-textfield', $modal).hide(); + + $('body').append($('
').addClass('modal-wrapper').append($modal)); + + // doesn't work + // Drupal.attachBehaviors('bodi>.modal-wrapper'); + + $.event.trigger('ajax-register-block-loaded'); + + $("#user-register-form #edit-submit--2", $modal).click(function(event){ + event.preventDefault(); + $.ajax({ + type: 'POST', + dataType:'json', + url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/register/submit", + data: $('#user-register-form', $modal).serialize(), + success: function(data) { onUserLoginRegisterSubmit(data, destination);}, + error: function() {trace('error');} + }); + return false; + }); + + $("#user-login #edit-submit", $modal).click(function(event){ + event.preventDefault(); + $.ajax({ + type: 'POST', + dataType:'json', + url: Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/login/submit", + data: $('#user-login', $modal).serialize(), + success: function(data){ onUserLoginRegisterSubmit(data, destination); }, + error: function() { trace('error'); } + }); + return false; + }); + + // google analytics + // $.event.trigger({ + // type : "record-stat", + // categorie : 'Search', + // action : keys, + // label : 'filters : '+ stringTypes.join(' ,'), + // value : json.count + // }); + + + }); + }; + + function onUserLoginRegisterSubmit(data, destination){ + cleanModalErrors(); + if(data.errors != null){ + for(field in data.errors){ + $('#user-login input[name='+field+']', '.modal-content').addClass('error'); + $('.modal-content').prepend($('
').addClass('message-error').html(data.errors[field])); + } + }else{ + window.location.pathname = destination; + } + }; + + function cleanModalErrors() { + $('.message-error', '.modal-content').remove(); + $('input', '.modal-content').removeClass('error'); + } + + init(); +}; + +$(document).ready(function() { + var materiouser = new MaterioUser(); +}); + +})(jQuery); \ No newline at end of file diff --git a/js/materio_user.min.js b/js/materio_user.min.js new file mode 100755 index 00000000..27620fb8 --- /dev/null +++ b/js/materio_user.min.js @@ -0,0 +1 @@ +!function(t){MaterioUser=function(){function e(){a&&r()}function r(){t(".get-link a").bind("click",function(e){e.preventDefault();var r=t(this),a=r.attr("href").match("/?destination=([^,]+)/");return i(a),!1})}function i(){t.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/register",function(e){var r,i,a,n=t("
").addClass("modal-content").append(e.block);t("input.form-text",n).each(function(){r=t(this).attr("id"),i=t("label[for="+r+"]").hide().text(),a=t(this).parent().find(".description").hide().text(),t(this).attr("placeholder",i).attr("title",a)}),t("body").append(t("
").addClass("modal-wrapper").append(n)),t.event.trigger("ajax-register-block-loaded"),t("#user-register-form #edit-submit--2").click(function(e){return e.preventDefault(),t.ajax({type:"POST",dataType:"json",url:Drupal.settings.basePath+Drupal.settings.pathPrefix+"materio_user/register/submit",data:t("#user-register-form",n).serialize(),success:function(){},error:function(){}}),!1})})}var a=t("body").is(".page-node-11187")||t("body").is(".page-node-11186");e()},t(document).ready(function(){new MaterioUser})}(jQuery); \ No newline at end of file diff --git a/materio_user.module b/materio_user.module index 04d7f713..03b14c6d 100755 --- a/materio_user.module +++ b/materio_user.module @@ -1,6 +1,14 @@ MENU_CALLBACK, + 'file' => 'materio_user.pages.inc', + ); + + $items['materio_user/register'] = $base+array( + 'title' => 'Materio base user ajax', + 'page callback' => 'materio_user_register', + // 'page arguments' => array(), + 'access callback' => TRUE, + ); + + $items['materio_user/register/submit'] = $base+array( + 'title' => 'Materio base user ajax', + 'page callback' => 'materio_user_register_submit', + // 'page arguments' => array(), + 'access callback' => TRUE, + ); + + $items['materio_user/login/submit'] = $base+array( + 'title' => 'Materio base user ajax', + 'page callback' => 'materio_user_login_submit', + // 'page arguments' => array(), + 'access callback' => TRUE, + ); + + return $items; +} + /** * Implements hook_menu_alter(). */ @@ -87,7 +127,7 @@ function materio_user_block_view($delta = '') { /** * Implements hook_form_alter(). */ -function materio_user_form_alter(&$form, &$form_state, $form_id) { +function DISABLED_NOT_FINISHED_materio_user_form_alter(&$form, &$form_state, $form_id) { // dsm($form_id); if( $form_id == "user_register_form" && !user_access('administer users') ){ // dsm($form); @@ -131,60 +171,20 @@ function materio_user_form_alter(&$form, &$form_state, $form_id) { // } } -// function materio_user_element_info() { -// return array( -// "new_password_confirm" => array( -// '#input' => TRUE, -// '#process' => array('materio_user_process_new_password_confirm'), -// '#theme_wrappers' => array('form_element'), -// ), -// ); -// } - -/** - * Expand a password_confirm field into two text boxes. - */ -// function materio_user_process_new_password_confirm($element) { -// $element['pass1'] = array( -// '#type' => 'password', -// '#title' => t('New password'), -// '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'], -// '#required' => $element['#required'], -// '#attributes' => array('class' => array('password-field')), -// ); -// $element['pass2'] = array( -// '#type' => 'password', -// '#title' => t('Confirm password'), -// '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'], -// '#required' => $element['#required'], -// '#attributes' => array('class' => array('password-confirm')), -// ); -// $element['#element_validate'] = array('password_confirm_validate'); -// $element['#tree'] = TRUE; - -// if (isset($element['#size'])) { -// $element['pass1']['#size'] = $element['pass2']['#size'] = $element['#size']; -// } - -// return $element; -// } - - - function materio_user_user_register_form_submit($form, &$form_state){ // dsm($form, 'form'); // dsm($form_state, 'form_state'); // dsm($_REQUEST, '$_REQUEST'); // dsm($_GET, '$_GET'); - unset($_REQUEST['destination'], $_REQUEST['edit']['destination']); + // unset($_REQUEST['destination'], $_REQUEST['edit']['destination']); - $form_state['redirect'] = array( - 'node/11187', - array( - 'fragment' => $_GET['q'] == 'node/11187' ? 'content-bottom' : '', // if we register from the membership page, then go directly to the form - ) - ); + // $form_state['redirect'] = array( + // 'node/11187', + // array( + // 'fragment' => $_GET['q'] == 'node/11187' ? 'content-bottom' : '', // if we register from the membership page, then go directly to the form + // ) + // ); // $frontlink = l('continue with basic', ''); // // drupal_get_messages('status'); @@ -206,7 +206,7 @@ function materio_user_webform_client_111186_validate($form, &$form_state){ // dsm($form, 'form'); // dsm($form_state, 'form_state'); $values = $form_state['values']['submitted']; - if ($values['membership_options'] == 3) { + if ($values['column_left']['membership_options'] == 3) { foreach ($values['collaborators'] as $coll_key => $coll) { foreach ($coll as $field_key => $field_value) { if($field_value == ''){ @@ -224,7 +224,6 @@ function materio_user_webform_client_111186_validate($form, &$form_state){ } - } // function materio_user_webform_client_111186_submit($form, &$form_state){ diff --git a/materio_user.pages.inc b/materio_user.pages.inc new file mode 100644 index 00000000..df8b2697 --- /dev/null +++ b/materio_user.pages.inc @@ -0,0 +1,55 @@ +'. t('create your materiO\' account') . ''; + // $return['block'] .= drupal_render(drupal_get_form('user_register_form')); + + // $block = module_invoke('materio_user', 'block_view', 'user_register'); + // $return['block'] = render($block); + + $block = block_load('materio_user','user_register'); + + $return['block'] = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block)))); + + // dsm($block, '$block'); + drupal_json_output($return); + // return 'hello'; +} + +function materio_user_register_submit(){ + $return = array(); + $return['POST'] = $_POST; + + $form_state = array( "values"=>$_POST); + drupal_form_submit($_POST['form_id'], $form_state); + + $return['form_state'] = $form_state; + + $return['errors'] = form_get_errors(); + if($return['errors']) + unset ($_SESSION['messages']['error']); + + // after registration user is automaticly logged in, thank's to login tobogan module + + drupal_json_output($return); +} + +function materio_user_login_submit(){ + $return = array(); + $return['POST'] = $_POST; + + $form_state = array("values"=>$_POST); + drupal_form_submit($_POST['form_id'], $form_state); + + $return['errors'] = form_get_errors(); + if($return['errors']) + unset ($_SESSION['messages']['error']); + + // if user-login form succed we retreive the user uid on $form_state, then we can effectively loggin the user + if($uid = $form_state['uid']) + user_login_submit(array(), $form_state); + + drupal_json_output($return); +} \ No newline at end of file