Current File : /home/tradevaly/www/app/Http/Controllers/Admin/BannerController.php |
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\User;
use App\Models\Category;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Mail;
use Illuminate\Support\Facades\Notification;
use App\Notifications\admin\advertisment\ActiveAdvertisment;
class BannerController extends Controller
{
public function index(){
$data['categories']= Banner::where('position','top_category_banner')->orWhere('position','bottom_category_banner')->latest()->get();
$data['buyers']= Banner::where('position','top_buyer_banner')->orWhere('position','bottom_buyer_banner')->latest()->get();
$data['products']= Banner::where('position','top_product_banner')->orWhere('position','bottom_product_banner')->latest()->get();
$data['companies']= Banner::where('position','top_company_banner')->orWhere('position','bottom_company_banner')->latest()->get();
$data['manufactures']= Banner::where('position','top_manufacture_banner')->orWhere('position','bottom_manufacture_banner')->latest()->get();
$data['contacts']= Banner::where('position','contact-banner')->get();
$data['banners']= Banner::Where('position','slider')->orWhere('position','after-banner-left')->orWhere('position','after-banner-left-center')->orWhere('position','after-banner-right-center')->orWhere('position','after-banner-right')->orWhere('position','after-category-left')->orWhere('position','after-category-right')->latest()->get();
return view('backend.pages.Banner.index')->with($data);
}
public function buyers(){
$data['banners'] = Banner::where('position','top_buyer_banner')->orWhere('position','bottom_buyer_banner')->latest()->get();
return view('backend.pages.Banner.buyers')->with($data);
}
public function product(){
$data['banners'] = Banner::where('position','top_product_banner')->orWhere('position','bottom_product_banner')->latest()->get();
return view('backend.pages.Banner.product')->with($data);
}
public function contact(){
$data['banners'] = Banner::where('position','contact-banner')->latest()->get();
return view('backend.pages.Banner.contact')->with($data);
}
public function company(){
$data['banners'] = Banner::where('position','top_company_banner')->orWhere('position','bottom_company_banner')->latest()->get();
return view('backend.pages.Banner.company')->with($data);
}
public function manufacture(){
$data['banners'] = Banner::where('position','top_manufacture_banner')->orWhere('position','bottom_manufacture_banner')->latest()->get();
return view('backend.pages.Banner.manufacture')->with($data);
}
public function category(){
$data['banners'] = Banner::where('position','top_category_banner')->orWhere('position','bottom_category_banner')->latest()->get();
return view('backend.pages.Banner.category')->with($data);
}
public function updateBannerStatus(Request $request){
$banner = Banner::find($request->banner_id);
if($request->ajax()){
$data= $request->all();
if($banner->status==1){
$status=0;
}else{
$status=1;
}
$banner->status = $status;
$banner->save();
return response()->json(['status'=>$status, 'id'=>$data['banner_id']]);
}
}
public function delete($id){
$data= Banner::find($id);
if(Storage::disk('public')->exists('images/admin/banner/'.$data->image))
{
Storage::disk('public')->delete('images/admin/banner/'.$data->image);
}
$data->delete();
Toastr::success('Deleted!');
return redirect()->back();
}
public function add(Request $request){
$data['users'] = User::latest()->get();
if($request->isMethod("post")){
$request->validate([
'image'=>'required|mimes:png,jpg,jpeg,webp,gif|max:5000',
'link'=>'required',
'start'=>'required',
'end'=>'required',
'user_id'=>'required',
]);
$banner =new Banner;
if($request->hasFile('image'))
{
$image = $request->file('image');
$currentDate=Carbon::now()->toDateString();
$imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if(!Storage::disk('public')->exists('images/admin/banner/'.$image))
{
Storage::disk('public')->makeDirectory('images/admin/banner/'.$image);
}
if($image->getClientOriginalExtension() == 'gif'){
Storage::disk('public')->put('images/admin/banner/',$image);
$imageName = $image->hashName();
}
else{
$bannerImage = Image::make($image)->resize(840,418)->stream();
Storage::disk('public')->put('images/admin/banner/'.$imageName,$bannerImage);
}
// $bannerImage = Image::make($image)->resize(840,418)->stream();
// Storage::disk('public')->put('images/admin/banner/'.$imageName,$bannerImage);
$banner->image=$imageName;
$banner->isActive=1;
}else{
$banner->image= "default.png";
}
if($request->category_id){
$banner->category_id = $request->category_id;
}
$startDateTime = Carbon::parse($request->start);
$endDateTime = Carbon::parse($request->end);
$banner->start = $startDateTime;
$banner->end = $endDateTime;
$banner->user_id=$request->user_id;
$banner->link=$request->link;
$banner->position=$request->position;
$banner->save();
$user = User::find($request->user_id);
Notification::send($user, new ActiveAdvertisment($user));
Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new ActiveAdvertisment($user));
Toastr::success('Success!','Banner Uploaded!');
return redirect()->back();
}
$data['categories'] = Category::where('parent_id' , 0)->latest()->get();
return view('backend.pages.Banner.add')->with($data);
}
public function edit(Request $request ,$id){
$data['banner']=Banner::find($id);
$data['users'] = User::latest()->get();
$data['categories'] = Category::where('parent_id' , 0)->latest()->get();
if($request->isMethod("post")){
$banner = $data['banner'];
$request->validate([
'image'=>'nullable|mimes:png,jpg,jpeg,webp,gif|max:5000',
'link'=>'required',
'start'=>'required',
'end'=>'required',
'user_id'=>'required',
]);
if($request->hasFile('image'))
{
$image=$request->file('image');
$currentDate=Carbon::now()->toDateString();
$imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if(Storage::disk('public')->exists('images/admin/banner/'.$request->image))
{
Storage::disk('public')->delete('images/admin/banner/'.$request->image);
}
if($image->getClientOriginalExtension() == 'gif'){
Storage::disk('public')->put('images/admin/banner/',$image);
$imageName = $image->hashName();
}
else{
$bannerImage = Image::make($image)->resize(840,418)->stream();
Storage::disk('public')->put('images/admin/banner/'.$imageName,$bannerImage);
}
$banner->image=$imageName;
}
if($request->category_id){
$banner->category_id = $request->category_id;
}
$startDateTime = Carbon::parse($request->start);
$endDateTime = Carbon::parse($request->end);
$banner->start = $startDateTime;
$banner->end = $endDateTime;
$banner->user_id=$request->user_id;
$banner->link=$request->link;
$banner->save();
Toastr::success('Success!','Updated successfully!');
return back();
}
return view('backend.pages.Banner.edit')->with($data);
}
public function home_page_banner(Request $request ,$id){
$data['users'] = User::latest()->get();
$banner = Banner::findOrFail($id);
$data['categories'] = Category::where('parent_id' , 0)->latest()->get();
if($request->isMethod("post")){
if($request->hasFile('image'))
{
$image=$request->file('image');
$currentDate=Carbon::now()->toDateString();
$imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if(Storage::disk('public')->exists('images/admin/banner/'.$request->image))
{
Storage::disk('public')->delete('images/admin/banner/'.$request->image);
}
if($image->getClientOriginalExtension() == 'gif'){
Storage::disk('public')->put('images/admin/banner/',$image);
$imageName = $image->hashName();
}
else{
$bannerImage = Image::make($image)->resize(840,418)->stream();
Storage::disk('public')->put('images/admin/banner/'.$imageName,$bannerImage);
}
$banner->image=$imageName;
}
$startDateTime = Carbon::parse($request->start);
$endDateTime = Carbon::parse($request->end);
$banner->link=$request->link;
$banner->position=$request->position;
$banner->start = $startDateTime;
$banner->user_id = $request->user_id;
$banner->end = $endDateTime;
$banner->status=1;
$banner->save();
// dd($banner);
Toastr::success('Success!','Banner Updated successfully!');
return redirect('b2b/component/banner/index');
}
$data['banner']=$banner;
// die();
return view('backend.pages.Banner.home_banner_edit')->with($data);
}
}