Projet

Général

Profil

Paste
Télécharger (3,55 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_fields_compare.inc @ 6eb8d15f

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_fields_compare.
6
 */
7

    
8
/**
9
 * A handler to filter a view using fields comparison.
10
 *
11
 * @ingroup views_filter_handlers
12
 */
13

    
14
class views_handler_filter_fields_compare extends views_handler_filter {
15

    
16
  function can_expose() {
17
    return FALSE;
18
  }
19

    
20
  /**
21
   * Overrides views_handler_filter#option_definition().
22
   */
23
  function option_definition() {
24
    $options = parent::option_definition();
25

    
26
    $options['left_field'] = $options['right_field'] = array('default' => '');
27

    
28
    return $options;
29
  }
30

    
31
  /**
32
   * Provide a list of all operators.
33
   */
34
  function fields_operator_options() {
35
    return array(
36
      '<' => t('Is less than'),
37
      '<=' => t('Is less than or equal to'),
38
      '=' => t('Is equal to'),
39
      '<>' =>  t('Is not equal to'),
40
      '>=' => t('Is greater than or equal to'),
41
      '>' => t('Is greater than')
42
    );
43
  }
44

    
45
  /**
46
   * Provide a list of available fields.
47
   */
48
  function field_options() {
49
    $options = array();
50

    
51
    $field_handlers = $this->view->display_handler->get_handlers('field');
52
    foreach ($field_handlers as $field => $handler) {
53
      if ($handler->table != 'views') {
54
        $options[$field] = $handler->ui_name();
55
      }
56
    }
57

    
58
    return $options;
59
  }
60

    
61
  /**
62
   * Overrides views_handler_filter#options_form().
63
   */
64
  function options_form(&$form, &$form_state) {
65
    parent::options_form($form, $form_state);
66

    
67
    $field_options = $this->field_options();
68

    
69
    $form['left_field'] = array(
70
      '#type' => 'select',
71
      '#title' => t('Left field'),
72
      '#default_value' => $this->options['left_field'],
73
      '#options' => $field_options,
74
      '#weight' => -3,
75
    );
76

    
77
    $form['operator'] = array(
78
      '#type' => 'select',
79
      '#title' => t('Operator'),
80
      '#default_value' => $this->options['operator'],
81
      '#options' => $this->fields_operator_options(),
82
      '#weight' => -2,
83
    );
84

    
85
    $form['right_field'] = array(
86
      '#type' => 'select',
87
      '#title' => t('Right field'),
88
      '#default_value' => $this->options['right_field'],
89
      '#options' => $field_options,
90
      '#weight' => -1,
91
    );
92

    
93
  }
94

    
95
  /**
96
   * Overrides views_handler_filter#query().
97
   *
98
   * Build extra condition from existing fields (from existing joins).
99
   */
100
  function query() {
101
    $left = $this->options['left_field'];
102
    $right = $this->options['right_field'];
103

    
104
    // Get all existing field handlers.
105
    $field_handlers = $this->view->display_handler->get_handlers('field');
106

    
107
    // Make sure the selected fields still exist.
108
    if (!isset($field_handlers[$left], $field_handlers[$right])) {
109
      return;
110
    }
111

    
112
    // Get the left table and field.
113
    $left_handler = $field_handlers[$left];
114
    $left_handler->set_relationship();
115
    $left_table_alias = $this->query->ensure_table($left_handler->table, $left_handler->relationship);
116

    
117
    // Get the left table and field.
118
    $right_handler = $field_handlers[$right];
119
    $right_handler->set_relationship();
120
    $right_table_alias = $this->query->ensure_table($right_handler->table, $right_handler->relationship);
121

    
122
    // Build piece of SQL.
123
    $snippet =
124
      $left_table_alias . '.' . $left_handler->real_field .
125
      ' ' . $this->options['operator'] . ' ' .
126
      $right_table_alias . '.' . $right_handler->real_field;
127

    
128
    $this->query->add_where_expression($this->options['group'], $snippet);
129
  }
130

    
131
  /**
132
   * Overrides views_handler_filter#admin_summary().
133
   */
134
  function admin_summary() {
135
    return check_plain(
136
      $this->options['left_field'] . ' ' .
137
      $this->options['operator'] . ' ' .
138
      $this->options['right_field']
139
    );
140
  }
141

    
142
}