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

203 lines
5.3 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Customer\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Customer\Repositories\CustomerAddressRepository;
use Auth;
/**
* Customer controlller for the customer basically for the tasks of customers which will
* be done after customer authenticastion.
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AddressController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
protected $_config;
protected $customer;
protected $address;
public function __construct(
CustomerRepository $customer,
CustomerAddressRepository $address
)
{
$this->middleware('customer');
$this->_config = request('_config');
$this->customer = auth()->guard('customer')->user();
$this->address = $address;
}
/**
2018-10-17 10:33:57 +00:00
* Address Route index page
*
* @return view
*/
public function index()
{
return view($this->_config['view'])->with('addresses', $this->customer->addresses);
}
/**
2018-10-17 10:33:57 +00:00
* Show the address create form
*
* @return view
*/
public function create()
{
return view($this->_config['view']);
}
/**
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()
{
2019-03-12 06:29:00 +00:00
request()->merge(['address1' => implode(PHP_EOL, array_filter(request()->input('address1')))]);
$data = collect(request()->input())->except('_token')->toArray();
$this->validate(request(), [
'address1' => 'string|required',
'country' => 'string|required',
'state' => 'string|required',
'city' => 'string|required',
2018-10-10 10:41:33 +00:00
'postcode' => 'required',
'phone' => 'required'
]);
$cust_id['customer_id'] = $this->customer->id;
$data = array_merge($cust_id, $data);
2019-01-15 11:54:41 +00:00
if ($this->customer->addresses->count() == 0) {
$data['default_address'] = 1;
}
if ($this->address->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']);
} else {
2019-01-16 08:38:39 +00:00
session()->flash('error', trans('shop::app.customer.account.address.create.error'));
return redirect()->back();
}
}
/**
* For editing the existing addresses of current logged in customer
*
2018-10-17 10:33:57 +00:00
* @return view
*/
public function edit($id)
{
2019-04-09 08:35:57 +00:00
$address = $this->address->findOneWhere([
'id' => $id,
'customer_id' => auth()->guard('customer')->user()->id
]);
2019-04-09 08:35:57 +00:00
if (! $address)
abort(404);
2019-04-09 08:35:57 +00:00
return view($this->_config['view'], compact('address'));
}
2018-10-17 10:33:57 +00:00
/**
* Edit's the premade resource of customer called
2018-10-17 10:33:57 +00:00
* Address.
*
* @return redirect
*/
public function update($id)
{
2019-03-12 06:29:00 +00:00
request()->merge(['address1' => implode(PHP_EOL, array_filter(request()->input('address1')))]);
$this->validate(request(), [
'address1' => 'string|required',
'country' => 'string|required',
'state' => 'string|required',
'city' => 'string|required',
'postcode' => 'required',
'phone' => 'required'
]);
$data = collect(request()->input())->except('_token')->toArray();
$addresses = $this->customer->addresses;
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'));
$this->address->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');
}
/**
* To change the default address or make the default address, by default when first address is created will be the default address
*
* @return Response
*/
public function makeDefault($id)
{
2019-01-15 11:54:41 +00:00
if ($default = $this->customer->default_address) {
2018-10-30 12:46:40 +00:00
$this->address->find($default->id)->update(['default_address' => 0]);
}
2019-01-15 11:54:41 +00:00
if ($address = $this->address->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
*
* @param integer $id
*
* @return response mixed
*/
public function destroy($id)
{
2019-04-09 08:35:57 +00:00
$address = $this->address->findOneWhere([
'id' => $id,
'customer_id' => auth()->guard('customer')->user()->id
]);
2019-04-09 08:35:57 +00:00
if (! $address)
abort(404);
2019-04-09 08:35:57 +00:00
$this->address->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');
}
}