2020-10-03 20:36:35 +00:00
|
|
|
<?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-03 20:36:35 +00:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\EditTeamRequest;
|
|
|
|
use App\Http\Requests\NewTeamRequest;
|
|
|
|
use App\Http\Requests\SendInviteRequest;
|
|
|
|
use App\Mail\InviteToTeam;
|
|
|
|
use App\Team;
|
|
|
|
use App\User;
|
2020-10-08 18:19:10 +00:00
|
|
|
use App\Vacancy;
|
2020-10-03 20:36:35 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
2020-10-08 18:19:10 +00:00
|
|
|
use Mpociot\Teamwork\Exceptions\UserNotInTeamException;
|
2020-10-03 20:36:35 +00:00
|
|
|
use Mpociot\Teamwork\Facades\Teamwork;
|
|
|
|
use Mpociot\Teamwork\TeamInvite;
|
|
|
|
|
|
|
|
class TeamController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2020-10-08 18:19:10 +00:00
|
|
|
$teams = Team::with('users.roles')->get();
|
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
return view('dashboard.teams.teams')
|
2020-10-08 18:19:10 +00:00
|
|
|
->with('teams', $teams);
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(NewTeamRequest $request)
|
|
|
|
{
|
2020-10-08 18:19:10 +00:00
|
|
|
$team = Team::create([
|
2020-10-03 20:36:35 +00:00
|
|
|
'name' => $request->teamName,
|
2020-10-10 16:30:26 +00:00
|
|
|
'owner_id' => Auth::user()->id,
|
2020-10-03 20:36:35 +00:00
|
|
|
]);
|
|
|
|
|
2020-10-08 18:19:10 +00:00
|
|
|
Auth::user()->teams()->attach($team->id);
|
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
$request->session()->flash('success', 'Team successfully created.');
|
2020-10-10 16:30:26 +00:00
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit(Team $team)
|
|
|
|
{
|
|
|
|
return view('dashboard.teams.edit-team')
|
|
|
|
->with('team', $team)
|
2020-10-08 18:19:10 +00:00
|
|
|
->with('users', User::all())
|
2020-10-09 21:27:36 +00:00
|
|
|
->with('vacancies', Vacancy::with('teams')->get()->all());
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(EditTeamRequest $request, Team $team)
|
|
|
|
{
|
|
|
|
$team->description = $request->teamDescription;
|
|
|
|
$team->openJoin = $request->joinType;
|
|
|
|
|
|
|
|
$team->save();
|
|
|
|
|
|
|
|
$request->session()->flash('success', 'Team edited successfully.');
|
2020-10-10 16:30:26 +00:00
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
return redirect()->to(route('teams.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invite(SendInviteRequest $request, Team $team)
|
2020-10-09 21:27:36 +00:00
|
|
|
{
|
2020-10-03 20:36:35 +00:00
|
|
|
$user = User::findOrFail($request->user);
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
if (! $team->openJoin) {
|
|
|
|
if (! Teamwork::hasPendingInvite($user->email, $team)) {
|
|
|
|
Teamwork::inviteToTeam($user, $team, function (TeamInvite $invite) use ($user) {
|
2020-10-03 20:36:35 +00:00
|
|
|
Mail::to($user)->send(new InviteToTeam($invite));
|
|
|
|
});
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
$request->session()->flash('success', 'Invite sent! They can now accept or deny it.');
|
2020-10-10 16:30:26 +00:00
|
|
|
} else {
|
2020-10-03 20:36:35 +00:00
|
|
|
$request->session()->flash('error', 'This user has already been invited.');
|
|
|
|
}
|
2020-10-10 16:30:26 +00:00
|
|
|
} else {
|
2020-10-03 20:36:35 +00:00
|
|
|
$request->session()->flash('error', 'You can\'t invite users to public teams.');
|
|
|
|
}
|
|
|
|
|
2020-10-09 21:27:36 +00:00
|
|
|
return redirect()->back();
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processInviteAction(Request $request, $action, $token)
|
|
|
|
{
|
2020-10-10 16:30:26 +00:00
|
|
|
switch ($action) {
|
2020-10-03 20:36:35 +00:00
|
|
|
case 'accept':
|
|
|
|
|
|
|
|
$invite = Teamwork::getInviteFromAcceptToken($token);
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
if ($invite && $invite->user->is(Auth::user())) {
|
2020-10-03 20:36:35 +00:00
|
|
|
Teamwork::acceptInvite($invite);
|
2020-10-10 16:30:26 +00:00
|
|
|
$request->session()->flash('success', 'Invite accepted! You have now joined '.$invite->team->name.'.');
|
|
|
|
} else {
|
2020-10-03 20:36:35 +00:00
|
|
|
$request->session()->flash('error', 'Invalid or expired invite URL.');
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'deny':
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
$invite = Teamwork::getInviteFromDenyToken($token);
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
if ($invite && $invite->user->is(Auth::user())) {
|
2020-10-03 20:36:35 +00:00
|
|
|
Teamwork::denyInvite($invite);
|
|
|
|
$request->session()->flash('success', 'Invite denied! Ask for another invite if this isn\'t what you meant.');
|
2020-10-10 16:30:26 +00:00
|
|
|
} else {
|
2020-10-03 20:36:35 +00:00
|
|
|
$request->session()->flash('error', 'Invalid or expired invite URL.');
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
default:
|
|
|
|
$request->session()->flash('error', 'Sorry, but the invite URL you followed was malformed. Try asking for another invite, or submit a bug report.');
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This page will show the user's current teams
|
|
|
|
return redirect()->to(route('teams.index'));
|
|
|
|
}
|
2020-10-08 18:19:10 +00:00
|
|
|
|
|
|
|
public function switchTeam(Request $request, Team $team)
|
|
|
|
{
|
2020-10-10 16:30:26 +00:00
|
|
|
try {
|
2020-10-08 18:19:10 +00:00
|
|
|
Auth::user()->switchTeam($team);
|
|
|
|
|
|
|
|
$request->session()->flash('success', 'Switched teams! Your team dashboard will now use this context.');
|
2020-10-10 16:30:26 +00:00
|
|
|
} catch (UserNotInTeamException $ex) {
|
2020-10-08 18:19:10 +00:00
|
|
|
$request->session()->flash('error', 'You can\'t switch to a team you don\'t belong to.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
2020-10-09 21:27:36 +00:00
|
|
|
|
|
|
|
// Since it's a separate form, we shouldn't use the same update method
|
|
|
|
public function assignVacancies(Request $request, Team $team)
|
|
|
|
{
|
|
|
|
// P.S. To future developers
|
|
|
|
// This method gave me a lot of trouble lol. It's hard to write code when you're half asleep.
|
|
|
|
// There may be an n+1 query in the view and I don't think there's a way to avoid that without writing a lot of extra code.
|
|
|
|
|
|
|
|
$requestVacancies = $request->assocVacancies;
|
|
|
|
$currentVacancies = $team->vacancies->pluck('id')->all();
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
if (is_null($requestVacancies)) {
|
|
|
|
foreach ($team->vacancies as $vacancy) {
|
2020-10-09 21:27:36 +00:00
|
|
|
$team->vacancies()->detach($vacancy->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->session()->flash('success', 'Removed all vacancy associations.');
|
2020-10-10 16:30:26 +00:00
|
|
|
|
2020-10-09 21:27:36 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
$vacancyDiff = array_diff($requestVacancies, $currentVacancies);
|
|
|
|
$deselectedDiff = array_diff($currentVacancies, $requestVacancies);
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
if (! empty($vacancyDiff) || ! empty($deselectedDiff)) {
|
|
|
|
foreach ($vacancyDiff as $selectedVacancy) {
|
2020-10-09 21:27:36 +00:00
|
|
|
$team->vacancies()->attach($selectedVacancy);
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
foreach ($deselectedDiff as $deselectedVacancy) {
|
2020-10-09 21:27:36 +00:00
|
|
|
$team->vacancies()->detach($deselectedVacancy);
|
|
|
|
}
|
2020-10-10 16:30:26 +00:00
|
|
|
} else {
|
2020-10-09 21:27:36 +00:00
|
|
|
$team->vacancies()->attach($requestVacancies);
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->session()->flash('success', 'Assignments changed successfully.');
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
return redirect()->back();
|
2020-10-09 21:27:36 +00:00
|
|
|
}
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|