Completed
Push — master ( 417a50...a61c99 )
by Dmytro
07:12
created

UIPagination::show()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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