You are here

cleanpager.module in Clean Pagination 5

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

File

cleanpager.module
View source
<?php

/**
 * Implementation of hook_menu()
 */
function cleanpager_menu($may_cache) {
  $items = array();
  if (cleanpager_check_match()) {
    if (variable_get('cleanpager_use_seo_links', '') == 1) {
      drupal_add_js(drupal_get_path('module', 'cleanpager') . '/cleanpager.js');
    }
  }
  if ($may_cache == FALSE) {
    return false;
  }
  $items[] = array(
    'path' => 'admin/settings/cleanpage',
    'title' => t('Clean Pagination Settings'),
    'callback' => 'drupal_get_form',
    'callback arguments' => 'cleanpager_admin_settings',
    'access' => user_access('cleanpager administer'),
    'description' => t('Global configuration of Clean Pagination functionality.'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Defines admin settings form
 */
function cleanpager_admin_settings() {
  $form['cleanpager_pages'] = array(
    '#title' => 'Pages',
    '#description' => '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' => 'Use SEO Links',
    '#description' => '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', ''),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_perm()
 */
function cleanpager_perm() {
  return array(
    "cleanpage administer",
  );
}
function cleanpager_init() {
  if (cleanpager_check_match()) {
    $url_array = explode('/', $_GET['q']);
    $page = end($url_array);
    array_pop($url_array);

    //die($page . 't');
    if (is_numeric($page)) {

      //die($page);
      $_GET['page'] = $page;
    }
  }
}

/**
 * Checks if the page should use clean pagination
 */
function cleanpager_check_match() {
  $matches = variable_get('cleanpager_pages', '');
  if (trim($matches) != '') {
    $matches = explode("\n", $matches);
    foreach ($matches as $match) {
      $match = trim($match);
      $match = str_replace('/', '\\/', $match);
      if (preg_match('/' . $match . '([0-9]*)/', $_GET['q'])) {
        return true;
        break;
      }
    }
  }
  return false;
}

/**
 * Override theme for a pager link
 */
function phptemplate_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
  if (!cleanpager_check_match()) {
    $q = $_GET['q'];
    $page = isset($_GET['page']) ? $_GET['page'] : '';
    if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
      $parameters['page'] = $new_page;
    }
  }
  else {
    $new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)));
    $arguments = explode('/', $_GET['q']);
    if ($_GET['page'] > 0) {
      array_pop($arguments);
    }
    $pre_page_q = implode('/', $arguments);

    //echo $pre_page_q;
    $q .= $pre_page_q;
    if ($new_page) {
      $q .= '/' . $new_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,
        ));
      }
    }
    if (is_numeric($text) && $q && variable_get('cleanpager_use_seo_links', '') == 1) {
      $text = 'Visit ' . $pre_page_q . ' Page ' . ($new_page + 1);
    }
  }
  return l($text, $q, $attributes, count($query) ? implode('&', $query) : NULL);
}

/**
 * Implementation of hook_footer
 * 
 * Adds the URL to pagination links
 */
function cleanpager_footer() {
}

Functions

Namesort descending Description
cleanpager_admin_settings Defines admin settings form
cleanpager_check_match Checks if the page should use clean pagination
cleanpager_footer Implementation of hook_footer
cleanpager_init
cleanpager_menu Implementation of hook_menu()
cleanpager_perm Implementation of hook_perm()
phptemplate_pager_link Override theme for a pager link