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

166 lines
4.6 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Customer\Http\Controllers;
use Hash;
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;
/**
* Customer controlller for the customer basically for the tasks of customers which will be
* done after customer authentication.
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
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
*
2019-07-01 11:33:36 +00:00
* @var Object
*/
2019-07-01 11:33:36 +00:00
protected $customerRepository;
2019-10-09 13:00:18 +00:00
/**
* ProductReviewRepository object
*
* @var array
*/
protected $productReviewRepository;
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
*
2019-07-01 11:33:36 +00:00
* @param \Webkul\Customer\Repositories\CustomerRepository $customer
2019-10-09 13:00:18 +00:00
* @param \Webkul\Product\Repositories\ProductReviewRepository $productReview
2018-09-21 13:43:41 +00:00
* @return void
*/
2019-10-09 13:00:18 +00:00
public function __construct(CustomerRepository $customerRepository, ProductReviewRepository $productReviewRepository)
{
$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;
}
/**
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.
*
2019-07-01 11:33:36 +00:00
* @return response
*/
public function update()
{
$id = auth()->guard('customer')->user()->id;
$this->validate(request(), [
'first_name' => 'string',
'last_name' => 'string',
'gender' => 'required',
'date_of_birth' => 'date|before:today',
'email' => 'email|unique:customers,email,'.$id,
'oldpassword' => 'required_with:password',
'password' => 'confirmed|min:6'
]);
$data = collect(request()->input())->except('_token')->toArray();
2019-07-01 11:33:36 +00:00
if ($data['date_of_birth'] == "")
unset($data['date_of_birth']);
if ($data['oldpassword'] != "" || $data['oldpassword'] != null) {
if(Hash::check($data['oldpassword'], auth()->guard('customer')->user()->password)) {
$data['password'] = bcrypt($data['password']);
} else {
session()->flash('warning', trans('shop::app.customer.account.profile.unmatch'));
return redirect()->back();
}
} else {
unset($data['password']);
}
2019-07-01 11:33:36 +00:00
if ($this->customerRepository->update($data, $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.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$id = auth()->guard('customer')->user()->id;
$customer = $this->customer->findorFail($id);
try {
$this->customer->delete($id);
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Customer']));
return redirect()->route($this->_config['redirect']);
} catch(\Exception $e) {
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Customer']));
return redirect()->route($this->_config['redirect']);
}
}
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'));
}
}