-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
297 lines (259 loc) · 8.82 KB
/
Plugin.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Sixgweb\ListSaver;
use App;
use Event;
use System\Classes\PluginBase;
use Sixgweb\ListSaver\Models\Settings;
use Sixgweb\ListSaver\Models\Preference;
use Backend\Classes\Controller as BackendController;
/**
* Plugin Information File
*
* @link https://docs.octobercms.com/3.x/extend/system/plugins.html
*/
class Plugin extends PluginBase
{
/**
* Memoized flag to determine if the plugin is disabled for the current controller
*/
protected $isEnabledForPath;
/**
* pluginDetails about this plugin.
*/
public function pluginDetails()
{
return [
'name' => 'ListSaver',
'description' => 'Adds ability to save the current ListController filter and list setup values',
'author' => 'Sixgweb',
'icon' => 'icon-list'
];
}
public function boot()
{
if (App::runningInBackend() && $this->isEnabledForPath()) {
$this->extendFilterScopes();
$this->extendFilterScopesBefore();
$this->extendListControllerConfig();
if (Settings::get('uselist_filename')) {
$this->extendImportExportControllerConfig();
}
}
}
/**
* Register permissions
*
* @return array
*/
public function registerPermissions()
{
return [
'sixgweb.listsaver.access' => [
'tab' => 'List Saver',
'label' => 'Access List Saver'
],
'sixgweb.listsaver.share' => [
'tab' => 'List Saver',
'label' => 'Share Lists (if enabled)'
],
'sixgweb.listsaver.settings' => [
'tab' => 'List Saver',
'label' => 'Manage Settings'
],
];
}
/**
* Register listsaver filter widget
*
* @return array
*/
public function registerFilterWidgets()
{
return [
\Sixgweb\ListSaver\FilterWidgets\ListSaver::class => 'listsaver',
];
}
/**
* Register settings
*
* @return array
*/
public function registerSettings()
{
return [
'settings' => [
'label' => 'ListSaver',
'description' => 'Manage list saver settings.',
'category' => 'ListSaver',
'icon' => 'icon-list',
'class' => \Sixgweb\ListSaver\Models\Settings::class,
'permissions' => ['sixgweb.listsaver.settings'],
]
];
}
/**
* Some controllers may not have a filter value defined but do allow
* list setup. This forces the filter to appear.
*
* @return void
*/
protected function extendListControllerConfig()
{
Event::listen('system.extendConfigFile', function ($path, $config) {
if (strpos($path, 'config_list.yaml')) {
$showSetup = $config['showSetup'] ?? false;
$hasFilter = isset($config['filter']);
if (!$showSetup && !$hasFilter) {
return $config;
}
$config['filter'] = $config['filter'] ?? [];
return $config;
}
});
}
/**
* If exporting and useList is true, then set the export fileName to the
* listsaver name, if it has one.
*
* Notes: If your controller overrides the behaviors export method and you're dynamically
* setting export[useList] in the export override, this will not work. Move the
* useList override to the constructor, after parent::__construct() is called.
*
* @return void
*/
protected function extendImportExportControllerConfig()
{
Event::listen('backend.page.beforeDisplay', function ($controller) {
//Only run on the export action
if ($controller->getAction() == 'export') {
//If importExportController config useList is false, then return
$exportController = $controller->asExtension('ImportExportController');
if (!$exportController->getConfig('export[useList]')) {
return;
}
if (!$listController = $controller->asExtension('ListController')) {
return;
}
//Make the listWidget and set the filterWidget
$listController->makeList();
//Get the filterWidget or return
if (!$filterWidget = $listController->listGetFilterWidget()) {
return;
}
//Get the listSaver scope or return
if (!$listSaver = $filterWidget->getScope('listsaver')) {
return;
}
//Get the scopeValue or return
if (!$val = $filterWidget->getScopeValue($listSaver)) {
return;
}
//Slugify the listsaver value and set the export fileName
$val = is_array($val) ? $val[key($val)] : $val;
$val = str_slug($val);
$config = $exportController->getConfig('export');
$config['fileName'] = $val;
$exportController->setConfig(['export' => $config]);
}
});
}
/**
* This is a workaround to set scopevalues in the session
* before other plugins reference them. We remove the scope
* to allow the filter widget to correctly handle the scope type
* when added via the listcontroller or 3rd party extensions.
*
* @return void
*/
protected function extendFilterScopesBefore()
{
Event::listen('backend.filter.extendScopesBefore', function ($filterWidget) {
//Not applying a listsaver list
if (!post('scopeName') == 'listsaver') {
return;
}
//No listsaver preference id posted
if (!$id = post('list_saver_preference')) {
return;
}
//Preference not found
if (!$preference = Preference::find($id)) {
return;
}
//Loop preference filters, add, push value to session and remove
foreach ($preference->filter as $key => $value) {
$filterWidget->addScopes([
$key => [
'label' => $key,
],
]);
$scope = $filterWidget->getScope($key);
$filterWidget->putScopeValue($scope, $value);
$filterWidget->removeScope($key);
}
});
}
/**
* Dynamically add the listsaver filterwiget to the listcontroller
*
* @return void
*/
protected function extendFilterScopes()
{
Event::listen('backend.filter.extendScopes', function ($filterWidget) {
//Check if is ListController
if (!$filterWidget->getController()->methodExists('listExtendColumns')) {
return;
}
/**
* Add ability to define listSaver actions in list_config.yaml
*
* listSaver:
* actions:
* - index
* - export
* - customAction
*/
$listConfig = $filterWidget->getController()->listGetConfig(null);
$actions = $listConfig->listSaver['actions'] ?? ['index', 'export'];
//Skip if not in defined actions
if (in_array($filterWidget->getController()->getAction(), $actions) === false) {
return;
}
$dependsOn = [];
//Widget depends on all scopes
if ($allScopes = $filterWidget->getScopes()) {
$dependsOn = array_keys($allScopes);
}
$filterWidget->addScopes([
'listsaver' => [
'label' => __('List Saver'),
'type' => 'listsaver',
'dependsOn' => $dependsOn,
'permissions' => ['sixgweb.listsaver.access'],
],
]);
});
}
protected function isEnabledForPath()
{
if (isset($this->isEnabledForPath)) {
return $this->isEnabledForPath;
}
$this->isEnabledForPath = true;
if ($enabled = Settings::get('enabled_paths')) {
$this->isEnabledForPath = false;
foreach ($enabled as $enable) {
$path = $enable['path'] ?? '';
if (isset($path[0]) && $path[0] == '/') {
$path = substr($path, 1);
}
if ($path == \Request::path()) {
$this->isEnabledForPath = true;
break;
}
}
}
return $this->isEnabledForPath;
}
}