1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Tenants\Http\Controllers\Adminarea; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Cortex\Tenants\Models\Tenant; |
9
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
10
|
|
|
use Cortex\Foundation\DataTables\LogsDataTable; |
11
|
|
|
use Cortex\Foundation\Importers\DefaultImporter; |
12
|
|
|
use Cortex\Foundation\DataTables\ImportLogsDataTable; |
13
|
|
|
use Cortex\Foundation\Http\Requests\ImportFormRequest; |
14
|
|
|
use Cortex\Foundation\DataTables\ImportRecordsDataTable; |
15
|
|
|
use Cortex\Tenants\DataTables\Adminarea\TenantsDataTable; |
16
|
|
|
use Cortex\Foundation\Http\Controllers\AuthorizedController; |
17
|
|
|
use Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest; |
18
|
|
|
|
19
|
|
|
class TenantsController extends AuthorizedController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
protected $resource = Tenant::class; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* List all tenants. |
28
|
|
|
* |
29
|
|
|
* @param \Cortex\Tenants\DataTables\Adminarea\TenantsDataTable $tenantsDataTable |
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
32
|
|
|
*/ |
33
|
|
|
public function index(TenantsDataTable $tenantsDataTable) |
34
|
|
|
{ |
35
|
|
|
return $tenantsDataTable->with([ |
36
|
|
|
'id' => 'adminarea-tenants-index-table', |
37
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-index'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* List tenant logs. |
42
|
|
|
* |
43
|
|
|
* @param \Cortex\Tenants\Models\Tenant $tenant |
44
|
|
|
* @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable |
45
|
|
|
* |
46
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function logs(Tenant $tenant, LogsDataTable $logsDataTable) |
49
|
|
|
{ |
50
|
|
|
return $logsDataTable->with([ |
51
|
|
|
'resource' => $tenant, |
52
|
|
|
'tabs' => 'adminarea.tenants.tabs', |
53
|
|
|
'id' => "adminarea-tenants-{$tenant->getRouteKey()}-logs-table", |
54
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-tab'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Import tenants. |
59
|
|
|
* |
60
|
|
|
* @param \Cortex\Tenants\Models\Tenant $tenant |
61
|
|
|
* @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable |
62
|
|
|
* |
63
|
|
|
* @return \Illuminate\View\View |
64
|
|
|
*/ |
65
|
|
|
public function import(Tenant $tenant, ImportRecordsDataTable $importRecordsDataTable) |
66
|
|
|
{ |
67
|
|
|
return $importRecordsDataTable->with([ |
68
|
|
|
'resource' => $tenant, |
69
|
|
|
'tabs' => 'adminarea.tenants.tabs', |
70
|
|
|
'url' => route('adminarea.tenants.stash'), |
71
|
|
|
'id' => "adminarea-tenants-{$tenant->getRouteKey()}-import-table", |
72
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-dropzone'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Stash tenants. |
77
|
|
|
* |
78
|
|
|
* @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request |
79
|
|
|
* @param \Cortex\Foundation\Importers\DefaultImporter $importer |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function stash(ImportFormRequest $request, DefaultImporter $importer) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
// Handle the import |
86
|
|
|
$importer->config['resource'] = $this->resource; |
87
|
|
|
$importer->handleImport(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Hoard tenants. |
92
|
|
|
* |
93
|
|
|
* @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request |
94
|
|
|
* |
95
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
96
|
|
|
*/ |
97
|
|
|
public function hoard(ImportFormRequest $request) |
98
|
|
|
{ |
99
|
|
|
foreach ((array) $request->get('selected_ids') as $recordId) { |
100
|
|
|
$record = app('cortex.foundation.import_record')->find($recordId); |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
$fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.tenants.tenant')->getFillable()))->toArray(); |
104
|
|
|
|
105
|
|
|
tap(app('rinvex.tenants.tenant')->firstOrNew($fillable), function ($instance) use ($record) { |
106
|
|
|
$instance->save() && $record->delete(); |
107
|
|
|
}); |
108
|
|
|
} catch (Exception $exception) { |
109
|
|
|
$record->notes = $exception->getMessage().(method_exists($exception, 'getMessageBag') ? "\n".json_encode($exception->getMessageBag())."\n\n" : ''); |
|
|
|
|
110
|
|
|
$record->status = 'fail'; |
111
|
|
|
$record->save(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return intend([ |
116
|
|
|
'back' => true, |
117
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.import_complete')], |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* List tenant import logs. |
123
|
|
|
* |
124
|
|
|
* @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable |
125
|
|
|
* |
126
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
127
|
|
|
*/ |
128
|
|
|
public function importLogs(ImportLogsDataTable $importLogsDatatable) |
129
|
|
|
{ |
130
|
|
|
return $importLogsDatatable->with([ |
131
|
|
|
'resource' => trans('cortex/tenants::common.tenant'), |
132
|
|
|
'tabs' => 'adminarea.tenants.tabs', |
133
|
|
|
'id' => 'adminarea-tenants-import-logs-table', |
134
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-tab'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Show tenant create/edit form. |
139
|
|
|
* |
140
|
|
|
* @param \Cortex\Tenants\Models\Tenant $tenant |
141
|
|
|
* |
142
|
|
|
* @return \Illuminate\View\View |
143
|
|
|
*/ |
144
|
|
|
protected function form(Tenant $tenant) |
145
|
|
|
{ |
146
|
|
|
$countries = collect(countries())->map(function ($country, $code) { |
147
|
|
|
return [ |
148
|
|
|
'id' => $code, |
149
|
|
|
'text' => $country['name'], |
150
|
|
|
'emoji' => $country['emoji'], |
151
|
|
|
]; |
152
|
|
|
})->values(); |
153
|
|
|
|
154
|
|
|
$tags = app('rinvex.tags.tag')->pluck('name', 'id'); |
155
|
|
|
$languages = collect(languages())->pluck('name', 'iso_639_1'); |
156
|
|
|
|
157
|
|
|
return view('cortex/tenants::adminarea.pages.tenant', compact('tenant', 'countries', 'languages', 'tags')); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Store new tenant. |
162
|
|
|
* |
163
|
|
|
* @param \Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest $request |
164
|
|
|
* @param \Cortex\Tenants\Models\Tenant $tenant |
165
|
|
|
* |
166
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
167
|
|
|
*/ |
168
|
|
|
public function store(TenantFormRequest $request, Tenant $tenant) |
169
|
|
|
{ |
170
|
|
|
return $this->process($request, $tenant); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Update given tenant. |
175
|
|
|
* |
176
|
|
|
* @param \Cortex\Tenants\Http\Requests\Adminarea\TenantFormRequest $request |
177
|
|
|
* @param \Cortex\Tenants\Models\Tenant $tenant |
178
|
|
|
* |
179
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
180
|
|
|
*/ |
181
|
|
|
public function update(TenantFormRequest $request, Tenant $tenant) |
182
|
|
|
{ |
183
|
|
|
return $this->process($request, $tenant); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Process stored/updated tenant. |
188
|
|
|
* |
189
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
190
|
|
|
* @param \Cortex\Tenants\Models\Tenant $tenant |
191
|
|
|
* |
192
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
193
|
|
|
*/ |
194
|
|
|
protected function process(FormRequest $request, Tenant $tenant) |
195
|
|
|
{ |
196
|
|
|
// Prepare required input fields |
197
|
|
|
$data = $request->validated(); |
198
|
|
|
|
199
|
|
|
! $request->hasFile('profile_picture') |
200
|
|
|
|| $tenant->addMediaFromRequest('profile_picture') |
201
|
|
|
->sanitizingFileName(function ($fileName) { |
202
|
|
|
return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION); |
203
|
|
|
}) |
204
|
|
|
->toMediaCollection('profile_picture', config('cortex.foundation.media.disk')); |
205
|
|
|
|
206
|
|
|
! $request->hasFile('cover_photo') |
207
|
|
|
|| $tenant->addMediaFromRequest('cover_photo') |
208
|
|
|
->sanitizingFileName(function ($fileName) { |
209
|
|
|
return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION); |
210
|
|
|
}) |
211
|
|
|
->toMediaCollection('cover_photo', config('cortex.foundation.media.disk')); |
212
|
|
|
|
213
|
|
|
// Save tenant |
214
|
|
|
$tenant->fill($data)->save(); |
215
|
|
|
|
216
|
|
|
return intend([ |
217
|
|
|
'url' => route('adminarea.tenants.index'), |
218
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/tenants::common.tenant'), 'identifier' => $tenant->name])], |
219
|
|
|
]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Destroy given tenant. |
224
|
|
|
* |
225
|
|
|
* @param \Cortex\Tenants\Models\Tenant $tenant |
226
|
|
|
* |
227
|
|
|
* @throws \Exception |
228
|
|
|
* |
229
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
230
|
|
|
*/ |
231
|
|
|
public function destroy(Tenant $tenant) |
232
|
|
|
{ |
233
|
|
|
$tenant->delete(); |
234
|
|
|
|
235
|
|
|
return intend([ |
236
|
|
|
'url' => route('adminarea.tenants.index'), |
237
|
|
|
'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/tenants::common.tenant'), 'identifier' => $tenant->name])], |
238
|
|
|
]); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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.