1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Uccello\Core\Http\Controllers\Core; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
6
|
|
|
use Illuminate\Routing\Controller as BaseController; |
7
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
8
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Illuminate\Support\Facades\View; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Illuminate\Support\Facades\Schema; |
13
|
|
|
use Uccello\Core\Models\Domain; |
14
|
|
|
use Uccello\Core\Models\Module; |
15
|
|
|
use Uccello\Core\Support\MenuGenerator; |
16
|
|
|
use Illuminate\Database\Eloquent\Collection; |
17
|
|
|
use Illuminate\Support\Facades\Cache; |
18
|
|
|
|
19
|
|
|
abstract class Controller extends BaseController |
20
|
|
|
{ |
21
|
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Uccello\Core\Models\Domain |
25
|
|
|
*/ |
26
|
|
|
protected $domain; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Uccello\Core\Models\Module |
30
|
|
|
*/ |
31
|
|
|
protected $module; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Illuminate\Http\Request |
35
|
|
|
*/ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $viewName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new controller instance. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
$this->middleware('auth'); |
51
|
|
|
|
52
|
|
|
// Check user permissions |
53
|
|
|
$this->checkPermissions(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Process and display asked page |
58
|
|
|
* @param Domain|null $domain |
59
|
|
|
* @param Module $module |
60
|
|
|
* @param Request $request |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Http\Response |
63
|
|
|
*/ |
64
|
|
|
public function process(?Domain $domain, Module $module, Request $request) |
65
|
|
|
{ |
66
|
|
|
// Pre-process |
67
|
|
|
$this->preProcess($domain, $module, $request); |
68
|
|
|
|
69
|
|
|
return $this->autoView(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Instantiance variables and check permissions |
74
|
|
|
* @param Domain|null $domain |
75
|
|
|
* @param Module $module |
76
|
|
|
* @param boolean $forBlade |
77
|
|
|
*/ |
78
|
|
|
protected function preProcess(?Domain &$domain, Module $module, Request $request, $forBlade=true) |
79
|
|
|
{ |
80
|
|
|
// If we don't use multi domains, find the first one |
81
|
|
|
if (!uccello()->useMultiDomains()) { |
|
|
|
|
82
|
|
|
$domain = Domain::firstOrFail(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Get domain |
86
|
|
|
$this->domain = $domain; |
87
|
|
|
|
88
|
|
|
// Get module |
89
|
|
|
$this->module = $module; |
90
|
|
|
|
91
|
|
|
// Get request |
92
|
|
|
$this->request = $request; |
93
|
|
|
|
94
|
|
|
// Save last visited domain by user |
95
|
|
|
$this->saveUserLastVisitedDomain(); |
96
|
|
|
|
97
|
|
|
// Share blade variables |
98
|
|
|
if ($forBlade) { |
99
|
|
|
$this->shareBladeVariables(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Save last visited domain by user |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
protected function saveUserLastVisitedDomain() |
109
|
|
|
{ |
110
|
|
|
$user = Auth::user(); |
111
|
|
|
$user->last_domain_id = $this->domain->id; |
|
|
|
|
112
|
|
|
$user->save(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Share global variables with all Blade views |
117
|
|
|
*/ |
118
|
|
|
protected function shareBladeVariables() |
119
|
|
|
{ |
120
|
|
|
// Selected domain |
121
|
|
|
View::share('domain', $this->domain); |
122
|
|
|
|
123
|
|
|
// Selected module |
124
|
|
|
View::share('module', $this->module); |
125
|
|
|
|
126
|
|
|
// All modules |
127
|
|
|
View::share('modules', $this->getAllModules(true)); |
128
|
|
|
|
129
|
|
|
// Admin environment |
130
|
|
|
View::share('admin_env', $this->module->isAdminModule()); |
131
|
|
|
|
132
|
|
|
// Menu |
133
|
|
|
$menuGenerator = new MenuGenerator(); |
134
|
|
|
$menuGenerator->makeMenu($this->domain, $this->module); |
135
|
|
|
$menu = $menuGenerator->getMenu(); |
136
|
|
|
View::share('menu', $menu); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Check user permissions |
141
|
|
|
*/ |
142
|
|
|
protected function checkPermissions() |
143
|
|
|
{ |
144
|
|
|
// |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get all modules from database |
149
|
|
|
* @param boolean $getAdminModules |
150
|
|
|
* @return Module[] |
151
|
|
|
*/ |
152
|
|
|
protected function getAllModules($getAdminModules = false) |
153
|
|
|
{ |
154
|
|
|
$keyName = $getAdminModules ? 'modules_all' : 'modules_not_admin'; |
155
|
|
|
|
156
|
|
|
return Cache::rememberForever($keyName, function () use($getAdminModules) { |
157
|
|
|
$modules = [ ]; |
158
|
|
|
|
159
|
|
|
$allModules = Module::all(); |
160
|
|
|
|
161
|
|
|
if ($getAdminModules) { |
162
|
|
|
$modules = $allModules; |
163
|
|
|
} else { |
164
|
|
|
foreach ($allModules as $module) { |
165
|
|
|
if (!$module->isAdminModule()) { |
166
|
|
|
$modules[ ] = $module; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $modules; |
172
|
|
|
}); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Retrieve record instance if "$key" param is defined or return a new empty instance. |
177
|
|
|
* |
178
|
|
|
* @param string $key |
179
|
|
|
* @return mixed|null |
180
|
|
|
*/ |
181
|
|
|
protected function getRecordFromRequest($key = 'id') |
182
|
|
|
{ |
183
|
|
|
if (empty($this->module->model_class)) { |
184
|
|
|
return null; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// Retrieve model class |
188
|
|
|
$modelClass = $this->module->model_class; |
|
|
|
|
189
|
|
|
|
190
|
|
|
// An id is defined, retrieve the record from the database fail (404) |
191
|
|
|
if ($this->request->has($key)) { |
192
|
|
|
$recordId = (int)$this->request->input($key); |
193
|
|
|
$record = $modelClass::findOrFail($recordId); |
194
|
|
|
} |
195
|
|
|
// Make a new empty instance |
196
|
|
|
else { |
197
|
|
|
$record = new $modelClass(); |
198
|
|
|
|
199
|
|
|
// Set domain if column exists |
200
|
|
|
if (Schema::hasColumn($record->getTable(), 'domain_id')) { |
201
|
|
|
$record->domain_id = $this->domain->id; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $record; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Detects which view it must use and returns the evaluated view contents. |
210
|
|
|
* |
211
|
|
|
* @param array $data |
212
|
|
|
* @param array $mergeData |
213
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
214
|
|
|
*/ |
215
|
|
|
protected function autoView($data = [ ], $mergeData = [ ]) |
216
|
|
|
{ |
217
|
|
|
$viewToUse = uccello()->view($this->module->package, $this->module, $this->viewName); |
|
|
|
|
218
|
|
|
|
219
|
|
|
return view($viewToUse, $data, $mergeData); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|