1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Http\Controllers\Managerarea; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Cortex\Auth\Models\Manager; |
10
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
11
|
|
|
use Cortex\Foundation\DataTables\LogsDataTable; |
12
|
|
|
use Cortex\Foundation\Importers\DefaultImporter; |
13
|
|
|
use Cortex\Foundation\DataTables\ActivitiesDataTable; |
14
|
|
|
use Cortex\Foundation\DataTables\ImportLogsDataTable; |
15
|
|
|
use Cortex\Foundation\Http\Requests\ImportFormRequest; |
16
|
|
|
use Cortex\Foundation\DataTables\ImportRecordsDataTable; |
17
|
|
|
use Cortex\Auth\DataTables\Managerarea\ManagersDataTable; |
18
|
|
|
use Cortex\Foundation\Http\Controllers\AuthorizedController; |
19
|
|
|
use Cortex\Auth\Http\Requests\Managerarea\ManagerFormRequest; |
20
|
|
|
use Cortex\Auth\Http\Requests\Managerarea\ManagerAttributesFormRequest; |
21
|
|
|
|
22
|
|
|
class ManagersController extends AuthorizedController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected $resource = Manager::class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* List all managers. |
31
|
|
|
* |
32
|
|
|
* @param \Cortex\Auth\DataTables\Managerarea\ManagersDataTable $managersDataTable |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
35
|
|
|
*/ |
36
|
|
|
public function index(ManagersDataTable $managersDataTable) |
37
|
|
|
{ |
38
|
|
|
return $managersDataTable->with([ |
39
|
|
|
'id' => 'managerarea-managers-index-table', |
40
|
|
|
])->render('cortex/foundation::managerarea.pages.datatable-index'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* List manager logs. |
45
|
|
|
* |
46
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
47
|
|
|
* @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
public function logs(Manager $manager, LogsDataTable $logsDataTable) |
52
|
|
|
{ |
53
|
|
|
return $logsDataTable->with([ |
54
|
|
|
'resource' => $manager, |
55
|
|
|
'tabs' => 'managerarea.managers.tabs', |
56
|
|
|
'id' => "managerarea-managers-{$manager->getRouteKey()}-logs-table", |
57
|
|
|
])->render('cortex/foundation::managerarea.pages.datatable-tab'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get a listing of the resource activities. |
62
|
|
|
* |
63
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
64
|
|
|
* @param \Cortex\Foundation\DataTables\ActivitiesDataTable $activitiesDataTable |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function activities(Manager $manager, ActivitiesDataTable $activitiesDataTable) |
69
|
|
|
{ |
70
|
|
|
return $activitiesDataTable->with([ |
71
|
|
|
'resource' => $manager, |
72
|
|
|
'tabs' => 'managerarea.managers.tabs', |
73
|
|
|
'id' => "managerarea-managers-{$manager->getRouteKey()}-activities-table", |
74
|
|
|
])->render('cortex/foundation::managerarea.pages.datatable-tab'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Show the form for create/update of the given resource attributes. |
79
|
|
|
* |
80
|
|
|
* @param \Illuminate\Http\Request $request |
81
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\View\View |
84
|
|
|
*/ |
85
|
|
|
public function attributes(Request $request, Manager $manager) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
return view('cortex/auth::managerarea.pages.manager-attributes', compact('manager')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Process the account update form. |
92
|
|
|
* |
93
|
|
|
* @param \Cortex\Auth\Http\Requests\Managerarea\ManagerAttributesFormRequest $request |
94
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
97
|
|
|
*/ |
98
|
|
|
public function updateAttributes(ManagerAttributesFormRequest $request, Manager $manager) |
99
|
|
|
{ |
100
|
|
|
$data = $request->validated(); |
101
|
|
|
|
102
|
|
|
// Update profile |
103
|
|
|
$manager->fill($data)->save(); |
104
|
|
|
|
105
|
|
|
return intend([ |
106
|
|
|
'back' => true, |
107
|
|
|
'with' => ['success' => trans('cortex/auth::messages.account.updated_attributes')], |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Import managers. |
113
|
|
|
* |
114
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
115
|
|
|
* @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable |
116
|
|
|
* |
117
|
|
|
* @return \Illuminate\View\View |
118
|
|
|
*/ |
119
|
|
|
public function import(Manager $manager, ImportRecordsDataTable $importRecordsDataTable) |
120
|
|
|
{ |
121
|
|
|
return $importRecordsDataTable->with([ |
122
|
|
|
'resource' => $manager, |
123
|
|
|
'tabs' => 'managerarea.attributes.tabs', |
124
|
|
|
'url' => route('managerarea.attributes.stash'), |
125
|
|
|
'id' => "managerarea-attributes-{$manager->getRouteKey()}-import-table", |
126
|
|
|
])->render('cortex/foundation::managerarea.pages.datatable-dropzone'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Stash managers. |
131
|
|
|
* |
132
|
|
|
* @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request |
133
|
|
|
* @param \Cortex\Foundation\Importers\DefaultImporter $importer |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function stash(ImportFormRequest $request, DefaultImporter $importer) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
// Handle the import |
140
|
|
|
$importer->config['resource'] = $this->resource; |
141
|
|
|
$importer->config['name'] = 'username'; |
142
|
|
|
$importer->handleImport(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Hoard managers. |
147
|
|
|
* |
148
|
|
|
* @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
151
|
|
|
*/ |
152
|
|
|
public function hoard(ImportFormRequest $request) |
153
|
|
|
{ |
154
|
|
|
foreach ((array) $request->get('selected_ids') as $recordId) { |
155
|
|
|
$record = app('cortex.foundation.import_record')->find($recordId); |
156
|
|
|
|
157
|
|
|
try { |
158
|
|
|
$fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.auth.manager')->getFillable()))->toArray(); |
159
|
|
|
|
160
|
|
|
tap(app('rinvex.auth.manager')->firstOrNew($fillable), function ($instance) use ($record) { |
161
|
|
|
$instance->save() && $record->delete(); |
162
|
|
|
}); |
163
|
|
|
} catch (Exception $exception) { |
164
|
|
|
$record->notes = $exception->getMessage().(method_exists($exception, 'getMessageBag') ? "\n".json_encode($exception->getMessageBag())."\n\n" : ''); |
|
|
|
|
165
|
|
|
$record->status = 'fail'; |
166
|
|
|
$record->save(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return intend([ |
171
|
|
|
'back' => true, |
172
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.import_complete')], |
173
|
|
|
]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* List manager import logs. |
178
|
|
|
* |
179
|
|
|
* @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable |
180
|
|
|
* |
181
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
182
|
|
|
*/ |
183
|
|
|
public function importLogs(ImportLogsDataTable $importLogsDatatable) |
184
|
|
|
{ |
185
|
|
|
return $importLogsDatatable->with([ |
186
|
|
|
'resource' => trans('cortex/auth::common.manager'), |
187
|
|
|
'tabs' => 'managerarea.managers.tabs', |
188
|
|
|
'id' => 'managerarea-managers-import-logs-table', |
189
|
|
|
])->render('cortex/foundation::managerarea.pages.datatable-tab'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Create new manager. |
194
|
|
|
* |
195
|
|
|
* @param \Illuminate\Http\Request $request |
196
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
197
|
|
|
* |
198
|
|
|
* @return \Illuminate\View\View |
199
|
|
|
*/ |
200
|
|
|
public function create(Request $request, Manager $manager) |
201
|
|
|
{ |
202
|
|
|
return $this->form($request, $manager); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Edit given manager. |
207
|
|
|
* |
208
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
209
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
210
|
|
|
* |
211
|
|
|
* @return \Illuminate\View\View |
212
|
|
|
*/ |
213
|
|
|
public function edit(ManagerFormRequest $request, Manager $manager) |
214
|
|
|
{ |
215
|
|
|
return $this->form($request, $manager); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Show manager create/edit form. |
220
|
|
|
* |
221
|
|
|
* @param \Illuminate\Http\Request $request |
222
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
223
|
|
|
* |
224
|
|
|
* @return \Illuminate\View\View |
225
|
|
|
*/ |
226
|
|
|
protected function form(Request $request, Manager $manager) |
227
|
|
|
{ |
228
|
|
|
$countries = collect(countries())->map(function ($country, $code) { |
229
|
|
|
return [ |
230
|
|
|
'id' => $code, |
231
|
|
|
'text' => $country['name'], |
232
|
|
|
'emoji' => $country['emoji'], |
233
|
|
|
]; |
234
|
|
|
})->values(); |
235
|
|
|
|
236
|
|
|
$tags = app('rinvex.tags.tag')->pluck('name', 'id'); |
237
|
|
|
$languages = collect(languages())->pluck('name', 'iso_639_1'); |
238
|
|
|
$genders = ['male' => trans('cortex/auth::common.male'), 'female' => trans('cortex/auth::common.female')]; |
239
|
|
|
$abilities = $request->user($this->getGuard())->getManagedAbilities(); |
240
|
|
|
$roles = $request->user($this->getGuard())->getManagedRoles(); |
241
|
|
|
|
242
|
|
|
return view('cortex/auth::managerarea.pages.manager', compact('manager', 'abilities', 'roles', 'countries', 'languages', 'genders', 'tags')); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Store new manager. |
247
|
|
|
* |
248
|
|
|
* @param \Cortex\Auth\Http\Requests\Managerarea\ManagerFormRequest $request |
249
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
250
|
|
|
* |
251
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
252
|
|
|
*/ |
253
|
|
|
public function store(ManagerFormRequest $request, Manager $manager) |
254
|
|
|
{ |
255
|
|
|
return $this->process($request, $manager); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Update given manager. |
260
|
|
|
* |
261
|
|
|
* @param \Cortex\Auth\Http\Requests\Managerarea\ManagerFormRequest $request |
262
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
263
|
|
|
* |
264
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
265
|
|
|
*/ |
266
|
|
|
public function update(ManagerFormRequest $request, Manager $manager) |
267
|
|
|
{ |
268
|
|
|
return $this->process($request, $manager); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Process stored/updated manager. |
273
|
|
|
* |
274
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
275
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
276
|
|
|
* |
277
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
278
|
|
|
*/ |
279
|
|
|
protected function process(FormRequest $request, Manager $manager) |
280
|
|
|
{ |
281
|
|
|
// Prepare required input fields |
282
|
|
|
$data = $request->validated(); |
283
|
|
|
|
284
|
|
|
! $request->hasFile('profile_picture') |
285
|
|
|
|| $manager->addMediaFromRequest('profile_picture') |
286
|
|
|
->sanitizingFileName(function ($fileName) { |
287
|
|
|
return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION); |
288
|
|
|
}) |
289
|
|
|
->toMediaCollection('profile_picture', config('cortex.foundation.media.disk')); |
290
|
|
|
|
291
|
|
|
! $request->hasFile('cover_photo') |
292
|
|
|
|| $manager->addMediaFromRequest('cover_photo') |
293
|
|
|
->sanitizingFileName(function ($fileName) { |
294
|
|
|
return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION); |
295
|
|
|
}) |
296
|
|
|
->toMediaCollection('cover_photo', config('cortex.foundation.media.disk')); |
297
|
|
|
|
298
|
|
|
// Save manager |
299
|
|
|
$manager->fill($data)->save(); |
300
|
|
|
|
301
|
|
|
return intend([ |
302
|
|
|
'url' => route('managerarea.managers.index'), |
303
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.manager'), 'identifier' => $manager->username])], |
304
|
|
|
]); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Destroy given manager. |
309
|
|
|
* |
310
|
|
|
* @param \Cortex\Auth\Models\Manager $manager |
311
|
|
|
* |
312
|
|
|
* @throws \Exception |
313
|
|
|
* |
314
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
315
|
|
|
*/ |
316
|
|
|
public function destroy(Manager $manager) |
317
|
|
|
{ |
318
|
|
|
$manager->delete(); |
319
|
|
|
|
320
|
|
|
return intend([ |
321
|
|
|
'url' => route('managerarea.managers.index'), |
322
|
|
|
'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.manager'), 'identifier' => $manager->username])], |
323
|
|
|
]); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.