diff --git a/app/Http/Controllers/operationsController.php b/app/Http/Controllers/operationsController.php index 032d9ae..eb68878 100644 --- a/app/Http/Controllers/operationsController.php +++ b/app/Http/Controllers/operationsController.php @@ -24,6 +24,8 @@ class operationsController extends Controller ->select('operations.id', 'operations.operationDate', 'operations.location', 'operations.target', 'operations.dangerType', 'operations.description', 'operations.commanderID', 'operations.fireStationID', 'users.name', 'users.surname') ->get(); + $fireFighters = array(); + $trucks = array(); foreach($operations as $operation){ $id =$operation->id; $fireFighters[$id] = DB::table('users')->where("fireStationID", "=", auth()->user()->fireStationID ) diff --git a/app/Http/Controllers/trainingsController.php b/app/Http/Controllers/trainingsController.php index 4ab312b..4a80175 100644 --- a/app/Http/Controllers/trainingsController.php +++ b/app/Http/Controllers/trainingsController.php @@ -7,7 +7,7 @@ use App\trainingsFirefighters; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\View; - +use Illuminate\Support\Facades\Validator; class trainingsController extends Controller { @@ -16,6 +16,8 @@ class trainingsController extends Controller if(auth()->user() != null && auth()->user()->fireStationID != null ){ $trainings = DB::table('trainings')->where("fireStationID", '=', auth()->user()->fireStationID) ->whereNull('deleted_at')->get(); + + $fireFighters = array(); foreach($trainings as $training) { $id = $training->id; $fireFighters[$id] = DB::table('users')->where("fireStationID", "=", auth()->user()->fireStationID) @@ -55,50 +57,90 @@ class trainingsController extends Controller } } - public function ajaxRequest(){ - $this->validate(request(), [ + public function ajaxRequest(Request $request){ + $validator = Validator::make($request->all(), [ 'firefighterID' => 'required', 'trainingID' => 'required', - 'dateOfComplete' => 'required', - 'dateOfExpiry' => 'required', + 'dateOfComplete' => 'required|date', + 'dateOfExpiry' => 'required|date', ], [ 'required' => ':attribute jest wymagany(a).', + 'dateOfComplete.required' => 'Data wystawienia szkolenia/badania jest wymagana.', + 'dateOfExpiry.required' => 'Data ważności szkolenia/badania jest wymagana.' ]); - $request = request(); - $trainingFirefighter = trainingsFirefighters::where([ - ['firefighterID', '=', $request->firefighterID], - ['trainingID', '=', $request-> trainingID] - ])->first(); + // 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, + ]); + } - 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, - ]); } -// $input = $request->all(); -// -// return response()->json(['success'=>'Got Simple Ajax Request.']); + } + + public function deleteFireFighterTrainings(Request $request){ + $validator = Validator::make($request->all(), [ + 'firefighterID' => 'required', + 'trainingID' => 'required', + ]); + + trainingsFirefighters::where([ + ['fireFighterID', '=', $request->firefighterID], + ['trainingID', '=', $request->trainingID], + ])->delete(); } public function update(){ } + + public function destroy($id) + { + trainings::where('id',$id)->delete(); + + return redirect()->to('/szkolenia'); + } } diff --git a/app/trainings.php b/app/trainings.php index 3ed7ae7..0683f30 100644 --- a/app/trainings.php +++ b/app/trainings.php @@ -3,10 +3,12 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; + class trainings extends Model { + use SoftDeletes; protected $primaryKey = 'id'; - protected $fillable = ['fireStationID', 'trainingName']; } diff --git a/resources/views/trainings.blade.php b/resources/views/trainings.blade.php index 30a768f..89ea4d4 100644 --- a/resources/views/trainings.blade.php +++ b/resources/views/trainings.blade.php @@ -37,12 +37,11 @@ Akcje @foreach($trainings as $training) -
+ {{ $training->trainingName }} 5 - - + {{ csrf_field() }} @method('DELETE') diff --git a/resources/views/trainingsAddFireFighters.blade.php b/resources/views/trainingsAddFireFighters.blade.php index 515cfe1..1c2e09c 100644 --- a/resources/views/trainingsAddFireFighters.blade.php +++ b/resources/views/trainingsAddFireFighters.blade.php @@ -4,7 +4,8 @@ Add Edit Delete Table Row Example using JQuery - ItSolutionStuff.com - + + @section('left-menu') @parent @@ -39,7 +40,7 @@ {{$fireFighter->name}} {{$fireFighter->surname}} {{$fireFighter->dateOfComplete}} {{$fireFighter->dateOfExpiry}} - + @endforeach @@ -64,16 +65,16 @@ } - function cancelButton(firefighterID){ + function cancelButton(firefighterID, dateOfComplete, dateOfExpiry){ $('.btn-info', '#'+firefighterID).css('display', 'inline'); $('.btn-warning', '#'+firefighterID).css('display', 'none'); $('.btn-success', '#'+firefighterID).css('display', 'none'); - $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(1)").html('{{$fireFighter->dateOfComplete}}'); - $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(2)").html('{{$fireFighter->dateOfExpiry}}'); + $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(1)").html(dateOfComplete); + $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(2)").html(dateOfExpiry); } - function updateButton(firefighterID){ + function updateButton(firefighterID, dateOfComplete, dateOfExpiry){ $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') @@ -91,77 +92,75 @@ 'dateOfExpiry': $('input[name=dateOfExpiry]').val() }, success: function(data) { - if ((data.errors)) { - $('.error').removeClass('hidden'); - $('.error').text(data.errors.name); - } else { - $('.error').remove(); $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(1)").html($('input[name=dateOfComplete]').val()); $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(2)").html($('input[name=dateOfExpiry]').val()); - } + + $('.btn-info', '#'+firefighterID).css('display', 'inline'); + $('.btn-warning', '#'+firefighterID).css('display', 'none'); + $('.btn-success', '#'+firefighterID).css('display', 'none'); + console.log('sukces') }, + error: function(jqXhr, json, errorThrown){// this are default for ajax errors + var errors = jqXhr.responseJSON; + var errorsHtml = ''; + $.each(errors['errors'], function (index, value) { + errorsHtml += ''; + }); + //I use SweetAlert2 for this + swal.fire({ + title: "Error " + jqXhr.status + ': ' + errorThrown,// this will output "Error 422: Unprocessable Entity" + html: errorsHtml, + width: 'auto', + confirmButtonText: 'Spróbuj ponownie', + cancelButtonText: 'Anuluj Edycję', + showCancelButton: true, + }).then((result) => { + if(result.value){ + console.log('spróbuj') + } else if(result.dismiss === Swal.DismissReason.cancel){ + console.log('anuluj'); + cancelButton(firefighterID, dateOfComplete, dateOfExpiry); + } + }) + }, + }); - $('.btn-info', '#'+firefighterID).css('display', 'inline'); - $('.btn-warning', '#'+firefighterID).css('display', 'none'); - $('.btn-success', '#'+firefighterID).css('display', 'none'); - {{--$('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(1)").html('{{$fireFighter->dateOfComplete}}');--}} - {{--$('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(2)").html('{{$fireFighter->dateOfExpiry}}');--}} + } - // $("form").submit(function(e){ - // e.preventDefault(); - // var name = $("input[name='name']").val(); - // var email = $("input[name='email']").val(); - // - // $(".data-table tbody").append(""+name+""+email+""); - // - // $("input[name='name']").val(''); - // $("input[name='email']").val(''); - // }); - // - // $("body").on("click", ".btn-delete", function(){ - // $(this).parents("tr").remove(); - // }); - // - // $("body").on("click", ".btn-edit", function(){ - // var name = $(this).parents("tr").attr('data-name'); - // var email = $(this).parents("tr").attr('data-email'); - // - // $(this).parents("tr").find("td:eq(0)").html(''); - // $(this).parents("tr").find("td:eq(1)").html(''); - // - // $(this).parents("tr").find("td:eq(2)").prepend("") - // $(this).hide(); - // }); - // - // $("body").on("click", ".btn-cancel", function(){ - // var name = $(this).parents("tr").attr('data-name'); - // var email = $(this).parents("tr").attr('data-email'); - // - // $(this).parents("tr").find("td:eq(0)").text(name); - // $(this).parents("tr").find("td:eq(1)").text(email); - // - // $(this).parents("tr").find(".btn-edit").show(); - // $(this).parents("tr").find(".btn-update").remove(); - // $(this).parents("tr").find(".btn-cancel").remove(); - // }); - // - // $("body").on("click", ".btn-update", function(){ - // var name = $(this).parents("tr").find("input[name='edit_name']").val(); - // var email = $(this).parents("tr").find("input[name='edit_email']").val(); - // - // $(this).parents("tr").find("td:eq(0)").text(name); - // $(this).parents("tr").find("td:eq(1)").text(email); - // - // $(this).parents("tr").attr('data-name', name); - // $(this).parents("tr").attr('data-email', email); - // - // $(this).parents("tr").find(".btn-edit").show(); - // $(this).parents("tr").find(".btn-cancel").remove(); - // $(this).parents("tr").find(".btn-update").remove(); - // }); + function deleteButton(firefighterID){ + swal.fire({ + title: "Czy chcesz wyczyścić?", + width: 'auto', + confirmButtonText: 'Tak', + cancelButtonText: 'Nie', + showCancelButton: true, + }).then((result) => { + if(result.value){ + $.ajaxSetup({ + headers: { + 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') + } + }); + + $.ajax({ + type: 'post', + url: '/szkolenia/addTrainingsFireFighters/delete/', + data: { + 'firefighterID': firefighterID, + 'trainingID': '{{$training->id}}', + }, + success: function(data) { + $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(1)").html(''); + $('.btn-info', '#'+firefighterID).parents("tr").find("td:eq(2)").html(''); + } + }); + } + }) + + } diff --git a/routes/web.php b/routes/web.php index 81fd1ce..1106b2b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -79,6 +79,8 @@ Route::get('/szkolenia', 'trainingsController@create'); Route::post('/szkolenia', 'trainingsController@store'); Route::get('/szkolenia/addTrainingsFireFighters/{id}', 'trainingsController@addTrainingsFireFighters'); Route::post('/szkolenia/addTrainingsFireFighters/', 'trainingsController@ajaxRequest'); +Route::post('/szkolenia/addTrainingsFireFighters/delete', 'trainingsController@deleteFireFighterTrainings'); +Route::resource('trainings', 'trainingsController'); Route::get('/userprofile', 'userProfileController@create'); Route::get('/userprofile/edit', 'userProfileController@editForm');