Passed
Push — task/order-skills-by-id ( 15d8c6 )
by Yonathan
05:54
created

JobController::hrIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use App\Models\JobApplication;
10
use Carbon\Carbon;
11
use App\Models\JobPoster;
12
use App\Models\JobPosterQuestion;
13
use App\Models\Lookup\ApplicationStatus;
14
use App\Models\Lookup\CitizenshipDeclaration;
15
use App\Models\Lookup\JobPosterStatus;
16
use App\Models\Lookup\VeteranStatus;
17
use App\Models\Manager;
18
use App\Services\JobPosterDefaultQuestions;
19
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
20
use App\Services\Validation\JobPosterValidator;
21
use Facades\App\Services\WhichPortal;
22
use Illuminate\Support\Facades\Response;
23
use Illuminate\Support\Facades\Validator;
24
25
class JobController extends Controller
26
{
27
    /**
28
     * Display a listing of JobPosters.
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function index()
33
    {
34
        // If true, show the Paused due to COVID-19 message.
35
        $emergency_response = config('seasonal.is_covid_emergency');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $emergency_response = /** @scrutinizer ignore-call */ config('seasonal.is_covid_emergency');
Loading history...
36
37
        // Find published jobs that are currently open for applications.
38
        // Eager load required relationships: Department, Province, JobTerm.
39
        // Eager load the count of submitted applications, to prevent the relationship
40
        // from being actually loaded and firing off events.
41
        $jobs = JobPoster::where('internal_only', false)
42
            ->where('department_id', '!=', config('app.strategic_response_department_id'))
43
            ->where('job_poster_status_id', JobPosterStatus::where('key', 'live')->first()->id)
44
            ->with([
45
                'department',
46
                'province',
47
                'job_term',
48
            ])
49
            ->withCount([
50
                'submitted_applications',
51
            ])
52
            ->get();
53
54
        $null_alert = $emergency_response
55
            ? Lang::get('applicant/job_index.index.covid_null_alert')
56
            : Lang::get('applicant/job_index.index.null_alert');
57
        return view('applicant/job_index', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        return /** @scrutinizer ignore-call */ view('applicant/job_index', [
Loading history...
58
            'job_index' => Lang::get('applicant/job_index'),
59
            'null_alert' => $null_alert,
60
            'jobs' => $jobs
61
        ]);
62
    }
63
64
    /**
65
     * Display a listing of a manager's JobPosters.
66
     *
67
     * @return \Illuminate\Http\Response
68
     */
69
    public function managerIndex()
70
    {
71
        $manager = Auth::user()->manager;
72
73
        $jobs = JobPoster::where('manager_id', $manager->id)
74
            ->with('classification')
75
            ->withCount('submitted_applications')
76
            ->get();
77
78
        foreach ($jobs as &$job) {
79
            $chosen_lang = $job->chosen_lang;
80
81
            // Show chosen lang title if current title is empty.
82
            if (empty($job->title)) {
83
                $job->title = $job->getTranslation('title', $chosen_lang);
84
                $job->trans_required = true;
85
            }
86
87
            // Always preview and edit in the chosen language.
88
            $job->preview_link = LaravelLocalization::getLocalizedURL($chosen_lang, route('manager.jobs.show', $job));
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            $job->preview_link = LaravelLocalization::getLocalizedURL($chosen_lang, /** @scrutinizer ignore-call */ route('manager.jobs.show', $job));
Loading history...
89
            $job->edit_link = LaravelLocalization::getLocalizedURL($chosen_lang, route('manager.jobs.edit', $job));
90
        }
91
92
93
        return view('manager/job_index', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        return /** @scrutinizer ignore-call */ view('manager/job_index', [
Loading history...
94
            // Localization Strings.
95
            'jobs_l10n' => Lang::get('manager/job_index'),
96
            // Data.
97
            'jobs' => $jobs,
98
        ]);
99
    }
100
101
    /**
102
     * Display a listing of a hr advisor's JobPosters.
103
     *
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function hrIndex(Request $request)
107
    {
108
        $hrAdvisor = $request->user()->hr_advisor;
109
        return view('hr_advisor/job_index', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        return /** @scrutinizer ignore-call */ view('hr_advisor/job_index', [
Loading history...
110
            'title' => Lang::get('hr_advisor/job_index.title'),
111
            'hr_advisor_id' => $hrAdvisor->id
112
        ]);
113
    }
114
115
116
117
118
    /**
119
     * Delete a draft Job Poster.
120
     *
121
     * @param  \Illuminate\Http\Request $request   Incoming request object.
122
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function destroy(Request $request, JobPoster $jobPoster)
126
    {
127
        $jobPoster->delete();
128
    }
129
130
    /**
131
     * Display the specified job poster.
132
     *
133
     * @param  \Illuminate\Http\Request $request   Incoming request object.
134
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
135
     * @return \Illuminate\Http\Response
136
     */
137
    public function show(Request $request, JobPoster $jobPoster)
138
    {
139
        $jobPoster->load([
140
            'department',
141
            'criteria.skill.skill_type',
142
            'manager.team_culture',
143
            'manager.work_environment'
144
        ]);
145
146
        $user = Auth::user();
147
148
        // TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model.
149
        $workplacePhotos = [];
150
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
151
            $workplacePhotos[] = [
152
                'description' => $photoCaption->description,
153
                'url' => '/images/user.png'
154
            ];
155
        }
156
157
        // TODO: replace route('manager.show',manager.id) in templates with link using slug.
158
        $essential = $jobPoster->criteria->filter(
159
            function ($value, $key) {
160
                return $value->criteria_type->name == 'essential';
161
            }
162
        )->sortBy('id');
163
        $asset = $jobPoster->criteria->filter(
164
            function ($value, $key) {
165
                return $value->criteria_type->name == 'asset';
166
            }
167
        )->sortBy('id');
168
        $criteria = [
169
            'essential' => $essential,
170
            'asset' => $asset,
171
        ];
172
173
        $jobLang = Lang::get('applicant/job_post');
174
175
        $applyButton = [];
176
        if (WhichPortal::isManagerPortal()) {
177
            $applyButton = [
178
                'href' => route('manager.jobs.edit', $jobPoster->id),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
                'href' => /** @scrutinizer ignore-call */ route('manager.jobs.edit', $jobPoster->id),
Loading history...
179
                'title' => $jobLang['apply']['edit_link_title'],
180
                'text' => $jobLang['apply']['edit_link_label'],
181
            ];
182
        } elseif (WhichPortal::isHrPortal()) {
183
            if ($jobPoster->hr_advisors->contains('user_id', $user->id)) {
184
                $applyButton = [
185
                    'href' => route('hr_advisor.jobs.summary', $jobPoster->id),
186
                    'title' => null,
187
                    'text' => Lang::get('hr_advisor/job_summary.summary_title'),
188
                ];
189
            } else {
190
                $applyButton = [
191
                    'href' => route('hr_advisor.jobs.index'),
192
                    'title' => null,
193
                    'text' => Lang::get('hr_advisor/job_index.title'),
194
                ];
195
            }
196
        } elseif (Auth::check() && $jobPoster->isOpen()) {
197
            $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
198
                ->where('job_poster_id', $jobPoster->id)->first();
199
            // If applicants job application is not draft anymore then link to application preview page.
200
            if ($application != null && $application->application_status->name != 'draft') {
201
                $applyButton = [
202
                    'href' => route('applications.show', $application->id),
203
                    'title' => $jobLang['apply']['view_link_title'],
204
                    'text' => $jobLang['apply']['view_link_label'],
205
                ];
206
            } else {
207
                $applyButton = [
208
                    'href' => route('job.application.edit.1', $jobPoster->id),
209
                    'title' => $jobLang['apply']['apply_link_title'],
210
                    'text' => $jobLang['apply']['apply_link_label'],
211
                ];
212
            }
213
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
214
            $applyButton = [
215
                'href' => route('job.application.edit.1', $jobPoster->id),
216
                'title' => $jobLang['apply']['login_link_title'],
217
                'text' => $jobLang['apply']['login_link_label'],
218
            ];
219
        } else {
220
            $applyButton = [
221
                'href' => null,
222
                'title' => null,
223
                'text' => $jobLang['apply']['job_closed_label'],
224
            ];
225
        }
226
227
        $jpb_release_date = strtotime('2019-08-21 16:18:17');
228
        $job_created_at = strtotime($jobPoster->created_at);
229
230
        // If the poster is part of the Strategic Talent Response dept, use the talent stream template.
231
        // Else, If the job poster is created after the release of the JPB.
232
        // Then, render with updated poster template.
233
        // Else, render with old poster template.
234
        if ($jobPoster->isInStrategicResponseDepartment()) {
235
            return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
            return /** @scrutinizer ignore-call */ view(
Loading history...
236
                'applicant/strategic_response_job_post',
237
                [
238
                    'job_post' => $jobLang,
239
                    'frequencies' => Lang::get('common/lookup/frequency'),
240
                    'skill_template' => Lang::get('common/skills'),
241
                    'job' => $jobPoster,
242
                    'manager' => $jobPoster->manager,
243
                    'criteria' => $criteria,
244
                    'apply_button' => $applyButton,
245
                ]
246
            );
247
        } elseif ($job_created_at > $jpb_release_date) {
248
            // Updated job poster (JPB).
249
            return view(
250
                'applicant/jpb_job_post',
251
                [
252
                    'job_post' => $jobLang,
253
                    'frequencies' => Lang::get('common/lookup/frequency'),
254
                    'skill_template' => Lang::get('common/skills'),
255
                    'job' => $jobPoster,
256
                    'manager' => $jobPoster->manager,
257
                    'criteria' => $criteria,
258
                    'apply_button' => $applyButton,
259
                ]
260
            );
261
        } else {
262
            // Old job poster.
263
            return view(
264
                'applicant/job_post',
265
                [
266
                    'job_post' => $jobLang,
267
                    'frequencies' => Lang::get('common/lookup/frequency'),
268
                    'manager' => $jobPoster->manager,
269
                    'manager_profile_photo_url' => '/images/user.png', // TODO get real photo.
270
                    'team_culture' => $jobPoster->manager->team_culture,
271
                    'work_environment' => $jobPoster->manager->work_environment,
272
                    'workplace_photos' => $workplacePhotos,
273
                    'job' => $jobPoster,
274
                    'criteria' => $criteria,
275
                    'apply_button' => $applyButton,
276
                    'skill_template' => Lang::get('common/skills'),
277
                ]
278
            );
279
        }
280
    }
