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 |
31
|
|
|
($this->setup)($this->crud, $this->getParentCrudEntry()); |
32
|
|
|
|
33
|
|
|
// Store the configuration in cache for Ajax requests |
34
|
|
|
$this->cacheSetupClosure(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (! $this->crud->has('list.datatablesUrl')) { |
|
|
|
|
38
|
|
|
$this->crud->set('list.datatablesUrl', $this->crud->getRoute()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Reset the active controller |
42
|
|
|
CrudManager::unsetActiveController($controller); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function getParentCrudEntry() |
46
|
|
|
{ |
47
|
|
|
$cruds = CrudManager::getCrudPanels(); |
|
|
|
|
48
|
|
|
$parentCrud = reset($cruds); |
49
|
|
|
|
50
|
|
|
if ($parentCrud && $parentCrud->getCurrentEntry()) { |
51
|
|
|
CrudManager::storeInitializedOperation( |
|
|
|
|
52
|
|
|
$parentCrud->controller, |
53
|
|
|
$parentCrud->getCurrentOperation() |
54
|
|
|
); |
55
|
|
|
return $parentCrud->getCurrentEntry(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function generateTableId(): string |
62
|
|
|
{ |
63
|
|
|
$controllerPart = str_replace('\\', '_', $this->controller); |
64
|
|
|
$namePart = $this->name ?? 'default'; |
65
|
|
|
$uniqueId = md5($controllerPart.'_'.$namePart); |
66
|
|
|
|
67
|
|
|
return 'crudTable_'.$uniqueId; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Store the datatable configuration in the cache for later use in Ajax requests. |
72
|
|
|
*/ |
73
|
|
|
private function cacheSetupClosure() |
74
|
|
|
{ |
75
|
|
|
if (! $this->setup) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$controllerClass = $this->controller; |
80
|
|
|
$cruds = CrudManager::getCrudPanels(); |
81
|
|
|
$parentCrud = reset($cruds); |
82
|
|
|
|
83
|
|
|
if ($parentCrud && $parentCrud->getCurrentEntry()) { |
84
|
|
|
$parentEntry = $parentCrud->getCurrentEntry(); |
85
|
|
|
$parentController = $parentCrud->controller; |
86
|
|
|
$cacheKey = 'datatable_config_'.$this->tableId; |
87
|
|
|
|
88
|
|
|
Cache::forget($cacheKey); |
89
|
|
|
|
90
|
|
|
// Store the controller class, parent entry, element type and name |
91
|
|
|
Cache::put($cacheKey, [ |
92
|
|
|
'controller' => $controllerClass, |
93
|
|
|
'parentController' => $parentController, |
94
|
|
|
'parent_entry' => $parentEntry, |
95
|
|
|
'element_name' => $this->name, |
96
|
|
|
'operations' => CrudManager::getInitializedOperations($parentController), |
|
|
|
|
97
|
|
|
], now()->addHours(1)); |
98
|
|
|
|
99
|
|
|
$this->crud->set('list.datatable_id', $this->tableId); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public static function applyCachedSetupClosure($crud) |
104
|
|
|
{ |
105
|
|
|
$tableId = request('datatable_id'); |
106
|
|
|
|
107
|
|
|
if (! $tableId) { |
108
|
|
|
\Log::debug('Missing datatable_id in request parameters'); |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$cacheKey = 'datatable_config_'.$tableId; |
114
|
|
|
$cachedData = Cache::get($cacheKey); |
115
|
|
|
|
116
|
|
|
if (! $cachedData) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
try { |
121
|
|
|
// Get the parent crud instance |
122
|
|
|
self::initializeOperations($cachedData['parentController'], $cachedData['operations']); |
123
|
|
|
|
124
|
|
|
$entry = $cachedData['parent_entry']; |
125
|
|
|
$elementName = $cachedData['element_name']; |
126
|
|
|
$widgets = Widget::collection(); |
127
|
|
|
foreach ($widgets as $widget) { |
128
|
|
|
if ($widget['type'] === 'datatable' && |
129
|
|
|
(isset($widget['name']) && $widget['name'] === $elementName) && |
130
|
|
|
(isset($widget['setup']) && $widget['setup'] instanceof \Closure)) { |
131
|
|
|
$widget['setup']($crud, $entry); |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
|
138
|
|
|
} catch (\Exception $e) { |
139
|
|
|
\Log::error('Error applying cached datatable config: '.$e->getMessage(), [ |
140
|
|
|
'exception' => $e, |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private static function initializeOperations(string $parentController, $operations) |
147
|
|
|
{ |
148
|
|
|
$parentCrud = CrudManager::setupCrudPanel($parentController); |
149
|
|
|
|
150
|
|
|
foreach($operations as $operation) { |
151
|
|
|
$parentCrud->initialized = false; |
152
|
|
|
CrudManager::setupCrudPanel($parentController, $operation); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function render() |
157
|
|
|
{ |
158
|
|
|
return view('crud::datatable.datatable', [ |
159
|
|
|
'crud' => $this->crud, |
160
|
|
|
'modifiesUrl' => $this->modifiesUrl, |
161
|
|
|
'tableId' => $this->tableId, |
162
|
|
|
]); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|