Projet

Général

Profil

Paste
Télécharger (11,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_exposed_form.inc @ 6eb8d15f

1
<?php
2

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

    
8
/**
9
 * @defgroup views_exposed_form_plugins Views exposed form plugins
10
 * @{
11
 * Plugins that handle the validation/submission and rendering of exposed forms.
12
 *
13
 * If needed, it is possible to use them to add additional form elements.
14
 *
15
 * @see hook_views_plugins()
16
 */
17

    
18
/**
19
 * The base plugin to handle exposed filter forms.
20
 */
21
class views_plugin_exposed_form extends views_plugin {
22

    
23
  /**
24
   * Initialize the plugin.
25
   *
26
   * @param $view
27
   *   The view object.
28
   * @param $display
29
   *   The display handler.
30
   */
31
  function init(&$view, &$display, $options = array()) {
32
    $this->view = &$view;
33
    $this->display = &$display;
34

    
35
    $this->unpack_options($this->options, $options);
36
  }
37

    
38
  function option_definition() {
39
    $options = parent::option_definition();
40
    $options['submit_button'] = array('default' => 'Apply', 'translatable' => TRUE);
41
    $options['reset_button'] = array('default' => FALSE, 'bool' => TRUE);
42
    $options['reset_button_label'] = array('default' => 'Reset', 'translatable' => TRUE);
43
    $options['exposed_sorts_label'] = array('default' => 'Sort by', 'translatable' => TRUE);
44
    $options['expose_sort_order'] = array('default' => TRUE, 'bool' => TRUE);
45
    $options['sort_asc_label'] = array('default' => 'Asc', 'translatable' => TRUE);
46
    $options['sort_desc_label'] = array('default' => 'Desc', 'translatable' => TRUE);
47
    $options['autosubmit'] = array('default' => FALSE, 'bool' => TRUE);
48
    $options['autosubmit_hide'] = array('default' => TRUE, 'bool' => TRUE);
49
    return $options;
50
  }
51

    
52
  function options_form(&$form, &$form_state) {
53
    parent::options_form($form, $form_state);
54
    $form['submit_button'] = array(
55
      '#type' => 'textfield',
56
      '#title' => t('Submit button text'),
57
      '#description' => t('Text to display in the submit button of the exposed form.'),
58
      '#default_value' => $this->options['submit_button'],
59
      '#required' => TRUE,
60
    );
61

    
62
    $form['reset_button'] = array (
63
      '#type' => 'checkbox',
64
      '#title' => t('Include reset button'),
65
      '#description' => t('If checked the exposed form will provide a button to reset all the applied exposed filters'),
66
      '#default_value' => $this->options['reset_button'],
67
    );
68

    
69
    $form['reset_button_label'] = array(
70
     '#type' => 'textfield',
71
      '#title' => t('Reset button label'),
72
      '#description' => t('Text to display in the reset button of the exposed form.'),
73
      '#default_value' => $this->options['reset_button_label'],
74
      '#required' => TRUE,
75
      '#dependency' => array(
76
        'edit-exposed-form-options-reset-button' => array(1)
77
      ),
78
    );
79

    
80
    $form['exposed_sorts_label'] = array(
81
      '#type' => 'textfield',
82
      '#title' => t('Exposed sorts label'),
83
      '#description' => t('Text to display as the label of the exposed sort select box.'),
84
      '#default_value' => $this->options['exposed_sorts_label'],
85
      '#required' => TRUE,
86
    );
87

    
88
    $form['expose_sort_order'] = array(
89
      '#type' => 'checkbox',
90
      '#title' => t('Expose sort order'),
91
      '#description' => t('Allow the user to choose the sort order. If sort order is not exposed, the sort criteria settings for each sort will determine its order.'),
92
      '#default_value' => $this->options['expose_sort_order'],
93
    );
94

    
95
    $form['sort_asc_label'] = array(
96
      '#type' => 'textfield',
97
      '#title' => t('Ascending'),
98
      '#description' => t('Text to use when exposed sort is ordered ascending.'),
99
      '#default_value' => $this->options['sort_asc_label'],
100
      '#required' => TRUE,
101
      '#dependency' => array('edit-exposed-form-options-expose-sort-order' => array(TRUE)),
102
    );
103

    
104
    $form['sort_desc_label'] = array(
105
      '#type' => 'textfield',
106
      '#title' => t('Descending'),
107
      '#description' => t('Text to use when exposed sort is ordered descending.'),
108
      '#default_value' => $this->options['sort_desc_label'],
109
      '#required' => TRUE,
110
      '#dependency' => array('edit-exposed-form-options-expose-sort-order' => array(TRUE)),
111
    );
112

    
113
    $form['autosubmit'] = array(
114
      '#type' => 'checkbox',
115
      '#title' => t('Autosubmit'),
116
      '#description' => t('Automatically submit the form once an element is changed.'),
117
      '#default_value' => $this->options['autosubmit'],
118
    );
119

    
120
    $form['autosubmit_hide'] = array(
121
      '#type' => 'checkbox',
122
      '#title' => t('Hide submit button'),
123
      '#description' => t('Hide submit button if javascript is enabled.'),
124
      '#default_value' => $this->options['autosubmit_hide'],
125
      '#dependency' => array(
126
        'edit-exposed-form-options-autosubmit' => array(1),
127
      ),
128
    );
129
  }
130

    
131
  /**
132
   * Render the exposed filter form.
133
   *
134
   * This actually does more than that; because it's using FAPI, the form will
135
   * also assign data to the appropriate handlers for use in building the
136
   * query.
137
   */
138
  function render_exposed_form($block = FALSE) {
139
    // Deal with any exposed filters we may have, before building.
140
    $form_state = array(
141
      'view' => &$this->view,
142
      'display' => &$this->display,
143
      'method' => 'get',
144
      'rerender' => TRUE,
145
      'no_redirect' => TRUE,
146
      'always_process' => TRUE,
147
    );
148

    
149
    // Some types of displays (eg. attachments) may wish to use the exposed
150
    // filters of their parent displays instead of showing an additional
151
    // exposed filter form for the attachment as well as that for the parent.
152
    if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
153
      unset($form_state['rerender']);
154
    }
155

    
156
    if (!empty($this->ajax)) {
157
      $form_state['ajax'] = TRUE;
158
    }
159

    
160
    $form_state['exposed_form_plugin'] = $this;
161
    $form = drupal_build_form('views_exposed_form', $form_state);
162
    $output = drupal_render($form);
163

    
164
    if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
165
      return "";
166
    }
167
    else {
168
      return $output;
169
    }
170
  }
