View source
<?php
function views_simple_pager_views_plugins() {
return array(
'pager' => array(
'views_simple_pager' => array(
'title' => t('Paged output using Views Simple Pager'),
'short title' => t('Views Simple Pager'),
'help' => t('Use the Views Simple Pager output.'),
'handler' => 'views_simple_pager',
'uses options' => TRUE,
'parent' => 'full',
),
),
);
}
class views_simple_pager extends views_plugin_pager {
function summary_title() {
$name = t('Simple pager');
if (!empty($this->options['offset'])) {
return format_plural($this->options['items_per_page'], '@name, @count item, skip @skip', '@name, @count items, skip @skip', array(
'@name' => $name,
'@count' => $this->options['items_per_page'],
'@skip' => $this->options['offset'],
));
}
return format_plural($this->options['items_per_page'], '@name, @count item', 'Simple pager, @count items', array(
'@name' => $name,
'@count' => $this->options['items_per_page'],
));
}
function option_definition() {
$options = parent::option_definition();
$options['items_per_page'] = array(
'default' => 10,
);
$options['offset'] = array(
'default' => 0,
);
$options['id'] = array(
'default' => 0,
);
$options['total_pages'] = array(
'default' => '',
);
$options['prev_label'] = array(
'default' => 'previous',
'translatable' => TRUE,
);
$options['next_label'] = array(
'default' => 'next',
'translatable' => TRUE,
);
$options['reverse_links'] = array(
'default' => FALSE,
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$pager_text = $this->display->handler
->get_pager_text();
$form['items_per_page'] = array(
'#title' => $pager_text['items per page title'],
'#type' => 'textfield',
'#description' => $pager_text['items per page description'],
'#default_value' => $this->options['items_per_page'],
);
$form['offset'] = array(
'#type' => 'textfield',
'#title' => t('Offset'),
'#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.'),
'#default_value' => $this->options['offset'],
);
$form['id'] = array(
'#type' => 'textfield',
'#title' => t('Pager ID'),
'#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."),
'#default_value' => $this->options['id'],
);
$form['total_pages'] = array(
'#type' => 'textfield',
'#title' => t('Number of pages'),
'#description' => t('The total number of pages. Leave empty to show all pages.'),
'#default_value' => $this->options['total_pages'],
);
$form['prev_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for "previous" pager link'),
'#description' => t('The label to use for the traditional "previous" link that traverses towards the top of the list.'),
'#default_value' => $this->options['prev_label'],
);
$form['next_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for "next" pager link'),
'#description' => t('The label to use for the traditional "next" link that traverses towards the bottom of the list.'),
'#default_value' => $this->options['next_label'],
);
$form['reverse_links'] = array(
'#type' => 'checkbox',
'#title' => t('Reverse placement of "previous" and "next" links'),
'#description' => t('If checked, this option will reverse the "traditional" link layout (i.e. "next" will come first, then "previous")'),
'#default_value' => $this->options['reverse_links'],
);
}
function options_validate(&$form, &$form_state) {
$err_elems = array();
if (!is_numeric($form_state['values']['pager_options']['items_per_page'])) {
$err_elems[] = 'pager_options][items_per_page';
}
if (!empty($form_state['values']['pager_options']['offset']) && !is_numeric($form_state['values']['pager_options']['offset'])) {
$err_elems[] = 'pager_options][offset';
}
if (!empty($form_state['values']['pager_options']['total_pages']) && !is_numeric($form_state['values']['pager_options']['total_pages'])) {
$err_elems[] = 'pager_options][total_pages';
}
foreach ($err_elems as $el) {
form_set_error($el, t('Please insert a non-negative integer value'));
}
if (empty($form_state['values']['pager_options']['prev_label'])) {
form_set_error('pager_options][prev_label', t('Previous pager item label cannot be blank'));
}
if (empty($form_state['values']['pager_options']['next_label'])) {
form_set_error('pager_options][next_label', t('Next pager item label cannot be blank'));
}
}
function query() {
$limit = $this->options['items_per_page'];
$offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
if (!empty($this->options['total_pages'])) {
if ($this->current_page >= $this->options['total_pages']) {
$limit = $this->options['items_per_page'];
$offset = $this->options['total_pages'] * $this->options['items_per_page'];
}
}
$this->view->query
->set_limit($limit);
$this->view->query
->set_offset($offset);
}
function render($input) {
$pager_theme = views_theme_functions('views_simple_pager', $this->view, $this->display);
return theme($pager_theme, array(
'parameters' => $input,
'element' => $this->options['id'],
'options' => $this->options,
));
}
function set_current_page($number = NULL) {
if (isset($number)) {
$this->current_page = $number;
return;
}
global $pager_page_array;
if (empty($pager_page_array)) {
$pager_page_array = array();
}
$page = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
for ($i = 0; $i <= $this->options['id'] || $i < count($pager_page_array); $i++) {
$pager_page_array[$i] = empty($page[$i]) ? 0 : $page[$i];
}
$this->current_page = intval($pager_page_array[$this->options['id']]);
if ($this->current_page < 0) {
$this->current_page = 0;
}
}
function get_pager_total() {
if ($items_per_page = intval($this
->get_items_per_page())) {
return ceil($this->total_items / $items_per_page);
}
else {
return 1;
}
}
function update_page_info() {
if (!empty($this->options['total_pages'])) {
if ($this->options['total_pages'] * $this->options['items_per_page'] < $this->total_items) {
$this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
}
}
$items_per_page = $this
->get_items_per_page();
if (!empty($items_per_page)) {
global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
$pager_limits[$this->options['id']] = $this->options['items_per_page'];
$pager_total_items[$this->options['id']] = $this->total_items;
$pager_total[$this->options['id']] = $this
->get_pager_total();
if ($this->current_page < 0) {
$this->current_page = 0;
}
elseif ($this->current_page >= $pager_total[$this->options['id']]) {
$this->current_page = $pager_total[$this->options['id']] - 1;
}
$pager_page_array[$this->options['id']] = $this->current_page;
}
}
}