[04/09/2024] Installation du module ForcePhone pour rendre le téléphone obligatoire à la création du compte.

This commit is contained in:
2024-09-04 21:46:00 +02:00
parent 38fbe0533e
commit 5a79db3015
22 changed files with 1063 additions and 0 deletions

View 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
{
}

View File

@@ -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)
);
}
}
}

View 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
{
}

View File

@@ -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
)
);
}
}
}
}