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

191 lines
5.0 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Customer\Http\Controllers;
use Webkul\Customer\Http\Requests\CustomerAddressRequest;
use Webkul\Customer\Repositories\CustomerAddressRepository;
class AddressController extends Controller
{
/**
* Contains route related configuration.
*
2019-07-01 11:33:36 +00:00
* @var array
*/
protected $_config;
2019-07-01 11:33:36 +00:00
/**
* Current customer.
*
* @var \Webkul\Customer\Models\Customer
*/
protected $customer;
2020-03-05 05:34:57 +00:00
/**
* Create a new controller instance.
*
* @param \Webkul\Customer\Repositories\CustomerAddressRepository $customerAddressRepository
* @return void
*/
public function __construct(protected CustomerAddressRepository $customerAddressRepository)
{
$this->_config = request('_config');
}
/**
* Address route index page.
2018-10-17 10:33:57 +00:00
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
*/
public function index()
{
$customer = auth()->guard('customer')->user();
return view($this->_config['view'])->with('addresses', $customer->addresses);
}
/**
* Show the address create form.
2018-10-17 10:33:57 +00:00
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
*/
public function create()
{
return view($this->_config['view'], [
'defaultCountry' => config('app.default_country'),
]);
}
/**
2018-10-17 10:33:57 +00:00
* Create a new address for customer.
*
2018-10-17 10:33:57 +00:00
* @return view
*/
public function store(CustomerAddressRequest $request)
{
$customer = auth()->guard('customer')->user();
2021-12-29 12:22:38 +00:00
$data = $request->all();
$data['customer_id'] = $customer->id;
$data['address1'] = implode(PHP_EOL, array_filter(request()->input('address1')));
if ($customer->addresses->count() == 0) {
$data['default_address'] = 1;
}
2019-07-01 11:33:36 +00:00
if ($this->customerAddressRepository->create($data)) {
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('shop::app.customer.account.address.create.success'));
return redirect()->route($this->_config['redirect']);
}
session()->flash('error', trans('shop::app.customer.account.address.create.error'));
return redirect()->back();
}
/**
* For editing the existing addresses of current logged in customer.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
*/
public function edit($id)
{
$customer = auth()->guard('customer')->user();
2019-07-01 11:33:36 +00:00
$address = $this->customerAddressRepository->findOneWhere([
2020-01-10 13:07:58 +00:00
'id' => $id,
'customer_id' => $customer->id,
2019-04-09 08:35:57 +00:00
]);
if (! $address) {
2019-04-09 08:35:57 +00:00
abort(404);
}
2020-03-05 05:34:57 +00:00
return view($this->_config['view'], array_merge(compact('address'), [
'defaultCountry' => config('app.default_country'),
2020-03-05 05:34:57 +00:00
]));
}
2018-10-17 10:33:57 +00:00
/**
2020-03-05 05:34:57 +00:00
* Edit's the premade resource of customer called Address.
2018-10-17 10:33:57 +00:00
*
2020-03-05 05:34:57 +00:00
* @param int $id
* @return \Illuminate\Http\Response
2018-10-17 10:33:57 +00:00
*/
public function update($id, CustomerAddressRequest $request)
{
$customer = auth()->guard('customer')->user();
2021-12-29 12:22:38 +00:00
$data = $request->all();
$data['address1'] = implode(PHP_EOL, array_filter(request()->input('address1')));
$addresses = $customer->addresses;
2020-01-10 13:07:58 +00:00
foreach ($addresses as $address) {
2019-04-09 08:35:57 +00:00
if ($id == $address->id) {
session()->flash('success', trans('shop::app.customer.account.address.edit.success'));
2019-07-01 11:33:36 +00:00
$this->customerAddressRepository->update($data, $id);
return redirect()->route('customer.address.index');
}
}
2019-03-31 20:02:58 +00:00
session()->flash('warning', trans('shop::app.security-warning'));
return redirect()->route('customer.address.index');
}
/**
2020-03-05 05:34:57 +00:00
* To change the default address or make the default address,
* by default when first address is created will be the default address.
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Http\Response
*/
public function makeDefault($id)
{
$customer = auth()->guard('customer')->user();
if ($default = $customer->default_address) {
2019-07-01 11:33:36 +00:00
$this->customerAddressRepository->find($default->id)->update(['default_address' => 0]);
2020-02-19 13:48:54 +00:00
}
2019-07-01 11:33:36 +00:00
if ($address = $this->customerAddressRepository->find($id)) {
2018-10-30 12:46:40 +00:00
$address->update(['default_address' => 1]);
} else {
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('shop::app.customer.account.address.index.default-delete'));
}
return redirect()->back();
}
/**
* Delete address of the current customer.
*
2020-03-05 05:34:57 +00:00
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$customer = auth()->guard('customer')->user();
2019-07-01 11:33:36 +00:00
$address = $this->customerAddressRepository->findOneWhere([
2020-01-10 13:07:58 +00:00
'id' => $id,
'customer_id' => $customer->id,
2019-04-09 08:35:57 +00:00
]);
2020-02-19 13:48:54 +00:00
if (! $address) {
2019-04-09 08:35:57 +00:00
abort(404);
2020-02-19 13:48:54 +00:00
}
2019-07-01 11:33:36 +00:00
$this->customerAddressRepository->delete($id);
2019-04-09 08:35:57 +00:00
session()->flash('success', trans('shop::app.customer.account.address.delete.success'));
2019-03-31 20:02:58 +00:00
return redirect()->route('customer.address.index');
}
}