rbrecruiter/app/Http/Controllers/VacancyController.php

151 lines
4.9 KiB
PHP
Raw Normal View History

<?php
2020-10-10 16:30:26 +00:00
/*
* Copyright © 2020 Miguel Nogueira
*
* This file is part of Raspberry Staff Manager.
*
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Raspberry Staff Manager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
*/
2020-10-10 16:30:26 +00:00
namespace App\Http\Controllers;
2020-07-11 19:34:26 +00:00
use App\Facades\JSON;
use App\Form;
2020-10-10 16:30:26 +00:00
use App\Http\Requests\VacancyEditRequest;
use App\Http\Requests\VacancyRequest;
use App\Notifications\VacancyClosed;
2020-10-10 16:30:26 +00:00
use App\User;
use App\Vacancy;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class VacancyController extends Controller
{
2020-05-03 04:04:26 +00:00
public function index()
{
2020-10-10 16:30:26 +00:00
$this->authorize('viewAny', Vacancy::class);
return view('dashboard.administration.positions')
->with([
'forms' => Form::all(),
2020-10-10 16:30:26 +00:00
'vacancies' => Vacancy::all(),
]);
}
public function store(VacancyRequest $request)
{
$messageIsError = false;
$this->authorize('create', Vacancy::class);
$form = Form::find($request->vacancyFormID);
2020-10-10 16:30:26 +00:00
if (! is_null($form)) {
/* note: since we can't convert HTML back to Markdown, we'll have to do the converting when the user requests a page,
* and leave the database with Markdown only so it can be used and edited everywhere.
* for several vacancies, this would require looping through all of them and replacing MD with HTML, which is obviously not the most clean solution;
* however, the Model can be configured to return MD instead of HTML on that specific field saving us from looping.
*/
Vacancy::create([
'vacancyName' => $request->vacancyName,
'vacancyDescription' => $request->vacancyDescription,
2020-07-11 19:34:26 +00:00
'vacancyFullDescription' => $request->vacancyFullDescription,
'vacancySlug' => Str::slug($request->vacancyName),
'permissionGroupName' => $request->permissionGroup,
'discordRoleID' => $request->discordRole,
'vacancyFormID' => $request->vacancyFormID,
2020-10-10 16:30:26 +00:00
'vacancyCount' => $request->vacancyCount,
]);
$message = __('Vacancy successfully opened. It will now show in the home page.');
2020-10-10 16:30:26 +00:00
} else {
$message = __('You cannot create a vacancy without a valid form.');
$messageIsError = true;
}
return redirect()
->back()
->with(($messageIsError) ? 'error' : 'success', $message);
}
public function updatePositionAvailability(Request $request, $status, Vacancy $vacancy)
{
$this->authorize('update', $vacancy);
2020-10-10 16:30:26 +00:00
if (! is_null($vacancy)) {
$type = 'success';
2020-10-10 16:30:26 +00:00
switch ($status) {
case 'open':
$vacancy->open();
$message = __('Position successfully opened!');
break;
case 'close':
$vacancy->close();
$message = __('Position successfully closed!');
2020-10-10 16:30:26 +00:00
foreach (User::all() as $user) {
if ($user->isStaffMember()) {
$user->notify(new VacancyClosed($vacancy));
}
}
break;
default:
$message = __("Please do not tamper with the URLs. To report a bug, please contact an administrator.");
$type = 'error';
}
2020-10-10 16:30:26 +00:00
} else {
$message = __("The position you're trying to update doesn't exist!");
2020-10-10 16:30:26 +00:00
$type = 'error';
}
return redirect()
->back()
->with($type, $message);
2020-10-10 16:30:26 +00:00
2020-05-03 04:04:26 +00:00
}
2020-09-03 01:50:19 +00:00
public function edit(Request $request, Vacancy $vacancy)
2020-07-11 19:34:26 +00:00
{
2020-10-10 16:30:26 +00:00
$this->authorize('update', $vacancy);
2020-07-11 19:34:26 +00:00
return view('dashboard.administration.editposition')
2020-09-03 01:52:21 +00:00
->with('vacancy', $vacancy);
2020-07-11 19:34:26 +00:00
}
2020-07-25 00:20:43 +00:00
public function update(VacancyEditRequest $request, Vacancy $vacancy)
2020-07-11 19:34:26 +00:00
{
2020-10-10 16:30:26 +00:00
$this->authorize('update', $vacancy);
2020-07-11 19:34:26 +00:00
$vacancy->vacancyFullDescription = $request->vacancyFullDescription;
2020-10-10 16:30:26 +00:00
$vacancy->vacancyDescription = $request->vacancyDescription;
$vacancy->vacancyCount = $request->vacancyCount;
2020-07-11 19:34:26 +00:00
2020-10-10 16:30:26 +00:00
$vacancy->save();
2020-07-11 19:34:26 +00:00
return redirect()
->back()
->with('success', __('Vacancy successfully updated.'));
2020-07-11 19:34:26 +00:00
}
}