1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\GridBundle\Sort; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\GridBundle\Filter\FilterManagerInterface; |
15
|
|
|
use Lug\Component\Grid\Model\ColumnInterface; |
16
|
|
|
use Lug\Component\Grid\Sort\SorterInterface; |
17
|
|
|
use Lug\Component\Grid\Sort\SorterRendererInterface; |
18
|
|
|
use Lug\Component\Grid\View\GridViewInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
20
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author GeLo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class SorterRenderer implements SorterRendererInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var RequestStack |
29
|
|
|
*/ |
30
|
|
|
private $requestStack; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FilterManagerInterface |
34
|
|
|
*/ |
35
|
|
|
private $filterManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var UrlGeneratorInterface |
39
|
|
|
*/ |
40
|
|
|
private $urlGenerator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param RequestStack $requestStack |
44
|
|
|
* @param FilterManagerInterface $filterManager |
45
|
|
|
* @param UrlGeneratorInterface $urlGenerator |
46
|
|
|
*/ |
47
|
9 |
|
public function __construct( |
48
|
|
|
RequestStack $requestStack, |
49
|
|
|
FilterManagerInterface $filterManager, |
50
|
|
|
UrlGeneratorInterface $urlGenerator |
51
|
|
|
) { |
52
|
9 |
|
$this->requestStack = $requestStack; |
53
|
9 |
|
$this->filterManager = $filterManager; |
54
|
9 |
|
$this->urlGenerator = $urlGenerator; |
55
|
9 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
8 |
|
public function render(GridViewInterface $grid, ColumnInterface $column, $sorting) |
61
|
|
|
{ |
62
|
8 |
|
$definition = $grid->getDefinition(); |
63
|
8 |
|
$name = $column->getName(); |
64
|
|
|
|
65
|
8 |
|
if (!$definition->hasSort($name)) { |
66
|
1 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
$sort = $sorting === SorterInterface::ASC ? $name : '-'.$name; |
70
|
7 |
|
$routeParameters = []; |
71
|
|
|
|
72
|
7 |
|
if (($request = $this->requestStack->getMasterRequest()) !== null) { |
73
|
6 |
|
$routeParameters = array_merge($request->attributes->get('_route_params', []), $request->query->all()); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
7 |
|
if (!isset($routeParameters['grid']['reset']) |
77
|
7 |
|
&& isset($routeParameters['grid']['sorting']) |
78
|
7 |
|
&& $routeParameters['grid']['sorting'] === $sort) { |
79
|
1 |
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
if ($definition->hasOption('persistent') && $definition->getOption('persistent')) { |
83
|
2 |
|
$filters = $this->filterManager->get($definition); |
84
|
|
|
|
85
|
2 |
|
if (isset($filters['sorting']) && $filters['sorting'] === $sort) { |
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
1 |
|
} |
89
|
|
|
|
90
|
5 |
|
$routeParameters['grid']['sorting'] = $sort; |
91
|
5 |
|
unset($routeParameters['grid']['reset']); |
92
|
|
|
|
93
|
5 |
|
return $this->urlGenerator->generate($definition->getOption('grid_route'), $routeParameters); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|