Wykończenie szkoleń/badań - soft delete szkolenia, usuwanie szkolenia strażakowi, dodanie działających ostrzeżeń przy edycji/usuwaniu, naprawa zmiany komórki w formularz - Do zrobienia: zmiana nazwy szkolenia, ostrzeżenie przy soft deletee

This commit is contained in:
czup 2020-01-03 05:13:22 +01:00
parent 969b9126c8
commit 40c0f2ef33
6 changed files with 147 additions and 101 deletions

View File

@ -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 )

View File

@ -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');
}
}

View File

@ -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'];
}

View File

@ -37,12 +37,11 @@
<td>Akcje</td>
@foreach($trainings as $training)
<tr>
<form action="{{ route('equipment.destroy', $training->id)}}" method="post">
<form action="{{ route('trainings.destroy', $training->id)}}" method="post">
<td id="name{{ $training->id }}">{{ $training->trainingName }}</td>
<td id="amount{{ $training->id }}">5</td>
<td><input type="button" onclick="showMoreInformation('{{$training->id}}')" id="more{{$training->id}}" value="Więcej">
<a href="{{ URL::asset('sprzet/edit/'.$training->id) }}"><input type="button" onclick="" value="Edytuj"> </a>
<a href="{{ URL::asset('szkolenia/addTrainingsFireFighters/'.$training->id) }}"><input type="button" onclick="" value="Dodaj zbadanych"></a>
<a href="{{ URL::asset('szkolenia/addTrainingsFireFighters/'.$training->id) }}"><input type="button" onclick="" value="Zarządzaj"></a>
{{ csrf_field() }}
@method('DELETE')
<button class="btn btn-danger" type="submit">Usuń</button>

View File

@ -4,7 +4,8 @@
<title>Add Edit Delete Table Row Example using JQuery - ItSolutionStuff.com</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<meta name="csrf-token" content="{{ csrf_token() }}" />
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<meta name="csrf-token" content="{{ csrf_token() }}">
@section('left-menu')
@parent
@ -39,7 +40,7 @@
<td>{{$fireFighter->name}} {{$fireFighter->surname}}</td>
<td>{{$fireFighter->dateOfComplete}}</td>
<td>{{$fireFighter->dateOfExpiry}}</td>
<td><button class="btn btn-warning" type="submit" style="display:none" id="{{$fireFighter->userID}}" onclick="cancelButton('{{$fireFighter->userID}}')">Anuluj</button> <button class="btn btn-success" type="submit" style="display:none" id="{{$fireFighter->userID}}" onclick="updateButton('{{$fireFighter->userID}}')">Zapisz</button> <button class="btn btn-info" type="submit" id="{{$fireFighter->userID}}" onclick="editButton('{{$fireFighter->userID}}')">Edytuj</button> <button class="btn btn-danger btn-delete" type="submit" id="{{$fireFighter->userID}}">Usuń</button></td>
<td><button class="btn btn-warning" type="submit" style="display:none" id="{{$fireFighter->userID}}" onclick="cancelButton('{{$fireFighter->userID}}', '{{$fireFighter->dateOfComplete}}', '{{$fireFighter->dateOfExpiry}}')">Anuluj</button> <button class="btn btn-success" type="submit" style="display:none" id="{{$fireFighter->userID}}" onclick="updateButton('{{$fireFighter->userID}}', '{{$fireFighter->dateOfComplete}}', '{{$fireFighter->dateOfExpiry}}')">Zapisz</button> <button class="btn btn-info" type="submit" id="{{$fireFighter->userID}}" onclick="editButton('{{$fireFighter->userID}}')">Edytuj</button> <button class="btn btn-danger btn-delete" type="submit" id="{{$fireFighter->userID}}" onclick="deleteButton('{{$fireFighter->userID}}')">Usuń</button></td>
</tr>
@endforeach
</tbody>
@ -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 += '<ul class="list-group"><li class="list-group-item alert alert-danger">' + value + '</li></ul>';
});
//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("<tr data-name='"+name+"' data-email='"+email+"'><td>"+name+"</td><td>"+email+"</td><td><button class='btn btn-info btn-xs btn-edit'>Edit</button><button class='btn btn-danger btn-xs btn-delete'>Delete</button></td></tr>");
//
// $("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('<input name="edit_name" value="'+name+'">');
// $(this).parents("tr").find("td:eq(1)").html('<input name="edit_email" value="'+email+'">');
//
// $(this).parents("tr").find("td:eq(2)").prepend("<button class='btn btn-info btn-xs btn-update'>Update</button><button class='btn btn-warning btn-xs btn-cancel'>Cancel</button>")
// $(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('');
}
});
}
})
}
</script>

View File

@ -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');