Source for file PageLister_CountHandler.class.php

Documentation is available at PageLister_CountHandler.class.php

  1. <?php
  2. /**
  3. * Project: PageLister
  4. * File: PageLister_CountHandler.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. * Count handler for entries
  29. *
  30. * @package PageLister
  31. */
  32. class PageLister_CountHandler {
  33. /**
  34. * Handler function to count entries.
  35. *
  36. * @var string
  37. */
  38. private $handler = null;
  39.  
  40. /**
  41. * Custom arguments for the count function
  42. *
  43. * @var array
  44. */
  45. private $args = array();
  46.  
  47.  
  48. /**
  49. * Creates a default handler on-the-fly.
  50. *
  51. * If you do not need a custom handler, you can use this static function
  52. * to create a default handler. at the moment, MySQL and MySQLi is supported.
  53. * If you need you own handler, you can use <b>SetHandler()</b>.
  54. *
  55. * You can pass additional arguments to every default handler. MySQL can have
  56. * a link identifier, if you don't work with a single connection. For MySQLi,
  57. * you have to deliver the resource of the MySQLi-object. So there are three
  58. * options at the moment:
  59. *
  60. * <code>
  61. * $handler = PageLister::CountHandler('MySQL');
  62. * $handler = PageLister::CountHandler('MySQL', $db);
  63. * $handler = PageLister::CountHandler('MySQLi', $db);
  64. * </code>
  65. *
  66. * @param string $handler
  67. * @return object
  68. */
  69. public static function DefaultHandler ($handler) {
  70. $args = array_slice(func_get_args(), 1);
  71.  
  72. $countHandler = new PageLister_CountHandler;
  73.  
  74. if (!method_exists($countHandler, 'DefaultHandler_'.$handler)) {
  75. trigger_error('PageLister_CountHandler: Invalid default handler.', E_USER_ERROR);
  76. return false;
  77. }
  78.  
  79. call_user_func(array('PageLister_CountHandler', 'DefaultHandler_'.$handler), $countHandler, $args);
  80.  
  81. return $countHandler;
  82. }
  83.  
  84. /**
  85. * Sets a custom handler for counting.
  86. *
  87. * You may want to use you own handler for the counting. You cann pass a function
  88. * name, which should be called for this. PageLister_CountHandler will then call
  89. * those function with one argument, the query.
  90. *
  91. * @param string $function
  92. * @return boolean
  93. */
  94. public function SetHandler ($function) {
  95. if (!function_exists($function)) {
  96. trigger_error('PageLister_CountHandler: Function does not exist.', E_USER_ERROR);
  97. return false;
  98. }
  99.  
  100. $this->handler = $function;
  101.  
  102. return true;
  103. }
  104.  
  105. /**
  106. * Sets arguments for calling the count function
  107. *
  108. * If you add a custom handler, you may want to add custom args to the function,
  109. * which you cant deliver as strings, such as resources. Default handlers will
  110. * use this function by themself, so don't call it after creating a default
  111. * handler.
  112. *
  113. * @param array $args
  114. * @return boolean
  115. */
  116. public function SetArgs ($args) {
  117. if (!is_array($args)) {
  118. trigger_error('PageLister_CountHandler: Argument has to be an array.', E_USER_ERROR);
  119. return false;
  120. }
  121.  
  122. $this->args = $args;
  123.  
  124. return true;
  125. }
  126.  
  127. /**
  128. * Default handler for MySQL.
  129. *
  130. * @param resource $handler
  131. * @param array $args
  132. * @return boolean
  133. */
  134. public static function DefaultHandler_MySQL ($handler, $args) {
  135. $handler->SetArgs($args);
  136. if (isset($args[0])) {
  137. $mysql_query_addition = ', $args[0]';
  138. } else {
  139. $mysql_query_addition = '';
  140. }
  141.  
  142. $handler->SetHandler(
  143. create_function('$query, $args',
  144. '$mysqlQuery = mysql_query($query'.$mysql_query_addition.')
  145. or trigger_error(\'PageLister_CountHandler: SQL-Error: \'.mysql_error().\'\', E_USER_ERROR);
  146. $entriesTotal = mysql_num_rows($mysqlQuery);
  147. return $entriesTotal;
  148. ')
  149. );
  150.  
  151. return true;
  152. }
  153.  
  154. /**
  155. * Default handler for MySQLi.
  156. *
  157. * @param resource $handler
  158. * @param array $args
  159. * @return boolean
  160. */
  161. public static function DefaultHandler_MySQLi ($handler, $args) {
  162. try {
  163. if (!isset($args[0])) {
  164. throw new Exception();
  165. }
  166. if (!is_resource($args[0])) {
  167. throw new Exception();
  168. }
  169. } catch (Exception $e) {
  170. trigger_error('PageLister_CountHandler: Argument 1 has to be a ressource.', E_USER_ERROR);
  171. return false;
  172. }
  173.  
  174. $handler->SetArgs(array('result', $args[0]));
  175.  
  176. $handler->SetHandler(
  177. create_function('$query, $args',
  178. '$result = $args["result"]->query($query)
  179. or trigger_error(\'PageLister_CountHandler: SQL-Error: \'.mysqli_error().\'\', E_USER_ERROR);
  180. $entriesTotal = $result->num_rows;
  181. $result->close();
  182. return $entriesTotal;
  183. ')
  184. );
  185.  
  186. return true;
  187. }
  188.  
  189. /**
  190. * Returns the total amount of entries.
  191. *
  192. * After setting an handler, this method will count the total amount of
  193. * entries for the given query.
  194. *
  195. * @param string $query
  196. * @return int
  197. */
  198. public function CountTotalEntries ($query) {
  199. if (is_null($this->handler)) {
  200. trigger_error('PageLister_CountHandler: No handler set yet.', E_USER_ERROR);
  201. return false;
  202. }
  203.  
  204. $entries = call_user_func($this->handler, $query, $this->args);
  205.  
  206. return $entries;
  207. }
  208. }
  209. ?>

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