Passed
Push — master ( 3b1e15...43df0a )
by Dmytro
04:55
created

UIPagination::correctPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Asymptix\ui\controls;
4
5
/**
6
 * Pagination UI control class.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2009 - 2016, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
class UIPagination extends \Asymptix\ui\UIControl {
16
    /**
17
     * Default dialog panel HTML template.
18
     */
19
    const DEFAULT_TEMPLATE = "core/ui/templates/controls/ui_pagination.tpl.php";
20
21
    public $url = "";
22
23
    public $pagesNumber;
24
    public $firstPage;
25
    public $previousPage;
26
    public $currentPage;
27
    public $nextPage;
28
    public $lastPage;
29
    public $pagesOffset;
30
31
    /**
32
     * Display Pagination control.
33
     *
34
     * @param string $url Basic URL of the pagination links (common part).
35
     * @param int $pagesNumber Total pages number.
36
     * @param int $currentPage Current page number.
37
     * @param int $pagesOffset Number of visible pagination links before and
38
     *           after current page link.
39
     * @param string $template Path to the pagination HTML template.
40
     */
41
    public function __construct($url = "", $pagesNumber = 1, $currentPage = 1, $pagesOffset = 3, $template = "") {
42
        $this->url = $url;
43
44
        $this->pagesNumber = $pagesNumber;
45
        $this->setCurrentPage($currentPage);
46
        $this->pagesOffset = $pagesOffset;
47
48
        if ($this->pagesNumber > 1) {
49
            $this->correctPages();
50
51
            if (empty($template)) {
52
                $template = self::DEFAULT_TEMPLATE;
53
            }
54
            parent::__construct([], $template);
55
        }
56
    }
57
58
    private function correctPages() {
59
        $this->firstPage = $this->currentPage - $this->pagesOffset;
60
        $this->previousPage = $this->currentPage - 1;
61
        $this->nextPage = $this->currentPage + 1;
62
        $this->lastPage = $this->currentPage + $this->pagesOffset;
63
    }
64
65
    public function setCurrentPage($currentPage) {
66
        if (Tools::isInteger($currentPage)) {
67
            if ($currentPage < 0) {
68
                $this->currentPage = 1;
69
            } else {
70
                $this->currentPage = $currentPage;
71
            }
72
        } else {
73
            $this->currentPage = 1;
74
        }
75
    }
76
77
    public static function getCurrentPageNumber($page, $totalPages) {
78
        $page = (int)$page;
79
80
        if ($page < 1) {
81
            return 1;
82
        }
83
84
        if ($page > $totalPages) {
85
            return $totalPages;
86
        }
87
88
        return $page;
89
    }
90
91
}
92