PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/components/pagination.php

https://bitbucket.org/fastestsolution/dev_poda_cz
PHP | 507 lines | 271 code | 29 blank | 207 comment | 29 complexity | 82f09dbef68134c3ee69979aa4abe614 MD5 | raw file
  1. <?php
  2. /**
  3. * Pagination Component, responsible for managing the DATA required for pagination.
  4. */
  5. class PaginationComponent extends Object
  6. {
  7. // Configuration/Default variables
  8. /**
  9. * Specify whether the component will use AJAX links if available.
  10. * Tests for the presence of the RequestHandler component and if present will generate
  11. * AJAX links. However, if the prototype library js has not been included, normal updates
  12. * will take place.
  13. *
  14. * @var boolean
  15. * @access public
  16. */
  17. var $ajaxAutoDetect = true;
  18. /**
  19. * Specify the div to update if using ajax updates
  20. *
  21. * @var string
  22. * @access public
  23. */
  24. var $ajaxDivUpdate = "content";
  25. /**
  26. * The id of a form ID to be submitted with all pagination ajax links.
  27. *
  28. * @var string
  29. * @access public
  30. */
  31. var $ajaxFormId = NULL;
  32. /**
  33. * Specify link style
  34. *
  35. * @var string "html"/"ajax"
  36. * @access public
  37. */
  38. var $style = "html";
  39. /**
  40. * Specify link parameter style
  41. *
  42. * @var string "get"/"pretty"
  43. * @access public
  44. */
  45. var $paramStyle = "get";
  46. /**
  47. * Used only for pretty style urls - must match the
  48. *
  49. * @var string "get"/"pretty"
  50. * @access public
  51. */
  52. var $paramSeperator = "/";
  53. /**
  54. * Specify DEFAULT start page number
  55. *
  56. * @var integer
  57. * @access public
  58. */
  59. var $page = 1;
  60. /**
  61. * Specify DEFAULT number of items to be displayed per page. Also used as the limit
  62. * for the subsequent SQL search.
  63. *
  64. * @var integer
  65. * @access public
  66. */
  67. var $show = 5;
  68. /**
  69. * Specify DEFAULT sort column.
  70. *
  71. * @var string
  72. * @access public
  73. */
  74. var $sortBy = 'id';
  75. /**
  76. * Specify DEFAULT sort direction.
  77. *
  78. * @var string
  79. * @access public
  80. */
  81. var $direction = 'ASC';
  82. /**
  83. * Specify the maximum number of pages to be included in the list of pages. Should be an odd number, otherwise rounded down.
  84. *
  85. * @var integer
  86. * @access public
  87. */
  88. var $maxPages = 10;
  89. /**
  90. * Options for results per page.
  91. *
  92. * @var array
  93. * @access public
  94. */
  95. var $resultsPerPage = array(2,5,10,20,50,100,500);
  96. /**
  97. * Show links to the first and last page, if the number of pages exceeds the maxPage count.
  98. *
  99. * @var boolean
  100. * @access public
  101. */
  102. var $showLimits = true;
  103. /**
  104. * An array of parameter names which cannot be specified by the url
  105. *
  106. * @var array
  107. * @access public
  108. */
  109. var $privateParams = Array();
  110. // Do not edit below this line unless you wish to customize the core functionality of this Component
  111. /**
  112. * Place holder for the sort class. Irrelavent for models without associations
  113. *
  114. * @var boolean
  115. * @access private
  116. */
  117. var $sortByClass = NULL;
  118. /**
  119. * Place holder for the model class.
  120. *
  121. * @var boolean
  122. * @access private
  123. */
  124. var $modelClass = NULL;
  125. /**
  126. * Place holder for the base url
  127. *
  128. * @var boolean
  129. * @access private
  130. */
  131. var $url = NULL;
  132. /**
  133. * Place holder for the controller
  134. *
  135. * @var boolean
  136. * @access private
  137. */
  138. var $controller = true;
  139. /**
  140. * Place holder for the sanitize object
  141. *
  142. * @var boolean
  143. * @access private
  144. */
  145. var $sanitize = true;
  146. /**
  147. * Place holder for the data array passed to the view
  148. *
  149. * @var boolean
  150. * @access private
  151. */
  152. var $paging;
  153. /**
  154. * Startup - Link the component to the controller.
  155. *
  156. * @param controller
  157. */
  158. function startup(&$controller)
  159. {
  160. $this->controller =& $controller;
  161. }
  162. /**
  163. * Initialize the pagination data.
  164. *
  165. * @param unknown
  166. * @param array
  167. * @options array
  168. * @return array
  169. */
  170. var $recursive = 0;
  171. function init($criteria=NULL,$parameters=Array(),$options=Array())
  172. {
  173. // oprava GET kvuli validite &amp;
  174. $_get = array();
  175. foreach($_GET as $key => $value){
  176. $_get[strtr($key,array('amp;'=>''))] = $value;
  177. }
  178. $_GET = $_get;
  179. uses('sanitize');
  180. $this->Sanitize = &new Sanitize;
  181. $this->_initFields($options);
  182. $this->_checkAjax();
  183. $this->_initSort();
  184. $this->_initPaging($parameters);
  185. $this->_initURL();
  186. $this->_setParameter("show",$parameters);
  187. // If the number of results per page isn't in the list, reset to default
  188. if ((isset($this->paging["show"]))&&(!in_array($this->paging["show"],$this->resultsPerPage)))
  189. {
  190. $this->paging["show"]=$this->paging['Defaults']['show'];
  191. }
  192. $this->_setParameter("page",$parameters);
  193. $this->_setParameter("sortBy",$parameters);
  194. $this->_setParameter("sortByClass",$parameters); // Overriding the model class if specified.
  195. $this->_setParameter("direction",$parameters);
  196. $this->_check4Form();
  197. $this->_setPrivateParameter("ajaxDivUpdate");
  198. $this->_setPrivateParameter("ajaxFormId");
  199. $this->_setPrivateParameter("maxPages");
  200. $this->_setPrivateParameter("showLimits");
  201. $this->_setPrivateParameter("style");
  202. $this->_setPrivateParameter("paramStyle");
  203. $this->_setPrivateParameter("paramSeperator");
  204. $this->_setPrivateParameter("url");
  205. if (isset($this->total)) // If the field is already set, we passed in the options the total number of results
  206. {
  207. $count = $this->total;
  208. }
  209. else
  210. {
  211. //$this->controller->{$this->modelClass}->Behaviors->detach('Translation');
  212. //unset($this->controller->{$this->modelClass}->actsAs);
  213. $this->controller->{$this->modelClass}->Behaviors->detach('Trans');
  214. $count = $this->controller->{$this->modelClass}->find('count',array('conditions'=>$criteria));
  215. }
  216. //pr($count);
  217. //pr($criteria);
  218. //pr($count);
  219. $this->checkPage($count);
  220. $this->paging['total'] = $count;
  221. $this->trimResultsPerPage($count);
  222. $this->_setPrivateParameter("resultsPerPage");
  223. $this->paging['pageCount'] = ceil($count / $this->paging['show'] );
  224. $this->controller->set('paging',$this->paging);
  225. $this->order = $this->paging['sortByClass'].".".$this->paging['sortBy'].' '.strtoupper($this->paging['direction']);
  226. // For backwards compatability & clarity
  227. $this->limit = $this->paging['show'];
  228. $this->page = $this->paging['page'];
  229. // For less code in the calling method..
  230. return (Array($this->order,$this->paging['show'],$this->paging['page']));
  231. }
  232. /**
  233. * Don't give the choice to display pages with no results
  234. *
  235. * @param integer
  236. */
  237. function trimResultsPerPage ($count = 0)
  238. {
  239. while (($limit = current($this->resultsPerPage))&&(!isset($capKey)))
  240. {
  241. if ($limit >= $count)
  242. {
  243. $capKey = key($this->resultsPerPage);
  244. }
  245. next($this->resultsPerPage);
  246. if (isset($capKey))
  247. {
  248. array_splice($this->resultsPerPage, ($capKey+1));
  249. }
  250. }
  251. }
  252. /**
  253. * Set the page to the last if there would be no results, and to 1 if a negetive
  254. * page number is specified
  255. *
  256. * @param integer
  257. */
  258. function checkPage ($count = 0)
  259. {
  260. if ((($this->paging['page']-1)*$this->paging['show'])>=$count)
  261. {
  262. $this->paging['page'] = floor($count/$this->paging['show']+0.99);
  263. }
  264. }
  265. /**
  266. * Set Object fields
  267. *
  268. * @param unknown
  269. */
  270. function _initFields($options)
  271. {
  272. foreach($options as $option=>$val)
  273. {
  274. $this->$option = $val;
  275. }
  276. }
  277. /**
  278. * Set Pagination with default Parameters
  279. *
  280. * @param unknown
  281. */
  282. function _initPaging($parameters)
  283. {
  284. $this->paging['importParams']=$parameters;
  285. $this->paging['Defaults'] = Array (
  286. "page"=>$this->page,
  287. "show"=>$this->show,
  288. "sortBy"=>$this->sortBy,
  289. "sortByClass"=>$this->sortByClass,
  290. "direction"=>$this->direction
  291. );
  292. }
  293. /**
  294. * If everything is in place, use Ajax by default
  295. *
  296. * @param unknown
  297. */
  298. function _checkAjax(){
  299. if ($this->ajaxAutoDetect==true&& isset($this->controller->RequestHandler)){
  300. $this->style = "ajax";
  301. } else {
  302. //echo 'no';
  303. }
  304. }
  305. /**
  306. * Set the DEFAULT sort class
  307. *
  308. * @param unknown
  309. */
  310. function _initSort()
  311. {
  312. if (!$this->modelClass)
  313. {
  314. $ModelClass = $this->modelClass = $this->controller->modelClass;
  315. }
  316. else
  317. {
  318. $ModelClass = $this->modelClass;
  319. }
  320. if (!$this->sortBy)
  321. {
  322. $this->sortBy = $this->controller->$ModelClass->primaryKey;
  323. }
  324. if (!$this->sortByClass)
  325. {
  326. $this->sortByClass = $ModelClass;
  327. }
  328. }
  329. /**
  330. * Set the base url for updates.
  331. *
  332. * @param unknown
  333. */
  334. function _initURL()
  335. {
  336. if ($this->url) // A url was specified in the paramters
  337. {
  338. if (substr($this->url, -1, 1)<>"/")
  339. {
  340. $this->url .= "/";
  341. }
  342. }
  343. else // No url in the parameters, derive it.
  344. {
  345. if ($this->paramStyle=="get")
  346. {
  347. $this->url = str_replace($this->controller->webroot,"/",$this->controller->here);
  348. }
  349. else
  350. {
  351. $this->url = "";
  352. if (isset($this->controller->params['admin']))
  353. {
  354. $this->url .= "/".$this->controller->params['admin'];
  355. $action = substr($this->controller->action, strlen($this->controller->params['admin']."_"));
  356. }
  357. else
  358. {
  359. $action = $this->controller->action;
  360. }
  361. if ($this->controller->plugin)
  362. {
  363. $this->url .= "/".$this->controller->plugin;
  364. }
  365. $this->url .= "/".$this->controller->name;
  366. $this->url .= "/".$action;
  367. if (isset($this->paging['importParams']['_unamedParameters']))
  368. {
  369. $unnamedString = implode ("/", $this->paging['importParams']['_unamedParameters']);
  370. $this->url .= "/".$unnamedString;
  371. unset($this->paging['importParams']['_unamedParameters']);
  372. }
  373. $this->url .= "/";
  374. }
  375. }
  376. if (defined('BASE_URL')) { // Hack for no mod_rewrite
  377. $this->url = preg_replace( "!".BASE_URL."!", '', $this->url); // Remove the base from the url
  378. $this->url = preg_replace("!\?.*!", '', $this->url); // Remove the get parameters
  379. }
  380. }
  381. /**
  382. * If the parameters have been changed/set by a form action, update the params array.
  383. * Would perhaps be best to redirect to the equivalent url, which isn't implemented as
  384. * the relavent method is in the helper and as such inaccessible here.
  385. *
  386. * @param unknown
  387. */
  388. function _check4Form()
  389. {
  390. if(isset($this->controller->data['pagination']))
  391. {
  392. if (isset($this->controller->data['pagination']['sortByComposite']))
  393. {
  394. $Composite = Array();
  395. $Composite = explode("::",$this->controller->data['pagination']['sortByComposite']);
  396. if (isset($Composite[0]))
  397. {
  398. $this->controller->data['pagination']['sortBy'] = $Composite[0];
  399. }
  400. if (isset($Composite[1]))
  401. {
  402. $this->controller->data['pagination']['direction'] = $Composite[1];
  403. }
  404. if (isset($Composite[2]))
  405. {
  406. $this->controller->data['pagination']['sortByClass'] = $Composite[2];
  407. }
  408. else
  409. {
  410. $this->controller->data['pagination']['sortByClass'] = $this->paging['Defaults']['sortByClass'];
  411. }
  412. unset($this->controller->data['pagination']['sortByComposite']);
  413. }
  414. foreach($this->controller->data['pagination'] as $parameter=>$value)
  415. {
  416. if (!in_array($parameter, $this->privateParams))
  417. {
  418. $this->paging[$parameter] = $this->Sanitize->paranoid($value,array("-","_"));
  419. }
  420. }
  421. }
  422. }
  423. /**
  424. * Set a parameter to be passed to the view which cannot be specified/overriden from the url.
  425. *
  426. * @param unknown
  427. */
  428. function _setPrivateParameter($parameter)
  429. {
  430. $this->paging[$parameter]= $this->$parameter;
  431. }
  432. /**
  433. * Set a parameter to be passed to the view overriden from the url if present.
  434. *
  435. * @param unknown
  436. * @param array
  437. * @param field
  438. */
  439. function _setParameter($parameter,$parameters=Array(),$field=NULL)
  440. {
  441. $field = $field?$field:$parameter;
  442. if (in_array($parameter, $this->privateParams))
  443. {
  444. $this->paging[$field] = $this->paging['Defaults'][$field];
  445. }
  446. else
  447. {
  448. if ($this->paramStyle=="get")
  449. {
  450. if (isset($_GET[$parameter]))
  451. {
  452. $this->paging[$field] = $this->Sanitize->paranoid($_GET[$parameter],array("-","_"));
  453. }
  454. else
  455. {
  456. $this->paging[$field]= $this->$field;
  457. }
  458. }
  459. elseif ($this->paramStyle=="pretty")
  460. {
  461. if (isset($parameters[$parameter]))
  462. {
  463. $this->paging[$field] = $this->Sanitize->paranoid($parameters[$parameter],array("-","_"));
  464. }
  465. else
  466. {
  467. $this->paging[$field]= $this->$field;
  468. }
  469. }
  470. else
  471. {
  472. echo ("parameter error");
  473. die;
  474. }
  475. }
  476. }
  477. }
  478. ?>