From 7572721451a9b88bf24628dc55eb7469a8a082bf Mon Sep 17 00:00:00 2001 From: elham Date: Mon, 4 Mar 2019 12:29:21 +0430 Subject: [PATCH] #1 vehical bookings changed, tracking of approver user, approval time finished, update of bookings fulfilled, assigned roles for users, register new user as signed in user, update of users, approving pending user and something else fulfilled. --- .../Controllers/Auth/RegisterController.php | 10 +- app/Http/Controllers/HomeController.php | 53 ++- .../Controllers/bookVehicalController.php | 42 --- app/Http/Controllers/bookingController.php | 208 +++++++++--- app/Http/Controllers/carController.php | 124 ++++--- app/Http/Controllers/driverController.php | 9 +- app/Http/Controllers/maktab.php | 1 + app/Http/Controllers/rolesController.php | 92 ++++++ app/Http/Controllers/userController.php | 151 ++++++++- app/Http/Kernel.php | 3 + app/User.php | 4 +- app/car.php | 2 +- composer.json | 3 +- composer.lock | 67 +++- config/permission.php | 129 ++++++++ .../2019_01_02_071350_create_cars_table.php | 4 +- ...019_01_02_071557_create_bookings_table.php | 15 +- ...034_add_foreign_keys_to_bookings_table.php | 10 +- .../2019_02_12_050329_book_vehicals.php | 39 --- ...065338_add_null_conditional_constraint.php | 35 -- ...2019_02_17_143122_approval_constraints.php | 52 +++ .../2019_02_20_102515_add_time_booked.php | 31 ++ ..._03_01_085053_create_permission_tables.php | 102 ++++++ .../2019_03_02_141023_add_status_user.php | 33 ++ public/js/crud.js | 297 +++++++++++++++-- resources/views/bookings/index.blade.php | 306 ++++++++---------- resources/views/bookings/pendings.blade.php | 107 ++++++ resources/views/cars/index.blade.php | 75 +++-- resources/views/drivers/index.blade.php | 42 +-- resources/views/home.blade.php | 6 +- resources/views/layouts/app.blade.php | 43 ++- resources/views/roles/index.blade.php | 205 ++++++++++++ resources/views/users/index.blade.php | 296 ++++++++++++----- resources/views/users/pendings.blade.php | 85 +++++ routes/web.php | 15 +- 35 files changed, 2098 insertions(+), 598 deletions(-) delete mode 100644 app/Http/Controllers/bookVehicalController.php create mode 100644 app/Http/Controllers/rolesController.php create mode 100644 config/permission.php delete mode 100644 database/migrations/2019_02_12_050329_book_vehicals.php delete mode 100644 database/migrations/2019_02_14_065338_add_null_conditional_constraint.php create mode 100644 database/migrations/2019_02_17_143122_approval_constraints.php create mode 100644 database/migrations/2019_02_20_102515_add_time_booked.php create mode 100644 database/migrations/2019_03_01_085053_create_permission_tables.php create mode 100644 database/migrations/2019_03_02_141023_add_status_user.php create mode 100644 resources/views/bookings/pendings.blade.php create mode 100644 resources/views/roles/index.blade.php create mode 100644 resources/views/users/pendings.blade.php diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 57d7ecd..9ad2333 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -48,14 +48,18 @@ public function __construct() */ protected function validator(array $data) { + $passRegexMessage = [ + 'password.regex'=>' Password must has 8 letters, at least one letter, one number and one special charecter ', + 'phone.regex'=>'phone number must start with 07 and not be greater than 10 numbers']; return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], - 'password' => ['required', 'string', 'min:6', 'confirmed'], + 'password' => ['required', 'string', 'min:6', 'confirmed','regex:/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/'], 'position' =>['required','string','min:4'], 'directorate'=>['required','string','min:4'], - 'phone' =>['required','regex:/^07[0-9]{8}/'], // - ]); + 'phone' =>['required','regex:/^07[0-9]{8}/'], + + ],$passRegexMessage); } /** diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 588f937..6aaabd9 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -1,12 +1,15 @@ 'writer']); + Role::firstOrCreate(['name' => 'normal user']); + Role::firstOrCreate(['name' => 'approver']); + Role::firstOrCreate(['name' => 'editor']); + Role::firstOrCreate(['name' => 'remover']); + Role::firstOrCreate(['name' => 'admin']); + + Permission::firstOrCreate(['name'=>'edit']); + Permission::firstOrCreate(['name'=>'insert']); + Permission::firstOrCreate(['name'=>'delete']); + Permission::firstOrCreate(['name'=>'approve']); + Permission::firstOrCreate(['name'=>'book vehical']); + + $writerRole = Role::findByName('writer'); + $approveRole = Role::findByName('approver'); + $editorRole = Role::findByName('editor'); + $removerRole = Role::findByName('remover'); + $nomal_user_role = Role::findByName('normal user'); + + $insertPermission = Permission::findByName('insert'); + $editPermission = Permission::findByName('edit'); + $approvePermission = Permission::findByName('approve'); + $removePermission = Permission::findByName('delete'); + $book_vehicalPermission = Permission::findByName('book vehical'); + + $adminRole = Role::findByName('admin'); + $adminRole->syncPermissions($insertPermission,$editPermission,$approvePermission,$removePermission,$book_vehicalPermission); + + $writerRole->givePermissionTo($insertPermission); + $editorRole->givePermissionTo($editPermission); + $nomal_user_role->givePermissionTo($book_vehicalPermission); + // $user = User::find(1); + // $user->assignRole('admin'); + // auth()->user()->assignRole('writer'); + // $user->givePermissionTo('delete'); + // $permissions = $user->getDirectPermissions(); + // $permissions; + // $users = User::role('writer')->get(); // Returns only users with the role 'writer' + // return $users; + // $user->givePermissionTo('insert'); + + return view('home'); } } diff --git a/app/Http/Controllers/bookVehicalController.php b/app/Http/Controllers/bookVehicalController.php deleted file mode 100644 index 2273ee3..0000000 --- a/app/Http/Controllers/bookVehicalController.php +++ /dev/null @@ -1,42 +0,0 @@ -get(); - return view('bookings/book vehical')->with('rows',$rows); - - } - // - public function sendData(Request $request){ - - $destination= $request->input('destination'); - $description= $request->input('description'); - $pickup_time = $request->input('pickup_time'); - $return_time = $request->input('return_time'); - $count = $request->input('count'); - - try { - - DB::table('bookVehicals')->insert( - ['pickup_time' => $pickup_time, 'return_time' => $return_time, 'user_id' =>1, - 'count'=>$count,'destination' =>$destination,'description'=>$description] - ); - return "your request for car is wait for approval of admin "; - } - catch(QueryException $ex){ - print($ex->getMessage()); - } - } - - -} diff --git a/app/Http/Controllers/bookingController.php b/app/Http/Controllers/bookingController.php index 15d6a69..6df8c00 100644 --- a/app/Http/Controllers/bookingController.php +++ b/app/Http/Controllers/bookingController.php @@ -3,9 +3,12 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; + use App\booking; use DB; +use Response; use illuminate\Http\QueryException; +use Validator; class bookingController extends Controller { @@ -16,18 +19,33 @@ class bookingController extends Controller */ public function index() { - $rows = DB::table('bookVehicals')->get(); - $countOfRows = DB::table('bookVehicals')->count(); - // return view('bookings/index')->with('rows',$rows); - - $bookingsData = Booking::all(); - return view('/bookings/index')->with(['bookingsData'=>$bookingsData,'rows'=>$rows,'countOfRows'=>$countOfRows]); + $approvedCount = DB::table('bookings')->where('approval',true)->count(); + $rejectedCount = DB::table('bookings')->where('approval',false)->wherenotnull('approver_description')->count(); + $pendingsCount = DB::table('bookings')->wherenull('approval')->wherenull('approver_description')->count(); + $bookingsData = Booking::orderByDesc('approval')->get(); + return view('/bookings/index')->with(['bookingsData'=>$bookingsData,'approvedCount'=>$approvedCount + ,'rejectedCount'=>$rejectedCount,'pendingsCount'=>$pendingsCount]); } + public function pendings(){ + $freeCars = DB::select(DB::raw("SELECT distinct bookings.plate_no,cars.type,cars.plate_no,cars.driver_id FROM + cars LEFT JOIN bookings ON bookings.plate_no = cars.plate_no WHERE bookings.plate_no IS NULL and cars.status=true + OR approval_return_time < now()")); + + $freeDrivers = DB::select(DB::raw("SELECT distinct bookings.driver_id,drivers.phone_no,drivers.driver_id, + drivers.name FROM drivers LEFT JOIN bookings ON bookings.driver_id = drivers.driver_id WHERE + bookings.driver_id IS NULL and drivers.status = TRUE OR approval_return_time < now()")); + + $countPendings = DB::table('bookings')->where("approval",null)->count(); + $pendings = DB::table('bookings')->where('approval',null)->get(); + + return view('/bookings/pendings')->with(['pendings'=>$pendings,"countPendings"=>$countPendings, + 'freeDrivers'=>$freeDrivers,'freeCars'=>$freeCars]); + } public function reject(Request $request){ $insertQuery = DB::table('bookings')->insert(['pickup_time'=>$request->input('pickup_time'), 'return_time'=>$request->input('return_time'),'count'=>$request->input('count'), - 'description'=>$request->input('description'),'destination'=>$request->input('destination'),'approval'=>false,'driver_id'=>null,'car_id'=>null, + 'description'=>$request->input('description'),'destination'=>$request->input('destination'),'approval'=>false,'driver_id'=>null,'plate_no'=>null, 'user_id'=>$request->input('user_id')]); } @@ -49,48 +67,43 @@ public function create() */ public function store(Request $request) { - $bookRows = DB::table('bookVehicals')->get(); - - $pickupt_time = $request->input('pickup_time'); + $user_id =$request->input('user_id'); + + $pickup_time = $request->input('pickup_time'); $return_time = $request->input('return_time'); $count = $request->input('count'); $description = $request->input('description'); - $destination = $request->input('destination'); - $approval = $request->input('approval'); - $driver_id = $request->input('driver_id'); - $car_id = $request->input('car_id'); - $user_id = $request->input('user_id'); - - // return $pickupt_time . " " .$return_time . " " .$count . " " .$description . " " .$destination . " " .$driver_id . " " . - // $car_id . " " .$user_id; - - try{ - - // $insertQuery = Booking::create($request->all()); - if($request->input('approval')=='false' OR $request->input('approval') === false){ - $insertQuery = DB::table('bookings')->insert(['pickup_time'=>$pickupt_time,'return_time'=>$return_time,'count'=>$count, - 'description'=>$description,'destination'=>$destination,'approval'=>$approval,'driver_id'=>null,'car_id'=>null, - 'user_id'=>$user_id]); - return "Please select true for approval field"; - } - else{ - if(!($car_id)||!($driver_id) || $car_id ==null ||$driver_id ==null){return "driver ID and car ID is required";} - $insertQuery = DB::table('bookings')->insert(['pickup_time'=>$pickupt_time,'return_time'=>$return_time,'count'=>$count, - 'description'=>$description,'destination'=>$destination,'approval'=>$approval,'driver_id'=>$driver_id,'car_id'=>$car_id, - 'user_id'=>$user_id]); - $del = DB::table('bookVehicals')->where('pickup_time',$pickupt_time)->where('return_time',$return_time)->where('count',$count)-> - where('destination',$destination)->where('description',$description)->delete(); - - return "Approved!" . " driver ID ".$driver_id . " and Car ID: " .$car_id; + $destination = $request->input('destination'); + $rules = array('pickup_time'=>'date|required', + 'return_time'=>'after:pickup_time|date|required', + 'count:numeric|min:1', + 'description:string|min:10', + 'destination:string|min:5'); + $validator = Validator::make($request->all(),$rules); + if($validator->fails()){ + return $validator->errors()->toArray(); + } + else{ + try{ + + DB::statement(DB::raw("INSERT INTO bookings (booking_id,pickup_time,return_time,count,description,destination + ,user_id,book_created_time,book_updated_time) + values ( + DEFAULT,'$pickup_time','$return_time',$count,'$description','$destination',$user_id,now(),now() + ) + ")); + + return "YOU HAVE SUCSSESSFULLY BOOKED VEHICAL"; + }catch(QueryException $e){ + return $e->getMessage(); } - } - catch(QueryException $ex){ - print_r($ex->getMessage()); + } + - } + /** * Display the specified resource. @@ -111,7 +124,22 @@ public function show($id) */ public function edit($id) { - // + $bookings = Booking::find($id); + $freeCars = DB::select(DB::raw("SELECT distinct bookings.plate_no,cars.type,cars.plate_no,cars.driver_id FROM + cars LEFT JOIN bookings ON bookings.plate_no = cars.plate_no WHERE bookings.plate_no IS NULL and cars.status=true + OR approval_return_time < now()")); + + $freeDrivers = DB::select(DB::raw("SELECT distinct bookings.driver_id,drivers.phone_no,drivers.driver_id, + drivers.name FROM drivers LEFT JOIN bookings ON bookings.driver_id = drivers.driver_id WHERE + bookings.driver_id IS NULL and drivers.status = TRUE OR approval_return_time < now()")); + + $countPendings = DB::table('bookings')->where("approval",null)->count(); + $pendings = DB::table('bookings')->where('approval',null)->get(); + + + if (\Illuminate\Support\Facades\Request::ajax()) { + + return Response::json(array('bookings'=>$bookings,'freeDrivers'=>$freeDrivers,'freeCars'=>$freeCars));} } /** @@ -124,6 +152,76 @@ public function edit($id) public function update(Request $request, $id) { // + + $user_id =$request->input('user_id'); + + $booking_id = $id; + $approval = $request->input('approval'); + $approval_pickup_time = $request->input('approval_pickup_time'); + $approval_return_time = $request->input('approval_return_time'); + $driver_id = $request->input('driver_id'); + $plate_no = $request->input('plate_no'); + $approver_description = $request->input('approver_description'); + + + if($approval===false||$approval==false||$approval=="false"){ + try{ + DB::statement(DB::raw("update bookings set approval = $approval, approver_description = + '$approver_description',approver_user_id=$user_id where booking_id = $id")); + return "you have rejected"; + + } catch(QueryException $e){ + return $e->getMessage(); + } + + }else{ + + if($approval == true || $approval =="true"|| $approval ===true){ + $rules = array( + 'approval_pickup_time' => 'required|date', + 'approval_return_time' => 'required|date|after:approval_pickup_time', + 'driver_id' => 'required', + 'plate_no' => 'required', + ); + $validator = Validator::make($request->all(),$rules); + if($validator->fails()){ + return $validator->errors()->toArray(); + } + else{ + } + + try{ + DB::statement(DB::raw("update bookings set plate_no=$plate_no, approval =$approval,approval_pickup_time='$approval_pickup_time' + ,approval_return_time='$approval_return_time',driver_id=$driver_id,approver_description = '$approver_description',approver_user_id=$user_id, + created_at=now(),updated_at=now() where + booking_id=$id")); + return "successfully approved"; + } + catch(QueryException $e){ + return $ex->getMessage(); + } + } + } + // if($approval){ + // return "Null Value"; + // } + // if(empty($request->input('approval')) || $request->input('approval')==null || $request->input('approval')=="" + // || $request->input('approval')=="null"){ + // $approval_pickup_time = null; $approval_return_time = null; $driver_id = null; $car_id =null;$approver_description =null; + // $rules = array("approval_pickup_time"=>"required|date_format:Y-m-d H:i:s", + // "approval_return_time"=>"date_format:Y-m-d H:i:s"); + // return $request->all(); + // die(); + // } + // return $approval; + // $validator = Validator::make($request->all(), $rules); + // if($validator->fails()){ + // return $validator->errors()->toArray(); + // } + // else{ + // return "There is no Error"; + // } + } /** @@ -136,4 +234,30 @@ public function destroy($id) { // } + public function freeCar(Request $request){ + $pickup_time = $request->input('pickup_time'); + + $pick_time_plate= DB::select(DB::raw("SELECT plate_no FROM bookings where + approval_return_time < '$pickup_time'")); + $x=0; + foreach ($pick_time_plate as $value) { + $freeCar[$x] = $value->plate_no; + $x++; + } + return $pick_time_plate; + + } + public function freeDriver(Request $request){ + $pickup_time = $request->input('pickup_time'); + + + $pick_time_driver= DB::select(DB::raw("SELECT driver_id FROM bookings where + approval_return_time < '$pickup_time'")); + $x=0; + foreach ($pick_time_driver as $value) { + $freeDriver[$x] = $value->driver_id; + $x++; + } + return $pick_time_driver; + } } diff --git a/app/Http/Controllers/carController.php b/app/Http/Controllers/carController.php index 8496fe2..a0769cf 100644 --- a/app/Http/Controllers/carController.php +++ b/app/Http/Controllers/carController.php @@ -19,19 +19,18 @@ class carController extends Controller public function index() { // - $carsData = DB::table('cars')->orderBy('car_id','des')->get(); + $drivers = DB::select(DB::raw("SELECT drivers.driver_id,drivers.name FROM drivers left JOIN cars on cars.driver_id = drivers.driver_id where cars.driver_id IS NULL")); + + $carsData = DB::table('cars')->orderBy('plate_no','des')->get(); $dataCounts = DB::table('cars')->count(); - return view('/cars/index')->with(compact('carsData','dataCounts')); + return view('/cars/index')->with(compact('carsData','dataCounts','drivers')); } public function searchCar(Request $request){ $searchOn = $request->input('searchon'); $searchInput= $request->input('searchInp'); - if($searchOn == "car_id"){ - $dataArray = DB::table('cars')->where("car_id",'LIKE',"%$searchInput%")->get(); - $dataCount = DB::table('cars')->where("car_id",'LIKE',"%$searchInput%")->count();} - + if($searchOn == "plate_no"){ $dataArray = DB::table('cars')->where("plate_no",'LIKE',"%$searchInput%")->get(); $dataCount = DB::table('cars')->where("plate_no",'LIKE',"%$searchInput%")->count();} @@ -59,7 +58,7 @@ public function searchCar(Request $request){ foreach ($dataArray as $data) { if($data->status === false){$data->status = 'False';}else{$data->status='True';} - echo "". $data->car_id . "" . + echo "" . $data->plate_no . "". "" . $data->color . "". "" . $data->model . "" . @@ -68,8 +67,8 @@ public function searchCar(Request $request){ "" . $data->driver_id . "" . "" . $data->created_at . "" . "" . $data->updated_at . "" . - "Update - Delete + "Update + Delete "; }} @@ -103,35 +102,44 @@ public function store(Request $request) $type = $request->input('type'); $status = $request->input('status'); $driver_id = $request->input('driver_id'); + $rules = array( - 'plate_no' => 'required|max:6|min:3|unique:cars', + 'plate_no' => 'required|numeric|max:999999|min:100|unique:cars', 'color' => 'required|string|min:3|max:15', 'model' => 'required', 'type' => 'required', - 'status' => 'required'); + 'status' => 'required', + ); + + if(!(empty($driver_id))){$rules+=['driver_id'=>'unique:cars'];} + + $validator = Validator::make($request->all(),$rules); + + if($validator->fails()){ + + return $validator->errors()->toArray(); + } + else{ + try{ + Car::create($request->all()); + // DB::statement(DB::raw("insert into cars values($plate_no,'$color','$model','$type',$status,now(),now(),$driver_id)")); + //||||----- Car::create($request->all()) appear this error-->> + //note PHP can not check null value it can check '' values only and only. + // NOTE SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "NULL" + // (SQL: insert into "cars" ("plate_no", "color", "model", "type", "status", "driver_id", "updated_at", "created_at") + // values (99999, red, 2009, corolla, true, NULL, 2019-02-21 17:46:52, 2019-02-21 17:46:52) returning "plate_no")----\\|||| + // + + return "successfully done " . $plate_no; + } + catch(QueryException $ex){ + print($ex->getMessage()); + } + } - - $validator = Validator::make($request->all(),$rules); - - if($validator->fails()){ - - return $validator->errors()->toArray(); - - } - else{ - - try { - Car::create($request->all()); - $id = DB::getPdo()->lastInsertId(); - return "successfully done " . $id; - } - catch(QueryException $ex){ - print($ex->getMessage()); - } + - } - - + } /** @@ -140,17 +148,17 @@ public function store(Request $request) * @param int $id * @return \Illuminate\Http\Response */ - public function show($id) + public function show($plate_no) { // - $data = DB::table('cars')->where('car_id',$id)->first(); + $data = DB::table('cars')->where('plate_no',$plate_no)->first(); if($data->status == 1){$data->status = "True";}else{$data->status="False";} - $row = "".$data->car_id."" . $data->plate_no ."" . $data->color ."" + $row = "".$data->plate_no."" . $data->color ."" .$data->model."".$data->type."".$data->status."".$data->driver_id."".$data->created_at."".$data->updated_at. "UpdateDelete "; + .$data->plate_no ."' id='".$data->plate_no."'class='btn btn-primary updateBtn' >UpdateDelete "; // return json_encode($data); return $row; } @@ -161,9 +169,11 @@ public function show($id) * @param int $id * @return \Illuminate\Http\Response */ - public function edit($id) + public function edit($plate_no_for_update) { - $data = Car::find($id); + $data = Car::find($plate_no_for_update); + + $data->plate_no_for_update = $plate_no_for_update; return $data; } @@ -174,10 +184,9 @@ public function edit($id) * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $plate_no_for_update) { - $id; - $data = $this->edit($id); + $data = $this->edit($plate_no_for_update); $plate_no = $request->input('plate_no'); $color = $request->input('car_color'); $model = $request->input('car_model'); @@ -185,12 +194,14 @@ public function update(Request $request, $id) $status = $request->input('car_status'); $driver_id = $request->input('driver_id'); + + if($plate_no == $data['plate_no'] && $color ==$data['color'] && $model == - $data['model']&& $status ==$data['status']&&$type==$data['type']&&$driver_id==$data['driver_id']){ + $data['model']&& $status === $data['status']&&$type==$data['type']&&$driver_id==$data['driver_id']){ $responseErr = "There is nothing for update, please enter new things into field/fields"; return $responseErr; }else{ - $rules = array('plate_no' => 'required|string|max:6|min:3', 'car_color' => 'required|string|min:3|max:15', + $rules = array('plate_no' => 'required|numeric|max:999999|min:100', 'car_color' => 'required|string|min:3|max:15', 'car_model' => 'required', 'car_type' => 'required','car_status' => 'required'); $validator = Validator::make($request->all(),$rules); if($validator->fails()){ @@ -200,32 +211,39 @@ public function update(Request $request, $id) $arrayOfErr = array(); if(!($data['plate_no'] == $plate_no)){$plate_count = DB::table('cars')->where('plate_no',$plate_no)->count(); if($plate_count > 0){$arrayOfErr[0] = "plate number(".$plate_no.") has already been taken";}} - - if(!($data['driver_id'] == $driver_id)){ + + + + if(!($data['driver_id'] == $driver_id) && !(empty($driver_id)) ){ + $driver_id_count = DB::table('cars')->where('driver_id',$driver_id)->count(); if($driver_id_count > 0){ $car_of_driver = DB::table('cars')->where('driver_id',$driver_id)->first(); $arrayOfErr[1] = "car with plate number of " . $car_of_driver->plate_no - . " is assigned for driver with ID ". $driver_id;} + . " is assigned for driver with ID ". $driver_id; + } } - + + if($d_id_driver<=0 && $driver_id) {$arrayOfErr[2] = "there is no driver registered with ID of " . $driver_id . " ";} + if($arrayOfErr){ return $arrayOfErr; } else{ try { - $update = DB::table('cars')->where('car_id',$id) + $update = DB::table('cars')->where('plate_no',$plate_no_for_update) ->update(['plate_no'=>$plate_no,'color'=>$color,'model'=>$model,'type'=>$type,'status'=>$status,'driver_id'=>$driver_id]); - return "successfully updated"; + return "successfully updated"; } catch(QueryException $ex){ - if($ex->getMessage()) + print($ex->getMessage()); } } } + } @@ -235,10 +253,10 @@ public function update(Request $request, $id) * @param int $id * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($plate_no) { try{ - $deleteData = Car::find($id); + $deleteData = Car::find($plate_no); $deleteData->delete(); return "successfully deleted"; } diff --git a/app/Http/Controllers/driverController.php b/app/Http/Controllers/driverController.php index 5973128..0575f75 100644 --- a/app/Http/Controllers/driverController.php +++ b/app/Http/Controllers/driverController.php @@ -54,7 +54,7 @@ public function searchDriver(Request $request){ if($dataCount >0 ){ foreach ($dataArray as $data) { - if($data->status === false){$data->status = 'False';}else{$data->status='True';} + if($data->status === false){$data->status = 'false';}else{$data->status='true';} echo "". $data->driver_id . "" . "" . $data->name . "". "" . $data->father_name . "". @@ -124,7 +124,7 @@ public function store(Request $request) public function show($id) { $data = DB::table('drivers')->where('driver_id',$id)->first(); - if($data->status == 1){$data->status = "True";}else{$data->status="False";} + if($data->status == 1){$data->status = 'true';}else{$data->status='false';} $row = "".$data->driver_id."" . $data->name ."" . $data->father_name ."" .$data->phone_no."".$data->status."".$data->created_at."".$data->updated_at."input('driver_phone_no'); $status = $request->input('driver_status'); - if($name == $data['name'] && $father_name ==$data['father_name'] && $phone_no == - $data['phone_no']&& $status ==$data['status']) + + if($name == $data['name'] && $father_name ==$data['father_name'] && + $phone_no == $data['phone_no'] && $status ===$data['status']) { $responseErr = "There is nothing for update, please enter new things into field/fields"; return $responseErr; diff --git a/app/Http/Controllers/maktab.php b/app/Http/Controllers/maktab.php index 42a6f89..7e7b0b7 100644 --- a/app/Http/Controllers/maktab.php +++ b/app/Http/Controllers/maktab.php @@ -7,3 +7,4 @@ class maktab extends Controller { +} \ No newline at end of file diff --git a/app/Http/Controllers/rolesController.php b/app/Http/Controllers/rolesController.php new file mode 100644 index 0000000..f81a4a5 --- /dev/null +++ b/app/Http/Controllers/rolesController.php @@ -0,0 +1,92 @@ +count(); + $role = DB::select( DB::raw("SELECT roles.name as roleName, users.name,model_has_roles.role_id,users.id,model_has_roles.model_id from users left join model_has_roles + on model_has_roles.model_id = users.id join roles on roles.id = model_has_roles.role_id where users.id is not null")); $dataOfusersTable = User::all(); - return view('/users/index')->with('dataOfusersTable',$dataOfusersTable); + return view('/users/index')->with(['dataOfusersTable'=>$dataOfusersTable,'countOfUsers'=>$countOfUsers,'role'=>$role]); } + public function searchUser(Request $request){ + + $searchOn = $request->input('searchon'); + $searchInput= $request->input('searchInp'); + + if($searchOn == "user_id"){ + $dataArray = DB::table('users')->where("id",'LIKE',"%$searchInput%")->get(); + $dataCount = DB::table('users')->where("id",'LIKE',"%$searchInput%")->count();} + + if($searchOn == "name"){ + $dataArray = DB::table('users')->where("name",'LIKE',"%$searchInput%")->get(); + $dataCount = DB::table('users')->where("name",'LIKE',"%$searchInput%")->count();} + + if($searchOn == "position"){ + $dataArray = DB::table('users')->where("position",'LIKE',"%$searchInput%")->get(); + $dataCount = DB::table('users')->where("position",'LIKE',"%$searchInput%")->count();} + + if($searchOn == "directorate"){ + $dataArray = DB::table('users')->where("directorate",'LIKE',"%$searchInput%")->get(); + $dataCount = DB::table('users')->where("directorate",'LIKE',"%$searchInput%")->count();} + + if($searchOn == "status"){ + if($searchInput == 'a'){$searchInput = str_replace('a', false, false);} + $dataArray = DB::table('users')->where("status",'LIKE',"%$searchInput%")->get(); + $dataCount = DB::table('users')->where("status",'LIKE',"%$searchInput%")->count();} + if($searchOn == "id"){ + $dataArray = DB::table('users')->where("id",'LIKE',"%$searchInput%")->get(); + $dataCount = DB::table('users')->where("id",'LIKE',"%$searchInput%")->count();} + if($dataCount >0 ){ + + foreach ($dataArray as $data) { + if($data->status === false){$data->status = 'False';}else{$data->status='True';} + echo + "" . $data->id . "". + "" . $data->name . "". + "" . $data->position . "" . + "" . $data->directorate . "" . + "" . $data->email . "" . + "" . $data->status . "" . + "" . $data->created_at . "" . + "" . $data->updated_at . "" . + "Update + Delete + "; + }} + + else{return "

