Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_exposed_form.inc @ 5d12d676

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 object $view
27
   *   The view object.
28
   * @param object $display
29
   *   The display handler.
30
   * @param array $options
31
   *   Any additional options that are being added.
32
   */
33
  public function init(&$view, &$display, $options = array()) {
34
    $this->view = &$view;
35
    $this->display = &$display;
36

    
37
    $this->unpack_options($this->options, $options);
38
  }
39

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

    
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function options_form(&$form, &$form_state) {
61
    parent::options_form($form, $form_state);
62
    $form['submit_button'] = array(
63
      '#type' => 'textfield',
64
      '#title' => t('Submit button text'),
65
      '#description' => t('Text to display in the submit button of the exposed form.'),
66
      '#default_value' => $this->options['submit_button'],
67
      '#required' => TRUE,
68
    );
69

    
70
    $form['reset_button'] = array (
71
      '#type' => 'checkbox',
72
      '#title' => t('Include reset button'),
73
      '#description' => t('If checked the exposed form will provide a button to reset all the applied exposed filters'),
74
      '#default_value' => $this->options['reset_button'],
75
    );
76

    
77
    $form['reset_button_label'] = array(
78
     '#type' => 'textfield',
79
      '#title' => t('Reset button label'),
80
      '#description' => t('Text to display in the reset button of the exposed form.'),
81
      '#default_value' => $this->options['reset_button_label'],
82
      '#required' => TRUE,
83
      '#dependency' => array(
84
        'edit-exposed-form-options-reset-button' => array(1)
85
      ),
86
    );
87

    
88
    $form['exposed_sorts_label'] = array(
89
      '#type' => 'textfield',
90
      '#title' => t('Exposed sorts label'),
91
      '#description' => t('Text to display as the label of the exposed sort select box.'),
92
      '#default_value' => $this->options['exposed_sorts_label'],
93
      '#required' => TRUE,
94
    );
95

    
96
    $form['expose_sort_order'] = array(
97
      '#type' => 'checkbox',
98
      '#title' => t('Expose sort order'),
99
      '#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.'),
100
      '#default_value' => $this->options['expose_sort_order'],
101
    );
102

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

    
112
    $form['sort_desc_label'] = array(
113
      '#type' => 'textfield',
114
      '#title' => t('Descending'),
115
      '#description' => t('Text to use when exposed sort is ordered descending.'),
116
      '#default_value' => $this->options['sort_desc_label'],
117
      '#required' => TRUE,
118
      '#dependency' => array('edit-exposed-form-options-expose-sort-order' => array(TRUE)),
119
    );
120

    
121
    $form['autosubmit'] = array(
122
      '#type' => 'checkbox',
123
      '#title' => t('Autosubmit'),
124
      '#description' => t('Automatically submit the form once an element is changed.'),
125
      '#default_value' => $this->options['autosubmit'],
126
    );
127

    
128
    $form['autosubmit_hide'] = array(
129
      '#type' => 'checkbox',
130
      '#title' => t('Hide submit button'),
131
      '#description' => t('Hide submit button if javascript is enabled.'),
132
      '#default_value' => $this->options['autosubmit_hide'],
133
      '#dependency' => array(
134
        'edit-exposed-form-options-autosubmit' => array(1),
135
      ),
136
    );
137
  }
138

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

    
157
    // Some types of displays (eg. attachments) may wish to use the exposed
158
    // filters of their parent displays instead of showing an additional
159
    // exposed filter form for the attachment as well as that for the parent.
160
    if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
161
      unset($form_state['rerender']);
162
    }
163

    
164
    if (!empty($this->ajax)) {
165
      $form_state['ajax'] = TRUE;
166
    }
167

    
168
    $form_state['exposed_form_plugin'] = $this;
169
    $form = drupal_build_form('views_exposed_form', $form_state);
170
    $output = drupal_render($form);
171

    
172
    if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
173
      return "";
174
    }
175
    else {
176
      return $output;
177
    }
178
  }
179

    
180
  /**
181
   * {@inheritdoc}
182
   */
183
  public function query() {
184
    $view = $this->view;
185
    $exposed_data = isset($view->exposed_data) ? $view->exposed_data : array();
186
    $sort_by = isset($exposed_data['sort_by']) ? $exposed_data['sort_by'] : NULL;
187
    if (!empty($sort_by) && $this->view->style_plugin->build_sort()) {
188
      // Make sure the original order of sorts is preserved
189
      // (e.g. a sticky sort is often first)
190
      if (isset($view->sort[$sort_by])) {
191
        $view->query->orderby = array();
192
        foreach ($view->sort as $key => $sort) {
193
          if (!$sort->is_exposed()) {
194
            $sort->query();
195
          }
196
          elseif ($key == $sort_by) {
197
            if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('ASC', 'DESC'))) {
198
              $sort->options['order'] = $exposed_data['sort_order'];
199
            }
200
            $sort->set_relationship();
201
            $sort->query();
202
          }
203
        }
204
      }
205
    }
206
  }
207

    
208
  /**
209
   * {@inheritdoc}
210
   */
211
  public function pre_render($values) {
212
  }
213

    
214
  /**
215
   * {@inheritdoc}
216
   */
217
  public function post_render(&$output) {
218
  }
219

    
220
  /**
221
   * {@inheritdoc}
222
   */
223
  public function pre_execute() {
224
  }
225

    
226
  /**
227
   * {@inheritdoc}
228
   */
