Projet

Général

Profil

Paste
Télécharger (15,5 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

    
8
/**
9
 * The plugin to handle full pager.
10
 *
11
 * @ingroup views_pager_plugins
12
 */
13
class views_plugin_pager_full extends views_plugin_pager {
14
  function summary_title() {
15
    if (!empty($this->options['offset'])) {
16
      return format_plural($this->options['items_per_page'], '@count item, skip @skip', 'Paged, @count items, skip @skip', array('@count' => $this->options['items_per_page'], '@skip' => $this->options['offset']));
17
    }
18
      return format_plural($this->options['items_per_page'], '@count item', 'Paged, @count items', array('@count' => $this->options['items_per_page']));
19
  }
20

    
21
  function option_definition() {
22
    $options = parent::option_definition();
23
    $options['items_per_page'] = array('default' => 10);
24
    $options['offset'] = array('default' => 0);
25
    $options['id'] = array('default' => 0);
26
    $options['total_pages'] = array('default' => '');
27
    // Use the same default quantity that core uses by default.
28
    $options['quantity'] = array('default' => 9);
29
    $options['expose'] = array(
30
      'contains' => array(
31
        'items_per_page' => array('default' => FALSE, 'bool' => TRUE),
32
        'items_per_page_label' => array('default' => 'Items per page', 'translatable' => TRUE),
33
        'items_per_page_options' => array('default' => '5, 10, 20, 40, 60'),
34
        'items_per_page_options_all' => array('default' => FALSE, 'bool' => TRUE),
35
        'items_per_page_options_all_label' => array('default' => '- All -', 'translatable' => TRUE),
36

    
37
        'offset' => array('default' => FALSE, 'bool' => TRUE),
38
        'offset_label' => array('default' => 'Offset', 'translatable' => TRUE),
39
      ),
40
    );
41
    $options['tags'] = array(
42
      'contains' => array(
43
        'first' => array('default' => '« first', 'translatable' => TRUE),
44
        'previous' => array('default' => '‹ previous', 'translatable' => TRUE),
45
        'next' => array('default' => 'next ›', 'translatable' => TRUE),
46
        'last' => array('default' => 'last »', 'translatable' => TRUE),
47
      ),
48
    );
49
    return $options;
50
  }
51

    
52
  /**
53
   * Provide the default form for setting options.
54
   */
55
  function options_form(&$form, &$form_state) {
56
    parent::options_form($form, $form_state);
57
    $pager_text = $this->display->handler->get_pager_text();
58
    $form['items_per_page'] = array(
59
      '#title' => $pager_text['items per page title'],
60
      '#type' => 'textfield',
61
      '#description' => $pager_text['items per page description'],
62
      '#default_value' => $this->options['items_per_page'],
63
    );
64

    
65
    $form['offset'] = array(
66
      '#type' => 'textfield',
67
      '#title' => t('Offset'),
68
      '#description' => t('The number of items to skip. For example, if this field is 3, the first 3 items will be skipped and not displayed.'),
69
      '#default_value' => $this->options['offset'],
70
    );
71

    
72
    $form['id'] = array(
73
      '#type' => 'textfield',
74
      '#title' => t('Pager ID'),
75
      '#description' => t("Unless you're experiencing problems with pagers related to this view, you should leave this at 0. If using multiple pagers on one page you may need to set this number to a higher value so as not to conflict within the ?page= array. Large values will add a lot of commas to your URLs, so avoid if possible."),
76
      '#default_value' => $this->options['id'],
77
    );
78

    
79
    $form['total_pages'] = array(
80
      '#type' => 'textfield',
81
      '#title' => t('Number of pages'),
82
      '#description' => t('The total number of pages. Leave empty to show all pages.'),
83
      '#default_value' => $this->options['total_pages'],
84
    );
85

    
86
    $form['quantity'] = array(
87
      '#type' => 'textfield',
88
      '#title' => t('Number of pager links visible'),
89
      '#description' => t('Specify the number of links to pages to display in the pager.'),
90
      '#default_value' => $this->options['quantity'],
91
    );
92

    
93
    $form['tags'] = array (
94
      '#type' => 'fieldset',
95
      '#collapsible' => FALSE,
96
      '#collapsed' => FALSE,
97
      '#tree' => TRUE,
98
      '#title' => t('Tags'),
99
      '#input' => TRUE,
100
      '#description' => t('A lists of labels for the controls in the pager'),
101
    );
102

    
103
    $form['tags']['first'] = array(
104
      '#type' => 'textfield',
105
      '#title' => t('Text for "first"-link'),
106
      '#description' => t('Text for "first"-link'),
107
      '#default_value' => $this->options['tags']['first'],
108
    );
109

    
110
    $form['tags']['previous'] = array(
111
      '#type' => 'textfield',
112
      '#title' => t('Text for "previous"-link'),
113
      '#description' => t('Text for "previous"-link'),
114
      '#default_value' => $this->options['tags']['previous'],
115
    );
116

    
117
    $form['tags']['next'] = array(
118
      '#type' => 'textfield',
119
      '#title' => t('Text for "next"-link'),
120
      '#description' => t('Text for "next"-link'),
121
      '#default_value' => $this->options['tags']['next'],
122
    );
123

    
124
    $form['tags']['last'] = array(
125
      '#type' => 'textfield',
126
      '#title' => t('Text for "last"-link'),
127
      '#description' => t('Text for "last"-link'),
128
      '#default_value' => $this->options['tags']['last'],
129
    );
130

    
131
    $form['expose'] = array (
132
      '#type' => 'fieldset',
133
      '#collapsible' => FALSE,
134
      '#collapsed' => FALSE,
135
      '#tree' => TRUE,
136
      '#title' => t('Exposed options'),
137
      '#input' => TRUE,
138
      '#description' => t('Exposing this options allows users to define their values in a exposed form when view is displayed'),
139
    );
140

    
141
    $form['expose']['items_per_page'] = array(
142
      '#type' => 'checkbox',
143
      '#title' => t('Expose items per page'),
144
      '#description' => t('When checked, users can determine how many items per page show in a view'),
145
      '#default_value' => $this->options['expose']['items_per_page'],
146
    );
147

    
148
    $form['expose']['items_per_page_label'] = array(
149
      '#type' => 'textfield',
150
      '#title' => t('Items per page label'),
151
      '#required' => TRUE,
152
      '#description' => t('Label to use in the exposed items per page form element.'),
153
      '#default_value' => $this->options['expose']['items_per_page_label'],
154
      '#dependency' => array(
155
        'edit-pager-options-expose-items-per-page' => array(1)
156
      ),
157
    );
158

    
159
    $form['expose']['items_per_page_options'] = array(
160
      '#type' => 'textfield',
161
      '#title' => t('Exposed items per page options'),
162
      '#required' => TRUE,
163
      '#description' => t('Set between which values the user can choose when determining the items per page. Separated by comma.'),
164
      '#default_value' => $this->options['expose']['items_per_page_options'],
165
      '#dependency' => array(
166
        'edit-pager-options-expose-items-per-page' => array(1)
167
      ),
168
    );
169

    
170

    
171
    $form['expose']['items_per_page_options_all'] = array(
172
      '#type' => 'checkbox',
173
      '#title' => t('Include all items option'),
174
      '#description' => t('If checked, an extra item will be included to items per page to display all items'),
175
      '#default_value' => $this->options['expose']['items_per_page_options_all'],
176
    );
177

    
178
    $form['expose']['items_per_page_options_all_label'] = array(
179
      '#type' => 'textfield',
180
      '#title' => t('All items label'),
181
      '#description' => t('Which label will be used to display all items'),
182
      '#default_value' => $this->options['expose']['items_per_page_options_all_label'],
183
      '#dependency' => array(
184
        'edit-items-per-page-options-all' => array(1),
185
      ),
186
    );
187

    
188
    $form['expose']['offset'] = array(
189
      '#type' => 'checkbox',
190
      '#title' => t('Expose Offset'),
191
      '#description' => t('When checked, users can determine how many items should be skipped at the beginning.'),
192
      '#default_value' => $this->options['expose']['offset'],
193
    );
194

    
195
    $form['expose']['offset_label'] = array(
196
      '#type' => 'textfield',
197
      '#title' => t('Offset label'),
198
      '#required' => TRUE,
199
      '#description' => t('Label to use in the exposed offset form element.'),
200
      '#default_value' => $this->options['expose']['offset_label'],
201
      '#dependency' => array(
202
        'edit-pager-options-expose-offset' => array(1)
203
      ),
204
    );
205
  }
206

    
207
  function options_validate(&$form, &$form_state) {
208
    // Only accept integer values.
209
    $error = FALSE;
210
    $exposed_options = $form_state['values']['pager_options']['expose']['items_per_page_options'];
211
    if (strpos($exposed_options, '.') !== FALSE) {
212
      $error = TRUE;
213
    }
214
    $options = explode(',',$exposed_options);
215
    if (!$error && is_array($options)) {
216
      foreach ($options as $option) {
217
        if (!is_numeric($option) || intval($option) == 0) {
218
          $error = TRUE;
219
        }
220
      }
221
    }
222
    else {
223
      $error = TRUE;
224
    }
225
    if ($error) {
226
      form_set_error('pager_options][expose][items_per_page_options', t('Please insert a list of integer numeric values separated by commas: e.g: 10, 20, 50, 100'));
227
    }
228

    
229
    // Take sure that the items_per_page is part of the expose settings.
230
    if (!empty($form_state['values']['pager_options']['expose']['items_per_page']) && !empty($form_state['values']['pager_options']['items_per_page'])) {
231
      $items_per_page = $form_state['values']['pager_options']['items_per_page'];
232
      if (array_search($items_per_page, $options) === FALSE) {
233
        form_set_error('pager_options][expose][items_per_page_options', t('Please insert the items per page (@items_per_page) from above.',
234
          array('@items_per_page' => $items_per_page))
235
        );
236
      }
237
    }
238
  }
239

    
240
  function query() {
241
    if ($this->items_per_page_exposed()) {
242
      if (!empty($_GET['items_per_page']) && $_GET['items_per_page'] > 0) {
243
        $this->options['items_per_page'] = $_GET['items_per_page'];
244
      }
245
      elseif (!empty($_GET['items_per_page']) && $_GET['items_per_page'] == 'All' && $this->options['expose']['items_per_page_options_all']) {
246
        $this->options['items_per_page'] = 0;
247
      }
248
    }
249
    if ($this->offset_exposed()) {
250
      if (isset($_GET['offset']) && $_GET['offset'] >= 0) {
251
        $this->options['offset'] = $_GET['offset'];
252
      }
253
    }
254

    
255
    $limit = $this->options['items_per_page'];
256
    $offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
257
    if (!empty($this->options['total_pages'])) {
258
      if ($this->current_page >= $this->options['total_pages']) {
259
        $limit = $this->options['items_per_page'];
260
        $offset = $this->options['total_pages'] * $this->options['items_per_page'];
261
      }
262
    }
263

    
264
    $this->view->query->set_limit($limit);
265
    $this->view->query->set_offset($offset);
266
  }
267

    
268
  function render($input) {
269
    $pager_theme = views_theme_functions('pager', $this->view, $this->display);
270
    // The 0, 1, 3, 4 index are correct. See theme_pager documentation.
271
    $tags = array(
272
      0 => $this->options['tags']['first'],
273
      1 => $this->options['tags']['previous'],
274
      3 => $this->options['tags']['next'],
275
      4 => $this->options['tags']['last'],
276
    );
277
    $output = theme($pager_theme, array(
278
      'tags' => $tags,
279
      'element' => $this->get_pager_id(),
280
      'parameters' => $input,
281
      'quantity' => $this->options['quantity'],
282
    ));
283
    return $output;
284
  }
285

    
286
  /**
287
   * Set the current page.
288
   *
289
   * @param $number
290
   *   If provided, the page number will be set to this. If NOT provided,
291
   *   the page number will be set from the global page array.
292
   */
293
  function set_current_page($number = NULL) {
294
    if (isset($number)) {
295
      $this->current_page = $number;
296
      return;
297
    }
298

    
299
    // If the current page number was not specified, extract it from the global
300
    // page array.
301
    global $pager_page_array;
302

    
303
    if (empty($pager_page_array)) {
304
      $pager_page_array = array();
305
    }
306

    
307
    // Fill in missing values in the global page array, in case the global page
308
    // array hasn't been initialized before.
309
    $page = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
310

    
311
    $pager_id = $this->get_pager_id();
312
    for ($i = 0; $i <= $pager_id || $i < count($pager_page_array); $i++) {
313
      $pager_page_array[$i] = empty($page[$i]) ? 0 : $page[$i];
314
    }
315

    
316
    $this->current_page = intval($pager_page_array[$pager_id]);
317

    
318
    if ($this->current_page < 0) {
319
      $this->current_page = 0;
320
    }
321
  }
322

    
323
  function get_pager_total() {
324
    if ($items_per_page = intval($this->get_items_per_page())) {
325
      return ceil($this->total_items / $items_per_page);
326
    }
327
    else {
328
      return 1;
329
    }
330
  }
331

    
332
  /**
333
   * Update global paging info.
334
   *
335
   * This is called after the count query has been run to set the total
336
   * items available and to update the current page if the requested
337
   * page is out of range.
338
   */
339
  function update_page_info() {
340
    if (!empty($this->options['total_pages'])) {
341
      if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
342
        $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
343
      }
344
    }
345

    
346
    // Don't set pager settings for items per page = 0.
347
    $items_per_page = $this->get_items_per_page();
348
    if (!empty($items_per_page)) {
349
      // Dump information about what we already know into the globals.
350
      global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
351
      // Set the limit.
352
      $pager_id = $this->get_pager_id();
353
      $pager_limits[$pager_id] = $this->options['items_per_page'];
354
      // Set the item count for the pager.
355
      $pager_total_items[$pager_id] = $this->total_items;
356
      // Calculate and set the count of available pages.
357
      $pager_total[$pager_id] = $this->get_pager_total();
358

    
359
      // See if the requested page was within range:
360
      if ($this->current_page < 0) {
361
        $this->current_page = 0;
362
      }
363
      else if ($this->current_page >= $pager_total[$pager_id]) {
364
        // Pages are numbered from 0 so if there are 10 pages, the last page is 9.
365
        $this->current_page = $pager_total[$pager_id] - 1;
366
      }
367

    
368
      // Put this number in to guarantee that we do not generate notices when the pager
369
      // goes to look for it later.
370
      $pager_page_array[$pager_id] = $this->current_page;
371
    }
372
  }
373

    
374
  function uses_exposed() {
375
    return $this->items_per_page_exposed() || $this->offset_exposed();
376
  }
377

    
378
  function items_per_page_exposed() {
379
    return !empty($this->options['expose']['items_per_page']);
380
  }
381

    
382
  function offset_exposed() {
383
    return !empty($this->options['expose']['offset']);
384
  }
385

    
386
  function exposed_form_alter(&$form, &$form_state) {
387
    if ($this->items_per_page_exposed()) {
388
      $options = explode(',', $this->options['expose']['items_per_page_options']);
389
      $sanitized_options = array();
390
      if (is_array($options)) {
391
        foreach ($options as $option) {
392
          $sanitized_options[intval($option)] = intval($option);
393
        }
394
        if (!empty($this->options['expose']['items_per_page_options_all']) && !empty($this->options['expose']['items_per_page_options_all_label'])) {
395
          $sanitized_options['All'] = $this->options['expose']['items_per_page_options_all_label'];
396
        }
397
        $form['items_per_page'] = array(
398
          '#type' => 'select',
399
          '#title' => $this->options['expose']['items_per_page_label'],
400
          '#options' => $sanitized_options,
401
          '#default_value' => $this->get_items_per_page(),
402
        );
403
      }
404
    }
405

    
406
    if ($this->offset_exposed()) {
407
      $form['offset'] = array(
408
        '#type' => 'textfield',
409
        '#size' => 10,
410
        '#maxlength' => 10,
411
        '#title' => $this->options['expose']['offset_label'],
412
        '#default_value' => $this->get_offset(),
413
      );
414
    }
415
  }
416

    
417
  function exposed_form_validate(&$form, &$form_state) {
418
    if (!empty($form_state['values']['offset']) && trim($form_state['values']['offset'])) {
419
      if (!is_numeric($form_state['values']['offset']) || $form_state['values']['offset'] < 0) {
420
        form_set_error('offset', t('Offset must be an number greather or equal than 0.'));
421
      }
422
    }
423
  }
424
}