Projet

Général

Profil

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

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

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

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function summary_title() {
19
    if (!empty($this->options['offset'])) {
20
      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']));
21
    }
22
    return format_plural($this->options['items_per_page'], '@count item', 'Paged, @count items', array('@count' => $this->options['items_per_page']));
23
  }
24

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

    
44
        'offset' => array('default' => FALSE, 'bool' => TRUE),
45
        'offset_label' => array('default' => 'Offset', 'translatable' => TRUE),
46
      ),
47
    );
48
    $options['tags'] = array(
49
      'contains' => array(
50
        'first' => array('default' => '« first', 'translatable' => TRUE),
51
        'previous' => array('default' => '‹ previous', 'translatable' => TRUE),
52
        'next' => array('default' => 'next ›', 'translatable' => TRUE),
53
        'last' => array('default' => 'last »', 'translatable' => TRUE),
54
      ),
55
    );
56
    return $options;
57
  }
58

    
59
  /**
60
   * Provide the default form for setting options.
61
   */
62
  public function options_form(&$form, &$form_state) {
63
    parent::options_form($form, $form_state);
64
    $pager_text = $this->display->handler->get_pager_text();
65
    $form['items_per_page'] = array(
66
      '#title' => $pager_text['items per page title'],
67
      '#type' => 'textfield',
68
      '#description' => $pager_text['items per page description'],
69
      '#default_value' => $this->options['items_per_page'],
70
    );
71

    
72
    $form['offset'] = array(
73
      '#type' => 'textfield',
74
      '#title' => t('Offset'),
75
      '#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.'),
76
      '#default_value' => $this->options['offset'],
77
    );
78

    
79
    $form['id'] = array(
80
      '#type' => 'textfield',
81
      '#title' => t('Pager ID'),
82
      '#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."),
83
      '#default_value' => $this->options['id'],
84
    );
85

    
86
    $form['total_pages'] = array(
87
      '#type' => 'textfield',
88
      '#title' => t('Number of pages'),
89
      '#description' => t('The total number of pages. Leave empty to show all pages.'),
90
      '#default_value' => $this->options['total_pages'],
91
    );
92

    
93
    $form['quantity'] = array(
94
      '#type' => 'textfield',
95
      '#title' => t('Number of pager links visible'),
96
      '#description' => t('Specify the number of links to pages to display in the pager.'),
97
      '#default_value' => $this->options['quantity'],
98
    );
99

    
100
    $form['tags'] = array(
101
      '#type' => 'fieldset',
102
      '#collapsible' => FALSE,
103
      '#collapsed' => FALSE,
104
      '#tree' => TRUE,
105
      '#title' => t('Tags'),
106
      '#input' => TRUE,
107
      '#description' => t('A lists of labels for the controls in the pager'),
108
    );
109

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

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

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

    
131
    $form['tags']['last'] = array(
132
      '#type' => 'textfield',
133
      '#title' => t('Text for "last"-link'),
134
      '#description' => t('Text for "last"-link'),
135
      '#default_value' => $this->options['tags']['last'],
136
    );
137

    
138
    $form['expose'] = array(
139
      '#type' => 'fieldset',
140
      '#collapsible' => FALSE,
141
      '#collapsed' => FALSE,
142
      '#tree' => TRUE,
143
      '#title' => t('Exposed options'),
144
      '#input' => TRUE,
145
      '#description' => t('Exposing this options allows users to define their values in a exposed form when view is displayed'),
146
    );
147

    
148
    $form['expose']['items_per_page'] = array(
149
      '#type' => 'checkbox',
150
      '#title' => t('Expose items per page'),
151
      '#description' => t('When checked, users can determine how many items per page show in a view'),
152
      '#default_value' => $this->options['expose']['items_per_page'],
153
    );
154

    
155
    $form['expose']['items_per_page_label'] = array(
156
      '#type' => 'textfield',
157
      '#title' => t('Items per page label'),
158
      '#required' => TRUE,
159
      '#description' => t('Label to use in the exposed items per page form element.'),
160
      '#default_value' => $this->options['expose']['items_per_page_label'],
161
      '#dependency' => array(
162
        'edit-pager-options-expose-items-per-page' => array(1),
163
      ),
164
    );
165

    
166
    $form['expose']['items_per_page_options'] = array(
167
      '#type' => 'textfield',
168
      '#title' => t('Exposed items per page options'),
169
      '#required' => TRUE,
170
      '#description' => t('Set between which values the user can choose when determining the items per page. Separated by comma.'),
171
      '#default_value' => $this->options['expose']['items_per_page_options'],
172
      '#dependency' => array(
173
        'edit-pager-options-expose-items-per-page' => array(1),
174
      ),
175
    );
