You are here

cleanpager.module in Clean Pagination 6

Same filename and directory in other branches
  1. 8 cleanpager.module
  2. 5 cleanpager.module
  3. 7 cleanpager.module

File

cleanpager.module
View source
<?php

define('CLEANPAGER_ADDITIONAL_PATH_VARIABLE', 'page');

/**
 * Implementation of hook_menu()
 */
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;
}

/**
 * Defines admin settings form
 */
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);
}

/**
 * Implementation of hook_perm().
 */
function cleanpager_perm() {
  return array(
    "cleanpager administer",
  );
}

/**
 * Implementation of hook_init().
 */
function cleanpager_init() {

  // The current url looks like /test?page=1 but it should have clean pager
  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');
    }
  }
}

/**
 * Checks if the page should use clean pagination
 */
function cleanpager_check_match() {
  $q = cleanpager_path();
  if (drupal_match_path($q, variable_get('cleanpager_pages', ''))) {
    _cleanpager_rewrite_path();
    return $q;
  }
  return FALSE;
}

/**
 * Remove number at end of path
 */
function cleanpager_path() {
  $q = $_GET['q'];
  $q_array = explode('/', $_GET['q']);
  if (_cleanpager_is_pager_element(end($q_array))) {
    array_pop($q_array);

    // Check for page/page_number
    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;
}

/**
 * Override theme for a pager link
 */
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;
  }

  // Set each pager link title
  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,
  ));
}

/**
 * Implementation of hook_theme_registry_alter().
 */
function cleanpager_theme_registry_alter(&$theme_registry) {

  // Kill the next/previous forum topic navigation links.
  if ($theme_registry['pager_link']['function']['theme_pager_link']) {
    $theme_registry['pager_link']['function'] = 'cleanpager_theme_pager_link';
  }
}

/**
 * Remove number at end of path
 */
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);

      // Check for page/page_number
      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;
}

/**
 * Determine if the given string a 'page' value.
 */
function _cleanpager_is_pager_element($value) {
  if (is_numeric($value)) {
    return TRUE;
  }

  // Handle multiple pagers (i.e. 0,0,1,0);
  $parts = explode(',', $value);
  foreach ($parts as $p) {
    if (!is_numeric($p)) {
      return FALSE;
    }
  }
  return TRUE;
}

Functions

Namesort descending Description
cleanpager_admin_settings Defines admin settings form
cleanpager_check_match Checks if the page should use clean pagination
cleanpager_init Implementation of hook_init().
cleanpager_menu Implementation of hook_menu()
cleanpager_path Remove number at end of path
cleanpager_perm Implementation of hook_perm().
cleanpager_theme_pager_link Override theme for a pager link
cleanpager_theme_registry_alter Implementation of hook_theme_registry_alter().
_cleanpager_is_pager_element Determine if the given string a 'page' value.
_cleanpager_rewrite_path Remove number at end of path

Constants