[04/09/2024] Installation du module ForcePhone pour rendre le téléphone obligatoire à la création du compte.
This commit is contained in:
83
local/modules/ForcePhone/Controller/ApiController.php
Normal file
83
local/modules/ForcePhone/Controller/ApiController.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ForcePhone\Controller;
|
||||
|
||||
|
||||
use ForcePhone\ForcePhone;
|
||||
use libphonenumber\PhoneNumberFormat;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\HttpFoundation\JsonResponse;
|
||||
use Thelia\Model\CountryQuery;
|
||||
use Thelia\Model\OrderQuery;
|
||||
|
||||
|
||||
class ApiController extends BaseFrontController
|
||||
{
|
||||
public function checkPhoneOrder($orderId)
|
||||
{
|
||||
$order = OrderQuery::create()->filterById($orderId)->findOne();
|
||||
|
||||
if (null === $order) {
|
||||
return new JsonResponse('Order not found', 400);
|
||||
}
|
||||
|
||||
$phoneUtil = PhoneNumberUtil::getInstance();
|
||||
|
||||
$address = $order->getOrderAddressRelatedByDeliveryOrderAddressId();
|
||||
|
||||
if (!empty($address->getPhone())) {
|
||||
|
||||
$phoneNumberProto = $phoneUtil->parse($address->getPhone(), $address->getCountry()->getIsoalpha2());
|
||||
|
||||
$isValid = $phoneUtil->isValidNumber($phoneNumberProto);
|
||||
|
||||
if (!$isValid) {
|
||||
return new JsonResponse('Wrong phone number', 400);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($address->getCellphone())) {
|
||||
|
||||
$phoneNumberProto = $phoneUtil->parse($address->getCellphone(), $address->getCountry()->getIsoalpha2());
|
||||
|
||||
$isValid = $phoneUtil->isValidNumber($phoneNumberProto);
|
||||
|
||||
if (!$isValid) {
|
||||
return new JsonResponse('Wrong cellphone number', 400);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
ForcePhone::getConfigValue('force_one', false) &&
|
||||
empty($address->getCellphone()) && empty($address->getPhone())
|
||||
) {
|
||||
return new JsonResponse('No phone number found', 400);
|
||||
}
|
||||
|
||||
return new JsonResponse('Success', 200);
|
||||
}
|
||||
|
||||
public function reformatPhoneNumber($phone, $countryId)
|
||||
{
|
||||
$phoneUtil = PhoneNumberUtil::getInstance();
|
||||
$country = CountryQuery::create()->filterById($countryId)->findOne();
|
||||
|
||||
if (!$country){
|
||||
return new JsonResponse('Invalid country id', 400);
|
||||
}
|
||||
|
||||
$phoneNumberProto = $phoneUtil->parse($phone, $country->getIsoalpha2());
|
||||
|
||||
$isValid = $phoneUtil->isValidNumber($phoneNumberProto);
|
||||
|
||||
if (!$isValid) {
|
||||
return new JsonResponse('Invalid phone number', 400);
|
||||
}
|
||||
$phone = $phoneUtil->format($phoneNumberProto, PhoneNumberFormat::INTERNATIONAL);
|
||||
|
||||
return new JsonResponse($phone, 200);
|
||||
}
|
||||
|
||||
}
|
||||
87
local/modules/ForcePhone/Controller/ConfigureController.php
Normal file
87
local/modules/ForcePhone/Controller/ConfigureController.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace ForcePhone\Controller;
|
||||
|
||||
use ForcePhone\ForcePhone;
|
||||
use Thelia\Controller\Admin\BaseAdminController;
|
||||
use Thelia\Core\Security\AccessManager;
|
||||
use Thelia\Core\Security\Resource\AdminResources;
|
||||
use Thelia\Core\Thelia;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Tools\URL;
|
||||
use Thelia\Tools\Version\Version;
|
||||
|
||||
class ConfigureController extends BaseAdminController
|
||||
{
|
||||
public function configure()
|
||||
{
|
||||
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'forcephone', AccessManager::UPDATE)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$configurationForm = $this->createForm('forcephone_configuration');
|
||||
|
||||
$message = null;
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($configurationForm);
|
||||
|
||||
// Get the form field values
|
||||
$data = $form->getData();
|
||||
|
||||
foreach ($data as $name => $value) {
|
||||
if (is_array($value)) {
|
||||
$value = implode(';', $value);
|
||||
}
|
||||
|
||||
ForcePhone::setConfigValue($name, $value);
|
||||
}
|
||||
|
||||
// Log configuration modification
|
||||
$this->adminLogAppend(
|
||||
"forcephone.configuration.message",
|
||||
AccessManager::UPDATE,
|
||||
"ForcePhone configuration updated"
|
||||
);
|
||||
|
||||
// Redirect to the success URL,
|
||||
if ($this->getRequest()->get('save_mode') == 'stay') {
|
||||
// If we have to stay on the same page, redisplay the configuration page/
|
||||
$url = '/admin/module/ForcePhone';
|
||||
} else {
|
||||
// If we have to close the page, go back to the module back-office page.
|
||||
$url = '/admin/modules';
|
||||
}
|
||||
|
||||
return $this->generateRedirect(URL::getInstance()->absoluteUrl($url));
|
||||
} catch (FormValidationException $ex) {
|
||||
$message = $this->createStandardFormValidationErrorMessage($ex);
|
||||
} catch (\Exception $ex) {
|
||||
$message = $ex->getMessage();
|
||||
}
|
||||
|
||||
$this->setupFormErrorContext(
|
||||
$this->getTranslator()->trans("ForcePhone configuration", [], ForcePhone::DOMAIN_NAME),
|
||||
$message,
|
||||
$configurationForm,
|
||||
$ex
|
||||
);
|
||||
|
||||
// Before 2.2, the errored form is not stored in session
|
||||
if (Version::test(Thelia::THELIA_VERSION, '2.2', false, "<")) {
|
||||
return $this->render('module-configure', [ 'module_code' => 'ForcePhone' ]);
|
||||
} else {
|
||||
return $this->generateRedirect(URL::getInstance()->absoluteUrl('/admin/module/ForcePhone'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user