Merge branch '20-update-telephone-type-new-approach' into 'master'

fix: Use `odolbeau/phone-number-bundle` for formatting phone number type fields.

See merge request Chill-Projet/chill-bundles!322
This commit is contained in:
2022-03-02 20:39:52 +00:00
44 changed files with 711 additions and 254 deletions

View File

@@ -21,6 +21,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Iterator;
use libphonenumber\PhoneNumberUtil;
use Nelmio\Alice\Loader\NativeLoader;
use Nelmio\Alice\ObjectSet;
@@ -29,6 +30,13 @@ use function count;
class LoadThirdParty extends Fixture implements DependentFixtureInterface
{
private PhoneNumberUtil $phoneNumberUtil;
public function __construct()
{
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}
public function getDependencies()
{
return [
@@ -66,7 +74,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface
Address::class => [
'address1' => [
'name' => '<fr_FR:company()>',
'telephone' => '<fr_FR:phonenumber()>',
'telephone' => $this->phoneNumberUtil->getExampleNumber('FR'),
'email' => '<email()>',
'comment' => '<fr_FR:realTextBetween(10, 500)>',
],
@@ -116,7 +124,7 @@ class LoadThirdParty extends Fixture implements DependentFixtureInterface
ThirdParty::class => [
'thirdparty{1..75}' => [
'name' => '<fr_FR:company()>',
'telephone' => '<fr_FR:phonenumber()>',
'telephone' => $this->phoneNumberUtil->getExampleNumber('FR'),
'email' => '<email()>',
'comment' => '<fr_FR:realTextBetween(10, 500)>',
'address' => '@address<current()>',

View File

@@ -24,6 +24,7 @@ use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Serializer\Annotation\Groups;
@@ -253,14 +254,11 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
private ?ThirdPartyProfession $profession = null;
/**
* @ORM\Column(name="telephone", type="string", length=64, nullable=true)
* @Assert\Regex("/^([\+{1}])([0-9\s*]{4,20})$/",
* message="Invalid phone number: it should begin with the international prefix starting with ""+"", hold only digits and be smaller than 20 characters. Ex: +33123456789"
* )
* @ORM\Column(name="telephone", type="phone_number", nullable=true)
* @PhonenumberConstraint(type="any")
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $telephone = null;
private ?PhoneNumber $telephone = null;
/**
* @ORM\Column(name="types", type="json", nullable=true)
@@ -502,7 +500,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
/**
* Get telephone.
*/
public function getTelephone(): ?string
public function getTelephone(): ?PhoneNumber
{
return $this->telephone;
}
@@ -821,12 +819,8 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
/**
* Set telephone.
*
* @param string|null $telephone
*
* @return ThirdParty
*/
public function setTelephone($telephone = null)
public function setTelephone(?PhoneNumber $telephone = null): self
{
$this->telephone = $telephone;

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\ThirdPartyBundle\Form;
use Chill\MainBundle\Form\Type\ChillCollectionType;
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Form\Type\PickAddressType;
use Chill\MainBundle\Form\Type\PickCenterType;
@@ -75,7 +76,7 @@ class ThirdPartyType extends AbstractType
->add('name', TextType::class, [
'required' => true,
])
->add('telephone', TextType::class, [
->add('telephone', ChillPhoneNumberType::class, [
'label' => 'Phonenumber',
'required' => false,
])

View File

@@ -110,8 +110,8 @@
}) }}
</li>
<li><i class="fa fa-li fa-phone"></i>
{% if thirdparty.telephone %}
<a href="{{ 'tel:' ~ thirdparty.telephone }}">{{ thirdparty.telephone|chill_format_phonenumber }}</a>
{% if thirdparty.telephone is not null %}
<a href="{{ 'tel:' ~ thirdparty.telephone|phone_number_format('E164') }}">{{ thirdparty.telephone|chill_format_phonenumber }}</a>
{% else %}
<span class="chill-no-data-statement">{{ 'thirdparty.No_phonenumber'|trans }}</span>
{% endif %}
@@ -136,7 +136,7 @@
</li>
<li><i class="fa fa-li fa-phone"></i>
{% if thirdparty.telephone %}
<a href="{{ 'tel:' ~ thirdparty.telephone }}">{{ thirdparty.telephone|chill_format_phonenumber }}</a>
<a href="{{ 'tel:' ~ thirdparty.telephone|phone_number_format('E164') }}">{{ thirdparty.telephone|chill_format_phonenumber }}</a>
{% else %}
<span class="chill-no-data-statement">{{ 'thirdparty.No_phonenumber'|trans }}</span>
{% endif %}

View File

@@ -67,10 +67,10 @@
<dt>{{ 'Phonenumber'|trans }}</dt>
<dd>
{% if thirdParty.telephone == null %}
<span class="chill-no-data-statement">{{ 'No phone given'|trans }}</span>
<span class="chill-no-data-statement">{{ 'thirdparty.No_phonenumber'|trans }}</span>
{% else %}
<a href="{{ 'tel:' ~ thirdParty.telephone }}">
{{ thirdParty.telephone|chill_print_or_message("thirdparty.No_phonenumber") }}
<a href="{{ 'tel:' ~ thirdParty.telephone|phone_number_format('E164') }}">
{{ thirdParty.telephone|chill_format_phonenumber }}
</a>
{% endif %}
</dd>

View File

@@ -62,7 +62,7 @@ class ThirdPartyNormalizer implements NormalizerAwareInterface, NormalizerInterf
}, $thirdParty->getTypesAndCategories()),
'profession' => $this->normalizer->normalize($thirdParty->getProfession(), $format, $context),
'address' => $this->normalizer->normalize($thirdParty->getAddress(), $format, ['address_rendering' => 'short']),
'phonenumber' => $thirdParty->getTelephone(),
'phonenumber' => $this->normalizer->normalize($thirdParty->getTelephone()),
'email' => $thirdParty->getEmail(),
'isChild' => $thirdParty->isChild(),
'parent' => $this->normalizer->normalize($thirdParty->getParent(), $format, $context),

View File

@@ -0,0 +1,70 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\ThirdParty;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use libphonenumber\PhoneNumberUtil;
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
final class Version20220302143821 extends AbstractMigration implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(64)');
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS NULL');
}
public function getDescription(): string
{
return 'Upgrade phonenumber on third parties';
}
public function up(Schema $schema): void
{
$carrier_code = $this->container
->getParameter('chill_main')['phone_helper']['default_carrier_code'];
if (null === $carrier_code) {
throw new RuntimeException('no carrier code');
}
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(35)');
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT');
$this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(35)');
$this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS \'(DC2Type:phone_number)\'');
$this->addSql(
'UPDATE chill_3party.third_party SET ' .
$this->buildMigrationPhonenumberClause($carrier_code, 'telephone')
);
}
private function buildMigrationPhoneNumberClause(string $defaultCarriercode, string $field): string
{
$util = PhoneNumberUtil::getInstance();
$countryCode = $util->getCountryCodeForRegion($defaultCarriercode);
return sprintf('%s=CASE
WHEN %s = \'\' THEN NULL
WHEN LEFT(%s, 1) = \'0\'
THEN \'+%s\' || replace(replace(substr(%s, 2), \'(0)\', \'\'), \' \', \'\')
ELSE replace(replace(%s, \'(0)\', \'\'),\' \', \'\')
END', $field, $field, $field, $countryCode, $field, $field);
}
}