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