2023-09-07 14:01:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\Training;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Http\Request;
|
2024-01-02 01:21:13 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2023-09-07 14:01:54 +02:00
|
|
|
use App\Utils\Logger;
|
|
|
|
|
|
|
|
class TrainingController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2024-01-07 18:43:52 +01:00
|
|
|
if(!$request->user()->hasPermission("trainings-read")) abort(401);
|
2023-09-07 14:01:54 +02:00
|
|
|
User::where('id', $request->user()->id)->update(['last_access' => now()]);
|
|
|
|
|
2024-01-02 01:21:13 +01:00
|
|
|
$query = Training::join('users', 'users.id', '=', 'chief_id')
|
|
|
|
->select('trainings.*', 'users.name as chief')
|
|
|
|
->with('crew:name')
|
|
|
|
->orderBy('start', 'desc')
|
|
|
|
->get();
|
|
|
|
if($request->has('from')) {
|
|
|
|
try {
|
|
|
|
$from = Carbon::parse($request->input('from'));
|
|
|
|
$query->whereDate('start', '>=', $from->toDateString());
|
|
|
|
} catch (\Carbon\Exceptions\InvalidFormatException $e) { }
|
|
|
|
}
|
|
|
|
if($request->has('to')) {
|
|
|
|
try {
|
|
|
|
$to = Carbon::parse($request->input('to'));
|
|
|
|
$query->whereDate('start', '<=', $to->toDateString());
|
|
|
|
} catch (\Carbon\Exceptions\InvalidFormatException $e) { }
|
|
|
|
}
|
2023-09-07 14:01:54 +02:00
|
|
|
return response()->json(
|
2024-01-02 01:21:13 +01:00
|
|
|
$query->get()
|
2023-09-07 14:01:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get single Training
|
|
|
|
*/
|
|
|
|
public function show(Request $request, $id)
|
|
|
|
{
|
2024-01-07 18:43:52 +01:00
|
|
|
if(!$request->user()->hasPermission("trainings-read")) abort(401);
|
2023-09-07 14:01:54 +02:00
|
|
|
User::where('id', $request->user()->id)->update(['last_access' => now()]);
|
|
|
|
|
|
|
|
return response()->json(
|
|
|
|
Training::join('users', 'users.id', '=', 'chief_id')
|
|
|
|
->select('trainings.*', 'users.name as chief')
|
|
|
|
->with('crew:name')
|
|
|
|
->find($id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function extractTrainingUsers($training)
|
|
|
|
{
|
|
|
|
$usersList = [$training->chief_id];
|
|
|
|
foreach($training->crew as $crew) {
|
|
|
|
$usersList[] = $crew->id;
|
|
|
|
}
|
|
|
|
return array_unique($usersList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add or update Training.
|
|
|
|
*/
|
|
|
|
public function createOrUpdate(Request $request)
|
|
|
|
{
|
|
|
|
$adding = !isset($request->id) || is_null($request->id);
|
|
|
|
|
2024-01-07 18:43:52 +01:00
|
|
|
if(!$adding && !$request->user()->hasPermission("trainings-update")) abort(401);
|
|
|
|
if($adding && !$request->user()->hasPermission("trainings-create")) abort(401);
|
|
|
|
|
2023-09-07 14:01:54 +02:00
|
|
|
$training = $adding ? new Training() : Training::where("id",$request->id)->with('crew')->first();
|
|
|
|
|
|
|
|
if(is_null($training)) abort(404);
|
|
|
|
|
|
|
|
if(!$adding) {
|
|
|
|
$usersToDecrement = $this->extractTrainingUsers($training);
|
|
|
|
User::whereIn('id', $usersToDecrement)->decrement('trainings');
|
|
|
|
|
|
|
|
$training->crew()->detach();
|
|
|
|
$training->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$training->name = $request->name;
|
|
|
|
$training->chief()->associate($request->chief);
|
|
|
|
$training->notes = $request->notes;
|
|
|
|
$training->start = $request->start/1000;
|
|
|
|
$training->end = $request->end/1000;
|
|
|
|
$training->place = $request->place;
|
|
|
|
$training->addedBy()->associate($request->user());
|
|
|
|
$training->updatedBy()->associate($request->user());
|
|
|
|
$training->save();
|
|
|
|
|
|
|
|
$training->crew()->attach(array_unique($request->crew));
|
|
|
|
$training->save();
|
|
|
|
|
|
|
|
$usersToIncrement = array_unique(array_merge(
|
|
|
|
[$request->chief],
|
|
|
|
$request->crew
|
|
|
|
));
|
|
|
|
User::whereIn('id', $usersToIncrement)->increment('trainings');
|
|
|
|
|
|
|
|
Logger::log($adding ? "Esercitazione aggiunta" : "Esercitazione modificata");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*/
|
2024-01-07 18:43:52 +01:00
|
|
|
public function destroy(Request $request, $id)
|
2023-09-07 14:01:54 +02:00
|
|
|
{
|
2024-01-07 18:43:52 +01:00
|
|
|
if(!$request->user()->hasPermission("trainings-delete")) abort(401);
|
2023-09-07 14:01:54 +02:00
|
|
|
$training = Training::find($id);
|
|
|
|
$usersToDecrement = $this->extractTrainingUsers($training);
|
|
|
|
User::whereIn('id', $usersToDecrement)->decrement('trainings');
|
|
|
|
$training->delete();
|
|
|
|
Logger::log("Esercitazione eliminata");
|
|
|
|
}
|
|
|
|
}
|