Viewing File: /home/ubuntu/shop-website-base/app/Http/Controllers/User/UserController.php
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\Helper;
use App\Models\User,App\Models\ProductWishlist, App\Models\Order,App\Models\OrderProduct,App\Models\Product;
use App\Models\UserBillingAddress,App\Models\Category,App\Models\SubCategory;
use App\Models\Faq;
use App\Models\Admin;
use Auth, DB, Hash, Exception, File, Setting, Log ,Validator;
class UserController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
protected $paginate_count;
public function __construct() {
$this->middleware('auth:user',['except' => ['index']]);
}
/**
* @method index()
*
* @uses Index
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function index(Request $request) {
$banner_products = Product::VerifedProduct()
->where('is_banner',YES)
->orderBy('updated_at','desc')
->take(12)->get();
$skip = $request->skip ? $request->skip : 0;
$base_query = Product::CommonResponse();
if($request->search_key) {
$search_key = $request->search_key;
$base_query = $base_query
->Where('products.name','LIKE','%'.$search_key.'%')
->orWhere('products.description','LIKE','%'.$search_key.'%');
}
if($request->sort_key){
switch ($request->sort_key) {
case SORT_BY_POPULARITY:
$base_query = $base_query->orderBy('used_quantity','DESC');
break;
case SORT_BY_ALPHABETICAL:
$base_query = $base_query->orderBy('name','asc');
break;
case SORT_BY_LOW_TO_HIGH:
$base_query = $base_query->orderBy('amount','asc');
break;
case SORT_BY_HIGH_TO_LOW:
$base_query = $base_query->orderBy('amount','desc');
break;
default:
$base_query = $base_query->orderBy('updated_at','desc');
break;
}
}
else{
$base_query = $base_query->orderBy('updated_at','desc');
}
$base_query = $base_query->skip($skip)->take(8);
$total_products = $base_query->count();
$product = $base_query->get();
if($request->ajax()) {
$view = view('user.products._products')
->with('product', $product)
->with('total_current_products', count($product))
->with('total_products', $product)
->with('skip', $skip);
$data = ['total_products'=> $total_products, 'total_current_products' => count($product),'view'=> $view->render()];
return $data;
}
$today_special = Product::VerifedProduct()
->where('is_today_special',YES)
->orderBy('updated_at','desc')
->get();
$faqs = Faq::where('status',APPROVED)->get();
return view('user.index')
->with('page','index')
->with('banner_products', $banner_products)
->with('product',$product)
->with('total_current_products', count($product))
->with('total_products', $product)
->with('faqs', $faqs)
->with('today_special', $today_special);
}
/**
* @method index()
*
* @uses Index
*
* @created Bhawya
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function index_backeup() {
$categories = Category::where('status',APPROVED)->orderBy('updated_at','desc')->take(12)->get();
$sub_categories = SubCategory::where('status',APPROVED)->orderBy('updated_at','desc')->take(12)->get();
$new_products = Product::orderBy('updated_at','desc')->CommonResponse()->take(8)->get();
$category = Category::where('status',APPROVED)->orderBy('updated_at','desc')->first();
$featured_products = [];
if($category) {
$featured_products = Product::where('category_id',$category->id)->orderBy('updated_at','desc')->CommonResponse()->take(8)->get();
}
if(Auth::guard('user')->check()) {
$wishlist_product_ids = ProductWishlist::where('user_id',Auth::guard('user')->user()->id)->pluck('product_id');
$wishlist_products = Product::whereIn('product_id',$wishlist_product_ids)->orderBy('updated_at','desc')->CommonResponse()->take(8)->get();
}
return view('user.index')
->with('categories', $categories)
->with('sub_categories',$sub_categories)
->with('new_products',$new_products)
->with('featured_products',$featured_products)
->with('category_name',$category->name ?? '')
->with('wishlist_products',$wishlist_products ?? []);
}
/**
* @method profile()
*
* @uses profile
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function profile() {
return view('user.account.profile')->with('page', 'profile');
}
/**
* @method wishlist()
*
* @uses wishlist
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function wishlist(Request $request){
$product_wishlists = ProductWishlist::where('user_id',Auth::guard('user')->user()->id)->get();
return view('user.wishlists.index')->with('page', 'wishlist')->with('product_wishlists',$product_wishlists);
}
/**
* @method orders()
*
* @uses orders
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function orders(Request $request){
$orders = Order::where('user_id',Auth::guard('user')->user()->id)->orderBy('updated_at','desc')->get();
return view('user.orders.index')
->with('page', 'orders')
->with('orders',$orders);
}
/**
* @method product_wishlist_delete()
*
* @uses delete the user wishlist based on user id
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function product_wishlist_delete(Request $request) {
try {
DB::begintransaction();
$product_wishlist = ProductWishlist::find($request->product_wishlist_id);
if(!$product_wishlist) {
throw new Exception(tr('wishlist_not_found'), 101);
}
if($product_wishlist->delete()) {
DB::commit();
return redirect()->route('user.wishlist')->with('flash_success',tr('wishlist_delete_success'));
}
throw new Exception(tr('wishlist_delete_failed'));
} catch(Exception $e){
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method profile_save()
*
* @uses To update details of the user
*
* @created sakthi
*
* @updated
*
* @param object $request - request details
*
* @return json response of the user
*/
public function profile_save(Request $request) {
try {
DB::begintransaction();
$rules = [
'first_name' => 'required|max:191',
'last_name' => 'required|max:191',
'email' => $request->user_id ? 'required|email|max:191|unique:users,email,'.$request->user_id.',id' : 'required|email|max:191|unique:users,email,NULL,id',
'mobile' => $request->mobile ? 'digits_between:6,13' : '',
'picture'=> 'mimes:jpg,png,jpeg|max:2048|nullable',
'user_id' => 'nullable|exists:users,id',
];
Helper::custom_validator($request->all(), $rules);
if (strlen($request->mobile) == substr_count($request->mobile, 0)) {
return redirect()->back()->withInput()->with('flash_error', tr('mobile_validate_0'));
}
$user = User::find($request->user_id);
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->email = $request->email;
$user->mobile = $request->mobile;
$user->about = $request->about;
// Upload picture
if($request->hasFile('picture')) {
if($request->user_id) {
Helper::storage_delete_file($user->picture, FILE_PATH_USER);
// Delete the old pic
}
$user->picture = Helper::storage_upload_file($request->file('picture'), FILE_PATH_USER);
}
if($user->save()) {
DB::commit();
return redirect()->route('user.profile')->with('flash_success', tr('profile_updated_success'));
}
throw new Exception(tr('profile_update_failed'),101);
} catch(Exception $e){
DB::rollback();
return redirect()->back()->withInput()->with('flash_error', $e->getMessage());
}
}
/**
* @method billing_address()
*
* @uses List user billing address
*
* @created Ganesh
*
* @updated Ganesh
*
* @param object $request
*
* @return user details
*/
public function billing_address(Request $request) {
try {
$billing_address = UserBillingAddress::where('user_id',Auth::guard('user')->user()->id)->orderBy('updated_at','desc')->get();
$billings = new UserBillingAddress;
return view('user.billing_address.index')
->with('page', 'billing_address')
->with('billings',$billings)
->with('billing_address',$billing_address);
} catch (Exception $e) {
return redirect()->back()->withInput()->with('flash_error', $e->getMessage());
}
}
/**
* @method billing_address_save()
*
* @uses save the user address based on user id
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function billing_address_save(Request $request) {
try {
DB::begintransaction();
$rules = [
'address' => 'required|max:255',
'street_address' => 'required|max:255',
'zipcode' => 'required|max:8',
'city' => 'required|max:50',
'type'=> 'required',
];
Helper::custom_validator($request->all(), $rules);
$user_billing_address = UserBillingAddress::find($request->billing_address_id) ?? new UserBillingAddress;
$user_billing_address->user_id = Auth::guard('user')->user()->id;
$user_billing_address->street_address = $request->street_address;
$user_billing_address->address = $request->address;
$user_billing_address->zipcode = $request->zipcode;
$user_billing_address->city = $request->city;
$user_billing_address->type = $request->type;
if($user_billing_address->save()) {
$message = $request->billing_address_id ? tr('billing_address_update_success') : tr('billing_address_create_success');
DB::commit();
return redirect()->route('user.billing_address')->with('flash_success',$message);
}
throw new Exception(tr('profile_update_failed'),101);
} catch (Exception $e) {
return redirect()->back()->withInput()->with('flash_error', $e->getMessage());
}
}
/**
* @method billing_address_delete()
*
* @uses delete the user address based on user id
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function billing_address_delete(Request $request) {
try {
DB::begintransaction();
$billing_address = UserBillingAddress::find($request->billing_address_id);
if(!$billing_address) {
throw new Exception(tr('billing_address_not_found'), 101);
}
if($billing_address->delete()) {
DB::commit();
return redirect()->route('user.billing_address')->with('flash_success',tr('billing_address_delete_success'));
}
throw new Exception(tr('billing_address_delete_failed'));
} catch(Exception $e){
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method change_password()
*
* @uses To change the password viewpage
*
* @created Sakthi
*
* @updated
*
* @param object $request - Password & confirm Password
*
* @return return view page
*/
public function change_password() {
return view('user.account.password')->with('page', 'change_password');
}
/**
* @method password_save()
*
* @uses To change the password of the user
*
* @created Sakthi
*
* @updated
*
* @param object $request - Password & confirm Password
*
* @return return response of the user
*/
public function password_save(Request $request) {
try {
DB::beginTransaction();
$rules = [
'password' => 'required|confirmed|min:6',
'old_password' => 'required|min:6',
];
Helper::custom_validator($request->all(), $rules);
$user = User::find($request->user_id);
if(!$user) {
Auth::guard('user')->logout();
throw new Exception(tr('user_not_found'), 101);
}
if(Hash::check($request->old_password,$user->password)) {
$user->password = Hash::make($request->password);
$user->save();
DB::commit();
Auth::guard('user')->logout();
return redirect()->route('user.login')->with('flash_success', tr('user_password_change_success'));
} else {
throw new Exception(tr('user_password_mismatch'));
}
} catch(Exception $e) {
DB::rollback();
return redirect()->back()->withInput()->with('flash_error' , $e->getMessage());
}
}
/**
* @method delete_account()
*
* @uses To delete the account
*
* @created Sakthi
*
* @updated
*
* @param object $request - user_id
*
* @return return view page
*/
public function delete_account() {
return view('user.account.delete')->with('page', 'delete_account');
}
/**
* @method delete_account_process
*
* @uses delete logged user account
*
* @created Ganesh
*
* @updated
*
* @param object $request -
*
* @return response success/failure message
*
**/
public function delete_account_process(Request $request) {
try {
DB::beginTransaction();
// Validation start
$rules = ['password' => 'required'];
Helper::custom_validator($request->all(), $rules, $custom_errors = []);
// Validation end
$user = User::find($request->user_id);
if(!$user) {
throw new Exception(tr('not_registered_user'));
}
if(!Hash::check($request->password, $user->password)) {
throw new Exception(tr('invalid_password'));
}
if($user->delete()) {
DB::commit();
return redirect()->route('user.login')->with('flash_success', tr('account_delete_success'));
} else {
throw new Exception(api_error(119), 119);
}
} catch(Exception $e) {
DB::rollback();
return redirect()->back()->with('flash_error',$e->getMessage());
}
}
/**
* @method order_cancel()
*
* @uses orders
*
* @created Ganesh
*
* @updated
*
* @param object $request - User Id
*
* @return response of success/failure details with view page
*
*/
public function orders_cancel(Request $request){
try {
DB::beginTransaction();
$order = Order::find($request->order_id);
if(!$order) {
throw new Exception(tr('order_not_found'), 101);
}
$order->status = ORDER_CANCELLED;
if($order->save()) {
DB::commit();
$message = tr('order_cancel_success');
$this->dispatch(new \App\Jobs\OrderCancelJob($order));
return redirect()->back()->with('flash_success', $message);
}
throw new Exception(tr('order_cancel_failed'));
} catch(Exception $e) {
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
}
Back to Directory
File Manager