|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* A GridField component that allows to temporary disable the |
|
5
|
|
|
* pagination. |
|
6
|
|
|
* |
|
7
|
|
|
* It should be included before the GridFieldPaginator component that |
|
8
|
|
|
* must be present (otherwise this class would be pretty useless). |
|
9
|
|
|
* |
|
10
|
|
|
* @package silverstripe-togglepaginator |
|
11
|
|
|
*/ |
|
12
|
|
|
class GridFieldTogglePaginator implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The icon to use on the enable pagination button. Can be null. |
|
16
|
|
|
* @config |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $enable_icon; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The icon to use on the disable pagination button. Can be null. |
|
22
|
|
|
* @config |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $disable_icon; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Fragment to write the button to. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $target; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* ID of the session data. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $state_id; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Session data (an associative array). |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $state; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
public function __construct($target = 'buttons-before-right') |
|
43
|
|
|
{ |
|
44
|
|
|
$this->target = $target; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Update $state and $state_id properties. |
|
49
|
|
|
* |
|
50
|
|
|
* If the $state array does not exist in the session, create it. |
|
51
|
|
|
* |
|
52
|
|
|
* The current implementation is borrowed directly from |
|
53
|
|
|
* GridField_FormAction::getAttributes() 3.2.0-beta2. |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function updateState($grid) |
|
56
|
|
|
{ |
|
57
|
|
|
if (isset($this->state_id)) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$state = array( |
|
62
|
|
|
'grid' => $grid->getName(), |
|
63
|
|
|
'actionName' => 'toggle', |
|
64
|
|
|
'active' => true, |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
// Ensure the id doesn't contain only numeric characters |
|
68
|
|
|
$this->state_id = 'gf_' . substr(md5(serialize($state)), 0, 8); |
|
69
|
|
|
|
|
70
|
|
|
$this->state = Session::get($this->state_id); |
|
71
|
|
|
if (! is_array($this->state) || ! array_key_exists('active', $this->state)) { |
|
72
|
|
|
$this->state = $state; |
|
73
|
|
|
Session::set($this->state_id, $state); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Implements the GridField_HTMLProvider interface. |
|
79
|
|
|
* |
|
80
|
|
|
* @param GridField $grid |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getHTMLFragments($grid) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->updateState($grid); |
|
86
|
|
|
|
|
87
|
|
|
$active = $this->state['active']; |
|
88
|
|
|
$params = http_build_query(array('StateID' => $this->state_id)); |
|
89
|
|
|
$data = new ArrayData(array( |
|
90
|
|
|
'state' => $active ? 'ui-state-default' : 'ss-ui-alternate ui-state-highlight', |
|
91
|
|
|
'icon' => Config::inst()->get(__CLASS__, $active ? 'enable_icon' : 'disable_icon'), |
|
92
|
|
|
'label' => $active ? _t(__CLASS__ . '.DISABLE', 'Disable') : _t(__CLASS__ . '.ENABLE', 'Enable'), |
|
93
|
|
|
'name' => 'action_gridFieldAlterAction?' . $params, |
|
94
|
|
|
'url' => $grid->Link(), |
|
95
|
|
|
)); |
|
96
|
|
|
|
|
97
|
|
|
return array( |
|
98
|
|
|
$this->target => $data->renderWith(__CLASS__), |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Required by the GridField_ActionProvider interface. |
|
104
|
|
|
* |
|
105
|
|
|
* @param GridField $grid |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getActions($grid) |
|
109
|
|
|
{ |
|
110
|
|
|
return array('toggle'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Handle the action. |
|
115
|
|
|
* |
|
116
|
|
|
* It swithces the "state" flag and saves the data into a session |
|
117
|
|
|
* variable for later reuse. |
|
118
|
|
|
* |
|
119
|
|
|
* @param GridField $grid The subject GridField instance |
|
120
|
|
|
* @param string $action The action name (lowercase!) |
|
121
|
|
|
* @param mixed $arguments Optional argument(s) for the action |
|
122
|
|
|
* @param array $data Form data, if relevant |
|
123
|
|
|
* |
|
124
|
|
|
* @throws InvalidArgumentException |
|
125
|
|
|
*/ |
|
126
|
|
|
public function handleAction(GridField $grid, $action, $arguments, $data) |
|
127
|
|
|
{ |
|
128
|
|
|
switch ($action) { |
|
129
|
|
|
|
|
130
|
|
|
case 'toggle': |
|
131
|
|
|
$this->updateState($grid); |
|
132
|
|
|
$this->state['active'] = ! $this->state['active']; |
|
133
|
|
|
Session::set($this->state_id, $this->state); |
|
|
|
|
|
|
134
|
|
|
break; |
|
135
|
|
|
|
|
136
|
|
|
default: |
|
137
|
|
|
throw new InvalidArgumentException(sprintf( |
|
138
|
|
|
'Action "%s" not understood', |
|
139
|
|
|
$action |
|
140
|
|
|
)); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Required by the GridField_DataManipulator interface. |
|
146
|
|
|
* |
|
147
|
|
|
* Disable pagination on the GridFieldPaginator component, if |
|
148
|
|
|
* required by the current "state" flag. It must be called before |
|
149
|
|
|
* GridFieldPaginator::getManipulatedData() to take effect, hence |
|
150
|
|
|
* the requirement that GridFieldTogglePaginator must be added |
|
151
|
|
|
* before GridFieldPaginator. |
|
152
|
|
|
* |
|
153
|
|
|
* @param GridField $grid |
|
154
|
|
|
* @return SS_List |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getManipulatedData(GridField $grid, SS_List $list) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->updateState($grid); |
|
159
|
|
|
if (! $this->state['active']) { |
|
160
|
|
|
$grid->getConfig()->getComponentByType('GridFieldPaginator') |
|
161
|
|
|
->setItemsPerPage(PHP_INT_MAX); |
|
162
|
|
|
} |
|
163
|
|
|
return $list; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.