171

    
172
  function query() {
173
    $view = $this->view;
174
    $exposed_data = isset($view->exposed_data) ? $view->exposed_data : array();
175
    $sort_by = isset($exposed_data['sort_by']) ? $exposed_data['sort_by'] : NULL;
176
    if (!empty($sort_by) && $this->view->style_plugin->build_sort()) {
177
      // Make sure the original order of sorts is preserved
178
      // (e.g. a sticky sort is often first)
179
      if (isset($view->sort[$sort_by])) {
180
        $view->query->orderby = array();
181
        foreach ($view->sort as $key => $sort) {
182
          if (!$sort->is_exposed()) {
183
            $sort->query();
184
          }
185
          else if ($key == $sort_by) {
186
            if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('ASC', 'DESC'))) {
187
              $sort->options['order'] = $exposed_data['sort_order'];
188
            }
189
            $sort->set_relationship();
190
            $sort->query();
191
          }
192
        }
193
      }
194
    }
195
  }
196

    
197
  function pre_render($values) { }
198

    
199
  function post_render(&$output) { }
200

    
201
  function pre_execute() { }
202

    
203
  function post_execute() { }
204

    
205
  function exposed_form_alter(&$form, &$form_state) {
206
    if (!empty($this->options['reset_button'])) {
207
      $form['reset'] = array(
208
        '#value' => $this->options['reset_button_label'],
209
        '#type' => 'submit',
210
      );
211
    }
212

    
213
    $form['submit']['#value'] = $this->options['submit_button'];
214
    // Check if there is exposed sorts for this view
215
    $exposed_sorts = array();
216
    foreach ($this->view->sort as $id => $handler) {
217
      if ($handler->can_expose() && $handler->is_exposed()) {
218
        $exposed_sorts[$id] = check_plain($handler->options['expose']['label']);
219
      }
220
    }
221

    
222
    if (count($exposed_sorts)) {
223
      $form['sort_by'] = array(
224
        '#type' => 'select',
225
        '#options' => $exposed_sorts,
226
        '#title' => $this->options['exposed_sorts_label'],
227
      );
228
      $sort_order = array(
229
        'ASC' => $this->options['sort_asc_label'],
230
        'DESC' => $this->options['sort_desc_label'],
231
      );
232
      if (isset($form_state['input']['sort_by']) && isset($this->view->sort[$form_state['input']['sort_by']])) {
233
        $default_sort_order = $this->view->sort[$form_state['input']['sort_by']]->options['order'];
234
      } else {
235
        $first_sort = reset($this->view->sort);
236
        $default_sort_order = $first_sort->options['order'];
237
      }
238

    
239
      if (!isset($form_state['input']['sort_by'])) {
240
        $keys = array_keys($exposed_sorts);
241
        $form_state['input']['sort_by'] = array_shift($keys);
242
      }
243

    
244
      if ($this->options['expose_sort_order']) {
245
        $form['sort_order'] = array(
246
          '#type' => 'select',
247
          '#options' => $sort_order,
248
          '#title' => t('Order'),
249
          '#default_value' => $default_sort_order,
250
        );
251
      }
252
      $form['submit']['#weight'] = 10;
253
      if (isset($form['reset'])) {
254
        $form['reset']['#weight'] = 10;
255
      }
256
    }
257

    
258
    $pager = $this->view->display_handler->get_plugin('pager');
259
    if ($pager) {
260
      $pager->exposed_form_alter($form, $form_state);
261
      $form_state['pager_plugin'] = $pager;
262
    }
263

    
264

    
265
    // Apply autosubmit values.
266
    if (!empty($this->options['autosubmit'])) {
267
      $form = array_merge_recursive($form, array('#attributes' => array('class' => array('ctools-auto-submit-full-form'))));
268
      $form['submit']['#attributes']['class'][] = 'ctools-use-ajax';
269
      $form['submit']['#attributes']['class'][] = 'ctools-auto-submit-click';
270
      $form['#attached']['js'][] = drupal_get_path('module', 'ctools') . '/js/auto-submit.js';
271

    
272
      if (!empty($this->options['autosubmit_hide'])) {
273
        $form['submit']['#attributes']['class'][] = 'js-hide';
274
      }
275
    }
276
  }