281
282
    /**
283
     * Display the form for editing an existing Job Poster
284
     * Only allows editing fields that don't appear on the react-built Job Poster Builder.
285
     *
286
     * @param  \Illuminate\Http\Request $request   Incoming request object.
287
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
288
     * @return \Illuminate\Http\Response
289
     */
290
    public function edit(Request $request, JobPoster $jobPoster)
291
    {
292
        $manager = $jobPoster->manager;
293
294
        $defaultQuestionManager = new JobPosterDefaultQuestions();
295
        $defaultQuestionManager->initializeQuestionsIfEmpty($jobPoster);
296
297
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

297
        return /** @scrutinizer ignore-call */ view(
Loading history...
298
            'manager/job_create',
299
            [
300
                // Localization Strings.
301
                'job_l10n' => Lang::get('manager/job_edit'),
302
                // Data.
303
                'manager' => $manager,
304
                'job' => $jobPoster,
305
                'form_action_url' => route('admin.jobs.update', $jobPoster),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

305
                'form_action_url' => /** @scrutinizer ignore-call */ route('admin.jobs.update', $jobPoster),
Loading history...
306
            ]
307
        );
308
    }
309
310
    /**
311
     * Create a blank job poster for the specified manager
312
     *
313
     * @param  \App\Models\Manager $manager Incoming Manager object.
314
     * @return \Illuminate\Http\Response Job Create view
315
     */
