Projet

Général

Profil

Paste
Télécharger (6,73 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / views / handlers / views_handler_sort.inc @ 4543c6c7

1
<?php
2

    
3
/**
4
 * @file
5
 * @todo.
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
  function can_expose() { return TRUE; }
25

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

    
35
  function option_definition() {
36
    $options = parent::option_definition();
37

    
38
    $options['order'] = array('default' => 'ASC');
39
    $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
40
    $options['expose'] = array(
41
      'contains' => array(
42
        'label' => array('default' => '', 'translatable' => TRUE),
43
      ),
44
    );
45
    return $options;
46
  }
47

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

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

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

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

    
106
    // Then add the button itself.
107
    if (empty($this->options['exposed'])) {
108
      $form['expose_button']['markup'] = array(
109
        '#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>',
110
      );
111
      $form['expose_button']['button'] = array(
112
        '#limit_validation_errors' => array(),
113
        '#type' => 'submit',
114
        '#value' => t('Expose sort'),
115
        '#submit' => array('views_ui_config_item_form_expose'),
116
      );
117
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
118
    }
119
    else {
120
      $form['expose_button']['markup'] = array(
121
        '#markup' => '<div class="description exposed-description">' . t('This sort is exposed. If you hide it, users will not be able to change it.') . '</div>',
122
      );
123
      $form['expose_button']['button'] = array(
124
        '#limit_validation_errors' => array(),
125
        '#type' => 'submit',
126
        '#value' => t('Hide sort'),
127
        '#submit' => array('views_ui_config_item_form_expose'),
128
      );
129
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
130
    }
131
  }
132

    
133
  /**
134
   * Simple validate handler
135
   */
136
  function options_validate(&$form, &$form_state) {
137
    $this->sort_validate($form, $form_state);
138
    if (!empty($this->options['exposed'])) {
139
      $this->expose_validate($form, $form_state);
140
    }
141

    
142
  }
143

    
144
  /**
145
   * Simple submit handler
146
   */
147
  function options_submit(&$form, &$form_state) {
148
    unset($form_state['values']['expose_button']); // don't store this.
149
    $this->sort_submit($form, $form_state);
150
    if (!empty($this->options['exposed'])) {
151
      $this->expose_submit($form, $form_state);
152
    }
153
  }
154

    
155
  /**
156
   * Shortcut to display the value form.
157
   */
158
  function show_sort_form(&$form, &$form_state) {
159
    $options = $this->sort_options();
160
    if (!empty($options)) {
161
      $form['order'] = array(
162
        '#type' => 'radios',
163
        '#options' => $options,
164
        '#default_value' => $this->options['order'],
165
      );
166
    }
167
  }
168

    
169
  function sort_validate(&$form, &$form_state) { }
170

    
171
  function sort_submit(&$form, &$form_state) { }
172

    
173
  /**
174
   * Provide a list of options for the default sort form.
175
   * Should be overridden by classes that don't override sort_form
176
   */
177
  function sort_options() {
178
    return array(
179
      'ASC' => t('Sort ascending'),
180
      'DESC' => t('Sort descending'),
181
    );
182
  }
183

    
184
  function expose_form(&$form, &$form_state) {
185
    // #flatten will move everything from $form['expose'][$key] to $form[$key]
186
    // prior to rendering. That's why the pre_render for it needs to run first,
187
    // so that when the next pre_render (the one for fieldsets) runs, it gets
188
    // the flattened data.
189
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
190
    $form['expose']['#flatten'] = TRUE;
191

    
192
    $form['expose']['label'] = array(
193
      '#type' => 'textfield',
194
      '#default_value' => $this->options['expose']['label'],
195
      '#title' => t('Label'),
196
      '#required' => TRUE,
197
      '#size' => 40,
198
      '#weight' => -1,
199
   );
200
  }
201

    
202
  /**
203
   * Provide default options for exposed sorts.
204
   */
205
  function expose_options() {
206
    $this->options['expose'] = array(
207
      'order' => $this->options['order'],
208
      'label' => $this->definition['title'],
209
    );
210
  }
211
}
212

    
213
/**
214
 * A special handler to take the place of missing or broken handlers.
215
 *
216
 * @ingroup views_sort_handlers
217
 */
218
class views_handler_sort_broken extends views_handler_sort {
219
  function ui_name($short = FALSE) {
220
    return t('Broken/missing handler');
221
  }
222

    
223
  function ensure_my_table() { /* No table to ensure! */ }
224
  function query($group_by = FALSE) { /* No query to run */ }
225
  function options_form(&$form, &$form_state) {
226
    $form['markup'] = array(
227
      '#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>',
228
    );
229
  }
230

    
231
  /**
232
   * Determine if the handler is considered 'broken'
233
   */
234
  function broken() { return TRUE; }
235
}
236

    
237

    
238
/**
239
 * @}
240
 */