PageRenderTime 37ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Pager/Layout.php

https://github.com/nationalfield/symfony
PHP | 490 lines | 171 code | 56 blank | 263 comment | 13 complexity | c6afd8557fd2c50f84cec8d500a6c277 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.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_Pager_Layout
  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.doctrine-project.org
  30. * @since 0.9
  31. */
  32. class Doctrine_Pager_Layout
  33. {
  34. /**
  35. * @var Doctrine_Pager $_pager Doctrine_Pager object related to the pager layout
  36. */
  37. private $_pager;
  38. /**
  39. * @var Doctrine_Pager_Range $_pagerRange Doctrine_Pager_Range object related to the pager layout
  40. */
  41. private $_pagerRange;
  42. /**
  43. * @var string $_template Template to be applied for inactive pages
  44. * (and also active is selected template is not defined)
  45. */
  46. private $_template;
  47. /**
  48. * @var string $_selectedTemplate Template to be applied for active page
  49. */
  50. private $_selectedTemplate;
  51. /**
  52. * @var string $_separatorTemplate Separator template, applied between each page
  53. */
  54. private $_separatorTemplate;
  55. /**
  56. * @var string $_urlMask URL to be assigned for each page. Masks are used as: {%var_name}
  57. */
  58. private $_urlMask;
  59. /**
  60. * @var array $_maskReplacements Stores references of masks and their correspondent
  61. * (replaces defined masks with new masks or values)
  62. */
  63. private $_maskReplacements = array();
  64. /**
  65. * __construct
  66. *
  67. * @param Doctrine_Pager $pager Doctrine_Pager object related to the pager layout
  68. * @param Doctrine_Pager_Range $pagerRange Doctrine_Pager_Range object related to the pager layout
  69. * @param string $urlMask URL to be assigned for each page
  70. * @return void
  71. */
  72. public function __construct($pager, $pagerRange, $urlMask)
  73. {
  74. $this->_setPager($pager);
  75. $this->_setPagerRange($pagerRange);
  76. $this->_setUrlMask($urlMask);
  77. $this->setTemplate('[<a href="{%url}">{%page}</a>]');
  78. $this->setSelectedTemplate('');
  79. $this->setSeparatorTemplate('');
  80. }
  81. /**
  82. * getPager
  83. *
  84. * Returns the Doctrine_Pager object related to the pager layout
  85. *
  86. * @return Doctrine_Pager Doctrine_Pager object related to the pager range
  87. */
  88. public function getPager()
  89. {
  90. return $this->_pager;
  91. }
  92. /**
  93. * _setPager
  94. *
  95. * Defines the Doctrine_Pager object related to the pager layout
  96. *
  97. * @param $pager Doctrine_Pager object related to the pager range
  98. * @return void
  99. */
  100. protected function _setPager($pager)
  101. {
  102. $this->_pager = $pager;
  103. }
  104. /**
  105. * execute
  106. *
  107. * Handy method to execute the query without need to retrieve the Pager instance
  108. *
  109. * @param $params Optional parameters to Doctrine_Query::execute
  110. * @param $hydrationMode Hydration Mode of Doctrine_Query::execute returned ResultSet.
  111. * @return Doctrine_Collection The root collection
  112. */
  113. public function execute($params = array(), $hydrationMode = null)
  114. {
  115. return $this->getPager()->execute($params, $hydrationMode);
  116. }
  117. /**
  118. * getPagerRange
  119. *
  120. * Returns the Doctrine_Pager_Range subclass object related to the pager layout
  121. *
  122. * @return Doctrine_Pager_Range Doctrine_Pager_Range subclass object related to the pager range
  123. */
  124. public function getPagerRange()
  125. {
  126. return $this->_pagerRange;
  127. }
  128. /**
  129. * _setPagerRange
  130. *
  131. * Defines the Doctrine_Pager_Range subclass object related to the pager layout
  132. *
  133. * @param $pagerRange Doctrine_Pager_Range subclass object related to the pager range
  134. * @return void
  135. */
  136. protected function _setPagerRange($pagerRange)
  137. {
  138. $this->_pagerRange = $pagerRange;
  139. $this->getPagerRange()->setPager($this->getPager());
  140. }
  141. /**
  142. * getUrlMask
  143. *
  144. * Returns the URL to be assigned for each page
  145. *
  146. * @return string URL to be assigned for each page
  147. */
  148. public function getUrlMask()
  149. {
  150. return $this->_urlMask;
  151. }
  152. /**
  153. * _setUrlMask
  154. *
  155. * Defines the URL to be assigned for each page
  156. *
  157. * @param $urlMask URL to be assigned for each page
  158. * @return void
  159. */
  160. protected function _setUrlMask($urlMask)
  161. {
  162. $this->_urlMask = $urlMask;
  163. }
  164. /**
  165. * getTemplate
  166. *
  167. * Returns the Template to be applied for inactive pages
  168. *
  169. * @return string Template to be applied for inactive pages
  170. */
  171. public function getTemplate()
  172. {
  173. return $this->_template;
  174. }
  175. /**
  176. * setTemplate
  177. *
  178. * Defines the Template to be applied for inactive pages
  179. * (also active page if selected template not defined)
  180. *
  181. * @param $template Template to be applied for inactive pages
  182. * @return void
  183. */
  184. public function setTemplate($template)
  185. {
  186. $this->_template = $template;
  187. }
  188. /**
  189. * getSelectedTemplate
  190. *
  191. * Returns the Template to be applied for active page
  192. *
  193. * @return string Template to be applied for active page
  194. */
  195. public function getSelectedTemplate()
  196. {
  197. return $this->_selectedTemplate;
  198. }
  199. /**
  200. * setSelectedTemplate
  201. *
  202. * Defines the Template to be applied for active page
  203. *
  204. * @param $selectedTemplate Template to be applied for active page
  205. * @return void
  206. */
  207. public function setSelectedTemplate($selectedTemplate)
  208. {
  209. $this->_selectedTemplate = $selectedTemplate;
  210. }
  211. /**
  212. * getSeparatorTemplate
  213. *
  214. * Returns the Separator template, applied between each page
  215. *
  216. * @return string Separator template, applied between each page
  217. */
  218. public function getSeparatorTemplate()
  219. {
  220. return $this->_separatorTemplate;
  221. }
  222. /**
  223. * setSeparatorTemplate
  224. *
  225. * Defines the Separator template, applied between each page
  226. *
  227. * @param $separatorTemplate Separator template, applied between each page
  228. * @return void
  229. */
  230. public function setSeparatorTemplate($separatorTemplate)
  231. {
  232. $this->_separatorTemplate = $separatorTemplate;
  233. }
  234. /**
  235. * addMaskReplacement
  236. *
  237. * Defines a mask replacement. When parsing template, it converts replacement
  238. * masks into new ones (or values), allowing to change masks behavior on the fly
  239. *
  240. * @param $oldMask Mask to be replaced
  241. * @param $newMask Mask or Value that will be defined after replacement
  242. * @param $asValue Optional value (default false) that if defined as true,
  243. * changes the bahavior of replacement mask to replacement
  244. * value
  245. * @return void
  246. */
  247. public function addMaskReplacement($oldMask, $newMask, $asValue = false)
  248. {
  249. if (($oldMask = trim($oldMask)) != 'page_number') {
  250. $this->_maskReplacements[$oldMask] = array(
  251. 'newMask' => $newMask,
  252. 'asValue' => ($asValue === false) ? false : true
  253. );
  254. }
  255. }
  256. /**
  257. * removeMaskReplacement
  258. *
  259. * Remove a mask replacement
  260. *
  261. * @param $oldMask Replacement Mask to be removed
  262. * @return void
  263. */
  264. public function removeMaskReplacement($oldMask)
  265. {
  266. if (isset($this->_maskReplacements[$oldMask])) {
  267. $this->_maskReplacements[$oldMask] = null;
  268. unset($this->_maskReplacements[$oldMask]);
  269. }
  270. }
  271. /**
  272. * cleanMaskReplacements
  273. *
  274. * Remove all mask replacements
  275. *
  276. * @return void
  277. */
  278. public function cleanMaskReplacements()
  279. {
  280. $this->_maskReplacements = null;
  281. $this->_maskReplacements = array();
  282. }
  283. /**
  284. * display
  285. *
  286. * Displays the pager on screen based on templates and options defined
  287. *
  288. * @param $options Optional parameters to be applied in template and url mask
  289. * @param $return Optional parameter if you want to capture the output of this method call
  290. * (Default value is false), instead of printing it
  291. * @return void If you would like to capture the output of Doctrine_Pager_Layout::display(),
  292. * use the $return parameter. If this parameter is set to TRUE, this method
  293. * will return its output, instead of printing it (which it does by default)
  294. */
  295. public function display($options = array(), $return = false)
  296. {
  297. $range = $this->getPagerRange()->rangeAroundPage();
  298. $str = '';
  299. // For each page in range
  300. for ($i = 0, $l = count($range); $i < $l; $i++) {
  301. // Define some optional mask values
  302. $options['page_number'] = $range[$i];
  303. $str .= $this->processPage($options);
  304. // Apply separator between pages
  305. if ($i < $l - 1) {
  306. $str .= $this->getSeparatorTemplate();
  307. }
  308. }
  309. // Possible wish to return value instead of print it on screen
  310. if ($return) {
  311. return $str;
  312. }
  313. echo $str;
  314. }
  315. /**
  316. * processPage
  317. *
  318. * Parses the template and returns the string of a processed page
  319. *
  320. * @param array Optional parameters to be applied in template and url mask
  321. * @return string Processed template for the given page
  322. */
  323. public function processPage($options = array())
  324. {
  325. // Check if at least basic options are defined
  326. if ( !isset($options['page_number'])) {
  327. throw new Doctrine_Pager_Exception(
  328. 'Cannot process template of the given page. ' .
  329. 'Missing at least one of needed parameters: \'page\' or \'page_number\''
  330. );
  331. // Should never reach here
  332. return '';
  333. }
  334. // Assign "page" options index if not defined yet
  335. if ( !isset($this->_maskReplacements['page']) && !isset($options['page'])) {
  336. $options['page'] = $options['page_number'];
  337. }
  338. return $this->_parseTemplate($options);
  339. }
  340. /**
  341. * Simply calls display, and returns the output.
  342. */
  343. public function __toString()
  344. {
  345. return $this->display(array(), true);
  346. }
  347. /**
  348. * _parseTemplate
  349. *
  350. * Parse the template of a given page and return the processed template
  351. *
  352. * @param array Optional parameters to be applied in template and url mask
  353. * @return string
  354. */
  355. protected function _parseTemplate($options = array())
  356. {
  357. $str = $this->_parseUrlTemplate($options);
  358. $replacements = $this->_parseReplacementsTemplate($options);
  359. return strtr($str, $replacements);
  360. }
  361. /**
  362. * _parseUrlTemplate
  363. *
  364. * Parse the url mask to return the correct template depending of the options sent.
  365. * Already process the mask replacements assigned.
  366. *
  367. * @param $options Optional parameters to be applied in template and url mask
  368. * @return string
  369. */
  370. protected function _parseUrlTemplate($options = array())
  371. {
  372. $str = '';
  373. // If given page is the current active one
  374. if ($options['page_number'] == $this->getPager()->getPage()) {
  375. $str = $this->_parseMaskReplacements($this->getSelectedTemplate());
  376. }
  377. // Possible attempt where Selected == Template
  378. if ($str == '') {
  379. $str = $this->_parseMaskReplacements($this->getTemplate());
  380. }
  381. return $str;
  382. }
  383. /**
  384. * _parseUrl
  385. *
  386. * Parse the mask replacements of a given page
  387. *
  388. * @param $options Optional parameters to be applied in template and url mask
  389. * @return string
  390. */
  391. protected function _parseReplacementsTemplate($options = array())
  392. {
  393. // Defining "url" options index to allow {%url} mask
  394. $options['url'] = $this->_parseUrl($options);
  395. $replacements = array();
  396. foreach ($options as $k => $v) {
  397. $replacements['{%'.$k.'}'] = $v;
  398. }
  399. return $replacements;
  400. }
  401. /**
  402. * _parseUrl
  403. *
  404. * Parse the url mask of a given page and return the processed url
  405. *
  406. * @param $options Optional parameters to be applied in template and url mask
  407. * @return string
  408. */
  409. protected function _parseUrl($options = array())
  410. {
  411. $str = $this->_parseMaskReplacements($this->getUrlMask());
  412. $replacements = array();
  413. foreach ($options as $k => $v) {
  414. $replacements['{%'.$k.'}'] = $v;
  415. }
  416. return strtr($str, $replacements);
  417. }
  418. /**
  419. * _parseMaskReplacements
  420. *
  421. * Parse the mask replacements, changing from to-be replaced mask with new masks/values
  422. *
  423. * @param $str String to have masks replaced
  424. * @return string
  425. */
  426. protected function _parseMaskReplacements($str)
  427. {
  428. $replacements = array();
  429. foreach ($this->_maskReplacements as $k => $v) {
  430. $replacements['{%'.$k.'}'] = ($v['asValue'] === true) ? $v['newMask'] : '{%'.$v['newMask'].'}';
  431. }
  432. return strtr($str, $replacements);
  433. }
  434. }