1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * The base plugin to handle exposed filter forms. |
---|
5 | */ |
---|
6 | class views_plugin_exposed_form extends views_plugin { |
---|
7 | |
---|
8 | /** |
---|
9 | * Initialize the plugin. |
---|
10 | * |
---|
11 | * @param $view |
---|
12 | * The view object. |
---|
13 | * @param $display |
---|
14 | * The display handler. |
---|
15 | */ |
---|
16 | function init(&$view, &$display, $options = array()) { |
---|
17 | $this->view = &$view; |
---|
18 | $this->display = &$display; |
---|
19 | |
---|
20 | $this->unpack_options($this->options, $options); |
---|
21 | } |
---|
22 | |
---|
23 | /** |
---|
24 | * Return a string to display as the clickable title for the |
---|
25 | * control. |
---|
26 | */ |
---|
27 | function summary_title() { |
---|
28 | return t('Unknown'); |
---|
29 | } |
---|
30 | |
---|
31 | function option_definition() { |
---|
32 | $options = parent::option_definition(); |
---|
33 | $options['submit_button'] = array('default' => 'Apply', 'translatable' => TRUE); |
---|
34 | $options['reset_button'] = array('default' => FALSE, 'bool' => TRUE); |
---|
35 | $options['reset_button_label'] = array('default' => 'Reset', 'translatable' => TRUE); |
---|
36 | $options['exposed_sorts_label'] = array('default' => 'Sort by', 'translatable' => TRUE); |
---|
37 | $options['sort_asc_label'] = array('default' => 'Asc', 'translatable' => TRUE); |
---|
38 | $options['sort_desc_label'] = array('default' => 'Desc', 'translatable' => TRUE); |
---|
39 | if (module_exists('ctools')) { |
---|
40 | $options['autosubmit'] = array('default' => FALSE); |
---|
41 | $options['autosubmit_hide'] = array('default' => FALSE); |
---|
42 | } |
---|
43 | return $options; |
---|
44 | } |
---|
45 | |
---|
46 | function options_form(&$form, &$form_state) { |
---|
47 | $form['submit_button'] = array( |
---|
48 | '#type' => 'textfield', |
---|
49 | '#title' => t('Submit button text'), |
---|
50 | '#description' => t('Text to display in the submit button of the exposed form.'), |
---|
51 | '#default_value' => $this->options['submit_button'], |
---|
52 | '#required' => TRUE, |
---|
53 | ); |
---|
54 | |
---|
55 | $form['reset_button'] = array ( |
---|
56 | '#type' => 'checkbox', |
---|
57 | '#title' => t('Include reset button'), |
---|
58 | '#description' => t('If checked the exposed form will provide a button to reset all the applied exposed filters'), |
---|
59 | '#default_value' => $this->options['reset_button'], |
---|
60 | ); |
---|
61 | |
---|
62 | $form['reset_button_label'] = array( |
---|
63 | '#type' => 'textfield', |
---|
64 | '#title' => t('Reset button label'), |
---|
65 | '#description' => t('Text to display in the reset button of the exposed form.'), |
---|
66 | '#default_value' => $this->options['reset_button_label'], |
---|
67 | '#required' => TRUE, |
---|
68 | '#dependency' => array( |
---|
69 | 'edit-exposed-form-options-reset-button' => array(1) |
---|
70 | ), |
---|
71 | '#process' => array('views_process_dependency'), |
---|
72 | ); |
---|
73 | |
---|
74 | $form['exposed_sorts_label'] = array( |
---|
75 | '#type' => 'textfield', |
---|
76 | '#title' => t('Exposed sorts label'), |
---|
77 | '#description' => t('Text to display as the label of the exposed sort select box.'), |
---|
78 | '#default_value' => $this->options['exposed_sorts_label'], |
---|
79 | '#required' => TRUE, |
---|
80 | ); |
---|
81 | |
---|
82 | $form['sort_asc_label'] = array( |
---|
83 | '#type' => 'textfield', |
---|
84 | '#title' => t('Ascending'), |
---|
85 | '#description' => t('Text to use when exposed sort is ordered ascending.'), |
---|
86 | '#default_value' => $this->options['sort_asc_label'], |
---|
87 | '#required' => TRUE, |
---|
88 | ); |
---|
89 | |
---|
90 | $form['sort_desc_label'] = array( |
---|
91 | '#type' => 'textfield', |
---|
92 | '#title' => t('Descending'), |
---|
93 | '#description' => t('Text to use when exposed sort is ordered descending.'), |
---|
94 | '#default_value' => $this->options['sort_desc_label'], |
---|
95 | '#required' => TRUE, |
---|
96 | ); |
---|
97 | |
---|
98 | if (module_exists('ctools')) { |
---|
99 | $form['autosubmit'] = array( |
---|
100 | '#type' => 'checkbox', |
---|
101 | '#title' => t('Autosubmit'), |
---|
102 | '#description' => t('Automatically submit the form once an element is changed.'), |
---|
103 | '#default_value' => $this->options['autosubmit'], |
---|
104 | ); |
---|
105 | |
---|
106 | $form['autosubmit_hide'] = array( |
---|
107 | '#type' => 'checkbox', |
---|
108 | '#title' => t('Hide submit button'), |
---|
109 | '#description' => t('Hide submit button if javascript is enabled.'), |
---|
110 | '#default_value' => $this->options['autosubmit_hide'], |
---|
111 | '#dependency' => array( |
---|
112 | 'edit-exposed-form-options-autosubmit' => array(1), |
---|
113 | ), |
---|
114 | ); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Render the exposed filter form. |
---|
120 | * |
---|
121 | * This actually does more than that; because it's using FAPI, the form will |
---|
122 | * also assign data to the appropriate handlers for use in building the |
---|
123 | * query. |
---|
124 | */ |
---|
125 | function render_exposed_form($block = FALSE) { |
---|
126 | // Deal with any exposed filters we may have, before building. |
---|
127 | $form_state = array( |
---|
128 | 'view' => &$this->view, |
---|
129 | 'display' => &$this->display, |
---|
130 | 'method' => 'get', |
---|
131 | 'rerender' => TRUE, |
---|
132 | 'no_redirect' => TRUE, |
---|
133 | ); |
---|
134 | |
---|
135 | // Some types of displays (eg. attachments) may wish to use the exposed |
---|
136 | // filters of their parent displays instead of showing an additional |
---|
137 | // exposed filter form for the attachment as well as that for the parent. |
---|
138 | if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) { |
---|
139 | unset($form_state['rerender']); |
---|
140 | } |
---|
141 | |
---|
142 | if (!empty($this->ajax)) { |
---|
143 | $form_state['ajax'] = TRUE; |
---|
144 | } |
---|
145 | |
---|
146 | $form_state['exposed_form_plugin'] = $this; |
---|
147 | $output = drupal_build_form('views_exposed_form', $form_state); |
---|
148 | |
---|
149 | if (!empty($form_state['js settings'])) { |
---|
150 | $this->view->js_settings = $form_state['js settings']; |
---|
151 | } |
---|
152 | |
---|
153 | if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) { |
---|
154 | return ""; |
---|
155 | } |
---|
156 | else { |
---|
157 | return $output; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | function query() { |
---|
162 | $view = $this->view; |
---|
163 | $exposed_data = $view->exposed_data; |
---|
164 | if (!empty($exposed_data['sort_by'])) { |
---|
165 | $handler = $view->sort[$exposed_data['sort_by']]; |
---|
166 | if (isset($handler)) { |
---|
167 | $view->query->orderby = array(); |
---|
168 | if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('ASC', 'DESC'))) { |
---|
169 | $handler->options['order'] = $exposed_data['sort_order']; |
---|
170 | } |
---|
171 | $handler->query(); |
---|
172 | foreach ($view->sort as $sort) { |
---|
173 | if (!$sort->is_exposed()) { |
---|
174 | $sort->query(); |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | function pre_render(&$values) { } |
---|
182 | |
---|
183 | function post_render(&$output) { } |
---|
184 | |
---|
185 | function pre_execute() { } |
---|
186 | |
---|
187 | function exposed_form_alter(&$form, &$form_state) { |
---|
188 | if (!empty($this->options['reset_button'])) { |
---|
189 | $form['reset'] = array( |
---|
190 | '#value' => $this->options['reset_button_label'], |
---|
191 | '#type' => 'submit', |
---|
192 | ); |
---|
193 | } |
---|
194 | |
---|
195 | $form['submit']['#value'] = $this->options['submit_button']; |
---|
196 | // Check if there is exposed sorts for this view |
---|
197 | $exposed_sorts = array(); |
---|
198 | foreach ($this->view->sort as $id => $handler) { |
---|
199 | if ($handler->can_expose() && $handler->is_exposed()) { |
---|
200 | $exposed_sorts[$id] = check_plain($handler->options['expose']['label']); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | if (count($exposed_sorts)) { |
---|
205 | $form['sort_by'] = array( |
---|
206 | '#type' => 'select', |
---|
207 | '#options' => $exposed_sorts, |
---|
208 | '#title' => $this->options['exposed_sorts_label'], |
---|
209 | ); |
---|
210 | $sort_order = array( |
---|
211 | 'ASC' => $this->options['sort_asc_label'], |
---|
212 | 'DESC' => $this->options['sort_desc_label'], |
---|
213 | ); |
---|
214 | $first_sort = reset($this->view->sort); |
---|
215 | $form['sort_order'] = array( |
---|
216 | '#type' => 'select', |
---|
217 | '#options' => $sort_order, |
---|
218 | '#title' => t('Order'), |
---|
219 | '#default_value' => $first_sort->options['order'], |
---|
220 | ); |
---|
221 | $form['submit']['#weight'] = 10; |
---|
222 | if (isset($form['reset'])) { |
---|
223 | $form['reset']['#weight'] = 10; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | $pager = $this->view->display_handler->get_plugin('pager'); |
---|
228 | if ($pager) { |
---|
229 | $pager->exposed_form_alter($form, $form_state); |
---|
230 | $form_state['pager_plugin'] = $pager; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | // Apply autosubmit values. |
---|
235 | if (!empty($this->options['autosubmit'])) { |
---|
236 | $form['#attributes']['class'] .= ' ctools-auto-submit-full-form'; |
---|
237 | $form['submit']['#attributes']['class'] = 'ctools-use-ajax ctools-auto-submit-click'; |
---|
238 | ctools_add_js('auto-submit'); |
---|
239 | |
---|
240 | if (!empty($this->options['autosubmit_hide'])) { |
---|
241 | $form['submit']['#attributes']['class'] .= ' js-hide'; |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | function exposed_form_validate(&$form, &$form_state) { |
---|
247 | if (isset($form_state['pager_plugin'])) { |
---|
248 | $form_state['pager_plugin']->exposed_form_validate($form, $form_state); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * This function is executed when exposed form is submited. |
---|
254 | * |
---|
255 | * @param $form |
---|
256 | * Nested array of form elements that comprise the form. |
---|
257 | * @param $form_state |
---|
258 | * A keyed array containing the current state of the form. |
---|
259 | * @param $exclude |
---|
260 | * Nested array of keys to exclude of insert into |
---|
261 | * $view->exposed_raw_input |
---|
262 | */ |
---|
263 | function exposed_form_submit(&$form, &$form_state, &$exclude) { |
---|
264 | if (!empty($form_state['values']['op']) && $form_state['values']['op'] == $this->options['reset_button_label']) { |
---|
265 | $this->reset_form($form, $form_state); |
---|
266 | } |
---|
267 | if (isset($form_state['pager_plugin'])) { |
---|
268 | $form_state['pager_plugin']->exposed_form_submit($form, $form_state, $exclude); |
---|
269 | $exclude[] = 'pager_plugin'; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * Provide a reset form if one was requested. |
---|
275 | */ |
---|
276 | function reset_form(&$form, &$form_state) { |
---|
277 | // If filters are not overridden, store the 'remember' settings on the |
---|
278 | // default display. If they are, store them on this display. This way, |
---|
279 | // multiple displays in the same view can share the same filters and |
---|
280 | // remember settings. |
---|
281 | $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display; |
---|
282 | unset($_SESSION['views'][$this->view->name][$display_id]); |
---|
283 | $form_state['no_redirect'] = FALSE; |
---|
284 | $form_state['redirect'] = !empty($this->view->ajax_path) ? $this->view->ajax_path : $_GET['q']; |
---|
285 | } |
---|
286 | } |
---|