View source
<?php
define('CLEANPAGER_ADDITIONAL_PATH_VARIABLE', 'page');
function cleanpager_menu() {
$items = array();
$items['admin/settings/cleanpage'] = array(
'title' => 'Clean Pagination Settings',
'description' => 'Global configuration of Clean Pagination functionality.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'cleanpager_admin_settings',
),
'access arguments' => array(
'cleanpager administer',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function cleanpager_admin_settings() {
$form = array();
$form['cleanpager_pages'] = array(
'#title' => t('Pages'),
'#description' => t('Set which pages to apply clean pagination to. Put each page on a new line and use the full page name, without a leading slash. Probably the easiest way to test this out to to create a view with only 1 or 2 nodes per page and add the URL to this list. An example how you would type a page is "admin/content/node".'),
'#type' => 'textarea',
'#rows' => '7',
'#default_value' => variable_get('cleanpager_pages', ''),
);
$form['cleanpager_use_seo_links'] = array(
'#title' => t('Use SEO Links'),
'#description' => t('Using SEO links will add the page URL to the pagination links, and then will remove them via jquery once the page is loaded.'),
'#type' => 'checkbox',
'#default_value' => variable_get('cleanpager_use_seo_links', ''),
);
$form['cleanpager_redirect_301'] = array(
'#title' => t('Use 301 Redirects'),
'#description' => t('If a user lands on a page that has ?page=X in the query string but should have clean pagination, issue a 301 redirect.'),
'#type' => 'checkbox',
'#default_value' => variable_get('cleanpager_redirect_301', FALSE),
);
$form['cleanpager_use_additional_path'] = array(
'#title' => t('Use !page_variable/page_number', array(
'!page_variable' => CLEANPAGER_ADDITIONAL_PATH_VARIABLE,
)),
'#description' => t('Instead of simply adding /page_number at the end of paged urls, use /!page_variable/page_number.', array(
'!page_variable' => CLEANPAGER_ADDITIONAL_PATH_VARIABLE,
)),
'#type' => 'checkbox',
'#default_value' => variable_get('cleanpager_use_additional_path', TRUE),
);
return system_settings_form($form);
}
function cleanpager_perm() {
return array(
"cleanpager administer",
);
}
function cleanpager_init() {
if (variable_get('cleanpager_redirect_301', FALSE) && isset($_GET['page']) && ($path = cleanpager_path()) && drupal_match_path($path, variable_get('cleanpager_pages', ''))) {
if (variable_get('cleanpager_use_additional_path', TRUE)) {
$path .= '/' . CLEANPAGER_ADDITIONAL_PATH_VARIABLE;
}
$path .= '/' . $_GET['page'];
drupal_goto($path, NULL, NULL, 301);
}
elseif (cleanpager_check_match()) {
if (variable_get('cleanpager_use_seo_links', '') == 1) {
drupal_add_js(drupal_get_path('module', 'cleanpager') . '/cleanpager.js');
}
}
}
function cleanpager_check_match() {
$q = cleanpager_path();
if (drupal_match_path($q, variable_get('cleanpager_pages', ''))) {
_cleanpager_rewrite_path();
return $q;
}
return FALSE;
}
function cleanpager_path() {
$q = $_GET['q'];
$q_array = explode('/', $_GET['q']);
if (_cleanpager_is_pager_element(end($q_array))) {
array_pop($q_array);
if (variable_get('cleanpager_use_additional_path', TRUE)) {
if (end($q_array) == CLEANPAGER_ADDITIONAL_PATH_VARIABLE) {
array_pop($q_array);
$q = implode('/', $q_array);
}
}
else {
$q = implode('/', $q_array);
}
}
return $q;
}
function cleanpager_theme_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
$page = isset($_GET['page']) ? $_GET['page'] : '';
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
$parameters['page'] = $new_page;
}
$cleanpage = cleanpager_check_match();
if ($cleanpage) {
unset($parameters['page']);
}
$query = array();
if (count($parameters)) {
$query[] = drupal_query_string_encode($parameters, array());
}
$querystring = pager_get_querystring();
if ($querystring != '') {
$query[] = $querystring;
}
if (!isset($attributes['title'])) {
static $titles = NULL;
if (!isset($titles)) {
$titles = array(
t('« first') => t('Go to first page'),
t('‹ previous') => t('Go to previous page'),
t('next ›') => t('Go to next page'),
t('last »') => t('Go to last page'),
);
}
if (isset($titles[$text])) {
$attributes['title'] = $titles[$text];
}
else {
if (is_numeric($text)) {
$attributes['title'] = t('Go to page @number', array(
'@number' => $text,
));
}
}
}
$q = $_GET['q'];
if ($cleanpage) {
$q = cleanpager_path();
if (variable_get('cleanpager_use_additional_path', TRUE)) {
$cleanpage .= '/' . CLEANPAGER_ADDITIONAL_PATH_VARIABLE;
}
}
if (isset($new_page) && $cleanpage && $new_page > 0) {
if (variable_get('cleanpager_use_seo_links', '') == 1) {
$text = 'Visit ' . $q . ' Page ' . $text;
}
return l($text, $cleanpage . '/' . $new_page, array(
'attributes' => $attributes,
'query' => count($query) ? implode('&', $query) : NULL,
));
}
return l($text, $q, array(
'attributes' => $attributes,
'query' => count($query) ? implode('&', $query) : NULL,
));
}
function cleanpager_theme_registry_alter(&$theme_registry) {
if ($theme_registry['pager_link']['function']['theme_pager_link']) {
$theme_registry['pager_link']['function'] = 'cleanpager_theme_pager_link';
}
}
function _cleanpager_rewrite_path(&$pager = FALSE, &$page = NULL) {
static $q, $pr, $p;
if (!isset($q)) {
$q = $_GET['q'];
$p = 0;
$pr = FALSE;
$q_array = explode('/', $_GET['q']);
if (_cleanpager_is_pager_element(end($q_array))) {
$p = array_pop($q_array);
if (variable_get('cleanpager_use_additional_path', TRUE)) {
if (end($q_array) == CLEANPAGER_ADDITIONAL_PATH_VARIABLE) {
array_pop($q_array);
$pr = TRUE;
}
}
else {
$pr = TRUE;
}
if ($pr) {
$q = implode('/', $q_array);
$_REQUEST['q'] = $_GET['q'] = $q;
$_REQUEST['page'] = $_GET['page'] = $p;
}
}
}
$page = $p;
$pager = $pr;
return $q;
}
function _cleanpager_is_pager_element($value) {
if (is_numeric($value)) {
return TRUE;
}
$parts = explode(',', $value);
foreach ($parts as $p) {
if (!is_numeric($p)) {
return FALSE;
}
}
return TRUE;
}