You are here

function uc_attribute_load_multiple in Ubercart 6.2

Same name and namespace in other branches
  1. 8.4 uc_attribute/uc_attribute.module \uc_attribute_load_multiple()
  2. 7.3 uc_attribute/uc_attribute.module \uc_attribute_load_multiple()

Loads attribute objects from the database.

@todo If we feel it necessary, we could optimize this, by inverting the logic; that is, we could make uc_attribute load call this function and allow this function to minimize the number of queries necessary. -cha0s

Parameters

$aids: Attribute IDs to load.

$type: The type of attribute. 'product', or 'class'. Any other type will fetch a base attribute.

$id: The ID of the product/class this attribute belongs to.

Return value

An array of loaded attributes.

4 calls to uc_attribute_load_multiple()
UbercartAttributeTestCase::testAttributeAPI in uc_attribute/uc_attribute.test
Tests the basic attribute API.
UbercartAttributeTestCase::testAttributeUIAttributeOptionsBulkEdit in uc_attribute/uc_attribute.test
Tests the "bulk edit attribute options" user interface.
UbercartCartLinksTestCase::createCartLinksProduct in uc_cart_links/uc_cart_links.test
Creates a product with all attribute types and options.
uc_attribute_load_product_attributes in uc_attribute/uc_attribute.module
Fetches an array of attribute objects that belong to a product.

File

uc_attribute/uc_attribute.module, line 660

Code

function uc_attribute_load_multiple($aids = array(), $type = '', $id = NULL) {
  $sql = uc_attribute_type_info($type);
  $conditions = array();

  // Filter by the attribute IDs requested.
  if (!empty($aids)) {

    // Sanity check - filter out non-numeric attribute IDs.
    $conditions[] = "ua.aid IN (" . implode(", ", array_filter($aids, 'is_numeric')) . ")";
  }

  // Product/class attributes.
  if (!empty($type)) {
    $conditions[] = "uca.{$sql['id']} = {$sql['placeholder']}";
    $conditions = implode(" AND", $conditions);

    // Seems like a big query to get attribute IDs, but it's all about the sort.
    // (I'm not sure if the default ordering is propagating down correctly here.
    // It appears that product/class attributes with no ordering won't let the
    // attribute's propagate down, as it does when loading. -cha0s)
    $result = db_query("\n      SELECT    uca.aid\n      FROM      {$sql['attr_table']} AS uca\n      LEFT JOIN {uc_attributes} AS ua ON uca.aid = ua.aid\n      WHERE     {$conditions}\n      ORDER BY  uca.ordering, ua.name", $id);
  }
  else {

    // Padding just to make sure that everything's fine if we don't get an aid
    // condition. Keeps it elegant.
    $conditions[] = "1";
    $conditions = implode(" AND ", $conditions);
    $result = db_query("SELECT aid FROM {uc_attributes} ua WHERE {$conditions} ORDER BY ordering, name");
  }

  // Load the attributes.
  $attributes = array();
  while ($aid = db_result($result)) {
    $attributes[$aid] = uc_attribute_load($aid, $id, $type);
  }
  return $attributes;
}