229
  public function post_execute() {
230
  }
231

    
232
  /**
233
   * {@inheritdoc}
234
   */
235
  public function exposed_form_alter(&$form, &$form_state) {
236
    if (!empty($this->options['reset_button'])) {
237
      $form['reset'] = array(
238
        '#value' => $this->options['reset_button_label'],
239
        '#type' => 'submit',
240
      );
241
    }
242

    
243
    $form['submit']['#value'] = $this->options['submit_button'];
244
    // Check if there is exposed sorts for this view
245
    $exposed_sorts = array();
246
    foreach ($this->view->sort as $id => $handler) {
247
      if ($handler->can_expose() && $handler->is_exposed()) {
248
        $exposed_sorts[$id] = check_plain($handler->options['expose']['label']);
249
      }
250
    }
251

    
252
    if (count($exposed_sorts)) {
253
      $form['sort_by'] = array(
254
        '#type' => 'select',
255
        '#options' => $exposed_sorts,
256
        '#title' => $this->options['exposed_sorts_label'],
257
      );
258
      $sort_order = array(
259
        'ASC' => $this->options['sort_asc_label'],
260
        'DESC' => $this->options['sort_desc_label'],
261
      );
262
      if (isset($form_state['input']['sort_by']) && isset($this->view->sort[$form_state['input']['sort_by']])) {
263
        $default_sort_order = $this->view->sort[$form_state['input']['sort_by']]->options['order'];
264
      }
265
      else {
266
        $first_sort = reset($this->view->sort);
267
        $default_sort_order = $first_sort->options['order'];
268
      }
269

    
270
      if (!isset($form_state['input']['sort_by'])) {
271
        $keys = array_keys($exposed_sorts);
272
        $form_state['input']['sort_by'] = array_shift($keys);
273
      }
274

    
275
      if ($this->options['expose_sort_order']) {
276
        $form['sort_order'] = array(
277
          '#type' => 'select',
278
          '#options' => $sort_order,
279
          '#title' => t('Order'),
280
          '#default_value' => $default_sort_order,
281
        );
282
      }
283
      $form['submit']['#weight'] = 10;
284
      if (isset($form['reset'])) {
285
        $form['reset']['#weight'] = 10;
286
      }
287
    }
288

    
289
    $pager = $this->view->display_handler->get_plugin('pager');
290
    if ($pager) {
291
      $pager->exposed_form_alter($form, $form_state);
292
      $form_state['pager_plugin'] = $pager;
293
    }
294

    
295

    
296
    // Apply autosubmit values.
297
    if (!empty($this->options['autosubmit'])) {
298
      $form = array_merge_recursive($form, array('#attributes' => array('class' => array('ctools-auto-submit-full-form'))));
299
      $form['submit']['#attributes']['class'][] = 'ctools-use-ajax';
300
      $form['submit']['#attributes']['class'][] = 'ctools-auto-submit-click';
301
      $form['#attached']['js'][] = drupal_get_path('module', 'ctools') . '/js/auto-submit.js';
302

    
303
      if (!empty($this->options['autosubmit_hide'])) {
304
        $form['submit']['#attributes']['class'][] = 'js-hide';
305
      }
306
    }
307
  }
308

    
309
  /**
310
   * {@inheritdoc}
311
   */
312
  public function exposed_form_validate(&$form, &$form_state) {
313
    if (isset($form_state['pager_plugin'])) {
314
      $form_state['pager_plugin']->exposed_form_validate($form, $form_state);
315
    }
316
  }
317

    
318
  /**
319
  * This function is executed when exposed form is submited.
320
  *
321
  * @param array $form
322
  *   Nested array of form elements that comprise the form.
323
  * @param array $form_state
324
  *   A keyed array containing the current state of the form.
325
  * @param array $exclude
326
  *   Nested array of keys to exclude of insert into $view->exposed_raw_input.
327
  */
328
  public function exposed_form_submit(&$form, &$form_state, &$exclude) {
329
    if (!empty($form_state['values']['op']) && $form_state['values']['op'] == $this->options['reset_button_label']) {
330
      $this->reset_form($form, $form_state);
331
    }
332
    if (isset($form_state['pager_plugin'])) {
333
      $form_state['pager_plugin']->exposed_form_submit($form, $form_state, $exclude);
334
      $exclude[] = 'pager_plugin';
335
    }
336
  }
337

    
338
  /**
339
   * {@inheritdoc}
340
   */
341
  public function reset_form(&$form, &$form_state) {
342
    // _SESSION is not defined for users who are not logged in.
343

    
344
    // If filters are not overridden, store the 'remember' settings on the
345
    // default display. If they are, store them on this display. This way,
346
    // multiple displays in the same view can share the same filters and
347
    // remember settings.
348
    $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
349

    
350
    if (isset($_SESSION['views'][$this->view->name][$display_id])) {
351
      unset($_SESSION['views'][$this->view->name][$display_id]);
352
    }
353

    
354
    // Set the form to allow redirect.
355
    if (empty($this->view->live_preview)) {
356
      $form_state['no_redirect'] = FALSE;
357
    }
358
    else {
359
      $form_state['rebuild'] = TRUE;
360
      $this->view->exposed_data = array();
361
    }
362

    
363
    $form_state['redirect'] = current_path();
364
    $form_state['values'] = array();
365
  }
366

    
367
}
368

    
369
/**
370
 * @}
371
 */