1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PaginatorPlugin.php - The Jaxon Paginator plugin |
5
|
|
|
* |
6
|
|
|
* @package jaxon-core |
|
|
|
|
7
|
|
|
* @copyright 2024 Thierry Feuzeu |
8
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
9
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Jaxon\Plugin\Response\Pagination; |
13
|
|
|
|
14
|
|
|
use Jaxon\App\View\ViewRenderer; |
15
|
|
|
use Jaxon\App\View\Store; |
16
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
17
|
|
|
use Jaxon\Request\Call\Call; |
18
|
|
|
use Jaxon\Request\Call\Parameter; |
19
|
|
|
|
20
|
|
|
use function array_map; |
21
|
|
|
use function array_pop; |
22
|
|
|
use function array_shift; |
23
|
|
|
use function count; |
24
|
|
|
use function trim; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Usage |
28
|
|
|
* |
29
|
|
|
* Step 1: Render a template containing a wrapper for the pagination. |
30
|
|
|
* |
31
|
|
|
* $html = $this->render($pageTemplate, [ |
32
|
|
|
* // ... |
33
|
|
|
* ]); |
34
|
|
|
* |
35
|
|
|
* Step 2: Create a paginator and render the pagination into the wrapper. |
36
|
|
|
* |
37
|
|
|
* $this->response->pg->paginator($pageNumber, $perPage, $total) |
38
|
|
|
* ->paginate($this->rq()->page(), $wrapperId); |
39
|
|
|
* // Or, using the response shortcut |
40
|
|
|
* $this->response->paginator($pageNumber, $perPage, $total) |
41
|
|
|
* ->paginate($this->rq()->page(), $wrapperId); |
42
|
|
|
* // Or, in a class that inherits from CallableClass |
43
|
|
|
* $this->paginator($pageNumber, $perPage, $total) |
44
|
|
|
* ->paginate($this->rq()->page(), $wrapperId); |
45
|
|
|
*/ |
|
|
|
|
46
|
|
|
class PaginatorPlugin extends ResponsePlugin |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @const The plugin name |
50
|
|
|
*/ |
51
|
|
|
const NAME = 'pg'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var ViewRenderer |
55
|
|
|
*/ |
56
|
|
|
protected $xRenderer; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The constructor. |
60
|
|
|
* |
61
|
|
|
* @param ViewRenderer $xRenderer |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function __construct(ViewRenderer $xRenderer) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->xRenderer = $xRenderer; |
66
|
|
|
} |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
|
|
|
|
71
|
|
|
public function getName(): string |
72
|
|
|
{ |
73
|
|
|
return self::NAME; |
74
|
|
|
} |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
|
|
|
|
79
|
|
|
public function getHash(): string |
80
|
|
|
{ |
81
|
|
|
// Use the version number as hash |
82
|
|
|
return '5.0.0'; |
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array<Page> $aPages |
|
|
|
|
87
|
|
|
* |
88
|
|
|
* @return null|Store |
89
|
|
|
*/ |
90
|
|
|
private function _render(array $aPages): ?Store |
91
|
|
|
{ |
92
|
|
|
$aPages = array_map(function($xPage) { |
|
|
|
|
93
|
|
|
return $this->xRenderer->render('pagination::links/' . $xPage->sType, [ |
94
|
|
|
'page' => $xPage->nNumber, |
95
|
|
|
'text' => $xPage->sText, |
96
|
|
|
]); |
97
|
|
|
}, $aPages); |
98
|
|
|
$aPrevPage = array_shift($aPages); // The first entry in the array |
99
|
|
|
$aNextPage = array_pop($aPages); // The last entry in the array |
100
|
|
|
|
101
|
|
|
return $this->xRenderer->render('pagination::wrapper', [ |
102
|
|
|
'links' => $aPages, |
103
|
|
|
'prev' => $aPrevPage, |
104
|
|
|
'next' => $aNextPage, |
105
|
|
|
]); |
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Create a paginator |
110
|
|
|
* |
111
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
112
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
113
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
114
|
|
|
* |
115
|
|
|
* @return Paginator |
116
|
|
|
*/ |
117
|
|
|
public function paginator(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator |
118
|
|
|
{ |
119
|
|
|
return new Paginator($this, $nCurrentPage, $nItemsPerPage, $nTotalItems); |
120
|
|
|
} |
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Render an HTML pagination control. |
124
|
|
|
* |
125
|
|
|
* @param Paginator $xPaginator |
|
|
|
|
126
|
|
|
* @param Call $xCall |
|
|
|
|
127
|
|
|
* @param string $sWrapperId |
|
|
|
|
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function render(Paginator $xPaginator, Call $xCall, string $sWrapperId) |
132
|
|
|
{ |
133
|
|
|
$aPages = $xPaginator->pages(); |
134
|
|
|
if(count($aPages) === 0) |
135
|
|
|
{ |
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
$xStore = $this->_render($aPages); |
139
|
|
|
if(!$xStore) |
140
|
|
|
{ |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Append the page number to the parameter list, if not yet given. |
145
|
|
|
if(!$xCall->hasPageNumber()) |
146
|
|
|
{ |
147
|
|
|
$xCall->addParameter(Parameter::PAGE_NUMBER, 0); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Show the pagination links |
151
|
|
|
$sWrapperId = trim($sWrapperId); |
152
|
|
|
$this->response()->html($sWrapperId, $xStore->__toString()); |
153
|
|
|
// Set click handlers on the pagination links |
154
|
|
|
$this->addCommand('pg.paginate', [ |
155
|
|
|
'id' => $sWrapperId, |
156
|
|
|
'func' => $xCall->toArray(), |
157
|
|
|
]); |
158
|
|
|
} |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|