You are here

function uc_order_select_customer in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_order/uc_order.module \uc_order_select_customer()
  2. 6.2 uc_order/uc_order.admin.inc \uc_order_select_customer()

Presents the customer search results and let one of them be chosen.

See also

uc_order_select_customer_form()

1 string reference to 'uc_order_select_customer'
uc_order_menu in uc_order/uc_order.module
Implements hook_menu().

File

uc_order/uc_order.admin.inc, line 662
Order administration menu items.

Code

function uc_order_select_customer($email = NULL) {
  $build = array();
  $options = NULL;

  // Return the search results and let them pick one!
  if (arg(4) == 'search') {
    $first_name = str_replace('*', '%', db_like($_POST['first_name']));
    $last_name = str_replace('*', '%', db_like($_POST['last_name']));
    $email = str_replace('*', '%', db_like($_POST['email']));
    $query = db_select('users', 'u')
      ->distinct();
    $query
      ->leftJoin('uc_orders', 'o', 'u.uid = o.uid');
    $query
      ->fields('u', array(
      'uid',
      'mail',
    ))
      ->fields('o', array(
      'billing_first_name',
      'billing_last_name',
    ))
      ->condition('u.uid', 0, '>')
      ->orderBy('o.billing_last_name');
    if ($first_name && $first_name !== '%') {
      $query
        ->condition('o.billing_first_name', $first_name, 'LIKE');
    }
    if ($last_name && $last_name !== '%') {
      $query
        ->condition('o.billing_last_name', $last_name, 'LIKE');
    }
    if ($email && $email !== '%') {
      $query
        ->condition(db_or()
        ->condition('o.primary_email', $email, 'LIKE')
        ->condition('u.mail', $email, 'LIKE'));
    }
    $result = $query
      ->execute();
    $options = array();
    foreach ($result as $user) {
      if (empty($user->billing_first_name) && empty($user->billing_last_name)) {
        $name = '';
      }
      else {
        $name = $user->billing_last_name . ', ' . $user->billing_first_name . ' ';
      }
      $options[$user->uid . ':' . $user->mail] = $name . '(' . $user->mail . ')';
    }
    if (count($options) == 0) {
      $build['description'] = array(
        '#markup' => '<p>' . t('Search returned no results.') . '</p>',
      );
      $options = NULL;
    }
    else {
      $build['description'] = array(
        '<p>' . t('Search returned the following:') . '</p>',
      );
    }
  }

  // Check to see if the e-mail address for a new user is unique.
  if (arg(5) == 'check') {
    $email = check_plain($_POST['email']);
    $build['email'] = array(
      '#markup' => '',
    );
    if (!valid_email_address($email)) {
      $build['email']['#markup'] .= t('Invalid e-mail address.') . '<br />';
    }
    $result = db_query("SELECT uid, mail FROM {users} WHERE mail = :mail", array(
      ':mail' => $email,
    ));
    if ($user = $result
      ->fetchObject()) {
      $build['email']['#markup'] .= t('An account already exists for that e-mail.') . '<br /><br />';
      $build['email']['#markup'] .= '<b>' . t('Use this account now?') . '</b><br />' . t('User @uid - @mail', array(
        '@uid' => $user->uid,
        '@mail' => $user->mail,
      )) . ' <input type="button" ' . 'onclick="select_existing_customer(' . $user->uid . ', \'' . $user->mail . '\');" value="' . t('Apply') . '" /><br /><br /><hr /><br/>';
    }
    else {
      $name = uc_store_email_to_username($email);
      $fields = array(
        'name' => $name,
        'mail' => $email,
        'pass' => user_password(6),
        'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
      );
      $account = user_save('', $fields);
      if ($_POST['sendmail'] == 'true') {

        // Manually set the password so it appears in the e-mail.
        $account->password = $fields['pass'];

        // Send the e-mail through the user module.
        drupal_mail('user', 'register_admin_created', $email, uc_store_mail_recipient_language($email), array(
          'account' => $account,
        ), uc_store_email_from());
        $build['email']['#markup'] .= t('Account details sent to e-mail provided.<br /><br /><strong>Username:</strong> @username<br /><strong>Password:</strong> @password', array(
          '@username' => $fields['name'],
          '@password' => $fields['pass'],
        )) . '<br /><br />';
      }
      $build['result'] = array(
        '#markup' => '<strong>' . t('Use this account now?') . '</strong><br />' . t('User @uid - @mail', array(
          '@uid' => $account->uid,
          '@mail' => $account->mail,
        )) . ' <input type="button" ' . 'onclick="select_existing_customer(' . $account->uid . ', \'' . $account->mail . '\');" value="' . t('Apply') . '" /><br /><br /><hr /><br/>',
      );
    }
  }
  $build['customer_select_form'] = drupal_get_form('uc_order_select_customer_form', $options);
  print drupal_render($build);
  exit;
}