Test Setup Failed
Push — master ( 9d57f0...d907fb )
by Adam
01:38
created

Pagination::getPage()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 7
nop 3
1
<?php
2
3
/**
4
 * Description of Pagination
5
 *
6
 * @author Adam Binnersley <[email protected]>
7
 */
8
namespace Pager;
9
10
class Pagination{
11
    
12
    public static $current;
13
    protected static $page;
14
    protected static $lastpage;
15
    
16
    /**
17
     * Returns paging buttons for the number of records
18
     * @param int $records The total number of records
19
     * @param string $pageURL The URL of the page you are creating the paging for
20
     * @param int $start The start number for the results
21
     * @param string $additional Any additional get values to include in the URL
22
     * @param int $maxshown The number of records that are shown on each page
23
     * @param int $numpagesshown The number of pagination buttons to display
24
     * @return string Returns the pagination menu
25
     */
26
    public static function paging($records, $pageURL, $start = 0, $additional = '', $maxshown = 50, $numpagesshown = 11){
27
        if($records > $maxshown){
28
            if($start >= 1){self::$current = $start;}else{self::$current = 1;}
29
            self::$lastpage = ceil($records / $maxshown);
30
            
31
            if(!empty($additional)){$additional = 'search='.$additional.'&amp;';}
32
            
33
            self::getPage($records, $maxshown, $numpagesshown);
34
            $paging = '<ul class="pagination">';
35
            if(self::$current != 1){
36
                if(self::$current != 2){$paging.= '<li><a href="'.$pageURL.'?'.$additional.'">&laquo;</a></li>';}
37
                $paging.= '<li><a href="'.$pageURL.'?'.$additional.'page='.($start - 1).'">&lt;</a></li>';
38
            }
39
            while(self::$page <= self::$lastpage){
40
                if(self::$current == self::$page){$curselect = ' class="active"';}else{$curselect = '';}
41
                $paging.= '<li'.$curselect.'><a href="'.$pageURL.'?'.$additional.'page='.self::$page.'">'.self::$page.'</a></li>';
42
                self::$page = (self::$page + 1);
43
            }
44
            if(self::$current != self::$lastpage){
45
                $paging.= '<li><a href="'.$pageURL.'?'.$additional.'page='.($start + 1).'">&gt;</a></li>';
46
                if(self::$current != (self::$lastpage - 1)){$paging.= '<li><a href="'.$pageURL.'?'.$additional.'page='.ceil($records / $maxshown).'">&raquo;</a></li>';}
47
            }
48
            $paging.= '</ul>';
49
            return $paging;
50
        }
51
        return false;
52
    }
53
    
54
    /**
55
     * Gets the current page
56
     * @param int $records The total number of records
57
     * @param int $maxshown The number of records that are shown on each page
58
     * @param int $numpages The number of pagination buttons to display
59
     * return void Nothing is returned
60
     */
61
    protected static function getPage($records, $maxshown, $numpages){
62
        $show = floor($numpages / 2);
63
        if(self::$lastpage > $numpages){
64
            if(self::$current > $show){self::$page = self::$current - $show;}else{self::$page = 1;}
65
66
            if(self::$current < (self::$lastpage - $show)){
67
                self::$lastpage = self::$current + $show;
68
                if(self::$current <= $show){self::$lastpage = self::$current + ($numpages - self::$current);}
69
            }
70
            else{self::$page = self::$current - ($numpages - ((ceil($records / $maxshown) - self::$current)) - 1);}
71
        }
72
        else{self::$page = 1;}
73
    }
74
}
75