You are here

function uc_store_customers in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_store/uc_store.admin.inc \uc_store_customers()

Display customer administration page.

1 call to uc_store_customers()
uc_store_customer_search in uc_store/uc_store.module
Display the customer search page.
1 string reference to 'uc_store_customers'
uc_store_menu in uc_store/uc_store.module
Implementation of hook_menu().

File

uc_store/uc_store.module, line 625
Contains global Ubercart functions and store administration functionality.

Code

function uc_store_customers($message = NULL, $query = NULL, $count_query = NULL, $page_length = 25) {
  if (!module_exists('uc_order')) {
    return t('You must enable the order module to track customers.');
  }
  if (is_null($query)) {
    $query = "SELECT DISTINCT o.uid, u.mail, o.billing_first_name, " . "o.billing_last_name, o.billing_city, o.billing_zone, " . "o.billing_country FROM {uc_orders} AS o LEFT JOIN " . "{users} AS u ON o.uid = u.uid WHERE o.uid > 0 AND " . "o.order_status IN " . uc_order_status_list('general', TRUE);
    $count_query = "";
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $count_query = "SELECT COUNT(DISTINCT o.uid, o.billing_first_name, " . "o.billing_last_name, u.mail) FROM {uc_orders} AS o " . "LEFT JOIN {users} AS u ON o.uid = u.uid WHERE o.uid > 0 " . "AND o.order_status IN " . uc_order_status_list('general', TRUE);
        break;
      case 'pgsql':
        $count_query = "SELECT DISTINCT o.uid, o.billing_last_name, o.billing_first_name, " . "COUNT(*) " . "FROM {uc_orders} AS o " . "LEFT JOIN {users} AS u ON o.uid = u.uid WHERE o.uid > 0 " . "AND o.order_status IN " . uc_order_status_list('general', TRUE) . " GROUP BY o.uid, o.billing_last_name, o.billing_first_name ";
        break;
    }
    $message = t('The following users on your site have placed orders:');
  }
  $header = array(
    t('View'),
    array(
      'data' => t('Name'),
      'field' => 'o.billing_last_name',
      'sort' => 'asc',
    ),
    array(
      'data' => t('E-mail'),
      'field' => 'u.mail',
    ),
    array(
      'data' => t('City'),
      'field' => 'o.billing_city',
    ),
    array(
      'data' => t('ID'),
      'field' => 'o.uid',
    ),
  );
  $query .= tablesort_sql($header);
  $count_query .= tablesort_sql($header);
  $address = variable_get('uc_customer_list_address', 'billing');
  if ($address == 'shipping') {
    $query = str_replace('billing', 'delivery', $query);
    $count_query = str_replace('billing', 'delivery', $count_query);
  }
  else {
    $address = 'billing';
  }
  $result = pager_query($query, $page_length, 0, $count_query);
  while ($customer = db_fetch_object($result)) {
    $icons = l(uc_store_get_icon('admin/store/customers', TRUE), 'user/' . $customer->uid, array(
      'title' => t('View user details.'),
    ), NULL, NULL, FALSE, TRUE) . l(uc_store_get_icon('admin/store/orders', TRUE), 'admin/store/customers/orders/' . $customer->uid, array(
      'title' => t("View customer's order."),
    ), NULL, NULL, FALSE, TRUE);
    if ($address == 'shipping') {
      $name = ucfirst($customer->delivery_last_name) . ', ' . ucfirst($customer->delivery_first_name);
      $city = ucfirst($customer->delivery_city) . ', ' . uc_get_zone_code($customer->delivery_zone);
    }
    else {
      $name = ucfirst($customer->billing_last_name) . ', ' . ucfirst($customer->billing_first_name);
      $city = ucfirst($customer->billing_city) . ', ' . uc_get_zone_code($customer->billing_zone);
    }
    if ($name == ', ') {
      $name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $customer->uid));
      $name = t('User: !name', array(
        '!name' => $name,
      ));
    }
    $rows[] = array(
      'data' => array(
        array(
          'data' => $icons,
        ),
        array(
          'data' => check_plain($name),
        ),
        array(
          'data' => check_plain($customer->mail),
        ),
        array(
          'data' => check_plain($city),
        ),
        array(
          'data' => $customer->uid,
        ),
      ),
      'id' => 'customer-' . $customer->uid,
    );
  }
  uc_add_js(drupal_get_path('module', 'uc_store') . '/uc_store.js');
  $output = '<p>' . $message . '</p>' . theme('table', $header, $rows, array(
    'width' => '100%',
    'class' => 'uc-customer-table',
  )) . '<br />' . theme_pager(NULL, $page_length);
  return $output;
}