eOSP2/app/Http/Controllers/DecorationsController.php

75 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\User;
use App\decorationsFirefighters;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Carbon\Carbon; // formatowanie daty
function formatDate($date)
{
if ($date == null)
return $date;
else
$fdate = Carbon::parse($date);
return $fdate;
}
class DecorationsController extends Controller
{
public function create($id){
if(auth()->user() != null && auth()->user()->accessLevel() == 50 ){ //prezes,naczelnik
$awardedDecorations = DB::table('decorationsFirefighters')->where("decorationsFirefighters.firefighterID", '=', $id)
->whereNull('decorationsFirefighters.deleted_at')
->leftJoin('decorations', 'decorationsFirefighters.decorationID', '=', 'decorations.id')
->select('decorationName', 'dateOfAward', 'firefighterID', 'decorations.id AS decorationsId', 'decorationsFirefighters.id AS decorationsFirefightersID')
->get();
$alreadyAwarded = $awardedDecorations->pluck('decorationsId')->toArray(); // tablica id wszystkich wyróżnień już posiadanych
$firefighter = User::find($id, ['id', 'name', 'surname']);
$decoration = DB::table('decorations')
->whereNotIn('id', $alreadyAwarded) //ograniczenie wyboru do wyróżnień jeszcze nie posiadanych
->pluck('decorationName', 'id');
return View::make("decorations")
->with(compact('firefighter'))
->with(compact('awardedDecorations'))
->with(compact('decoration'));
} else{
return redirect()->to('/strazacy');
}
}
public function store()
{
$this->validate(request(),[
'decoration' => 'required',
'dateOfAward' => 'required',
],
[
'required' => ':attribute jest wymagany(e).',
]);
$request = request();
$decoration = decorationsFirefighters::create([
'firefighterID' => $request-> firefighterID,
'decorationID' => $request-> decoration,
'dateOfAward' => formatDate($request-> dateOfAward),
]);
return back();
}
public function destroy($id)
{
decorationsFirefighters::where('id',$id)->delete();
return response()->json([
'success' => 'Record deleted successfully!'
]);
}
}