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