Projet

Général

Profil

Paste
Télécharger (2,03 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_counter.inc @ 651307cd

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Definition of views_handler_field_counter.
6
 */
7
8
/**
9
 * Field handler to show a counter of the current row.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_counter extends views_handler_field {
14
  function option_definition() {
15
    $options = parent::option_definition();
16
    $options['counter_start'] = array('default' => 1);
17 13c3c9b4 Assos Assos
    $options['reverse'] = array('default' => FALSE);
18 85ad3d82 Assos Assos
    return $options;
19
  }
20
21
  function options_form(&$form, &$form_state) {
22
    $form['counter_start'] = array(
23
      '#type' => 'textfield',
24
      '#title' => t('Starting value'),
25
      '#default_value' => $this->options['counter_start'],
26
      '#description' => t('Specify the number the counter should start at.'),
27
      '#size' => 2,
28
    );
29
30 13c3c9b4 Assos Assos
    $form['reverse'] = array(
31
      '#type' => 'checkbox',
32
      '#title' => t('Reverse'),
33
      '#default_value' => $this->options['reverse'],
34
      '#description' => t('Reverse the counter.'),
35
    );
36
37 85ad3d82 Assos Assos
    parent::options_form($form, $form_state);
38
  }
39
40
  function query() {
41
    // do nothing -- to override the parent query.
42
  }
43
44
  function render($values) {
45 13c3c9b4 Assos Assos
    $reverse = empty($this->options['reverse']) ? 1 : -1;
46
47 85ad3d82 Assos Assos
    // Note:  1 is subtracted from the counter start value below because the
48
    // counter value is incremented by 1 at the end of this function.
49 13c3c9b4 Assos Assos
    $counter_start = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] : 0;
50
    $count = ($reverse == -1) ? count($this->view->result) + $counter_start : $counter_start -1;
51 85ad3d82 Assos Assos
    $pager = $this->view->query->pager;
52 13c3c9b4 Assos Assos
53 85ad3d82 Assos Assos
    // Get the base count of the pager.
54
    if ($pager->use_pager()) {
55 13c3c9b4 Assos Assos
      if ($reverse == -1) {
56
        $count = ($pager->total_items + $counter_start - ($pager->get_current_page() * $pager->get_items_per_page()) + $pager->get_offset());
57
      } else {
58
        $count += (($pager->get_items_per_page() * $pager->get_current_page() + $pager->get_offset())) * $reverse;
59
      }
60 85ad3d82 Assos Assos
    }
61
    // Add the counter for the current site.
62 13c3c9b4 Assos Assos
    $count += ($this->view->row_index + 1) * $reverse;
63 85ad3d82 Assos Assos
64
    return $count;
65
  }
66
}