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 Illuminate\Foundation\Http\FormRequest; |
9
|
|
|
use Cortex\Foundation\DataTables\LogsDataTable; |
10
|
|
|
use Cortex\Foundation\Importers\DefaultImporter; |
11
|
|
|
use Cortex\Foundation\DataTables\ImportLogsDataTable; |
12
|
|
|
use Cortex\Foundation\Http\Requests\ImportFormRequest; |
13
|
|
|
use Cortex\Foundation\Http\Controllers\AuthorizedController; |
14
|
|
|
use Cortex\Attributes\DataTables\Adminarea\AttributesDataTable; |
15
|
|
|
use Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest; |
16
|
|
|
|
17
|
|
|
class AttributesController extends AuthorizedController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
protected $resource = Attribute::class; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List all attributes. |
26
|
|
|
* |
27
|
|
|
* @param \Cortex\Attributes\DataTables\Adminarea\AttributesDataTable $attributesDataTable |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
30
|
|
|
*/ |
31
|
|
|
public function index(AttributesDataTable $attributesDataTable) |
32
|
|
|
{ |
33
|
|
|
return $attributesDataTable->with([ |
34
|
|
|
'id' => 'adminarea-attributes-index-table', |
35
|
|
|
'phrase' => trans('cortex/attributes::common.attributes'), |
36
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List attribute logs. |
41
|
|
|
* |
42
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
43
|
|
|
* @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function logs(Attribute $attribute, LogsDataTable $logsDataTable) |
48
|
|
|
{ |
49
|
|
|
return $logsDataTable->with([ |
50
|
|
|
'resource' => $attribute, |
51
|
|
|
'tabs' => 'adminarea.attributes.tabs', |
52
|
|
|
'phrase' => trans('cortex/attributes::common.attributes'), |
53
|
|
|
'id' => "adminarea-attributes-{$attribute->getKey()}-logs-table", |
54
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-logs'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Import attributes. |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\View\View |
61
|
|
|
*/ |
62
|
|
|
public function import() |
63
|
|
|
{ |
64
|
|
|
return view('cortex/foundation::adminarea.pages.import', [ |
65
|
|
|
'id' => 'adminarea-attributes-import', |
66
|
|
|
'tabs' => 'adminarea.attributes.tabs', |
67
|
|
|
'url' => route('adminarea.attributes.hoard'), |
68
|
|
|
'phrase' => trans('cortex/attributes::common.attributes'), |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Hoard attributes. |
74
|
|
|
* |
75
|
|
|
* @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request |
76
|
|
|
* @param \Cortex\Foundation\Importers\DefaultImporter $importer |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function hoard(ImportFormRequest $request, DefaultImporter $importer) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
// Handle the import |
83
|
|
|
$importer->config['resource'] = $this->resource; |
84
|
|
|
$importer->handleImport(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* List attribute import logs. |
89
|
|
|
* |
90
|
|
|
* @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable |
91
|
|
|
* |
92
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
93
|
|
|
*/ |
94
|
|
|
public function importLogs(ImportLogsDataTable $importLogsDatatable) |
95
|
|
|
{ |
96
|
|
|
return $importLogsDatatable->with([ |
97
|
|
|
'resource' => 'attribute', |
98
|
|
|
'tabs' => 'adminarea.attributes.tabs', |
99
|
|
|
'id' => 'adminarea-attributes-import-logs-table', |
100
|
|
|
'phrase' => trans('cortex/attributes::common.attributes'), |
101
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-import-logs'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create new attribute. |
106
|
|
|
* |
107
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\View\View |
110
|
|
|
*/ |
111
|
|
|
public function create(Attribute $attribute) |
112
|
|
|
{ |
113
|
|
|
return $this->form($attribute); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Edit given attribute. |
118
|
|
|
* |
119
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\View\View |
122
|
|
|
*/ |
123
|
|
|
public function edit(Attribute $attribute) |
124
|
|
|
{ |
125
|
|
|
return $this->form($attribute); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Show attribute create/edit form. |
130
|
|
|
* |
131
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
132
|
|
|
* |
133
|
|
|
* @return \Illuminate\View\View |
134
|
|
|
*/ |
135
|
|
|
protected function form(Attribute $attribute) |
136
|
|
|
{ |
137
|
|
|
$groups = app('rinvex.attributes.attribute')->distinct()->get(['group'])->pluck('group', 'group')->toArray(); |
138
|
|
|
$entities = array_combine(app('rinvex.attributes.entities')->toArray(), app('rinvex.attributes.entities')->toArray()); |
|
|
|
|
139
|
|
|
$types = array_combine($typeKeys = array_keys(Attribute::typeMap()), array_map(function ($item) { |
140
|
|
|
return trans('cortex/attributes::common.'.$item); |
141
|
|
|
}, $typeKeys)); |
142
|
|
|
|
143
|
|
|
asort($types); |
144
|
|
|
asort($groups); |
145
|
|
|
asort($entities); |
146
|
|
|
|
147
|
|
|
return view('cortex/attributes::adminarea.pages.attribute', compact('attribute', 'groups', 'entities', 'types')); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Store new attribute. |
152
|
|
|
* |
153
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
154
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
155
|
|
|
* |
156
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
157
|
|
|
*/ |
158
|
|
|
public function store(AttributeFormRequest $request, Attribute $attribute) |
159
|
|
|
{ |
160
|
|
|
return $this->process($request, $attribute); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Update given attribute. |
165
|
|
|
* |
166
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
167
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
168
|
|
|
* |
169
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
170
|
|
|
*/ |
171
|
|
|
public function update(AttributeFormRequest $request, Attribute $attribute) |
172
|
|
|
{ |
173
|
|
|
return $this->process($request, $attribute); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Process stored/updated attribute. |
178
|
|
|
* |
179
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
180
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
181
|
|
|
* |
182
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
183
|
|
|
*/ |
184
|
|
|
protected function process(FormRequest $request, Attribute $attribute) |
185
|
|
|
{ |
186
|
|
|
// Prepare required input fields |
187
|
|
|
$data = $request->validated(); |
188
|
|
|
|
189
|
|
|
// Save attribute |
190
|
|
|
$attribute->fill($data)->save(); |
191
|
|
|
|
192
|
|
|
return intend([ |
193
|
|
|
'url' => route('adminarea.attributes.index'), |
194
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'attribute', 'id' => $attribute->name])], |
|
|
|
|
195
|
|
|
]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Destroy given attribute. |
200
|
|
|
* |
201
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
202
|
|
|
* |
203
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
204
|
|
|
*/ |
205
|
|
|
public function destroy(Attribute $attribute) |
206
|
|
|
{ |
207
|
|
|
$attribute->delete(); |
208
|
|
|
|
209
|
|
|
return intend([ |
210
|
|
|
'url' => route('adminarea.attributes.index'), |
211
|
|
|
'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'attribute', 'id' => $attribute->name])], |
|
|
|
|
212
|
|
|
]); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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.