Completed
Push — master ( 8927df...f70445 )
by Abdelrahman
02:05
created

src/Http/Controllers/Backend/RolesController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Http\Controllers\Backend;
17
18
use Illuminate\Http\Request;
19
use Rinvex\Fort\Models\Role;
20
use Rinvex\Fort\Contracts\RoleRepositoryContract;
21
use Rinvex\Fort\Http\Controllers\AuthorizedController;
22
use Rinvex\Fort\Http\Requests\Backend\RoleStoreRequest;
23
use Rinvex\Fort\Http\Requests\Backend\RoleUpdateRequest;
24
25
class RolesController extends AuthorizedController
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $resource = 'roles';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected $resourceActionWhitelist = ['assign'];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceActionWhitelist exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
36
37
    /**
38
     * The role repository instance.
39
     *
40
     * @var \Rinvex\Fort\Contracts\RoleRepositoryContract
41
     */
42
    protected $roleRepository;
43
44
    /**
45
     * Create a new users controller instance.
46
     *
47
     * @param \Rinvex\Fort\Contracts\RoleRepositoryContract $roleRepository
48
     */
49
    public function __construct(RoleRepositoryContract $roleRepository)
50
    {
51
        parent::__construct();
52
53
        $this->roleRepository = $roleRepository;
54
    }
55
56
    /**
57
     * Display a listing of the resource.
58
     *
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function index()
62
    {
63
        $roles = $this->roleRepository->paginate(config('rinvex.fort.backend.items_per_page'));
64
65
        return view('rinvex/fort::backend/roles.index', compact('roles'));
66
    }
67
68
    /**
69
     * Display the specified resource.
70
     *
71
     * @param \Rinvex\Fort\Models\Role $role
72
     *
73
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
74
     */
75
    public function show(Role $role)
76
    {
77
        $resources = app('rinvex.fort.ability')->findAll()->groupBy('resource');
78
        $actions   = ['list', 'view', 'create', 'update', 'delete', 'import', 'export'];
79
        $columns   = ['resource', 'list', 'view', 'create', 'update', 'delete', 'import', 'export', 'other'];
80
81
        return view('rinvex/fort::backend/roles.show', compact('role', 'resources', 'actions', 'columns'));
82
    }
83
84
    /**
85
     * Show the form for creating a new resource.
86
     *
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function create()
90
    {
91
        return $this->form('create', 'store', $this->roleRepository->createModel());
92
    }
93
94
    /**
95
     * Show the form for copying the given resource.
96
     *
97
     * @param \Rinvex\Fort\Models\Role $role
98
     *
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function copy(Role $role)
102
    {
103
        return $this->form('copy', 'store', $role);
104
    }
105
106
    /**
107
     * Show the form for editing the given resource.
108
     *
109
     * @param \Rinvex\Fort\Models\Role $role
110
     *
111
     * @return \Illuminate\Http\Response
112
     */
113
    public function edit(Role $role)
114
    {
115
        return $this->form('edit', 'update', $role);
116
    }
117
118
    /**
119
     * Store a newly created resource in storage.
120
     *
121
     * @param \Rinvex\Fort\Http\Requests\Backend\RoleStoreRequest $request
122
     *
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function store(RoleStoreRequest $request)
126
    {
127
        return $this->process($request);
128
    }
129
130
    /**
131
     * Update the given resource in storage.
132
     *
133
     * @param \Rinvex\Fort\Http\Requests\Backend\RoleUpdateRequest $request
134
     * @param \Rinvex\Fort\Models\Role                             $role
135
     *
136
     * @return \Illuminate\Http\Response
137
     */
138
    public function update(RoleUpdateRequest $request, Role $role)
139
    {
140
        return $this->process($request, $role);
141
    }
142
143
    /**
144
     * Delete the given resource from storage.
145
     *
146
     * @param \Rinvex\Fort\Models\Role $role
147
     *
148
     * @return \Illuminate\Http\Response
149
     */
150
    public function delete(Role $role)
151
    {
152
        $result = $this->roleRepository->delete($role);
153
154
        return intend([
155
            'route' => 'rinvex.fort.backend.roles.index',
156
            'with'  => ['rinvex.fort.alert.warning' => trans('rinvex/fort::backend/messages.role.deleted', ['roleId' => $result->id])],
157
        ]);
158
    }
159
160
    /**
161
     * Import the given resources into storage.
162
     *
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function import()
166
    {
167
        //
168
    }
169
170
    /**
171
     * Export the given resources from storage.
172
     *
173
     * @return \Illuminate\Http\Response
174
     */
175
    public function export()
176
    {
177
        //
178
    }
179
180
    /**
181
     * Bulk control the given resources.
182
     *
183
     * @return \Illuminate\Http\Response
184
     */
185
    public function bulk()
186
    {
187
        //
188
    }
189
190
    /**
191
     * Show the form for create/edit/copy of the given resource.
192
     *
193
     * @param string                   $mode
194
     * @param string                   $action
195
     * @param \Rinvex\Fort\Models\Role $role
196
     *
197
     * @return \Illuminate\Http\Response
198
     */
199
    protected function form($mode, $action, Role $role)
200
    {
201
        $abilityList = app('rinvex.fort.ability')->findAll()->groupBy('resource')->map(function ($ability) {
202
            return $ability->pluck('title', 'id');
203
        })->toArray();
204
205
        return view('rinvex/fort::backend/roles.form', compact('role', 'abilityList', 'mode', 'action'));
206
    }
207
208
    /**
209
     * Process the form for store/update of the given resource.
210
     *
211
     * @param \Illuminate\Http\Request $request
212
     * @param \Rinvex\Fort\Models\Role $role
213
     *
214
     * @return \Illuminate\Http\Response
215
     */
216
    protected function process(Request $request, Role $role = null)
217
    {
218
        // Prepare required input fields
219
        $input     = $request->except(['_method', '_token', 'id']);
220
        $abilities = $request->user($this->getGuard())->can('grant-abilities') ? ['abilities' => array_pull($input, 'abilityList')] : [];
221
222
        // Store data into the entity
223
        $result = $this->roleRepository->store($role, $input + $abilities);
224
225
        // Repository `store` method returns false if no attributes
226
        // updated, happens save button clicked without chaning anything
227
        $message = ! is_null($role)
228
            ? ($result === false
229
                ? ['rinvex.fort.alert.warning' => trans('rinvex/fort::backend/messages.role.nothing_updated', ['roleId' => $role->id])]
230
                : ['rinvex.fort.alert.success' => trans('rinvex/fort::backend/messages.role.updated', ['roleId' => $result->id])])
231
            : ['rinvex.fort.alert.success' => trans('rinvex/fort::backend/messages.role.created', ['roleId' => $result->id])];
232
233
        return intend([
234
            'route' => 'rinvex.fort.backend.roles.index',
235
            'with'  => $message,
236
        ]);
237
    }
238
}
239