This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortableColumns.php
231 lines (200 loc) · 6.8 KB
/
SortableColumns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Component\SortableColumns;
use Doctrine\Common\Collections\ArrayCollection;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
/**
* Class SortableColumns
*
* SortableColumns is a zikula component to help manage data table column headings that can be clicked to sort the data.
* The collection is an ArrayCollection of Zikula\Component\SortableColumns\Column objects.
* Use the ::generateSortableColumns method to create an array of attributes (url, css class) indexed by column name
* which can be used in the generation of table headings/links.
*/
class SortableColumns
{
/**
* @var RouterInterface
*/
private $router;
/**
* The route name string to generate urls for column headers
* @var string
*/
private $routeName;
/**
* A collection of Columns to manage
* @var ArrayCollection
*/
private $columnCollection;
/**
* The default column (if unset, the first column add is used)
* @var Column
*/
private $defaultColumn;
/**
* The column used to sort the data
* @var Column
*/
private $sortColumn;
/**
* The direction to sorted (constant from Column class)
* @var string
*/
private $sortDirection = Column::DIRECTION_ASCENDING;
/**
* The name of the html field that holds the selected orderBy field (default: `sort-field`)
* @var string
*/
private $sortFieldName;
/**
* The name of the html field that holds the selected orderBy direction (default: `sort-direction`)
* @var string
*/
private $directionFieldName;
/**
* Additional url parameters that must be included in the generated urls
* @var array
*/
private $additionalUrlParameters = [];
public function __construct(
RouterInterface $router,
string $routeName,
string $sortFieldName = 'sort-field',
string $directionFieldName = 'sort-direction'
) {
$this->router = $router;
$this->routeName = $routeName;
$this->sortFieldName = $sortFieldName;
$this->directionFieldName = $directionFieldName;
$this->columnCollection = new ArrayCollection();
}
/**
* Create an array of column definitions indexed by column name
* <code>
* ['a' =>
* ['url' => '/foo?sort-direction=ASC&sort-field=a',
* 'class' => 'z-order-unsorted'
* ],
* ]
* </code>
*/
public function generateSortableColumns(): array
{
$resultArray = [];
/** @var Column $column */
foreach ($this->columnCollection as $column) {
$this->additionalUrlParameters[$this->directionFieldName] = $column->isSortColumn() ? $column->getReverseSortDirection() : $column->getCurrentSortDirection();
$this->additionalUrlParameters[$this->sortFieldName] = $column->getName();
$resultArray[$column->getName()] = [
'url' => $this->router->generate($this->routeName, $this->additionalUrlParameters),
'class' => $column->getCssClassString(),
];
}
return $resultArray;
}
/**
* Add one column.
*/
public function addColumn(Column $column): void
{
$this->columnCollection->set($column->getName(), $column);
}
/**
* Shortcut to add an array of columns.
*/
public function addColumns(array $columns = []): void
{
foreach ($columns as $column) {
if ($column instanceof Column) {
$this->addColumn($column);
} else {
throw new InvalidArgumentException('Columns must be an instance of \Zikula\Component\SortableColumns\Column.');
}
}
}
public function removeColumn(string $name): void
{
$this->columnCollection->remove($name);
}
public function getColumn(?string $name): ?Column
{
return $this->columnCollection->get($name);
}
/**
* Set the column to sort by and the sort direction.
*/
public function setOrderBy(Column $sortColumn = null, string $sortDirection = null): void
{
$sortColumn = $sortColumn ?: $this->getDefaultColumn();
if (null === $sortColumn) {
return;
}
$sortDirection = $sortDirection ?: Column::DIRECTION_ASCENDING;
$this->setSortDirection($sortDirection);
$this->setSortColumn($sortColumn);
}
/**
* Shortcut to set OrderBy using the Request object.
*/
public function setOrderByFromRequest(Request $request): void
{
if (null === $this->getDefaultColumn()) {
return;
}
$sortColumnName = $request->get($this->sortFieldName, $this->getDefaultColumn()->getName());
$sortDirection = $request->get($this->directionFieldName, Column::DIRECTION_ASCENDING);
$this->setOrderBy($this->getColumn($sortColumnName), $sortDirection);
}
public function getSortColumn(): ?Column
{
return $this->sortColumn ?? $this->getDefaultColumn();
}
private function setSortColumn(Column $sortColumn): void
{
if ($this->columnCollection->contains($sortColumn)) {
$this->sortColumn = $sortColumn;
$sortColumn->setSortColumn(true);
$sortColumn->setCurrentSortDirection($this->getSortDirection());
}
}
public function getSortDirection(): string
{
return $this->sortDirection;
}
private function setSortDirection(string $sortDirection): void
{
if (in_array($sortDirection, [Column::DIRECTION_ASCENDING, Column::DIRECTION_DESCENDING], true)) {
$this->sortDirection = $sortDirection;
}
}
public function getDefaultColumn(): ?Column
{
if (!empty($this->defaultColumn)) {
return $this->defaultColumn;
}
return $this->columnCollection->first();
}
public function setDefaultColumn(Column $defaultColumn): void
{
$this->defaultColumn = $defaultColumn;
}
public function getAdditionalUrlParameters(): array
{
return $this->additionalUrlParameters;
}
public function setAdditionalUrlParameters(array $additionalUrlParameters = []): void
{
$this->additionalUrlParameters = $additionalUrlParameters;
}
}