summaryrefslogtreecommitdiffstats
path: root/views/relation_handler_relationship.inc
blob: 17a8b841156a91e3c1f614fcc2434adb9022f7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

/**
 * @file
 * Views relationship support.
 */

class relation_handler_relationship extends views_handler_relationship {
  /**
   * Define r_index option.
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['r_index'] = array('default' => -1);
    return $options;  
  }

  /**
   * Let the user choose r_index.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);

    $options = $this->options_form_summary_options();
    if ($this->definition['directional']) {
      $form['r_index'] = array(
        '#type' => 'select',
        '#options' => $options,
        '#title' => t('Position of the left side entity'),
        '#default_value' => $this->options['r_index'],
      );
    }
  }

  /**
   * Return the main options, which are shown in the summary title.
   */
  function options_form_summary_options() {
    return $this->definition['directional'] ? array(
      -1 => t('Any'),
      0 => t('Source'),
      1 => t('Target'),
    ) : array();
  }
}

class relation_handler_join extends views_join {
  /**
   * Build the SQL for the join this object represents.
   */
  function build_join($select_query, $table, $view_query) {
    $field = field_info_field('endpoints');
    $relation_data_table_name = _field_sql_storage_tablename($field);
    $entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
    $entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');
    $r_index_field_name = _field_sql_storage_columnname('endpoints', 'r_index');
    // Join the left table with the entity type to the relation_data table.
    $left = $view_query->get_table_info($this->left_table);
    $entity_type_left = $this->definition['entity_type_left'];
    $conditions = "$left[alias].$this->left_field = %alias.$entity_id_field_name AND %alias.$entity_type_field_name = '$entity_type_left'";
    if ($this->definition['directional'] && $this->options['r_index'] > -1) {
      $conditions .= " AND %alias.$r_index_field_name = " . $this->options['r_index'];
    }
    // Left join alias.
    $l = $select_query->addJoin($this->type, $relation_data_table_name, NULL, $conditions);

    // Execute a self-join.
    $entity_type_right = $this->definition['entity_type_right'];
    // entity_id here is the ID of the relation entity. 
    $relation_type = $this->definition['relation_type'];
    $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'";
    // Right join alias.
    $r = $select_query->addJoin($this->type, $relation_data_table_name, NULL, $conditions);

    // Join the right table to the relation_data table.
    $conditions = "%alias.$this->field = $r.$entity_id_field_name";
    $select_query->addJoin($this->type, $table['table'], $table['alias'], $conditions);
  }
}