Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/app/Http/Controllers/Shop/ |
<?php
namespace App\Http\Controllers\Shop;
use App\Models\Banner;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Post;
use App\Models\PostCategory;
use App\Models\PostTag;
use App\Models\Product;
use App\Models\Setting;
use App\Models\User;
use App\Models\Newsletter;
use Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Storage;
use Session;
use App\Notifications\WelcomeNotification;
class FrontendController extends Controller
{
public function index(Request $request)
{
return redirect()->route($request->user()->role);
}
public function home()
{
$learningCategoryId = Category::where('slug', 'learning')->value('id');
$featured = Product::with(['reviews', 'cat_info'])->where('status', 'active')
->where('is_featured', 1)
->where('cat_id', '!=', $learningCategoryId)
->orderBy('price', 'DESC')->limit(2)->get();
$posts = Post::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
$banners = Banner::where('status', 'active')->limit(3)->orderBy('id', 'DESC')->get();
$products = Product::with(['reviews', 'cat_info'])
->where('status', 'active')
->where('cat_id', '!=', $learningCategoryId)
->orderBy('id', 'DESC')->limit(8)->get();
$category = Category::where('status', 'active')->where('is_parent', 1)->orderBy('title', 'ASC')->get();
$hotProducts = Product::with(['reviews', 'cat_info'])->where('status', 'active')
->where('condition', 'hot')
->where('cat_id', '!=', $learningCategoryId)
->orderBy('id', 'DESC')->limit(8)->get();
return view('frontend.new-index')
->with('featured', $featured)
->with('posts', $posts)
->with('banners', $banners)
->with('product_lists', $products)
->with('category_lists', $category)
->with('apartments', $featured)// TEST only
->with('popularSales', $products)
->with('hotProducts', $hotProducts);
}
public function aboutUs()
{
return view('frontend.pages.about-us');
}
public function contact()
{
return view('frontend.pages.contact', [
'siteSettings' => Setting::get()
]);
}
public function productDetail($slug)
{
$product = Product::getProductBySlug($slug);
if (!$product) {
abort(404);
}
if ($product->cat_info?->slug !== 'learning') {
if (!Cookie::has(Product::RECENTLY_VIEWED)) {
$recentlyViewed = [];
} else {
$recentlyViewed = explode(',', Cookie::get(Product::RECENTLY_VIEWED));
}
array_unshift($recentlyViewed, $product->id);
Cookie::queue(Product::RECENTLY_VIEWED, implode(',', $recentlyViewed), (60 * 60 * 12) * 2);
}
return view('frontend.pages.new-product-detail')
->with('product_detail', $product);
}
private function searchFilter(Request $request, $products)
{
if ($request->input('category')) {
$slug = explode(',', $request->input('category'));
$cat_ids = Category::select('id')->whereIn('slug', $slug)->pluck('id')->toArray();
$products->whereIn('cat_id', $cat_ids);
}
if ($request->input('brand')) {
$slugs = explode(',', $request->input('category'));
$brand_ids = Brand::select('id')->whereIn('slug', $slugs)->pluck('id')->toArray();
$products->whereIn('brand_id', $brand_ids);
}
if ($request->input('sortBy')) {
switch ($request->input('sortBy')) {
case 'title':
$products = $products->orderBy('title', 'ASC');
break;
case 'price':
$products = $products->orderBy('price', 'ASC');
break;
}
}
if ($request->input('price_range')) {
$price = explode('-', $request->input('price_range'));
$products->whereBetween('price', $price);
}
// Sort by number
if (!empty($_GET['show'])) {
$products = $products->where('status', 'active')->paginate($_GET['show']);
} else {
$products = $products->where('status', 'active')->paginate(6);
}
return $products;
}
public function productLists(Request $request)
{
$products = Product::query()->with('reviews');
$products = $this->searchFilter($request, $products);
$viewName = $request->input('mode') === 'list' ? 'frontend.pages.product-lists' : 'frontend.pages.product-grids';
return view($viewName)
->with('products', $products);
}
public function productSearch(Request $request)
{
$products = Product::query();
$term = str_replace(' ', '%', trim($request->input('q')));
if (!empty($term)) {
$products = $products
->where(function ($query) use ($term) {
$query
->orwhere('title', 'like', "{$term}%")
->orwhere('slug', 'like', "{$term}%")
->orwhere('description', 'like', "{$term}%")
->orwhere('summary', 'like', "{$term}%")
->orwhere('price', 'like', "{$term}%");
});
}
$this->searchFilter($request, $products);
return view('frontend.pages.product-grids')
->with('products', $products->paginate('9'));
}
public function productBrand(Request $request)
{
$products = Product::getByBrand($request->slug);
$recentProducts = Product::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
return view('frontend.pages.product-grids')
->with('products', $products)
->with('recent_products', $recentProducts);
}
public function productCat(Request $request)
{
$products = Product::getProductsByCategorySlug($request->slug);
$recentProducts = Product::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
$viewName = $request->input('mode') === 'list' ? 'frontend.pages.product-lists' : 'frontend.pages.product-grids';
return view($viewName)
->with('products', $products)
->with('recent_products', $recentProducts);
}
public function productSubCat(Request $request)
{
$products = Product::getProductsBySubCategorySlug($request->sub_slug);
$recent_products = Product::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
$viewName = $request->input('mode') === 'list' ? 'frontend.pages.product-lists' : 'frontend.pages.product-grids';
return view($viewName)
->with('products', $products)
->with('recent_products', $recent_products);
}
public function blog()
{
$post = Post::query();
if (!empty($_GET['category'])) {
$slug = explode(',', $_GET['category']);
// dd($slug);
$cat_ids = PostCategory::select('id')->whereIn('slug', $slug)->pluck('id')->toArray();
return $cat_ids;
$post->whereIn('post_cat_id', $cat_ids);
// return $post;
}
if (!empty($_GET['tag'])) {
$slug = explode(',', $_GET['tag']);
// dd($slug);
$tag_ids = PostTag::select('id')->whereIn('slug', $slug)->pluck('id')->toArray();
// return $tag_ids;
$post->where('post_tag_id', $tag_ids);
// return $post;
}
if (!empty($_GET['show'])) {
$post = $post->where('status', 'active')->orderBy('id', 'DESC')->paginate($_GET['show']);
} else {
$post = $post->where('status', 'active')->orderBy('id', 'DESC')->paginate(9);
}
// $post=Post::where('status','active')->paginate(8);
$rcnt_post = Post::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
return view('frontend.pages.blog')->with('posts', $post)->with('recent_posts', $rcnt_post);
}
public function blogDetail($slug)
{
$post = Post::getPostBySlug($slug);
$rcnt_post = Post::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
// return $post;
return view('frontend.pages.blog-detail')->with('post', $post)->with('recent_posts', $rcnt_post);
}
public function blogSearch(Request $request)
{
// return $request->all();
$rcnt_post = Post::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
$posts = Post::orwhere('title', 'like', '%'.$request->search.'%')
->orwhere('quote', 'like', '%'.$request->search.'%')
->orwhere('summary', 'like', '%'.$request->search.'%')
->orwhere('description', 'like', '%'.$request->search.'%')
->orwhere('slug', 'like', '%'.$request->search.'%')
->orderBy('id', 'DESC')
->paginate(8);
return view('frontend.pages.blog')->with('posts', $posts)->with('recent_posts', $rcnt_post);
}
public function blogFilter(Request $request)
{
$data = $request->all();
// return $data;
$catURL = '';
if (!empty($data['category'])) {
foreach ($data['category'] as $category) {
if (empty($catURL)) {
$catURL .= '&category='.$category;
} else {
$catURL .= ','.$category;
}
}
}
$tagURL = '';
if (!empty($data['tag'])) {
foreach ($data['tag'] as $tag) {
if (empty($tagURL)) {
$tagURL .= '&tag='.$tag;
} else {
$tagURL .= ','.$tag;
}
}
}
// return $tagURL;
// return $catURL;
return redirect()->route('blog', $catURL.$tagURL);
}
public function blogByCategory(Request $request)
{
$post = PostCategory::getBlogByCategory($request->slug);
$rcnt_post = Post::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
return view('frontend.pages.blog')->with('posts', $post->post)->with('recent_posts', $rcnt_post);
}
public function blogByTag(Request $request)
{
// dd($request->slug);
$post = Post::getBlogByTag($request->slug);
// return $post;
$rcnt_post = Post::where('status', 'active')->orderBy('id', 'DESC')->limit(3)->get();
return view('frontend.pages.blog')->with('posts', $post)->with('recent_posts', $rcnt_post);
}
// Login
public function login()
{
if (auth()->check()) {
if (Auth::user()->role === 'admin') {
return redirect()->route('admin');
} else {
return redirect()->route('home');
}
}
if (request()->input('redirect_uri')) {
Session::put('url.intended', request()->input('redirect_uri'));
}
return view('frontend.pages.login');
}
public function loginSubmit(Request $request)
{
$data = $request->all();
if (Auth::attempt([
'email' => $data['email'],
'password' => $data['password'],
'status' => 'active'
])
) {
Session::put('user', $data['email']);
request()->session()->flash('success', 'Successfully login');
if (Auth::user()->role === 'admin') {
return redirect()->route('admin');
} else {
return redirect()->intended(route('home'));
}
} else {
request()->session()->flash('error', 'Invalid email and password please try again!');
return redirect()->back();
}
}
public function logout()
{
Session::forget('user');
Auth::logout();
request()->session()->flash('success', 'Logout successfully');
return back();
}
public function register()
{
return view('frontend.pages.register');
}
public function registerSubmit(Request $request)
{
$this->validate($request, [
'first_name' => 'string|required|min:2',
'last_name' => 'string|required|min:2',
'email' => 'string|required|email|unique:users,email',
'phone' => 'required',
'address' => 'string|required',
'password' => 'required|min:6|confirmed',
'bvn' => 'required',
'identification_kind' => 'required',
'identification_value' => 'required',
'identification_file' => 'required|mimes:pdf,jpg,gif,png',
'accept_tc' => 'required'
]);
$data = $request->all();
if ($user = $this->create($data)) {
$path = "documents/{$user->id}/";
$disk = Storage::disk('local');
$documents = [];
if ($request->file('identification_file')) {
$saveAs = md5(uniqid(true));
$file = $request->file('identification_file');
$documents['identification'] = [
'kind' => $request->input('identification_kind'),
'value' => $request->input('identification_value'),
'saveAs' => "{$saveAs}.".$file->getClientOriginalExtension(),
'originalFile' => $file->getClientOriginalName(),
];
$disk->putFileAs(
$path,
$file,
$documents['identification']['saveAs']
);
}
if ($request->input('accept_newsletter_email')) {
Newsletter::create([
'email' => $user->email
]);
}
$user->updateQuietly(['documents' => $documents]);
Session::put('user', $data['email']);
$user->notify(new WelcomeNotification($user));
request()->session()->flash('success', 'Successfully registered');
return redirect()
->route('login.form');
}
return back()
->withError('Please try again!');
}
public function create(array $data)
{
return User::create([
'name' => "{$data['last_name']} {$data['first_name']}",
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'address' => $data['address'],
'password' => Hash::make($data['password']),
'bvn' => $data['bvn'],
'status' => 'active',
]);
}
// Reset password
public function showResetForm()
{
return view('auth.passwords.old-reset');
}
public function subscribe(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
]);
if (Newsletter::create([
'email' => $request->input('email')
])) {
request()->session()->flash('success', 'Subscribed! Please check your email');
} else {
request()->session()->flash('error', 'Something went wrong! please try again');
}
return redirect()->route('home');
// if (!Newsletter::isSubscribed($request->email)) {
// Newsletter::subscribePending($request->email);
// if (Newsletter::lastActionSucceeded()) {
// request()->session()->flash('success', 'Subscribed! Please check your email');
// return redirect()->route('home');
// } else {
// Newsletter::getLastError();
// return back()->with('error', 'Something went wrong! please try again');
// }
// } else {
// request()->session()->flash('error', 'Already Subscribed');
// return back();
// }
}
}