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\Plugin\AbstractResponsePlugin; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Usage |
19
|
|
|
* |
20
|
|
|
* Step 1: Render a template containing a wrapper for the pagination. |
21
|
|
|
* |
22
|
|
|
* $html = $this->render($pageTemplate, [ |
23
|
|
|
* // ... |
24
|
|
|
* ]); |
25
|
|
|
* |
26
|
|
|
* Step 2: Create a paginator and render the pagination into the wrapper. |
27
|
|
|
* |
28
|
|
|
* $this->response->pg->paginator($pageNumber, $perPage, $total) |
29
|
|
|
* ->render($this->rq()->page(), $wrapperId); |
30
|
|
|
* |
31
|
|
|
* // Or, using the response shortcut |
32
|
|
|
* $this->response->paginator($pageNumber, $perPage, $total) |
33
|
|
|
* ->render($this->rq()->page(), $wrapperId); |
34
|
|
|
* |
35
|
|
|
* // In a class that inherits from CallableClass |
36
|
|
|
* $this->paginator($pageNumber, $perPage, $total) |
37
|
|
|
* ->render($this->rq()->page(), $wrapperId); |
38
|
|
|
* |
39
|
|
|
* // In a class that inherits from Component (no need for a wrapper id) |
40
|
|
|
* $this->paginator($pageNumber, $perPage, $total) |
41
|
|
|
* ->render($this->rq()->page()); |
42
|
|
|
*/ |
43
|
|
|
class PaginatorPlugin extends AbstractResponsePlugin |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @const The plugin name |
47
|
|
|
*/ |
48
|
|
|
const NAME = 'pg'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ViewRenderer |
52
|
|
|
*/ |
53
|
|
|
protected $xRenderer; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The constructor. |
57
|
|
|
* |
58
|
|
|
* @param ViewRenderer $xRenderer |
59
|
|
|
*/ |
60
|
|
|
public function __construct(ViewRenderer $xRenderer) |
61
|
|
|
{ |
62
|
|
|
$this->xRenderer = $xRenderer; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function getName(): string |
69
|
|
|
{ |
70
|
|
|
return self::NAME; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function getHash(): string |
77
|
|
|
{ |
78
|
|
|
// Use the version number as hash |
79
|
|
|
return '5.0.0'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function getCss(): string |
86
|
|
|
{ |
87
|
|
|
return ' |
88
|
|
|
<style> |
89
|
|
|
.pagination li a { |
90
|
|
|
cursor: pointer; |
91
|
|
|
} |
92
|
|
|
</style> |
93
|
|
|
'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the view renderer |
98
|
|
|
* |
99
|
|
|
* @return ViewRenderer |
100
|
|
|
*/ |
101
|
|
|
public function renderer(): ViewRenderer |
102
|
|
|
{ |
103
|
|
|
return $this->xRenderer; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create a paginator |
108
|
|
|
* |
109
|
|
|
* @param int $nPageNumber The current page number |
110
|
|
|
* @param int $nItemsPerPage The number of items per page |
111
|
|
|
* @param int $nTotalItems The total number of items |
112
|
|
|
* |
113
|
|
|
* @return Paginator |
114
|
|
|
*/ |
115
|
|
|
public function paginator(int $nPageNumber, int $nItemsPerPage, int $nTotalItems): Paginator |
116
|
|
|
{ |
117
|
|
|
return new Paginator($this, $nPageNumber, $nItemsPerPage, $nTotalItems); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|