Passed
Push — master ( d5c329...413b55 )
by Adam
02:40
created

Pagination   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 21
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 108
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A paging() 0 18 4
A buildLink() 0 3 2
A buildQueryString() 0 6 2
B getPage() 0 11 5
A preLinks() 0 8 4
A postLinks() 0 8 4
1
<?php
2
namespace Pager;
3
4
class Pagination {
5
    public static $current;
6
    protected $queryString = array();
7
8
    protected static $page;
9
    protected static $pageURL;
10
    protected static $lastpage;
11
    
12
    /**
13
     * Returns paging buttons for the number of records
14
     * @param int $records The total number of records
15
     * @param string $pageURL The URL of the page you are creating the paging for
16
     * @param int $start The start number for the results
17
     * @param array $additional Any additional get values to include in the URL
18
     * @param int $maxshown The number of records that are shown on each page
19
     * @param int $numpagesshown The number of pagination buttons to display
20
     * @param boolean $arrows If you want arrows to display before and after for next and previous set to true (default) else set to false
21
     * @return string Returns the pagination menu
22
     */
23
    public static function paging($records, $pageURL, $start = 0, $additional = array(), $maxshown = 50, $numpagesshown = 11, $arrows = true) {
24
        self::$pageURL = $pageURL;
25
        $this->queryString = $additional;
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
26
        if ($records > $maxshown) {
27
            self::$current = $start >= 1 ? intval($start) : 1;
28
            self::$lastpage = ceil($records / $maxshown);
29
            
30
            self::getPage($records, $maxshown, $numpagesshown);
31
            $paging = '<ul class="pagination">' . $this->preLinks($arrows);
32
  
33
            while (self::$page <= self::$lastpage) {
34
                $paging .= $this->buildLink(self::$page, self::$page, (self::$current == self::$page));
35
                self::$page = (self::$page + 1);
36
            }
37
            return $paging . $this->postLinks($arrows) . '</ul>';
38
        }
39
        return false;
40
    }
41
    
42
    /**
43
     * Build a link item with the given values
44
     * @param string $link This should be any additional items to be included as part of the link
45
     * @param mixed $page This should be the link test on the link normally set as numbers but may be anything like arrows or dots etc
46
     * @param boolean $current If this is the current link item set this as true so the class is added to the link item
47
     * @return string This will return the paging item as a string
48
     */
49
    protected function buildLink($link, $page, $current = false){
50
        return '<li'.($current === true ? ' class="active"' : '').'><a href="'.self::$pageURL.'?'.$this->buildQueryString($link).'">'.$page.'</a></li>';
51
    }
52
    
53
    /**
54
     * Builds the query string to add to the URL
55
     * @param mixed $page If the page variable is set to a number will add the page number to the query string else will not add any additional items
56
     * @return string The complete string will be returned to add to the link item
57
     */
58
    protected function buildQueryString($page){
59
        if(is_numeric($page)){
60
            $this->queryString['page'] = $page;
61
        }
62
        return http_build_query(array_filter($this->queryString));
63
    }
64
    
65
    /**
66
     * Gets the current page
67
     * @param int $records The total number of records
68
     * @param int $maxshown The number of records that are shown on each page
69
     * @param int $numpages The number of pagination buttons to display
70
     * return void Nothing is returned
71
     */
72
    protected static function getPage($records, $maxshown, $numpages) {
73
        $show = floor($numpages / 2);
74
        if (self::$lastpage > $numpages) {
75
            if (self::$current > $show) { self::$page = self::$current - $show; } else { self::$page = 1; }
76
77
            if (self::$current < (self::$lastpage - $show)) {
78
                self::$lastpage = self::$current + $show;
79
                if (self::$current <= $show) { self::$lastpage = self::$current + ($numpages - self::$current); }
80
            } else { self::$page = self::$current - ($numpages - ((ceil($records / $maxshown) - self::$current)) - 1); }
81
        } else { self::$page = 1; }
82
    }
83
    
84
    /**
85
     * Returns the previous arrows as long as arrows is set to true and the page is not the first page
86
     * @param boolean $arrows If you want to display previous arrows set to true else set to false
87
     * @return string Any previous link arrows will be returned as a string
88
     */
89
    protected function preLinks($arrows = true){
90
        $paging = '';
91
        if (self::$current != 1 && $arrows) {
92
            if (self::$current != 2) { $paging .= $this->buildLink('', '&laquo;'); }
93
            $paging .= $this->buildLink((self::$current - 1), '&lt;');
94
        }
95
        return $paging;
96
    }
97
    
98
    /**
99
     * Returns the next arrows as long as arrows is set to true and the page is not the last page
100
     * @param boolean $arrows If you want to display next arrows set to true else set to false
101
     * @return string Any next link arrows will be returned as a string
102
     */
103
    protected function postLinks($arrows = true){
104
        $paging = '';
105
        if (self::$current != self::$lastpage && $arrows) {
106
            $paging .= $this->buildLink((self::$current + 1), '&gt;');
107
            if (self::$current != (self::$lastpage - 1)) { $paging .= $this->buildLink(self::$lastpage, '&raquo;'); }
108
        }
109
        return $paging;
110
    }
111
}
112