Viewing file: MultiRestaurantController.php (27.33 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace Modules\MultiRestaurant\Http\Controllers;
use App\Models\OrderDetails; use App\Models\Page; use App\Models\FAQ; use App\Models\Restaurant; use App\Models\Setting; use App\Models\Settings; use App\Models\Table; use App\Models\Item; use App\Models\Category; use Illuminate\Contracts\Support\Renderable; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Modules\MultiRestaurant\Entities\Cart; use Illuminate\Support\Facades\File; use Modules\MultiRestaurant\Entities\City; use Modules\MultiRestaurant\Entities\Review; use Modules\MultiRestaurant\Entities\TableBooking; use Modules\MultiRestaurant\Entities\User; use Carbon\Carbon; use App\Models\Reservation; class MultiRestaurantController extends Controller { /** * Display a listing of the resource. * @return Renderable */ public function index() { $data['cities'] = City::where('status','active')->get(); $restaurants = Restaurant::orderBy('created_at', 'desc'); $new = $restaurants->where('on_multi_restaurant','publish')->take(15)->get(); $featured_restaurant = $restaurants->where('featured_restaurant','yes')->take(15)->get(); $categorys = Category::get(); $data['restaurants'] = $new; $data['featured_restaurants'] = $featured_restaurant; $data['categorys'] = $categorys; return view('multirestaurant::index',$data); } public function restaurants(Request $request) { $restaurants = Restaurant::where('on_multi_restaurant','publish')->paginate(10); $categorys = Category::get(); $data['restaurants'] = $restaurants; $data['categorys'] = $categorys; $data['name'] = $request->name; $data['date'] = $request->date; $data['time'] = $request->time; $data['location'] = $request->location; return view('multirestaurant::multiRestaurant',$data); }
public function getAllRestaurants(Request $request) { $query = Restaurant::with('items','reviews')->where('on_multi_restaurant','publish')->orderBy('created_at', 'desc');
if ($request->name) { $query->where('name', 'like', '%' . $request->name . '%') ->orWhere('location', 'like', '%' . $request->name . '%'); } if ($request->price_range) { $filteredItems = Item::whereBetween('price', [0, $request->price_range])->pluck('restaurant_id'); $query->whereIn('id', $filteredItems); }
if ($request->categories) { $categoryIds = explode(',', $request->categories); $item = Item::whereIn('category_id', $categoryIds)->pluck('restaurant_id'); $query->whereIn('id', $item); }
if ($request->date) { $dayName = Carbon::parse($request->date)->format('l'); $allRestaurants = $query->get(); $openIdrestaurant = [];
foreach ($allRestaurants as $restaurant) { $openingHours = json_decode($restaurant->opening_hours, true);
if (!is_array($openingHours)) continue;
$matchingDay = collect($openingHours)->first(function ($day) use ($dayName) { return $day['opening_date'] === $dayName && !empty($day['opening_hour']); });
if ($matchingDay && isset($matchingDay['opening_hour'])) { $timeRange = $matchingDay['opening_hour'];
if (strpos($timeRange, ' - ') !== false) { [$startTimeStr, $endTimeStr] = explode(' - ', $timeRange); try { $startTime = Carbon::parse($startTimeStr); $endTime = Carbon::parse($endTimeStr); if ($request->time) { $userTime = Carbon::parse($request->time); if ($userTime->between($startTime, $endTime)) { $openIdrestaurant[] = $restaurant->id; } } else { $openIdrestaurant[] = $restaurant->id; } } catch (\Carbon\Exceptions\InvalidFormatException $e) { continue; } } } }
$query->whereIn('id', $openIdrestaurant); }
if ($request->location) { $query->where('location', 'like', '%' . $request->location . '%'); }
$restaurants = $query->paginate(10);
return response()->json([ 'data' => $restaurants->items(), ]); }
public function getAllRestaurantItems(Request $request){ $restaurant = Restaurant::where('id',$request->id)->first();
$items = $restaurant->items();
if ($request->category_id) { $items = $items->where('category_id',$request->category_id); } $items = $items->with('tax')->get(); return response()->json([ 'data' => $items, 'currency_symbol' => $restaurant->currency_symbol, ]);
} public function getRestaurantsByCountry(Request $request){
$restaurants = Restaurant::with('items','reviews')->where('on_multi_restaurant','publish') ->orderBy('created_at', 'desc');
$state = getStateByCode($request->country); $country_res = ''; if($request->country){ $restaurants->where('country',$request->country);
$country_res = $restaurants->whereIn('state', $state)->get(); }
if($request->city){ $restaurants->where('state',$request->city); }
$restaurants = $restaurants->take(15)->get();
return response()->json([ 'data' => $restaurants, 'state' => $country_res->pluck('state') ?? [], 'country_res' => $country_res, ]); } public function restaurant($id) { $restaurant = Restaurant::where('id',$id)->first(); if(!$restaurant){ return redirect()->back()->with('error',trans('multirestaurant::layout.restaurant_not_found')); } $items = $restaurant->items()->get(); $categoryIds = $items->pluck('category_id'); $categories = Category::whereIn('id', $categoryIds)->get(); $data['restaurant'] = $restaurant; $data['categories'] = $categories; $data['reviews'] = Review::where('restaurant_id',$restaurant->id)->where('status','enable')->get(); $avg_reviews = Review::where('restaurant_id',$restaurant->id)->where('status','enable')->avg('rating'); $data['avg_reviews'] = round($avg_reviews, 2); $data['reviewCounts'] = Review::where('restaurant_id',$restaurant->id)->where('status', 'enable') ->selectRaw(' COUNT(*) as total, SUM(CASE WHEN rating = 5 THEN 1 ELSE 0 END) as excellent, SUM(CASE WHEN rating = 4 THEN 1 ELSE 0 END) as good, SUM(CASE WHEN rating = 3 THEN 1 ELSE 0 END) as average, SUM(CASE WHEN rating = 2 THEN 1 ELSE 0 END) as below_average, SUM(CASE WHEN rating = 1 THEN 1 ELSE 0 END) as poor') ->first();
return view('multirestaurant::restaurantDetails',$data); } public function make_reservation(Request $request){ if(env('APP_DEMO')){ return redirect()->back()->withErrors(['msg' => trans('layout.app_demo_message')]); } $restaurant = Restaurant::where('id',$request->restaurant_id)->first(); $request->validate([ 'date'=>'required', 'contact_number'=>'required', 'party_size'=>'required', ]);
$auth = auth()->user();
if (!$auth){ return redirect()->route('login',['type'=>'customer']); } $ready_reservation = Reservation::where('restaurant_id',$restaurant->id)->where('customer_id',$auth->id)->where('date',$request->date)->first(); if($ready_reservation){ return redirect()->back()->with('error',trans('multirestaurant::layout.ready_reservation')); } $reservation = new Reservation(); $reservation->date = $request->date; $reservation->party_size = $request->party_size; $reservation->restaurant_id = $restaurant->id; $reservation->customer_id = $auth->id; $reservation->contact_number = $request->contact_number; $reservation->status = 'pending'; $reservation->save();
return redirect()->back()->with('success',trans('multirestaurant::layout.make_reservation_success'));
} public function faq() { $data['faqs'] = FAQ::where('status','published')->get(); return view('multirestaurant::faq',$data); } public function about_us() { return view('multirestaurant::about_us'); } public function addToCart() { $user = auth()->user(); if (!$user){ return redirect()->route('login',['type'=>'customer']); } $data['carts'] = User::find($user->id)->carts()->get()->unique('restaurant_id'); $data['items'] = User::find($user->id)->carts()->get(); $data['order_status'] = User::find($user->id)->order_status()->first(); return view('multirestaurant::addToCart',$data); }
function deg2rads($e) { return $e * pi() / 180; }
function distance($lat1, $lon1, $lat2, $lon2, $unit = 'K') {
$theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $unit = strtoupper($unit);
if ($unit == "K") { return ($miles * 1.609344); } else if ($unit == "N") { return ($miles * 0.8684); } else { return $miles; } }
public function find_restaurant(Request $request) {
if (!$request->city) { $request->validate([ 'restaurant_location' => 'required', 'order_type' => 'required', ]); } if ($request->city){ $data['city_restaurants'] = Restaurant::where('location', 'like', '%'.$request->city.'%')->where('on_multi_restaurant','publish')->get(); }else{
$restaurants = Restaurant::where($request->order_type, '=', 'yes')->where('on_multi_restaurant','publish')->get();
$restInfo = []; foreach ($restaurants as $restaurant) { $lat = isset($restaurant->direction) ? json_decode($restaurant->direction)->lat : ''; $long = isset($restaurant->direction) ? json_decode($restaurant->direction)->long : ''; $dist = $this->distance($request->location_lat, $request->location_long, $lat, $long); $restInfo[] = ['id' => $restaurant->id, 'distance' => $dist]; }; array_multisort(array_map(function ($element) { return $element['distance']; }, $restInfo), SORT_ASC, $restInfo); $restaurantWithDistance = collect(array_slice($restInfo, 0, 1000)); $restLimitedIds = $restaurantWithDistance->pluck('id'); $resultRestaurants = $restaurants->whereIn('id', $restLimitedIds)->all(); $restData = []; foreach ($resultRestaurants as $key => $restaurant) {
$resDistance = $restaurantWithDistance->where('id', $restaurant->id)->first(); if ($resDistance['distance'] < 15) { $restData[$key]['id'] = $restaurant->id; $restData[$key]['name'] = $restaurant->name; $restData[$key]['delivery_fee'] = $restaurant->delivery_fee; $restData[$key]['location'] = $restaurant->location; $restData[$key]['distance'] = isset($resDistance['distance']) ? $resDistance['distance'] : ''; $restData[$key]['description'] = clean($restaurant->description); $restData[$key]['image'] = isset($restaurant->cover_image) ? asset('uploads/' . $restaurant->cover_image) : asset('images/df-rest.png'); $restData[$key]['route'] = route('show.restaurant', ['slug' => $restaurant->slug, 'id' => $restaurant->id, 'd' => isset($resDistance['distance']) ? round($resDistance['distance'], 1) : '0']);
}
}
$data['distance'] = $restInfo; $data['restaurants'] = $restData;
$data['tables'] = Table::where('status','active')->get(); $data['order_type'] = $request->order_type; }
return view('multirestaurant::multiRestaurant',$data);
} public function cart(Request $request) { $auth = auth()->user(); if (!$auth){ return redirect()->route('login',['type'=>'customer' , 'restaurant' => $request->restaurant]); }
foreach ($request->item_id as $item){ $item_id = \Modules\MultiRestaurant\Entities\Cart::where('item_id',$item)->where('customer_id',$auth->id)->first(); if($item_id){ foreach ($request->item_quantity as $quantity){ \Modules\MultiRestaurant\Entities\Cart::where('item_id',$item_id->item_id)->update(['item_quantity'=>$quantity+$item_id->item_quantity]); } }else{ foreach ($request->item_id as $item){ foreach ($request->item_quantity as $quantity){ $item_add = new Cart(); $item_add->customer_id= $auth->id; $item_add->item_quantity= $quantity; $item_add->restaurant_id = $request->restaurant; } $item_add->item_id = $item; $item_add->save(); } } }
return redirect()->back(); }
public function table_booking_store(Request $request) { $request->validate([ 'name' => 'required|max:191', 'email' => 'required', 'phone_number' => 'required|max:20', 'time' => 'required', 'date' => 'required', 'restaurant_table_id' => 'required', 'table_id' => 'required', 'person' => 'required', ]); $tableBokking= new TableBooking(); $tableBokking->restaurant_id= $request->restaurant_table_id; $tableBokking->name= $request->name; $tableBokking->phone_number = $request->phone_number; $tableBokking->email = $request->email; $tableBokking->time= $request->time; $tableBokking->date = $request->date; $tableBokking->table_id = $request->table_id; $tableBokking->person= $request->person; $tableBokking->reference_number = $request->reference_number; $tableBokking->reference_name = $request->reference_name; $tableBokking->comment = $request->comment; $tableBokking->save(); return redirect()->back()->with('success', trans('multirestaurant::layout.your_table_successfully_booked')); } public function table_booking() { $tables = Table::where('user_id',auth()->user()->id)->pluck('id'); $tableBooks = \Modules\MultiRestaurant\Entities\TableBooking::whereIn('table_id',$tables)->get(); $data['tables'] = $tableBooks; return view('multirestaurant::table_booking', $data); } public function table_booking_approved(Request $request) { \Modules\MultiRestaurant\Entities\TableBooking::where('id',$request->table_id)->update(['status'=>'approved']); return redirect()->back()->with('success', trans('multirestaurant::layout.table_booking_approve_success')); } public function table_booking_rejected(Request $request) { \Modules\MultiRestaurant\Entities\TableBooking::where('id',$request->table_id)->update(['status'=>'rejected']); return redirect()->back()->with('success', trans('multirestaurant::layout.table_booking_rejected_success')); }
public function table_booking_delete(Request $request) { \Modules\MultiRestaurant\Entities\TableBooking::where('id',$request->table_id)->delete(); return redirect()->back()->with('success', trans('multirestaurant::layout.table_delete_msg')); }
public function cities() { $user = auth()->user(); if ($user->type != 'admin'){ abort('404'); } $data['cities'] = User::find($user->id)->cities()->get(); return view('multirestaurant::cities.index',$data); }
public function city_create() { return view('multirestaurant::cities.create'); } public function city_edit($id) { $data['city'] = City::where('id',$id)->first(); return view('multirestaurant::cities.edit',$data); }
public function city_store(Request $request) { $request->validate([ "name" => "required|max:191", "status" => "required|in:active,inactive", 'city_image' => 'image', ]); $user = auth()->user(); if ($user->type != 'admin'){ abort('404'); } if ($request->hasFile('city_image')) { $file = $request->file('city_image'); $imageName = time() . '.' . $file->extension(); $file->move(public_path('/uploads'), $imageName); $request['image'] = $imageName; } User::find($user->id)->cities()->create($request->all()); return redirect()->back()->with('success', trans('multirestaurant::layout.city_create_successfully')); }
public function city_update(Request $request) { $request->validate([ "name" => "required|max:191", "status" => "required|in:active,inactive", 'city_image' => 'image', ]);
$user = auth()->user(); if ($user->type != 'admin'){ abort('404'); } if ($request->hasFile('city_image')) { $file = $request->file('city_image'); $imageName = time() . '.' . $file->extension(); $file->move(public_path('/uploads'), $imageName); $request['image'] = $imageName; }
User::find($user->id)->cities()->where('id',$request->id)->update($request->only('name','status','image'));
return redirect()->back()->with('success', trans('multirestaurant::layout.city_updated_successfully')); } public function city_delete($id) { $user = auth()->user(); if ($user->type != 'admin'){ abort('404'); } $city = User::find($user->id)->cities()->findOrFail($id); $city->delete(); return redirect()->back()->with('success', trans('multirestaurant::layout.city_deleted_successfully')); }
public function multirestaurant_template() { $user = auth()->user(); if ($user->type != 'admin'){ abort('404'); } return view('multirestaurant::template.index'); }
public function multirestaurant_template_store(Request $request) { if(env('APP_DEMO')){ return redirect()->back()->withErrors(['msg' => trans('layout.app_demo_message')]); } $user = auth()->user(); if ($user->type != 'admin'){ abort('404'); }
$data_template = Setting::where('name','multirestaurant_template')->first(); if ($data_template){ $template = json_decode($data_template->value); }
if(isset($template->bg_image_file_name) && $template->bg_image_file_name){ $request['bg_image_file_name'] = $template->bg_image_file_name; }
$previousImages = is_array($template->banner_bg_image_file_names) ? $template->banner_bg_image_file_names : explode(',', $template->banner_bg_image_file_names ?? '');
$existingImages = $request->input('existing_banner_bg_image_file_names', []); if (!is_array($existingImages)) { $existingImages = explode(',', $existingImages); }
$deletedImages = array_diff($previousImages, $existingImages); $newexistingImages = array_diff($existingImages, $deletedImages);
foreach ($deletedImages as $fileToDelete) { $path = public_path('/uploads/' . $fileToDelete); if (File::exists($path)) { File::delete($path); } }
$newImages = []; if ($request->hasFile('banner_bg_image')) { foreach ($request->file('banner_bg_image') as $index => $file) { if ($file->isValid()) { $filename = time() . '_banner_' . $index . '.' . $file->getClientOriginalExtension(); $file->move(public_path('/uploads'), $filename); $newImages[] = $filename; } } }
// Final array $finalImages = array_merge($newexistingImages, $newImages);
// Add to request for further processing $request['banner_bg_image_file_names'] = $finalImages;
if ($request->hasFile('section_mobile_img')) {
if (!empty($template->section_mobile_image)) { $oldFilePath = public_path('/uploads/' . $template->section_mobile_image); if (File::exists($oldFilePath)) { File::delete($oldFilePath); } }
// Upload new image $file = $request->file('section_mobile_img'); $imageOneName = time() . '_3.' . $file->getClientOriginalExtension(); $file->move(public_path('/uploads'), $imageOneName); $request['section_mobile_image'] = $imageOneName; } elseif (!empty($template->section_mobile_image)) { $request['section_mobile_image'] = $template->section_mobile_image; }
if ($request->hasFile('bg_image')) { $file = $request->file('bg_image'); $imageOneName = time().'_1' . '.' . $file->extension(); $file->move(public_path('/uploads'), $imageOneName); $request['bg_image_file_name'] = $imageOneName; } if(isset($template->section_two_bg_image_file_name) && $template->section_two_bg_image_file_name){ $request['section_two_bg_image_file_name'] = $template->section_two_bg_image_file_name; } if ($request->hasFile('section_two_bg_image')) { $file = $request->file('section_two_bg_image'); $imageTwoName = time().'_2' . '.' . $file->extension(); $file->move(public_path('/uploads'), $imageTwoName); $request['section_two_bg_image_file_name'] = $imageTwoName; } if(isset($template->section_three_bg_image_file_name) && $template->section_three_bg_image_file_name){ $request['section_three_bg_image_file_name'] = $template->section_three_bg_image_file_name; } if ($request->hasFile('section_three_bg_image')) { $file = $request->file('section_three_bg_image'); $imageThreeName = time().'_3' . '.' . $file->extension(); $file->move(public_path('/uploads'), $imageThreeName); $request['section_three_bg_image_file_name'] = $imageThreeName; }
if (isset($data_template) && $data_template->name == 'multirestaurant_template'){ $template = Setting::where('name', '=', 'multirestaurant_template')->first(); $template->value = json_encode($request->only('title','bg_image_file_name','banner_bg_image_file_names','section_two_blur_title', 'section_two_title', 'section_two_link', 'section_two_description_title', 'section_two_description', 'section_two_bg_image_file_name', 'section_three_blur_title', 'section_three_title', 'section_three_link', 'section_three_description_title', 'section_three_description', 'section_three_bg_image_file_name', 'section_four_description_title', 'section_four_description','section_mobile_title','section_mobile_sub_title','section_mobile_description','section_mobile_image')); $template->save(); }else{ $template = new Setting(); $template->name = 'multirestaurant_template'; $template->value = json_encode($request->only('title','bg_image_file_name','banner_bg_image_file_names','section_two_blur_title', 'section_two_title', 'section_two_link', 'section_two_description_title', 'section_two_description', 'section_two_bg_image_file_name', 'section_three_blur_title', 'section_three_title', 'section_three_link', 'section_three_description_title', 'section_three_description', 'section_three_bg_image_file_name', 'section_four_description_title', 'section_four_description','section_mobile_title','section_mobile_sub_title','section_mobile_description','section_mobile_image')); $template->save(); } cache()->flush();
return redirect()->back()->with('success', trans('multirestaurant::layout.template_updated_successfully')); }
public function multirestaurant_order_status() { $user = auth()->user(); if (!$user){ return redirect()->route('login',['type'=>'customer']); } $orders = User::find($user->id)->order_status()->get(); $items = []; foreach ($orders as $order){ $item = OrderDetails::where('order_id',$order->id)->get(); foreach ($item as $ite){ $items[]=$ite; } } $data['payment_status'] = $orders; $data['orders_items'] = $items; return view('multirestaurant::order_status',$data); }
/** * Show the form for creating a new resource. * @return Renderable */
/** * Store a newly created resource in storage. * @param Request $request * @return Renderable */ public function remove_add_to_cart_item($id) { if (!auth()->user()){ return redirect()->route('login',['type'=>'customer']); } \Modules\MultiRestaurant\Entities\Cart::where('restaurant_id',$id)->where('customer_id',auth()->user()->id)->delete(); return redirect()->back()->with('success', trans('multirestaurant::layout.extra_item_successfully')); }
/** * Show the specified resource. * @param int $id * @return Renderable */ public function remove_cart_item($id) { if (!auth()->user()){ return redirect()->route('login',['type'=>'customer']); } \Modules\MultiRestaurant\Entities\Cart::where('id',$id)->where('customer_id',auth()->user()->id)->delete(); return redirect()->back()->with('success', trans('multirestaurant::layout.extra_item_successfully')); }
/** * Show the form for editing the specified resource. * @param int $id * @return Renderable */ public function edit($id) { return view('multirestaurant::edit'); }
/** * Update the specified resource in storage. * @param Request $request * @param int $id * @return Renderable */ public function update(Request $request, $id) { // }
/** * Remove the specified resource from storage. * @param int $id * @return Renderable */ public function destroy($id) { // } public function page($page){
$data['page'] = Page::where('url',$page)->where('status','published')->firstOrFail(); return view('multirestaurant::page',$data); } }
|