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 $updatesUrl = true, |
19
|
|
|
private ?\Closure $configure = null, |
20
|
|
|
private ?string $type = null, |
21
|
|
|
private ?string $name = null |
22
|
|
|
) { |
23
|
|
|
// Set active controller for proper context |
24
|
|
|
CrudManager::setActiveController($controller); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$this->crud ??= CrudManager::crudFromController($controller, 'list'); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$this->tableId = $this->generateTableId(); |
29
|
|
|
|
30
|
|
|
if ($this->configure) { |
31
|
|
|
// Apply the configuration |
32
|
|
|
($this->configure)($this->crud, null); |
33
|
|
|
|
34
|
|
|
// Store the configuration in cache for Ajax requests |
35
|
|
|
$this->storeDatatableConfig(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (! $this->crud->has('list.datatablesUrl')) { |
|
|
|
|
39
|
|
|
$this->crud->set('list.datatablesUrl', $this->crud->getRoute()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Reset the active controller |
43
|
|
|
CrudManager::unsetActiveController($controller); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function generateTableId(): string |
47
|
|
|
{ |
48
|
|
|
$controllerPart = str_replace('\\', '_', $this->controller); |
49
|
|
|
$typePart = $this->type ?? 'default'; |
50
|
|
|
$namePart = $this->name ?? 'default'; |
51
|
|
|
$uniqueId = md5($controllerPart.'_'.$typePart.'_'.$namePart); |
52
|
|
|
|
53
|
|
|
return 'crudTable_'.$uniqueId; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Store the datatable configuration in the cache for later use in Ajax requests. |
58
|
|
|
*/ |
59
|
|
|
private function storeDatatableConfig() |
60
|
|
|
{ |
61
|
|
|
if (! $this->configure) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$controllerClass = $this->controller; |
66
|
|
|
$cruds = CrudManager::getCruds(); |
|
|
|
|
67
|
|
|
$parentCrud = reset($cruds); |
68
|
|
|
|
69
|
|
|
if ($parentCrud && $parentCrud->getCurrentEntry()) { |
70
|
|
|
$parentEntry = $parentCrud->getCurrentEntry(); |
71
|
|
|
$parentController = $parentCrud->controller; |
72
|
|
|
$cacheKey = 'datatable_config_'.$this->tableId; |
73
|
|
|
|
74
|
|
|
Cache::forget($cacheKey); |
75
|
|
|
|
76
|
|
|
// Store the controller class, parent entry, element type and name |
77
|
|
|
Cache::put($cacheKey, [ |
78
|
|
|
'controller' => $controllerClass, |
79
|
|
|
'parentController' => $parentController, |
80
|
|
|
'parent_entry' => $parentEntry, |
81
|
|
|
'element_type' => $this->type, |
82
|
|
|
'element_name' => $this->name, |
83
|
|
|
], now()->addHours(1)); |
84
|
|
|
|
85
|
|
|
$this->crud->set('list.datatable_id', $this->tableId); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function applyCachedConfigurationClosure($crud) |
90
|
|
|
{ |
91
|
|
|
$tableId = request('datatable_id'); |
92
|
|
|
|
93
|
|
|
if (! $tableId) { |
94
|
|
|
\Log::debug('Missing datatable_id in request parameters'); |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$cacheKey = 'datatable_config_'.$tableId; |
100
|
|
|
$cachedData = Cache::get($cacheKey); |
101
|
|
|
|
102
|
|
|
if (! $cachedData) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
// Get the parent crud instance |
108
|
|
|
$parentCrud = CrudManager::crudFromController($cachedData['parentController']); |
109
|
|
|
$parentCrud->initialized = false; |
110
|
|
|
$parentCrud = CrudManager::crudFromController($cachedData['parentController'], 'show'); |
|
|
|
|
111
|
|
|
$entry = $cachedData['parent_entry']; |
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 = Widget::collection(); |
118
|
|
|
|
119
|
|
|
foreach ($widgets as $widget) { |
120
|
|
|
if ($widget['type'] === 'datatable' && |
121
|
|
|
(isset($widget['name']) && $widget['name'] === $elementName) && |
122
|
|
|
(isset($widget['configure']) && $widget['configure'] instanceof \Closure)) { |
123
|
|
|
$widget['configure']($crud, $entry); |
124
|
|
|
Cache::forget($cacheKey); |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
Cache::forget($cacheKey); |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} catch (\Exception $e) { |
132
|
|
|
\Log::error('Error applying cached datatable config: '.$e->getMessage(), [ |
133
|
|
|
'exception' => $e, |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
Cache::forget($cacheKey); |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function render() |
141
|
|
|
{ |
142
|
|
|
return view('crud::datatable.datatable', [ |
143
|
|
|
'crud' => $this->crud, |
144
|
|
|
'updatesUrl' => $this->updatesUrl, |
145
|
|
|
'tableId' => $this->tableId, |
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|