1 | <?php |
||
20 | class UserController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
24 | */ |
||
25 | 1 | public function index() |
|
41 | |||
42 | 1 | public function create() |
|
51 | |||
52 | /** |
||
53 | * @param StoreUserRequest $request |
||
54 | * |
||
55 | * @return \Illuminate\Http\RedirectResponse |
||
56 | */ |
||
57 | 1 | public function store(StoreUserRequest $request) |
|
70 | |||
71 | /** |
||
72 | * @param $userId |
||
73 | * |
||
74 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
75 | */ |
||
76 | 2 | public function show($userId) |
|
77 | { |
||
78 | $user = User::with(['subordinates' => function ($query) { |
||
79 | 2 | $query->active(); |
|
80 | 2 | }, |
|
81 | 2 | 'groups', 'duties', 'attachments', |
|
82 | 2 | 'visits', 'notes.author', 'notes.attachments', |
|
83 | 2 | 'travels.author', 'travels.attachments', ]) |
|
84 | 2 | ->findOrFail($userId); |
|
85 | |||
86 | //Make sure the user can't access other people's pages. |
||
87 | 2 | $this->authorize('show_user', $user); |
|
88 | |||
89 | 2 | $user['clearance'] = $this->spellOutClearance($user['clearance']); |
|
90 | 2 | $user['access_level'] = $this->spellOutClearance($user['access_level']); |
|
91 | |||
92 | 2 | $trainings = $user->assignedTrainings()->with('author', 'training.attachments', 'attachments')->orderBy('completed_date', 'DESC')->get(); |
|
93 | |||
94 | $user_training_types = $this->getUserTrainingTypes($trainings); |
||
95 | 2 | $training_user_types = $user_training_types[0]; // List of the user's training types |
|
96 | 2 | $training_blocks = $user_training_types[1]; // List of training block titles for user |
|
97 | |||
98 | $activityLog = []; |
||
99 | if (Gate::allows('view')) { |
||
100 | $activityLog = $user->getUserLog($user); |
||
101 | } |
||
102 | |||
103 | $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 | $q->where('id', $userId); |
||
108 | 2 | })->orWhereHas('groups.users', function ($q) use ($userId) { |
|
109 | $q->where('id', $userId); |
||
110 | 2 | })->get(); |
|
111 | 2 | ||
112 | 1 | return view('user.show', compact('user', 'duties', 'previous', 'next', |
|
113 | 'trainings', 'activityLog', 'training_blocks', 'training_user_types')); |
||
114 | } |
||
115 | 2 | ||
116 | public function edit(User $user) |
||
117 | { |
||
118 | $this->authorize('edit'); |
||
119 | 2 | ||
120 | 2 | $supervisors = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('userFullName', 'id')->toArray(); |
|
121 | 2 | $groups = Group::all(); |
|
122 | 2 | ||
123 | return view('user.edit', compact('user', 'supervisors', 'groups')); |
||
124 | 2 | } |
|
125 | 2 | ||
126 | 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 | 1 | ||
134 | $user->update($data); |
||
135 | 1 | ||
136 | //Handle user groups |
||
137 | if (!array_key_exists('groups', $data)) { |
||
138 | 1 | $data['groups'] = []; |
|
139 | } |
||
140 | 1 | $user->groups()->sync($data['groups']); |
|
141 | |||
142 | 1 | //Handled closed area access (MUST come AFTER syncing groups). |
|
143 | if (array_key_exists('access', $data)) { |
||
144 | 1 | foreach ($data['access'] as $group_id => $accessLevel) { |
|
145 | $user->groups()->updateExistingPivot($group_id, ['access' => $accessLevel]); |
||
146 | 1 | } |
|
147 | } |
||
148 | |||
149 | 1 | return redirect()->action('UserController@show', $user->id); |
|
150 | 1 | } |
|
151 | |||
152 | 1 | /** |
|
153 | * @param $userId |
||
154 | * |
||
155 | 1 | * @return string |
|
156 | */ |
||
157 | public function destroy($userId) |
||
158 | { |
||
159 | $this->authorize('edit'); |
||
160 | |||
161 | 1 | Storage::deleteDirectory('user_'.$userId); |
|
162 | User::findOrFail($userId)->delete(); |
||
163 | |||
164 | return 'success'; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param $trainings[] |
||
169 | 1 | * 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 | 1 | * |
|
172 | * @return user_training_types[], training_block_titles[] |
||
|
|||
173 | 1 | */ |
|
174 | 1 | public function getUserTrainingTypes($trainings = []) |
|
175 | { |
||
176 | 1 | $training_block_titles = $user_training_types = []; |
|
177 | foreach ($trainings as $trainingUser) { |
||
178 | if (is_null($trainingUser->completed_date)) { |
||
179 | $training_block_titles['AAA'] = 'Scheduled'; |
||
180 | $user_training_types[$trainingUser->id] = 'Scheduled'; |
||
181 | } elseif ($trainingUser->Training->trainingType) { |
||
182 | $typeName = $trainingUser->Training->trainingType->name; |
||
183 | $training_block_titles[$typeName] = $typeName; |
||
184 | $user_training_types[$trainingUser->id] = $typeName; |
||
185 | } else { // No training type |
||
186 | $training_block_titles['999'] = 'Miscellaneous'; |
||
187 | $user_training_types[$trainingUser->id] = 'Miscellaneous'; |
||
188 | } |
||
189 | } |
||
190 | ksort($training_block_titles); // Order by key |
||
191 | 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) |
||
215 | |||
216 | /** |
||
217 | * @param JpasImport $import |
||
218 | * |
||
219 | * @return \Illuminate\Http\RedirectResponse |
||
220 | */ |
||
221 | public function resolveImport(JpasImport $import) |
||
231 | |||
232 | /** |
||
233 | 2 | * Generate the grab the previous and next user if our users are sorted alphabetically. |
|
234 | 1 | * |
|
235 | 1 | * @param $user |
|
236 | * @param $previous |
||
237 | 2 | * @param $next |
|
238 | */ |
||
239 | 2 | private function previousAndNextUsers($user, &$previous, &$next) |
|
255 | |||
256 | 2 | /** |
|
257 | * @param $clearance |
||
258 | * |
||
259 | * @return mixed |
||
260 | */ |
||
261 | 2 | private function spellOutClearance($clearance) |
|
278 | } |
||
279 |
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.