316
    public function createAsManager(Manager $manager)
317
    {
318
        $jobPoster = new JobPoster();
319
        $jobPoster->manager_id = $manager->id;
320
321
        // Save manager-specific info to the job poster - equivalent to the intro step of the JPB
322
        $divisionEn = $manager->getTranslation('division', 'en');
323
        $divisionFr = $manager->getTranslation('division', 'fr');
324
        $jobPoster->fill([
325
            'department_id' => $manager->user->department_id,
326
            'division' => ['en' => $divisionEn],
327
            'division' => ['fr' => $divisionFr],
328
        ]);
329
330
        $jobPoster->save();
331
332
        return redirect()->route('manager.jobs.edit', $jobPoster->id);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

332
        return /** @scrutinizer ignore-call */ redirect()->route('manager.jobs.edit', $jobPoster->id);
Loading history...
333
    }
334
335
    /**
336
     * Update a resource in storage
337
     * NOTE: Only saves fields that are not on the react-built Job Poster Builder
338
     *
339
     * @param  \Illuminate\Http\Request $request   Incoming request object.
340
     * @param  \App\Models\JobPoster    $jobPoster Optional Job Poster object.
341
     * @return \Illuminate\Http\Response
342
     */
343
    public function store(Request $request, JobPoster $jobPoster)
