205 lines
7.2 KiB
PHP
205 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\PompoBundle\View;
|
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
|
use External\PompoBundle\DRS\Util\DRSApi;
|
|
use KupShop\KupShopBundle\Exception\RedirectException;
|
|
use KupShop\KupShopBundle\Util\StringUtil;
|
|
use Query\Operator;
|
|
|
|
class UserView extends \KupShop\UserBundle\View\UserView
|
|
{
|
|
/** @required */
|
|
public DRSApi $api;
|
|
|
|
public function getBodyVariables()
|
|
{
|
|
$vars = parent::getBodyVariables();
|
|
|
|
if ($this->newUser() && $this->request->get('customer')) {
|
|
foreach ($this->getDRSCustomerData() as $key => $value) {
|
|
$vars['input'][$key] = ['value' => $value];
|
|
}
|
|
}
|
|
|
|
return $vars;
|
|
}
|
|
|
|
protected function handleSubmit(): void
|
|
{
|
|
$customerId = $this->request->get('customerId');
|
|
// Byl odeslan formular s cislem zakaznika a cislem karty
|
|
if ($this->request->get('Submit') === 'login-using-customer-id') {
|
|
$cardCode = $this->request->get('customerCardCode');
|
|
|
|
// Obe pole musi byt vyplneny
|
|
if (empty($customerId) || empty($cardCode)) {
|
|
$this->returnError(
|
|
translate('regFieldsEmpty', 'pompo')
|
|
);
|
|
}
|
|
|
|
// Najdu zakaznika v DRSu
|
|
try {
|
|
$customer = $this->api->getUserById((int) $customerId);
|
|
} catch (\Throwable $e) {
|
|
$customer = null;
|
|
}
|
|
|
|
if (!$customer) {
|
|
$this->returnError(
|
|
translate('regCustomerNotFound', 'pompo')
|
|
);
|
|
}
|
|
|
|
// Kontroluju, ze zakaznik uz neni registrovany na shopu
|
|
if (!$this->isRegistrationAllowed((int) $customerId)) {
|
|
$this->returnError(
|
|
translate('regRegisteredAlready', 'pompo')
|
|
);
|
|
}
|
|
|
|
$cardFound = false;
|
|
// Kontroluju, ze existuje karta, kterou zadal
|
|
foreach ($customer['customercard'] ?? [] as $card) {
|
|
if (trim($card['@attributes']['number']) == trim($cardCode)) {
|
|
$cardFound = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Pokud karta neexistuje, tak vyhazuju chybu
|
|
if (!$cardFound) {
|
|
$this->returnError(
|
|
translate('regCustomerNotFound', 'pompo')
|
|
);
|
|
}
|
|
|
|
$this->addSuccessMessage(
|
|
translate('regCustomerFound', 'pompo')
|
|
);
|
|
|
|
// Pokud je vse OK, tak redirectuju na registraci s GET parametrem customerId - diky tomu prednactu formular podle DRSu
|
|
throw new RedirectException(
|
|
path('register', ['customer' => base64_encode((string) $customerId)]).'#user-register-form'
|
|
);
|
|
}
|
|
|
|
if ($customerId = $this->getRegisteredCustomerId()) {
|
|
$email = getVal('email');
|
|
try {
|
|
// musim udelat update na email uzivatele, aby pak zafungovalo naparovani na uz existujici ucet, ktery se vytvoril z DRSu
|
|
if (!empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
sqlQueryBuilder()
|
|
->update('users', 'u')
|
|
->join('u', 'drs_users', 'du', 'du.id_user = u.id')
|
|
->set('u.email', ':email')
|
|
->set('u.figure', ':figure')
|
|
->where('du.id_drs = :customerId AND u.passw = ""')
|
|
->addParameters(
|
|
[
|
|
'email' => $email,
|
|
'figure' => 'N',
|
|
'customerId' => $customerId,
|
|
]
|
|
)
|
|
->execute();
|
|
}
|
|
|
|
// Pokud je volan handleSubmit a mam cislo uzivatele z DRSu, tak nasetuju custom data, ktere reknou, ze se po registraci maji
|
|
// sesynchronizovat karty uzivatele
|
|
$this->user->setCustomData('forceUserSynchronization', $customerId);
|
|
} catch (UniqueConstraintViolationException $e) {
|
|
}
|
|
}
|
|
|
|
// handle klasicke registrace
|
|
parent::handleSubmit();
|
|
}
|
|
|
|
public function getDRSCustomerData(): array
|
|
{
|
|
// Nacteni DRS dat do registracniho formulare
|
|
if ($customerId = $this->getRegisteredCustomerId()) {
|
|
// zkontroluju, ze uzivatel uz neni registrovany a timpadem se muze registrovat pres DRS
|
|
if (!$this->isRegistrationAllowed($customerId)) {
|
|
$this->returnError(translate('regRegisteredAlready', 'pompo'));
|
|
}
|
|
|
|
if ($customer = $this->api->getUserById($customerId)) {
|
|
if (StringUtil::startsWith($customer['@attributes']['firstName'] ?? '', 'DEL_')) {
|
|
$this->returnError(translate('registrationInvalidLink', 'pompo'));
|
|
}
|
|
|
|
$email = $customer['@attributes']['email'] ?? '';
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$email = '';
|
|
}
|
|
|
|
$result = [
|
|
'email' => $email,
|
|
'name' => $customer['@attributes']['firstName'] ?? '',
|
|
'surname' => $customer['@attributes']['lastName'] ?? '',
|
|
];
|
|
|
|
foreach ($customer['address'] ?? [] as $address) {
|
|
$prefix = '';
|
|
if ($address['@attributes']['addressType'] != 1) {
|
|
continue;
|
|
}
|
|
|
|
$result[$prefix.'name'] = $address['@attributes']['firstName'] ?? '';
|
|
$result[$prefix.'surname'] = $address['@attributes']['lastName'] ?? '';
|
|
$result[$prefix.'city'] = $address['@attributes']['city'] ?? '';
|
|
$result[$prefix.'street'] = $address['@attributes']['street'] ?? '';
|
|
$result[$prefix.'zip'] = $address['@attributes']['zipCode'] ?? '';
|
|
$result[$prefix.'country'] = $address['@attributes']['country'] ?? 'CZ';
|
|
$result[$prefix.'firm'] = $address['@attributes']['company'] ?? '';
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
private function isRegistrationAllowed(int $customerId): bool
|
|
{
|
|
$user = sqlQueryBuilder()
|
|
->select('u.id, u.passw')
|
|
->from('users', 'u')
|
|
->join('u', 'drs_users', 'du', 'du.id_user = u.id')
|
|
->andWhere(Operator::equals(['du.id_drs' => $customerId]))
|
|
->execute()->fetchAssociative();
|
|
|
|
if ($user && !empty($user['passw'])) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private function getRegisteredCustomerId(): ?int
|
|
{
|
|
if ($customerId = $this->request->get('customer')) {
|
|
// cislo zakaznika je po submitu v GET datech, ale je base encodnuty
|
|
return (int) base64_decode($customerId);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function returnError(string $message): void
|
|
{
|
|
$this->addErrorMessage($message);
|
|
|
|
throw new RedirectException(
|
|
path('register')
|
|
);
|
|
}
|
|
}
|