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
|
|
|
DatatableCache::applyAndStoreSetupClosure( |
31
|
|
|
$this->tableId, |
32
|
|
|
$this->controller, |
33
|
|
|
$this->setup, |
34
|
|
|
$this->name, |
35
|
|
|
$this->crud, |
36
|
|
|
$this->getParentCrudEntry() |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (! $this->crud->has('list.datatablesUrl')) { |
|
|
|
|
41
|
|
|
$this->crud->set('list.datatablesUrl', $this->crud->getRoute()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Reset the active controller |
45
|
|
|
CrudManager::unsetActiveController(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function getParentCrudEntry() |
49
|
|
|
{ |
50
|
|
|
$cruds = CrudManager::getCrudPanels(); |
|
|
|
|
51
|
|
|
$parentCrud = reset($cruds); |
52
|
|
|
|
53
|
|
|
if ($parentCrud && $parentCrud->getCurrentEntry()) { |
54
|
|
|
CrudManager::storeInitializedOperation( |
|
|
|
|
55
|
|
|
$parentCrud->controller, |
56
|
|
|
$parentCrud->getCurrentOperation() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return $parentCrud->getCurrentEntry(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function generateTableId(): string |
66
|
|
|
{ |
67
|
|
|
$controllerPart = str_replace('\\', '_', $this->controller); |
68
|
|
|
$namePart = $this->name ?? 'default'; |
69
|
|
|
$uniqueId = md5($controllerPart.'_'.$namePart); |
70
|
|
|
|
71
|
|
|
return 'crudTable_'.$uniqueId; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function render() |
75
|
|
|
{ |
76
|
|
|
return view('crud::components.datatable.datatable', [ |
77
|
|
|
'crud' => $this->crud, |
78
|
|
|
'modifiesUrl' => $this->modifiesUrl, |
79
|
|
|
'tableId' => $this->tableId, |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|