File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Auth/RegisterController.php
<?php
namespace App\KommaApp\Auth;
use App\Http\Controllers\Controller;
use App\KommaApp\EUCountries;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Shop\Notifications\CustomerRegistered;
use App\KommaApp\Shop\Vies\Rules\VatNumberSoft;
use App\KommaApp\Shop\Vies\ViesResult;
use App\KommaApp\Shop\Vies\ViesService;
use App\KommaApp\Users\Models\Role;
use App\KommaApp\Users\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
$page = $this->pageService->getPageByCodeName('register');
$otherLanguageRoutes = $this->languageService->getOtherLanguagesRoutes($page);
$selectableCountries = [];
foreach (EUCountries::DATA as $euCountry)
{
$selectableCountries[$euCountry['iso_code']] = $euCountry['native'];
}
asort($selectableCountries);
$referer = \Request::server('HTTP_REFERER');
if(url($this->links->register->route) !== $referer) {
\Session::put('referer', $referer);
}
return view('site.auth.register', [
'links' => $this->links,
'categories' => $this->categories,
'countries' => $selectableCountries,
'otherLanguages' => $otherLanguageRoutes,
'referer' => \Session::get('referer'),
]);
}
/**
* For ajax requests in register form
*
* @return JsonResponse
*/
public function validateVatNumber()
{
$viesService = \App::make(ViesService::class);
$result = $viesService->validateVatNumber(\Input::get('company_vat_number')) ? true : false;
return new JsonResponse($result, 200);
}
/**
* Create a new (customer) user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$currentLanguage = \App::getLanguage();
$customer = new User();
/** @var ViesService $viesService */
$viesService = \App::make(ViesService::class);
$result = $viesService->validateVatNumber($data['company_vat_number']);
if(is_a($result, ViesResult::class)) {
$customer->company_vat_number_validated = 1;
}
else if(is_int($result))
{
$customer->company_vat_number_validated = 0; //Vies service was offline.
}
$customer->first_name = $data["first_name"];
$customer->last_name = $data["last_name"];
$customer->company_name = $data["company_name"];
$customer->chamber_of_commerce = $data["chamber_of_commerce"];
$customer->company_street = $data["company_street"];
$customer->company_house_number = $data["company_house_number"];
$customer->company_zip_code = $data["company_zip_code"];
$customer->company_city = $data["company_city"];
$customer->company_country = $data["company_country"];
$customer->company_vat_number = $data["company_vat_number"];
$customer->shipping_street = $data["shipping_street"];
$customer->shipping_house_number = $data["shipping_house_number"];
$customer->shipping_zip_code = $data["shipping_zip_code"];
$customer->shipping_city = $data["shipping_city"];
$customer->shipping_country = $data["shipping_country"];
$customer->email = $data["email"];
$customer->telephone = $data["telephone"];
$customer->gender = $data["gender"];
$customer->username = $customer->email;
$customer->role_id = Role::Customer;
$customer->locale = \App::getLanguage()->iso_2;
$customer->save();
$this->notifyAdminsForNewCustomer($customer);
\App::setLanguage($currentLanguage);
return $customer;
}
/**
* Notify admins that a new user has been created
*
* @param $customer
*/
public function notifyAdminsForNewCustomer(User $customer)
{
$language = \App::getLanguage();
User::where('role_id', '=', Role::Admin)->get()->each(function(User $admin) use ($customer) {
$language = Language::where('iso_2', '=', $admin->locale)->first();
\App::setLanguage($language);
$admin->notify((new CustomerRegistered($customer)));
\Log::debug('Notifyed admin with id: '.$admin->id.' that user with id '.$customer->id.' registered');
});
\App::setLanguage($language);
}
/**
* Get the post register / login (e.g. after login) redirect path.
*
* @return string
*/
public function redirectPath()
{
return \App::getLanguage()->iso_2.'/register';
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
if(!$request->has('gender')) {
$request->request->add(['gender' => 'male']);
}
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
if($this->registered($request, $user)) {
return $this->registered($request, $user);
} else {
return $this->showRegistrationForm()->with('registered', true);
}
}
public function validator($data) {
return Validator::make($data, [
'email' => 'required|email|max:255|unique:users',
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'company_name' => 'required|string|max:255',
'chamber_of_commerce' => 'required|string|max:255',
'company_street' => 'required|string|max:255',
'company_house_number' => 'required|string|max:255',
'company_zip_code' => 'required|string|max:255',
'company_city' => 'required|string|max:255',
'company_country' => 'required|string|max:255',
'company_vat_number' => ['required', 'string', new VatNumberSoft],
'shipping_street' => 'required_if:differentShippingAddress, true|string|max:255',
'shipping_house_number' => 'required_if:differentShippingAddress, true|string|max:255',
'shipping_zip_code' => 'required_if:differentShippingAddress, true|string|max:255',
'shipping_city' => 'required_if:differentShippingAddress, true|string|max:255',
'shipping_country' => 'required_if:differentShippingAddress, true|string|max:255',
'telephone' => 'required',
'conditions' => 'required',
]);
}
}