You are here

function image_gallery_handler_field_gallery_cover::get_cover_node_nid in Image 6

Same name and namespace in other branches
  1. 7 contrib/image_gallery/views/image_gallery_handler_field_gallery_cover.inc \image_gallery_handler_field_gallery_cover::get_cover_node_nid()

Get the cover nid for the current tid.

This makes up our query from the field definition and handler functions, and then runs it according to the descendant and depth options given for the field.

3 calls to image_gallery_handler_field_gallery_cover::get_cover_node_nid()
image_gallery_handler_field_gallery_cover::render in contrib/image_gallery/views/image_gallery_handler_field_gallery_cover.inc
Returns field html. Just a dummy to inherit.
image_gallery_handler_field_gallery_cover_latest_time::render in contrib/image_gallery/views/image_gallery_handler_field_gallery_cover_latest_time.inc
Returns field html.
image_gallery_handler_field_gallery_cover_thumbnail::render in contrib/image_gallery/views/image_gallery_handler_field_gallery_cover_thumbnail.inc
Returns field html.

File

contrib/image_gallery/views/image_gallery_handler_field_gallery_cover.inc, line 96

Class

image_gallery_handler_field_gallery_cover
Parent class for field handlers that gives us a gallery cover.

Code

function get_cover_node_nid($values) {

  // The term id of the current gallery.
  // This comes straight from the database row so should be safe for
  // inclusion in a query.
  $tid = $values->tid;

  // Get the basic query but only once for all rows of the view.
  // This can be set in the handler definition, or in the handler class's
  // get_cover_node_nid_query function for more complex cases.
  // The handler definition takes priority over the function.
  // This is stored in the handler object so we only have to get it
  // once for the whole view.
  if (!isset($this->basic_query)) {
    if (isset($this->definition['basic query'])) {
      $this->basic_query = $this->definition['basic query'];
    }
    else {
      $this->basic_query = $this
        ->get_cover_node_nid_query();
    }
  }

  // If we don't have a query, return NULL now to prevent SQL errors.
  if (is_null($this->basic_query)) {
    return NULL;
  }

  // Add the definition's ORDER clause, if there is one.
  if ($this->definition['order clause']) {
    $this->basic_query = str_replace('***ORDER_CLAUSE***', $this->definition['order clause'], $this->basic_query);
  }

  // Look at the options set for this handler.
  $depth = $this->options['depth'];
  $descendants_method = $this->options['descendants'];

  // Depth of 0 is the same as 'single'.
  if ($depth === 0) {
    $descendants_method = 'single';
  }

  // Add our WHERE clause.
  switch ($descendants_method) {

    // Plain: only consider this gallery.
    // Recursive: only consider this gallery (we'll recurse later).
    case 'single':
    case 'recurse':
      $where_clause = "tn.tid = {$tid} ";
      $query = str_replace('***WHERE_CLAUSE***', $where_clause, $this->basic_query);
      break;

    // Flat: all descendant galleries considered for the cover query.
    case 'flat':
      if ($depth == 'all') {
        $tree = taxonomy_get_tree($this->vid, $tid);
      }
      else {
        $tree = taxonomy_get_tree($this->vid, $tid, -1, $depth);
      }
      $descendant_tids = array_merge(array(
        $tid,
      ), array_map('_taxonomy_get_tid_from_term', $tree));

      // The values of $descendant_tids should be safe for raw inclusion in the
      // SQL since they're all loaded from integer fields in the database.
      $where_clause = 'tn.tid IN (' . implode(',', $descendant_tids) . ')';
      $query = str_replace('***WHERE_CLAUSE***', $where_clause, $this->basic_query);
      break;
  }

  // Run the query to get the cover nid.
  if ($nid = db_result(db_query_range(db_rewrite_sql($query), '', 0, 1))) {
    return $nid;
  }

  // We have no nid: the gallery is empty. Go into descendant galleries if the
  // recurse option is set.
  if ($this->options['descendants'] == 'recurse' && $this->options['depth'] > 0) {
    $nid = $this
      ->get_cover_node_nid_recurse($tid, 1);
    return $nid;
  }
}