Project

General

Profile

Paste
Download (3.46 KB) Statistics
| Branch: | Revision:

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

1
<?php
2

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

    
8
/**
9
 * Complex filter to handle filtering for many to one relationships.
10
 *
11
 * Examples: terms (many terms per node), roles (many roles per user).
12
 *
13
 * The construct method needs to be overridden to provide a list of options;
14
 * alternately, the value_form and admin_summary methods need to be overriden
15
 * to provide something that isn't just a select list.
16
 *
17
 * @ingroup views_filter_handlers
18
 */
19
class views_handler_filter_many_to_one extends views_handler_filter_in_operator {
20

    
21
  /**
22
   * @var views_many_to_one_helper
23
   *
24
   * Stores the Helper object which handles the many_to_one complexity.
25
   */
26
  public $helper = NULL;
27

    
28
  /**
29
   *
30
   */
31
  public $value_form_type = 'select';
32

    
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public function init(&$view, &$options) {
37
    parent::init($view, $options);
38
    $this->helper = new views_many_to_one_helper($this);
39
  }
40

    
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public function option_definition() {
45
    $options = parent::option_definition();
46

    
47
    $options['operator']['default'] = 'or';
48
    $options['value']['default'] = array();
49

    
50
    if (isset($this->helper)) {
51
      $this->helper->option_definition($options);
52
    }
53
    else {
54
      $helper = new views_many_to_one_helper($this);
55
      $helper->option_definition($options);
56
    }
57

    
58
    return $options;
59
  }
60

    
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function operators() {
65
    $operators = array(
66
      'or' => array(
67
        'title' => t('Is one of'),
68
        'short' => t('or'),
69
        'short_single' => t('='),
70
        'method' => 'op_helper',
71
        'values' => 1,
72
        'ensure_my_table' => 'helper',
73
      ),
74
      'and' => array(
75
        'title' => t('Is all of'),
76
        'short' => t('and'),
77
        'short_single' => t('='),
78
        'method' => 'op_helper',
79
        'values' => 1,
80
        'ensure_my_table' => 'helper',
81
      ),
82
      'not' => array(
83
        'title' => t('Is none of'),
84
        'short' => t('not'),
85
        'short_single' => t('<>'),
86
        'method' => 'op_helper',
87
        'values' => 1,
88
        'ensure_my_table' => 'helper',
89
      ),
90
    );
91
    // if the definition allows for the empty operator, add it.
92
    if (!empty($this->definition['allow empty'])) {
93
      $operators += array(
94
        'empty' => array(
95
          'title' => t('Is empty (NULL)'),
96
          'method' => 'op_empty',
97
          'short' => t('empty'),
98
          'values' => 0,
99
        ),
100
        'not empty' => array(
101
          'title' => t('Is not empty (NOT NULL)'),
102
          'method' => 'op_empty',
103
          'short' => t('not empty'),
104
          'values' => 0,
105
        ),
106
      );
107
    }
108

    
109
    return $operators;
110
  }
111

    
112
  /**
113
   * {@inheritdoc}
114
   */
115
  public function value_form(&$form, &$form_state) {
116
    parent::value_form($form, $form_state);
117

    
118
    if (empty($form_state['exposed'])) {
119
      $this->helper->options_form($form, $form_state);
120
    }
121
  }
122

    
123
  /**
124
   * Override ensure_my_table so we can control how this joins in.
125
   * The operator actually has influence over joining.
126
   */
127
  public function ensure_my_table() {
128
    // Defer to helper if the operator specifies it.
129
    $info = $this->operators();
130
    if (isset($info[$this->operator]['ensure_my_table']) && $info[$this->operator]['ensure_my_table'] == 'helper') {
131
      return $this->helper->ensure_my_table();
132
    }
133

    
134
    return parent::ensure_my_table();
135
  }
136

    
137
  /**
138
   * {@inheritdoc}
139
   */
140
  public function op_helper() {
141
    if (empty($this->value)) {
142
      return;
143
    }
144
    $this->helper->add_filter();
145
  }
146

    
147
}