|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
|
6
|
|
|
use Backpack\CRUD\CrudManager; |
|
7
|
|
|
use Backpack\CRUD\app\Library\Widget; |
|
8
|
|
|
|
|
9
|
|
|
class DatatableCache extends SetupCache |
|
10
|
|
|
{ |
|
11
|
|
|
public function __construct() |
|
12
|
|
|
{ |
|
13
|
|
|
$this->cachePrefix = 'datatable_config_'; |
|
14
|
|
|
$this->cacheDuration = 60; // 1 hour |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get the instance of DatatableCache. |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function instance(): self |
|
21
|
|
|
{ |
|
22
|
|
|
static $instance = null; |
|
23
|
|
|
|
|
24
|
|
|
if ($instance === null) { |
|
25
|
|
|
$instance = new static(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return $instance; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Cache setup closure for a datatable component. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $tableId The table ID to use as cache key |
|
35
|
|
|
* @param string $controllerClass The controller class |
|
36
|
|
|
* @param \Closure|null $setup The setup closure |
|
37
|
|
|
* @param string|null $name The element name |
|
38
|
|
|
* @param CrudPanel $crud The CRUD panel instance to update with datatable_id |
|
39
|
|
|
* @return bool Whether the operation was successful |
|
40
|
|
|
*/ |
|
41
|
|
|
public function cacheForComponent(string $tableId, string $controllerClass, ?\Closure $setup = null, ?string $name = null, ?CrudPanel $crud = null): bool |
|
42
|
|
|
{ |
|
43
|
|
|
if (! $setup) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$cruds = CrudManager::getCrudPanels(); |
|
|
|
|
|
|
48
|
|
|
$parentCrud = reset($cruds); |
|
49
|
|
|
|
|
50
|
|
|
if ($parentCrud && $parentCrud->getCurrentEntry()) { |
|
51
|
|
|
$parentEntry = $parentCrud->getCurrentEntry(); |
|
52
|
|
|
$parentController = $parentCrud->controller; |
|
53
|
|
|
|
|
54
|
|
|
// Store in cache |
|
55
|
|
|
$this->store( |
|
56
|
|
|
$tableId, |
|
57
|
|
|
$controllerClass, |
|
58
|
|
|
$parentController, |
|
59
|
|
|
$parentEntry, |
|
60
|
|
|
$name |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
// Set the datatable_id in the CRUD panel if provided |
|
64
|
|
|
if ($crud) { |
|
65
|
|
|
$crud->set('list.datatable_id', $tableId); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Apply cached setup to a CRUD instance using the request's datatable_id. |
|
76
|
|
|
* |
|
77
|
|
|
* @param CrudPanel $crud The CRUD panel instance |
|
78
|
|
|
* @return bool Whether the operation was successful |
|
79
|
|
|
*/ |
|
80
|
|
|
public function applyFromRequest(CrudPanel $crud): bool |
|
81
|
|
|
{ |
|
82
|
|
|
$tableId = request('datatable_id'); |
|
83
|
|
|
|
|
84
|
|
|
if (! $tableId) { |
|
85
|
|
|
\Log::debug('Missing datatable_id in request parameters'); |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->apply($tableId, $crud); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Apply a setup closure to a CrudPanel instance. |
|
94
|
|
|
* |
|
95
|
|
|
* @param CrudPanel $crud The CRUD panel instance |
|
96
|
|
|
* @param string $controllerClass The controller class |
|
97
|
|
|
* @param \Closure $setupClosure The setup closure |
|
98
|
|
|
* @param mixed $entry The entry to pass to the setup closure |
|
99
|
|
|
* @return bool Whether the operation was successful |
|
100
|
|
|
*/ |
|
101
|
|
|
public function applySetupClosure(CrudPanel $crud, string $controllerClass, \Closure $setupClosure, $entry = null): bool |
|
102
|
|
|
{ |
|
103
|
|
|
$originalSetup = $setupClosure; |
|
104
|
|
|
$modifiedSetup = function ($crud, $entry) use ($originalSetup, $controllerClass) { |
|
105
|
|
|
CrudManager::setActiveController($controllerClass); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
// Run the original closure |
|
108
|
|
|
return ($originalSetup)($crud, $entry); |
|
109
|
|
|
}; |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
// Execute the modified closure |
|
113
|
|
|
($modifiedSetup)($crud, $entry); |
|
114
|
|
|
|
|
115
|
|
|
return true; |
|
116
|
|
|
} finally { |
|
117
|
|
|
// Clean up |
|
118
|
|
|
CrudManager::unsetActiveController(); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Prepare datatable data for storage in the cache. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $controllerClass The controller class |
|
126
|
|
|
* @param string $parentController The parent controller |
|
127
|
|
|
* @param mixed $parentEntry The parent entry |
|
128
|
|
|
* @param string|null $elementName The element name |
|
129
|
|
|
* @return array The data to be cached |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function prepareDataForStorage(...$args): array |
|
132
|
|
|
{ |
|
133
|
|
|
[$controllerClass, $parentController, $parentEntry, $elementName] = $args; |
|
134
|
|
|
|
|
135
|
|
|
return [ |
|
136
|
|
|
'controller' => $controllerClass, |
|
137
|
|
|
'parentController' => $parentController, |
|
138
|
|
|
'parent_entry' => $parentEntry, |
|
139
|
|
|
'element_name' => $elementName, |
|
140
|
|
|
'operations' => CrudManager::getInitializedOperations($parentController), |
|
|
|
|
|
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Apply data from the cache to configure a datatable. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $cachedData The cached data |
|
148
|
|
|
* @param CrudPanel $crud The CRUD panel instance |
|
149
|
|
|
* @return bool Whether the operation was successful |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function applyFromCache($cachedData, ...$args): bool |
|
152
|
|
|
{ |
|
153
|
|
|
[$crud] = $args; |
|
154
|
|
|
|
|
155
|
|
|
try { |
|
156
|
|
|
// Initialize operations for the parent controller |
|
157
|
|
|
$this->initializeOperations($cachedData['parentController'], $cachedData['operations']); |
|
158
|
|
|
$entry = $cachedData['parent_entry']; |
|
159
|
|
|
$elementName = $cachedData['element_name']; |
|
160
|
|
|
|
|
161
|
|
|
$widgets = Widget::collection(); |
|
162
|
|
|
$found = false; |
|
163
|
|
|
|
|
164
|
|
|
foreach ($widgets as $widget) { |
|
165
|
|
|
if ($widget['type'] === 'datatable' && |
|
166
|
|
|
(isset($widget['name']) && $widget['name'] === $elementName) && |
|
167
|
|
|
(isset($widget['setup']) && $widget['setup'] instanceof \Closure)) { |
|
168
|
|
|
|
|
169
|
|
|
$this->applySetupClosure($crud, $cachedData['controller'], $widget['setup'], $entry); |
|
170
|
|
|
$found = true; |
|
171
|
|
|
break; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $found; |
|
176
|
|
|
} catch (\Exception $e) { |
|
177
|
|
|
\Log::error('Error applying cached datatable config: ' . $e->getMessage(), [ |
|
178
|
|
|
'exception' => $e, |
|
179
|
|
|
]); |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Initialize operations for a parent controller. |
|
186
|
|
|
*/ |
|
187
|
|
|
private function initializeOperations(string $parentController, $operations): void |
|
188
|
|
|
{ |
|
189
|
|
|
$parentCrud = CrudManager::setupCrudPanel($parentController); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
foreach ($operations as $operation) { |
|
192
|
|
|
$parentCrud->initialized = false; |
|
193
|
|
|
CrudManager::setupCrudPanel($parentController, $operation); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |