[drupal-devel] paging module

Marco Scutari marco.scutari at tiscali.it
Fri Sep 23 09:40:41 UTC 2005


Even if it's not quite complete, I'm sending a beta version of the 
paging module I agreed to code in the following feature request:

http://drupal.org/node/30089

{Comments,Opinion,Remarks} are welcome.

Regards
    Marco

<?

/*

  Copyright 2005 Marco Scutari

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


function paging_help($section) {

  switch ($section) {

    case 'admin/help#paging':
      return t("<p>Break long pages into smaller ones by means of ".
               "a \"page\" tag:</p>\n".
               "<pre>\n".
               "first page here.\n".
               "&lt;!-- page --&gt;\n".
               "second page here.\n".
               "</pre>\n".
               "<p>Splitted pages have path:</p>\n".
               "<pre>\n".
               "http://www.example.org/longpage.html?chunk=2\n".
               "</pre>\n".
               "<p>or (if url aliases are not used/available):</p>\n".
               "<pre>\n".
               "http://www.example.org/node/3/4\n".
               "</pre>\n".
               "<p>Navigation links are automagically generated by ".
               "this module.</p>\n".
               "<p>\"Clean URLs\" are required.</p>\n");
    case 'admin/modules#description':
      return t("Break long pages into smaller ones.");

  }//SWITCH

}//PAGING_HELP

/*
 *  Implementation of hook_view().
 */

function paging_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {

  switch ($op) {

    case 'form pre':
    case 'fields':

      /*
       *  Replace the "pg" tag with the original "page" when the page
       *  is about to be saved or the edit form is displayed.
       */

      $node-> body = str_replace("<!-- pg -->", "<!-- page -->", $node->body);
      break;

    case 'validate':

      /*
       *  Replace the "page" tag not to break page preview.
       */

      $node-> body = str_replace("<!-- page -->", "<!-- pg -->", $node->body);
      break;

    case 'view':

      /* get the url for the links. */
      $url = substr($_SERVER["REQUEST_URI"], 0,
                         strpos($_SERVER["REQUEST_URI"]."?", "?"));

      /* check paging is really needed and avoid paging 
         printer friendly pages as hell. */
      if (ereg("<!-- page -->", $node->body) && 
          !ereg("node/".$node->nid."/print", $url)) {

        /* explode the node body on the "paging" tag. */
        $temp_array = explode("<!-- page -->", $node->body);

        /* sanity check for the "chunk" parameter. */
        if (array_key_exists("chunk", $_GET) && 
          is_numeric($_GET["chunk"]) && ($_GET["chunk"] > 1)) {

          $chunk = $_GET["chunk"] - 1;

        }//THEN
        else if ($_GET["chunk"] == "all") {

          return TRUE;

        }//THEN
        else if (count($temp_array) > 1) {

          $chunk = 0;

        }//THEN

        /* paging: keep only the right part of the node body. */
        $node->body = $temp_array[$chunk];

        /* print navigation links. */
        $node->body .= "<div class=\"chunk\">\n ";

        if ($chunk > 0) {

          /* print the "previous" link. */
          $node->body .= l(t("previous"),
$url."?chunk=".$chunk)."&nbsp;|&nbsp;\n";

        }//THEN

        for ($i = 1; $i <= count($temp_array); $i++) {

          if ($chunk + 1 == $i) {

            /* print the current page number. */
            $node->body .= $i."&nbsp;|&nbsp;";

          }//THEN
          else {

          /* print links to other pages. */
          $node->body .= l($i, $url."?chunk=".$i)."&nbsp;|&nbsp;\n";

          }//ELSE

        }//FOR

        if ($chunk < count($temp_array) - 1) {

          /* print the "next" link. */
          $node->body .= l(t("next"), $url."?chunk=".($chunk+2)).
                         "&nbsp;|&nbsp;\n";

        }//THEN

        $node->body .= l(t("whole page"), $url."?chunk=all")."\n";

      $node->body .= "</div>\n ";

    }//THEN

  }//SWITCH

}//PAGING_NODEAPI

/*
 *  Implementation of hook_menu().
 */

function paging_menu($may_cache) {
  $items = array();

  /* check the url is like "node/%d/%d". */
  if (arg(0) == 'node' && is_numeric(arg(1)) && is_numeric(arg(2))) {

    $node = node_load(array('nid' => arg(1)));

    if ($node->nid) {

      /* register the function. */
      $items[] = array('path' => 'node/'.arg(1).'/'.arg(2), 
        'title' => "paging",
        'callback' => '_paging_redirect',
        'access' => node_access('view', $node),
        'type' => MENU_CALLBACK);

    }//THEN

  }//THEN

  return $items;

}//PAGING_MENU

/* 
 *  Paging redirect from "node/%d/%d" to "%s?chunk=%d" or
 *  "node/%d?chunk=2" (backup).
 */

function _paging_redirect($nid = 0, $pid = 0) {

  drupal_goto(drupal_get_path_alias("node/".arg(1))."?chunk=".arg(2));

}//_PAGING_REDIRECT

?>


----
Marco Scutari                               marco.scutari at tiscali.it
Linux Registered User #341807                  http://counter.li.org
powered by : 
Debian Sid GNU/Linux (SGI-XFS)                       Kernel 2.6.11.7



More information about the drupal-devel mailing list