1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Basic argument handler to implement string arguments that may have length |
---|
5 | * limits. |
---|
6 | * |
---|
7 | * @ingroup views_argument_handlers |
---|
8 | */ |
---|
9 | class views_handler_argument_string extends views_handler_argument { |
---|
10 | function init(&$view, $options) { |
---|
11 | parent::init($view, $options); |
---|
12 | if (!empty($this->definition['many to one'])) { |
---|
13 | $this->helper = new views_many_to_one_helper($this); |
---|
14 | |
---|
15 | // Ensure defaults for these, during summaries and stuff: |
---|
16 | $this->operator = 'or'; |
---|
17 | $this->value = array(); |
---|
18 | } |
---|
19 | } |
---|
20 | |
---|
21 | function option_definition() { |
---|
22 | $options = parent::option_definition(); |
---|
23 | |
---|
24 | $options['glossary'] = array('default' => FALSE); |
---|
25 | $options['ignorecase'] = array('default' => FALSE); |
---|
26 | $options['limit'] = array('default' => 0); |
---|
27 | $options['case'] = array('default' => 'none'); |
---|
28 | $options['path_case'] = array('default' => 'none'); |
---|
29 | $options['transform_dash'] = array('default' => FALSE); |
---|
30 | $options['break_phrase'] = array('default' => FALSE); |
---|
31 | |
---|
32 | if (!empty($this->definition['many to one'])) { |
---|
33 | $options['add_table'] = array('default' => FALSE); |
---|
34 | $options['require_value'] = array('default' => FALSE); |
---|
35 | } |
---|
36 | |
---|
37 | return $options; |
---|
38 | } |
---|
39 | |
---|
40 | function options_form(&$form, &$form_state) { |
---|
41 | parent::options_form($form, $form_state); |
---|
42 | |
---|
43 | $form['glossary'] = array( |
---|
44 | '#type' => 'checkbox', |
---|
45 | '#title' => t('Glossary mode'), |
---|
46 | '#description' => t('Glossary mode applies a limit to the number of characters used in the argument, which allows the summary view to act as a glossary.'), |
---|
47 | '#default_value' => $this->options['glossary'], |
---|
48 | ); |
---|
49 | |
---|
50 | $form['ignorecase'] = array( |
---|
51 | '#type' => 'checkbox', |
---|
52 | '#title' => t('Ignore case'), |
---|
53 | '#description' => t('Ignore case allows for doing database searches without case sensitivity. MySQL already works in lower-case mode, so MySQL users should leave this unchecked to improve performance.'), |
---|
54 | '#default_value' => $this->options['ignorecase'], |
---|
55 | ); |
---|
56 | |
---|
57 | $form['limit'] = array( |
---|
58 | '#type' => 'textfield', |
---|
59 | '#title' => t('Character limit'), |
---|
60 | '#description' => t('How many characters of the argument to filter against. If set to 1, all fields starting with the letter in the argument would be matched.'), |
---|
61 | '#default_value' => $this->options['limit'], |
---|
62 | '#process' => array('views_process_dependency'), |
---|
63 | '#dependency' => array('edit-options-glossary' => array(TRUE)), |
---|
64 | ); |
---|
65 | |
---|
66 | $form['case'] = array( |
---|
67 | '#type' => 'select', |
---|
68 | '#title' => t('Case'), |
---|
69 | '#description' => t('When printing the argument result, how to transform the case.'), |
---|
70 | '#options' => array( |
---|
71 | 'none' => t('No transform'), |
---|
72 | 'upper' => t('Upper case'), |
---|
73 | 'lower' => t('Lower case'), |
---|
74 | 'ucfirst' => t('Capitalize first letter'), |
---|
75 | 'ucwords' => t('Capitalize each word'), |
---|
76 | ), |
---|
77 | '#default_value' => $this->options['case'], |
---|
78 | ); |
---|
79 | |
---|
80 | $form['path_case'] = array( |
---|
81 | '#type' => 'select', |
---|
82 | '#title' => t('Case in path'), |
---|
83 | '#description' => t('When printing url paths, how to transform the case of the argument. Do not use this unless with Postgres as it uses case sensitive comparisons.'), |
---|
84 | '#options' => array( |
---|
85 | 'none' => t('No transform'), |
---|
86 | 'upper' => t('Upper case'), |
---|
87 | 'lower' => t('Lower case'), |
---|
88 | 'ucfirst' => t('Capitalize first letter'), |
---|
89 | 'ucwords' => t('Capitalize each word'), |
---|
90 | ), |
---|
91 | '#default_value' => $this->options['path_case'], |
---|
92 | ); |
---|
93 | |
---|
94 | $form['transform_dash'] = array( |
---|
95 | '#type' => 'checkbox', |
---|
96 | '#title' => t('Transform spaces to dashes in URL'), |
---|
97 | '#default_value' => $this->options['transform_dash'], |
---|
98 | ); |
---|
99 | |
---|
100 | if (!empty($this->definition['many to one'])) { |
---|
101 | $form['add_table'] = array( |
---|
102 | '#type' => 'checkbox', |
---|
103 | '#title' => t('Allow multiple arguments to work together'), |
---|
104 | '#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.'), |
---|
105 | '#default_value' => !empty($this->options['add_table']), |
---|
106 | ); |
---|
107 | |
---|
108 | $form['require_value'] = array( |
---|
109 | '#type' => 'checkbox', |
---|
110 | '#title' => t('Do not display items with no value in summary'), |
---|
111 | '#default_value' => !empty($this->options['require_value']), |
---|
112 | ); |
---|
113 | } |
---|
114 | |
---|
115 | // allow + for or, , for and |
---|
116 | $form['break_phrase'] = array( |
---|
117 | '#type' => 'checkbox', |
---|
118 | '#title' => t('Allow multiple terms per argument'), |
---|
119 | '#description' => t('If selected, users can enter multiple arguments in the form of 1+2+3 or 1,2,3.'), |
---|
120 | '#default_value' => !empty($this->options['break_phrase']), |
---|
121 | ); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Build the summary query based on a string |
---|
126 | */ |
---|
127 | function summary_query() { |
---|
128 | if (empty($this->definition['many to one'])) { |
---|
129 | $this->ensure_my_table(); |
---|
130 | } |
---|
131 | else { |
---|
132 | $this->table_alias = $this->helper->summary_join(); |
---|
133 | } |
---|
134 | |
---|
135 | if (empty($this->options['glossary'])) { |
---|
136 | // Add the field. |
---|
137 | if (empty($this->options['ignorecase'])){ |
---|
138 | $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field); |
---|
139 | $this->query->set_count_field($this->table_alias, $this->real_field); |
---|
140 | } |
---|
141 | else { |
---|
142 | $this->base_alias = $this->query->add_field($this->table_alias, 'LOWER(' . $this->real_field . ')'); |
---|
143 | $this->query->set_count_field($this->table_alias, 'LOWER(' . $this->real_field . ')'); |
---|
144 | } |
---|
145 | } |
---|
146 | else { |
---|
147 | // Add the field. |
---|
148 | $formula = $this->get_formula(); |
---|
149 | if (empty($this->options['ignorecase'])){ |
---|
150 | $this->base_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated'); |
---|
151 | $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated'); |
---|
152 | } |
---|
153 | else { |
---|
154 | $this->base_alias = $this->query->add_field(NULL, 'LOWER(' . $formula . ')', $this->field . '_truncated'); |
---|
155 | $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated'); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | $this->summary_name_field(); |
---|
160 | return $this->summary_basics(FALSE); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Get the formula for this argument. |
---|
165 | * |
---|
166 | * $this->ensure_my_table() MUST have been called prior to this. |
---|
167 | */ |
---|
168 | function get_formula() { |
---|
169 | return "SUBSTR($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")"; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Build the query based upon the formula |
---|
174 | */ |
---|
175 | function query() { |
---|
176 | $argument = $this->argument; |
---|
177 | if (!empty($this->options['transform_dash'])) { |
---|
178 | $argument = strtr($argument, '-', ' '); |
---|
179 | } |
---|
180 | |
---|
181 | if (!empty($this->options['break_phrase'])) { |
---|
182 | views_break_phrase_string($argument, $this); |
---|
183 | } |
---|
184 | else { |
---|
185 | $this->value = array($argument); |
---|
186 | $this->operator = 'or'; |
---|
187 | } |
---|
188 | |
---|
189 | if (!empty($this->definition['many to one'])) { |
---|
190 | if (!empty($this->options['glossary'])) { |
---|
191 | $this->helper->formula = TRUE; |
---|
192 | } |
---|
193 | $this->helper->ensure_my_table(); |
---|
194 | $this->helper->add_filter(); |
---|
195 | return; |
---|
196 | } |
---|
197 | |
---|
198 | $this->ensure_my_table(); |
---|
199 | if (empty($this->options['glossary'])) { |
---|
200 | $field = "$this->table_alias.$this->real_field"; |
---|
201 | } |
---|
202 | else { |
---|
203 | $field = $this->get_formula(); |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | if (count($this->value) > 1) { |
---|
208 | $operator = 'IN'; |
---|
209 | $placeholders = '(' . implode(', ', array_fill(0, sizeof($this->value), "'%s'")) . ')'; |
---|
210 | $argument = $this->value; |
---|
211 | } |
---|
212 | else { |
---|
213 | $placeholders = "'%s'"; |
---|
214 | $operator = '='; |
---|
215 | } |
---|
216 | |
---|
217 | if (empty($this->options['ignorecase'])){ |
---|
218 | $this->query->add_where(0, "$field $operator $placeholders", $argument); |
---|
219 | } |
---|
220 | else { |
---|
221 | $this->query->add_where(0, "LOWER($field) $operator LOWER($placeholders)", $argument); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | function summary_argument($data) { |
---|
226 | $value = $this->case_transform($data->{$this->base_alias}, 'path_case'); |
---|
227 | if (!empty($this->options['transform_dash'])) { |
---|
228 | $value = strtr($value, ' ', '-'); |
---|
229 | } |
---|
230 | return $value; |
---|
231 | } |
---|
232 | |
---|
233 | function case_transform($string, $option) { |
---|
234 | global $multibyte; |
---|
235 | |
---|
236 | switch ($this->options[$option]) { |
---|
237 | default: |
---|
238 | return $string; |
---|
239 | case 'upper': |
---|
240 | return drupal_strtoupper($string); |
---|
241 | case 'lower': |
---|
242 | return drupal_strtolower($string); |
---|
243 | case 'ucfirst': |
---|
244 | return drupal_strtoupper(drupal_substr($string, 0, 1)) . drupal_substr($string, 1); |
---|
245 | case 'ucwords': |
---|
246 | if ($multibyte == UNICODE_MULTIBYTE) { |
---|
247 | return mb_convert_case($string, MB_CASE_TITLE); |
---|
248 | } else { |
---|
249 | return ucwords($string); |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | function title() { |
---|
255 | $this->argument = $this->case_transform($this->argument, 'case'); |
---|
256 | if (!empty($this->options['transform_dash'])) { |
---|
257 | $this->argument = strtr($this->argument, '-', ' '); |
---|
258 | } |
---|
259 | |
---|
260 | if (!empty($this->options['break_phrase'])) { |
---|
261 | views_break_phrase_string($this->argument, $this); |
---|
262 | } |
---|
263 | else { |
---|
264 | $this->value = array($this->argument); |
---|
265 | $this->operator = 'or'; |
---|
266 | } |
---|
267 | |
---|
268 | if (empty($this->value)) { |
---|
269 | return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized'); |
---|
270 | } |
---|
271 | |
---|
272 | if ($this->value === array(-1)) { |
---|
273 | return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input'); |
---|
274 | } |
---|
275 | |
---|
276 | return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query()); |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | * Override for specific title lookups. |
---|
281 | */ |
---|
282 | function title_query() { |
---|
283 | return drupal_map_assoc($this->value, 'check_plain'); |
---|
284 | } |
---|
285 | |
---|
286 | function summary_name($data) { |
---|
287 | return $this->case_transform(parent::summary_name($data), 'case'); |
---|
288 | } |
---|
289 | |
---|
290 | } |
---|