You are here

function uc_checkout_pane_customer in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_cart/uc_cart_checkout_pane.inc \uc_checkout_pane_customer()
  2. 6.2 uc_cart/uc_cart_checkout_pane.inc \uc_checkout_pane_customer()

Gets the user's email address for login.

1 string reference to 'uc_checkout_pane_customer'
uc_cart_uc_checkout_pane in uc_cart/uc_cart.module
Implements hook_uc_checkout_pane().

File

uc_cart/uc_cart_checkout_pane.inc, line 36
Callbacks for the default Ubercart checkout panes plus helper functions.

Code

function uc_checkout_pane_customer($op, $order, $form = NULL, &$form_state = NULL) {
  global $user;
  switch ($op) {
    case 'view':
      if ($user->uid) {
        $email = $user->mail;
        $description = t('Order information will be sent to your account e-mail listed below.');

        // . '<br />'
        $contents['primary_email'] = array(
          '#type' => 'hidden',
          '#value' => $email,
        );
        $contents['email_text'] = array(
          '#markup' => '<div>' . t('<b>E-mail address:</b> @email (<a href="!url">edit</a>)', array(
            '@email' => $email,
            '!url' => url('user/' . $user->uid . '/edit', array(
              'query' => drupal_get_destination(),
            )),
          )) . '</div>',
        );
      }
      else {
        $email = $order->primary_email;
        $description = t('Enter a valid email address for this order or <a href="!url">click here</a> to login with an existing account and return to checkout.', array(
          '!url' => url('user/login', array(
            'query' => drupal_get_destination(),
          )),
        ));
        $contents['primary_email'] = uc_textfield(t('E-mail address'), $email, TRUE, NULL, 64);
      }
      if (variable_get('uc_cart_email_validation', FALSE) && !$user->uid) {
        $contents['primary_email_confirm'] = uc_textfield(t('Confirm e-mail address'), $email, TRUE, NULL, 64);
      }
      if ($user->uid == 0) {
        $contents['new_account'] = array();
        if (variable_get('uc_cart_new_account_name', FALSE)) {
          $contents['new_account']['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Username'),
            '#default_value' => isset($order->data['new_user']['name']) ? $order->data['new_user']['name'] : '',
            '#maxlength' => 60,
            '#size' => 32,
          );
        }
        if (variable_get('uc_cart_new_account_password', FALSE)) {
          $contents['new_account']['pass'] = array(
            '#type' => 'password',
            '#title' => t('Password'),
            '#maxlength' => 32,
            '#size' => 32,
          );
          $contents['new_account']['pass_confirm'] = array(
            '#type' => 'password',
            '#title' => t('Confirm password'),
            '#description' => t('Passwords must match to proceed.'),
            '#maxlength' => 32,
            '#size' => 32,
          );
        }
        if (!empty($contents['new_account'])) {
          $array = array(
            '#type' => 'fieldset',
            '#title' => t('New account details'),
            '#description' => variable_get('uc_cart_new_account_details', t('<b>Optional.</b> New customers may supply custom account details.<br />We will create these for you if no values are entered.')),
            '#collapsible' => FALSE,
          );
          $contents['new_account'] = array_merge($array, $contents['new_account']);
        }
      }
      return array(
        'description' => $description,
        'contents' => $contents,
      );
    case 'process':
      $pane = $form_state['values']['panes']['customer'];
      if (!empty($pane['primary_email']) && !valid_email_address($pane['primary_email'])) {
        form_set_error('panes][customer][primary_email', t('You must enter a valid e-mail address.'));
      }
      $order->primary_email = $pane['primary_email'];
      if (variable_get('uc_cart_email_validation', FALSE) && !$user->uid && $pane['primary_email'] !== $pane['primary_email_confirm']) {
        form_set_error('panes][customer][primary_email_confirm', t('The e-mail address did not match.'));
      }

      // Invalidate if an account already exists for this e-mail address, and the user is not logged into that account
      if (!variable_get('uc_cart_mail_existing', TRUE) && $user->uid == 0 && !empty($pane['primary_email'])) {
        if (db_query("SELECT uid FROM {users} WHERE mail LIKE :mail", array(
          ':mail' => $pane['primary_email'],
        ))
          ->fetchField() > 0) {
          form_set_error('panes][customer][primary_email', t('An account already exists for your e-mail address. You will either need to login with this e-mail address or use a different e-mail address.'));
        }
      }

      // If new users can specify names or passwords then...
      if ((variable_get('uc_cart_new_account_name', FALSE) || variable_get('uc_cart_new_account_password', FALSE)) && $user->uid == 0) {

        // Skip if an account already exists for this e-mail address.
        if (variable_get('uc_cart_mail_existing', TRUE) && db_query("SELECT uid FROM {users} WHERE mail LIKE :mail", array(
          ':mail' => $pane['primary_email'],
        ))
          ->fetchField() > 0) {
          drupal_set_message(t('An account already exists for your e-mail address. The new account details you entered will be disregarded.'));
        }
        else {

          // Validate the username.
          if (variable_get('uc_cart_new_account_name', FALSE) && !empty($pane['new_account']['name'])) {
            $message = user_validate_name($pane['new_account']['name']);
            if (!empty($message)) {
              form_set_error('panes][customer][new_account][name', $message);
            }
            elseif (db_query("SELECT uid FROM {users} WHERE name LIKE :name", array(
              ':name' => $pane['new_account']['name'],
            ))
              ->fetchField()) {
              form_set_error('panes][customer][new_account][name', t('The username %name is already taken. Please enter a different name or leave the field blank for your username to be your e-mail address.', array(
                '%name' => $pane['new_account']['name'],
              )));
            }
            else {
              $order->data['new_user']['name'] = $pane['new_account']['name'];
            }
          }

          // Validate the password.
          if (variable_get('uc_cart_new_account_password', FALSE)) {
            if (strcmp($pane['new_account']['pass'], $pane['new_account']['pass_confirm'])) {
              form_set_error('panes][customer][new_account][pass_confirm', t('The passwords you entered did not match. Please try again.'));
            }
            if (!empty($pane['new_account']['pass'])) {
              require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
              $order->data['new_user']['hash'] = user_hash_password(trim($pane['new_account']['pass']));
            }
          }
        }
      }
      if ($user->uid) {
        $order->uid = $user->uid;
      }
      return TRUE;
    case 'review':
      $review[] = array(
        'title' => t('E-mail'),
        'data' => check_plain($order->primary_email),
      );
      return $review;
    case 'settings':
      $form['uc_cart_new_account_details'] = array(
        '#type' => 'textarea',
        '#title' => t('New account details help message'),
        '#description' => t('Enter the help message displayed in the new account details fieldset when shown.'),
        '#default_value' => variable_get('uc_cart_new_account_details', t('<b>Optional.</b> New customers may supply custom account details.<br />We will create these for you if no values are entered.')),
      );
      return $form;
  }
}