PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/application/libraries/MY_Pagination.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 110 lines | 33 code | 21 blank | 56 comment | 5 complexity | 05f31461877416b2c7b2253971034752 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * @name MY_Pagination.php
  4. * @version 1.0
  5. * @author Joost van Veen www.accentinteractive.nl
  6. * @created: Sun Jul 27 16:27:26 GMT 2008 16:27:26
  7. *
  8. * Extends CI's pagination class (http://codeigniter.com/user_guide/libraries/pagination.html)
  9. * It sets some variables for configuration of the pagination class dynamically,
  10. * depending on the URI, so we don't have to substract the offset from the URI,
  11. * or set $config['base_url'] and $config['uri_segment'] manually in the controller
  12. *
  13. * Here is what is set by this extension class:
  14. * 1. $this->offset - the current offset
  15. * 2. $this->uri_segment - the URI segment to be used for pagination
  16. * 3. $this->base_url - the base url to be used for pagination
  17. * (where $this refers to the pagination class)
  18. *
  19. * The way this works is simple:
  20. * If there we use pagination, it must ALWAYS follow the following syntax and be
  21. * located at the END of the URI:
  22. * PAGINATION_SELECTOR/offset
  23. *
  24. * The PAGINATION_SELECTOR is a special string which we know will ONLY be in the
  25. * URI when paging is set. Let's say the PAGINATION_SELECTOR is 'Page' (since most
  26. * coders never use any capitals in the URI, most of the times any string with
  27. * a single capital character in it will suffice).
  28. *
  29. * Example use (in controller):
  30. * // Initialize pagination
  31. * $config['total_rows'] = $this->db->count_all_results('my_table');
  32. * $config['per_page'] = 10; // You'd best set this in a config file, but hey
  33. * $this->pagination->initialize($config);
  34. * $this->data['pagination'] = $this->pagination->create_links();
  35. *
  36. * // Retrieve paginated results, using the dynamically determined offset
  37. * $this->db->limit($config['per_page'], $this->pagination->offset);
  38. * $query = $this->db->get('my_table');
  39. *
  40. */
  41. class MY_Pagination extends CI_Pagination {
  42. var $offset = 0;
  43. var $pagination_selector = 'page';
  44. function MY_Pagination()
  45. {
  46. parent::CI_Pagination();
  47. log_message('debug', "MY_Pagination Class Initialized");
  48. $this->_set_pagination_offset();
  49. }
  50. /**
  51. * Set dynamic pagination variables in $CI->data['pagvars']
  52. *
  53. */
  54. function _set_pagination_offset()
  55. {
  56. // Instantiate the CI super object so we have access to the uri class
  57. $CI =& get_instance();
  58. // parse uri
  59. preg_match('/\/'.$this->pagination_selector.'(\/)?([0-9]+)?$/i', $CI->uri->uri_string(), $matches);
  60. // Store pagination offset if it is set
  61. if ($matches) {
  62. // set uri based on matches
  63. $uri = substr($CI->uri->uri_string(), 0, strrpos($CI->uri->uri_string(), $matches[0]));
  64. // Get the segment offset for the pagination selector
  65. $segments = $CI->uri->segment_array();
  66. // Loop through segments to retrieve pagination offset
  67. foreach ($segments as $key => $value) {
  68. // Find the pagination_selector and work from there
  69. if ($value == $this->pagination_selector) {
  70. // Store pagination offset
  71. $this->offset = $CI->uri->segment($key + 1);
  72. // Store pagination segment
  73. $this->uri_segment = $key + 1;
  74. // Set base url for paging. This only works if the
  75. // pagination_selector and paging offset are AT THE END of
  76. // the URI!
  77. //$pos = strrpos($uri, $this->pagination_selector);
  78. $this->base_url = $CI->config->item('base_url') . $uri . '/' . $this->pagination_selector;
  79. }
  80. }
  81. }
  82. else { // Pagination selector was not found in URI string. So offset is 0
  83. $this->offset = 0;
  84. $this->uri_segment = 0;
  85. $this->base_url = $CI->config->item('base_url') . $CI->uri->uri_string() . '/' . $this->pagination_selector;
  86. }
  87. }
  88. }
  89. ?>