176

    
177
    $form['expose']['items_per_page_options_all'] = array(
178
      '#type' => 'checkbox',
179
      '#title' => t('Include all items option'),
180
      '#description' => t('If checked, an extra item will be included to items per page to display all items'),
181
      '#default_value' => $this->options['expose']['items_per_page_options_all'],
182
    );
183

    
184
    $form['expose']['items_per_page_options_all_label'] = array(
185
      '#type' => 'textfield',
186
      '#title' => t('All items label'),
187
      '#description' => t('Which label will be used to display all items'),
188
      '#default_value' => $this->options['expose']['items_per_page_options_all_label'],
189
      '#dependency' => array(
190
        'edit-items-per-page-options-all' => array(1),
191
      ),
192
    );
193

    
194
    $form['expose']['offset'] = array(
195
      '#type' => 'checkbox',
196
      '#title' => t('Expose Offset'),
197
      '#description' => t('When checked, users can determine how many items should be skipped at the beginning.'),
198
      '#default_value' => $this->options['expose']['offset'],
199
    );
200

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

    
213
  /**
214
   * {@inheritdoc}
215
   */
216
  public function options_validate(&$form, &$form_state) {
217
    // Only accept integer values.
218
    $error = FALSE;
219
    $exposed_options = $form_state['values']['pager_options']['expose']['items_per_page_options'];
220
    if (strpos($exposed_options, '.') !== FALSE) {
221
      $error = TRUE;
222
    }
223
    $options = explode(',', $exposed_options);
224
    if (!$error && is_array($options)) {
225
      foreach ($options as $option) {
226
        if (!is_numeric($option) || intval($option) == 0) {
227
          $error = TRUE;
228
        }
229
      }
230
    }
231
    else {
232
      $error = TRUE;
233
    }
234
    if ($error) {
235
      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'));
236
    }
237

    
238
    // Take sure that the items_per_page is part of the expose settings.
239
    if (!empty($form_state['values']['pager_options']['expose']['items_per_page']) && !empty($form_state['values']['pager_options']['items_per_page'])) {
240
      $items_per_page = $form_state['values']['pager_options']['items_per_page'];
241
      if (array_search($items_per_page, $options) === FALSE) {
242
        form_set_error('pager_options][expose][items_per_page_options', t('Please insert the items per page (@items_per_page) from above.',
243
          array('@items_per_page' => $items_per_page))
244
        );
245
      }
246
    }
247
  }
248

    
249
  /**
250
   * {@inheritdoc}
251
   */
252
  public function query() {
253
    if ($this->items_per_page_exposed()) {
254
      if (!empty($_GET['items_per_page']) && $_GET['items_per_page'] > 0) {
255
        $this->options['items_per_page'] = $_GET['items_per_page'];
256
      }
257
      elseif (!empty($_GET['items_per_page']) && $_GET['items_per_page'] == 'All' && $this->options['expose']['items_per_page_options_all']) {
258
        $this->options['items_per_page'] = 0;
259
      }
260
    }
261
    if ($this->offset_exposed()) {
262
      if (isset($_GET['offset']) && $_GET['offset'] >= 0) {
263
        $this->options['offset'] = $_GET['offset'];
264
      }
265
    }
266

    
267
    $limit = $this->options['items_per_page'];
268
    $offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
269
    if (!empty($this->options['total_pages'])) {
270
      if ($this->current_page >= $this->options['total_pages']) {
271
        $limit = $this->options['items_per_page'];
272
        $offset = $this->options['total_pages'] * $this->options['items_per_page'];
273
      }
274
    }
275

    
276
    $this->view->query->set_limit($limit);
277
    $this->view->query->set_offset($offset);
278
  }
279

    
280
  /**
281
   * {@inheritdoc}
282
   */
283
  public function render($input) {
284
    $pager_theme = views_theme_functions('pager', $this->view, $this->display);
285
    // The 0, 1, 3, 4 index are correct. See theme_pager documentation.
286
    $tags = array(
287
      0 => $this->options['tags']['first'],
288
      1 => $this->options['tags']['previous'],
289
      3 => $this->options['tags']['next'],
290
      4 => $this->options['tags']['last'],
291
    );
292
    $output = theme($pager_theme, array(
293
      'tags' => $tags,
294
      'element' => $this->get_pager_id(),
295
      'parameters' => $input,
296
      'quantity' => $this->options['quantity'],
297
    ));
298
    return $output;
299
  }
300

    
301
  /**
302
   * Set the current page.
303
   *
304
   * @param int $number
305
   *   If provided, the page number will be set to this. If NOT provided,
306
   *   the page number will be set from the global page array.
307
   */
