1 | <?php |
---|
2 | /** |
---|
3 | * An argument handler for use in fields that have a many to one relationship |
---|
4 | * with the table(s) to the left. This adds a bunch of options that are |
---|
5 | * reasonably common with this type of relationship. |
---|
6 | * Definition terms: |
---|
7 | * - numeric: If true, the field will be considered numeric. Probably should |
---|
8 | * always be set TRUE as views_handler_argument_string has many to one |
---|
9 | * capabilities. |
---|
10 | * - zero is null: If true, a 0 will be handled as empty, so for example |
---|
11 | * a default argument can be provided or a summary can be shown. |
---|
12 | * |
---|
13 | * @ingroup views_argument_handlers |
---|
14 | */ |
---|
15 | class views_handler_argument_many_to_one extends views_handler_argument { |
---|
16 | function init(&$view, $options) { |
---|
17 | parent::init($view, $options); |
---|
18 | $this->helper = new views_many_to_one_helper($this); |
---|
19 | |
---|
20 | // Ensure defaults for these, during summaries and stuff: |
---|
21 | $this->operator = 'or'; |
---|
22 | $this->value = array(); |
---|
23 | } |
---|
24 | |
---|
25 | function option_definition() { |
---|
26 | $options = parent::option_definition(); |
---|
27 | |
---|
28 | if (!empty($this->definition['numeric'])) { |
---|
29 | $options['break_phrase'] = array('default' => FALSE); |
---|
30 | } |
---|
31 | |
---|
32 | $options['add_table'] = array('default' => FALSE); |
---|
33 | $options['require_value'] = array('default' => FALSE); |
---|
34 | |
---|
35 | views_many_to_one_helper::option_definition($options); |
---|
36 | return $options; |
---|
37 | } |
---|
38 | |
---|
39 | function options_form(&$form, &$form_state) { |
---|
40 | parent::options_form($form, $form_state); |
---|
41 | |
---|
42 | // allow + for or, , for and |
---|
43 | if (!empty($this->definition['numeric'])) { |
---|
44 | $form['break_phrase'] = array( |
---|
45 | '#type' => 'checkbox', |
---|
46 | '#title' => t('Allow multiple terms per argument'), |
---|
47 | '#description' => t('If selected, users can enter multiple arguments in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'), |
---|
48 | '#default_value' => !empty($this->options['break_phrase']), |
---|
49 | ); |
---|
50 | } |
---|
51 | |
---|
52 | $form['add_table'] = array( |
---|
53 | '#type' => 'checkbox', |
---|
54 | '#title' => t('Allow multiple arguments to work together'), |
---|
55 | '#description' => t('If selected, multiple instances of this argument can work together, as though multiple terms were supplied to the same argument. This setting is not compatible with the "Reduce duplicates" setting.'), |
---|
56 | '#default_value' => !empty($this->options['add_table']), |
---|
57 | ); |
---|
58 | |
---|
59 | $form['require_value'] = array( |
---|
60 | '#type' => 'checkbox', |
---|
61 | '#title' => t('Do not display items with no value in summary'), |
---|
62 | '#default_value' => !empty($this->options['require_value']), |
---|
63 | ); |
---|
64 | |
---|
65 | $this->helper->options_form($form, $form_state); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Override ensure_my_table so we can control how this joins in. |
---|
70 | * The operator actually has influence over joining. |
---|
71 | */ |
---|
72 | function ensure_my_table() { |
---|
73 | $this->helper->ensure_my_table(); |
---|
74 | } |
---|
75 | |
---|
76 | function query() { |
---|
77 | $empty = FALSE; |
---|
78 | if (isset($this->definition['zero is null']) && $this->definition['zero is null']) { |
---|
79 | if (empty($this->argument)) { |
---|
80 | $empty = TRUE; |
---|
81 | } |
---|
82 | } |
---|
83 | else { |
---|
84 | if (!isset($this->argument)) { |
---|
85 | $empty = TRUE; |
---|
86 | } |
---|
87 | } |
---|
88 | if ($empty) { |
---|
89 | parent::ensure_my_table(); |
---|
90 | $this->query->add_where(0, "$this->table_alias.$this->real_field IS NULL"); |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | if (!empty($this->options['break_phrase'])) { |
---|
95 | views_break_phrase($this->argument, $this); |
---|
96 | } |
---|
97 | else { |
---|
98 | $this->value = array($this->argument); |
---|
99 | $this->operator = 'or'; |
---|
100 | } |
---|
101 | |
---|
102 | $this->helper->add_filter(); |
---|
103 | } |
---|
104 | |
---|
105 | function title() { |
---|
106 | if (!$this->argument) { |
---|
107 | return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized'); |
---|
108 | } |
---|
109 | |
---|
110 | if (!empty($this->options['break_phrase'])) { |
---|
111 | views_break_phrase($this->argument, $this); |
---|
112 | } |
---|
113 | else { |
---|
114 | $this->value = array($this->argument); |
---|
115 | $this->operator = 'or'; |
---|
116 | } |
---|
117 | |
---|
118 | // @todo -- both of these should check definition for alternate keywords. |
---|
119 | |
---|
120 | if (empty($this->value)) { |
---|
121 | return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized'); |
---|
122 | } |
---|
123 | |
---|
124 | if ($this->value === array(-1)) { |
---|
125 | return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input'); |
---|
126 | } |
---|
127 | |
---|
128 | return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query()); |
---|
129 | } |
---|
130 | |
---|
131 | function summary_query() { |
---|
132 | $field = $this->table . '.' . $this->field; |
---|
133 | $join = $this->get_join(); |
---|
134 | |
---|
135 | if (!empty($this->options['require_value'])) { |
---|
136 | $join->type = 'INNER'; |
---|
137 | } |
---|
138 | |
---|
139 | if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) { |
---|
140 | $this->table_alias = $this->query->ensure_table($this->table, $this->relationship, $join); |
---|
141 | } |
---|
142 | else { |
---|
143 | $this->table_alias = $this->helper->summary_join(); |
---|
144 | } |
---|
145 | |
---|
146 | // Add the field. |
---|
147 | $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field); |
---|
148 | |
---|
149 | $this->summary_name_field(); |
---|
150 | |
---|
151 | return $this->summary_basics(); |
---|
152 | } |
---|
153 | |
---|
154 | function summary_argument($data) { |
---|
155 | $value = $data->{$this->base_alias}; |
---|
156 | if (empty($value)) { |
---|
157 | $value = 0; |
---|
158 | } |
---|
159 | |
---|
160 | return $value; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Override for specific title lookups. |
---|
165 | */ |
---|
166 | function title_query() { |
---|
167 | return $this->value; |
---|
168 | } |
---|
169 | } |
---|