function views_handler_relationship_groupwise_max::query

Called to implement a relationship in a query. This is mostly a copy of our parent's query() except for this bit with the join class.

Overrides views_handler_relationship::query

File

handlers/views_handler_relationship_groupwise_max.inc, line 350

Class

views_handler_relationship_groupwise_max
Relationship handler that allows a groupwise maximum of the linked in table.

Code

public function query() {
    // Figure out what base table this relationship brings to the party.
    $table_data = views_fetch_data($this->definition['base']);
    $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
    $this->ensure_my_table();
    $def = $this->definition;
    $def['table'] = $this->definition['base'];
    $def['field'] = $base_field;
    $def['left_table'] = $this->table_alias;
    $def['left_field'] = $this->field;
    if (!empty($this->options['required'])) {
        $def['type'] = 'INNER';
    }
    if ($this->options['subquery_regenerate']) {
        // For testing only, regenerate the subquery each time.
        $def['left_query'] = $this->left_query($this->options);
    }
    else {
        // Get the stored subquery SQL string.
        $cid = 'views_relationship_groupwise_max:' . $this->view->name . ':' . $this->view->current_display . ':' . $this->options['id'];
        $cache = cache_get($cid, 'cache_views_data');
        if (isset($cache->data)) {
            $def['left_query'] = $cache->data;
        }
        else {
            $def['left_query'] = $this->left_query($this->options);
            cache_set($cid, $def['left_query'], 'cache_views_data');
        }
    }
    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
        $join = new $def['join_handler']();
    }
    else {
        $join = new views_join_subquery();
    }
    $join->definition = $def;
    $join->construct();
    $join->adjusted = TRUE;
    // Use a short alias for this.
    $alias = $def['table'] . '_' . $this->table;
    $this->alias = $this->query
        ->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
}