View source
<?php
class Pagination {
protected static $instance = NULL;
protected $page = array();
protected $headers = array();
protected $style = NULL;
protected $value = NULL;
public $re_custom = '/(<p>\\s*)?\\[\\s*(pagebreak\\s*\\]|(header\\s*=\\s*([^\\]]*)\\]))(\\s*<\\/p>)?/mi';
public $re_tag = '/(<p>\\s*)?<h3>\\s*(.*?)\\s*<\\/h3>(\\s*<\\/p>)?/mi';
public static function instance() {
if (!self::$instance) {
self::$instance = new Pagination();
}
return self::$instance;
}
public function paginate($text, $cutoff) {
$textsize = strlen($text);
do {
$section = $this
->parseText($text, $cutoff);
$this->page[] = $section;
$textsize = strlen($text);
} while ($textsize > 0);
}
public function getStyle($type) {
$this->style = $this
->getSettings('style', $type);
return $this->style;
}
public function getValue($type) {
$this->value = $this
->getSettings('value', $type);
return $this->value;
}
public function getHeaders($nid = 0) {
if (!$nid) {
return array();
}
static $query;
if (!$query && $this->value > PAGINATION_AUTO) {
$result = db_select('node_pagination', 'n')
->fields('n', array(
'headers',
))
->condition('nid', $nid, '=')
->execute()
->fetchObject();
$headers = isset($result->headers) ? unserialize($result->headers) : array();
foreach ($headers as $key => $header) {
$this->headers[$key + 1] = $header;
}
}
return $this->headers;
}
public function getPageCount() {
return count($this->page);
}
public function getPage($page) {
$section = isset($this->page[$page]) ? $this->page[$page] : '';
return $section;
}
public function getPageVar() {
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$page = $page > 0 ? --$page : $page;
return $page;
}
public function getPager() {
global $pager_page_array, $pager_total;
$pager_page_array = explode(',', (int) $this
->getPageVar());
$pager_total[0] = $this
->getPageCount();
$pager_page_array[0] = max(0, min((int) $pager_page_array[0], (int) $pager_total[0]));
$pager = theme('pager');
$regex = '#(\\?|\\&)page=(.+?)(\\"|&|/)#se';
$pager = preg_replace($regex, "'\$1page='.(\$2 + 1).''.stripslashes('\$3').''", $pager);
return $pager;
}
public function getToc($nid, $ignore_title = FALSE) {
if (!$nid) {
return;
}
drupal_add_css(drupal_get_path('module', 'pagination') . '/css/pagination.css');
$toc = array();
$headers = $this
->getHeaders($nid);
$headers[0] = menu_get_active_title();
foreach ($headers as $key => $header) {
$page = $key + 1;
$header = str_replace(array(
'<br />',
'<br>',
), '', $header);
$options = array(
'attributes' => array(
'title' => t('Go to page !page', array(
'!page' => $page,
)),
),
'query' => array(
'page' => $page,
),
);
$toc[] = array(
'data' => $this
->getPageVar() == $key ? check_plain($header) : l($header, $_GET['q'], $options),
'class' => array(
$this
->getPageVar() == $key ? 'pagination-toc-item current' : 'pagination-toc-item',
),
);
}
$items = theme('item_list', array(
'items' => $toc,
'type' => variable_get('pagination_list_type', 'ul'),
'attributes' => array(
'class' => array(
'pagination-toc-list',
),
),
));
$items = preg_replace('#&(\\S{2,7}[^;];)#e', "'&\$1'", $items);
$toc = theme('pagination_toc', array(
'toc' => $ignore_title ? NULL : t('Table of Contents:'),
'title' => drupal_get_title(),
'pages' => $items,
));
return $toc;
}
protected function getSettings($setting, $type) {
static $pagination = array();
if (!isset($pagination[$type])) {
$result = db_select('pagination', 'p')
->fields('p')
->condition('type', $type, '=')
->execute()
->fetchObject();
$pagination[$type] = array(
'style' => isset($result->style) ? $result->style : 0,
'value' => isset($result->paginate) ? $result->paginate : 0,
);
}
$result = isset($pagination[$type][$setting]) ? $pagination[$type][$setting] : 0;
return $result;
}
protected function setHeader($header) {
static $first;
if (!$first and $this->value < PAGINATION_AUTO) {
$first = TRUE;
$this->headers[] = t('Page 1');
}
$this->headers[] = $header;
}
protected function parseText(&$text, $cutoff) {
$page_count = empty($this->headers) ? count($this->headers) + 1 : count($this->headers);
$section = '';
switch ($cutoff) {
case 1:
preg_match($this->re_custom, $text, $matches);
if (isset($matches[0])) {
$header = isset($matches[4]) && $matches[4] ? $matches[4] : t('Page !num', array(
'!num' => $page_count + 1,
));
$section = $this
->parseSection($text, $matches, 1, 5);
$this
->setHeader($header);
}
else {
$section = $text;
$text = '';
}
break;
case 2:
$tag = 'h' . variable_get('pagination_header', 3);
if ('h3' !== $tag) {
$this->re_tag = str_replace('h3', $tag, $this->re_tag);
}
preg_match($this->re_tag, $text, $matches);
if (isset($matches[0])) {
$header = isset($matches[2]) ? $matches[2] : t('Page !num', array(
'!num' => $page_count + 1,
));
$section = $this
->parseSection($text, $matches, 1, 3);
$this
->setHeader($header);
}
else {
$section = $text;
$text = '';
}
break;
default:
$header = t('Page !num', array(
'!num' => $page_count + 1,
));
$section = $this
->parseChunk($text, $cutoff * 6);
$text = substr($text, strlen($section));
$this
->setHeader($header);
break;
}
return $section;
}
protected function parseSection(&$text, $matches, $start, $end) {
$open = isset($matches[$start]) && !isset($matches[$end]) ? $matches[$start] : '';
$close = isset($matches[$end]) && $matches[$end] && isset($matches[$start]) && !$matches[$start] ? $matches[$end] : '';
$split = strpos($text, $matches[0]);
$section = substr($text, 0, $split) . $close;
$text = $open . substr($text, $split + strlen($matches[0]));
return $section;
}
protected function parseChunk($text, $size) {
return text_summary($text, NULL, $size);
}
}