1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Datatable; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
6
|
|
|
use Backpack\CRUD\app\Library\Widget; |
7
|
|
|
use Backpack\CRUD\CrudManager; |
8
|
|
|
use Illuminate\Support\Facades\Cache; |
9
|
|
|
use Illuminate\View\Component; |
10
|
|
|
|
11
|
|
|
class Datatable extends Component |
12
|
|
|
{ |
13
|
|
|
protected string $tableId; |
14
|
|
|
|
15
|
|
|
public function __construct( |
16
|
|
|
private string $controller, |
17
|
|
|
private ?CrudPanel $crud = null, |
18
|
|
|
private bool $modifiesUrl = false, |
19
|
|
|
private ?\Closure $setup = null, |
20
|
|
|
private ?string $name = null, |
21
|
|
|
) { |
22
|
|
|
// Set active controller for proper context |
23
|
|
|
CrudManager::setActiveController($controller); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$this->crud ??= CrudManager::setupCrudPanel($controller, 'list'); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$this->tableId = $this->generateTableId(); |
28
|
|
|
|
29
|
|
|
if ($this->setup) { |
30
|
|
|
// Apply the configuration using the shared method |
31
|
|
|
$this->applySetupClosure($this->crud, $this->controller, $this->setup, $this->getParentCrudEntry()); |
|
|
|
|
32
|
|
|
$this->cacheSetupClosure(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (! $this->crud->has('list.datatablesUrl')) { |
|
|
|
|
36
|
|
|
$this->crud->set('list.datatablesUrl', $this->crud->getRoute()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Reset the active controller |
40
|
|
|
CrudManager::unsetActiveController(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function applySetupClosure(CrudPanel $crud, string $controllerClass, \Closure $setupClosure, $entry = null) |
44
|
|
|
{ |
45
|
|
|
$originalSetup = $setupClosure; |
46
|
|
|
$modifiedSetup = function($crud, $entry) use ($originalSetup, $controllerClass) { |
47
|
|
|
|
48
|
|
|
CrudManager::setActiveController($controllerClass); |
49
|
|
|
// Run the original closure |
50
|
|
|
return ($originalSetup)($crud, $entry); |
51
|
|
|
}; |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
// Execute the modified closure |
55
|
|
|
($modifiedSetup)($crud, $entry); |
56
|
|
|
return true; |
57
|
|
|
} finally { |
58
|
|
|
// Clean up |
59
|
|
|
CrudManager::unsetActiveController(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getParentCrudEntry() |
64
|
|
|
{ |
65
|
|
|
$cruds = CrudManager::getCrudPanels(); |
|
|
|
|
66
|
|
|
$parentCrud = reset($cruds); |
67
|
|
|
|
68
|
|
|
if ($parentCrud && $parentCrud->getCurrentEntry()) { |
69
|
|
|
CrudManager::storeInitializedOperation( |
|
|
|
|
70
|
|
|
$parentCrud->controller, |
71
|
|
|
$parentCrud->getCurrentOperation() |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return $parentCrud->getCurrentEntry(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function generateTableId(): string |
81
|
|
|
{ |
82
|
|
|
$controllerPart = str_replace('\\', '_', $this->controller); |
83
|
|
|
$namePart = $this->name ?? 'default'; |
84
|
|
|
$uniqueId = md5($controllerPart.'_'.$namePart); |
85
|
|
|
|
86
|
|
|
return 'crudTable_'.$uniqueId; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Store the datatable configuration in the cache for later use in Ajax requests. |
91
|
|
|
*/ |
92
|
|
|
private function cacheSetupClosure() |
93
|
|
|
{ |
94
|
|
|
if (! $this->setup) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$controllerClass = $this->controller; |
99
|
|
|
$cruds = CrudManager::getCrudPanels(); |
100
|
|
|
$parentCrud = reset($cruds); |
101
|
|
|
|
102
|
|
|
if ($parentCrud && $parentCrud->getCurrentEntry()) { |
103
|
|
|
$parentEntry = $parentCrud->getCurrentEntry(); |
104
|
|
|
$parentController = $parentCrud->controller; |
105
|
|
|
$cacheKey = 'datatable_config_'.$this->tableId; |
106
|
|
|
|
107
|
|
|
Cache::forget($cacheKey); |
108
|
|
|
|
109
|
|
|
// Store the controller class, parent entry, element type and name |
110
|
|
|
Cache::put($cacheKey, [ |
111
|
|
|
'controller' => $controllerClass, |
112
|
|
|
'parentController' => $parentController, |
113
|
|
|
'parent_entry' => $parentEntry, |
114
|
|
|
'element_name' => $this->name, |
115
|
|
|
'operations' => CrudManager::getInitializedOperations($parentController), |
|
|
|
|
116
|
|
|
], now()->addHours(1)); |
117
|
|
|
|
118
|
|
|
$this->crud->set('list.datatable_id', $this->tableId); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function applyCachedSetupClosure($crud) |
123
|
|
|
{ |
124
|
|
|
$tableId = request('datatable_id'); |
125
|
|
|
|
126
|
|
|
if (! $tableId) { |
127
|
|
|
\Log::debug('Missing datatable_id in request parameters'); |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$cacheKey = 'datatable_config_'.$tableId; |
133
|
|
|
$cachedData = Cache::get($cacheKey); |
134
|
|
|
|
135
|
|
|
if (! $cachedData) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
// Get the parent crud instance |
141
|
|
|
self::initializeOperations($cachedData['parentController'], $cachedData['operations']); |
142
|
|
|
$entry = $cachedData['parent_entry']; |
143
|
|
|
$elementName = $cachedData['element_name']; |
144
|
|
|
|
145
|
|
|
$widgets = Widget::collection(); |
146
|
|
|
|
147
|
|
|
foreach ($widgets as $widget) { |
148
|
|
|
if ($widget['type'] === 'datatable' && |
149
|
|
|
(isset($widget['name']) && $widget['name'] === $elementName) && |
150
|
|
|
(isset($widget['setup']) && $widget['setup'] instanceof \Closure)) { |
151
|
|
|
$instance = new self($cachedData['controller']); |
152
|
|
|
|
153
|
|
|
$instance->applySetupClosure($crud, $cachedData['controller'], $widget['setup'], $entry); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} catch (\Exception $e) { |
159
|
|
|
\Log::error('Error applying cached datatable config: '.$e->getMessage(), [ |
160
|
|
|
'exception' => $e, |
161
|
|
|
]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private static function initializeOperations(string $parentController, $operations) |
168
|
|
|
{ |
169
|
|
|
$parentCrud = CrudManager::setupCrudPanel($parentController); |
170
|
|
|
|
171
|
|
|
foreach ($operations as $operation) { |
172
|
|
|
$parentCrud->initialized = false; |
173
|
|
|
CrudManager::setupCrudPanel($parentController, $operation); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function render() |
178
|
|
|
{ |
179
|
|
|
return view('crud::datatable.datatable', [ |
180
|
|
|
'crud' => $this->crud, |
181
|
|
|
'modifiesUrl' => $this->modifiesUrl, |
182
|
|
|
'tableId' => $this->tableId, |
183
|
|
|
]); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|