1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Attributes\Http\Controllers\Adminarea; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Cortex\Attributes\Models\Attribute; |
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
|
|
|
* Show attribute create/edit form. |
139
|
|
|
* |
140
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
141
|
|
|
* |
142
|
|
|
* @return \Illuminate\View\View |
143
|
|
|
*/ |
144
|
|
|
protected function form(Attribute $attribute) |
145
|
|
|
{ |
146
|
|
|
$groups = app('rinvex.attributes.attribute')->distinct()->get(['group'])->pluck('group', 'group')->toArray(); |
147
|
|
|
$entities = array_combine(app('rinvex.attributes.entities')->toArray(), app('rinvex.attributes.entities')->toArray()); |
148
|
|
|
$types = array_combine($typeKeys = array_keys(Attribute::typeMap()), array_map(function ($item) { |
149
|
|
|
return trans('cortex/attributes::common.'.$item); |
150
|
|
|
}, $typeKeys)); |
151
|
|
|
|
152
|
|
|
asort($types); |
153
|
|
|
asort($groups); |
154
|
|
|
asort($entities); |
155
|
|
|
|
156
|
|
|
return view('cortex/attributes::adminarea.pages.attribute', compact('attribute', 'groups', 'entities', 'types')); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Store new attribute. |
161
|
|
|
* |
162
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
163
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
164
|
|
|
* |
165
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
166
|
|
|
*/ |
167
|
|
|
public function store(AttributeFormRequest $request, Attribute $attribute) |
168
|
|
|
{ |
169
|
|
|
return $this->process($request, $attribute); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Update given attribute. |
174
|
|
|
* |
175
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
176
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
177
|
|
|
* |
178
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
179
|
|
|
*/ |
180
|
|
|
public function update(AttributeFormRequest $request, Attribute $attribute) |
181
|
|
|
{ |
182
|
|
|
return $this->process($request, $attribute); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Process stored/updated attribute. |
187
|
|
|
* |
188
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
189
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
190
|
|
|
* |
191
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
192
|
|
|
*/ |
193
|
|
|
protected function process(FormRequest $request, Attribute $attribute) |
194
|
|
|
{ |
195
|
|
|
// Prepare required input fields |
196
|
|
|
$data = $request->validated(); |
197
|
|
|
|
198
|
|
|
// Save attribute |
199
|
|
|
$attribute->fill($data)->save(); |
200
|
|
|
|
201
|
|
|
return intend([ |
202
|
|
|
'url' => route('adminarea.attributes.index'), |
203
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/attributes::common.attribute'), 'identifier' => $attribute->name])], |
204
|
|
|
]); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Destroy given attribute. |
209
|
|
|
* |
210
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
211
|
|
|
* |
212
|
|
|
* @throws \Exception |
213
|
|
|
* |
214
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
215
|
|
|
*/ |
216
|
|
|
public function destroy(Attribute $attribute) |
217
|
|
|
{ |
218
|
|
|
$attribute->delete(); |
219
|
|
|
|
220
|
|
|
return intend([ |
221
|
|
|
'url' => route('adminarea.attributes.index'), |
222
|
|
|
'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/attributes::common.attribute'), 'identifier' => $attribute->name])], |
223
|
|
|
]); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
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.