You are here

function uc_order_select_customer in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_order/uc_order.module \uc_order_select_customer()
  2. 7.3 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 825
Order administration menu items.

Code

function uc_order_select_customer($email = NULL) {
  $options = NULL;
  $where = '';
  $output = '';

  // Return the search results and let them pick one!
  if (arg(4) == 'search') {
    $first_name = strtolower(str_replace('*', '%', $_POST['first_name']));
    $last_name = strtolower(str_replace('*', '%', $_POST['last_name']));
    $email = strtolower(str_replace('*', '%', $_POST['email']));
    $username = strtolower(str_replace('*', '%', $_POST['username']));
    if ($first_name && $first_name !== '%') {
      $where .= " AND LOWER(o.billing_first_name) LIKE '" . db_escape_string($first_name) . "'";
    }
    if ($last_name && $last_name !== '%') {
      $where .= " AND LOWER(o.billing_last_name) LIKE '" . db_escape_string($last_name) . "'";
    }
    if ($email && $email !== '%') {
      $where .= " AND (LOWER(o.primary_email) LIKE '" . db_escape_string($email) . "' OR LOWER(u.mail) LIKE '" . db_escape_string($email) . "')";
    }
    if ($username && $username !== '%') {
      $where .= " AND LOWER(u.name) LIKE '" . db_escape_string($username) . "'";
    }
    $query = "SELECT DISTINCT u.uid, u.name, u.mail, o.billing_first_name, " . "o.billing_last_name FROM {users} AS u LEFT JOIN {uc_orders} " . "AS o ON u.uid = o.uid WHERE u.uid > 0 AND (o.order_status " . "IS NULL OR o.order_status IN " . uc_order_status_list('general', TRUE) . ")" . $where . " ORDER BY o.billing_last_name ASC";
    $result = db_query($query);
    $options = array();
    while ($user = db_fetch_object($result)) {
      $name = '';
      if (!empty($user->billing_first_name) && !empty($user->billing_last_name)) {
        $name = $user->billing_last_name . ', ' . $user->billing_first_name . ' ';
      }

      // Options formated as "First Last <email@example.com> (username)".
      $options[$user->uid . ':' . $user->mail] = $name . '<' . $user->mail . '>' . ' (' . $user->name . ')';
    }
    if (count($options) == 0) {

      // No search results found.
      $output .= '<p>' . t('Search returned no results.') . '</p>';
      $options = NULL;
    }
    else {

      // Display search results.
      $output .= '<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 = $_POST['email'];
    if (!valid_email_address($email)) {
      $output .= t('Invalid e-mail address.') . '<br />';
    }
    $result = db_query("SELECT uid, mail FROM {users} WHERE LOWER(mail) = LOWER('%s')", $email);
    if ($user = db_fetch_object($result)) {
      $output .= t('An account already exists for that e-mail.') . '<br /><br />';
      $output .= '<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, NULL, array(
          'account' => $account,
        ), uc_store_email_from());
        $output .= 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 />';
      }
      $output .= '<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 />';
    }
  }
  $output .= drupal_get_form('uc_order_select_customer_form', $options);
  print $output;
  exit;
}