PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/system/application/plugins/doctrine/lib/Doctrine/Pager.php

https://github.com/kevindev/ci_campusync_auth
PHP | 576 lines | 203 code | 90 blank | 283 comment | 21 complexity | 85b7f3f8f9c72e3783172186c3f3a6dc MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Pager
  23. *
  24. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  25. * @package Doctrine
  26. * @subpackage Pager
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @version $Revision$
  29. * @link www.phpdoctrine.org
  30. * @since 0.9
  31. */
  32. class Doctrine_Pager
  33. {
  34. /**
  35. * @var Doctrine_Query $_query Doctrine_Query object related to the pager
  36. */
  37. protected $_query;
  38. /**
  39. * @var Doctrine_Query $_countQuery Doctrine_Query object related to the counter of pager
  40. */
  41. protected $_countQuery;
  42. /**
  43. * @var array $_countQueryParams Hold the params to be used by Doctrine_Query counter object of pager
  44. */
  45. protected $_countQueryParams;
  46. /**
  47. * @var integer $_numResults Number of results found
  48. */
  49. protected $_numResults;
  50. /**
  51. * @var integer $_maxPerPage Maximum number of itens per page
  52. */
  53. protected $_maxPerPage;
  54. /**
  55. * @var integer $page Current page
  56. */
  57. protected $_page;
  58. /**
  59. * @var integer $_lastPage Last page (total of pages)
  60. */
  61. protected $_lastPage;
  62. /**
  63. * @var boolean $_executed Pager was initialized (called "execute" at least once)
  64. */
  65. protected $_executed;
  66. /**
  67. * __construct
  68. *
  69. * @param mixed $query Accepts either a Doctrine_Query object or a string
  70. * (which does the Doctrine_Query class creation).
  71. * @param int $page Current page
  72. * @param int $maxPerPage Maximum itens per page
  73. * @return void
  74. */
  75. public function __construct($query, $page, $maxPerPage = 0)
  76. {
  77. $this->_setExecuted(false);
  78. $this->_setQuery($query);
  79. $this->_setPage($page);
  80. $this->setMaxPerPage($maxPerPage);
  81. }
  82. /**
  83. * _initialize
  84. *
  85. * Initialize Pager object calculating number of results
  86. *
  87. * @param $params Optional parameters to Doctrine_Query::execute
  88. * @return void
  89. */
  90. protected function _initialize($params = array())
  91. {
  92. // retrieve the number of items found
  93. $countQuery = $this->getCountQuery();
  94. $count = $countQuery->count($this->getCountQueryParams($params));
  95. $this->_setNumResults($count);
  96. $this->_setExecuted(true); // _adjustOffset relies of _executed equals true = getNumResults()
  97. $this->_adjustOffset();
  98. }
  99. /**
  100. * _adjustOffset
  101. *
  102. * Adjusts last page of Doctrine_Pager, offset and limit of Doctrine_Query associated
  103. *
  104. * @return void
  105. */
  106. protected function _adjustOffset()
  107. {
  108. // Define new total of pages
  109. $this->_setLastPage(
  110. max(1, ceil($this->getNumResults() / $this->getMaxPerPage()))
  111. );
  112. $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
  113. // Assign new offset and limit to Doctrine_Query object
  114. $p = $this->getQuery();
  115. $p->offset($offset);
  116. $p->limit($this->getMaxPerPage());
  117. }
  118. /**
  119. * getExecuted
  120. *
  121. * Returns the check if Pager was already executed at least once
  122. *
  123. * @return boolen Pager was executed
  124. */
  125. public function getExecuted()
  126. {
  127. return $this->_executed;
  128. }
  129. /**
  130. * _setExecuted
  131. *
  132. * Defines if Pager was already executed
  133. *
  134. * @param $executed Pager was executed
  135. * @return void
  136. */
  137. protected function _setExecuted($executed)
  138. {
  139. $this->_executed = $executed;
  140. }
  141. /**
  142. * getRange
  143. *
  144. * Builds and return a Doctrine_Pager_Range_* based on arguments
  145. *
  146. * @param string $rangeStyle Pager Range style
  147. * @param array $options Custom subclass implementation options.
  148. * Default is a blank array
  149. * @return Doctrine_Pager_Range Pager Range
  150. */
  151. public function getRange($rangeStyle, $options = array())
  152. {
  153. $class = 'Doctrine_Pager_Range_' . ucfirst($rangeStyle);
  154. return new $class($options, $this);
  155. }
  156. /**
  157. * getNumResults
  158. *
  159. * Returns the number of results found
  160. *
  161. * @return int the number of results found
  162. */
  163. public function getNumResults()
  164. {
  165. if ($this->getExecuted()) {
  166. return $this->_numResults;
  167. }
  168. throw new Doctrine_Pager_Exception(
  169. 'Cannot retrieve the number of results of a not yet executed Pager query'
  170. );
  171. }
  172. /**
  173. * _setNumResults
  174. *
  175. * Defines the number of total results on initial query
  176. *
  177. * @param $nb Number of results found on initial query fetch
  178. * @return void
  179. */
  180. protected function _setNumResults($nb)
  181. {
  182. $this->_numResults = $nb;
  183. }
  184. /**
  185. * getFirstPage
  186. *
  187. * Returns the first page
  188. *
  189. * @return int first page
  190. */
  191. public function getFirstPage()
  192. {
  193. return 1;
  194. }
  195. /**
  196. * getLastPage
  197. *
  198. * Returns the last page (total of pages)
  199. *
  200. * @return int last page (total of pages)
  201. */
  202. public function getLastPage()
  203. {
  204. if ($this->getExecuted()) {
  205. return $this->_lastPage;
  206. }
  207. throw new Doctrine_Pager_Exception(
  208. 'Cannot retrieve the last page number of a not yet executed Pager query'
  209. );
  210. }
  211. /**
  212. * _setLastPage
  213. *
  214. * Defines the last page (total of pages)
  215. *
  216. * @param $page last page (total of pages)
  217. * @return void
  218. */
  219. protected function _setLastPage($page)
  220. {
  221. $this->_lastPage = $page;
  222. if ($this->getPage() > $page) {
  223. $this->_setPage($page);
  224. }
  225. }
  226. /**
  227. * getLastPage
  228. *
  229. * Returns the current page
  230. *
  231. * @return int current page
  232. */
  233. public function getPage()
  234. {
  235. return $this->_page;
  236. }
  237. /**
  238. * getNextPage
  239. *
  240. * Returns the next page
  241. *
  242. * @return int next page
  243. */
  244. public function getNextPage()
  245. {
  246. if ($this->getExecuted()) {
  247. return min($this->getPage() + 1, $this->getLastPage());
  248. }
  249. throw new Doctrine_Pager_Exception(
  250. 'Cannot retrieve the last page number of a not yet executed Pager query'
  251. );
  252. }
  253. /**
  254. * getPreviousPage
  255. *
  256. * Returns the previous page
  257. *
  258. * @return int previous page
  259. */
  260. public function getPreviousPage()
  261. {
  262. if ($this->getExecuted()) {
  263. return max($this->getPage() - 1, $this->getFirstPage());
  264. }
  265. throw new Doctrine_Pager_Exception(
  266. 'Cannot retrieve the previous page number of a not yet executed Pager query'
  267. );
  268. }
  269. /**
  270. * getFirstIndice
  271. *
  272. * Return the first indice number for the current page
  273. *
  274. * @return int First indice number
  275. */
  276. public function getFirstIndice()
  277. {
  278. return ($this->getPage() - 1) * $this->getMaxPerPage() + 1;
  279. }
  280. /**
  281. * getLastIndice
  282. *
  283. * Return the last indice number for the current page
  284. *
  285. * @return int Last indice number
  286. */
  287. public function getLastIndice()
  288. {
  289. return min($this->getNumResults(), ($this->getPage() * $this->getMaxPerPage()));
  290. }
  291. /**
  292. * haveToPaginate
  293. *
  294. * Return true if it's necessary to paginate or false if not
  295. *
  296. * @return bool true if it is necessary to paginate, false otherwise
  297. */
  298. public function haveToPaginate()
  299. {
  300. if ($this->getExecuted()) {
  301. return $this->getNumResults() > $this->getMaxPerPage();
  302. }
  303. throw new Doctrine_Pager_Exception(
  304. 'Cannot know if it is necessary to paginate a not yet executed Pager query'
  305. );
  306. }
  307. /**
  308. * setPage
  309. *
  310. * Defines the current page and automatically adjust offset and limits
  311. *
  312. * @param $page current page
  313. * @return void
  314. */
  315. public function setPage($page)
  316. {
  317. $this->_setPage($page);
  318. $this->_setExecuted(false);
  319. }
  320. /**
  321. * _setPage
  322. *
  323. * Defines the current page
  324. *
  325. * @param $page current page
  326. * @return void
  327. */
  328. private function _setPage($page)
  329. {
  330. $page = intval($page);
  331. $this->_page = ($page <= 0) ? 1 : $page;
  332. }
  333. /**
  334. * getLastPage
  335. *
  336. * Returns the maximum number of itens per page
  337. *
  338. * @return int maximum number of itens per page
  339. */
  340. public function getMaxPerPage()
  341. {
  342. return $this->_maxPerPage;
  343. }
  344. /**
  345. * setMaxPerPage
  346. *
  347. * Defines the maximum number of itens per page and automatically adjust offset and limits
  348. *
  349. * @param $max maximum number of itens per page
  350. * @return void
  351. */
  352. public function setMaxPerPage($max)
  353. {
  354. if ($max > 0) {
  355. $this->_maxPerPage = $max;
  356. } else if ($max == 0) {
  357. $this->_maxPerPage = 25;
  358. } else {
  359. $this->_maxPerPage = abs($max);
  360. }
  361. $this->_setExecuted(false);
  362. }
  363. /**
  364. * getResultsInPage
  365. *
  366. * Returns the number of itens in current page
  367. *
  368. * @return int Number of itens in current page
  369. */
  370. public function getResultsInPage()
  371. {
  372. $page = $this->getPage();
  373. if ($page != $this->getLastPage()) {
  374. return $this->getMaxPerPage();
  375. }
  376. $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
  377. return abs($this->getNumResults() - $offset);
  378. }
  379. /**
  380. * getQuery
  381. *
  382. * Returns the Doctrine_Query collector object related to the pager
  383. *
  384. * @return Doctrine_Query Doctrine_Query object related to the pager
  385. */
  386. public function getQuery()
  387. {
  388. return $this->_query;
  389. }
  390. /**
  391. * _setQuery
  392. *
  393. * Defines the collector query to be used by pager
  394. *
  395. * @param Doctrine_Query Accepts either a Doctrine_Query object or a string
  396. * (which does the Doctrine_Query class creation).
  397. * @return void
  398. */
  399. protected function _setQuery($query)
  400. {
  401. if (is_string($query)) {
  402. $query = Doctrine_Query::create()->parseQuery($query);
  403. }
  404. $this->_query = $query;
  405. }
  406. /**
  407. * getCountQuery
  408. *
  409. * Returns the Doctrine_Query object that is used to make the count results to pager
  410. *
  411. * @return Doctrine_Query Doctrine_Query object related to the pager
  412. */
  413. public function getCountQuery()
  414. {
  415. return ($this->_countQuery !== null) ? $this->_countQuery : $this->_query;
  416. }
  417. /**
  418. * setCountQuery
  419. *
  420. * Defines the counter query to be used by pager
  421. *
  422. * @param Doctrine_Query Accepts either a Doctrine_Query object or a string
  423. * (which does the Doctrine_Query class creation).
  424. * @param array Optional params to be used by counter Doctrine_Query.
  425. * If not defined, the params passed to execute method will be used.
  426. * @return void
  427. */
  428. public function setCountQuery($query, $params = null)
  429. {
  430. if (is_string($query)) {
  431. $query = Doctrine_Query::create()->parseQuery($query);
  432. }
  433. $this->_countQuery = $query;
  434. $this->setCountQueryParams($params);
  435. $this->_setExecuted(false);
  436. }
  437. /**
  438. * getCountQueryParams
  439. *
  440. * Returns the params to be used by counter Doctrine_Query
  441. *
  442. * @return array Doctrine_Query counter params
  443. */
  444. public function getCountQueryParams($defaultParams = array())
  445. {
  446. return ($this->_countQueryParams !== null) ? $this->_countQueryParams : $defaultParams;
  447. }
  448. /**
  449. * setCountQueryParams
  450. *
  451. * Defines the params to be used by counter Doctrine_Query
  452. *
  453. * @param array Optional params to be used by counter Doctrine_Query.
  454. * If not defined, the params passed to execute method will be used.
  455. * @param boolean Optional argument that append the query param instead of overriding the existent ones.
  456. * @return void
  457. */
  458. public function setCountQueryParams($params = array(), $append = false)
  459. {
  460. if ($append && is_array($this->_countQueryParams)) {
  461. $this->_countQueryParams = array_merge($this->_countQueryParams, $params);
  462. } else {
  463. if ($params !== null && !is_array($params)) {
  464. $params = array($params);
  465. }
  466. $this->_countQueryParams = $params;
  467. }
  468. $this->_setExecuted(false);
  469. }
  470. /**
  471. * execute
  472. *
  473. * Executes the query, populates the collection and then return it
  474. *
  475. * @param $params Optional parameters to Doctrine_Query::execute
  476. * @param $hydrationMode Hydration Mode of Doctrine_Query::execute returned ResultSet.
  477. * @return Doctrine_Collection The root collection
  478. */
  479. public function execute($params = array(), $hydrationMode = null)
  480. {
  481. if (!$this->getExecuted()) {
  482. $this->_initialize($params);
  483. }
  484. return $this->getQuery()->execute($params, $hydrationMode);
  485. }
  486. }