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

154 lines
6.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\fireStation;
use DB;
class fireStationController extends Controller
{
public function create(){
if(auth()->user() != null && auth()->user()->fireStationID != null ){
$fireStation = fireStation::find(auth()->user()->fireStationID);
return view('unit', ["fireStation" => $fireStation]);
} else{
$voivodeships = DB::table('wojewodztwa')->pluck("name","id");
return view('unit',compact('voivodeships'));
}
}
public function editForm()
{
if(auth()->user() != null && auth()->user()->fireStationID != null )
{
$id = auth()->user()->fireStationID;
$fireStation = DB::table('firestations')->where("id", $id)->first();
$voivodeships = DB::table('wojewodztwa')->pluck("name","id");
return view('fireStationEdit', ["fireStation" => $fireStation], compact('voivodeships'));
}
else
return "Brak dostępu";
}
public function store()
{
$this->validate(request(),[
'fireStationName' => 'required|min:3|max:45',
'number' => 'required|numeric',
'voivodeship' => 'required',
'county' => 'required',
'community' => 'required',
'postOffice' => 'required|min:3|max:45',
'zipCode' => 'required|digits:5',
'latitude' => ['required', 'regex:/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/'],
'longitude' => ['required', 'regex:/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/'],
'address' => 'required|min:3|max:45',
'KRS' => 'required|digits:10',
'NIP' => 'required|digits:10',
'phoneNumber' => 'required|digits:9',
'email' => 'required|email|unique:fireStations',
],
[
'required' => ':attribute jest wymagany(e).',
'min' => ':attribute musi mieć przynajmniej :min znaki.',
'max' => ':attribute musi mieć nie więcej niż :max znaków.',
'numeric' => ':attribute może zawierać tylko cyfry.',
'digits' => ':attribute musi składać się z :digits cyfr.',
'unique' =>':attribute jest już zajęty.',
'email' => 'Niepoprawny adres e-mail.',
'latitude.regex' =>':attribute ma zakres od -90.0 do 90.0',
'longitude.regex' =>':attribute ma zakres od -180.0 do 180.0'
]);
$request = request();
$voivodeship = DB::table('wojewodztwa')->select('name')->where('id', $request -> voivodeship)->first();
$county = DB::table('powiaty')->select('name')->where('id', $request -> county)->first();
$community = DB::table('gminy')->select('name')->where('id', $request -> community)->first();
$jednostka = fireStation::create([
'name' => $request -> fireStationName,
'number' => $request -> number,
'voivodeship' => $voivodeship -> name,
'county' => $county -> name,
'community' => $community -> name,
'postOffice' => $request -> postOffice,
'zipCode' => $request -> zipCode,
'address' => $request -> address,
'latitude' => $request -> latitude,
'longitude' => $request -> longitude,
'KRS' => $request -> KRS,
'NIP' => $request -> NIP,
'phoneNumber' => $request -> phoneNumber,
'email' => $request -> email,
'creatorID' => auth()->user()->id,
'changingID' => auth()->user()->id
]);
$user = auth()->user();
$user->fireStationID = $jednostka->id;
$user->save();
return redirect()->to('/jednostka');
}
public function update(){
$this->validate(request(),[
'fireStationName' => 'required|min:3|max:45',
'number' => 'required|numeric',
'voivodeship' => 'required',
'county' => 'required',
'community' => 'required',
'postOffice' => 'required|min:3|max:45',
'zipCode' => 'required|digits:5',
'latitude' => ['required', 'regex:/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/'],
'longitude' => ['required', 'regex:/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/'],
'address' => 'required|min:3|max:45',
'KRS' => 'required|digits:10',
'NIP' => 'required|digits:10',
'phoneNumber' => 'required|digits:9',
'email' => 'required|email|unique:fireStations,email,'.auth()->user()->fireStationID, //wymagaj unikalnego adresu email ale pozwól na zachowanie starego adresu
],
[
'required' => ':attribute jest wymagany(e).',
'min' => ':attribute musi mieć przynajmniej :min znaki.',
'max' => ':attribute musi mieć nie więcej niż :max znaków.',
'numeric' => ':attribute może zawierać tylko cyfry.',
'digits' => ':attribute musi składać się z :digits cyfr.',
'unique' =>':attribute jest już zajęty.',
'email' => 'Niepoprawny adres e-mail.',
'latitude.regex' =>':attribute ma zakres od -90.0 do 90.0',
'longitude.regex' =>':attribute ma zakres od -180.0 do 180.0'
]);
$request = request();
$voivodeship = DB::table('wojewodztwa')->select('name')->where('id', $request -> voivodeship)->first();
$county = DB::table('powiaty')->select('name')->where('id', $request -> county)->first();
$community = DB::table('gminy')->select('name')->where('id', $request -> community)->first();
$fireStation = fireStation::find($request->fireStationID);
$fireStation-> name = $request-> fireStationName;
$fireStation-> number = $request-> number;
$fireStation-> voivodeship = $voivodeship-> name;
$fireStation-> county = $county-> name;
$fireStation-> community = $community-> name;
$fireStation-> postOffice = $request-> postOffice;
$fireStation-> zipCode = $request-> zipCode;
$fireStation-> address = $request-> address;
$fireStation-> latitude = $request-> latitude;
$fireStation-> longitude = $request-> longitude;
$fireStation-> KRS = $request-> KRS;
$fireStation-> NIP = $request-> NIP;
$fireStation-> phoneNumber = $request-> phoneNumber;
$fireStation-> email = $request-> email;
$fireStation-> changingID = auth()->user()->id;
$fireStation->save();
return redirect()->to('/jednostka');;
}
}