Data not found!

";} + + } + public function pendings(){ + + $countPendings = DB::table('users')->where("status",null)->count(); + $pendings = DB::table('users')->where('status',null)->get(); + $roles = DB::table('roles')->get(); + return view('/users/pendings')->with(['pendings'=>$pendings,"countPendings"=>$countPendings,'roles'=>$roles + ]); + } + public function reject(Request $request){ + $insertQuery = DB::table('bookings')->insert(['pickup_time'=>$request->input('pickup_time'), + 'return_time'=>$request->input('return_time'),'count'=>$request->input('count'), + 'description'=>$request->input('description'),'destination'=>$request->input('destination'),'approval'=>false,'driver_id'=>null,'plate_no'=>null, + 'user_id'=>$request->input('user_id')]); + + } + /** * Show the form for creating a new resource. * @@ -32,6 +105,36 @@ public function create() public function store(Request $request) { // + $passRegexMessage = ['password.regex'=>' Password must has 8 letters, at least one letter, one number and one special charecter ', + 'phone.regex'=>'phone number must start with 07 and not be greater than 10 numbers']; + $rules = array( + 'name' => ['required', 'string', 'max:255'], + 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], + 'password' => ['required', 'string', 'min:6', 'confirmed'], + 'position' =>['required','string','min:4'], + 'directorate'=>['required','string','min:4'], + 'phone' =>['required','regex:/^07[0-9]{8}/'], + ); + $validator = Validator::make($request->all(),$rules,$passRegexMessage); + + if($validator->fails()){ + + return $validator->errors()->toArray(); + + } + else{ + User::create([ + 'name' => $request->input('name'), + 'email' => $request->input('email'), + 'position' =>$request->input('position'), + 'directorate' => $request->input('directorate'), + 'phone' => $request->input('phone'), + 'status' =>$request->input('status'), + 'password' => Hash::make($request->input('password')), + ]); + $id = DB::getPdo()->lastInsertId(); + return 'successfully done '. $id ; + } } /** @@ -55,8 +158,9 @@ public function show($id) * @param int $id * @return \Illuminate\Http\Response */ - public function edit(Request $request) { - + public function edit($id) { + $data = User::find($id); + return $data; } /** * Update the specified resource in storage. @@ -68,6 +172,37 @@ public function edit(Request $request) { public function update(Request $request, $id) { // + + // $passRegexMessage = ['password.regex'=>' Password must has 8 letters, at least one letter, one number and one special charecter ', + // 'phone.regex'=>'phone number must start with 07 and not be greater than 10 numbers']; + $rules = array( + 'position' =>['required','string','min:4'], + 'directorate'=>['required','string','min:4'], + // 'name' => ['required', 'string', 'max:255'], + // 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], + // 'password' => ['required', 'string', 'min:6', 'confirmed'], + // 'phone' =>['required','regex:/^07[0-9]{8}/'], + ); + $validator = Validator::make($request->all(),$rules); + + if($validator->fails()){ + + return $validator->errors()->toArray(); + + } + else{ + $user = User::find($id); + $user->position = $request->input('position'); + $user->directorate =$request->input('directorate'); + $user->status = $request->input('status'); + $user->save(); + // $user->name = $request->input('name'); + // $user->email = $request->input('email'); + // $user->phone = $request->input('phone'); + // $user->password = Hash::make($request->input('password')); + + return 'successfully Updated user with ID: '. $id ; + } } /** @@ -76,9 +211,17 @@ public function update(Request $request, $id) * @param int $id * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id,Request $yesOrNo) { // + $dataID = DB::table("users")->where('id',$id)->first(); + $deleteResult = DB::table('users')->where('id',$id)->delete(); + if($deleteResult ===1){ + $result = "driver with ID: " .$dataID->id. " successfully deleted from table"; + return $result; } + else{ + return "Deletion not occured"; + } } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index a3d8c48..ff108a4 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -60,6 +60,9 @@ class Kernel extends HttpKernel 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + 'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class, + 'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class, + 'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class, ]; /** diff --git a/app/User.php b/app/User.php index c410023..c52a7b7 100644 --- a/app/User.php +++ b/app/User.php @@ -1,14 +1,14 @@ =7.0" + }, + "require-dev": { + "orchestra/testbench": "~3.4.2|~3.5.0|~3.6.0|~3.7.0|~3.8.0", + "phpunit/phpunit": "^5.7|6.2|^7.0", + "predis/predis": "^1.1" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\Permission\\PermissionServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\Permission\\": "src" + }, + "files": [ + "src/helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Permission handling for Laravel 5.4 and up", + "homepage": "https://github.com/spatie/laravel-permission", + "keywords": [ + "acl", + "laravel", + "permission", + "security", + "spatie" + ], + "time": "2019-02-26T21:25:08+00:00" + }, { "name": "swiftmailer/swiftmailer", "version": "v6.1.3", diff --git a/config/permission.php b/config/permission.php new file mode 100644 index 0000000..fbf9b89 --- /dev/null +++ b/config/permission.php @@ -0,0 +1,129 @@ + [ + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * Eloquent model should be used to retrieve your permissions. Of course, it + * is often just the "Permission" model but you may use whatever you like. + * + * The model you want to use as a Permission model needs to implement the + * `Spatie\Permission\Contracts\Permission` contract. + */ + + 'permission' => Spatie\Permission\Models\Permission::class, + + /* + * When using the "HasRoles" trait from this package, we need to know which + * Eloquent model should be used to retrieve your roles. Of course, it + * is often just the "Role" model but you may use whatever you like. + * + * The model you want to use as a Role model needs to implement the + * `Spatie\Permission\Contracts\Role` contract. + */ + + 'role' => Spatie\Permission\Models\Role::class, + + ], + + 'table_names' => [ + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'roles' => 'roles', + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * table should be used to retrieve your permissions. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'permissions' => 'permissions', + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * table should be used to retrieve your models permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_permissions' => 'model_has_permissions', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your models roles. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_roles' => 'model_has_roles', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'role_has_permissions' => 'role_has_permissions', + ], + + 'column_names' => [ + + /* + * Change this if you want to name the related model primary key other than + * `model_id`. + * + * For example, this would be nice if your primary keys are all UUIDs. In + * that case, name this `model_uuid`. + */ + + 'model_morph_key' => 'model_id', + ], + + /* + * When set to true, the required permission/role names are added to the exception + * message. This could be considered an information leak in some contexts, so + * the default setting is false here for optimum safety. + */ + + 'display_permission_in_exception' => false, + + 'cache' => [ + + /* + * By default all permissions are cached for 24 hours to speed up performance. + * When permissions or roles are updated the cache is flushed automatically. + */ + + 'expiration_time' => \DateInterval::createFromDateString('24 hours'), + + /* + * The key to use when tagging and prefixing entries in the cache. + */ + + 'key' => 'spatie.permission.cache', + + /* + * When checking for a permission against a model by passing a Permission + * instance to the check, this key determines what attribute on the + * Permissions model is used to cache against. + * + * Ideally, this should match your preferred way of checking permissions, eg: + * `$user->can('view-posts')` would be 'name'. + */ + + 'model_key' => 'name', + + /* + * You may optionally indicate a specific cache driver to use for permission and + * role caching using any of the `store` drivers listed in the cache.php config + * file. Using 'default' here means to use the `default` set in cache.php. + */ + + 'store' => 'default', + ], +]; diff --git a/database/migrations/2019_01_02_071350_create_cars_table.php b/database/migrations/2019_01_02_071350_create_cars_table.php index 1f47cdb..2962227 100644 --- a/database/migrations/2019_01_02_071350_create_cars_table.php +++ b/database/migrations/2019_01_02_071350_create_cars_table.php @@ -14,12 +14,12 @@ class CreateCarsTable extends Migration public function up() { Schema::create('cars', function (Blueprint $table) { - $table->increments('car_id'); - $table->string('plate_no')->unique(); + $table->integer('plate_no')->unsigned()->unique(); $table->string('color',15); $table->string('model',45); $table->string('type',25); $table->boolean('status')->default(1); + $table->timestamps(); }); } diff --git a/database/migrations/2019_01_02_071557_create_bookings_table.php b/database/migrations/2019_01_02_071557_create_bookings_table.php index 78584cd..dd748cc 100644 --- a/database/migrations/2019_01_02_071557_create_bookings_table.php +++ b/database/migrations/2019_01_02_071557_create_bookings_table.php @@ -16,11 +16,16 @@ public function up() Schema::create('bookings', function (Blueprint $table) { $table->increments('booking_id'); $table->dateTime('pickup_time'); - $table->dateTime('return_time'); - $table->tinyInteger('count'); - $table->longText('description'); - $table->string('destination'); - $table->boolean('approval'); + $table->dateTime('return_time'); + $table->tinyInteger('count'); + $table->longText('description'); + $table->string('destination'); + $table->dateTime('book_created_time'); + $table->dateTime('book_updated_time'); + $table->boolean('approval')->nullable(); + $table->longText('approver_description')->nullable(); + $table->dateTime('approval_pickup_time')->nullable(); + $table->dateTime('approval_return_time')->nullable(); $table->timestamps(); }); } diff --git a/database/migrations/2019_01_03_080034_add_foreign_keys_to_bookings_table.php b/database/migrations/2019_01_03_080034_add_foreign_keys_to_bookings_table.php index 3f8b340..9a6d949 100644 --- a/database/migrations/2019_01_03_080034_add_foreign_keys_to_bookings_table.php +++ b/database/migrations/2019_01_03_080034_add_foreign_keys_to_bookings_table.php @@ -18,16 +18,18 @@ public function up() $table->foreign('driver_id')->references('driver_id')->on('drivers')->onDelete('no action'); }); - Schema::table('bookings',function (Blueprint $table){ - $table->integer('car_id')->unsigned()->nullable(); - $table->foreign('car_id')->references('car_id')->on('cars')->onDelete('no action'); + $table->integer('plate_no')->nullable(); + $table->foreign('plate_no')->references('plate_no')->on('cars')->onDelete('no action'); }); - Schema::table('bookings',function (Blueprint $table){ $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('no action'); }); + Schema::table('bookings',function (Blueprint $table){ + $table->integer('approver_user_id')->unsigned()->nullable(); + $table->foreign('approver_user_id')->references('id')->on('users')->onDelete('no action'); + }); } /** diff --git a/database/migrations/2019_02_12_050329_book_vehicals.php b/database/migrations/2019_02_12_050329_book_vehicals.php deleted file mode 100644 index 4913fd9..0000000 --- a/database/migrations/2019_02_12_050329_book_vehicals.php +++ /dev/null @@ -1,39 +0,0 @@ -integer('user_id'); - $table->tinyInteger('count'); - $table->dateTime('pickup_time'); - $table->dateTime('return_time'); - $table->longText('description'); - $table->string('destination',255); - $table->timestamps(); - }); - } - - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - // - Schema::dropIfExists('bookACar'); - } -} diff --git a/database/migrations/2019_02_14_065338_add_null_conditional_constraint.php b/database/migrations/2019_02_14_065338_add_null_conditional_constraint.php deleted file mode 100644 index 4b3fa7b..0000000 --- a/database/migrations/2019_02_14_065338_add_null_conditional_constraint.php +++ /dev/null @@ -1,35 +0,0 @@ -increments('id'); + $table->string('name'); + $table->string('guard_name'); + $table->timestamps(); + }); + + Schema::create($tableNames['roles'], function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->string('guard_name'); + $table->timestamps(); + }); + + Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) { + $table->unsignedInteger('permission_id'); + + $table->string('model_type'); + $table->unsignedBigInteger($columnNames['model_morph_key']); + $table->index([$columnNames['model_morph_key'], 'model_type', ]); + + $table->foreign('permission_id') + ->references('id') + ->on($tableNames['permissions']) + ->onDelete('cascade'); + + $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'], + 'model_has_permissions_permission_model_type_primary'); + }); + + Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) { + $table->unsignedInteger('role_id'); + + $table->string('model_type'); + $table->unsignedBigInteger($columnNames['model_morph_key']); + $table->index([$columnNames['model_morph_key'], 'model_type', ]); + + $table->foreign('role_id') + ->references('id') + ->on($tableNames['roles']) + ->onDelete('cascade'); + + $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'], + 'model_has_roles_role_model_type_primary'); + }); + + Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { + $table->unsignedInteger('permission_id'); + $table->unsignedInteger('role_id'); + + $table->foreign('permission_id') + ->references('id') + ->on($tableNames['permissions']) + ->onDelete('cascade'); + + $table->foreign('role_id') + ->references('id') + ->on($tableNames['roles']) + ->onDelete('cascade'); + + $table->primary(['permission_id', 'role_id']); + + app('cache') + ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null) + ->forget(config('permission.cache.key')); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $tableNames = config('permission.table_names'); + + Schema::drop($tableNames['role_has_permissions']); + Schema::drop($tableNames['model_has_roles']); + Schema::drop($tableNames['model_has_permissions']); + Schema::drop($tableNames['roles']); + Schema::drop($tableNames['permissions']); + } +} diff --git a/database/migrations/2019_03_02_141023_add_status_user.php b/database/migrations/2019_03_02_141023_add_status_user.php new file mode 100644 index 0000000..0947757 --- /dev/null +++ b/database/migrations/2019_03_02_141023_add_status_user.php @@ -0,0 +1,33 @@ +boolean('status')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + // + }); + } +} diff --git a/public/js/crud.js b/public/js/crud.js index e1d5bb9..ceb5b38 100644 --- a/public/js/crud.js +++ b/public/js/crud.js @@ -19,13 +19,13 @@ $("#insertButton, .updateBtn").click(function(){ //load update data and validation of insert and update form var loc = window.location.href; -ApprovalBooking(); -function ApprovalBooking(){ +insertBooking(); +function insertBooking(){ $(".pending_table").submit(function(e){ e.preventDefault(); var dataForm = ($(this).serialize()); - var element =$(this); + $.ajax({ method: "post", url: "/bookings/", @@ -49,13 +49,35 @@ $("#bookACar").submit(function(e){ $.ajax({ method: 'POST', - url: "/book vehical", + url: "/bookings", data: data, success: function(data){ - $("#carBooking").modal('hide'); - $("#sucDiv").html(data); - $("#sucDiv").fadeIn(); - $("#sucDiv").fadeOut(2000); + console.log(data); + // $("#carBooking").modal('hide'); + // $("#sucDiv").html(data); + // $("#sucDiv").fadeIn(); + // $("#sucDiv").fadeOut(2000); + } + }); + $.ajax({ + method: 'POST', + url: "/bookings/freeCar", + data: data, + success: function(data){ + for (var i = 0; i < data.length; i++) { + $("select#plate_no").append(""); + } + // $("select#plate_no").append(""); + $("#approval").find('option').remove().end().append("") + $("#driver_id").find('option').remove().end().append("") + if(data['bookings']['approval']==null){ + $("#approval").append(""); + $("#approval").append(""); + } + if(data['bookings']['approval']==true){ + + $("#approval").append(""); + } + if(data['bookings']['approval']==false){ + $("#approval").append(""); + + } + + var i; + + for (i = 0; i < countOfCars; i++) { + + $('.plate_no').append(""); + + } + for (i = 0; i < countOfDrivers; i++) { + $('.driver_id').append(""); + } + + // $("#plate_no").val(data['plate_no']); + // $("#plate_no").append(""); + $("#approval_pickup_time").val(data['bookings']['approval_pickup_time']); + $("#approval_return_time").val(data['bookings']['approval_return_time']); + $("#approver_description").val(data['bookings']['approver_description']); + + } + }); + $("#updateModal").modal('show'); + }); +} }); //end of jqery \ No newline at end of file diff --git a/resources/views/bookings/index.blade.php b/resources/views/bookings/index.blade.php index 3ca570d..449a21c 100644 --- a/resources/views/bookings/index.blade.php +++ b/resources/views/bookings/index.blade.php @@ -1,225 +1,173 @@ @extends("layouts.app") @section('content') - - - -