You are here

function uc_order_select_customer in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_order/uc_order.admin.inc \uc_order_select_customer()
  2. 7.3 uc_order/uc_order.admin.inc \uc_order_select_customer()
1 string reference to 'uc_order_select_customer'
uc_order_menu in uc_order/uc_order.module
Implementation of hook_menu().

File

uc_order/uc_order.module, line 2195

Code

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

  // Return the search results and let them pick one!
  if (arg(4) == 'search') {
    $first_name = strtolower(str_replace('*', '%', check_plain(arg(5))));
    $last_name = strtolower(str_replace('*', '%', check_plain(arg(6))));
    $email = strtolower(str_replace('*', '%', check_plain(arg(7))));
    if ($first_name !== '0' && $first_name !== '%') {
      $where .= " AND LOWER(o.billing_first_name) LIKE '" . $first_name . "'";
    }
    if ($last_name !== '0' && $last_name !== '%') {
      $where .= " AND LOWER(o.billing_last_name) LIKE '" . $last_name . "'";
    }
    if ($email !== '0' && $email !== '%') {
      $where .= " AND (LOWER(o.primary_email) LIKE '" . $email . "' OR LOWER(u.mail) LIKE '" . $email . "')";
    }
    $query = "SELECT DISTINCT u.uid, 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)) {
      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) {
      $output .= '<p>' . t('Search returned no results.') . '</p>';
      $options = NULL;
    }
    else {
      $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 = check_plain(arg(6));
    if (!valid_email_address($email)) {
      $output .= t('Invalid e-mail address.') . '<br />';
    }
    $result = db_query("SELECT uid, mail FROM {users} WHERE mail = '%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 {
      $data = array(
        'name' => $email,
        'mail' => $email,
        'pass' => user_password(6),
        'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
      );
      $user = user_save(NULL, $data);
      if ($_POST['sendmail'] == 'true') {
        $variables = array(
          '!username' => $data['name'],
          '!site' => variable_get('site_name', 'Drupal'),
          '!password' => $data['pass'],
          '!uri' => $base_url,
          '!uri_brief' => substr($base_url, strlen('http://')),
          '!mailto' => $data['mail'],
          '!date' => format_date(time()),
          '!login_uri' => url('user', NULL, NULL, TRUE),
          '!edit_uri' => url('user/' . $user->uid . '/edit', NULL, NULL, TRUE),
          '!login_url' => user_pass_reset_url($user),
        );
        $from = uc_store_email_from();
        $subject = _user_mail_text('admin_subject', $variables);
        $body = _user_mail_text('admin_body', $variables);
        drupal_mail('user-register-welcome', $user->mail, $subject, $body, $from);
        $output .= t('Account details sent to e-mail provided.<br /><br /><strong>Username:</strong> !username<br /><strong>Password:</strong> !password', array(
          '!username' => $user->name,
          '!password' => $data['pass'],
        )) . '<br /><br />';
      }
      $output .= '<strong>' . t('Use this account now?') . '</strong><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 />';
    }
  }
  $output .= drupal_get_form('uc_order_select_customer_form', $options);
  print $output;
  exit;
}