|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backpack\CRUD\app\View\Components; |
| 4 | + |
| 5 | +use Backpack\CRUD\CrudManager; |
| 6 | +use Illuminate\View\Component; |
| 7 | + |
| 8 | +class Dataform extends Component |
| 9 | +{ |
| 10 | + public $crud; |
| 11 | + |
| 12 | + /** |
| 13 | + * Create a new component instance. |
| 14 | + * |
| 15 | + * @param string $controller The CRUD controller class name |
| 16 | + * @param string $operation The operation to use (create, update, etc.) |
| 17 | + * @param string|null $action Custom form action URL |
| 18 | + * @param string $method Form method (post, put, etc.) |
| 19 | + */ |
| 20 | + public function __construct( |
| 21 | + public string $controller, |
| 22 | + public string $id = 'backpack-form', |
| 23 | + public string $operation = 'create', |
| 24 | + public ?string $action = null, |
| 25 | + public string $method = 'post', |
| 26 | + public bool $hasUploadFields = false, |
| 27 | + public $entry = null, |
| 28 | + |
| 29 | + ) { |
| 30 | + // Get CRUD panel instance from the controller |
| 31 | + if (CrudManager::hasCrudPanel($controller)) { |
| 32 | + $previousOperation = CrudManager::getCrudPanel($controller)->getOperation(); |
| 33 | + } |
| 34 | + |
| 35 | + $this->crud = CrudManager::setupCrudPanel($controller, $operation); |
| 36 | + |
| 37 | + if (isset($previousOperation)) { |
| 38 | + $this->crud->setOperation($previousOperation); |
| 39 | + } |
| 40 | + |
| 41 | + if ($this->entry && $this->operation === 'update') { |
| 42 | + $this->action = $action ?? url($this->crud->route.'/'.$this->entry->getKey()); |
| 43 | + $this->method = 'put'; |
| 44 | + $this->crud->entry = $this->entry; |
| 45 | + $this->crud->setOperationSetting('fields', $this->crud->getUpdateFields()); |
| 46 | + } else { |
| 47 | + $this->action = $action ?? url($this->crud->route); |
| 48 | + } |
| 49 | + $this->hasUploadFields = $this->crud->hasUploadFields($operation, $this->entry?->getKey()); |
| 50 | + $this->id = $id.md5($this->action.$this->operation.$this->method.$this->controller); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get the view / contents that represent the component. |
| 55 | + * |
| 56 | + * @return \Illuminate\Contracts\View\View|\Closure|string |
| 57 | + */ |
| 58 | + public function render() |
| 59 | + { |
| 60 | + return view('crud::components.dataform.form', [ |
| 61 | + 'crud' => $this->crud, |
| 62 | + 'saveAction' => $this->crud->getSaveAction(), |
| 63 | + 'id' => $this->id, |
| 64 | + 'operation' => $this->operation, |
| 65 | + 'action' => $this->action, |
| 66 | + 'method' => $this->method, |
| 67 | + 'hasUploadFields' => $this->hasUploadFields, |
| 68 | + 'entry' => $this->entry, |
| 69 | + ]); |
| 70 | + } |
| 71 | +} |
0 commit comments