Current File : /home/tradevaly/www/app/Http/Controllers/Admin/CategoryController.php |
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Rfq;
use App\Models\Tag;
use App\Models\Product;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str;
use DataTables;
use App\Http\Controllers\User\UserController;
class CategoryController extends Controller
{
public function index(Request $request){
$data['categories'] = Category::latest()->where('parent_id', 0)->get();
if (request()->ajax()) {
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$actionBtn = '<a href="'.route('admin.category.add',$row->id).'" class="text-success" title="Add Subcategory" style="color: #fff">
<i class="fa fa-plus fa-lg"></i>
</a> <a href="'.route('admin.category.edit',$row->id).'" class="text-info" title="Edit" style="color: #fff">
<i class="fas fa-pencil-alt fa-lg"></i>
</a>
<a onclick="return confirm(\'Are you sure? If yes then all product will be deleted with this category.\')" href="'.route('admin.category.delete',$row->id).'" class="text-danger confirmDelete"><i class="fas fa-trash fa-lg"></i></a>';
return $actionBtn;
})
->addColumn('pic', function($row){
$actionBtn = ' <img style="height: 30px; width:30px; border-radius:10%;" src="'.asset('storage/images/admin/category/'.$row->image.'').'" alt="">';
return $actionBtn;
})
->addColumn('parent', function($row){
if($row->parent_id != 0) { return $row->parent->name; } else { return 'Main Category'; }
})
->addColumn('subcat', function($row){
if(Category::latest()->where('parent_id', $row->id)->count() > 0){ return '<a class="badge bg-warning" href="'.route('admin.category.subcat', $row->id).'">View Subcategories</a>'; } else { return 'No Sub Category'; }
})
->addColumn('statuschange', function($row){
if ($row->status==0){
return '<a class="btn btn-info updateCategoryStatus" title="Turn off" id="category-'.$row->id.'" category_id="'.$row->id.'" style="color: #fff">
<i class="fas fa-toggle-off"></i>
</a>';
} else {
return '<a class="btn btn-info updateCategoryStatus" title="Turn On" id="category-'.$row->id.'" category_id="'.$row->id.'" style="color: #fff">
<i class="fas fa-toggle-on"></i>
</a>';
}
})
->rawColumns(['action', 'statuschange', 'pic', 'subcat'])
->make(true);
}
return view('backend.pages.category.index')->with($data);
}
public function index_trash(Request $request){
if ($request->ajax()) {
$data = Category::onlyTrashed()->orderBy('deleted_at', 'DESC')->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$actionBtn = '<a href="'.route('admin.trash.category.trash_category_resore',$row->id).'" class="text-white btn btn-info" title="Restore" style="color: #fff">
<i class="fas fa-undo-alt"></i>
</a>
<a onclick="return confirm(\'Are you sure?\')" href="'.route('admin.trash.category.delete_trash',$row->id).'" class=" btn btn-danger text-white confirmDelete"><i class="fas fa-times"></i></a>';
return $actionBtn;
})
->addColumn('pic', function($row){
$actionBtn = ' <img style="height: 30px; width:30px; border-radius:10%;" src="'.asset('storage/images/admin/category/'.$row->image.'').'" alt="">';
return $actionBtn;
})
->addColumn('parent', function($row){
if($row->parent_id != 0 && !empty($row->parent)) { return $row->parent->name; } else { return 'Main Category'; }
})
->addColumn('subcat', function($row){
if(Category::onlyTrashed()->where('parent_id', $row->id)->count() > 0){ return '<a class="badge bg-warning" href="'.route('admin.trash.category.subcat', $row->id).'">View Subcategories</a>'; } else { return 'No Sub Category'; }
})->rawColumns(['action', 'statuschange', 'pic', 'subcat'])
->make(true);
}
return view('backend.pages.category.index_trash');
}
public function subcat(Request $request, $id){
$cat = Category::findOrfail($id);
if ($request->ajax()) {
$data = Category::where('parent_id', $id)->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$actionBtn = '<a href="'.route('admin.category.add',$row->id).'" class="btn btn-success" title="Add Subcategory" style="color: #fff">
<i class="material-icons">add</i>
</a> <a href="'.route('admin.category.edit',$row->id).'" class="btn btn-primary" title="Edit" style="color: #fff">
<i class="material-icons">edit</i>
</a><a onclick="return confirm(\'Are you sure?\')" href="'.route('admin.category.delete',$row->id).'" name="category" class="btn btn-danger confirmDelete" title="Delete" style="color: #fff">
<i class="material-icons">close</i>
</a>';
return $actionBtn;
})
->addColumn('pic', function($row){
$actionBtn = ' <img style="height: 30px; width:30px; border-radius:10%;" src="'.asset('storage/images/admin/category/'.$row->image.'').'" alt="">';
return $actionBtn;
})
->addColumn('parent', function($row){
if($row->parent_id != 0) { return $row->parent->name; } else { return 'Main Category'; }
})
->addColumn('subcat', function($row){
if(Category::where('parent_id', $row->id)->count() > 0){ return '<a class="btn btn-info" href="'.route('admin.category.subcat', $row->id).'">View Subcategories</a>'; } else { return 'No Sub Category'; }
})
->addColumn('statuschange', function($row){
if ($row->status==0){
return '<a class="btn btn-info updateCategoryStatus" title="Turn off / On" id="category-'.$row->id.'" category_id="'.$row->id.'" style="color: #fff">
<i class="fas fa-toggle-off"></i>
</a>';
} else {
return '<a class="btn btn-info updateCategoryStatus" title="Turn off / On" id="category-'.$row->id.'" category_id="'.$row->id.'" style="color: #fff">
<i class="fas fa-toggle-on"></i>
</a>';
}
})
->rawColumns(['action', 'statuschange', 'pic', 'subcat'])
->make(true);
}
return view('backend.pages.category.subcat', compact('cat'));
}
public function updateCategoryStatus($id){
$category = Category::findOrfail($id);
if($category->status == 1){
$status=0;
}else{
$status=1;
}
$category->status = $status;
$category->save();
Toastr::success('Success!','Category Updated Successfuly!');
return back();
}
public function add(Request $request, $id=0){
if($request->isMethod("post")){
$request->validate([
'name' => 'required|string|max:255',
], [
'name.required' => 'Please Enter Your Category Name',
]);
$url =Str::slug($request->name);
$data= new Category;
if($request->hasFile('image'))
{
$image=$request->file('image');
$currentDate=Carbon::now()->toDateString();
$imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if(!Storage::disk('public')->exists('images/admin/category'))
{
Storage::disk('public')->makeDirectory('images/admin/category');
}
$catImage = Image::make($image)->resize(120,90)->stream();
Storage::disk('public')->put('images/admin/category/'.$imageName,$catImage);
$data->image=$imageName;
}else{
$data->image= "default.png";
}
if($request->hasFile('banner'))
{
$banner=$request->file('banner');
$currentDate=Carbon::now()->toDateString();
$imageName=$currentDate.'-'.uniqid().'.'.$banner->getClientOriginalExtension();
if(!Storage::disk('public')->exists('images/admin/category/banner'))
{
Storage::disk('public')->makeDirectory('images/admin/category/banner');
}
$catImage = Image::make($banner)->resize(1920,332)->stream();
Storage::disk('public')->put('images/admin/category/banner/'.$imageName,$catImage);
$data->banner=$imageName;
}else{
$data->banner= "default.png";
}
$data->parent_id=$id;
$data->name=$request->name;
$data->slug=Str::slug($request->name);
$data->parent_id=$id;
$data->slug=$url;
$data->status=1;
$data->save();
Toastr::success('Success!','Category uploaded!');
return redirect()->back();
}
$categories = Category::where('parent_id', 0)->get();
return view('backend.pages.category.add')->with(compact('categories', 'id'));
}
public function delete($id){
$category=Category::findOrFail($id);
if($category->parent_id == 0){
$min_category=Category::where('parent_id',$category->id)->get()->count();
if($min_category !== 0){
Toastr::warning('warning!','Please Delete Subcategory First!');
return back();
}
$this->messages_delete_by_category($category->id);
}
if($category->parent_id !== 0 && $category->sub_id == 0){
$sub_category=Category::where('parent_id',$category->id)->get()->count();
if($sub_category !== 0){
Toastr::warning('warning!','Please Delete Child First!');
return back();
}
if($sub_category == 0){
foreach(Product::where('child_id',$category->id)->get() as $product){
$user = new UserController();
$user->delproduct($product->id);
}
}
}
$this->messages_delete_by_category($category->id);
if($category->image != 'image.jpg'){
if(Storage::disk('public')->exists('storage/images/admin/category/'.$category->image))
{
Storage::disk('public')->delete('storage/images/admin/category/'.$category->image);
}
}
$category->delete();
Toastr::success('warning!','Child Category Deleted With Products!');
return back();
}
public function delete_trash($id){
$user = new UserController();
$category = Category::onlyTrashed()->findOrFail($id);
$sub_categories = Category::withTrashed()->where('parent_id',$category->id)->get();
if(!empty($sub_categories)){
foreach($sub_categories as $sub_category ){
$child_categories = Category::withTrashed()->where('parent_id',$sub_category['id'])->get();
if(!empty($child_categories)){
foreach($child_categories as $child_category){
$this->trash_category_with_product($child_category->id);
$this->category_photo_fore_delete($child_category->image);
$child_category->forceDelete();
}
}
$this->trash_category_with_product($sub_category->id);
$this->category_photo_fore_delete($sub_category->image);
$sub_category->forceDelete();
}
}
$this->trash_category_with_product($category->id);
$this->category_photo_fore_delete($category->image);
$category->forceDelete();
Toastr::success('warning!','Category Permanent Deleted successfully!');
return back();
}
public function edit(Request $request, $id)
{
$data = Category::findOrFail($id);
$banner = $request->file('banner');
$image = $request->file('image');
if ($request->isMethod("post")) {
if (isset($image)) {
if($image->getClientOriginalExtension() == 'gif'){
$image = $request->file('image');
// Generate a unique file name
$fileName = uniqid().'.'.$image->getClientOriginalExtension();
// Save the file to the specified directory
$image->storePubliclyAs('public', $fileName);
}
$currentDate = Carbon::now()->toDateString();
$imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
if (Storage::disk('public')->exists('images/admin/category/' . $request->image)) {
Storage::disk('public')->delete('images/admin/category/' . $request->image);
}
$catImage = Image::make($image)->resize(120, 90)->stream();
Storage::disk('public')->put('images/admin/category/' . $imageName, $catImage);
$data->image = $imageName;
}
if (isset($banner)) {
$currentDate = Carbon::now()->toDateString();
$imageName = $currentDate . '-' . uniqid() . '.' . $banner->getClientOriginalExtension();
if (Storage::disk('public')->exists('images/admin/category/banner/' . $request->image)) {
Storage::disk('public')->delete('images/admin/category/banner/' . $request->image);
}
$catImage = Image::make($banner)->resize(1920, 332)->stream();
Storage::disk('public')->put('images/admin/category/banner/' . $imageName, $catImage);
$data->banner = $imageName;
}
$url = Str::slug($request->name);
$data->name = $request->name;
$data->slug = $url;
$data->status = 1;
$data->save();
Toastr::success('Success!', 'Category Updated!');
return redirect()->back();
}
$categories = Category::where('parent_id', 0)->get();
return view('backend.pages.category.edit')->with(compact('categories', 'data'));
}
public function messages_delete_by_category($id){
$rfqs = Rfq::where('category_id',$id)->get();
if($rfqs){
foreach($rfqs as $rfq){
if(Storage::disk('public')->exists('images/admin/rfq/'.$rfq->image))
{
Storage::disk('public')->delete('images/admin/rfq/'.$rfq->image);
}
$rfq->delete();
}
}
}
public function trash_category_resore($id){
$category = Category::onlyTrashed()->findOrFail($id);
if($category->parent_id ==! 0){
$parent = Category::onlyTrashed()->find($category->parent_id);
if($parent){
if($parent->parent_id ==! 0){
$grand = Category::onlyTrashed()->find($parent->parent_id);
if($grand){
$grand->restore();
}
};
$parent->restore();
}
};
$category->restore();
Toastr::success('Success!','Product Restore Successfully!');
return back();
}
public function trash_category_with_product($id){
$user = new UserController();
$products = Product::withTrashed()->where('child_id',$id)->get();
if(!empty($products)){
foreach($products as $product){
$user->delete_trash_product($product);
}
}
}
public function category_photo_fore_delete($image){
if($image !== 'image.jpg'){
if(Storage::disk('public')->exists('storage/images/admin/category/'.$image));
{
Storage::disk('public')->delete('storage/images/admin/category/'.$image);
}
}
}
}