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 |
5d12d676
|
Assos Assos
|
|
15 |
|
|
/**
|
16 |
|
|
* {@inheritdoc}
|
17 |
|
|
*/
|
18 |
|
|
public function option_definition() {
|
19 |
85ad3d82
|
Assos Assos
|
$options = parent::option_definition();
|
20 |
|
|
$options['counter_start'] = array('default' => 1);
|
21 |
13c3c9b4
|
Assos Assos
|
$options['reverse'] = array('default' => FALSE);
|
22 |
85ad3d82
|
Assos Assos
|
return $options;
|
23 |
|
|
}
|
24 |
|
|
|
25 |
5d12d676
|
Assos Assos
|
/**
|
26 |
|
|
* {@inheritdoc}
|
27 |
|
|
*/
|
28 |
|
|
public function options_form(&$form, &$form_state) {
|
29 |
85ad3d82
|
Assos Assos
|
$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 |
13c3c9b4
|
Assos Assos
|
$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 |
85ad3d82
|
Assos Assos
|
parent::options_form($form, $form_state);
|
45 |
|
|
}
|
46 |
|
|
|
47 |
5d12d676
|
Assos Assos
|
/**
|
48 |
|
|
* {@inheritdoc}
|
49 |
|
|
*/
|
50 |
|
|
public function query() {
|
51 |
|
|
// Do nothing -- to override the parent query.
|
52 |
85ad3d82
|
Assos Assos
|
}
|
53 |
|
|
|
54 |
5d12d676
|
Assos Assos
|
/**
|
55 |
|
|
* {@inheritdoc}
|
56 |
|
|
*/
|
57 |
|
|
public function render($values) {
|
58 |
13c3c9b4
|
Assos Assos
|
$reverse = empty($this->options['reverse']) ? 1 : -1;
|
59 |
|
|
|
60 |
5d12d676
|
Assos Assos
|
// Note: 1 is subtracted from the counter start value below because the
|
61 |
85ad3d82
|
Assos Assos
|
// counter value is incremented by 1 at the end of this function.
|
62 |
13c3c9b4
|
Assos Assos
|
$counter_start = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] : 0;
|
63 |
5d12d676
|
Assos Assos
|
$count = ($reverse == -1) ? count($this->view->result) + $counter_start : $counter_start - 1;
|
64 |
85ad3d82
|
Assos Assos
|
$pager = $this->view->query->pager;
|
65 |
13c3c9b4
|
Assos Assos
|
|
66 |
85ad3d82
|
Assos Assos
|
// Get the base count of the pager.
|
67 |
|
|
if ($pager->use_pager()) {
|
68 |
13c3c9b4
|
Assos Assos
|
if ($reverse == -1) {
|
69 |
|
|
$count = ($pager->total_items + $counter_start - ($pager->get_current_page() * $pager->get_items_per_page()) + $pager->get_offset());
|
70 |
5d12d676
|
Assos Assos
|
}
|
71 |
|
|
else {
|
72 |
13c3c9b4
|
Assos Assos
|
$count += (($pager->get_items_per_page() * $pager->get_current_page() + $pager->get_offset())) * $reverse;
|
73 |
|
|
}
|
74 |
85ad3d82
|
Assos Assos
|
}
|
75 |
|
|
// Add the counter for the current site.
|
76 |
13c3c9b4
|
Assos Assos
|
$count += ($this->view->row_index + 1) * $reverse;
|
77 |
85ad3d82
|
Assos Assos
|
|
78 |
|
|
return $count;
|
79 |
|
|
}
|
80 |
5d12d676
|
Assos Assos
|
|
81 |
85ad3d82
|
Assos Assos
|
} |