You are here

function efq_views_plugin_query::execute in EntityFieldQuery Views Backend 7

Executes the query and fills the associated view object with according values.

Values to set: $view->result, $view->total_rows, $view->execute_time, $view->pager['current_page'].

Overrides views_plugin_query::execute

File

./efq_views_plugin_query.inc, line 139

Class

efq_views_plugin_query

Code

function execute(&$view) {
  $query = $view->build_info['efq_query'];
  $count_query = $view->build_info['count_query'];
  $args = $view->build_info['query_args'];
  $query
    ->addMetaData('view', $view);
  $count_query
    ->addMetaData('view', $view);

  // Add the query tags.
  if (!empty($this->options['query_tags'])) {
    foreach ($this->options['query_tags'] as $tag) {
      $query
        ->addTag($tag);
      $count_query
        ->addTag($tag);
    }
  }
  $start = microtime(true);

  // Determine if the query entity type is local or remote.
  $remote = FALSE;
  $entity_controller = entity_get_controller($this->entity_type);
  if (module_exists('remote_entity') && is_a($entity_controller, 'RemoteEntityAPIDefaultController')) {

    // We're dealing with a remote entity so get the fully loaded list of
    // entities from its query class instead of EntityFieldQuery.
    $remote = TRUE;
    $remote_query = $entity_controller
      ->getRemoteEntityQuery();
    $remote_query
      ->buildFromEFQ($query);
    $remote_entities = $entity_controller
      ->executeRemoteEntityQuery($remote_query);
  }

  // if we are using the pager, calculate the total number of results
  if ($this->pager
    ->use_pager()) {
    try {

      //  Fetch number of pager items differently based on data locality.
      if ($remote) {

        // Count the number of items already received in the remote query.
        $this->pager->total_items = count($remote_entities);
      }
      else {

        /* !$remote */

        // Execute the local count query.
        $this->pager->total_items = $count_query
          ->execute();
      }
      if (!empty($this->pager->options['offset'])) {
        $this->pager->total_items -= $this->pager->options['offset'];
      }
      $this->pager
        ->update_page_info();
    } catch (Exception $e) {
      if (!empty($view->simpletest)) {
        throw $e;
      }

      // Show the full exception message in Views admin.
      if (!empty($view->live_preview)) {
        drupal_set_message($e
          ->getMessage(), 'error');
      }
      else {
        vpr('Exception in @human_name[@view_name]: @message', array(
          '@human_name' => $view->human_name,
          '@view_name' => $view->name,
          '@message' => $e
            ->getMessage(),
        ));
      }
      return;
    }
  }

  // Let the pager set limit and offset.
  $this->pager
    ->pre_execute($query, $args);
  if (!empty($this->limit) || !empty($this->offset)) {

    // We can't have an offset without a limit, so provide a very large limit instead.
    $limit = intval(!empty($this->limit) ? $this->limit : 999999);
    $offset = intval(!empty($this->offset) ? $this->offset : 0);

    // Set the range for the query.
    if ($remote) {

      // The remote query was already executed so slice the results as necessary.
      $remote_entities = array_slice($remote_entities, $offset, $limit, TRUE);
    }
    else {

      /* !$remote */

      // Set the range on the local query.
      $query
        ->range($offset, $limit);
    }
  }
  $view->result = array();
  try {

    // Populate the result array.
    if ($remote) {

      // Give each entity its ID and add it to the result array.
      foreach ($remote_entities as $entity_id => $entity) {
        $entity->entity_id = $entity_id;
        $entity->entity_type = $this->entity_type;
        $view->result[] = $entity;
      }
    }
    else {

      /* !$remote */

      // Execute the local query.
      $results = $query
        ->execute();

      // Load each entity, give it its ID, and then add to the result array.
      foreach ($results as $entity_type => $ids) {

        // This is later used for field rendering
        foreach (entity_load($entity_type, array_keys($ids)) as $entity_id => $entity) {
          $entity->entity_id = $entity_id;
          $entity->entity_type = $entity_type;
          $view->result[] = $entity;
        }
      }
    }
    $this->pager
      ->post_execute($view->result);
    if ($this->pager
      ->use_pager()) {
      $view->total_rows = $this->pager
        ->get_total_items();
    }
  } catch (Exception $e) {

    // Show the full exception message in Views admin.
    if (!empty($view->live_preview)) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
    else {
      vpr('Exception in @human_name[@view_name]: @message', array(
        '@human_name' => $view->human_name,
        '@view_name' => $view->name,
        '@message' => $e
          ->getMessage(),
      ));
    }
    return;
  }
  $view->execute_time = microtime(true) - $start;
}