Source for file paging-defs.php

Documentation is available at paging-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: paging-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for paging content. Handles the */
  24. /* splitting of a text block into pages of a given */
  25. /* size, and provides the Next/Previous urls. */
  26. /* */
  27. /* ******************************************************************** */
  28. /** @package cm */
  29. include_once("button-defs.php");
  30.  
  31. // ----------------------------------------------------------------------
  32. /**
  33. * Pages class
  34. * A class for managing pages of text. Initially written to provide
  35. * support for sending text to WAP phones, where the content had to
  36. * be paged into small chunks due to the limited capacity of the
  37. * phones.
  38. * @package cm
  39. */
  40. class pages extends RenderableObject {
  41. /** Pages content */
  42.  
  43. var $content = "";
  44. /** Size of each page in characters */
  45.  
  46. var $pagesize = 800;
  47. /** Number of chars to look ahead */
  48.  
  49. var $lookahead = 250;
  50. /** Number of chars to look back */
  51.  
  52. var $lookback = 120;
  53. /** Current page number */
  54.  
  55. var $pageno = 0;
  56. /** Text for current page */
  57.  
  58. var $pagetext = "";
  59. /** URL for getting next page */
  60.  
  61. var $paginghref = "";
  62. //.....................................................................
  63. /**
  64. * Constructor
  65. * Creates the basic text paging object.
  66. * @param string $content Text content for pages
  67. * @param string $paginghref URL for getting next page (back or forward)
  68. * @param integer $pagesize Max. size of each page in characters
  69. * @param integer $lookahead No. characters to look ahead
  70. * @param integer $pagesize No. characters to look back
  71. */
  72. function pages($content="", $paginghref="", $pagesize=800, $lookahead=250, $lookback=120) {
  73. $this->content = $content;
  74. $this->paginghref = $paginghref;
  75. $this->pagesize = $pagesize;
  76. $this->lookahead = $lookahead;
  77. $this->lookback = $lookback;
  78. } // pages
  79. //.....................................................................
  80. /**
  81. * Deliver page N
  82. * Delivers the text content for the specified page.
  83. * @param integer $pgno Number of the page to deliver
  84. * @param string $brtag Line-break tag to us (defaults to WML)
  85. * @return string Text content for specified page
  86. */
  87. function page($pgno, $brtag="<br/>") {
  88. if ( strlen($this->content) < ($this->pagesize + $this->lookahead) ) {
  89. $this->pageno = 1;
  90. $this->pagetext = $this->content;
  91. }
  92. else {
  93. $epos = 0;
  94. $fpos = strlen($this->content);
  95. for ( $spos=0, $tot_pages=1; $epos < $fpos; $tot_pages++ ) {
  96. $epos = $spos + $this->pagesize;
  97. if ( $epos > ($fpos - ($this->lookahead + $this->lookback)) ) $epos = $fpos;
  98. else {
  99. // Look forward for start of new paragraph...
  100. $tpos = strpos( substr($this->content,$epos), $brtag );
  101. if ( $tpos && $tpos > 0 && $tpos < $this->lookahead ) $epos += $tpos;
  102. else {
  103. // Look backward for start of paragraph...
  104. $tpos = strpos( strrev(substr($this->content, $epos - $this->lookback, $this->lookback)), strrev($brtag) );
  105. if ( $tpos && $tpos > 0 && $tpos < $this->lookback ) $epos -= $tpos;
  106. else {
  107. // Desperate: look forward for first space...
  108. $tpos = strpos( substr($this->content,$epos), " " );
  109. if ( $tpos && $tpos > 0 && $tpos < $this->lookahead ) $epos += $tpos;
  110. }
  111. }
  112. }
  113. // If we're at the required page, assign and stop..
  114. if ( $tot_pages == $pgno ) {
  115. $this->pageno = $pgno;
  116. $this->pagetext = substr($this->content, $spos, $epos - $spos);
  117. if ( $epos < $fpos ) {
  118. if ($this->paginghref != "") {
  119. $npg = $this->pageno + 1;
  120. if (strstr($this->paginghref, "?")) $href = $this->paginghref . "&amp;pg=$npg";
  121. else $href = $this->paginghref . "?pg=$npg";
  122. $pglink = new Link($href, " More..");
  123. }
  124. }
  125. }
  126. $spos = $epos;
  127. }
  128. // Add the page notification and link..
  129. $this->pagetext .= " [$pgno/" . ($tot_pages - 1) . "]";
  130. if (isset($pglink)) {
  131. $this->pagetext .= " " . $pglink->render();
  132. }
  133. }
  134. return $this->pagetext;
  135. } // page
  136. //.....................................................................
  137. /** Deliver WML format page */
  138.  
  139. function wml($pgno=1) {
  140. return $this->page($pgno, "<br/>");
  141. } // wml
  142. //.....................................................................
  143. /** Deliver HTML format page */
  144.  
  145. function html($pgno=1) {
  146. return $this->page($pgno, "<br>");
  147. } // html
  148.  
  149. } // pages class
  150. // ----------------------------------------------------------------------
  151.  
  152. ?>

Documentation generated by phpDocumentor 1.3.0RC3