Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_fields_compare.inc @ 4003efde

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
class views_handler_filter_fields_compare extends views_handler_filter {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function can_expose() {
19
    return FALSE;
20
  }
21

    
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public function option_definition() {
26
    $options = parent::option_definition();
27

    
28
    $options['left_field'] = $options['right_field'] = array('default' => '');
29

    
30
    return $options;
31
  }
32

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

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

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

    
60
    return $options;
61
  }
62

    
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public function options_form(&$form, &$form_state) {
67
    parent::options_form($form, $form_state);
68

    
69
    $field_options = $this->field_options();
70

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

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

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

    
95
  }
96

    
97
  /**
98
   * {@inheritdoc}
99
   */
100
  public function query() {
101
    // Build extra condition from existing fields (from existing joins).
102
    $left = $this->options['left_field'];
103
    $right = $this->options['right_field'];
104

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

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

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

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

    
123
    // Build piece of SQL.
124
    $snippet = $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
   * {@inheritdoc}
133
   */
134
  public 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
}