Completed
Push — master ( 6374b3...243f6e )
by D.
17:36 queued 12:41
created

UserController::resolveImport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace SET\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\File;
7
use Illuminate\Support\Facades\Gate;
8
use Illuminate\Support\Facades\Input;
9
use Illuminate\Support\Facades\Storage;
10
use Krucas\Notification\Facades\Notification;
11
use SET\Duty;
12
use SET\Group;
13
use SET\Handlers\Excel\JpasImport;
14
use SET\Http\Requests\StoreUserRequest;
15
use SET\User;
16
17
/**
18
 * Class UserController.
19
 */
20
class UserController extends Controller
21
{
22
    /**
23
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
24
     */
25 1
    public function index()
26
    {
27 1
        $this->authorize('view');
28
29 1
        $users = User::with([
30
            'assignedTrainings' => function ($q) {
31 1
                $q->whereNull('completed_date')
32 1
                    ->whereBetween('due_date', [Carbon::now()->subYear(), Carbon::now()->addWeeks(4)]);
33 1
            },
34 1
            'trainings',
35
        ])
36 1
            ->skipSystem()
37 1
            ->orderBy('last_name')->get();
38
39 1
        return view('user.index', compact('users'));
40
    }
41
42 1
    public function create()
43
    {
44 1
        $this->authorize('edit');
45
46 1
        $supervisors = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('userFullName', 'id')->toArray();
47 1
        $groups = Group::all();
48
49 1
        return view('user.create', compact('supervisors', 'groups'));
50
    }
51
52
    /**
53
     * @param StoreUserRequest $request
54
     *
55
     * @return \Illuminate\Http\RedirectResponse
56
     */
57 1
    public function store(StoreUserRequest $request)
58
    {
59 1
        $data = $request->all();
60 1
        $data['status'] = 'active';
61 1
        $user = User::create($data);
62
63 1
        if (array_key_exists('groups', $data)) {
64
            settype($data['groups'], 'array');
65
            $user->groups()->sync($data['groups']);
66
        }
67
68 1
        return redirect()->action('UserController@index');
69
    }
70
71
    /**
72
     * @param $userId
73
     *
74
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
75
     */
76 3
    public function show($userId)
77
    {
78
        $user = User::with(['subordinates' => function ($query) {
79 3
            $query->active();
80 3
        },
81 3
                            'groups', 'duties', 'attachments',
82 3
                            'visits', 'notes.author', 'notes.attachments',
83 3
                            'travels.author', 'travels.attachments', ])
84 3
                    ->findOrFail($userId);
85
86
        //Make sure the user can't access other people's pages.
87 3
        $this->authorize('show_user', $user);
88
89 3
        $user['clearance'] = $this->spellOutClearance($user['clearance']);
90 3
        $user['access_level'] = $this->spellOutClearance($user['access_level']);
91
92 3
        $trainings = $user->assignedTrainings()->with('author', 'training.attachments', 'attachments')->orderBy('completed_date', 'DESC')->get();
93
94 3
        $user_training_types = $this->getUserTrainingTypes($trainings);
95 3
        $training_user_types = $user_training_types[0]; // List of the user's training types
96 3
        $training_blocks = $user_training_types[1]; // List of training block titles for user
97
98 3
        $activityLog = [];
99 3
        if (Gate::allows('view')) {
100 2
            $activityLog = $user->getUserLog($user);
101
        }
102
103 3
        $this->previousAndNextUsers($user, $previous, $next);
104
105
        //This mess is just so that we can output the Security Check list or show none. Mainly just to show none.
106
        $duties = Duty::whereHas('users', function ($q) use ($userId) {
107 3
            $q->where('id', $userId);
108 3
        })->orWhereHas('groups.users', function ($q) use ($userId) {
109 3
            $q->where('id', $userId);
110 3
        })->get();
111
112 3
        return view('user.show', compact('user', 'duties', 'previous', 'next',
113 3
            'trainings', 'activityLog', 'training_blocks', 'training_user_types'));
114
    }
115
116 1
    public function edit(User $user)
117
    {
118 1
        $this->authorize('edit');
119
120 1
        $supervisors = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('userFullName', 'id')->toArray();
121 1
        $groups = Group::all();
122
123 1
        return view('user.edit', compact('user', 'supervisors', 'groups'));
124
    }
125
126 1
    public function update(User $user)
127
    {
128 1
        $this->authorize('edit');
129
130 1
        $data = Input::all();
131
132 1
        $data['destroyed_date'] = $user->getDestroyDate($data['status']);
133
134 1
        $user->update($data);
135
136
        //Handle user groups
137 1
        if (!array_key_exists('groups', $data)) {
138 1
            $data['groups'] = [];
139
        }
140 1
        $user->groups()->sync($data['groups']);
141
142
        //Handled closed area access (MUST come AFTER syncing groups).
143 1
        if (array_key_exists('access', $data)) {
144
            foreach ($data['access'] as $group_id => $accessLevel) {
145
                $user->groups()->updateExistingPivot($group_id, ['access' => $accessLevel]);
146
            }
147
        }
148
149 1
        return redirect()->action('UserController@show', $user->id);
150
    }
151
152
    /**
153
     * @param $userId
154
     *
155
     * @return string
156
     */
157 1
    public function destroy($userId)
158
    {
159 1
        $this->authorize('edit');
160
161 1
        Storage::deleteDirectory('user_'.$userId);
162 1
        User::findOrFail($userId)->delete();
163
164 1
        return 'success';
165
    }
166
167
    /**
168
     * @param $trainings[]
169
     * From the User's trainings, a list of the training types is determined and
170
     * a list of the training block titles is determined.
171
     *
172
     * @return user_training_types[], training_block_titles[]
0 ignored issues
show
Documentation introduced by
The doc-type user_training_types[], could not be parsed: Expected "|" or "end of type", but got "," at position 21. (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...
173
     */
174 5
    public function getUserTrainingTypes($trainings = [])
175
    {
176 5
        $training_block_titles = $user_training_types = [];
177 5
        foreach ($trainings as $trainingUser) {
178 2
            if (is_null($trainingUser->completed_date)) {
179 2
                $training_block_titles['AAA'] = 'Scheduled';
180 2
                $user_training_types[$trainingUser->id] = 'Scheduled';
181 2
            } elseif ($trainingUser->Training->trainingType) {
182 2
                $typeName = $trainingUser->Training->trainingType->name;
183 2
                $training_block_titles[$typeName] = $typeName;
184 2
                $user_training_types[$trainingUser->id] = $typeName;
185
            } else { // No training type
186 1
                $training_block_titles['999'] = 'Miscellaneous';
187 2
                $user_training_types[$trainingUser->id] = 'Miscellaneous';
188
            }
189
        }
190 5
        ksort($training_block_titles);  // Order by key
191 5
        return [$user_training_types, $training_block_titles];
192
    }
193
194
    /**
195
     * Process our JPAS import. Once that has been handled, we pass the file, changes,
196
     * unique/unmapped users & a user list to the user.import view.
197
     * That way we keep all this data for the resolve phase.
198
     *
199
     * @param JpasImport $import
200
     *
201
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
202
     */
203
    public function import(JpasImport $import)
204
    {
205
        $results = $import->handleImport();
206
        $uploadedFile = $import->getFile('file');
207
208
        $changes = $results['changes'];
209
        $unique = $results['unique'];
210
211
        $userList = User::orderBy('last_name')->get()->pluck('UserFullName', 'id')->toArray();
212
213
        return view('user.import', compact('unique', 'changes', 'userList', 'uploadedFile'));
214
    }
215
216
    /**
217
     * @param JpasImport $import
218
     *
219
     * @return \Illuminate\Http\RedirectResponse
220
     */
221
    public function resolveImport(JpasImport $import)
222
    {
223
        $import->handleImport();
224
225
        Notification::container()->success('Import Complete');
226
227
        File::delete($import->getFile('file'));
228
229
        return redirect()->action('HomeController@index');
230
    }
231
232
    /**
233
     * Generate the grab the previous and next user if our users are sorted alphabetically.
234
     *
235
     * @param $user
236
     * @param $previous
237
     * @param $next
238
     */
239 3
    private function previousAndNextUsers($user, &$previous, &$next)
240
    {
241
        //Build the previous/next user that are in alphabetical order.
242 3
        $users = User::skipSystem()->orderBy('last_name')->orderBy('first_name')->get();
243 3
        $previous = null; // set to null by default in case we are at the start of the list.
244 3
        while ($users->first()->id != $user->id) {
245 2
            $previous = $users->shift()->id;
246
        }
247
        //check if we have a record aft the current user. If not, then we are at the end.
248 3
        if ($users->count() > 1) {
249 2
            $users->shift();
250 2
            $next = $users->shift()->id;
251
        } else {
252 2
            $next = null;
253
        }
254 3
    }
255
256
    /**
257
     * @param $clearance
258
     *
259
     * @return mixed
260
     */
261 3
    private function spellOutClearance($clearance)
262
    {
263
        //fully spell out user's clearance.
264
        switch ($clearance) {
265 3
            case 'S':
266
                $clearance = 'Secret';
267
                break;
268 3
            case 'TS':
269
                $clearance = 'Top Secret';
270
                break;
271 3
            case 'Int S':
272
                $clearance = 'Interim Secret';
273
                break;
274
        }
275
276 3
        return $clearance;
277
    }
278
}
279