You are here

class PartyController in Party 7

Same name and namespace in other branches
  1. 8.2 includes/party.entity.inc \PartyController

The API controller class for the Party entity.

Hierarchy

Expanded class hierarchy of PartyController

1 string reference to 'PartyController'
party_entity_info in ./party.module
Implements hook_entity_info().

File

includes/party.entity.inc, line 11
Contains the controller classes for Party entities.

View source
class PartyController extends EntityAPIController {

  /**
   * Flag to indicate base table properties have been updated.
   * @var int
   */
  const UPDATE_PROPERTY = 0x1;

  /**
   * Flag to indicate fields have been updated requiring field attach invokes.
   * @var int
   */
  const UPDATE_FIELDS = 0x2;

  /**
   * Flag to indicate other things been updated which may require a full save.
   * @var int
   */
  const UPDATE_OTHER = 0x4;

  /**
   * Overrides EntityAPIController::invoke().
   *
   * Add in our additional logic for party labels. This is in here rather than
   * PartyController::save() so that we can get the label set as early as
   * possible. The tricky case is when a plugin requires the party to be saved
   * or we fall back to pid which also requires the party to be saved. For
   * those cases, we need to update the label immediately after we write the
   * record but before we invoke modules' implementation of
   * hook_entity_insert(). However, in the more common use case, we ideally
   * want the label to be set before hook_entity_presave() is invoked.
   */
  public function invoke($hook, $party) {

    // In presave, trigger our label and primary fields update without a save.
    if ($hook == 'presave' && empty($party->primary_fields_updated)) {
      $this
        ->setPrimaryFields($party, FALSE);
    }
    elseif (in_array($hook, array(
      'insert',
      'update',
    )) && !empty($party->primary_fields_updated)) {
      unset($party->primary_fields_updated);
    }

    // In insert our primary fields ran before there was a pid, so we re-run
    // them in case we were dependant on a pid.
    if ($hook == 'insert') {

      // Set the party_attaching_party flag on any attached entities.
      foreach ($party->data_set_controllers as $controller) {
        foreach ($controller
          ->getEntities() as $entity) {
          $entity->party_attaching_party = $party->pid;
        }
      }

      // Set primary fields, storing any properties but deferring other stores.
      $needs_store = $this
        ->setPrimaryFields($party, FALSE);
      if ($needs_store & PartyController::UPDATE_PROPERTY) {
        drupal_write_record($this->entityInfo['base table'], $party, $this->idKey);
      }
    }

    // Trigger the rest of the invoke action.
    parent::invoke($hook, $party);
    if ($hook == 'insert' && $needs_store & PartyController::UPDATE_OTHER) {
      unset($party->is_new);
      $party
        ->save();
    }
  }

