eOSP2/app/Http/Controllers/fireStationController.php

154 lines
6.7 KiB
PHP
Raw Normal View History

2019-09-24 18:05:37 +02:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\fireStation;
2019-09-30 19:19:32 +02:00
use DB;
2019-09-24 18:05:37 +02:00
class fireStationController extends Controller
{
2019-09-25 02:13:13 +02:00
public function create(){
if(auth()->user() != null && auth()->user()->fireStationID != null ){
$fireStation = fireStation::find(auth()->user()->fireStationID);
return view('unit', ["fireStation" => $fireStation]);
} else{
2019-09-30 19:19:32 +02:00
$voivodeships = DB::table('wojewodztwa')->pluck("name","id");
2019-12-07 01:56:30 +01:00
return view('unit',compact('voivodeships'));
2019-09-25 02:13:13 +02:00
}
}
2019-12-07 01:56:30 +01:00
public function editForm()
{
if(auth()->user() != null && auth()->user()->fireStationID != null )
{
$id = auth()->user()->fireStationID;
$fireStation = DB::table('fireStations')->where("id", $id)->first();
2019-12-07 01:56:30 +01:00
$voivodeships = DB::table('wojewodztwa')->pluck("name","id");
return view('fireStationEdit', ["fireStation" => $fireStation], compact('voivodeships'));
}
else
return "Brak dostępu";
}
2019-09-24 18:05:37 +02:00
public function store()
{
$this->validate(request(),[
2019-12-07 01:56:30 +01:00
'fireStationName' => 'required|min:3|max:45',
2019-09-25 15:46:57 +02:00
'number' => 'required|numeric',
'voivodeship' => 'required',
'county' => 'required',
'community' => 'required',
2019-09-25 15:46:57 +02:00
'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+)?)$/'],
2019-09-25 15:46:57 +02:00
'address' => 'required|min:3|max:45',
'KRS' => 'required|digits:10',
'NIP' => 'required|digits:10',
'phoneNumber' => 'required|digits:9',
2019-09-28 00:49:15 +02:00
'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'
2019-09-24 18:05:37 +02:00
]);
$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();
2019-09-24 18:05:37 +02:00
$jednostka = fireStation::create([
2019-12-07 01:56:30 +01:00
'name' => $request -> fireStationName,
2019-09-24 18:05:37 +02:00
'number' => $request -> number,
'voivodeship' => $voivodeship -> name,
'county' => $county -> name,
'community' => $community -> name,
2019-09-24 18:05:37 +02:00
'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');
}
2019-12-07 01:56:30 +01:00
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');;
}
2019-09-24 18:05:37 +02:00
}