277

    
278
  function exposed_form_validate(&$form, &$form_state) {
279
    if (isset($form_state['pager_plugin'])) {
280
      $form_state['pager_plugin']->exposed_form_validate($form, $form_state);
281
    }
282
  }
283

    
284
  /**
285
  * This function is executed when exposed form is submited.
286
  *
287
  * @param $form
288
  *   Nested array of form elements that comprise the form.
289
  * @param $form_state
290
  *   A keyed array containing the current state of the form.
291
  * @param $exclude
292
  *   Nested array of keys to exclude of insert into
293
  *   $view->exposed_raw_input
294
  */
295
  function exposed_form_submit(&$form, &$form_state, &$exclude) {
296
    if (!empty($form_state['values']['op']) && $form_state['values']['op'] == $this->options['reset_button_label']) {
297
      $this->reset_form($form, $form_state);
298
    }
299
    if (isset($form_state['pager_plugin'])) {
300
      $form_state['pager_plugin']->exposed_form_submit($form, $form_state, $exclude);
301
      $exclude[] = 'pager_plugin';
302
    }
303
  }
304

    
305
  function reset_form(&$form, &$form_state) {
306
    // _SESSION is not defined for users who are not logged in.
307

    
308
    // If filters are not overridden, store the 'remember' settings on the
309
    // default display. If they are, store them on this display. This way,
310
    // multiple displays in the same view can share the same filters and
311
    // remember settings.
312
    $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
313

    
314
    if (isset($_SESSION['views'][$this->view->name][$display_id])) {
315
      unset($_SESSION['views'][$this->view->name][$display_id]);
316
    }
317

    
318
    // Set the form to allow redirect.
319
    if (empty($this->view->live_preview)) {
320
      $form_state['no_redirect'] = FALSE;
321
    }
322
    else {
323
      $form_state['rebuild'] = TRUE;
324
      $this->view->exposed_data = array();
325
    }
326

    
327
    $form_state['redirect'] = current_path();
328
    $form_state['values'] = array();
329
  }
330
}
331

    
332
/**
333
 * @}
334
 */