  /**
   * Set the primary fields for the party.
   *
   * @param $party
   *   A party object.
   * @param bool|int $store
   *   Whether we should store any changes. TRUE means yes, FALSE means no. An
   *   integer comprised of the update flags (see return) can be used to specify
   *   whether to run specific stores.
   *
   * @return int
   *   Whether changes requiring a store were made. This is a flag which can
   *   contain:
   *   - PartyController::UPDATE_PROPERTY
   *   - PartyController::UPDATE_FIELDS
   *   - PartyController::UPDATE_OTHER
   */
  public function setPrimaryFields($party, $store = TRUE) {
    $needs_store = 0;

    // Convert a TRUE into all our flags.
    if ($store === TRUE) {
      $store = PartyController::UPDATE_PROPERTY | PartyController::UPDATE_FIELDS | PartyController::UPDATE_OTHER;
    }

    // Find all primary fields and get their values.
    $party_wrapper = entity_metadata_wrapper('party', $party);
    foreach (PartyPrimaryFields::getFields() as $target => $sources) {

      // Get the original value.
      $original_value = $party_wrapper->{$target}
        ->value();
      $final_value = NULL;

      // Find the first one with a valid value.
      foreach ($sources as $source) {

        // Get hold of our source wrapper.
        $wrapper = NULL;

        // The party data set is a special case.
        if ($source['data_set'] == 'party') {
          $wrapper = $party_wrapper;
        }
        else {
          $controller = $party
            ->getDataSetController($source['data_set']);
          if ($entity = $controller
            ->getEntity()) {
            $wrapper = entity_metadata_wrapper($controller
              ->getDataInfo('entity type'), $entity);
          }
        }

        // If we have a wrapper, get hold of the value.
        if ($wrapper && isset($wrapper->{$source['property']})) {
          if (!empty($source['value'])) {
            $new_value = $wrapper->{$source['property']}
              ->value() ? $wrapper->{$source['property']}->{$source['value']}
              ->value() : NULL;
          }
          else {
            $new_value = $wrapper->{$source['property']}
              ->value();
          }

          // Pass on for any callbacks.
          $new_value = PartyPrimaryFields::executeCallback($new_value, $source, $target);
          if (!empty($new_value)) {

            // Set final value and end loop.
            $final_value = $new_value;
            break;
          }
        }
      }

      // Update party primary field with new value.
      if ($final_value !== $original_value) {
        $party_wrapper->{$target} = $final_value;

        // Check what kind of save we need.
        $target_info = $party_wrapper
          ->getPropertyInfo($target);
        if (!empty($target_info['field'])) {
          $needs_store = $needs_store | PartyController::UPDATE_FIELDS;
        }
        elseif (!empty($target_info['schema field'])) {
          $needs_store = $needs_store | PartyController::UPDATE_PROPERTY;
        }
        else {
          $needs_store = $needs_store | PartyController::UPDATE_OTHER;
        }
      }
    }

    // Allow modules to respond in case they need to do anything.
    drupal_alter('party_primary_fields', $party, $needs_store);

    // Check whether we need to store the changes.
    if ($store && $needs_store) {

      // If we need to do a full save, set a flag to prevent recursion and run.
      if ($store & $needs_store & PartyController::UPDATE_OTHER) {
        $party->primary_fields_updated = TRUE;
        $party
          ->save();
      }
      else {

        // Load up the original party.
        $party->original = entity_load_unchanged('party', $party->pid);

        // If we need to do a write record, do that.
        if ($store & $needs_store & PartyController::UPDATE_PROPERTY) {
          drupal_write_record($this->entityInfo['base table'], $party, $this->idKey);
        }

        // If we need to invoke a field update, do that.
        if ($store & $needs_store & PartyController::UPDATE_FIELDS) {
          $this
            ->invoke('update', $party);
        }
        else {
          module_invoke_all('entity_update', $party, 'party');
        }
      }
    }
    elseif ($store && module_exists('search_api')) {
      search_api_track_item_change('party', array(
        $party->pid,
      ));
    }
    return $needs_store;
  }

  /**
   * Overriding the buildContent function to add entity specific fields.
   */
  public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
    $content = parent::buildContent($entity, $view_mode, $langcode, $content);
    return $content;
  }

  /**
   * Get the data set controller for a given entity and data set.
   *
   * @param Party $party
   *  The party to get the data set for.
   * @param $data_set_name
   *  The name of a data set.
   *
   * @return
   *  The controller class, without attached entities loaded yet.
   */
  public function getDataSetController($entity, $data_set_name) {

    // If we don't already have our data set, load it.
    if (!isset($entity->data_set_controllers[$data_set_name])) {
      $sets = party_get_data_set_info();
      if (empty($sets[$data_set_name])) {
        throw new Exception(t("Party data set '@data_set_name' does not exist.", array(
          '@data_set_name' => $data_set_name,
        )));
      }
      else {
        if (!($class = $sets[$data_set_name]['class']) || !class_exists($class)) {
          throw new Exception(t("Party data controller '@class' for data set '@data_set_name' does not exist.", array(
            '@class' => $class,
            '@data_set_name' => $data_set_name,
          )));
        }
      }
      $entity->data_set_controllers[$data_set_name] = new $class($data_set_name, $entity);
    }
    return $entity->data_set_controllers[$data_set_name];
  }

}

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::create public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::create
EntityAPIController::delete public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::delete 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::load public function Overridden. Overrides DrupalDefaultEntityController::load 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::save public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::save 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
PartyController::buildContent public function Overriding the buildContent function to add entity specific fields. Overrides EntityAPIController::buildContent
PartyController::getDataSetController public function Get the data set controller for a given entity and data set.
PartyController::invoke public function Overrides EntityAPIController::invoke(). Overrides EntityAPIController::invoke
PartyController::setPrimaryFields public function Set the primary fields for the party.
PartyController::UPDATE_FIELDS constant Flag to indicate fields have been updated requiring field attach invokes.
PartyController::UPDATE_OTHER constant Flag to indicate other things been updated which may require a full save.
PartyController::UPDATE_PROPERTY constant Flag to indicate base table properties have been updated.