sarga/packages/Webkul/Customer/src/Http/Controllers/CustomerController.php

244 lines
8.0 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Customer\Http\Controllers;
use Hash;
use Illuminate\Support\Facades\Event;
2021-01-04 16:10:56 +00:00
use Illuminate\Support\Facades\Mail;
use Webkul\Shop\Mail\SubscriptionEmail;
2019-07-01 11:33:36 +00:00
use Webkul\Customer\Repositories\CustomerRepository;
2019-10-09 13:00:18 +00:00
use Webkul\Product\Repositories\ProductReviewRepository;
2021-01-04 16:10:56 +00:00
use Webkul\Core\Repositories\SubscribersListRepository;
class CustomerController extends Controller
{
/**
2019-07-01 11:33:36 +00:00
* Contains route related configuration
*
2019-07-01 11:33:36 +00:00
* @var array
*/
protected $_config;
/**
* CustomerRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Customer\Repositories\CustomerRepository
2020-01-14 09:49:22 +00:00
*/
2019-07-01 11:33:36 +00:00
protected $customerRepository;
2019-10-09 13:00:18 +00:00
/**
* ProductReviewRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Customer\Repositories\ProductReviewRepository
2020-01-14 09:49:22 +00:00
*/
2019-10-09 13:00:18 +00:00
protected $productReviewRepository;
2021-01-04 16:10:56 +00:00
/**
* SubscribersListRepository
*
* @var \Webkul\Core\Repositories\SubscribersListRepository
*/
protected $subscriptionRepository;
2018-09-21 13:43:41 +00:00
/**
2019-07-01 11:33:36 +00:00
* Create a new controller instance.
2018-09-21 13:43:41 +00:00
*
2021-01-04 16:10:56 +00:00
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
* @param \Webkul\Product\Repositories\ProductReviewRepository $productReviewRepository
* @param \Webkul\Core\Repositories\SubscribersListRepository $subscriptionRepository
2018-09-21 13:43:41 +00:00
* @return void
2020-01-14 09:49:22 +00:00
*/
2020-03-05 05:34:57 +00:00
public function __construct(
CustomerRepository $customerRepository,
2021-01-04 16:10:56 +00:00
ProductReviewRepository $productReviewRepository,
SubscribersListRepository $subscriptionRepository
2020-03-05 05:34:57 +00:00
)
{
$this->middleware('customer');
$this->_config = request('_config');
2019-07-01 11:33:36 +00:00
$this->customerRepository = $customerRepository;
2019-10-09 13:00:18 +00:00
$this->productReviewRepository = $productReviewRepository;
2021-01-04 16:10:56 +00:00
$this->subscriptionRepository = $subscriptionRepository;
}
/**
2018-10-17 10:33:57 +00:00
* Taking the customer to profile details page
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
*/
public function index()
{
2019-07-01 11:33:36 +00:00
$customer = $this->customerRepository->find(auth()->guard('customer')->user()->id);
return view($this->_config['view'], compact('customer'));
2018-07-27 13:37:56 +00:00
}
/**
2018-10-17 10:33:57 +00:00
* For loading the edit form page.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
*/
public function edit()
{
2019-07-01 11:33:36 +00:00
$customer = $this->customerRepository->find(auth()->guard('customer')->user()->id);
return view($this->_config['view'], compact('customer'));
}
/**
2018-10-17 10:33:57 +00:00
* Edit function for editing customer profile.
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Http\Response
*/
public function update()
{
2020-08-01 09:37:51 +00:00
$isPasswordChanged = false;
$id = auth()->guard('customer')->user()->id;
$this->validate(request(), [
2021-08-20 06:58:58 +00:00
'first_name' => 'required|string',
'last_name' => 'required|string',
2020-01-14 09:49:22 +00:00
'gender' => 'required',
'date_of_birth' => 'date|before:today',
'email' => 'email|unique:customers,email,' . $id,
'password' => 'confirmed|min:6|required_with:oldpassword',
'oldpassword' => 'required_with:password',
2019-12-19 11:17:00 +00:00
'password_confirmation' => 'required_with:password',
]);
$data = collect(request()->input())->except('_token')->toArray();
if (isset ($data['date_of_birth']) && $data['date_of_birth'] == "") {
unset($data['date_of_birth']);
}
2021-01-04 16:10:56 +00:00
$data['subscribed_to_news_letter'] = isset($data['subscribed_to_news_letter']) ? 1 : 0;
if (isset ($data['oldpassword'])) {
if ($data['oldpassword'] != "" || $data['oldpassword'] != null) {
if (Hash::check($data['oldpassword'], auth()->guard('customer')->user()->password)) {
2020-08-01 09:37:51 +00:00
$isPasswordChanged = true;
2021-01-04 16:10:56 +00:00
$data['password'] = bcrypt($data['password']);
} else {
session()->flash('warning', trans('shop::app.customer.account.profile.unmatch'));
return redirect()->back();
}
} else {
unset($data['password']);
}
}
Event::dispatch('customer.update.before');
if ($customer = $this->customerRepository->update($data, $id)) {
2020-08-01 09:37:51 +00:00
if ($isPasswordChanged) {
Event::dispatch('user.admin.update-password', $customer);
}
Event::dispatch('customer.update.after', $customer);
2021-01-04 16:10:56 +00:00
if ($data['subscribed_to_news_letter']) {
$subscription = $this->subscriptionRepository->findOneWhere(['email' => $data['email']]);
2021-08-20 06:58:58 +00:00
2021-01-04 16:10:56 +00:00
if ($subscription) {
$this->subscriptionRepository->update([
'customer_id' => $customer->id,
'is_subscribed' => 1,
], $subscription->id);
} else {
$this->subscriptionRepository->create([
'email' => $data['email'],
'customer_id' => $customer->id,
'channel_id' => core()->getCurrentChannel()->id,
'is_subscribed' => 1,
'token' => $token = uniqid(),
]);
2021-08-20 06:58:58 +00:00
2021-01-04 16:10:56 +00:00
try {
Mail::queue(new SubscriptionEmail([
'email' => $data['email'],
'token' => $token,
]));
} catch (\Exception $e) { }
}
} else {
$subscription = $this->subscriptionRepository->findOneWhere(['email' => $data['email']]);
if ($subscription) {
$this->subscriptionRepository->update([
'customer_id' => $customer->id,
'is_subscribed' => 0,
], $subscription->id);
}
}
Session()->flash('success', trans('shop::app.customer.account.profile.edit-success'));
return redirect()->route($this->_config['redirect']);
} else {
Session()->flash('success', trans('shop::app.customer.account.profile.edit-fail'));
return redirect()->back($this->_config['redirect']);
}
}
2019-09-12 08:36:38 +00:00
/**
* Remove the specified resource from storage.
*
2020-03-05 05:34:57 +00:00
* @param int $id
2019-09-12 08:36:38 +00:00
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$id = auth()->guard('customer')->user()->id;
$data = request()->all();
2019-10-18 14:13:03 +00:00
$customerRepository = $this->customerRepository->findorFail($id);
2019-09-12 08:36:38 +00:00
try {
2019-12-19 11:17:00 +00:00
if (Hash::check($data['password'], $customerRepository->password)) {
$orders = $customerRepository->all_orders->whereIn('status', ['pending', 'processing'])->first();
2019-09-12 08:36:38 +00:00
2019-12-19 11:17:00 +00:00
if ($orders) {
2020-08-04 10:21:22 +00:00
session()->flash('error', trans('admin::app.response.order-pending', ['name' => 'Customer']));
2019-12-19 11:17:00 +00:00
return redirect()->route($this->_config['redirect']);
} else {
$this->customerRepository->delete($id);
2019-12-19 11:17:00 +00:00
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Customer']));
return redirect()->route('customer.session.index');
}
} else {
session()->flash('error', trans('shop::app.customer.account.address.delete.wrong-password'));
2019-09-12 08:36:38 +00:00
2019-12-19 11:17:00 +00:00
return redirect()->back();
}
2020-01-14 09:49:22 +00:00
} catch (\Exception $e) {
2019-10-18 14:13:03 +00:00
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Customer']));
2019-09-12 08:36:38 +00:00
2019-10-18 14:13:03 +00:00
return redirect()->route($this->_config['redirect']);
2019-09-12 08:36:38 +00:00
}
}
2018-10-17 10:33:57 +00:00
/**
* Load the view for the customer account panel, showing approved reviews.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-10-17 10:33:57 +00:00
*/
public function reviews()
{
2019-10-09 13:00:18 +00:00
$reviews = $this->productReviewRepository->getCustomerReview();
2018-09-21 13:43:41 +00:00
return view($this->_config['view'], compact('reviews'));
}
}