Source for file PageLister.class.php

Documentation is available at PageLister.class.php

  1. <?php
  2. /**
  3. * Project: PageLister
  4. * File: PageLister.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. * Include the handlers
  29. */
  30. require_once 'PageLister_CountHandler.class.php';
  31. require_once 'PageLister_HtmlHandler.class.php';
  32.  
  33.  
  34. /**
  35. * @package PageLister
  36. */
  37. class PageLister {
  38. /**
  39. * Name of the pagenumbers in $_GET.
  40. *
  41. * @var string
  42. */
  43. private $getName = 'pagenumber';
  44.  
  45. /**
  46. * The URL to link to.
  47. *
  48. * @var string
  49. */
  50. private $url = null;
  51.  
  52. /**
  53. * Total entries per page.
  54. *
  55. * @var int
  56. */
  57. private $entriesPerPage = 30;
  58.  
  59. /**
  60. * The query to execute.
  61. *
  62. * @var string
  63. */
  64. private $query = null;
  65.  
  66. /**
  67. * The count handler for the entries.
  68. *
  69. * @var resource
  70. */
  71. private $countHandler = null;
  72.  
  73. /**
  74. * The html handler for the output.
  75. *
  76. * @var resource
  77. */
  78. private $htmlHandler = null;
  79.  
  80. /**
  81. * Wether calculation is done or not.
  82. *
  83. * @var boolean
  84. */
  85. private $calculationDone = false;
  86.  
  87. /**
  88. * Total entries.
  89. *
  90. * @var int
  91. */
  92. private $entriesTotal;
  93.  
  94. /**
  95. * Total pages.
  96. *
  97. * @var int
  98. */
  99. private $pages;
  100.  
  101. /**
  102. * The current page.
  103. *
  104. * @var int
  105. */
  106. private $currentPage;
  107.  
  108. /**
  109. * The limit-offset for the query.
  110. *
  111. * @var int
  112. */
  113. private $limitOffset;
  114.  
  115.  
  116. /**
  117. * Sets the handler to count all entries.
  118. *
  119. * The PageLister uses an external handler for counting all entries. This way
  120. * don't have to define everytime a new handler, when you want to use more than
  121. * one PageLister.
  122. *
  123. * @param resource $resource
  124. * @return boolean
  125. */
  126. public function SetCountHandler ($object) {
  127. if (!is_object($object)) {
  128. trigger_error('PageLister: This is no object.', E_USER_ERROR);
  129. return false;
  130. }
  131.  
  132. if (!$object instanceof PageLister_CountHandler) {
  133. trigger_error('PageLister: This is no instance of PageLister_CountHandler.', E_USER_ERROR);
  134. return false;
  135. }
  136.  
  137. $this->countHandler = clone $object;
  138.  
  139. return true;
  140. }
  141.  
  142. /**
  143. * Sets the handler to output the html.
  144. *
  145. * The PageLister uses an external handler for outputting the HTML. This way
  146. * don't have to define everytime a new handler, when you want to use more than
  147. * one PageLister.
  148. *
  149. * @param resource $resource
  150. * @return boolean
  151. */
  152. public function SetHtmlHandler ($object) {
  153. if (!is_object($object)) {
  154. trigger_error('PageLister: This is no resource.', E_USER_ERROR);
  155. return false;
  156. }
  157.  
  158. if (!$object instanceof PageLister_HtmlHandler) {
  159. trigger_error('PageLister: This is no instance of PageLister_HtmlHandler.', E_USER_ERROR);
  160. return false;
  161. }
  162.  
  163. $this->htmlHandler = clone $object;
  164.  
  165. return true;
  166. }
  167.  
  168. /**
  169. * Sets the URL, where the HTML-Code should link to.
  170. *
  171. * If you want to link to another URL than the current one, you can specify
  172. * an URL with this function and change the name of the pagenumber in <var>$_GET</var>.
  173. * If you only want to change the name of the pagenumber, you may pass `null`
  174. * as URL. If you use searchengine-friendly URLS, you can pass an URL like:
  175. *
  176. * http://www.something.com/articles/page_%PN%/
  177. *
  178. * The %PN% will be replaced by the pagenumber in the HTML-Code. If there is
  179. * no placeholder for the pagenumber in the URL, PageLister will automaticaly
  180. * add the pagenumber to the end of the GET-variables.
  181. *
  182. * @param string $url
  183. * @param string $getName
  184. * @return boolean
  185. */
  186. public function SetURL ($url, $getName = 'pagenumber') {
  187. $this->url = $url;
  188. $this->getName = $getName;
  189.  
  190. return true;
  191. }
  192.  
  193. /**
  194. * Sets entries per page.
  195. *
  196. * If you want to display more or less entries than defaultvalue, you
  197. * can change the amount with this method.
  198. *
  199. * @param int $entries
  200. * @return boolean
  201. */
  202. public function SetEntriesPerPage ($entries) {
  203. if ($this->calculationDone) {
  204. trigger_error('PageLister: Calculation done, you may not modifie this settings yet.', E_USER_ERROR);
  205. return false;
  206. }
  207.  
  208. $this->entriesPerPage = max(1, (int)$entries);
  209.  
  210. return true;
  211. }
  212.  
  213. /**
  214. * Sets the Query to count.
  215. *
  216. * This method must be called before you call any of the Get-methods. Here you
  217. * can set the query to count. Remember, to not add an <i>LIMIT x,x</i> to the query,
  218. * this will be automaticaly done by the PageLister. The query will later be
  219. * handled by PageLister_CountHandler.
  220. *
  221. * @param string $query
  222. * @return boolean
  223. */
  224. public function SetQuery ($query) {
  225. if ($this->calculationDone) {
  226. trigger_error('PageLister: Calculation done, you may not modifie this settings yet.', E_USER_ERROR);
  227. return false;
  228. }
  229.  
  230. $this->query = $query;
  231.  
  232. return true;
  233. }
  234.  
  235. /**
  236. * Returns the calculated HTML-Code.
  237. *
  238. * After setting the query, you may want to get the HTML-Code. To do this,
  239. * you have to define a HTML-handler with the method SetHtmlHandler(). You
  240. * may change the HTML-handler after calling this and get different outputs
  241. * for the pagelist.
  242. *
  243. * @return string
  244. */
  245. public function GetHTML () {
  246. // Do calculation, of not done yet
  247. if (!$this->calculationDone) {
  248. $this->DoCalculation();
  249. }
  250.  
  251. // If the user has not give us an URL, take the REQUEST_URI
  252. if (is_null($this->url)) {
  253. $preparedURL = $this->PrepareURL($_SERVER['REQUEST_URI']);
  254. } else {
  255. $preparedURL = $this->PrepareURL($this->url);
  256. }
  257.  
  258.  
  259. $html = $this->htmlHandler->GenerateHTML($this->pages, $this->currentPage, $preparedURL);
  260.  
  261. return $html;
  262. }
  263.  
  264. /**
  265. * Returns the modified query.
  266. *
  267. * This method returns the modified query you may use to get the entries of
  268. * the current page. If you selected MySQLi as handler, you may use this as
  269. * following:
  270. *
  271. * <code>
  272. * $query = $PageLister->GetQuery();
  273. * $result = $mysqli->query($query);
  274. * </code>
  275. *
  276. * @return string
  277. */
  278. public function GetQuery () {
  279. // Do calculation, of not done yet
  280. if (!$this->calculationDone) {
  281. $this->DoCalculation();
  282. }
  283.  
  284. return $this->query.' LIMIT '.$this->limitOffset.','.$this->entriesPerPage;
  285. }
  286.  
  287. /**
  288. * Returns the total entries.
  289. *
  290. * @return int
  291. */
  292. public function GetTotalEntries () {
  293. // Do calculation, of not done yet
  294. if (!$this->calculationDone) {
  295. $this->DoCalculation();
  296. }
  297.  
  298. return $this->entriesTotal;
  299. }
  300.  
  301. /**
  302. * Returns a prepared URL.
  303. *
  304. * This method will verify and/or modify an URL, so it fits the needs of
  305. * the HTML-handler.
  306. *
  307. * @param string $url
  308. * @return string
  309. */
  310. private function PrepareURL ($url) {
  311. // If this is still a prepared URL, return it as it is
  312. if (strpos($url, '%PN%') !== false) {
  313. return $url;
  314. } else {
  315. // Check if there is an anker given
  316. if (preg_match("#(\\#.*)$#", $url, $ankerResult)) {
  317. $anker = $ankerResult[1];
  318. } else {
  319. $anker = '';
  320. }
  321. $url = preg_replace("#(\\#.*)$#", '', $url);
  322.  
  323. // Parse the GetName, if needed, out
  324. $url = preg_replace("#(\\?|&)".preg_quote($this->getName, '#')."=([^&\\#]*)#", '', $url);
  325.  
  326. // Check wether you hase to use ? oder &
  327. if (strpos($url, '?') === false) {
  328. return $url.'?'.$this->getName.'=%PN%'.$anker;
  329. } else {
  330. return $url.'&'.$this->getName.'=%PN%'.$anker;
  331. }
  332. }
  333. }
  334.  
  335. /**
  336. * Calculate the things, which the Get*-methods need.
  337. *
  338. * Here we calculate the most important things, like total entries, how
  339. * many pages we have, the current page and the offset for the query.
  340. *
  341. * @return boolean
  342. */
  343. private function DoCalculation () {
  344. // Check if the user has give us a query
  345. if (is_null($this->query)) {
  346. trigger_error('PageLister: No query given. Use SetQuery() first.', E_USER_ERROR);
  347. }
  348.  
  349. // Get total entries
  350. $this->entriesTotal = $this->countHandler->CountTotalEntries($this->query);
  351.  
  352. // Calculate some data
  353. $this->pages = ceil($this->entriesTotal / $this->entriesPerPage);
  354. $this->currentPage = max(1, min($this->pages, (isset($_GET[$this->getName]) ? (int)$_GET[$this->getName]: 1)));
  355. $this->limitOffset = ($this->currentPage - 1) * $this->entriesPerPage;
  356.  
  357. // Calculation is done, remember it
  358. $this->calculationDone = true;
  359.  
  360. // Simply return true
  361. return true;
  362. }
  363. }
  364. ?>

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