Merge branch '7.x-1.x' of git.drupal.org:project/relation into 7.x-1.x
[project/relation.git] / views / relation_handler_relationship.inc
CommitLineData
690a082a
KN
1<?php
2
0cbf252c 3/**
4 * @file
5 * Views relationship support.
6 */
7
06b0cf1f
KN
8abstract class relation_handler_relationship_base extends views_handler_relationship {
9 function init(&$view, &$options) {
10 parent::init($view, $options);
11 $field = field_info_field('endpoints');
12 $this->relation_data_table_name = _field_sql_storage_tablename($field);
13 $this->entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
14 $this->entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');
15 $this->r_index_field_name = _field_sql_storage_columnname('endpoints', 'r_index');
16 }
17
690a082a 18 /**
42905f30
KN
19 * Define r_index option.
20 */
21 function option_definition() {
22 $options = parent::option_definition();
6d4fd43b 23 $options['r_index'] = array('default' => -1);
cab16c0c 24 return $options;
42905f30
KN
25 }
26
27 /**
28 * Let the user choose r_index.
29 */
30 function options_form(&$form, &$form_state) {
31 parent::options_form($form, $form_state);
32
33 $options = $this->options_form_summary_options();
34 if ($this->definition['directional']) {
35 $form['r_index'] = array(
36 '#type' => 'select',
37 '#options' => $options,
33272867 38 '#title' => t('Position of the relationship base'),
42905f30 39 '#default_value' => $this->options['r_index'],
c9121485
KN
40 // check_plain()'d in the definition.
41 '#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
42 );
43 }
44 }
45
46 /**
47 * Return the main options, which are shown in the summary title.
48 */
49 function options_form_summary_options() {
50 return $this->definition['directional'] ? array(
51 -1 => t('Any'),
52 0 => t('Source'),
53 1 => t('Target'),
54 ) : array();
55 }
06b0cf1f 56}
42905f30 57
06b0cf1f 58class relation_handler_relationship extends relation_handler_relationship_base {
eb4575d4 59 function query() {
eb4575d4
KN
60 $this->ensure_my_table();
61 $type = empty($this->options['required']) ? 'LEFT' : 'INNER';
690a082a 62
eb4575d4
KN
63 // Join the left table with the entity type to the endpoints field data table.
64 $join = new views_join();
65 $join->definition = array(
66 'left_table' => $this->table_alias,
67 'left_field' => $this->real_field,
06b0cf1f
KN
68 'table' => $this->relation_data_table_name,
69 'field' => $this->entity_id_field_name,
eb4575d4
KN
70 'type' => $type,
71 'extra' => array(
72 array(
73 'field' => 'bundle',
06b0cf1f 74 'value' => $this->definition['relation_type'],
eb4575d4
KN
75 ),
76 array(
06b0cf1f
KN
77 'field' => $this->entity_type_field_name,
78 'value' => $this->definition['entity_type_left'],
eb4575d4
KN
79 ),
80 ),
81 );
82 if ($this->definition['directional'] && $this->options['r_index'] > -1) {
83 $join->definition['extra'][] = array(
06b0cf1f 84 'field' => $this->r_index_field_name,
eb4575d4
KN
85 'value' => $this->options['r_index'],
86 );
87 }
88 $join->construct();
89 $join->adjusted = TRUE;
06b0cf1f 90 $l = $this->query->add_table($this->relation_data_table_name, $this->relationship, $join);
5e198c72 91
eb4575d4
KN
92 // Execute a self-join.
93 $join = new views_join();
94 $join->definition = array(
95 'left_table' => $l,
96 'left_field' => 'entity_id',
06b0cf1f 97 'table' => $this->relation_data_table_name,
eb4575d4
KN
98 'field' => 'entity_id',
99 'type' => $type,
100 'extra' => array(
101 array(
06b0cf1f 102 'field' => $this->entity_type_field_name,
eb4575d4
KN
103 'value' => $this->definition['entity_type_right'],
104 ),
105 ),
106 );
107 $join->construct();
108 $join->adjusted = TRUE;
06b0cf1f 109 $r = $this->query->add_table($this->relation_data_table_name, $this->relationship, $join);
77c38430 110
eb4575d4
KN
111 $join = new views_join();
112 $join->definition = array(
113 'left_table' => $r,
06b0cf1f 114 'left_field' => $this->entity_id_field_name,
eb4575d4
KN
115 'table' => $this->definition['base'],
116 'field' => $this->definition['base field'],
117 'type' => $type,
118 );
119 // This technically should be on an earlier extra but this works just
120 // fine.
121 if ($this->definition['entity_type_left'] == $this->definition['entity_type_right']) {
06b0cf1f 122 $join->definition['extra'] = "$l.$this->r_index_field_name != $r.$this->r_index_field_name";
77c38430 123 }
eb4575d4
KN
124 $join->construct();
125 $join->adjusted = TRUE;
126 // use a short alias for this:
127 $alias = $this->definition['base'] . '_' . $this->table;
128 $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
129 }
130}
77c38430 131
06b0cf1f 132class relation_handler_relationship_left extends relation_handler_relationship_base {
28e651b3
KN
133}
134
06b0cf1f 135class relation_handler_relationship_right extends relation_handler_relationship_base {
5e198c72 136}