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