Removing entity_id -- use a non-required relationship instead. Now it's indexed enough.
[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 8class relation_handler_relationship extends views_handler_relationship {
690a082a 9 /**
42905f30
KN
10 * Define r_index option.
11 */
12 function option_definition() {
13 $options = parent::option_definition();
6d4fd43b 14 $options['r_index'] = array('default' => -1);
42905f30
KN
15 return $options;
16 }
17
18 /**
19 * Let the user choose r_index.
20 */
21 function options_form(&$form, &$form_state) {
22 parent::options_form($form, $form_state);
23
24 $options = $this->options_form_summary_options();
25 if ($this->definition['directional']) {
26 $form['r_index'] = array(
27 '#type' => 'select',
28 '#options' => $options,
33272867 29 '#title' => t('Position of the relationship base'),
42905f30 30 '#default_value' => $this->options['r_index'],
33272867 31 '#description' => t('Select whether the entity you are adding the relationship to is source or target of @relation_type_label relation.', array('@relation_type_label' => $this->definition['label'])),
42905f30
KN
32 );
33 }
34 }
35
36 /**
37 * Return the main options, which are shown in the summary title.
38 */
39 function options_form_summary_options() {
40 return $this->definition['directional'] ? array(
41 -1 => t('Any'),
42 0 => t('Source'),
43 1 => t('Target'),
44 ) : array();
45 }
6d4fd43b 46}
42905f30 47
6d4fd43b 48class relation_handler_join extends views_join {
42905f30 49 /**
690a082a
KN
50 * Build the SQL for the join this object represents.
51 */
52 function build_join($select_query, $table, $view_query) {
feb3e358
KN
53 $field = field_info_field('endpoints');
54 $relation_data_table_name = _field_sql_storage_tablename($field);
55 $entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
56 $entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');
57 $r_index_field_name = _field_sql_storage_columnname('endpoints', 'r_index');
690a082a
KN
58 // Join the left table with the entity type to the relation_data table.
59 $left = $view_query->get_table_info($this->left_table);
60 $entity_type_left = $this->definition['entity_type_left'];
feb3e358 61 $conditions = "$left[alias].$this->left_field = %alias.$entity_id_field_name AND %alias.$entity_type_field_name = '$entity_type_left'";
42905f30
KN
62 if ($this->definition['directional'] && $this->options['r_index'] > -1) {
63 $conditions .= " AND %alias.$r_index_field_name = " . $this->options['r_index'];
9167962e 64 }
51fccfd7 65 // Left join alias.
36c09e04 66 $l = $select_query->addJoin($this->type, $relation_data_table_name, NULL, $conditions);
690a082a 67
9167962e 68 // Execute a self-join.
690a082a 69 $entity_type_right = $this->definition['entity_type_right'];
6d4fd43b 70 // entity_id here is the ID of the relation entity.
36c09e04 71 $relation_type = $this->definition['relation_type'];
6d4fd43b 72 $conditions = "%alias.entity_id = $l.entity_id AND %alias.$r_index_field_name != $l.$r_index_field_name AND %alias.$entity_type_field_name = '$entity_type_right' AND %alias.bundle = '$relation_type'";
51fccfd7 73 // Right join alias.
36c09e04 74 $r = $select_query->addJoin($this->type, $relation_data_table_name, NULL, $conditions);
690a082a
KN
75
76 // Join the right table to the relation_data table.
feb3e358 77 $conditions = "%alias.$this->field = $r.$entity_id_field_name";
36c09e04 78 $select_query->addJoin($this->type, $table['table'], $table['alias'], $conditions);
690a082a
KN
79 }
80}