|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Contacts\Http\Controllers\Adminarea; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Cortex\Contacts\Models\Contact; |
|
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\Contacts\DataTables\Adminarea\ContactsDataTable; |
|
16
|
|
|
use Cortex\Foundation\Http\Controllers\AuthorizedController; |
|
17
|
|
|
use Cortex\Contacts\Http\Requests\Adminarea\ContactFormRequest; |
|
18
|
|
|
|
|
19
|
|
|
class ContactsController extends AuthorizedController |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $resource = Contact::class; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* List all contacts. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Cortex\Contacts\DataTables\Adminarea\ContactsDataTable $contactsDataTable |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
32
|
|
|
*/ |
|
33
|
|
|
public function index(ContactsDataTable $contactsDataTable) |
|
34
|
|
|
{ |
|
35
|
|
|
return $contactsDataTable->with([ |
|
36
|
|
|
'id' => 'adminarea-contacts-index-table', |
|
37
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-index'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* List contact logs. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Cortex\Contacts\Models\Contact $contact |
|
44
|
|
|
* @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
public function logs(Contact $contact, LogsDataTable $logsDataTable) |
|
49
|
|
|
{ |
|
50
|
|
|
return $logsDataTable->with([ |
|
51
|
|
|
'resource' => $contact, |
|
52
|
|
|
'tabs' => 'adminarea.contacts.tabs', |
|
53
|
|
|
'id' => "adminarea-contacts-{$contact->getRouteKey()}-logs-table", |
|
54
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-tab'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Import contacts. |
|
59
|
|
|
* |
|
60
|
|
|
* @param \Cortex\Contacts\Models\Contact $contact |
|
61
|
|
|
* @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Illuminate\View\View |
|
64
|
|
|
*/ |
|
65
|
|
|
public function import(Contact $contact, ImportRecordsDataTable $importRecordsDataTable) |
|
66
|
|
|
{ |
|
67
|
|
|
return $importRecordsDataTable->with([ |
|
68
|
|
|
'resource' => $contact, |
|
69
|
|
|
'tabs' => 'adminarea.contacts.tabs', |
|
70
|
|
|
'url' => route('adminarea.contacts.stash'), |
|
71
|
|
|
'id' => "adminarea-contacts-{$contact->getRouteKey()}-import-table", |
|
72
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-dropzone'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Stash contacts. |
|
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, Contact $contact, DefaultImporter $importer) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
// Handle the import |
|
86
|
|
|
$importer->config['resource'] = $contact; |
|
87
|
|
|
$importer->handleImport(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Hoard contacts. |
|
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.contacts.contact')->getFillable()))->toArray(); |
|
104
|
|
|
|
|
105
|
|
|
tap(app('rinvex.contacts.contact')->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 contact 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/contacts::common.contact'), |
|
132
|
|
|
'tabs' => 'adminarea.contacts.tabs', |
|
133
|
|
|
'id' => 'adminarea-contacts-import-logs-table', |
|
134
|
|
|
])->render('cortex/foundation::adminarea.pages.datatable-tab'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Show contact create/edit form. |
|
139
|
|
|
* |
|
140
|
|
|
* @param \Cortex\Contacts\Models\Contact $contact |
|
141
|
|
|
* |
|
142
|
|
|
* @return \Illuminate\View\View |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function form(Contact $contact) |
|
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
|
|
|
$sources = app('rinvex.contacts.contact')->distinct()->get(['source'])->pluck('source', 'source')->toArray(); |
|
157
|
|
|
$methods = app('rinvex.contacts.contact')->distinct()->get(['method'])->pluck('method', 'method')->toArray(); |
|
158
|
|
|
$genders = ['male' => trans('cortex/contacts::common.male'), 'female' => trans('cortex/contacts::common.female')]; |
|
159
|
|
|
|
|
160
|
|
|
return view('cortex/contacts::adminarea.pages.contact', compact('contact', 'genders', 'countries', 'languages', 'sources', 'methods', 'tags')); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Store new contact. |
|
165
|
|
|
* |
|
166
|
|
|
* @param \Cortex\Contacts\Http\Requests\Adminarea\ContactFormRequest $request |
|
167
|
|
|
* @param \Cortex\Contacts\Models\Contact $contact |
|
168
|
|
|
* |
|
169
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
170
|
|
|
*/ |
|
171
|
|
|
public function store(ContactFormRequest $request, Contact $contact) |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->process($request, $contact); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Update given contact. |
|
178
|
|
|
* |
|
179
|
|
|
* @param \Cortex\Contacts\Http\Requests\Adminarea\ContactFormRequest $request |
|
180
|
|
|
* @param \Cortex\Contacts\Models\Contact $contact |
|
181
|
|
|
* |
|
182
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
183
|
|
|
*/ |
|
184
|
|
|
public function update(ContactFormRequest $request, Contact $contact) |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->process($request, $contact); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Process stored/updated contact. |
|
191
|
|
|
* |
|
192
|
|
|
* @param \Illuminate\Foundation\Http\FormRequest $request |
|
193
|
|
|
* @param \Cortex\Contacts\Models\Contact $contact |
|
194
|
|
|
* |
|
195
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function process(FormRequest $request, Contact $contact) |
|
198
|
|
|
{ |
|
199
|
|
|
// Prepare required input fields |
|
200
|
|
|
$data = $request->validated(); |
|
201
|
|
|
|
|
202
|
|
|
// Save contact |
|
203
|
|
|
$contact->fill($data)->save(); |
|
204
|
|
|
|
|
205
|
|
|
return intend([ |
|
206
|
|
|
'url' => route('adminarea.contacts.index'), |
|
207
|
|
|
'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/contacts::common.contact'), 'identifier' => $contact->full_name])], |
|
208
|
|
|
]); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Destroy given contact. |
|
213
|
|
|
* |
|
214
|
|
|
* @param \Cortex\Contacts\Models\Contact $contact |
|
215
|
|
|
* |
|
216
|
|
|
* @throws \Exception |
|
217
|
|
|
* |
|
218
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
|
219
|
|
|
*/ |
|
220
|
|
|
public function destroy(Contact $contact) |
|
221
|
|
|
{ |
|
222
|
|
|
$contact->delete(); |
|
223
|
|
|
|
|
224
|
|
|
return intend([ |
|
225
|
|
|
'url' => route('adminarea.contacts.index'), |
|
226
|
|
|
'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/contacts::common.contact'), 'identifier' => $contact->full_name])], |
|
227
|
|
|
]); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
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.