auditionadmin/app/Http/Controllers/Admin/EnsembleController.php

129 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\EnsembleStoreOrUpdateRequest;
use App\Models\Ensemble;
use App\Models\Event;
use App\Models\SeatingLimit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use function redirect;
class EnsembleController extends Controller
{
public function index()
{
$events = Event::with('ensembles')->get();
return view('admin.ensembles.index', compact('events'));
}
public function store(EnsembleStoreOrUpdateRequest $request)
{
Log::channel('file')->warning('hello');
$validated = $request->validated();
// get the maximum value of rank from the ensemble table where event_id is equal to the request event_id
$maxCode = Ensemble::where('event_id', request('event_id'))->max('rank');
Ensemble::create([
'name' => $validated['name'],
'code' => $validated['code'],
'event_id' => $validated['event_id'],
'rank' => $maxCode + 1,
]);
return redirect()->route('admin.ensembles.index')->with('success', 'Ensemble created successfully');
}
public function destroy(Ensemble $ensemble)
{
if ($ensemble->seats->count() > 0) {
return redirect()->route('admin.ensembles.index')->with('error',
'Ensemble has students seated and cannot be deleted');
}
$ensemble->delete();
return redirect()->route('admin.ensembles.index')->with('success', 'Ensemble deleted successfully');
}
public function update(EnsembleStoreOrUpdateRequest $request, Ensemble $ensemble)
{
$valid = $request->validated();
$ensemble->update([
'name' => $valid['name'],
'code' => $valid['code'],
]);
return redirect()->route('admin.ensembles.index')->with('success', 'Ensemble updated successfully');
}
//TODO Consider moving seating limit related functions to their own controller with index, edit, and update methods
public function seatingLimits(Ensemble $ensemble)
{
$limits = [];
/**
* If we weren't called with an ensemble, we're going to use an array of ensembles to fill a drop-down and
* choose one. The user will be sent back here, this time with the chosen audition.
*/
$ensembles = Ensemble::with(['event'])->orderBy('event_id')->orderBy('rank')->get();
/**
* If we were called with an ensemble, we need to load existing seating limits. We will put them in an array
* indexed by audition_id for easy use in the form to set seating limits.
*/
if ($ensemble->exists()) {
$ensemble->load('seatingLimits');
foreach ($ensemble->seatingLimits as $lim) {
$limits[$lim->audition_id] = $lim->maximum_accepted;
}
}
return view('admin.ensembles.seatingLimits', compact('ensemble', 'ensembles', 'limits'));
}
public function seatingLimitsSet(Request $request, Ensemble $ensemble)
{
Cache::forget('auditionSeatingLimits');
$request->validate([
'audition' => 'required',
'audition.*' => ['integer', 'min:0'],
]);
foreach ($ensemble->auditions as $audition) {
SeatingLimit::upsert(
[
[
'ensemble_id' => $ensemble->id,
'audition_id' => $audition->id,
'maximum_accepted' => $request->audition[$audition->id],
],
],
uniqueBy: ['ensemble_id', 'audition_id'],
update: ['maximum_accepted']
);
}
return redirect()->route('admin.ensembles.seatingLimits.ensemble', $ensemble)->with('success',
'Seating limits set for '.$ensemble->name);
}
public function updateEnsembleRank(Request $request)
{
$order = $request->input('order');
$eventId = $request->input('event_id');
foreach ($order as $item) {
Ensemble::where('id', $item['id'])
->where('event_id', $eventId)
->update(['rank' => $item['rank']]);
}
return response()->json(['status' => 'success']);
}
}