1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\View\Components; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\CrudManager; |
6
|
|
|
use Illuminate\View\Component; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
9
|
|
|
|
10
|
|
|
abstract class ShowComponent extends Component |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Create a new component instance. |
14
|
|
|
*/ |
15
|
|
|
public function __construct( |
16
|
|
|
public Model $entry, |
17
|
|
|
public ?string $controller = null, |
18
|
|
|
public ?string $operation = 'show', |
19
|
|
|
public ?\Closure $setup = null, |
20
|
|
|
public ?CrudPanel $crud = null, |
21
|
|
|
public array $columns = [], |
22
|
|
|
public bool $displayButtons = true |
23
|
|
|
) { |
24
|
|
|
$this->setPropertiesFromController(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set properties from the controller context. |
29
|
|
|
* |
30
|
|
|
* This method initializes the CrudPanel and sets the active controller. |
31
|
|
|
* It also applies any setup closure provided. |
32
|
|
|
*/ |
33
|
|
|
protected function setPropertiesFromController() : void |
34
|
|
|
{ |
35
|
|
|
// If no CrudController is provided, do nothing |
36
|
|
|
if (!$this->controller) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// If no CrudPanel is provided, try to get it from the CrudManager |
41
|
|
|
$this->crud ??= CrudManager::setupCrudPanel($this->controller, $this->operation); |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Set active controller for proper context |
44
|
|
|
CrudManager::setActiveController($this->controller); |
|
|
|
|
45
|
|
|
|
46
|
|
|
// If a setup closure is provided, apply it |
47
|
|
|
if ($this->setup) { |
48
|
|
|
if (!empty($columns)) { |
|
|
|
|
49
|
|
|
throw new \Exception('You cannot define both setup closure and columns for a ' . class_basename(static::class) . ' component.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
($this->setup)($this->crud, $this->entry); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->columns = !empty($columns) ? $columns : $this->crud?->getOperationSetting('columns', $this->operation) ?? []; |
56
|
|
|
|
57
|
|
|
// Reset the active controller |
58
|
|
|
CrudManager::unsetActiveController(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the view name for the component. |
63
|
|
|
* This method must be implemented by child classes. |
64
|
|
|
*/ |
65
|
|
|
abstract protected function getViewName(): string; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the view / contents that represent the component. |
69
|
|
|
*/ |
70
|
|
|
public function render() |
71
|
|
|
{ |
72
|
|
|
// if no columns are set, don't load any view |
73
|
|
|
if (empty($this->columns)) { |
74
|
|
|
return ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return view($this->getViewName()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|