308
  public function set_current_page($number = NULL) {
309
    if (isset($number)) {
310
      $this->current_page = $number;
311
      return;
312
    }
313

    
314
    // If the current page number was not specified, extract it from the global
315
    // page array.
316
    global $pager_page_array;
317

    
318
    if (empty($pager_page_array)) {
319
      $pager_page_array = array();
320
    }
321

    
322
    // Fill in missing values in the global page array, in case the global page
323
    // array hasn't been initialized before.
324
    $page = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
325

    
326
    $pager_id = $this->get_pager_id();
327
    for ($i = 0; $i <= $pager_id || $i < count($pager_page_array); $i++) {
328
      $pager_page_array[$i] = empty($page[$i]) ? 0 : $page[$i];
329
    }
330

    
331
    $this->current_page = intval($pager_page_array[$pager_id]);
332

    
333
    if ($this->current_page < 0) {
334
      $this->current_page = 0;
335
    }
336
  }
337

    
338
  /**
339
   * {@inheritdoc}
340
   */
341
  public function get_pager_total() {
342
    if ($items_per_page = intval($this->get_items_per_page())) {
343
      return ceil($this->total_items / $items_per_page);
344
    }
345
    else {
346
      return 1;
347
    }
348
  }
349

    
350
  /**
351
   * Update global paging info.
352
   *
353
   * This is called after the count query has been run to set the total
354
   * items available and to update the current page if the requested
355
   * page is out of range.
356
   */
357
  public function update_page_info() {
358
    if (!empty($this->options['total_pages'])) {
359
      if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
360
        $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
361
      }
362
    }
363

    
364
    // Don't set pager settings for items per page = 0.
365
    $items_per_page = $this->get_items_per_page();
366
    if (!empty($items_per_page)) {
367
      // Dump information about what we already know into the globals.
368
      global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
369
      // Set the limit.
370
      $pager_id = $this->get_pager_id();
371
      $pager_limits[$pager_id] = $this->options['items_per_page'];
372
      // Set the item count for the pager.
373
      $pager_total_items[$pager_id] = $this->total_items;
374
      // Calculate and set the count of available pages.
375
      $pager_total[$pager_id] = $this->get_pager_total();
376

    
377
      // See if the requested page was within range.
378
      if ($this->current_page < 0) {
379
        $this->current_page = 0;
380
      }
381
      elseif ($this->current_page >= $pager_total[$pager_id]) {
382
        // Pages are numbered from 0 so if there are 10 pages, the last page is
383
        // 9.
384
        $this->current_page = $pager_total[$pager_id] - 1;
385
      }
386

    
387
      // Put this number in to guarantee that we do not generate notices when
388
      // the pager goes to look for it later.
389
      $pager_page_array[$pager_id] = $this->current_page;
390
    }
391
  }
392

    
393
  /**
394
   *
395
   */
396
  public function uses_exposed() {
397
    return $this->items_per_page_exposed() || $this->offset_exposed();
398
  }
399

    
400
  /**
401
   *
402
   */
403
  public function items_per_page_exposed() {
404
    return !empty($this->options['expose']['items_per_page']);
405
  }
406

    
407
  /**
408
   *
409
   */
410
  public function offset_exposed() {
411
    return !empty($this->options['expose']['offset']);
412
  }
413

    
414
  /**
415
   * {@inheritdoc}
416
   */
417
  public function exposed_form_alter(&$form, &$form_state) {
418
    if ($this->items_per_page_exposed()) {
419
      $options = explode(',', $this->options['expose']['items_per_page_options']);
420
      $sanitized_options = array();
421
      if (is_array($options)) {
422
        foreach ($options as $option) {
423
          $sanitized_options[intval($option)] = intval($option);
424
        }
425
        if (!empty($this->options['expose']['items_per_page_options_all']) && !empty($this->options['expose']['items_per_page_options_all_label'])) {
426
          $sanitized_options['All'] = $this->options['expose']['items_per_page_options_all_label'];
427
        }
428
        $form['items_per_page'] = array(
429
          '#type' => 'select',
430
          '#title' => $this->options['expose']['items_per_page_label'],
431
          '#options' => $sanitized_options,
432
          '#default_value' => $this->get_items_per_page(),
433
        );
434
      }
435
    }
436

    
437
    if ($this->offset_exposed()) {
438
      $form['offset'] = array(
439
        '#type' => 'textfield',
440
        '#size' => 10,
441
        '#maxlength' => 10,
442
        '#title' => $this->options['expose']['offset_label'],
443
        '#default_value' => $this->get_offset(),
444
      );
445
    }
446
  }
447

    
448
  /**
449
   * {@inheritdoc}
450
   */
451
  public function exposed_form_validate(&$form, &$form_state) {
452
    if (!empty($form_state['values']['offset']) && trim($form_state['values']['offset'])) {
453
      if (!is_numeric($form_state['values']['offset']) || $form_state['values']['offset'] < 0) {
454
        form_set_error('offset', t('Offset must be a number greater than or equal to 0.'));
455
      }
456
    }
457
  }
458

    
459
}