function tripal_views_query::execute

3.x tripal_views_query.inc tripal_views_query::execute(&$view)

Parameters

$view:

File

tripal/tripal_views_query.inc, line 198

Class

tripal_views_query

Code

function execute(&$view) {
  $query = $view->build_info['query'];
  $cquery = $view->build_info['count_query'];

  if ($query) {
    $start = microtime(TRUE);

    try {

      if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
        // TODO: The code below was taken from the
        // views_plugin_pager::execute_count_query($count_query) which would
        // be called here, but that function expects the query is a
        // database query rather than a TripalEntityField query.  We
        // really should create a new tripal_views_plugin_pager class
        // and call the corresponding function here, but due to time
        // constraints this is the shortcut.
        $total_items = $cquery->execute();
        $this->pager->total_items = $total_items;
        if (!empty($this->pager->options['offset'])) {
          $this->pager->total_items -= $this->pager->options['offset'];
        }
        $this->pager->update_page_info();
      }

      // TODO: we need to implement a new views_plugin_pager class to
      // override the pre_execute to set the range, instead we'll just do
      // it manully here until we have the class.
      $this->pager->pre_execute($query);
      $num_items_per_page = $this->pager->get_items_per_page();
      $offset = $this->pager->get_current_page() * $num_items_per_page;
      // I'm not sure why an offset would come back as -1 but it has happened
      // with Drupal Views.  This is a quick band-aid fix.
      $offset = ($offset < 0) ? 0 : $offset;
      $query->range($offset, $num_items_per_page);

      // Get the IDs
      $results = $query->execute();
      $entity_ids = (isset($results['TripalEntity']) AND is_array($results['TripalEntity'])) ? array_keys($results['TripalEntity']) : array();

      $this->pager->post_execute($view->result);
      if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
        $view->total_rows = $this->pager->get_total_items();
      }

      // Get the fields to attach to the entity
      $fields = array();
      $field_ids = array();
      foreach ($this->fields as $details) {
        $field_name = $details['field_name'];
        // If the field_name comes to us with a period in it then it means that
        // we need to separate the field name from sub-element names.
        $matches = array();
        if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
          $field_name = $matches[1];
          $element_name = $matches[2];
        }
        $field = field_info_field($field_name);
        if ($field) {
          $fields[$field_name] = $field;
          $field_ids[] = $field['id'];
        }
      }

      // Get the entity IDs from the query.
      $entities = tripal_load_entity('TripalEntity', $entity_ids, FALSE, $field_ids);
      $i = 0;
      foreach ($entities as $entity_id => $entity) {
        $view->result[$i] = new stdClass();
        foreach ($this->fields as $details) {
          $field_name = $details['field_name'];
          // The entity_id and link fields are not true fields. They are
          // added by the tripal_views_data_tripal_entity() function to provide
          // useful fields to reference entities. If we see these
          // we need to support them here by giving them values.
          if ($field_name == 'entity_id') {
            $view->result[$i]->$field_name = $entity;
            continue;
          }
          if ($field_name == 'link') {
            $view->result[$i]->$field_name = $entity;
            continue;
          }
          if ($field_name == 'edit_link') {
            $view->result[$i]->$field_name = $entity;
            continue;
          }
          if ($field_name == 'delete_link') {
            $view->result[$i]->$field_name = $entity;
            continue;
          }
          if ($field_name == 'status') {
            $view->result[$i]->$field_name = $entity->status;
            continue;
          }

          // If the field_name comes to us with a period in it then it means that
          // we need to separate the field name from sub-element names.
          $matches = array();
          if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
            $field_name = $matches[1];
            $element_name = $matches[2];
          }

          if (array_key_exists($field_name, $fields)) {
            $items = field_get_items('TripalEntity', $entity, $field_name);
            $view->result[$i]->$field_name = $items;
          }
        }
        // Always add the entity to the results so that handlers
        // can take advantage of it.
        $view->result[$i]->entity = $entity;
        $i++;
      }
    }
    catch (Exception $e) {
      $view->result = array();
      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()));
      }
    }
  }
  else {
    $start = microtime(TRUE);
  }
  $view->execute_time = microtime(TRUE) - $start;
}