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

199 lines
5.6 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Admin\Http\Controllers\Customer;
use Webkul\Customer\Rules\VatIdRule;
2021-05-19 11:50:28 +00:00
use Webkul\Admin\DataGrids\AddressDataGrid;
use Webkul\Admin\Http\Controllers\Controller;
2020-03-05 13:37:08 +00:00
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Customer\Repositories\CustomerAddressRepository;
class AddressController extends Controller
{
/**
2021-05-19 11:50:28 +00:00
* Contains route related configuration.
*
* @var array
*/
protected $_config;
2020-01-14 09:49:22 +00:00
/**
2021-05-19 11:50:28 +00:00
* CustomerRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Customer\Repositories\CustomerRepository
2020-01-14 09:49:22 +00:00
*/
2020-03-05 13:37:08 +00:00
protected $customerRepository;
2020-01-14 09:49:22 +00:00
/**
2021-05-19 11:50:28 +00:00
* CustomerAddressRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Customer\Repositories\CustomerAddressRepository
2020-01-14 09:49:22 +00:00
*/
2020-03-05 13:37:08 +00:00
protected $customerAddressRepository;
2020-01-14 09:49:22 +00:00
/**
* Create a new controller instance.
*
2020-03-05 13:37:08 +00:00
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
* @param \Webkul\Customer\Repositories\CustomerAddressRepository $customerAddressRepository
* @return void
*/
public function __construct(
2020-03-05 13:37:08 +00:00
CustomerRepository $customerRepository,
CustomerAddressRepository $customerAddressRepository
2021-05-19 11:50:28 +00:00
) {
2020-03-05 13:37:08 +00:00
$this->customerRepository = $customerRepository;
2020-03-05 13:37:08 +00:00
$this->customerAddressRepository = $customerAddressRepository;
$this->_config = request('_config');
}
/**
2021-05-19 11:50:28 +00:00
* Fetch address by customer id.
*
2020-03-05 13:37:08 +00:00
* @param int $id
* @return \Illuminate\View\View
*/
public function index($id)
2020-01-14 09:49:22 +00:00
{
2020-03-05 13:37:08 +00:00
$customer = $this->customerRepository->find($id);
2021-05-19 11:50:28 +00:00
if (request()->ajax()) {
return app(AddressDataGrid::class)->toJson();
}
return view($this->_config['view'], compact('customer'));
}
/**
* Show the form for creating a new resource.
*
2020-03-05 13:37:08 +00:00
* @param int $id
* @return \Illuminate\View\View
*/
public function create($id)
{
2020-03-05 13:37:08 +00:00
$customer = $this->customerRepository->find($id);
return view($this->_config['view'], compact('customer'));
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store()
{
2020-01-14 09:49:22 +00:00
request()->merge([
'address1' => implode(PHP_EOL, array_filter(request()->input('address1'))),
2020-01-14 09:49:22 +00:00
]);
$data = collect(request()->input())->except('_token')->toArray();
$this->validate(request(), [
'company_name' => 'string',
'address1' => 'string|required',
'country' => 'string|required',
'state' => 'string|required',
'city' => 'string|required',
'postcode' => 'required',
'phone' => 'required',
'vat_id' => new VatIdRule(),
]);
2020-03-05 13:37:08 +00:00
if ($this->customerAddressRepository->create($data)) {
session()->flash('success', trans('admin::app.customers.addresses.success-create'));
2021-05-04 03:00:36 +00:00
return redirect()->route('admin.customer.edit', ['id' => $data['customer_id']]);
} else {
session()->flash('success', trans('admin::app.customers.addresses.error-create'));
return redirect()->back();
}
}
/**
* Display a listing of the resource.
*
2020-03-05 13:37:08 +00:00
* @param int $id
* @return \Illuminate\View\View
*/
public function edit($id)
{
2020-03-05 13:37:08 +00:00
$address = $this->customerAddressRepository->find($id);
return view($this->_config['view'], compact('address'));
}
/**
2021-05-19 11:50:28 +00:00
* Edit's the pre made resource of customer called address.
*
2020-03-05 13:37:08 +00:00
* @param int $id
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Http\Response
*/
public function update($id)
{
request()->merge(['address1' => implode(PHP_EOL, array_filter(request()->input('address1')))]);
$this->validate(request(), [
'company_name' => 'string',
'address1' => 'string|required',
'country' => 'string|required',
'state' => 'string|required',
'city' => 'string|required',
'postcode' => 'required',
'phone' => 'required',
'vat_id' => new VatIdRule(),
]);
$data = collect(request()->input())->except('_token')->toArray();
2020-03-05 13:37:08 +00:00
$address = $this->customerAddressRepository->find($id);
2020-01-14 09:49:22 +00:00
if ($address) {
2020-03-05 13:37:08 +00:00
$this->customerAddressRepository->update($data, $id);
session()->flash('success', trans('admin::app.customers.addresses.success-update'));
return redirect()->route('admin.customer.addresses.index', ['id' => $address->customer_id]);
}
return redirect()->route($this->_config['redirect']);
}
/**
* Remove the specified resource from storage.
*
2020-03-05 13:37:08 +00:00
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2020-03-05 13:37:08 +00:00
$this->customerAddressRepository->delete($id);
session()->flash('success', trans('admin::app.customers.addresses.success-delete'));
return redirect()->route($this->_config['redirect']);
}
/**
2021-05-19 11:50:28 +00:00
* Mass delete the customer's addresses.
*
2020-03-05 13:37:08 +00:00
* @param int $id
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Http\Response
*/
public function massDestroy($id)
{
$addressIds = explode(',', request()->input('indexes'));
foreach ($addressIds as $addressId) {
2020-03-05 13:37:08 +00:00
$this->customerAddressRepository->delete($addressId);
}
session()->flash('success', trans('admin::app.customers.addresses.success-mass-delete'));
return redirect()->route($this->_config['redirect'], ['id' => $id]);
}
2021-05-19 11:50:28 +00:00
}