[04/09/2024] Installation du module ForcePhone pour rendre le téléphone obligatoire à la création du compte.
This commit is contained in:
19
local/modules/ForcePhone/Constraints/AtLeastOnePhone.php
Normal file
19
local/modules/ForcePhone/Constraints/AtLeastOnePhone.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?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\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
class AtLeastOnePhone extends Constraint
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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\Constraints;
|
||||
|
||||
use ForcePhone\ForcePhone;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
|
||||
class AtLeastOnePhoneValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* Checks if at least one phone number is provided
|
||||
*
|
||||
* @param mixed $value The value that should be validated
|
||||
* @param Constraint $constraint The constraint for the validation
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
$data = $this->context->getRoot()->getData();
|
||||
|
||||
if (empty($data["phone"]) && empty($data["cellphone"])) {
|
||||
$this->context->addViolationAt(
|
||||
"phone",
|
||||
Translator::getInstance()->trans("Please enter at least one phone number.", [], ForcePhone::DOMAIN_NAME)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
local/modules/ForcePhone/Constraints/CheckPhoneFormat.php
Normal file
19
local/modules/ForcePhone/Constraints/CheckPhoneFormat.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?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\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
class CheckPhoneFormat extends Constraint
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?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\Constraints;
|
||||
|
||||
use ForcePhone\ForcePhone;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\CountryQuery;
|
||||
|
||||
class CheckPhoneFormatValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* Checks if phone format correspond on country
|
||||
*
|
||||
* @param mixed $value The value that should be validated
|
||||
* @param Constraint $constraint The constraint for the validation
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
$data = $this->context->getRoot()->getData();
|
||||
|
||||
if (!empty($value)) {
|
||||
try {
|
||||
if (!isset($data['country']) || empty($data['country'])) {
|
||||
throw new \Exception('No country ID for checking phone format');
|
||||
}
|
||||
|
||||
$country = CountryQuery::create()->findOneById($data['country']);
|
||||
|
||||
if (!$country) {
|
||||
throw new \Exception('Country not found for checking phone format');
|
||||
}
|
||||
|
||||
$phoneUtil = PhoneNumberUtil::getInstance();
|
||||
|
||||
$phoneNumberProto = $phoneUtil->parse($value, $country->getIsoalpha2());
|
||||
|
||||
$isValid = $phoneUtil->isValidNumber($phoneNumberProto);
|
||||
} catch (\Exception $exception) {
|
||||
$isValid = false;
|
||||
|
||||
Tlog::getInstance()->warning($exception->getMessage());
|
||||
}
|
||||
|
||||
if (!$isValid) {
|
||||
$this->context->addViolation(
|
||||
Translator::getInstance()->trans(
|
||||
'Please enter a valid phone number',
|
||||
[],
|
||||
ForcePhone::DOMAIN_NAME
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user