You are here

class UcAddressesEntityController in Ubercart Addresses 7

Ubercart Addresses entity controller class.

Hierarchy

Expanded class hierarchy of UcAddressesEntityController

1 string reference to 'UcAddressesEntityController'
uc_addresses_entity_info in ./uc_addresses.module
Implements hook_entity_info().

File

class/uc_addresses.entity.inc, line 11
Entity integration code.

View source
class UcAddressesEntityController extends EntityAPIController {

  /**
   * Implements DrupalEntityControllerInterface::load().
   *
   * @return array
   *   An array of entities.
   */
  public function load($ids = array(), $conditions = array()) {
    $entities = array();
    if (!empty($this->entityInfo['entity class'])) {
      $class = $this->entityInfo['entity class'];
    }
    else {
      throw new UcAddressesException('There is no entity class specified for the uc_addresses entity.');
    }
    foreach ($ids as $id) {
      $address = UcAddressesAddressBook::loadAddress($id);
      if ($address) {
        $entities[$id] = $address;
      }
    }
    return $entities;
  }

  /**
   * Invokes load hook.
   *
   * This is a bit of a hack, because this makes the protected
   * method attachLoad() public by providing this wrapper method.
   * As this method is only called by the UcAddressesAddressBook
   * class, which takes control about address loading, we should
   * accept that the hack is needed.
   *
   * Please don't call this method in other places.
   *
   * @param array $entities
   *   An array of UcAddressesAddress instances.
   *
   * @return void
   */
  public function invokeLoad($entities) {
    if (!empty($entities)) {
      $this
        ->attachLoad($entities);
    }
  }

  /**
   * Overrides EntityAPIController::create().
   *
   * Creates a new address entity and makes sure it contains
   * some default values for it.
   *
   * @param array $values
   *   An array of values to set, keyed by property name.
   *
   * @return UcAddressesAddress
   *   A new instance of the entity type.
   */
  public function create(array $values = array()) {
    if (isset($values['uid'])) {

      // If the owner is given, an new address is created for the specific user.
      $entity = UcAddressesAddressBook::get($values['uid'])
        ->addAddress();
    }
    else {

      // Else, a new unowned address is given.
      $entity = UcAddressesAddressBook::newAddress();
    }
    $entity
      ->setMultipleFields($values);
    return $entity;
  }

  /**
   * Implements EntityAPIControllerInterface::save().
   *
   * Uses UcAddressesAddress save logics.
   *
   * @param $transaction
   *   (optional) a DatabaseTransaction object to use. Allows overrides to pass
   *   in their transaction object.
   *
   * @return void
   */
  public function save($entity, DatabaseTransaction $transaction = NULL) {
    $transaction = isset($transaction) ? $transaction : db_transaction();
    try {
      $result = $entity
        ->save();

      // Ignore slave server temporarily.
      db_ignore_slave();
      unset($entity->is_new);
      unset($entity->original);
    } catch (Exception $e) {
      $transaction
        ->rollback();
      watchdog_exception($this->entityType, $e);
      throw $e;
    }
    return $result;
  }

  /**
   * Implements EntityAPIControllerInterface::delete().
   *
   * Overridden to follow Ubercart Addresses logics.
   *
   * @return void
   */
  public function delete($ids, DatabaseTransaction $transaction = NULL) {
    $entities = $ids ? $this
      ->load($ids) : FALSE;
    if (!$entities) {

      // Do nothing, in case invalid or no ids have been passed.
      return;
    }
    try {
      foreach ($entities as $entity) {
        $entity
          ->delete();
      }

      // Ignore slave server temporarily.
      db_ignore_slave();
    } catch (Exception $e) {
      if (isset($transaction)) {
        $transaction
          ->rollback();
      }
      watchdog_exception($this->entityType, $e);
      throw $e;
    }
  }

  /**
   * Overrides EntityAPIController::buildContent().
   *
   * @return array
   *   The renderable array.
   */
  public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
    $address = $entity;
    $address_user = user_load($address
      ->getUserId());
    $options = array();

    // Check address access.
    if (UcAddressesPermissions::canViewAddress($address_user, $address)) {

      // Check if address may be edited too.
      if (UcAddressesPermissions::canEditAddress($address_user, $address)) {

        // Show edit link.
        $options['edit_link'] = TRUE;
      }
      if (UcAddressesPermissions::canDeleteAddress($address_user, $address)) {

        // Show delete link.
        $options['delete_link'] = TRUE;
      }
      $content['#theme'] = 'uc_addresses_list_address';
      $content['#address'] = $address;
      $content['#options'] = $options;
      $content['#attached'] = array(
        'css' => array(
          drupal_get_path('module', 'uc_addresses') . '/uc_addresses.css',
        ),
      );
    }
    else {
      $content['#access'] = FALSE;
    }
    return parent::buildContent($entity, $view_mode, $langcode, $content);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildQuery protected function Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery 1
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export 1
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::invoke public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::invoke 1
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController::resetCache 1
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIController::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::view 1
EntityAPIController::__construct public function Overridden. Overrides DrupalDefaultEntityController::__construct 1
UcAddressesEntityController::buildContent public function Overrides EntityAPIController::buildContent(). Overrides EntityAPIController::buildContent
UcAddressesEntityController::create public function Overrides EntityAPIController::create(). Overrides EntityAPIController::create
UcAddressesEntityController::delete public function Implements EntityAPIControllerInterface::delete(). Overrides EntityAPIController::delete
UcAddressesEntityController::invokeLoad public function Invokes load hook.
UcAddressesEntityController::load public function Implements DrupalEntityControllerInterface::load(). Overrides EntityAPIController::load
UcAddressesEntityController::save public function Implements EntityAPIControllerInterface::save(). Overrides EntityAPIController::save