1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\View\Components; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
6
|
|
|
use Backpack\CRUD\app\Library\Support\DatatableCache; |
7
|
|
|
use Backpack\CRUD\CrudManager; |
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 $modifiesUrl = false, |
18
|
|
|
private ?\Closure $setup = null, |
19
|
|
|
private ?string $name = null, |
20
|
|
|
) { |
21
|
|
|
// Set active controller for proper context |
22
|
|
|
CrudManager::setActiveController($controller); |
|
|
|
|
23
|
|
|
|
24
|
|
|
$this->crud ??= CrudManager::setupCrudPanel($controller, 'list'); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$this->tableId = $this->generateTableId(); |
27
|
|
|
|
28
|
|
|
if ($this->setup) { |
29
|
|
|
// Apply the configuration using DatatableCache |
30
|
|
|
$cache = DatatableCache::instance(); |
31
|
|
|
$cache->applySetupClosure($this->crud, $this->controller, $this->setup, $this->getParentCrudEntry()); |
|
|
|
|
32
|
|
|
|
33
|
|
|
// Cache the setup for later use |
34
|
|
|
$cache->cacheForComponent($this->tableId, $this->controller, $this->setup, $this->name, $this->crud); |
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(); |
|
|
|
|
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
|
|
|
|
56
|
|
|
return $parentCrud->getCurrentEntry(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function generateTableId(): string |
63
|
|
|
{ |
64
|
|
|
$controllerPart = str_replace('\\', '_', $this->controller); |
65
|
|
|
$namePart = $this->name ?? 'default'; |
66
|
|
|
$uniqueId = md5($controllerPart.'_'.$namePart); |
67
|
|
|
|
68
|
|
|
return 'crudTable_'.$uniqueId; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function render() |
72
|
|
|
{ |
73
|
|
|
return view('crud::components.datatable.datatable', [ |
74
|
|
|
'crud' => $this->crud, |
75
|
|
|
'modifiesUrl' => $this->modifiesUrl, |
76
|
|
|
'tableId' => $this->tableId, |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
} |