|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract; |
|
6
|
|
|
use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; |
|
7
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; |
|
8
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; |
|
9
|
|
|
use Backpack\CRUD\Backpack; |
|
10
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
|
11
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
|
12
|
|
|
use Illuminate\Routing\Controller; |
|
13
|
|
|
use Illuminate\Support\Str; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CrudController. |
|
17
|
|
|
* |
|
18
|
|
|
* @property-read CrudPanel $crud |
|
19
|
|
|
* @property array $data |
|
20
|
|
|
*/ |
|
21
|
|
|
class CrudController extends Controller implements CrudControllerContract |
|
22
|
|
|
{ |
|
23
|
|
|
use DispatchesJobs, ValidatesRequests; |
|
24
|
|
|
|
|
25
|
|
|
//public $crud; |
|
26
|
|
|
|
|
27
|
|
|
public $data = []; |
|
28
|
|
|
|
|
29
|
|
|
public $initialized = false; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
// --------------------------- |
|
34
|
|
|
// Create the CrudPanel object |
|
35
|
|
|
// --------------------------- |
|
36
|
|
|
// Used by developers inside their ProductCrudControllers as |
|
37
|
|
|
// $this->crud or using the CRUD facade. |
|
38
|
|
|
// |
|
39
|
|
|
// It's done inside a middleware closure in order to have |
|
40
|
|
|
// the complete request inside the CrudPanel object. |
|
41
|
|
|
$this->middleware(function ($request, $next) { |
|
42
|
|
|
if (! Backpack::hasCrudController(get_class($this))) { |
|
|
|
|
|
|
43
|
|
|
$this->initializeCrud($request); |
|
44
|
|
|
|
|
45
|
|
|
return $next($request); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->triggerControllerHooks(); |
|
49
|
|
|
|
|
50
|
|
|
$this->initialized = true; |
|
51
|
|
|
|
|
52
|
|
|
Backpack::crud($this)->setRequest($request); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
return $next($request); |
|
55
|
|
|
}); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function initializeCrud($request, $crudPanel = null, $operation = null): CrudPanel |
|
59
|
|
|
{ |
|
60
|
|
|
$crudPanel ??= Backpack::crud($this); |
|
61
|
|
|
|
|
62
|
|
|
if ($crudPanel->isInitialized()) { |
|
63
|
|
|
$crudPanel->setRequest($request); |
|
64
|
|
|
return $crudPanel; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$crudPanel->initialized = true; |
|
68
|
|
|
$crudPanel->setRequest($request); |
|
69
|
|
|
|
|
70
|
|
|
$this->triggerControllerHooks(); |
|
71
|
|
|
|
|
72
|
|
|
return $crudPanel; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function triggerControllerHooks() |
|
76
|
|
|
{ |
|
77
|
|
|
LifecycleHook::trigger('crud:before_setup_defaults', [$this]); |
|
78
|
|
|
$this->setupDefaults(); |
|
79
|
|
|
LifecycleHook::trigger('crud:after_setup_defaults', [$this]); |
|
80
|
|
|
|
|
81
|
|
|
LifecycleHook::trigger('crud:before_setup', [$this]); |
|
82
|
|
|
$this->setup(); |
|
83
|
|
|
LifecycleHook::trigger('crud:after_setup', [$this]); |
|
84
|
|
|
|
|
85
|
|
|
$this->setupConfigurationForCurrentOperation(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Allow developers to set their configuration options for a CrudPanel. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setup() |
|
92
|
|
|
{ |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Load routes for all operations. |
|
97
|
|
|
* Allow developers to load extra routes by creating a method that looks like setupOperationNameRoutes. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $segment Name of the current entity (singular). |
|
100
|
|
|
* @param string $routeName Route name prefix (ends with .). |
|
101
|
|
|
* @param string $controller Name of the current controller. |
|
102
|
|
|
*/ |
|
103
|
|
|
#[DeprecatedIgnoreOnRuntime('we dont call this method anymore unless you had it overwritten in your CrudController')] |
|
104
|
|
|
public function setupRoutes($segment, $routeName, $controller) |
|
105
|
|
|
{ |
|
106
|
|
|
preg_match_all('/(?<=^|;)setup([^;]+?)Routes(;|$)/', implode(';', get_class_methods($this)), $matches); |
|
107
|
|
|
|
|
108
|
|
|
if (count($matches[1])) { |
|
109
|
|
|
foreach ($matches[1] as $methodName) { |
|
110
|
|
|
$this->{'setup'.$methodName.'Routes'}($segment, $routeName, $controller); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Load defaults for all operations. |
|
117
|
|
|
* Allow developers to insert default settings by creating a method |
|
118
|
|
|
* that looks like setupOperationNameDefaults. |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function setupDefaults() |
|
121
|
|
|
{ |
|
122
|
|
|
preg_match_all('/(?<=^|;)setup([^;]+?)Defaults(;|$)/', implode(';', get_class_methods($this)), $matches); |
|
123
|
|
|
if (count($matches[1])) { |
|
124
|
|
|
foreach ($matches[1] as $methodName) { |
|
125
|
|
|
$this->{'setup'.$methodName.'Defaults'}(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Load configurations for the current operation. |
|
132
|
|
|
* |
|
133
|
|
|
* Allow developers to insert default settings by creating a method |
|
134
|
|
|
* that looks like setupOperationNameOperation (aka setupXxxOperation). |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function setupConfigurationForCurrentOperation(?string $operation = null) |
|
137
|
|
|
{ |
|
138
|
|
|
$operationName = $operation ?? $this->crud->getCurrentOperation(); |
|
139
|
|
|
if (! $operationName) { |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
$setupClassName = 'setup'.Str::studly($operationName).'Operation'; |
|
143
|
|
|
|
|
144
|
|
|
/* |
|
145
|
|
|
* FIRST, run all Operation Closures for this operation. |
|
146
|
|
|
* |
|
147
|
|
|
* It's preferred for this to run closures first, because |
|
148
|
|
|
* (1) setup() is usually higher in a controller than any other method, so it's more intuitive, |
|
149
|
|
|
* since the first thing you write is the first thing that is being run; |
|
150
|
|
|
* (2) operations use operation closures themselves, inside their setupXxxDefaults(), and |
|
151
|
|
|
* you'd like the defaults to be applied before anything you write. That way, anything you |
|
152
|
|
|
* write is done after the default, so you can remove default settings, etc; |
|
153
|
|
|
*/ |
|
154
|
|
|
LifecycleHook::trigger($operationName.':before_setup', [$this]); |
|
155
|
|
|
|
|
156
|
|
|
$this->crud->applyConfigurationFromSettings($operationName); |
|
157
|
|
|
/* |
|
158
|
|
|
* THEN, run the corresponding setupXxxOperation if it exists. |
|
159
|
|
|
*/ |
|
160
|
|
|
if (method_exists($this, $setupClassName)) { |
|
161
|
|
|
$this->{$setupClassName}(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
LifecycleHook::trigger($operationName.':after_setup', [$this]); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function __get($name) |
|
168
|
|
|
{ |
|
169
|
|
|
if ($name === 'crud') { |
|
170
|
|
|
return Backpack::getControllerCrud(get_class($this)); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $this->{$name}; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|