root/trunk/Framework/Pager.php

Revision 204, 3.8 kB (checked in by jstump, 10 months ago)

Converted everything over to exceptions from PEAR_Error.

Line 
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6  * Framework_Pager
7  *
8  * @author      Joe Stump <joe@joestump.net>
9  * @copyright   (c) 2005 2006 Joseph C. Stump. All rights reserved.
10  * @package     Framework
11  * @filesource
12  */
13
14 /**
15  * Framework_Pager
16  *
17  * @author      Joe Stump <joe@joestump.net>
18  * @package     Framework
19  */
20 class Framework_Pager
21 {
22     /**
23      * $start
24      *
25      * @access      public
26      * @var         int             $start
27      */
28     public $start = 0;
29
30     /**
31      * $limit
32      *
33      * @access      public
34      * @var         int             $limit
35      */
36     public $limit = 20;
37
38     /**
39      * $total
40      *
41      * @access      public
42      * @var         int             $total
43      */
44     public $total = 0;
45
46     /**
47      * $pages
48      *
49      * The number of pages to return. If you only want to show 5 pages in your
50      * listing set this to 5. You can set this as the 4th parameter in the
51      * constructor
52      *
53      * @access      public
54      * @var         int             $pages
55      */
56     public $pages = 5;
57
58     /**
59      * $totalPages
60      *
61      * The total number of pages for the given listing. For instance if your
62      * query has a total of 150 records and your limit is 10 then this variable
63      * would hold 15.
64      *
65      * @access      protected
66      * @var         int             $totalPages
67      * @var         int             $totalPages
68      */
69     protected $totalPages = 0;
70
71     /**
72      * getNextPage
73      *
74      * Returns the $start value for your LIMIT clause for the next page.
75      *
76      * @access      public
77      * @return      int
78      */
79     public function getNextPage()
80     {
81         if (($this->start + $this->limit) > $this->total) {
82             $nextPage = 0;
83         } else {
84             $nextPage = ($this->start + $this->limit);
85         }
86
87         return $nextPage;
88     }
89
90     /**
91      * getPrevPage
92      *
93      * Returns the $start value for your LIMIT clause for the previous page.
94      *
95      * @access      public
96      * @return      int
97      */
98     public function getPrevPage()
99     {
100         if ($this->start == 0) {
101             $prevPage = 0;
102         } else {
103             $prevPage = ($this->start - $this->limit);
104         }
105
106         return $prevPage;
107     }
108
109     /**
110      * getPageList
111      *
112      * Returns the page list as an array keyed by the page number with the
113      * value of the $start variable.
114      *
115      * @access      public
116      * @return      array
117      */
118     public function getPageList()
119     {
120         $totalPages = $this->totalPages = (int)ceil(($this->total / $this->limit));
121         $startPage  = (ceil(($this->start / $this->limit)) + 1);
122         $endPage    = $totalPages;
123         $listStart  = ceil($startPage - ($this->pages / 2));
124         $listEnd    = floor($startPage + ($this->pages / 2));
125
126         if ($listEnd > $totalPages) {
127             $listStart = ($totalPages - ($this->pages - 1));
128             $listEnd = $totalPages;
129         }
130
131         if (($listEnd - abs($listStart)) < ($this->pages - 1)) {
132             $listEnd = $this->pages;
133         }
134
135         if($listStart < 1) {
136             $listStart = 1;
137         }
138
139         $arr = array();
140         for ($i = $listStart ; $i <= $listEnd ; ++$i) {
141             if ($i > $totalPages) {
142                 break;
143             }
144
145             $arr[$i] = (($i - 1) * $this->limit);
146         }
147
148         return $arr;
149     }
150
151     /**
152      * getBeginning
153      *
154      * Returns the $start for the very first page.
155      *
156      * @access      public
157      * @return      int
158      */
159     public function getBeginning()
160     {
161         return 0;
162     }
163
164     /**
165      * getEnd
166      *
167      * Returns the $start for the very last page.
168      *
169      * @access public
170      * @return int
171      */
172     public function getEnd()
173     {
174         return (($this->totalPages * $this->limit) - $this->limit);
175     }
176 }
177
178 ?>
179
Note: See TracBrowser for help on using the browser.