You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

  1. <?php
  2. /**
  3. * PagerAction
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link
  7. * @package Nakon
  8. * @subpackage face
  9. * @since 04.02.2008
  10. * @version SVN$
  11. * @filesource $URL$
  12. */
  13. class Pager extends Action
  14. {
  15. public $template_dir = '.';
  16. protected $current_page = 1;
  17. protected $max_page_num;
  18. protected $num_rows;
  19. protected $limit;
  20. protected $start_offset = 0;
  21. protected $count = 0;
  22. public function __construct($count, $current_page = 1, $records_limit = 20)
  23. {
  24. parent::__construct();
  25. $this->count = $count;
  26. $this->limit = $records_limit;
  27. $this->max_page_num = ceil($this->count/$this->limit);
  28. $this->current_page = ((int)$current_page <= $this->max_page_num && (int)$current_page > 0) ? (int)$current_page : 1;
  29. $this->start_offset = $this->limit * ($this->current_page - 1);
  30. $this->num_rows = ($this->limit + $this->start_offset) <= $this->count ? ($this->limit + $this->start_offset) : $this->count;
  31. }
  32. /**
  33. * Возвращает текущую страницу
  34. *
  35. * @return int
  36. */
  37. public function getPage()
  38. {
  39. return $this->current_page;
  40. }
  41. public function startOffset()
  42. {
  43. return $this->start_offset;
  44. }
  45. public function limit()
  46. {
  47. return $this->limit;
  48. }
  49. public function setNumRows($num_rows)
  50. {
  51. $this->num_rows = $num_rows;
  52. }
  53. function init()
  54. {
  55. $this->template = "Pager";
  56. }
  57. function prepare()
  58. {
  59. $this->templater->assign('page', $this->current_page);
  60. $this->templater->assign('page_max', $this->max_page_num);
  61. $this->templater->assign('limit', $this->limit);
  62. $this->templater->assign('count', $this->count);
  63. }
  64. }
  65. ?>