|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Attributes\Http\Controllers\Adminarea; |
|
6
|
|
|
|
|
7
|
|
|
use Rinvex\Attributes\Models\Attribute; |
|
8
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
9
|
|
|
use Cortex\Foundation\DataTables\LogsDataTable; |
|
10
|
|
|
use Cortex\Foundation\Http\Controllers\AuthorizedController; |
|
11
|
|
|
use Cortex\Attributes\DataTables\Adminarea\AttributesDataTable; |
|
12
|
|
|
use Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest; |
|
13
|
|
|
|
|
14
|
|
|
class AttributesController extends AuthorizedController |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $resource = 'attribute'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* List all attributes. |
|
23
|
|
|
* |
|
24
|
|
|
* @param \Cortex\Attributes\DataTables\Adminarea\AttributesDataTable $attributesDataTable |
|
25
|
|
|
* |
|
26
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
27
|
|
|
*/ |
|
28
|
|
|
public function index(AttributesDataTable $attributesDataTable) |
|
29
|
|
|
{ |
|
30
|
|
|
return $attributesDataTable->with([ |
|
31
|
|
|
'id' => 'adminarea-attributes-index-table', |
|
32
|
|
|
'phrase' => trans('cortex/attributes::common.attributes'), |
|
33
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* List attribute logs. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
40
|
|
|
* @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
|
|
public function logs(Attribute $attribute, LogsDataTable $logsDataTable) |
|
45
|
|
|
{ |
|
46
|
|
|
return $logsDataTable->with([ |
|
47
|
|
|
'resource' => $attribute, |
|
48
|
|
|
'tabs' => 'adminarea.attributes.tabs', |
|
49
|
|
|
'phrase' => trans('cortex/attributes::common.attributes'), |
|
50
|
|
|
'id' => "adminarea-attributes-{$attribute->getKey()}-logs-table", |
|
51
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-logs'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create new attribute. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
58
|
|
|
* |
|
59
|
|
|
* @return \Illuminate\View\View |
|
60
|
|
|
*/ |
|
61
|
|
|
public function create(Attribute $attribute) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->form($attribute); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Edit given attribute. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\View\View |
|
72
|
|
|
*/ |
|
73
|
|
|
public function edit(Attribute $attribute) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->form($attribute); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Show attribute create/edit form. |
|
80
|
|
|
* |
|
81
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
82
|
|
|
* |
|
83
|
|
|
* @return \Illuminate\View\View |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function form(Attribute $attribute) |
|
86
|
|
|
{ |
|
87
|
|
|
$groups = app('rinvex.attributes.attribute')->distinct()->get(['group'])->pluck('group', 'group')->toArray(); |
|
88
|
|
|
$entities = array_combine(app('rinvex.attributes.entities')->toArray(), app('rinvex.attributes.entities')->toArray()); |
|
|
|
|
|
|
89
|
|
|
$types = array_combine($typeKeys = array_keys(Attribute::typeMap()), array_map(function ($item) { |
|
90
|
|
|
return trans('cortex/attributes::common.'.$item); |
|
91
|
|
|
}, $typeKeys)); |
|
92
|
|
|
|
|
93
|
|
|
ksort($types); |
|
94
|
|
|
ksort($groups); |
|
95
|
|
|
ksort($entities); |
|
96
|
|
|
|
|
97
|
|
|
return view('cortex/attributes::adminarea.pages.attribute', compact('attribute', 'groups', 'entities', 'types')); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Store new attribute. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
|
104
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
105
|
|
|
* |
|
106
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
107
|
|
|
*/ |
|
108
|
|
|
public function store(AttributeFormRequest $request, Attribute $attribute) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->process($request, $attribute); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Update given attribute. |
|
115
|
|
|
* |
|
116
|
|
|
* @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request |
|
117
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
118
|
|
|
* |
|
119
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
120
|
|
|
*/ |
|
121
|
|
|
public function update(AttributeFormRequest $request, Attribute $attribute) |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->process($request, $attribute); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Process stored/updated attribute. |
|
128
|
|
|
* |
|
129
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
|
130
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
131
|
|
|
* |
|
132
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function process(FormRequest $request, Attribute $attribute) |
|
135
|
|
|
{ |
|
136
|
|
|
// Prepare required input fields |
|
137
|
|
|
$data = $request->validated(); |
|
138
|
|
|
|
|
139
|
|
|
// Save attribute |
|
140
|
|
|
$attribute->fill($data)->save(); |
|
141
|
|
|
|
|
142
|
|
|
return intend([ |
|
143
|
|
|
'url' => route('adminarea.attributes.index'), |
|
144
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'attribute', 'id' => $attribute->slug])], |
|
|
|
|
|
|
145
|
|
|
]); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Destroy given attribute. |
|
150
|
|
|
* |
|
151
|
|
|
* @param \Cortex\Attributes\Models\Attribute $attribute |
|
|
|
|
|
|
152
|
|
|
* |
|
153
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
154
|
|
|
*/ |
|
155
|
|
|
public function destroy(Attribute $attribute) |
|
156
|
|
|
{ |
|
157
|
|
|
$attribute->delete(); |
|
158
|
|
|
|
|
159
|
|
|
return intend([ |
|
160
|
|
|
'url' => route('adminarea.attributes.index'), |
|
161
|
|
|
'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'attribute', 'id' => $attribute->slug])], |
|
|
|
|
|
|
162
|
|
|
]); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|