344
    {
345
        // Don't allow edits for published Job Posters
346
        // Also check auth while we're at it.
347
        $this->authorize('update', $jobPoster);
348
        JobPosterValidator::validateUnpublished($jobPoster);
349
350
        $input = $request->input();
351
352
        if ($jobPoster->manager_id == null) {
353
            $jobPoster->manager_id = $request->user()->manager->id;
354
            $jobPoster->save();
355
        }
356
357
        if ($request->input('question')) {
358
            $validator = Validator::make($request->input('question'), [
359
                '*.question.*' => 'required|string',
360
            ], [
361
                'required' => Lang::get('validation.custom.job_poster_question.required'),
362
                'string' => Lang::get('validation.custom.job_poster_question.string')
363
            ]);
364
365
            if ($validator->fails()) {
366
                $request->session()->flash('errors', $validator->errors());
367
                return redirect(route('admin.jobs.edit', $jobPoster->id));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

367
                return /** @scrutinizer ignore-call */ redirect(route('admin.jobs.edit', $jobPoster->id));
Loading history...
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

367
                return redirect(/** @scrutinizer ignore-call */ route('admin.jobs.edit', $jobPoster->id));
Loading history...
368
            }
369
        }
370
371
        $this->fillAndSaveJobPosterQuestions($input, $jobPoster, true);
372
373
        return redirect(route('manager.jobs.show', $jobPoster->id));
374
    }
375
376
    /**
377
     * Fill Job Poster's questions and save
378
     *
379
     * @param  mixed[]               $input     Field values.
380
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
381
     * @param  boolean               $replace   Remove existing relationships.
382
     * @return void
383
     */
384
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void
385
    {
386
        if ($replace) {
387
            $jobPoster->job_poster_questions()->delete();
388
        }
389
390
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
391
            return;
392
        }
393
394
        foreach ($input['question'] as $question) {
395
            $jobQuestion = new JobPosterQuestion();
396
            $jobQuestion->job_poster_id = $jobPoster->id;
397
            $jobQuestion->fill(
398
                [
399
                    'question' => [
400
                        'en' => $question['question']['en'],
401
                        'fr' => $question['question']['fr']
402
403
                    ],
404
                    'description' => [
405
                        'en' => $question['description']['en'],
406
                        'fr' => $question['description']['fr']
407
                    ]
408
                ]
409
            );
410
            $jobPoster->save();
411
            $jobQuestion->save();
412
        }
413
    }
414
415
    /**
416
     * Downloads a CSV file with the applicants who have applied to the job poster.
417
     *
418
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
419
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
420
     */
421
    protected function downloadApplicants(JobPoster $jobPoster)
422
    {
423
        $tables = [];
424
        // The first row in the array represents the names of the columns in the spreadsheet.
425
        $tables[0] = ['Status', 'Applicant Name', 'Email', 'Language'];
426
427
        $application_status_id = ApplicationStatus::where('name', 'submitted')->first()->id;
428
        $applications = JobApplication::where('job_poster_id', $jobPoster->id)
429
            ->where('application_status_id', $application_status_id)
430
            ->get();
431
432
        $index = 1;
433
        foreach ($applications as $application) {
434
            $status = '';
435
            $username = $application->user_name;
436
            $user_email = $application->user_email;
437
            $language = strtoupper($application->preferred_language->name);
438
            // If the applicants veteran status name is NOT 'none' then set status to veteran.
439
            $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
440
            if ($application->veteran_status_id != $non_veteran) {
441
                $status = 'Veteran';
442
            } else {
443
                // Check if the applicant is a canadian citizen.
444
                $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
445
                if ($application->citizenship_declaration->id == $canadian_citizen) {
446
                    $status = 'Citizen';
447
                } else {
448
                    $status = 'Non-citizen';
449
                }
450
            }
451
            $tables[$index] = [$status, $username, $user_email, $language];
452
            $index++;
453
        }
454
455
        $filename = $jobPoster->id . '-' . 'applicants-data.csv';
456
457
        // Open file.
458
        $file = fopen($filename, 'w');
459
        // Iterate through tables and add each line to csv file.
460
        foreach ($tables as $line) {
461
            fputcsv($file, $line);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

461
            fputcsv(/** @scrutinizer ignore-type */ $file, $line);
Loading history...
462
        }
463
        // Close open file.
464
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

464
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
465
466
        $headers = [
467
            'Content-Type' => 'text/csv',
468
            'Content-Disposition' => 'attachment; filename=' . $filename,
469
        ];
470
471
        return Response::download($filename, $filename, $headers);
472
    }
473
}
474