Moved the whole Views options thing to the right class, dereine patched Views for...
[project/relation.git] / views / relation_handler_relationship.inc
1 <?php
2
3 /**
4 * @file
5 * Views relationship support.
6 */
7
8 class relation_handler_relationship extends views_handler_relationship {
9 /**
10 * Define r_index option.
11 */
12 function option_definition() {
13 $options = parent::option_definition();
14 $options['r_index'] = array('default' => -1);
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,
29 '#title' => t('Position of the left side entity'),
30 '#default_value' => $this->options['r_index'],
31 );
32 }
33 }
34
35 /**
36 * Return the main options, which are shown in the summary title.
37 */
38 function options_form_summary_options() {
39 return $this->definition['directional'] ? array(
40 -1 => t('Any'),
41 0 => t('Source'),
42 1 => t('Target'),
43 ) : array();
44 }
45 }
46
47 class relation_handler_join extends views_join {
48 /**
49 * Build the SQL for the join this object represents.
50 */
51 function build_join($select_query, $table, $view_query) {
52 $field = field_info_field('endpoints');
53 $relation_data_table_name = _field_sql_storage_tablename($field);
54 $entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
55 $entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');
56 $r_index_field_name = _field_sql_storage_columnname('endpoints', 'r_index');
57 // Join the left table with the entity type to the relation_data table.
58 $left = $view_query->get_table_info($this->left_table);
59 $entity_type_left = $this->definition['entity_type_left'];
60 $conditions = "$left[alias].$this->left_field = %alias.$entity_id_field_name AND %alias.$entity_type_field_name = '$entity_type_left'";
61 if ($this->definition['directional'] && $this->options['r_index'] > -1) {
62 $conditions .= " AND %alias.$r_index_field_name = " . $this->options['r_index'];
63 }
64 // Left join alias.
65 $l = $select_query->addJoin($this->type, $relation_data_table_name, NULL, $conditions);
66
67 // Execute a self-join.
68 $entity_type_right = $this->definition['entity_type_right'];
69 // entity_id here is the ID of the relation entity.
70 $relation_type = $this->definition['relation_type'];
71 $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'";
72 // Right join alias.
73 $r = $select_query->addJoin($this->type, $relation_data_table_name, NULL, $conditions);
74
75 // Join the right table to the relation_data table.
76 $conditions = "%alias.$this->field = $r.$entity_id_field_name";
77 $select_query->addJoin($this->type, $table['table'], $table['alias'], $conditions);
78 }
79 }