Completed
Push — develop ( 2af228...a3b53b )
by Abdelrahman
12:12
created

ContactsController::hoard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Contacts\Http\Controllers\Adminarea;
6
7
use Cortex\Contacts\Models\Contact;
8
use Cortex\Foundation\DataTables\ImportLogsDataTable;
9
use Cortex\Foundation\Http\Requests\ImportFormRequest;
10
use Cortex\Foundation\Importers\DefaultImporter;
11
use Illuminate\Foundation\Http\FormRequest;
12
use Cortex\Foundation\DataTables\LogsDataTable;
13
use Cortex\Contacts\DataTables\Adminarea\ContactsDataTable;
14
use Cortex\Foundation\Http\Controllers\AuthorizedController;
15
use Cortex\Contacts\Http\Requests\Adminarea\ContactFormRequest;
16
17
class ContactsController extends AuthorizedController
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resource = Contact::class;
23
24
    /**
25
     * List all contacts.
26
     *
27
     * @param \Cortex\Contacts\DataTables\Adminarea\ContactsDataTable $contactsDataTable
28
     *
29
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
30
     */
31
    public function index(ContactsDataTable $contactsDataTable)
32
    {
33
        return $contactsDataTable->with([
34
            'id' => 'adminarea-contacts-index-table',
35
            'phrase' => trans('cortex/contacts::common.contacts'),
36
        ])->render('cortex/foundation::adminarea.pages.datatable');
37
    }
38
39
    /**
40
     * List contact logs.
41
     *
42
     * @param \Cortex\Contacts\Models\Contact             $contact
43
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
44
     *
45
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

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.

Loading history...
46
     */
47
    public function logs(Contact $contact, LogsDataTable $logsDataTable)
48
    {
49
        return $logsDataTable->with([
50
            'resource' => $contact,
51
            'tabs' => 'adminarea.contacts.tabs',
52
            'phrase' => trans('cortex/contacts::common.contacts'),
53
            'id' => "adminarea-contacts-{$contact->getKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
55
    }
56
57
    /**
58
     * Import contacts.
59
     *
60
     * @return \Illuminate\View\View
61
     */
62
    public function import()
63
    {
64
        return view('cortex/foundation::adminarea.pages.import', [
65
            'id' => 'adminarea-contacts-import',
66
            'tabs' => 'adminarea.contacts.tabs',
67
            'url' => route('adminarea.contacts.hoard'),
68
            'phrase' => trans('cortex/contacts::common.contacts'),
69
        ]);
70
    }
71
72
    /**
73
     * Hoard contacts.
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)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        // Handle the import
83
        $importer->config['resource'] = $this->resource;
84
        $importer->handleImport();
85
    }
86
87
    /**
88
     * List contact 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' => 'contact',
98
            'tabs' => 'adminarea.contacts.tabs',
99
            'id' => 'adminarea-contacts-import-logs-table',
100
            'phrase' => trans('cortex/contacts::common.contacts'),
101
        ])->render('cortex/foundation::adminarea.pages.datatable-import-logs');
102
    }
103
104
    /**
105
     * Create new contact.
106
     *
107
     * @param \Cortex\Contacts\Models\Contact $contact
108
     *
109
     * @return \Illuminate\View\View
110
     */
111
    public function create(Contact $contact)
112
    {
113
        return $this->form($contact);
114
    }
115
116
    /**
117
     * Edit given contact.
118
     *
119
     * @param \Cortex\Contacts\Models\Contact $contact
120
     *
121
     * @return \Illuminate\View\View
122
     */
123
    public function edit(Contact $contact)
124
    {
125
        return $this->form($contact);
126
    }
127
128
    /**
129
     * Show contact create/edit form.
130
     *
131
     * @param \Cortex\Contacts\Models\Contact $contact
132
     *
133
     * @return \Illuminate\View\View
134
     */
135
    protected function form(Contact $contact)
136
    {
137
        $countries = collect(countries())->map(function ($country, $code) {
138
            return [
139
                'id' => $code,
140
                'text' => $country['name'],
141
                'emoji' => $country['emoji'],
142
            ];
143
        })->values();
144
        $languages = collect(languages())->pluck('name', 'iso_639_1');
145
        $sources = app('rinvex.contacts.contact')->distinct()->get(['source'])->pluck('source', 'source')->toArray();
146
        $methods = app('rinvex.contacts.contact')->distinct()->get(['method'])->pluck('method', 'method')->toArray();
147
        $genders = ['male' => trans('cortex/contacts::common.male'), 'female' => trans('cortex/contacts::common.female')];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
148
149
        return view('cortex/contacts::adminarea.pages.contact', compact('contact', 'genders', 'countries', 'languages', 'sources', 'methods'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
150
    }
151
152
    /**
153
     * Store new contact.
154
     *
155
     * @param \Cortex\Contacts\Http\Requests\Adminarea\ContactFormRequest $request
156
     * @param \Cortex\Contacts\Models\Contact                             $contact
157
     *
158
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
159
     */
160
    public function store(ContactFormRequest $request, Contact $contact)
161
    {
162
        return $this->process($request, $contact);
163
    }
164
165
    /**
166
     * Update given contact.
167
     *
168
     * @param \Cortex\Contacts\Http\Requests\Adminarea\ContactFormRequest $request
169
     * @param \Cortex\Contacts\Models\Contact                             $contact
170
     *
171
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
172
     */
173
    public function update(ContactFormRequest $request, Contact $contact)
174
    {
175
        return $this->process($request, $contact);
176
    }
177
178
    /**
179
     * Process stored/updated contact.
180
     *
181
     * @param \Illuminate\Foundation\Http\FormRequest $request
182
     * @param \Cortex\Contacts\Models\Contact         $contact
183
     *
184
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
185
     */
186
    protected function process(FormRequest $request, Contact $contact)
187
    {
188
        // Prepare required input fields
189
        $data = $request->validated();
190
191
        // Save contact
192
        $contact->fill($data)->save();
193
194
        return intend([
195
            'url' => route('adminarea.contacts.index'),
196
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'contact', 'id' => $contact->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
197
        ]);
198
    }
199
200
    /**
201
     * Destroy given contact.
202
     *
203
     * @param \Cortex\Contacts\Models\Contact $contact
204
     *
205
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
206
     */
207
    public function destroy(Contact $contact)
208
    {
209
        $contact->delete();
210
211
        return intend([
212
            'url' => route('adminarea.contacts.index'),
213
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'contact', 'id' => $contact->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
214
        ]);
215
    }
216
}
217