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