Completed
Pull Request — master (#107)
by Glenn
10:03 queued 04:56
created

StaffController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace app\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Http\Requests;
7
use App\User;
8
use App\Countries;
9
use App\Teams;
10
use App\Role;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Input;
13
use Illuminate\Support\Facades\Mail;
14
use Illuminate\Support\Facades\Redirect;
15
use Intervention\Image\Facades\Image;
16
use App\Permission;
17
use Bouncer;
18
use App\Http\Controllers\Controller;
19
20
class StaffController extends Controller
21
{
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
    }
26
27
    /**
28
     * Display all users.
29
     *
30
     * @return mixed
31
     */
32
    public function index()
33
    {
34
        $data['users'] = User::orderBy('fname', 'asc')->paginate(10);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
35
36
        return view('staff/users', $data);
37
    }
38
39
    /**
40
     * Show the form for creating a new employee.
41
     *
42
     * @return mixed
43
     */
44
    public function create()
45
    {
46
        $data['countries'] = Countries::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
47
48
        return view('staff/create_user', $data);
49
    }
50
51
    /**
52
     * Store a newly created employee in storage.
53
     *
54
     * @param Requests\StaffValidator|Request $request
55
     *
56
     * @return mixed
57
     */
58
    public function store(Requests\StaffValidator $request)
59
    {
60
        $user = new User();
61
        $user->fname = $request->get('fname');
62
        $user->name = $request->get('name');
63
        $user->address = $request->get('address');
64
        $user->postal_code = $request->get('postal_code');
65
        $user->city = $request->get('city');
66
        $user->email = $request->get('email');
67
        $user->password = bcrypt($request->get('password'));
68
        $user->save();
69
70
        $mailbox = env('MAIL_USERNAME');
71
        $mail_password = $request->get('password');
72
        session()->flash('message', 'New employee has been added to the application');
73
74
        $injectionData = ['user' => $user, 'password' => $mail_password];
75
76
        Mail::send('emails.new_user', $injectionData, function ($m) use ($user, $mailbox) {
77
            $m->from($mailbox);
78
            $m->to($user->email)->subject('Your user credentials!');
79
        });
80
81
        return redirect('staff');
82
    }
83
84
    /**
85
     * Update a user.
86
     *
87
     * @param Int,    $id
0 ignored issues
show
Documentation introduced by
The doc-type Int, could not be parsed: Expected "|" or "end of type", but got "," at position 3. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
88
     * @param Request $request
89
     *
90
     * @return mixed
91
     */
92
    public function updateUser($id, Request $request)
93
    {
94
        $user = User::find($id);
95
        $user->fname = $request->get('fname');
96
        $user->name = $request->get('name');
97
        $user->address = $request->get('address');
98
        $user->postal_code = $request->get('postal_code');
99
        $user->city = $request->get('city');
100
        $user->email = $request->get('email');
101
        $user->assignRole($request->get('user_type'));
102
        $user->update();
103
104
        $user = User::find($id);
105
        Bouncer::assign($request->get('user_typ'))->to($user);
106
107
        session()->flash('message', 'User details have been updated');
108
109
        return \Redirect::back();
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115
    public function policies()
116
    {
117
        $data['roles'] = Role::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
118
119
        return view('staff/roles', $data);
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function addpolicies()
126
    {
127
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
128
129
        return view('staff/create_role', $data);
130
    }
131
132
    /**
133
     * @param Request $request
134
     *
135
     * @return string
136
     */
137
    public function addRole(Request $request)
138
    {
139
        $role = Role::create(['name' => $request->get('role_name')]);
140
        foreach ($request->get('permissions') as $permission) {
141
            $role->givePermissionTo($permission);
142
        }
143
        if ($assign_role) {
0 ignored issues
show
Bug introduced by
The variable $assign_role does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
144
            session()->flash('message', 'New user role has been created');
145
146
            return redirect('staff/policies');
147
        } else {
148
            return 'Mislukt';
149
        }
150
    }
151
152
    /**
153
     * @param $id
154
     *
155
     * @return mixed
156
     */
157
    public function editpolicies($id)
158
    {
159
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
160
161
        return view('staff/edit_role', $data);
162
    }
163
164
    /**
165
     * Remove the user role.
166
     *
167
     * @param int, $id
0 ignored issues
show
Documentation introduced by
The doc-type int, could not be parsed: Expected "|" or "end of type", but got "," at position 3. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
168
     *
169
     * @return redirect
170
     */
171
    public function destroyRole($id)
172
    {
173
        $role = Role::find($id);
174
        $role->delete();
175
        session()->flash('message', 'User role has been removed from the database');
176
177
        return redirect('staff/policies');
178
    }
179
180
    /**
181
     * Show all permission.
182
     */
183
    public function permissions()
184
    {
185
        $data['permissions'] = Permission::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
186
187
        return view('staff/permissions', $data);
188
    }
189
190
    /**
191
     * Show the form to create a new permission.
192
     */
193
    public function createPermission()
194
    {
195
        return view('staff/create_permission');
196
    }
197
198
    /**
199
     * Show the form to edit a permission.
200
     */
201
    public function EditPermission(Request $request, $id)
202
    {
203
        $data['permission'] = Permission::find($id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
204
        return view('staff/edit_permission', $data);
205
    }
206
207
    /**
208
     * Save the new permission.
209
     *
210
     * @param Request $request
211
     *
212
     * @return redirect
213
     */
214
    public function savePermission(Request $request)
215
    {
216
        Permission::create(['name' => $request->get('permission_name')]);
217
        session()->flash('message', 'The new permission has been added to the database');
218
219
        return redirect('staff/permissions');
220
    }
221
222
    public function destroyPermission($id)
223
    {
224
        $permission = Permission::find($id);
225
        $permission->delete();
226
227
        session()->flash('message', 'Permission has been removed from the database');
228
229
        return redirect('staff/permissions');
230
    }
231
232
    /**
233
     * Display the specified resource.
234
     *
235
     * @param int $id
236
     *
237
     * @return \Illuminate\Http\Response
238
     */
239
    public function show($id)
240
    {
241
        //
242
    }
243
244
    /**
245
     * Show the form for editing the specified resource.
246
     *
247
     * @param $id
248
     *
249
     * @return
250
     */
251
    public function edit($id)
252
    {
253
        $data['user'] = User::findOrFail($id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
254
        $data['teams'] = Teams::all();
255
        $data['countries'] = Countries::all();
256
        $data['roles'] = Role::all();
257
258
        return view('staff/edit_user', $data);
259
    }
260
261
    /**
262
     * @return mixed
263
     */
264
    public function profile()
265
    {
266
        $data['countries'] = Countries::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
267
268
        return view('staff/profile', $data);
269
    }
270
271
    /**
272
     * change a password.
273
     */
274
    public function chPass()
275
    {
276
    }
277
278
    /**
279
     * Update the specified resource in storage.
280
     *
281
     * @param Requests\accountManagementValidator|Request $request
282
     * @return \Illuminate\Http\Response
283
     */
284
    public function update(Requests\accountManagementValidator $request)
285
    {
286
        // TODO: Add validation - Tjoosten
287
        $user = User::findOrFail(auth()->user()->id);
288
        $user->fname = $request->get('email');
289
        $user->name = $request->get('name');
290
        $user->email = $request->get('email');
291
        $user->address = $request->get('address');
292
        $user->email = $request->get('email');
293
294
        if (Input::file()) {
295
            $image = Input::file('avatar');
296
            $filename = time().'.'.$image->getClientOriginalExtension();
297
            $path = public_path('profilepics/'.$filename);
298
299
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
300
            $user->image = $filename;
301
        }
302
303
        $user->save();
304
305
        return redirect()->back();
306
    }
307
308
    /**
309
     * Remove the specified employee from the database.
310
     *
311
     * @param int $id
312
     *
313
     * @return \Illuminate\Http\Response
314
     */
315
    public function destroy($id)
316
    {
317
        if (!Auth::user()->is('Administrator')) {
318
            return Redirect::back();
319
        }
320
321
        User::Destroy($id);
322
        session()->flash('message', 'User has been removed from the database');
323
324
        return redirect('staff');
325
    }
326
}
327