|
1
|
|
|
<?php /** MicroPaginationWidget */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Widget; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Mvc\Widget; |
|
6
|
|
|
use Micro\Web\Html\Html; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* PaginationWidget class file. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
12
|
|
|
* @link https://github.com/linpax/microphp-framework |
|
13
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
|
14
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
|
15
|
|
|
* @package Micro |
|
16
|
|
|
* @subpackage Widget |
|
17
|
|
|
* @version 1.0 |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class PaginationWidget extends Widget |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var int $countRows count rows */ |
|
23
|
|
|
public $countRows = 0; |
|
24
|
|
|
/** @var int $currentPage current page */ |
|
25
|
|
|
public $currentPage = 0; |
|
26
|
|
|
/** @var int $limit limit rows per page */ |
|
27
|
|
|
public $limit = 10; |
|
28
|
|
|
/** @var string $url url for pagination links */ |
|
29
|
|
|
public $url = ''; |
|
30
|
|
|
/** @var array $attributes attributes for list */ |
|
31
|
|
|
public $attributes = []; |
|
32
|
|
|
/** @var array $attributesElement attributes for list element */ |
|
33
|
|
|
public $attributesElement = []; |
|
34
|
|
|
/** @var array $attributesLink attributes for links */ |
|
35
|
|
|
public $attributesLink = []; |
|
36
|
|
|
|
|
37
|
|
|
/** @var int $totalPages count pages */ |
|
38
|
|
|
private $totalPages = 0; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Initialize widget |
|
43
|
|
|
* |
|
44
|
|
|
* @access public |
|
45
|
|
|
* |
|
46
|
|
|
* @result void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function init() |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->countRows < 1) { |
|
51
|
|
|
$this->totalPages = 0; |
|
52
|
|
|
$this->currentPage = 0; |
|
53
|
|
|
|
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->limit < 10) { |
|
58
|
|
|
$this->limit = 10; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->totalPages = (int)($this->countRows / $this->limit); |
|
62
|
|
|
|
|
63
|
|
|
if ($this->countRows % $this->limit) { |
|
64
|
|
|
$this->totalPages++; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->totalPages === 0) { |
|
68
|
|
|
$this->totalPages = 1; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($this->currentPage < 0) { |
|
72
|
|
|
$this->currentPage = 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($this->currentPage > $this->totalPages) { |
|
76
|
|
|
$this->currentPage = $this->totalPages; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Running widget |
|
82
|
|
|
* |
|
83
|
|
|
* @access public |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function run() |
|
88
|
|
|
{ |
|
89
|
|
|
$items = []; |
|
90
|
|
|
if ($this->totalPages > 0) { |
|
91
|
|
|
for ($i = 1; $i <= $this->totalPages; $i++) { |
|
92
|
|
|
$items[] = [ |
|
93
|
|
|
'text' => Html::href($i, $this->url.($i - 1), $this->attributesLink), |
|
94
|
|
|
'attr' => array_merge( |
|
95
|
|
|
$this->attributesElement, |
|
96
|
|
|
($i === (int)$this->currentPage + 1 ? ['class' => 'active'] : []) |
|
97
|
|
|
) |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return Html::lists($items, $this->attributes); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|