|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Attributes\Http\Controllers\Adminarea; |
|
6
|
|
|
|
|
7
|
|
|
use Cortex\Attributes\Models\Attribute; |
|
8
|
|
|
use Exception; |
|
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\Foundation\Http\Controllers\AuthorizedController; |
|
16
|
|
|
use Cortex\Attributes\DataTables\Adminarea\AttributesDataTable; |
|
17
|
|
|
use Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest; |
|
18
|
|
|
|
|
19
|
|
|
class AttributesController extends AuthorizedController |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $resource = Attribute::class; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* List all attributes. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Cortex\Attributes\DataTables\Adminarea\AttributesDataTable $attributesDataTable |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
32
|
|
|
*/ |
|
33
|
|
|
public function index(AttributesDataTable $attributesDataTable) |
|
34
|
|
|
{ |
|
35
|
|
|
return $attributesDataTable->with([ |
|
36
|
|
|
'id' => 'adminarea-attributes-index-table', |
|
37
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-index'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* List attribute logs. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
44
|
|
|
* @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
public function logs(Attribute $attribute, LogsDataTable $logsDataTable) |
|
49
|
|
|
{ |
|
50
|
|
|
return $logsDataTable->with([ |
|
51
|
|
|
'resource' => $attribute, |
|
52
|
|
|
'tabs' => 'adminarea.attributes.tabs', |
|
53
|
|
|
'id' => "adminarea-attributes-{$attribute->getRouteKey()}-logs-table", |
|
54
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-tab'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Import attributes. |
|
59
|
|
|
* |
|
60
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
61
|
|
|
* @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Illuminate\View\View |
|
64
|
|
|
*/ |
|
65
|
|
|
public function import(Attribute $attribute, ImportRecordsDataTable $importRecordsDataTable) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
return $importRecordsDataTable->with([ |
|
68
|
|
|
'resource' => $attribute, |
|
69
|
|
|
'tabs' => 'adminarea.attributes.tabs', |
|
70
|
|
|
'url' => route('adminarea.attributes.stash'), |
|
71
|
|
|
'id' => "adminarea-attributes-{$attribute->getRouteKey()}-import-table", |
|
72
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-dropzone'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Stash attributes. |
|
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 attributes. |
|
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.attributes.attribute')->getFillable()))->toArray(); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
tap(app('rinvex.attributes.attribute')->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 attribute 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/attributes::common.attribute'), |
|
132
|
|
|
'tabs' => 'adminarea.attributes.tabs', |
|
133
|
|
|
'id' => 'adminarea-attributes-import-logs-table', |
|
134
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-tab'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Create new attribute. |
|
139
|
|
|
* |
|
140
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
141
|
|
|
* |
|
142
|
|
|
* @return \Illuminate\View\View |
|
143
|
|
|
*/ |
|
144
|
|
|
public function create(Attribute $attribute) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->form($attribute); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Edit given attribute. |
|
151
|
|
|
* |
|
152
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
153
|
|
|
* |
|
154
|
|
|
* @return \Illuminate\View\View |
|
155
|
|
|
*/ |
|
156
|
|
|
public function edit(Attribute $attribute) |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->form($attribute); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Show attribute create/edit form. |
|
163
|
|
|
* |
|
164
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
165
|
|
|
* |
|
166
|
|
|
* @return \Illuminate\View\View |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function form(Attribute $attribute) |
|
169
|
|
|
{ |
|
170
|
|
|
$groups = app('rinvex.attributes.attribute')->distinct()->get(['group'])->pluck('group', 'group')->toArray(); |
|
171
|
|
|
$entities = array_combine(app('rinvex.attributes.entities')->toArray(), app('rinvex.attributes.entities')->toArray()); |
|
|
|
|
|
|
172
|
|
|
$types = array_combine($typeKeys = array_keys(Attribute::typeMap()), array_map(function ($item) { |
|
173
|
|
|
return trans('cortex/attributes::common.'.$item); |
|
174
|
|
|
}, $typeKeys)); |
|
175
|
|
|
|
|
176
|
|
|
asort($types); |
|
177
|
|
|
asort($groups); |
|
178
|
|
|
asort($entities); |
|
179
|
|
|
|
|
180
|
|
|
return view('cortex/attributes::adminarea.pages.attribute', compact('attribute', 'groups', 'entities', 'types')); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Store new attribute. |
|
185
|
|
|
* |
|
186
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
|
187
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
188
|
|
|
* |
|
189
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
190
|
|
|
*/ |
|
191
|
|
|
public function store(AttributeFormRequest $request, Attribute $attribute) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->process($request, $attribute); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Update given attribute. |
|
198
|
|
|
* |
|
199
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
|
200
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
201
|
|
|
* |
|
202
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
203
|
|
|
*/ |
|
204
|
|
|
public function update(AttributeFormRequest $request, Attribute $attribute) |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->process($request, $attribute); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Process stored/updated attribute. |
|
211
|
|
|
* |
|
212
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
|
213
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
214
|
|
|
* |
|
215
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function process(FormRequest $request, Attribute $attribute) |
|
218
|
|
|
{ |
|
219
|
|
|
// Prepare required input fields |
|
220
|
|
|
$data = $request->validated(); |
|
221
|
|
|
|
|
222
|
|
|
// Save attribute |
|
223
|
|
|
$attribute->fill($data)->save(); |
|
224
|
|
|
|
|
225
|
|
|
return intend([ |
|
226
|
|
|
'url' => route('adminarea.attributes.index'), |
|
227
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/attributes::common.attribute'), 'identifier' => $attribute->name])], |
|
|
|
|
|
|
228
|
|
|
]); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Destroy given attribute. |
|
233
|
|
|
* |
|
234
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
235
|
|
|
* |
|
236
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
237
|
|
|
*/ |
|
238
|
|
|
public function destroy(Attribute $attribute) |
|
239
|
|
|
{ |
|
240
|
|
|
$attribute->delete(); |
|
241
|
|
|
|
|
242
|
|
|
return intend([ |
|
243
|
|
|
'url' => route('adminarea.attributes.index'), |
|
244
|
|
|
'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/attributes::common.attribute'), 'identifier' => $attribute->name])], |
|
|
|
|
|
|
245
|
|
|
]); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.