You are here

class FlexiformController in Flexiform 7

The Controller for FlexiformSubmission entities

Hierarchy

Expanded class hierarchy of FlexiformController

1 string reference to 'FlexiformController'
flexiform_entity_info in ./flexiform.module
Implement hook_entity_info().

File

./flexiform.entity.inc, line 108
flexiform.entity.inc

View source
class FlexiformController extends EntityAPIControllerExportable {
  public function __construct($entityType) {
    parent::__construct($entityType);
  }

  /**
   * Create a flexiform - we first set up the values that are specific
   * to our flexiform schema but then also go through the EntityAPIController
   * function.
   *
   * @param $type
   *   The machine-readable type of the flexiform_submission.
   *
   * @return
   *   A flexiform object with all default fields initialized.
   */
  public function create(array $values = array()) {

    // Add values that are specific to our FlexiformSubmission
    $values += array(
      'id' => '',
      'is_new' => TRUE,
    );
    $flexiform = parent::create($values);
    return $flexiform;
  }

  /**
   * Overrides EntityAPIController::save().
   */
  public function save($flexiform, DatabaseTransaction $transaction = NULL) {
    $return = parent::save($flexiform);
    $this
      ->storeDisplays($flexiform);
    $this
      ->storeTags($flexiform);
    return $return;
  }

  /**
   * Overrides EntityAPIController::invoke().
   */
  public function invoke($hook, $entity) {
    parent::invoke($hook, $entity);
    if ($hook == 'insert' || $hook == 'update') {

      // Get hold of the display info.
      $display_info = flexiform_display_info();

      // Check for any displays implementing hook_menu.
      foreach ($entity->displays as $id => $display) {
        if (method_exists($display_info[$id]['class'], 'hook_menu')) {
          if ($hook == 'update') {

            // If the config has changed continue.
            if (isset($entity->original->displays[$id]) && $display == $entity->original->displays[$id]) {
              continue;
            }
          }

          // Mark the menu to be rebuilt.
          variable_set('menu_rebuild_needed', TRUE);
          break;
        }
      }
    }
  }

  /**
   * Overrides EntityAPIController::attachLoad().
   */
  public function attachLoad(&$queried_entities, $revision_id = FALSE) {
    $ids = array_keys($queried_entities);
    $tags = array();
    $result = db_select('flexiform_tags')
      ->fields('flexiform_tags', array(
      'id',
      'tag',
    ))
      ->condition('id', $ids, 'IN')
      ->execute();
    foreach ($result as $row) {
      $tags[$row->id][] = $row->tag;
    }
    foreach ($queried_entities as $entity_id => &$entity) {
      $entity->tags = !empty($tags[$entity_id]) ? $tags[$entity_id] : array();
    }
  }

  /**
   * Store the tags associated with a given flexiform.
   */
  protected function storeTags($flexiform) {
    db_delete('flexiform_tags')
      ->condition('id', $flexiform->id)
      ->execute();
    if (!empty($flexiform->tags)) {
      foreach ($flexiform->tags as $tag) {
        db_insert('flexiform_tags')
          ->fields(array(
          'id',
          'tag',
        ), array(
          $flexiform->id,
          $tag,
        ))
          ->execute();
      }
    }
  }

  /**
   * Store the displays associated with a given flexiform.
   */
  protected function storeDisplays($flexiform) {
    db_delete('flexiform_display')
      ->condition('form', $flexiform->form)
      ->execute();
    if (!empty($flexiform->displays)) {
      $query = db_insert('flexiform_display');
      $query
        ->fields(array(
        'id',
        'form',
        'display',
      ));
      foreach ($flexiform->displays as $key => $display) {
        if (($display = $flexiform
          ->getDisplay($key)) && $display
          ->isEnabled()) {
          $query
            ->values(array(
            'id' => $flexiform->id,
            'form' => $flexiform->form,
            'display' => $key,
          ));
        }
      }
      $query
        ->execute();
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
    $query = parent::buildQuery($ids, $conditions, $revision_id);
    $query_conditions =& $query
      ->conditions();
    foreach ($query_conditions as &$condition) {

      // One entry in $query_conditions is a string with key '#conjunction'.
      // @see QueryConditionInterface::conditions().
      if (is_array($condition)) {

        // Support using 'tags' => array('tag1', 'tag2') as condition.
        if ($condition['field'] == 'base.tags') {
          $query
            ->join('flexiform_tags', 'ft', 'base.id = ft.id');
          $condition['field'] = 'ft.tag';
        }
      }
    }
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($ids, DatabaseTransaction $transaction = NULL) {
    parent::delete($ids, $transaction);
    db_delete('flexiform_tags')
      ->condition('id', $ids)
      ->execute();
    db_delete('flexiform_display')
      ->condition('id', $ids)
      ->execute();
  }

  /**
   * Get flexiforms with a given display(s)
   */
  public function loadWithDisplay($display) {
    $query = db_select('flexiform_display');
    $query
      ->addField('flexiform_display', 'form');
    $query
      ->condition('display', $display);
    $query
      ->groupBy('form');
    $names = $query
      ->execute()
      ->fetchCol();
    return !empty($names) ? entity_load('flexiform', $names) : array();
  }

}

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::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::buildContent public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::buildContent
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIControllerExportable::$entityCacheByName protected property
EntityAPIControllerExportable::$nameKey protected property
EntityAPIControllerExportable::applyConditions protected function
EntityAPIControllerExportable::cacheGet protected function Overridden. Overrides DrupalDefaultEntityController::cacheGet
EntityAPIControllerExportable::cacheGetByName protected function Like cacheGet() but keyed by name.
EntityAPIControllerExportable::cacheSet protected function Overridden. Overrides DrupalDefaultEntityController::cacheSet
EntityAPIControllerExportable::export public function Overridden. Overrides EntityAPIController::export
EntityAPIControllerExportable::load public function Overridden to support passing numeric ids as well as names as $ids. Overrides EntityAPIController::load
EntityAPIControllerExportable::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides EntityAPIController::resetCache
EntityAPIControllerExportable::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::view
FlexiformController::attachLoad public function Overrides EntityAPIController::attachLoad(). Overrides EntityAPIControllerExportable::attachLoad
FlexiformController::buildQuery protected function Support loading by name key. Overrides EntityAPIControllerExportable::buildQuery
FlexiformController::create public function Create a flexiform - we first set up the values that are specific to our flexiform schema but then also go through the EntityAPIController function. Overrides EntityAPIController::create
FlexiformController::delete public function Overridden to care about reverted entities. Overrides EntityAPIControllerExportable::delete
FlexiformController::invoke public function Overrides EntityAPIController::invoke(). Overrides EntityAPIControllerExportable::invoke
FlexiformController::loadWithDisplay public function Get flexiforms with a given display(s)
FlexiformController::save public function Overrides EntityAPIController::save(). Overrides EntityAPIControllerExportable::save
FlexiformController::storeDisplays protected function Store the displays associated with a given flexiform.
FlexiformController::storeTags protected function Store the tags associated with a given flexiform.
FlexiformController::__construct public function Overridden. Overrides EntityAPIControllerExportable::__construct