1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Complex filter to handle filtering for many to one relationships, |
---|
5 | * such as terms (many terms per node) or roles (many roles per user). |
---|
6 | * |
---|
7 | * The construct method needs to be overridden to provide a list of options; |
---|
8 | * alternately, the value_form and admin_summary methods need to be overriden |
---|
9 | * to provide something that isn't just a select list. |
---|
10 | * |
---|
11 | * @ingroup views_filter_handlers |
---|
12 | */ |
---|
13 | class views_handler_filter_many_to_one extends views_handler_filter_in_operator { |
---|
14 | function init(&$view, $options) { |
---|
15 | parent::init($view, $options); |
---|
16 | $this->helper = new views_many_to_one_helper($this); |
---|
17 | } |
---|
18 | |
---|
19 | function option_definition() { |
---|
20 | $options = parent::option_definition(); |
---|
21 | |
---|
22 | $options['operator']['default'] = 'or'; |
---|
23 | $options['value']['default'] = array(); |
---|
24 | |
---|
25 | views_many_to_one_helper::option_definition($options); |
---|
26 | |
---|
27 | return $options; |
---|
28 | } |
---|
29 | |
---|
30 | function operators() { |
---|
31 | $operators = array( |
---|
32 | 'or' => array( |
---|
33 | 'title' => t('Is one of'), |
---|
34 | 'short' => t('or'), |
---|
35 | 'short_single' => t('='), |
---|
36 | 'method' => 'op_helper', |
---|
37 | 'values' => 1, |
---|
38 | 'ensure_my_table' => 'helper', |
---|
39 | ), |
---|
40 | 'and' => array( |
---|
41 | 'title' => t('Is all of'), |
---|
42 | 'short' => t('and'), |
---|
43 | 'short_single' => t('='), |
---|
44 | 'method' => 'op_helper', |
---|
45 | 'values' => 1, |
---|
46 | 'ensure_my_table' => 'helper', |
---|
47 | ), |
---|
48 | 'not' => array( |
---|
49 | 'title' => t('Is none of'), |
---|
50 | 'short' => t('not'), |
---|
51 | 'short_single' => t('<>'), |
---|
52 | 'method' => 'op_helper', |
---|
53 | 'values' => 1, |
---|
54 | 'ensure_my_table' => 'helper', |
---|
55 | ), |
---|
56 | ); |
---|
57 | // if the definition allows for the empty operator, add it. |
---|
58 | if (!empty($this->definition['allow empty'])) { |
---|
59 | $operators += array( |
---|
60 | 'empty' => array( |
---|
61 | 'title' => t('Is empty (NULL)'), |
---|
62 | 'method' => 'op_empty', |
---|
63 | 'short' => t('empty'), |
---|
64 | 'values' => 0, |
---|
65 | ), |
---|
66 | 'not empty' => array( |
---|
67 | 'title' => t('Is not empty (NOT NULL)'), |
---|
68 | 'method' => 'op_empty', |
---|
69 | 'short' => t('not empty'), |
---|
70 | 'values' => 0, |
---|
71 | ), |
---|
72 | ); |
---|
73 | } |
---|
74 | |
---|
75 | return $operators; |
---|
76 | } |
---|
77 | |
---|
78 | var $value_form_type = 'select'; |
---|
79 | function value_form(&$form, &$form_state) { |
---|
80 | parent::value_form($form, $form_state); |
---|
81 | |
---|
82 | if (empty($form_state['exposed'])) { |
---|
83 | $this->helper->options_form($form, $form_state); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Override ensure_my_table so we can control how this joins in. |
---|
89 | * The operator actually has influence over joining. |
---|
90 | */ |
---|
91 | function ensure_my_table() { |
---|
92 | // Defer to helper if the operator specifies it. |
---|
93 | $info = $this->operators(); |
---|
94 | if (isset($info[$this->operator]['ensure_my_table']) && $info[$this->operator]['ensure_my_table'] == 'helper') { |
---|
95 | return $this->helper->ensure_my_table(); |
---|
96 | } |
---|
97 | |
---|
98 | return parent::ensure_my_table(); |
---|
99 | } |
---|
100 | |
---|
101 | function op_helper() { |
---|
102 | if (empty($this->value)) { |
---|
103 | return; |
---|
104 | } |
---|
105 | $this->helper->add_filter(); |
---|
106 | } |
---|
107 | } |
---|