Source for file PageLister_HtmlHandler.class.php

Documentation is available at PageLister_HtmlHandler.class.php

  1. <?php
  2. /**
  3. * Project: PageLister
  4. * File: PageLister_HtmlHandler.class.php
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @link http://dev.dasprids.de/
  21. * @copyright 2006 DASPRiD's
  22. * @author Ben 'DASPRiD' Scholzen <mail@dasprids.de>
  23. * @package PageLister
  24. * @version 0.1.4
  25. */
  26.  
  27. /**
  28. * HTML handler for output
  29. *
  30. * @package PageLister
  31. */
  32. class PageLister_HtmlHandler {
  33. /**
  34. * Objects to replace.
  35. *
  36. * @var array
  37. */
  38. private $objects = array(
  39. 'first' => '<a href="%URL%">&laquo;</a>',
  40. 'prev' => '<a href="%URL%">&lt;</a>',
  41. 'next' => '<a href="%URL%">&gt;</a>',
  42. 'last' => '<a href="%URL%">&raquo;</a>',
  43. 'placeholder_before' => '... ',
  44. 'spacer' => ' ',
  45. 'passive_page' => '<a href="%URL%">%PAGE%</a>',
  46. 'current_page' => '<b>%PAGE%</b>',
  47. );
  48.  
  49. /**
  50. * Format of the HTML-output.
  51. *
  52. * @var string
  53. */
  54. private $format = '%FIRST% %PREV% %PAGES% %NEXT% %LAST%';
  55.  
  56. /**
  57. * How many pages should be shown beside the current one.
  58. *
  59. * @var int
  60. */
  61. private $pagesBeside = 3;
  62.  
  63.  
  64. /**
  65. * Sets the format of the HTML-output.
  66. *
  67. * You may want to modifie the HTML-output, so have have to change the format.
  68. * You can use any HTML-code, and use the following placeholders in it:
  69. *
  70. * %FIRST% - Jump to the first page
  71. * %PREV% - Jump to the previews page
  72. * %NEXT% - Jump to the next page
  73. * %LAST$ - Jump to the last page
  74. * %PAGES% - The pages list
  75. *
  76. * @param string $format
  77. * @return boolean
  78. */
  79. public function SetFormat ($format) {
  80. $this->format = $format;
  81.  
  82. return true;
  83. }
  84.  
  85. /**
  86. * Sets the value of an object.
  87. *
  88. * Here you may change the single objects. The objects `first`, `prev`,
  89. * `next` and `last` can have the placeholder %URL% in it. This is the
  90. * HTML-escaped url for links. The object `placeholder` holds the placeholder
  91. * before and after the pages list and the `spacer` holds the space between
  92. * the pages. The object `passive_page` contains the style of a single
  93. * passive page with the placeholders %URL% and %PAGE%. The `current_page`
  94. * object contains the current page with the placeholders %URL% and %PAGE%.
  95. *
  96. * @param string $object
  97. * @param string $value
  98. * @return boolean
  99. */
  100. public function SetObject ($object, $value) {
  101. if (!array_key_exists($object, $this->objects)) {
  102. trigger_error('PageLister_HtmlHandler: Invalid object.', E_USER_ERROR);
  103. return false;
  104. }
  105.  
  106. $this->objects[$object] = (string)$value;
  107.  
  108. return true;
  109. }
  110.  
  111. /**
  112. * Sets how many pages should be sown beside the current one.
  113. *
  114. * If you set a value of 4 for example, there will be shown 4 pages
  115. * before and 4 pages after the current one. If we are at the last page
  116. * for example, 8 pages will be shown before the current one.
  117. *
  118. * @param int $int
  119. */
  120. public function SetPagesBeside ($int) {
  121. $this->pagesBeside = max(1, (int)$int);
  122. }
  123.  
  124. /**
  125. * Generates the HTML-code.
  126. *
  127. * @param int $pages
  128. * @param int $currentPage
  129. * @param string $url
  130. * @return string
  131. */
  132. public function GenerateHTML ($pages, $currentPage, $url) {
  133. // Escape the given URL
  134. $url = htmlspecialchars($url);
  135.  
  136. // Init the html-string
  137. $html = $this->format;
  138.  
  139. // Replace the 4 static placeholders
  140. $replaces = array();
  141. if ($currentPage > 1) {
  142. $replaces['first'] = $this->ReplacePlaceholder($this->objects['first'], array('url' => $url, 'pn' => 1));
  143. $replaces['prev'] = $this->ReplacePlaceholder($this->objects['prev'], array('url' => $url, 'pn' => max(1, $currentPage - 1)));
  144. } else {
  145. $replaces['first'] = '';
  146. $replaces['prev'] = '';
  147. }
  148. if ($pages > $currentPage) {
  149. $replaces['next'] = $this->ReplacePlaceholder($this->objects['next'], array('url' => $url, 'pn' => min($pages, $currentPage + 1)));
  150. $replaces['last'] = $this->ReplacePlaceholder($this->objects['last'], array('url' => $url, 'pn' => $pages));
  151. } else {
  152. $replaces['next'] = '';
  153. $replaces['last'] = '';
  154. }
  155. $html = $this->ReplacePlaceholder($html, $replaces);
  156.  
  157. // Check how many pages before and after you have to print
  158. if ($currentPage > $this->pagesBeside && $currentPage + $this->pagesBeside <= $pages) {
  159. // (...) 2 3 4 [5] 6 7 8 (...)
  160. $pagesBefore = $pagesAfter = $this->pagesBeside;
  161. } else if ($currentPage <= $this->pagesBeside) {
  162. // 1 2 [3] 4 5 6 7 (...)
  163. // 1 2 [3] 4 5
  164. $pagesBefore = $currentPage - 1;
  165. $pagesAfter = min($this->pagesBeside * 2 - $pagesBefore, $pages - $currentPage);
  166. } else if ($currentPage > $this->pagesBeside) {
  167. // (...) 2 3 4 [5] 6 7 8
  168. $pagesAfter = $pages - $currentPage;
  169. $pagesBefore = min($this->pagesBeside * 2 - $pagesAfter, $currentPage - 1);
  170. }
  171.  
  172. // Init the pages string
  173. $pagesString = '';
  174.  
  175. // If the list does not start with the first page, add a placeholder
  176. if ($currentPage - $pagesBefore > 1) {
  177. $pagesString .= $this->objects['placeholder'].$this->objects['spacer'];
  178. }
  179.  
  180. // Add all pages to the list
  181. for ($i = $currentPage - $pagesBefore; $i <= $currentPage + $pagesAfter; $i++) {
  182. $pagesString .= $this->ReplacePlaceholder($this->objects[($i == $currentPage ? 'current_page': 'passive_page')], array('page' => $i, 'url' => $url, 'pn' => $i));
  183.  
  184. // If this is not the last page, add a spacer after
  185. if ($i < $currentPage + $pagesAfter) {
  186. $pagesString .= $this->objects['spacer'];
  187. }
  188. }
  189.  
  190. // If the list does not end with the last page, add a placeholder
  191. if ($currentPage + $pagesAfter < $pages) {
  192. $pagesString .= $this->objects['spacer'].$this->objects['placeholder'];
  193. }
  194.  
  195. $html = $this->ReplacePlaceholder($html, array('pages' => $pagesString));
  196.  
  197. return $html;
  198. }
  199.  
  200. /**
  201. * Replaces placeholders in a string.
  202. *
  203. * @param string $string
  204. * @param array $placeholders
  205. * @return string
  206. */
  207. private function ReplacePlaceholder ($string, $placeholders) {
  208. foreach ($placeholders AS $search => $replace) {
  209. $string = str_replace('%'.strtoupper($search).'%', $replace, $string);
  210. }
  211.  
  212. return $string;
  213. }
  214. }
  215. ?>

Documentation generated on Thu, 12 Oct 2006 12:20:38 +0200 by phpDocumentor 1.3.0RC3