![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mcoil.corals.io/app/Http/Controllers/Front/ |
<?php namespace App\Http\Controllers\Front; use App\Http\Controllers\Controller; use App\Shop\Addresses\Requests\CreateAddressRequest; use App\Shop\Addresses\Requests\UpdateAddressRequest; use App\Shop\Addresses\Repositories\AddressRepository; use App\Shop\Cities\Repositories\Interfaces\CityRepositoryInterface; use App\Shop\Addresses\Repositories\Interfaces\AddressRepositoryInterface; use App\Shop\Countries\Repositories\Interfaces\CountryRepositoryInterface; use App\Shop\Provinces\Repositories\Interfaces\ProvinceRepositoryInterface; use App\Shop\Addresses\Address; use App\Shop\Counties\County; use App\Shop\Towns\Town; use Illuminate\Http\Request; use Validator; use Illuminate\Support\Facades\DB; class CustomerAddressController extends Controller { /** * @var AddressRepositoryInterface */ private $addressRepo; /** * @var CountryRepositoryInterface */ private $countryRepo; /** * @var CityRepositoryInterface */ private $cityRepo; /** * @var ProvinceRepositoryInterface */ private $provinceRepo; /** * @param AddressRepositoryInterface $addressRepository * @param CountryRepositoryInterface $countryRepository * @param CityRepositoryInterface $cityRepository * @param ProvinceRepositoryInterface $provinceRepository */ public function __construct( AddressRepositoryInterface $addressRepository, CountryRepositoryInterface $countryRepository, CityRepositoryInterface $cityRepository, ProvinceRepositoryInterface $provinceRepository ) { $this->addressRepo = $addressRepository; $this->countryRepo = $countryRepository; $this->provinceRepo = $provinceRepository; $this->cityRepo = $cityRepository; } /** * @return \Illuminate\Http\RedirectResponse */ public function index() { return redirect()->route('accounts', ['tab' => 'address']); } /** * @param $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function create() { $customer = auth()->user(); $county_list = County::orderBy('name', 'asc')->get(); return view('front.customers.addresses.create', [ 'customer' => $customer, 'countries' => $this->countryRepo->listCountries(), 'cities' => $this->cityRepo->listCities(), 'provinces' => $this->provinceRepo->listProvinces(), 'county_list' => $county_list ]); } /** * @param $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * Used from checkout page to create a new address using ajax */ public function createAddress(Request $request) { $userId = auth()->user()->id; // Server Side Validation $validation = Validator::make( array( 'address_1' => $request->address_1, 'county' => $request->county, 'town' => $request->town, 'zip' => $request->zip, 'phone' => $request->phone, ), array( 'address_1' => array('required'), 'county' => array('required'), 'town' => array('required'), 'zip' => array('required'), 'phone' => array('required', 'min:7') ) ); $response = array(); if ( $validation->fails() ) { $error = $validation->errors()->first(); if( isset( $error ) && !empty( $error ) ) { $response = array( 'status' => 2, 'msg' => $error, 'data' => null ); } } else { $address_obj = new Address(); $address_obj->alias = ''; $address_obj->customer_id = $userId; $address_obj->country_id = 1; $address_obj->address_1 = $request->address_1; $address_obj->address_2 = $request->address_2; $address_obj->county = $request->county; $address_obj->town = $request->town; $address_obj->zip = $request->zip; $address_obj->phone = $request->phone; $address_obj->status = 1; $address_obj->billing_type_address = 0; if( $address_obj->save() ) { $response = array( 'status' => 1, 'msg' => 'Address created successfully', 'data' => array( 'id' => $address_obj->id, 'country_id'=> $address_obj->country_id, 'address_1' => $address_obj->address_1, 'address_2' => $address_obj->address_2, 'county' => $address_obj->county, 'town' => $address_obj->town, 'zip' => $address_obj->zip, 'phone' => $address_obj->phone, ) ); } else { $response = array( 'status' => 3, 'msg' => 'Some error in creating new address', 'data' => null ); } } return response()->json($response); } /** * @param $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * Used from checkout page to create a new billing address using ajax */ public function createBillingAddress(Request $request) { $userId = auth()->user()->id; // Server Side Validation $validation = Validator::make( array( 'address_1' => $request->address_1, 'county' => $request->county, 'town' => $request->town, 'zip' => $request->zip, 'phone' => $request->phone, ), array( 'address_1' => array('required'), 'county' => array('required'), 'town' => array('required'), 'zip' => array('required'), 'phone' => array('required', 'min:7') ) ); $response = array(); if ( $validation->fails() ) { $error = $validation->errors()->first(); if( isset( $error ) && !empty( $error ) ) { $response = array( 'status' => 2, 'msg' => $error, 'data' => null ); } } else { $address_obj = new Address(); $address_obj->alias = ''; $address_obj->customer_id = $userId; $address_obj->country_id = 1; $address_obj->address_1 = $request->address_1; $address_obj->address_2 = $request->address_2; $address_obj->county = $request->county; $address_obj->town = $request->town; $address_obj->zip = $request->zip; $address_obj->phone = $request->phone; $address_obj->status = 1; $address_obj->billing_type_address = 1; if( $address_obj->save() ) { // Save the number in customers table also DB::table('customers')->where(['id' => $userId])->update(['phone' => $request->phone]); $response = array( 'status' => 1, 'msg' => 'Billing address saved successfully.' ); } else { $response = array( 'status' => 3, 'msg' => 'Some error in creating new address.', 'data' => null ); } } return response()->json($response); } public function fetch_town(Request $request) { $towns = Town::where('county_id',$request->county_id)->orderBy('name', 'asc')->get(); $output="<option value=''>Select Town</option>"; foreach($towns as $town) { $output .="<option value='".$town->id."'>".$town->name."</option>"; } echo json_encode($output); } /** * @param CreateAddressRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(CreateAddressRequest $request) { $request['customer_id'] = auth()->user()->id; $address = Address::where(['customer_id' => $request['customer_id'], 'billing_type_address' => 1])->first(); if( $address ) { $address_obj = new Address(); $address_obj->alias = $request->alias; $address_obj->customer_id = $request->customer_id; $address_obj->country_id = 1; $address_obj->address_1 = $request->address_1; $address_obj->address_2 = $request->address_2; $address_obj->county = $request->county; $address_obj->town = $request->town; $address_obj->zip = $request->zip; $address_obj->phone = $request->phone; $address_obj->status = 1; $address_obj->save(); } else { $address_obj = new Address(); $address_obj->alias = $request->alias; $address_obj->customer_id = $request->customer_id; $address_obj->country_id = 1; $address_obj->address_1 = $request->address_1; $address_obj->address_2 = $request->address_2; $address_obj->county = $request->county; $address_obj->town = $request->town; $address_obj->zip = $request->zip; $address_obj->phone = $request->phone; $address_obj->status = 1; $address_obj->billing_type_address = 1; // extra added $address_obj->save(); } return redirect()->route('accounts', ['tab' => 'address'])->with('message', 'Address created successfully.'); } /** * @param $addressId * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function edit($customerId, $addressId) { $countries = $this->countryRepo->listCountries(); $address = $this->addressRepo->findCustomerAddressById($addressId, auth()->user()); return view('front.customers.addresses.edit', [ 'customer' => auth()->user(), 'address' => $address, 'countries' => $countries, 'cities' => $this->cityRepo->listCities(), 'provinces' => $this->provinceRepo->listProvinces() ]); } /** * @param UpdateAddressRequest $request * @param $addressId * * @return \Illuminate\Http\RedirectResponse */ public function update(UpdateAddressRequest $request, $customerId, $addressId) { $request['customer_id'] = auth()->user()->id; $address_obj = Address::find($addressId); $address_obj->alias = $request->alias; $address_obj->customer_id = $request->customer_id; $address_obj->country_id = 1; $address_obj->address_1 = $request->address_1; $address_obj->address_2 = $request->address_2; $address_obj->county = $request->county; $address_obj->town = $request->town; $address_obj->zip = $request->zip; $address_obj->phone = $request->phone; $address_obj->status = 1; $address_obj->save(); return redirect()->route('accounts', ['tab' => 'address'])->with('message', 'Address updated successfully.'); } /** * @param $addressId * * @return \Illuminate\Http\RedirectResponse * @throws \Exception */ public function destroy($customerId, $addressId) { $address = $this->addressRepo->findCustomerAddressById($addressId, auth()->user()); if ($address->orders()->exists()) { $address->status=0; $address->save(); } else { $address->delete(); } return redirect()->route('accounts', ['tab' => 'address'])->with('message', 'Address deleted successfully.'); } /** * @param $addressId * * @return array * @throws \Exception */ public function fetchAddressDetails(Request $request) { $addressId = $request->get('addressId', ''); $response = array(); if ( $addressId != '' ) { $address = Address::find($addressId); if ( $address ) { $response = array( 'status' => 1, 'msg' => 'Success', 'data' => array( 'address_1' => $address->address_1, 'address_2' => $address->address_2, 'town' => $address->town, 'county' => $address->county, 'zip' => $address->zip, 'phone' => $address->phone ) ); } else { $response = array( 'status' => 2, 'msg' => 'Invalid address id', 'data' => null ); } } else { $response = array( 'status' => 3, 'msg' => 'Invalid address id', 'data' => null ); } return response()->json($response); } /** * To update the existing address * * @return array * @throws \Exception */ public function updateAddress(Request $request) { $addressId = $request->get('address_id'); $address_1 = $request->get('address_1'); $address_2 = $request->get('address_2'); $town = $request->get('town'); $county = $request->get('county'); $zip = $request->get('zip'); $phone = $request->get('phone'); // Server Side Validation $validation = Validator::make( array( 'address_1' => $address_1, 'county' => $county, 'town' => $town, 'zip' => $zip, 'phone' => $phone, ), array( 'address_1' => array('required'), 'county' => array('required'), 'town' => array('required'), 'zip' => array('required'), 'phone' => array('required', 'min:7') ) ); $response = array(); if ( $validation->fails() ) { $error = $validation->errors()->first(); if ( isset( $error ) && !empty( $error ) ) { $response = array( 'status' => 2, 'msg' => $error ); } } else { $address = Address::find($addressId); $address->address_1 = $address_1; $address->address_2 = $address_2; $address->town = $town; $address->county = $county; $address->zip = $zip; $address->phone = $phone; if( $address->save() ) { // If the address is of billing address type then update the phone in customers table as well if( $address->billing_type_address == '1' ) { DB::table('customers')->where(['id' => $address->customer_id])->update(['phone' => $phone]); } $response = array( 'status' => 1, 'msg' => 'Address updated successfully' ); } else { $response = array( 'status' => 3, 'msg' => 'Some error in address update' ); } } return response()->json($response); } }