Completed
Push — develop ( 242664...eb47a8 )
by Abdelrahman
16:40
created

AdminsController::import()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Cortex\Foundation\DataTables\ImportLogsDataTable;
8
use Cortex\Foundation\Http\Requests\ImportFormRequest;
9
use Cortex\Foundation\Importers\DefaultImporter;
10
use Illuminate\Http\Request;
11
use Cortex\Auth\Models\Admin;
12
use Illuminate\Foundation\Http\FormRequest;
13
use Cortex\Foundation\DataTables\LogsDataTable;
14
use Cortex\Auth\DataTables\Adminarea\AdminsDataTable;
15
use Cortex\Foundation\DataTables\ActivitiesDataTable;
16
use Cortex\Auth\Http\Requests\Adminarea\AdminFormRequest;
17
use Cortex\Foundation\Http\Controllers\AuthorizedController;
18
use Cortex\Auth\Http\Requests\Adminarea\AdminAttributesFormRequest;
19
20
class AdminsController extends AuthorizedController
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected $resource = Admin::class;
26
27
    /**
28
     * List all admins.
29
     *
30
     * @param \Cortex\Auth\DataTables\Adminarea\AdminsDataTable $adminsDataTable
31
     *
32
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
33
     */
34
    public function index(AdminsDataTable $adminsDataTable)
35
    {
36
        return $adminsDataTable->with([
37
            'id' => 'adminarea-admins-index-table',
38
            'phrase' => trans('cortex/auth::common.admins'),
39
        ])->render('cortex/foundation::adminarea.pages.datatable');
40
    }
41
42
    /**
43
     * List admin logs.
44
     *
45
     * @param \Cortex\Auth\Models\Admin                   $admin
46
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
47
     *
48
     * @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...
49
     */
50
    public function logs(Admin $admin, LogsDataTable $logsDataTable)
