You are here

function uc_attribute_load in Ubercart 7.3

Same name and namespace in other branches
  1. 8.4 uc_attribute/uc_attribute.module \uc_attribute_load()
  2. 5 uc_attribute/uc_attribute.module \uc_attribute_load()
  3. 6.2 uc_attribute/uc_attribute.module \uc_attribute_load()

Loads an attribute from the database.

Parameters

int $aid: The ID of the attribute.

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

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

Return value

object|false The attribute object, or FALSE if it doesn't exist.

16 calls to uc_attribute_load()
hook_uc_product_alter in uc_product/uc_product.api.php
Make alterations to a specific variant of a product node.
UbercartAttributeCheckoutTestCase::testAttributeAddToCart in uc_attribute/tests/uc_attribute_checkout.test
Tests that product in cart has the selected attribute option.
UbercartAttributeTestCase::testAttributeAPI in uc_attribute/tests/uc_attribute.test
Tests the basic attribute API.
UbercartAttributeTestCase::testAttributeUIAddAttribute in uc_attribute/tests/uc_attribute.test
Tests the "add attribute" user interface.
UbercartAttributeTestCase::testAttributeUIClassAttributeOverview in uc_attribute/tests/uc_attribute.test
Tests the product class attribute user interface.

... See full list

File

uc_attribute/uc_attribute.module, line 673
Ubercart Attribute module.

Code

function uc_attribute_load($aid, $id = NULL, $type = '') {
  $sql = uc_attribute_type_info($type);
  switch ($type) {
    case 'product':
    case 'class':

      // Read attribute data.
      $query = db_select('uc_attributes', 'a')
        ->fields('a', array(
        'aid',
        'name',
        'description',
      ))
        ->condition('a.aid', $aid);
      $query
        ->leftJoin($sql['attr_table'], 'pa', "a.aid = pa.aid AND pa.{$sql['id']} = :id", array(
        ':id' => $id,
      ));
      $query
        ->fields('pa', array(
        'label',
        'default_option',
        'required',
        'ordering',
        'display',
        $sql['id'],
      ));
      $query
        ->addField('a', 'label', 'default_label');
      $query
        ->addField('a', 'ordering', 'default_ordering');
      $query
        ->addField('a', 'required', 'default_required');
      $query
        ->addField('a', 'display', 'default_display');
      $attribute = $query
        ->execute()
        ->fetchObject();

      // Don't try to build it further if it failed already.
      if (!$attribute) {
        return FALSE;
      }

      // Set any missing defaults.
      foreach (array(
        'ordering',
        'required',
        'display',
        'label',
      ) as $field) {
        if (isset($attribute->{"default_{$field}"}) && is_null($attribute->{$field})) {
          $attribute->{$field} = $attribute->{"default_{$field}"};
        }
      }
      if (empty($attribute->label)) {
        $attribute->label = $attribute->name;
      }

      // Read option data.
      $query = db_select($sql['opt_table'], 'po')
        ->fields('po', array(
        $sql['id'],
        'oid',
        'cost',
        'price',
        'weight',
        'ordering',
      ));
      $query
        ->leftJoin('uc_attribute_options', 'ao', "po.oid = ao.oid AND po.{$sql['id']} = :id", array(
        ':id' => $id,
      ));
      $query
        ->fields('ao', array(
        'name',
        'aid',
      ))
        ->condition('aid', $aid)
        ->orderBy('po.ordering')
        ->orderBy('ao.name');
      $result = $query
        ->execute();
      break;
    default:

      // Read attribute and option data.
      $attribute = db_query("SELECT * FROM {uc_attributes} WHERE aid = :aid", array(
        ':aid' => $aid,
      ))
        ->fetchObject();
      $result = db_query("SELECT * FROM {uc_attribute_options} WHERE aid = :aid ORDER BY ordering, name", array(
        ':aid' => $aid,
      ));

      // Don't try to build it further if it failed already.
      if (!$attribute) {
        return FALSE;
      }
      break;
  }

  // Got an attribute?
  if ($attribute) {

    // Get its options, too.
    $attribute->options = array();
    foreach ($result as $option) {
      $attribute->options[$option->oid] = $option;
    }
    uc_attribute_translate($attribute);
  }
  return $attribute;
}