Issue #1165286 by naught101: Changed Label->Name everywhere, fixes machine_name....
[project/relation.git] / views / relation_handler_relationship.inc
CommitLineData
690a082a
KN
1<?php
2
0cbf252c 3/**
4 * @file
5 * Views relationship support.
6 */
7
690a082a
KN
8
9class relation_handler_relationship extends views_handler_relationship {
10}
11
12class relation_handler_join extends views_join {
13 /**
14 * Build the SQL for the join this object represents.
15 */
16 function build_join($select_query, $table, $view_query) {
feb3e358
KN
17 $field = field_info_field('endpoints');
18 $relation_data_table_name = _field_sql_storage_tablename($field);
19 $entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
20 $entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');
21 $r_index_field_name = _field_sql_storage_columnname('endpoints', 'r_index');
b7b45f05 22 $directional = $this->definition['directional'];
690a082a
KN
23 // Join the left table with the entity type to the relation_data table.
24 $left = $view_query->get_table_info($this->left_table);
25 $entity_type_left = $this->definition['entity_type_left'];
feb3e358 26 $conditions = "$left[alias].$this->left_field = %alias.$entity_id_field_name AND %alias.$entity_type_field_name = '$entity_type_left'";
9167962e 27 if ($directional) {
feb3e358 28 $conditions .= " AND %alias.$r_index_field_name = 0";
9167962e 29 }
51fccfd7 30 // Left join alias.
feb3e358 31 $l = $select_query->innerJoin($relation_data_table_name, NULL, $conditions);
690a082a 32
9167962e 33 // Execute a self-join.
690a082a 34 $entity_type_right = $this->definition['entity_type_right'];
feb3e358
KN
35 // entity_id here is the ID of the relation entity. delta or
36 // $r_index_field_name does not matter.
37 $conditions = "%alias.entity_id = $l.entity_id AND %alias.delta != $l.delta AND %alias.$entity_type_field_name = '$entity_type_right'";
51fccfd7 38 // Right join alias.
feb3e358 39 $r = $select_query->innerJoin($relation_data_table_name, NULL, $conditions);
690a082a
KN
40
41 // Join the right table to the relation_data table.
feb3e358 42 $conditions = "%alias.$this->field = $r.$entity_id_field_name";
95baca69 43 $select_query->innerJoin($table['table'], $table['alias'], $conditions);
690a082a
KN
44 }
45}