Project

General

Profile

Paste
Download (7.15 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_sort.inc @ 8be7bf84

1
<?php
2

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

    
8
/**
9
 * @defgroup views_sort_handlers Views sort handlers
10
 * @{
11
 * Handlers to tell Views how to sort queries.
12
 */
13

    
14
/**
15
 * Base sort handler that has no options and performs a simple sort.
16
 *
17
 * @ingroup views_sort_handlers
18
 */
19
class views_handler_sort extends views_handler {
20

    
21
  /**
22
   * Determine if a sort can be exposed.
23
   */
24
  public function can_expose() {
25
    return TRUE;
26
  }
27

    
28
  /**
29
   * Called to add the sort to a query.
30
   */
31
  public function query() {
32
    $this->ensure_my_table();
33
    // Add the field.
34
    $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
35
  }
36

    
37
  /**
38
   * {@inheritdoc}
39
   */
40
  public function option_definition() {
41
    $options = parent::option_definition();
42

    
43
    $options['order'] = array('default' => 'ASC');
44
    $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
45
    $options['expose'] = array(
46
      'contains' => array(
47
        'label' => array('default' => '', 'translatable' => TRUE),
48
      ),
49
    );
50
    return $options;
51
  }
52

    
53
  /**
54
   * Display whether or not the sort order is ascending or descending
55
   */
56
  public function admin_summary() {
57
    if (!empty($this->options['exposed'])) {
58
      return t('Exposed');
59
    }
60
    switch ($this->options['order']) {
61
      case 'ASC':
62
      case 'asc':
63
      default:
64
        return t('asc');
65

    
66
      case 'DESC';
67
      case 'desc';
68
        return t('desc');
69
    }
70
  }
71

    
72
  /**
73
   * Basic options for all sort criteria
74
   */
75
  public function options_form(&$form, &$form_state) {
76
    parent::options_form($form, $form_state);
77
    if ($this->can_expose()) {
78
      $this->show_expose_button($form, $form_state);
79
    }
80
    $form['op_val_start'] = array('#value' => '<div class="clearfix">');
81
    $this->show_sort_form($form, $form_state);
82
    $form['op_val_end'] = array('#value' => '</div>');
83
    if ($this->can_expose()) {
84
      $this->show_expose_form($form, $form_state);
85
    }
86
  }
87

    
88
  /**
89
   * Shortcut to display the expose/hide button.
90
   */
91
  public function show_expose_button(&$form, &$form_state) {
92
    $form['expose_button'] = array(
93
      '#prefix' => '<div class="views-expose clearfix">',
94
      '#suffix' => '</div>',
95
      // Should always come first.
96
      '#weight' => -1000,
97
    );
98

    
99
    // Add a checkbox for JS users, which will have behavior attached to it
100
    // so it can replace the button.
101
    $form['expose_button']['checkbox'] = array(
102
      '#theme_wrappers' => array('container'),
103
      '#attributes' => array('class' => array('js-only')),
104
    );
105
    $form['expose_button']['checkbox']['checkbox'] = array(
106
      '#title' => t('Expose this sort to visitors, to allow them to change it'),
107
      '#type' => 'checkbox',
108
    );
109

    
110
    // Then add the button itself.
111
    if (empty($this->options['exposed'])) {
112
      $form['expose_button']['markup'] = array(
113
        '#markup' => '<div class="description exposed-description" style="float: left; margin-right:10px">' . t('This sort is not exposed. Expose it to allow the users to change it.') . '</div>',
114
      );
115
      $form['expose_button']['button'] = array(
116
        '#limit_validation_errors' => array(),
117
        '#type' => 'submit',
118
        '#value' => t('Expose sort'),
119
        '#submit' => array('views_ui_config_item_form_expose'),
120
      );
121
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
122
    }
123
    else {
124
      $form['expose_button']['markup'] = array(
125
        '#markup' => '<div class="description exposed-description">' . t('This sort is exposed. If you hide it, users will not be able to change it.') . '</div>',
126
      );
127
      $form['expose_button']['button'] = array(
128
        '#limit_validation_errors' => array(),
129
        '#type' => 'submit',
130
        '#value' => t('Hide sort'),
131
        '#submit' => array('views_ui_config_item_form_expose'),
132
      );
133
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
134
    }
135
  }
136

    
137
  /**
138
   * Simple validate handler.
139
   */
140
  public function options_validate(&$form, &$form_state) {
141
    $this->sort_validate($form, $form_state);
142
    if (!empty($this->options['exposed'])) {
143
      $this->expose_validate($form, $form_state);
144
    }
145

    
146
  }
147

    
148
  /**
149
   * Simple submit handler.
150
   */
151
  public function options_submit(&$form, &$form_state) {
152
    // Don't store this.
153
    unset($form_state['values']['expose_button']);
154

    
155
    $this->sort_submit($form, $form_state);
156
    if (!empty($this->options['exposed'])) {
157
      $this->expose_submit($form, $form_state);
158
    }
159
  }
160

    
161
  /**
162
   * Shortcut to display the value form.
163
   */
164
  public function show_sort_form(&$form, &$form_state) {
165
    $options = $this->sort_options();
166
    if (!empty($options)) {
167
      $form['order'] = array(
168
        '#type' => 'radios',
169
        '#options' => $options,
170
        '#default_value' => $this->options['order'],
171
      );
172
    }
173
  }
174

    
175
  /**
176
   * {@inheritdoc}
177
   */
178
  public function sort_validate(&$form, &$form_state) {
179
  }
180

    
181
  /**
182
   * {@inheritdoc}
183
   */
184
  public function sort_submit(&$form, &$form_state) {
185
  }
186

    
187
  /**
188
   * Provide a list of options for the default sort form.
189
   *
190
   * Should be overridden by classes that don't override sort_form.
191
   */
192
  public function sort_options() {
193
    return array(
194
      'ASC' => t('Sort ascending'),
195
      'DESC' => t('Sort descending'),
196
    );
197
  }
198

    
199
  /**
200
   * {@inheritdoc}
201
   */
202
  public function expose_form(&$form, &$form_state) {
203
    // #flatten will move everything from $form['expose'][$key] to $form[$key]
204
    // prior to rendering. That's why the pre_render for it needs to run first,
205
    // so that when the next pre_render (the one for fieldsets) runs, it gets
206
    // the flattened data.
207
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
208
    $form['expose']['#flatten'] = TRUE;
209

    
210
    $form['expose']['label'] = array(
211
      '#type' => 'textfield',
212
      '#default_value' => $this->options['expose']['label'],
213
      '#title' => t('Label'),
214
      '#required' => TRUE,
215
      '#size' => 40,
216
      '#weight' => -1,
217
    );
218
  }
219

    
220
  /**
221
   * Provide default options for exposed sorts.
222
   */
223
  public function expose_options() {
224
    $this->options['expose'] = array(
225
      'order' => $this->options['order'],
226
      'label' => $this->definition['title'],
227
    );
228
  }
229

    
230
}
231

    
232
/**
233
 * A special handler to take the place of missing or broken handlers.
234
 *
235
 * @ingroup views_sort_handlers
236
 */
237
class views_handler_sort_broken extends views_handler_sort {
238

    
239
  /**
240
   * {@inheritdoc}
241
   */
242
  public function ui_name($short = FALSE) {
243
    return t('Broken/missing handler');
244
  }
245

    
246
  /**
247
   * {@inheritdoc}
248
   */
249
  public function ensure_my_table() {
250
    // No table to ensure!
251
  }
252

    
253
  /**
254
   * {@inheritdoc}
255
   */
256
  public function query($group_by = FALSE) {
257
    // No query to run.
258
  }
259

    
260
  /**
261
   * {@inheritdoc}
262
   */
263
  public function options_form(&$form, &$form_state) {
264
    $form['markup'] = array(
265
      '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
266
    );
267
  }
268

    
269
  /**
270
   * Determine if the handler is considered 'broken'.
271
   */
272
  public function broken() {
273
    return TRUE;
274
  }
275

    
276
}
277

    
278

    
279
/**
280
 * @}
281
 */