forked from silvershop/silverstripe-listsorter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListSorter.php
More file actions
130 lines (110 loc) · 2.96 KB
/
ListSorter.php
File metadata and controls
130 lines (110 loc) · 2.96 KB
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
<?php
declare(strict_types=1);
namespace SilverShop\ListSorter;
use SilverStripe\Model\List\ArrayList;
use SilverStripe\Model\ModelData;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\DataList;
/**
* Control for front-end sorting manipulations
*/
class ListSorter extends ModelData
{
private HTTPRequest $request;
private array $sortOptions = [];
private $current;
public function __construct(HTTPRequest $request, $options = null)
{
$this->request = $request;
if (is_array($options)) {
$this->setSortOptions($options);
}
}
/**
* Replace all sort options with a new array
*
* @param array $options
*/
public function setSortOptions($options): void
{
$this->sortOptions = [];
foreach ($options as $key => $value) {
if (is_numeric($key)) {
$key = $value;
}
if ($value instanceof ListSorterOption) {
$this->addSortOption($value);
} else {
$this->addSortOption(
ListSorterOption::create($key, $value)
);
}
}
}
/**
* Add sort option, and set according to sort request param.
*/
public function addSortOption(ListSorterOption $option): void
{
$this->sortOptions[(string)$option] = $option;
$requestparam = $this->request->getVar('sort');
if ((string)$option === $requestparam) {
$this->current = $option;
}
if ((string)$option->getReverseOption() === $requestparam) {
$this->current = $option->getReverseOption();
}
}
/**
* Current sort option
*
* @return ListSorterOption|null
*/
protected function getCurrentOption()
{
return $this->current;
}
/**
* Set the current sort option
*
* @param $option
*/
public function setCurrentOption(ListSorterOption $option): void
{
$this->current = $option;
}
protected function isCurrent(ListSorterOption $option): bool
{
return $option === $this->getCurrentOption();
}
/**
* Get the available sorting options
*/
public function getSorts()
{
$sorts = ArrayList::create();
foreach ($this->sortOptions as $option) {
if ($this->isCurrent($option)) {
if ($option->isReversable()) {
$option = $option->getReverseOption();
}
$option = $option->customise(['IsCurrent' => true]);
}
$sorts->push($option);
}
return $sorts;
}
/**
* Sort the given data list with the current sort
*
* @param DataList $list
* @return DataList
*/
public function sortList($list)
{
if ($current = $this->getCurrentOption()) {
return $list->sort($current->getSortSet());
}
return $list;
}
}