|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\App; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Plugin\Response\Pagination\Paginator; |
|
6
|
|
|
|
|
7
|
|
|
trait PageDatabagTrait |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The current page number. |
|
11
|
|
|
* |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
private int $currentPage = 1; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get the pagination databag name. |
|
18
|
|
|
* |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract protected function bagName(): string; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get the pagination databag attribute. |
|
25
|
|
|
* |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract protected function bagAttr(): string; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get the page number. |
|
32
|
|
|
* |
|
33
|
|
|
* @param int $pageNumber |
|
34
|
|
|
* |
|
35
|
|
|
* @return int |
|
36
|
|
|
*/ |
|
37
|
|
|
private function getPageNumber(int $pageNumber): int |
|
38
|
|
|
{ |
|
39
|
|
|
// If no page number is provided, then get the value from the databag. |
|
40
|
|
|
return $pageNumber > 0 ? $pageNumber : |
|
41
|
|
|
(int)$this->bag($this->bagName())->get($this->bagAttr(), 1); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set the page number. |
|
46
|
|
|
* |
|
47
|
|
|
* @param int $currentPage |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
private function setCurrentPage(int $currentPage): void |
|
52
|
|
|
{ |
|
53
|
|
|
// Save the current page in the databag. |
|
54
|
|
|
$this->bag($this->bagName())->set($this->bagAttr(), $currentPage); |
|
55
|
|
|
$this->currentPage = $currentPage; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the paginator for the component. |
|
60
|
|
|
* |
|
61
|
|
|
* @param int $pageNumber |
|
62
|
|
|
* |
|
63
|
|
|
* @return Paginator |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function paginator(int $pageNumber): Paginator |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->cl(Pagination::class) |
|
|
|
|
|
|
68
|
|
|
// Use the js class name as component item identifier. |
|
69
|
|
|
->item($this->rq()->_class()) |
|
|
|
|
|
|
70
|
|
|
->paginator($this->getPageNumber($pageNumber), $this->limit(), $this->count()) |
|
|
|
|
|
|
71
|
|
|
// This callback will receive the final value of the current page number. |
|
72
|
|
|
->page(function(int $currentPage) { |
|
73
|
|
|
$this->setCurrentPage($currentPage); |
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the current page number |
|
79
|
|
|
* |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function currentPage(): int |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->currentPage; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|