124 lines
3.5 KiB
PHP
124 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\BowlerChart;
|
|
use App\Models\Group;
|
|
use App\Support\BowlerGrid;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class BowlerChartController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$user = $request->user();
|
|
|
|
$charts = $user->isAdmin()
|
|
? BowlerChart::with('group')->get()
|
|
: BowlerChart::with('group')
|
|
->whereIn('group_id', $user->groups()->select('groups.id'))
|
|
->get();
|
|
|
|
return view('charts.index', [
|
|
'chartsByGroup' => $charts->sortBy('name')->groupBy(fn ($c) => $c->group->name)->sortKeys(),
|
|
]);
|
|
}
|
|
|
|
public function create(Request $request): View
|
|
{
|
|
return view('charts.create', [
|
|
'groups' => $this->manageableGroups($request),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$data = $request->validate([
|
|
'group_id' => ['required', 'integer', 'exists:groups,id'],
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'description' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
|
|
if (! $request->user()->isAdmin() && ! $request->user()->ownsGroup((int) $data['group_id'])) {
|
|
abort(403, 'You must be an owner of the group to create charts in it.');
|
|
}
|
|
|
|
$chart = BowlerChart::create($data);
|
|
|
|
return redirect()
|
|
->route('charts.show', $chart)
|
|
->with('status', "Bowler chart \"{$chart->name}\" created.");
|
|
}
|
|
|
|
public function show(BowlerChart $chart): View
|
|
{
|
|
$this->authorize('view', $chart);
|
|
|
|
return view('charts.show', [
|
|
'chart' => $chart,
|
|
'months' => BowlerGrid::months(),
|
|
'groupedRows' => BowlerGrid::groupedRows($chart->visibleKpis(), $chart->visibleProjects()),
|
|
]);
|
|
}
|
|
|
|
public function edit(BowlerChart $chart): View
|
|
{
|
|
$this->authorize('update', $chart);
|
|
|
|
return view('charts.edit', ['chart' => $chart]);
|
|
}
|
|
|
|
public function update(Request $request, BowlerChart $chart): RedirectResponse
|
|
{
|
|
$this->authorize('update', $chart);
|
|
|
|
$data = $request->validate([
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'description' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
|
|
$chart->update($data);
|
|
|
|
return redirect()
|
|
->route('charts.show', $chart)
|
|
->with('status', "Bowler chart \"{$chart->name}\" updated.");
|
|
}
|
|
|
|
public function destroy(BowlerChart $chart): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $chart);
|
|
|
|
$name = $chart->name;
|
|
$chart->delete();
|
|
|
|
return redirect()
|
|
->route('charts.index')
|
|
->with('status', "Bowler chart \"{$name}\" deleted.");
|
|
}
|
|
|
|
/**
|
|
* The user's personal bowler chart: all of their own items, always available.
|
|
*/
|
|
public function personal(Request $request): View
|
|
{
|
|
return view('charts.personal', [
|
|
'months' => BowlerGrid::months(),
|
|
'groupedRows' => BowlerGrid::groupedRows(
|
|
$request->user()->kpis()->get(),
|
|
$request->user()->projects()->get(),
|
|
),
|
|
]);
|
|
}
|
|
|
|
private function manageableGroups(Request $request)
|
|
{
|
|
$user = $request->user();
|
|
|
|
return $user->isAdmin()
|
|
? Group::orderBy('name')->get()
|
|
: $user->groups()->wherePivot('role', 'owner')->get();
|
|
}
|
|
}
|