1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GpsLab component. |
5
|
|
|
* |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GpsLab\Bundle\PaginationBundle\Twig\Extension; |
12
|
|
|
|
13
|
|
|
use GpsLab\Bundle\PaginationBundle\Service\Configuration; |
14
|
|
|
use Twig\Environment; |
15
|
|
|
use Twig\Extension\AbstractExtension; |
16
|
|
|
use Twig\TwigFunction; |
17
|
|
|
|
18
|
|
|
class PaginationExtension extends AbstractExtension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $template; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $template |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct($template) |
29
|
|
|
{ |
30
|
6 |
|
$this->template = $template; |
31
|
6 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
1 |
|
public function getFunctions() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
1 |
|
new TwigFunction( |
40
|
1 |
|
'pagination_render', |
41
|
1 |
|
[$this, 'renderPagination'], |
42
|
1 |
|
['is_safe' => ['html'], 'needs_environment' => true] |
43
|
|
|
), |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param \Twig\Environment $env |
49
|
|
|
* @param Configuration $pagination |
50
|
|
|
* @param string $template |
51
|
|
|
* @param array $view_params |
52
|
|
|
* @param int $max_navigate |
53
|
|
|
* |
54
|
|
|
* @throws \Twig\Error\LoaderError |
55
|
|
|
* @throws \Twig\Error\RuntimeError |
56
|
|
|
* @throws \Twig\Error\SyntaxError |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
4 |
|
public function renderPagination( |
61
|
|
|
Environment $env, |
62
|
|
|
Configuration $pagination, |
63
|
|
|
$template = null, |
64
|
|
|
array $view_params = [], |
65
|
|
|
$max_navigate = 0 |
66
|
|
|
) { |
67
|
4 |
|
if ($max_navigate > 0) { |
68
|
|
|
// not change original object |
69
|
1 |
|
$new_pagination = clone $pagination; |
70
|
1 |
|
$new_pagination->setMaxNavigate($max_navigate); |
71
|
|
|
|
72
|
1 |
|
$pagination_view = $new_pagination->getView(); |
73
|
|
|
} else { |
74
|
3 |
|
$pagination_view = $pagination->getView(); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
return $env->render( |
78
|
4 |
|
$template ?: $this->template, |
79
|
4 |
|
array_merge($view_params, ['pagination' => $pagination_view]) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
1 |
|
public function getName() |
87
|
|
|
{ |
88
|
1 |
|
return 'gpslab_pagination_extension'; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|