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
|
function option_definition() {
|
15
|
$options = parent::option_definition();
|
16
|
$options['counter_start'] = array('default' => 1);
|
17
|
$options['reverse'] = array('default' => FALSE);
|
18
|
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
|
$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
|
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
|
$reverse = empty($this->options['reverse']) ? 1 : -1;
|
46
|
|
47
|
// 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
|
$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
|
$pager = $this->view->query->pager;
|
52
|
|
53
|
// Get the base count of the pager.
|
54
|
if ($pager->use_pager()) {
|
55
|
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
|
}
|
61
|
// Add the counter for the current site.
|
62
|
$count += ($this->view->row_index + 1) * $reverse;
|
63
|
|
64
|
return $count;
|
65
|
}
|
66
|
}
|