aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/paginator.php
blob: 306756c0e565bee474ff382f46d950726fcea2d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/*
 * PHP Pagination Class
 *
 * @author David Carr - dave@daveismyname.com - http://www.daveismyname.com
 * @version 1.0
 * @date October 20, 2013
 */
class Paginator{

        /**
     * set the number of items per page.
     *
     * @var numeric
    */
    private $_perPage;

    /**
     * set get parameter for fetching the page number
     *
     * @var string
    */
    private $_instance;

    /**
     * sets the page number.
     *
     * @var numeric
    */
    private $_page;

    /**
     * set the limit for the data source
     *
     * @var string
    */
    private $_limit;

    /**
     * set the total number of records/items.
     *
     * @var numeric
    */
    private $_totalRows = 0;



    /**
     *  __construct
     *  
     *  pass values when class is istantiated 
     *  
     * @param numeric  $_perPage  sets the number of iteems per page
     * @param numeric  $_instance sets the instance for the GET parameter
     */
    public function __construct($perPage,$instance){
        $this->_instance = $instance;        
        $this->_perPage = $perPage;
        $this->set_instance();        
    }

    /**
     * get_start
     *
     * creates the starting point for limiting the dataset
     * @return numeric
    */
    private function get_start(){
        return ($this->_page * $this->_perPage) - $this->_perPage;
    }

    /**
     * set_instance
     * 
     * sets the instance parameter, if numeric value is 0 then set to 1
     *
     * @var numeric
    */
    private function set_instance(){
        $this->_page = (int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]); 
        $this->_page = ($this->_page == 0 ? 1 : $this->_page);
    }

    /**
     * set_total
     *
     * collect a numberic value and assigns it to the totalRows
     *
     * @var numeric
    */
    public function set_total($_totalRows){
        $this->_totalRows = $_totalRows;
    }

    /**
     * get_limit
     *
     * returns the limit for the data source, calling the get_start method and passing in the number of items perp page
     * 
     * @return string
    */
    public function get_limit(){
        if (STORAGE == 'postgres') {
            return "LIMIT ".$this->_perPage." OFFSET ".$this->get_start();
        } else {
            return "LIMIT ".$this->get_start().",".$this->_perPage;
        }
    }

        /**
         * page_links
         *
         * create the html links for navigating through the dataset
         * 
         * @var sting $path optionally set the path for the link
         * @var sting $ext optionally pass in extra parameters to the GET
         * @return string returns the html menu
        */
    public function page_links($path='?',$ext=null)
    {
        $adjacents = "2";
        $prev = $this->_page - 1;
        $next = $this->_page + 1;
        $lastpage = ceil($this->_totalRows/$this->_perPage);
        $lpm1 = $lastpage - 1;

        $pagination = "";
        if($lastpage > 1)
        {   
            $pagination .= "<div class='pagination'>";
        if ($this->_page > 1)
            $pagination.= "<a href='".$path."$this->_instance=$prev"."$ext'>« previous</a>";
        else
            $pagination.= "<span class='disabled'>« previous</span>";   

        if ($lastpage < 7 + ($adjacents * 2))
        {   
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
        if ($counter == $this->_page)
            $pagination.= "<span class='current'>$counter</span>";
        else
            $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";                   
        }
        }
        elseif($lastpage > 5 + ($adjacents * 2))
        {
        if($this->_page < 1 + ($adjacents * 2))       
        {
        for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
        {
        if ($counter == $this->_page)
            $pagination.= "<span class='current'>$counter</span>";
        else
            $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";                   
        }
            $pagination.= "...";
            $pagination.= "<a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a>";
            $pagination.= "<a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a>";       
        }
        elseif($lastpage - ($adjacents * 2) > $this->_page && $this->_page > ($adjacents * 2))
        {
            $pagination.= "<a href='".$path."$this->_instance=1"."$ext'>1</a>";
            $pagination.= "<a href='".$path."$this->_instance=2"."$ext'>2</a>";
            $pagination.= "...";
        for ($counter = $this->_page - $adjacents; $counter <= $this->_page + $adjacents; $counter++)
        {
        if ($counter == $this->_page)
            $pagination.= "<span class='current'>$counter</span>";
        else
            $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";                   
        }
            $pagination.= "..";
            $pagination.= "<a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a>";
            $pagination.= "<a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a>";       
        }
        else
        {
            $pagination.= "<a href='".$path."$this->_instance=1"."$ext'>1</a>";
            $pagination.= "<a href='".$path."$this->_instance=2"."$ext'>2</a>";
            $pagination.= "..";
        for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
        {
        if ($counter == $this->_page)
            $pagination.= "<span class='current'>$counter</span>";
        else
            $pagination.= "<a href='".$path."$this->_instance=$counter"."$ext'>$counter</a>";                   
        }
        }
        }

        if ($this->_page < $counter - 1)
            $pagination.= "<a href='".$path."$this->_instance=$next"."$ext'>next »</a>";
        else
            $pagination.= "<span class='disabled'>next »</span>";
            $pagination.= "</div>\n";       
        }


    return $pagination;
    }
}