1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Renderer.php - Paginator renderer |
5
|
|
|
* |
6
|
|
|
* Render pagination links. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Jaxon\Utils\Pagination; |
16
|
|
|
|
17
|
|
|
use Jaxon\Utils\Template\Template; |
18
|
|
|
|
19
|
|
|
class Renderer |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Set the paginator to be rendered. |
23
|
|
|
* |
24
|
|
|
* @var Paginator |
25
|
|
|
*/ |
26
|
|
|
protected $xPaginator = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The template manager. |
30
|
|
|
* |
31
|
|
|
* Will be used to render HTML code for links. |
32
|
|
|
* |
33
|
|
|
* @var Template |
34
|
|
|
*/ |
35
|
|
|
protected $xTemplate = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The class contructor |
39
|
|
|
* |
40
|
|
|
* @param Template $xTemplate |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Template $xTemplate) |
43
|
|
|
{ |
44
|
|
|
$this->xTemplate = $xTemplate; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the paginator to be rendered. |
49
|
|
|
* |
50
|
|
|
* @param Paginator $xPaginator The paginator to be rendered |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function setPaginator(\Jaxon\Utils\Pagination\Paginator $xPaginator) |
55
|
|
|
{ |
56
|
|
|
$this->xPaginator = $xPaginator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Render the previous link. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
protected function getPrevLink() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if(!($sCall = $this->xPaginator->getPrevCall())) |
67
|
|
|
{ |
68
|
|
|
return ''; |
69
|
|
|
} |
70
|
|
|
return $this->xTemplate->render('pagination::links/prev', |
71
|
|
|
['call' => $sCall, 'text' => $this->xPaginator->getPreviousText()]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Render the next link. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
protected function getNextLink() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if(!($sCall = $this->xPaginator->getNextCall())) |
82
|
|
|
{ |
83
|
|
|
return ''; |
84
|
|
|
} |
85
|
|
|
return $this->xTemplate->render('pagination::links/next', |
86
|
|
|
['call' => $sCall, 'text' => $this->xPaginator->getNextText()]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Render the pagination links. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function getLinks() |
95
|
|
|
{ |
96
|
|
|
$sLinks = ''; |
97
|
|
|
foreach($this->xPaginator->getPages() as $page) |
98
|
|
|
{ |
99
|
|
|
if($page['call']) |
100
|
|
|
{ |
101
|
|
|
$sTemplate = ($page['isCurrent'] ? 'pagination::links/current' : 'pagination::links/enabled'); |
102
|
|
|
$sLinks .= $this->xTemplate->render($sTemplate, ['call' => $page['call'], 'text' => $page['num']]); |
103
|
|
|
} |
104
|
|
|
else |
105
|
|
|
{ |
106
|
|
|
$sLinks .= $this->xTemplate->render('pagination::links/disabled', ['text' => $page['num']]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
return $sLinks; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Render an HTML pagination control. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function render() |
118
|
|
|
{ |
119
|
|
|
return $this->xTemplate->render('pagination::wrapper', [ |
120
|
|
|
'links' => $this->getLinks(), |
121
|
|
|
'prev' => $this->getPrevLink(), |
122
|
|
|
'next' => $this->getNextLink(), |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.