You are here

function _commerce_customer_match_customer_profiles_standard in Commerce Core 7

Helper function for commerce_customer_match_customer_profiles().

Returns an array of products matching the specific parameters.

1 call to _commerce_customer_match_customer_profiles_standard()
commerce_customer_match_customer_profiles in modules/customer/commerce_customer.module
Fetches an array of all customer profiles matching the given parameters.

File

modules/customer/commerce_customer.module, line 1321
Defines the customer profile entity and API functions to manage customers and interact with them.

Code

function _commerce_customer_match_customer_profiles_standard($field, $ids = array(), $limit = NULL) {

  // Build the query object with the necessary fields.
  $query = db_select('commerce_customer_profile', 'cp');
  $profile_id_alias = $query
    ->addField('cp', 'profile_id');
  $profile_uid_alias = $query
    ->addField('cp', 'uid');
  $profile_type_alias = $query
    ->addField('cp', 'type');

  // Add a condition to the query to filter by matching profile types.
  if (!empty($field['settings']['referenceable_types']) && is_array($field['settings']['referenceable_types'])) {
    $types = array_diff(array_values($field['settings']['referenceable_types']), array(
      0,
      NULL,
    ));

    // Only filter by type if some types have been specified.
    if (!empty($types)) {
      $query
        ->condition('cp.type', $types, 'IN');
    }
  }
  if ($ids) {

    // Otherwise add a profile_id specific condition if specified.
    $query
      ->condition($profile_id_alias, $ids, 'IN');
  }

  // Order the results by ID and then profile type.
  $query
    ->orderBy($profile_id_alias)
    ->orderBy($profile_type_alias);

  // Add a limit if specified.
  if ($limit) {
    $query
      ->range(0, $limit);
  }

  // Execute the query and build the results array.
  $result = $query
    ->execute();
  $matches = array();
  foreach ($result
    ->fetchAll() as $profile) {
    $matches[$profile->profile_id] = array(
      'uid' => $profile->uid,
      'type' => $profile->type,
      'rendered' => t('Profile @profile_id', array(
        '@profile_id' => $profile->profile_id,
      )),
    );
  }
  return $matches;
}