2019-12-25 22:35:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-01-04 22:04:28 +01:00
|
|
|
use App\equipment;
|
2019-12-25 22:35:20 +01:00
|
|
|
use App\trainings;
|
|
|
|
use App\trainingsFirefighters;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\View;
|
2020-01-03 05:13:22 +01:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2019-12-25 22:35:20 +01:00
|
|
|
|
|
|
|
class trainingsController extends Controller
|
|
|
|
{
|
|
|
|
public function create(){
|
|
|
|
|
|
|
|
if(auth()->user() != null && auth()->user()->fireStationID != null ){
|
|
|
|
$trainings = DB::table('trainings')->where("fireStationID", '=', auth()->user()->fireStationID)
|
2020-01-29 01:57:19 +01:00
|
|
|
->whereNull('deleted_at')
|
|
|
|
->paginate(10);
|
2020-01-03 05:13:22 +01:00
|
|
|
|
|
|
|
$fireFighters = array();
|
2019-12-25 22:35:20 +01:00
|
|
|
foreach($trainings as $training) {
|
|
|
|
$id = $training->id;
|
|
|
|
$fireFighters[$id] = DB::table('users')->where("fireStationID", "=", auth()->user()->fireStationID)
|
|
|
|
->leftJoin('trainingsFirefighters', function ($join) use ($id) {
|
|
|
|
$join->on('users.id', '=', 'trainingsFirefighters.firefighterID');
|
|
|
|
$join->where('trainingsFirefighters.trainingID', '=', $id);
|
|
|
|
})
|
|
|
|
->select('trainingsFirefighters.*', 'users.name', 'users.surname', 'users.id as userID')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
return View::make("trainings")->with(compact( "trainings", "fireFighters"));
|
|
|
|
} else{
|
|
|
|
return view('trainings');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:04:28 +01:00
|
|
|
public function store(){
|
|
|
|
$this->validate(request(), [
|
2020-01-31 03:13:13 +01:00
|
|
|
'name' => 'required|max:45',
|
2020-01-04 22:04:28 +01:00
|
|
|
],
|
|
|
|
[
|
2020-01-31 03:13:13 +01:00
|
|
|
'required' => 'Nazwa jest wymagana.',
|
|
|
|
'max' => 'Nazwa musi mieć nie więcej niż :max znaków.',
|
2020-01-04 22:04:28 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$request = request();
|
|
|
|
$equipment = trainings::create([
|
|
|
|
'fireStationID' => auth()->user()->fireStationID,
|
|
|
|
'trainingName' => $request-> name,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return redirect()->to('/szkolenia');
|
|
|
|
}
|
|
|
|
|
2019-12-25 22:35:20 +01:00
|
|
|
public function addForm(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addTrainingsFireFighters($id){
|
|
|
|
|
|
|
|
if(auth()->user() != null && auth()->user()->fireStationID != null ){
|
|
|
|
$fireFighters = DB::table('users')->where("fireStationID", "=", auth()->user()->fireStationID )
|
|
|
|
->leftJoin('trainingsFirefighters', function ($join) use($id){
|
|
|
|
$join->on('users.id', '=', 'trainingsFirefighters.firefighterID');
|
|
|
|
$join->where('trainingsFirefighters.trainingID', '=', $id);
|
|
|
|
})
|
|
|
|
->select('trainingsFirefighters.*', 'users.name', 'users.surname', 'users.id as userID')
|
2020-01-29 01:57:19 +01:00
|
|
|
->paginate(10);
|
2019-12-25 22:35:20 +01:00
|
|
|
|
|
|
|
$training = DB::table('trainings')->where("id", '=', $id)
|
|
|
|
->whereNull('deleted_at')->first();
|
|
|
|
return View::make("trainingsAddFireFighters")->with(compact( "training", "fireFighters"));
|
|
|
|
} else{
|
|
|
|
return view('trainings');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 05:13:22 +01:00
|
|
|
public function ajaxRequest(Request $request){
|
|
|
|
$validator = Validator::make($request->all(), [
|
2019-12-25 22:35:20 +01:00
|
|
|
'firefighterID' => 'required',
|
|
|
|
'trainingID' => 'required',
|
2020-01-03 05:13:22 +01:00
|
|
|
'dateOfComplete' => 'required|date',
|
|
|
|
'dateOfExpiry' => 'required|date',
|
2019-12-25 22:35:20 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'required' => ':attribute jest wymagany(a).',
|
2020-01-03 05:13:22 +01:00
|
|
|
'dateOfComplete.required' => 'Data wystawienia szkolenia/badania jest wymagana.',
|
|
|
|
'dateOfExpiry.required' => 'Data ważności szkolenia/badania jest wymagana.'
|
2019-12-25 22:35:20 +01:00
|
|
|
]);
|
|
|
|
|
2020-01-03 05:13:22 +01:00
|
|
|
|
|
|
|
// WALIDACJA
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
|
|
|
if($request->ajax())
|
|
|
|
{
|
|
|
|
return response()->json(array(
|
|
|
|
'success' => false,
|
|
|
|
'message' => 'There are incorect values in the form!',
|
|
|
|
'errors' => $validator->getMessageBag()->toArray()
|
|
|
|
), 422);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->throwValidationException(
|
|
|
|
|
|
|
|
$request, $validator
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
// WYKONANIE ZAPYTAŃ PO POPRAWNEJ WALIDACJI
|
|
|
|
} else{
|
|
|
|
|
|
|
|
$trainingFirefighter = trainingsFirefighters::where([
|
|
|
|
['firefighterID', '=', $request->firefighterID],
|
|
|
|
['trainingID', '=', $request-> trainingID]
|
|
|
|
])->first();
|
|
|
|
|
|
|
|
if($trainingFirefighter == null){
|
|
|
|
// CREATE
|
|
|
|
$trainings = trainingsFirefighters::create([
|
|
|
|
'firefighterID' => $request-> firefighterID,
|
|
|
|
'trainingID' => $request-> trainingID,
|
|
|
|
'dateOfComplete' => $request-> dateOfComplete,
|
|
|
|
'dateOfExpiry' => $request-> dateOfExpiry,
|
|
|
|
'lifetime' => 1,
|
|
|
|
]);
|
|
|
|
}else{
|
|
|
|
// UPDATE
|
|
|
|
$trainingFirefighter->update([
|
|
|
|
'dateOfComplete' => $request-> dateOfComplete,
|
|
|
|
'dateOfExpiry' => $request-> dateOfExpiry,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-25 22:35:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-03 05:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteFireFighterTrainings(Request $request){
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
'firefighterID' => 'required',
|
|
|
|
'trainingID' => 'required',
|
|
|
|
]);
|
|
|
|
|
|
|
|
trainingsFirefighters::where([
|
|
|
|
['fireFighterID', '=', $request->firefighterID],
|
|
|
|
['trainingID', '=', $request->trainingID],
|
|
|
|
])->delete();
|
2019-12-25 22:35:20 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(){
|
|
|
|
|
|
|
|
}
|
2020-01-03 05:13:22 +01:00
|
|
|
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2020-01-29 01:57:19 +01:00
|
|
|
trainings::find($id)->delete($id);
|
2020-01-03 05:13:22 +01:00
|
|
|
|
2020-01-29 01:57:19 +01:00
|
|
|
return response()->json([
|
|
|
|
'success' => 'Record deleted successfully!'
|
|
|
|
]);
|
2020-01-03 05:13:22 +01:00
|
|
|
}
|
2020-01-04 22:04:28 +01:00
|
|
|
|
|
|
|
public function trainingsRename(Request $request){
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
'trainingID' => 'required',
|
|
|
|
'trainingName' => 'required',
|
|
|
|
]);
|
|
|
|
|
|
|
|
trainings::where('id', '=', $request->trainingID)
|
|
|
|
->update(['trainingName' => $request->trainingName]);
|
|
|
|
}
|
2019-12-25 22:35:20 +01:00
|
|
|
}
|