51
    {
52
        return $logsDataTable->with([
53
            'resource' => $admin,
54
            'tabs' => 'adminarea.admins.tabs',
55
            'phrase' => trans('cortex/auth::common.admins'),
56
            'id' => "adminarea-admins-{$admin->getKey()}-logs-table",
57
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
58
    }
59
60
    /**
61
     * Get a listing of the resource activities.
62
     *
63
     * @param \Cortex\Auth\Models\Admin                         $admin
64
     * @param \Cortex\Foundation\DataTables\ActivitiesDataTable $activitiesDataTable
65
     *
66
     * @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...
67
     */
68
    public function activities(Admin $admin, ActivitiesDataTable $activitiesDataTable)
69
    {
70
        return $activitiesDataTable->with([
71
            'resource' => $admin,
72
            'tabs' => 'adminarea.admins.tabs',
73
            'phrase' => trans('cortex/auth::common.admins'),
74
            'id' => "adminarea-admins-{$admin->getKey()}-activities-table",
75
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
76
    }
77
78
    /**
79
     * Show the form for create/update of the given resource attributes.
80
     *
81
     * @param \Illuminate\Http\Request  $request
82
     * @param \Cortex\Auth\Models\Admin $admin
83
     *
84
     * @return \Illuminate\View\View
85
     */
86
    public function attributes(Request $request, Admin $admin)
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...
87
    {
88
        return view('cortex/auth::adminarea.pages.admin-attributes', compact('admin'));
89
    }
90
91
    /**
92
     * Process the account update form.
93
     *
94
     * @param \Cortex\Auth\Http\Requests\Adminarea\AdminAttributesFormRequest $request
95
     * @param \Cortex\Auth\Models\Admin                                       $admin
96
     *
97
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
98
     */
99
    public function updateAttributes(AdminAttributesFormRequest $request, Admin $admin)
100
    {
101
        $data = $request->validated();
102
103
        // Update profile
104
        $admin->fill($data)->save();
105
106
        return intend([
107
            'back' => true,
108
            'with' => ['success' => trans('cortex/auth::messages.account.updated_attributes')],
109
        ]);
110
    }
111
112
    /**
113
     * Import admins.
114
     *
115
     * @return \Illuminate\View\View
116
     */
117
    public function import()
118
    {
119
        return view('cortex/foundation::adminarea.pages.import', [
120
            'id' => 'adminarea-admins-import',
121
            'tabs' => 'adminarea.admins.tabs',
122
            'url' => route('adminarea.admins.hoard'),
123
            'phrase' => trans('cortex/auth::common.admins'),
124
        ]);
125
    }
126
127
    /**
128
     * Hoard admins.
129
     *
130
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
131
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
132
     *
133
     * @return void
134
     */
135
    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...
136
    {
137
        // Handle the import
138
        $importer->config['resource'] = $this->resource;
139
        $importer->config['name'] = 'username';
140
        $importer->handleImport();
141
    }
142
143
    /**
144
     * List admin import logs.
145
     *
146
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
147
     *
148
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
149
     */
150
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
151
    {
152
        return $importLogsDatatable->with([
153
            'resource' => 'admin',
154
            'tabs' => 'adminarea.admins.tabs',
155
            'id' => 'adminarea-admins-import-logs-table',
156
            'phrase' => trans('cortex/admins::common.admins'),
157
        ])->render('cortex/foundation::adminarea.pages.datatable-import-logs');
158
    }
159
160
    /**
161
     * Create new admin.
162
     *
163
     * @param \Illuminate\Http\Request  $request
164
     * @param \Cortex\Auth\Models\Admin $admin
165
     *
166
     * @return \Illuminate\View\View
167
     */
168
    public function create(Request $request, Admin $admin)
169
    {
170
        return $this->form($request, $admin);
171
    }
172
173
    /**
174
     * Edit given admin.
175
     *
176
     * @param \Illuminate\Http\Request  $request
177
     * @param \Cortex\Auth\Models\Admin $admin
178
     *
179
     * @return \Illuminate\View\View
180
     */
181
    public function edit(Request $request, Admin $admin)
182
    {
183
        return $this->form($request, $admin);
184
    }
185
186
    /**
187
     * Show admin create/edit form.
188
     *
189
     * @param \Illuminate\Http\Request  $request
190
     * @param \Cortex\Auth\Models\Admin $admin
191
     *
192
     * @return \Illuminate\View\View
193
     */
194
    protected function form(Request $request, Admin $admin)
195
    {
196
        $countries = collect(countries())->map(function ($country, $code) {
197
            return [
198
                'id' => $code,
199
                'text' => $country['name'],
200
                'emoji' => $country['emoji'],
201
            ];
202
        })->values();
203
        $currentUser = $request->user($this->getGuard());
204
        $languages = collect(languages())->pluck('name', 'iso_639_1');
205
        $genders = ['male' => trans('cortex/auth::common.male'), 'female' => trans('cortex/auth::common.female')];
206
207
        $roles = $currentUser->can('superadmin')
208
            ? app('cortex.auth.role')->all()->pluck('name', 'id')->toArray()
209
            : $currentUser->roles->pluck('name', 'id')->toArray();
210
211
        $abilities = $currentUser->can('superadmin')
212
            ? app('cortex.auth.ability')->all()->groupBy('entity_type')->map->pluck('title', 'id')->toArray()
213
            : $currentUser->getAbilities()->groupBy('entity_type')->map->pluck('title', 'id')->toArray();
214
215
        asort($roles);
216
        ksort($abilities);
217
218
        return view('cortex/auth::adminarea.pages.admin', compact('admin', 'abilities', 'roles', 'countries', 'languages', 'genders'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
219
    }
220
221
    /**
222
     * Store new admin.
223
     *
224
     * @param \Cortex\Auth\Http\Requests\Adminarea\AdminFormRequest $request
225
     * @param \Cortex\Auth\Models\Admin                             $admin
226
     *
227
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
228
     */
229
    public function store(AdminFormRequest $request, Admin $admin)
230
    {
231
        return $this->process($request, $admin);
232
    }
233
234
    /**
235
     * Update given admin.
236
     *
237
     * @param \Cortex\Auth\Http\Requests\Adminarea\AdminFormRequest $request
238
     * @param \Cortex\Auth\Models\Admin                             $admin
239
     *
240
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
241
     */
242
    public function update(AdminFormRequest $request, Admin $admin)
243
    {
244
        return $this->process($request, $admin);
245
    }
246
247
    /**
248
     * Process stored/updated admin.
249
     *
250
     * @param \Illuminate\Foundation\Http\FormRequest $request
251
     * @param \Cortex\Auth\Models\Admin               $admin
252
     *
253
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
254
     */
255
    protected function process(FormRequest $request, Admin $admin)
256
    {
257
        // Prepare required input fields
258
        $data = $request->validated();
259
260
        ! $request->hasFile('profile_picture')
261
        || $admin->addMediaFromRequest('profile_picture')
262
                ->sanitizingFileName(function ($fileName) {
263
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
264
                })
265
                ->toMediaCollection('profile_picture', config('cortex.auth.media.disk'));
266
267
        ! $request->hasFile('cover_photo')
268
        || $admin->addMediaFromRequest('cover_photo')
269
                ->sanitizingFileName(function ($fileName) {
270
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
271
                })
272
                ->toMediaCollection('cover_photo', config('cortex.auth.media.disk'));
273
274
        // Save admin
275
        $admin->fill($data)->save();
276
277
        return intend([
278
            'url' => route('adminarea.admins.index'),
279
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'admin', 'id' => $admin->username])],
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...
280
        ]);
281
    }
282
283
    /**
284
     * Destroy given admin.
285
     *
286
     * @param \Cortex\Auth\Models\Admin $admin
287
     *
288
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
289
     */
290
    public function destroy(Admin $admin)
291
    {
292
        $admin->delete();
293
294
        return intend([
295
            'url' => route('adminarea.admins.index'),
296
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'admin', 'id' => $admin->username])],
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...
297
        ]);
298
    }
299
}
300