Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_counter.inc @ 4003efde

1
<?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

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20
    $options['counter_start'] = array('default' => 1);
21
    $options['reverse'] = array('default' => FALSE);
22
    return $options;
23
  }
24

    
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public function options_form(&$form, &$form_state) {
29
    $form['counter_start'] = array(
30
      '#type' => 'textfield',
31
      '#title' => t('Starting value'),
32
      '#default_value' => $this->options['counter_start'],
33
      '#description' => t('Specify the number the counter should start at.'),
34
      '#size' => 2,
35
    );
36

    
37
    $form['reverse'] = array(
38
      '#type' => 'checkbox',
39
      '#title' => t('Reverse'),
40
      '#default_value' => $this->options['reverse'],
41
      '#description' => t('Reverse the counter.'),
42
    );
43

    
44
    parent::options_form($form, $form_state);
45
  }
46

    
47
  /**
48
   * {@inheritdoc}
49
   */
50
  public function query() {
51
    // Do nothing -- to override the parent query.
52
  }
53

    
54
  /**
55
   * {@inheritdoc}
56
   */
57
  public function render($values) {
58
    $reverse = empty($this->options['reverse']) ? 1 : -1;
59

    
60
    // Note: 1 is subtracted from the counter start value below because the
61
    // counter value is incremented by 1 at the end of this function.
62
    $counter_start = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] : 0;
63
    $count = ($reverse == -1) ? count($this->view->result) + $counter_start : $counter_start - 1;
64
    $pager = $this->view->query->pager;
65

    
66
    // Get the base count of the pager.
67
    if ($pager->use_pager()) {
68
      if ($reverse == -1) {
69
        $count = ($pager->total_items + $counter_start - ($pager->get_current_page() * $pager->get_items_per_page()) + $pager->get_offset());
70
      }
71
      else {
72
        $count += (($pager->get_items_per_page() * $pager->get_current_page() + $pager->get_offset())) * $reverse;
73
      }
74
    }
75
    // Add the counter for the current site.
76
    $count += ($this->view->row_index + 1) * $reverse;
77

    
78
    return $count;
79
  }
80

    
81
}