1
0
forked from s421507/eOSP2
eOSP2/app/Http/Controllers/userProfileController.php

127 lines
5.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\User;
use App\Rules\Pesel;
class userProfileController extends Controller
{
public function create(){
if(auth()->user() != null && auth()->user()->fireStationID != null ){
$userData = DB::table('users')->where("users.id", '=', auth()->user()->id)
->leftJoin('ranks', 'users.degreeID', '=', 'ranks.id')
->leftJoin('unitFunctions', 'users.functionID', '=', 'unitFunctions.id')
->select('users.id','users.name', 'users.surname', 'users.PESEL','users.phoneNumber', 'users.email', 'ranks.rank', 'unitFunctions.unitFunction', 'users.number', 'users.secondName', 'users.fathersName', 'users.mothersName', 'users.address', 'users.apartment', 'users.placeOfBirth', 'users.driversLicense', 'users.joiningOSPDate', 'users.IDSeries', 'users.IDNumber', 'users.IDValidUntil', 'users.identityCardNumber', 'users.homePhoneNumber', 'users.education', 'users.profession')
->first();
return view("userProfile", ["userData" => $userData]);
} else{
return redirect()->to('/login');
}
}
public function editForm()
{
if(auth()->user() != null && auth()->user()->fireStationID != null )
{
$userData = DB::table('users')->where("id", '=', auth()->user()->id)
->first();
return view("userProfileEdit", ["userData" => $userData]);
}
else
return redirect()->to('/login');
}
public function update(){
$this->validate(request(), [
'name' =>'required|min:2|max:45|regex:/^[A-Za-zżźćńółęąśŻŹĆĄŚĘŁÓŃ\040\x27-]+$/',
'surname' =>'required|min:2|max:45|regex:/^[A-Za-zżźćńółęąśŻŹĆĄŚĘŁÓŃ\040\x27-]+$/',
'PESEL' => new Pesel,
'phoneNumber' => 'required|digits:9',
'email' => 'required|email|unique:users,email,'.auth()->user()->id, //wymagaj unikalnego adresu email ale pozwól na zachowanie starego adresu
'secondName' => 'max:45',
'fathersName' => 'max:45',
'mothersName' => 'max:45',
'homePhoneNumber' => 'max:20',
'address' => 'max:45',
'apartment'=> 'max:45',
'placeOfBirth'=> 'max:45',
'IDSeries'=> 'max:3',
'IDNumber'=> 'max:6',
'identityCardNumber'=> 'max:20',
'driversLicense'=> 'max:20',
'education'=> 'max:45',
'profession'=> 'max:45',
],
[
'required' => ':attribute jest wymagany(e).',
'min' => ':attribute musi mieć przynajmniej :min znaki.',
'max' => ':attribute musi mieć nie więcej niż :max znaków.',
'regex' => ':attribute może zawierać tylko litery, spacje, myślniki i apostrofy',
'digits' => ':attribute musi składać się z :digits cyfr.',
'unique' =>':attribute jest już zajęty.',
'confirmed' =>':attribute się nie zgadza.',
'email' => 'Niepoprawny adres e-mail.'
]);
$request = request();
$user = User::find( $request->userID);
$user-> name = $request->name;
$user-> surname = $request->surname;
$user-> PESEL = $request->PESEL;
$user-> phoneNumber = $request->phoneNumber;
$user-> email = $request->email;
$user-> changingID = auth()->user()-> id;
$user-> secondName = $request->secondName;
$user-> fathersName = $request->fathersName;
$user-> mothersName = $request->mothersName;
$user-> homePhoneNumber = $request->homePhoneNumber;
$user-> address = $request->address;
$user-> apartment = $request->apartment;
$user-> placeOfBirth = $request->placeOfBirth;
$user-> joiningOSPDate = $request->joiningOSPDate;
$user-> IDSeries = $request->IDSeries;
$user-> IDNumber = $request->IDNumber;
$user-> IDValidUntil = $request->IDValidUntil;
$user-> identityCardNumber = $request->identityCardNumber;
$user-> driversLicense= $request->driversLicense;
$user-> education = $request->education;
$user-> profession = $request->profession;
$user->save();
return redirect()->to('/userprofile');;
}
public function userTrainings(){
if(auth()->user() != null && auth()->user()->fireStationID != null ){
$userTrainings = DB::table('trainingsFirefighters')->where("trainingsFirefighters.firefighterID", '=', auth()->user()->id)
->leftJoin('trainings', 'trainingsFirefighters.trainingID', '=', 'trainings.id')
->whereNull('deleted_at')
->select('trainingsFirefighters.id','trainings.trainingName','trainingsFirefighters.dateOfComplete', 'trainingsFirefighters.dateOfExpiry')
->get();
return view("userTrainings", ["userTrainings" => $userTrainings]);
}
else{
return redirect()->to('/login');;
}
}
public function userDecorations(){
if(auth()->user() != null && auth()->user()->fireStationID != null ){
$userDecorations = DB::table('decorationsFirefighters')->where("decorationsFirefighters.firefighterID", '=', auth()->user()->id)
->whereNull('decorationsFirefighters.deleted_at')
->leftJoin('decorations', 'decorationsFirefighters.decorationID', '=', 'decorations.id')
->get();
return view("userDecorations", ["userDecorations" => $userDecorations]);
} else{
return "Brak dostepu";
}
}
}