forked from silvershop/silverstripe-listsorter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListSorterOption.php
More file actions
107 lines (86 loc) · 2.17 KB
/
ListSorterOption.php
File metadata and controls
107 lines (86 loc) · 2.17 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
<?php
declare(strict_types=1);
namespace SilverShop\ListSorter;
use SilverStripe\Model\ModelData;
use SilverStripe\Control\HTTP;
/**
* Encapsulate sort option title, sorting SQL,
* GET parameter key, and reverse option.
*/
class ListSorterOption extends ModelData
{
protected $title;
protected $id;
protected $sortSet;
protected $reverseOption;
public function __construct($title, $sortset, ListSorterOption $reverseOption = null)
{
$this->title = $title;
$this->setID($title);
$this->sortSet = $sortset;
if ($reverseOption instanceof \SilverShop\ListSorter\ListSorterOption) {
$this->setReverseOption($reverseOption);
}
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title): static
{
$this->title = $title;
return $this;
}
public function getSortSet()
{
return $this->sortSet;
}
public function setReverseOption(ListSorterOption $option): static
{
$this->reverseOption = $option;
if (!$option->isReversable()) {
if ($this->getID() === $option->getID()) {
$option->setID($option . "_rev");
}
$option->setReverseOption($this);
}
return $this;
}
public function getReverseOption()
{
return $this->reverseOption;
}
public function isReversable(): bool
{
return (bool)$this->reverseOption;
}
public function setID($id): static
{
$this->id = strtolower(trim($id));
return $this;
}
public function getID()
{
return $this->id;
}
public function __toString(): string
{
return $this->id;
}
public function getLink()
{
return $this->generateLink($this->getID());
}
/**
* Helper for creating sort links
*
* @param $id
* @return string
*/
private function generateLink($id)
{
//TODO: strip "start" pagination parameter,
//as most users won't want to remain on paginated page when sorting
return Http::setGetVar('sort', $id, null, '&');
}
}