1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\View\Components; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
6
|
|
|
use Backpack\CRUD\CrudManager; |
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\View\Component; |
9
|
|
|
|
10
|
|
|
class Dataform extends Component |
11
|
|
|
{ |
12
|
|
|
public $crud; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a new component instance. |
16
|
|
|
* |
17
|
|
|
* @param string $controller The CRUD controller class name |
18
|
|
|
* @param string $operation The operation to use (create, update, etc.) |
19
|
|
|
* @param string|null $action Custom form action URL |
20
|
|
|
* @param string $method Form method (post, put, etc.) |
21
|
|
|
*/ |
22
|
|
|
public function __construct( |
23
|
|
|
public string $controller, |
24
|
|
|
public string $id = 'backpack-form', |
25
|
|
|
public string $operation = 'create', |
26
|
|
|
public ?string $action = null, |
27
|
|
|
public string $method = 'post', |
28
|
|
|
public bool $hasUploadFields = false, |
29
|
|
|
public $entry = null, |
30
|
|
|
public ?Closure $setup = null, |
31
|
|
|
|
32
|
|
|
) { |
33
|
|
|
// Get CRUD panel instance from the controller |
34
|
|
|
CrudManager::setActiveController($controller); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$this->crud = CrudManager::setupCrudPanel($controller, $operation); |
|
|
|
|
37
|
|
|
//dd($this->crud); |
38
|
|
|
if ($this->entry && $this->operation === 'update') { |
39
|
|
|
$this->action = $action ?? url($this->crud->route.'/'.$this->entry->getKey()); |
40
|
|
|
$this->method = 'put'; |
41
|
|
|
$this->crud->entry = $this->entry; |
42
|
|
|
$this->crud->setOperationSetting('fields', $this->crud->getUpdateFields()); |
43
|
|
|
} else { |
44
|
|
|
$this->action = $action ?? url($this->crud->route); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
$this->hasUploadFields = $this->crud->hasUploadFields($operation, $this->entry?->getKey()); |
47
|
|
|
$this->id = $id.md5($this->action.$this->operation.$this->method.$this->controller); |
48
|
|
|
if ($this->setup) { |
49
|
|
|
$this->applySetupClosure(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
CrudManager::unsetActiveController(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function applySetupClosure(): bool |
57
|
|
|
{ |
58
|
|
|
$originalSetup = $this->setup; |
59
|
|
|
$controllerClass = $this->controller; |
60
|
|
|
$crud = $this->crud; |
61
|
|
|
$entry = $this->entry; |
62
|
|
|
|
63
|
|
|
$modifiedSetup = function ($crud, $entry) use ($originalSetup, $controllerClass) { |
64
|
|
|
CrudManager::setActiveController($controllerClass); |
65
|
|
|
|
66
|
|
|
// Run the original closure |
67
|
|
|
return ($originalSetup)($crud, $entry); |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
// Execute the modified closure |
72
|
|
|
($modifiedSetup)($crud, $entry); |
73
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} finally { |
76
|
|
|
// Clean up |
77
|
|
|
CrudManager::unsetActiveController(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the view / contents that represent the component. |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Contracts\View\View|\Closure|string |
85
|
|
|
*/ |
86
|
|
|
public function render() |
87
|
|
|
{ |
88
|
|
|
return view('crud::components.dataform.form', [ |
89
|
|
|
'crud' => $this->crud, |
90
|
|
|
'saveAction' => $this->crud->getSaveAction(), |
91
|
|
|
'id' => $this->id, |
92
|
|
|
'operation' => $this->operation, |
93
|
|
|
'action' => $this->action, |
94
|
|
|
'method' => $this->method, |
95
|
|
|
'hasUploadFields' => $this->hasUploadFields, |
96
|
|
|
'entry' => $this->entry, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|