You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					130 lines
				
				4.8 KiB
			
		
		
			
		
	
	
					130 lines
				
				4.8 KiB
			| 
								 
											2 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace App\Http\Controllers\Admin;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use App\Http\Controllers\Controller;
							 | 
						||
| 
								 | 
							
								use Illuminate\Support\Facades\Validator;
							 | 
						||
| 
								 | 
							
								use Illuminate\Http\Request;
							 | 
						||
| 
								 | 
							
								use App\Models\Banner;
							 | 
						||
| 
								 | 
							
								use App\Models\Restaurant;
							 | 
						||
| 
								 | 
							
								use Brian2694\Toastr\Facades\Toastr;
							 | 
						||
| 
								 | 
							
								use Carbon\Carbon;
							 | 
						||
| 
								 | 
							
								use Illuminate\Support\Facades\Storage;
							 | 
						||
| 
								 | 
							
								use App\CentralLogics\Helpers;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class BannerController extends Controller
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    function index()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $banners = Banner::latest()->paginate(config('default_pagination'));
							 | 
						||
| 
								 | 
							
								        return view('admin-views.banner.index', compact('banners'));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function store(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $validator = Validator::make($request->all(), [
							 | 
						||
| 
								 | 
							
								            'title' => 'required|max:191',
							 | 
						||
| 
								 | 
							
								            'image' => 'required|max:2048',
							 | 
						||
| 
								 | 
							
								            'banner_type' => 'required',
							 | 
						||
| 
								 | 
							
								            'zone_id' => 'required',
							 | 
						||
| 
								 | 
							
								            'restaurant_id' => 'required_if:banner_type,restaurant_wise',
							 | 
						||
| 
								 | 
							
								            'item_id' => 'required_if:banner_type,item_wise',
							 | 
						||
| 
								 | 
							
								        ], [
							 | 
						||
| 
								 | 
							
								            'zone_id.required' => translate('messages.select_a_zone'),
							 | 
						||
| 
								 | 
							
								            'restaurant_id.required_if'=> translate('messages.Restaurant is required when banner type is restaurant wise'),
							 | 
						||
| 
								 | 
							
								            'item_id.required_if'=> translate('messages.Food is required when banner type is food wise'),
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ($validator->fails()) {
							 | 
						||
| 
								 | 
							
								            return response()->json(['errors' => Helpers::error_processor($validator)]);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $banner = new Banner;
							 | 
						||
| 
								 | 
							
								        $banner->title = $request->title;
							 | 
						||
| 
								 | 
							
								        $banner->type = $request->banner_type;
							 | 
						||
| 
								 | 
							
								        $banner->zone_id = $request->zone_id;
							 | 
						||
| 
								 | 
							
								        $banner->image = Helpers::upload('banner/', 'png', $request->file('image'));
							 | 
						||
| 
								 | 
							
								        $banner->data = ($request->banner_type == 'restaurant_wise')?$request->restaurant_id:$request->item_id;
							 | 
						||
| 
								 | 
							
								        $banner->save();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json([], 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function edit(Banner $banner)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return view('admin-views.banner.edit', compact('banner'));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // public function view(Banner $banner)
							 | 
						||
| 
								 | 
							
								    // {
							 | 
						||
| 
								 | 
							
								    //     $restaurant_ids = json_decode($banner->restaurant_ids);
							 | 
						||
| 
								 | 
							
								    //     $restaurants = Restaurant::whereIn('id', $restaurant_ids)->paginate(10);
							 | 
						||
| 
								 | 
							
								    //     return view('admin-views.banner.view', compact('banner', 'restaurants', 'restaurant_ids'));
							 | 
						||
| 
								 | 
							
								    // }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function status(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $banner = Banner::findOrFail($request->id);
							 | 
						||
| 
								 | 
							
								        $banner->status = $request->status;
							 | 
						||
| 
								 | 
							
								        $banner->save();
							 | 
						||
| 
								 | 
							
								        Toastr::success(translate('messages.banner_status_updated'));
							 | 
						||
| 
								 | 
							
								        return back();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function update(Request $request, Banner $banner)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $validator = Validator::make($request->all(), [
							 | 
						||
| 
								 | 
							
								            'title' => 'required|max:191',
							 | 
						||
| 
								 | 
							
								            'banner_type' => 'required',
							 | 
						||
| 
								 | 
							
								            'zone_id' => 'required',
							 | 
						||
| 
								 | 
							
								            'image' => 'nullable|max:2048',
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            'restaurant_id' => 'required_if:banner_type,restaurant_wise',
							 | 
						||
| 
								 | 
							
								            'item_id' => 'required_if:banner_type,item_wise',
							 | 
						||
| 
								 | 
							
								        ], [
							 | 
						||
| 
								 | 
							
								            'zone_id.required' => translate('messages.select_a_zone'),
							 | 
						||
| 
								 | 
							
								            'restaurant_id.required_if'=> translate('messages.Restaurant is required when banner type is restaurant wise'),
							 | 
						||
| 
								 | 
							
								            'item_id.required_if'=> translate('messages.Food is required when banner type is food wise'),
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ($validator->fails()) {
							 | 
						||
| 
								 | 
							
								            return response()->json(['errors' => Helpers::error_processor($validator)]);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $banner->title = $request->title;
							 | 
						||
| 
								 | 
							
								        $banner->type = $request->banner_type;
							 | 
						||
| 
								 | 
							
								        $banner->zone_id = $request->zone_id;
							 | 
						||
| 
								 | 
							
								        $banner->image = $request->has('image') ? Helpers::update('banner/', $banner->image, 'png', $request->file('image')) : $banner->image;
							 | 
						||
| 
								 | 
							
								        $banner->data = $request->banner_type=='restaurant_wise'?$request->restaurant_id:$request->item_id;
							 | 
						||
| 
								 | 
							
								        $banner->save();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json([], 200);
							 | 
						||
| 
								 | 
							
								        // Toastr::success(translate('messages.banner_updated_successfully'));
							 | 
						||
| 
								 | 
							
								        // return redirect('admin/banner/add-new');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function delete(Banner $banner)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (Storage::disk('public')->exists('banner/' . $banner['image'])) {
							 | 
						||
| 
								 | 
							
								            Storage::disk('public')->delete('banner/' . $banner['image']);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $banner->delete();
							 | 
						||
| 
								 | 
							
								        Toastr::success(translate('messages.banner_deleted_successfully'));
							 | 
						||
| 
								 | 
							
								        return back();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function search(Request $request){
							 | 
						||
| 
								 | 
							
								        $key = explode(' ', $request['search']);
							 | 
						||
| 
								 | 
							
								        $banners=Banner::where(function ($q) use ($key) {
							 | 
						||
| 
								 | 
							
								            foreach ($key as $value) {
							 | 
						||
| 
								 | 
							
								                $q->orWhere('title', 'like', "%{$value}%");
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        })->limit(50)->get();
							 | 
						||
| 
								 | 
							
								        return response()->json([
							 | 
						||
| 
								 | 
							
								            'view'=>view('admin-views.banner.partials._table',compact('banners'))->render(),
							 | 
						||
| 
								 | 
							
								            'count'=>$banners->count()
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |