Merge branch 'master' of gitlab.com:Chill-Projet/chill-bundles

This commit is contained in:
2021-11-15 13:05:59 +01:00
27 changed files with 359 additions and 165 deletions

View File

@@ -28,14 +28,12 @@ use Chill\PersonBundle\Repository\AccompanyingPeriodACLAwareRepository;
use Symfony\Component\Workflow\Registry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class AccompanyingCourseApiController extends ApiController
final class AccompanyingCourseApiController extends ApiController
{
protected EventDispatcherInterface $eventDispatcher;
protected ValidatorInterface $validator;
private AccompanyingPeriodACLAwareRepository $accompanyingPeriodACLAwareRepository;
private EventDispatcherInterface $eventDispatcher;
private ValidatorInterface $validator;
private Registry $registry;
private ReferralsSuggestionInterface $referralAvailable;
public function __construct(

View File

@@ -47,13 +47,22 @@ class HouseholdApiController extends ApiController
$count = $this->householdRepository->countByAccompanyingPeriodParticipation($person);
$paginator = $this->getPaginatorFactory()->create($count);
if ($count === 0) {
$households = [];
} else {
$households = $this->householdRepository->findByAccompanyingPeriodParticipation($person,
$households = [];
if ($count !== 0) {
$allHouseholds = $this->householdRepository->findByAccompanyingPeriodParticipation($person,
$paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber());
}
$currentHouseholdPerson = $person->getCurrentHousehold();
foreach ($allHouseholds as $h) {
if ($h !== $currentHouseholdPerson) {
array_push($households, $h);
}
}
if (null !== $currentHouseholdPerson) {
$count = $count - 1;
$paginator = $this->getPaginatorFactory()->create($count);
}
}
$collection = new Collection($households, $paginator);
return $this->json($collection, Response::HTTP_OK, [],

View File

@@ -0,0 +1,40 @@
<?php
namespace Chill\PersonBundle\DataFixtures\Helper;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
trait PersonRandomHelper
{
private array $randPersons = [];
private ?int $countPersons = null;
protected function getRandomPerson(EntityManagerInterface $em): Person
{
$fetchBy = 5;
if (null === $this->countPersons) {
$qb = $em->createQueryBuilder();
$this->countPersons = $qb->select('count(p)')
->from(Person::class, 'p')
->getQuery()
->getSingleScalarResult()
;
}
if ([] === $this->randPersons) {
$qb = $em->createQueryBuilder();
$this->randPersons = $qb
->select('p')
->from(Person::class, 'p')
->getQuery()
->setFirstResult(\random_int(0, $this->countPersons - $fetchBy))
->setMaxResults($fetchBy)
->getResult()
;
}
return \array_pop($this->randPersons);
}
}

View File

@@ -262,7 +262,7 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
$manager->persist($accompanyingPeriod);
echo "add person'".$person->__toString()."'\n";
$this->addReference(self::PERSON, $person);
$this->addReference(self::PERSON.$person->getId(), $person);
}
private function getRandomUser(): User

View File

@@ -10,7 +10,26 @@ use Doctrine\Persistence\ObjectManager;
class LoadRelations extends Fixture implements FixtureGroupInterface
{
public const RELATIONS = 'relations';
public const RELATION_KEY = 'relations';
public const RELATIONS = [
['title' => ['fr' => 'Mère'], 'reverseTitle' => ['fr' => 'Fille']],
['title' => ['fr' => 'Mère'], 'reverseTitle' => ['fr' => 'Fils']],
['title' => ['fr' => 'Père'], 'reverseTitle' => ['fr' => 'Fille']],
['title' => ['fr' => 'Père'], 'reverseTitle' => ['fr' => 'Fils']],
['title' => ['fr' => 'Frère'], 'reverseTitle' => ['fr' => 'Frère']],
['title' => ['fr' => 'Soeur'], 'reverseTitle' => ['fr' => 'Soeur']],
['title' => ['fr' => 'Frère'], 'reverseTitle' => ['fr' => 'Soeur']],
['title' => ['fr' => 'Demi-frère'], 'reverseTitle' => ['fr' => 'Demi-frère']],
['title' => ['fr' => 'Demi-soeur'], 'reverseTitle' => ['fr' => 'Demi-soeur']],
['title' => ['fr' => 'Demi-frère'], 'reverseTitle' => ['fr' => 'Demi-soeur']],
['title' => ['fr' => 'Oncle'], 'reverseTitle' => ['fr' => 'Neveu']],
['title' => ['fr' => 'Oncle'], 'reverseTitle' => ['fr' => 'Nièce']],
['title' => ['fr' => 'Tante'], 'reverseTitle' => ['fr' => 'Neveu']],
['title' => ['fr' => 'Tante'], 'reverseTitle' => ['fr' => 'Nièce']],
];
public static function getGroups(): array
{
@@ -19,37 +38,17 @@ class LoadRelations extends Fixture implements FixtureGroupInterface
public function load(ObjectManager $manager)
{
$relations = [
['title' => ['fr' => 'Mère'], 'reverseTitle' => ['fr' => 'Fille']],
['title' => ['fr' => 'Mère'], 'reverseTitle' => ['fr' => 'Fils']],
['title' => ['fr' => 'Père'], 'reverseTitle' => ['fr' => 'Fille']],
['title' => ['fr' => 'Père'], 'reverseTitle' => ['fr' => 'Fils']],
['title' => ['fr' => 'Frère'], 'reverseTitle' => ['fr' => 'Frère']],
['title' => ['fr' => 'Soeur'], 'reverseTitle' => ['fr' => 'Soeur']],
['title' => ['fr' => 'Frère'], 'reverseTitle' => ['fr' => 'Soeur']],
['title' => ['fr' => 'Demi-frère'], 'reverseTitle' => ['fr' => 'Demi-frère']],
['title' => ['fr' => 'Demi-soeur'], 'reverseTitle' => ['fr' => 'Demi-soeur']],
['title' => ['fr' => 'Demi-frère'], 'reverseTitle' => ['fr' => 'Demi-soeur']],
['title' => ['fr' => 'Oncle'], 'reverseTitle' => ['fr' => 'Neveu']],
['title' => ['fr' => 'Oncle'], 'reverseTitle' => ['fr' => 'Nièce']],
['title' => ['fr' => 'Tante'], 'reverseTitle' => ['fr' => 'Neveu']],
['title' => ['fr' => 'Tante'], 'reverseTitle' => ['fr' => 'Nièce']],
];
foreach($relations as $value){
foreach (self::RELATIONS as $key => $value){
print "Creating a new relation type: relation" . $value['title']['fr'] . "reverse relation: " . $value['reverseTitle']['fr'] . "\n";
$relation = new Relation();
$relation->setTitle($value['title'])
->setReverseTitle($value['reverseTitle']);
$manager->persist($relation);
$this->addReference(self::RELATIONS, $relation);
$this->addReference(self::RELATION_KEY.$key, $relation);
}
$manager->flush();
}
}

View File

@@ -3,17 +3,24 @@ declare(strict_types=1);
namespace Chill\PersonBundle\DataFixtures\ORM;
use Chill\MainBundle\DataFixtures\ORM\LoadUsers;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\DataFixtures\Helper\PersonRandomHelper;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Chill\PersonBundle\DataFixtures\ORM\LoadPeople;
use Chill\PersonBundle\DataFixtures\ORM\LoadRelations;
use Chill\PersonBundle\Entity\Relationships\Relationship;
class LoadRelationships extends Fixture implements DependentFixtureInterface
{
use PersonRandomHelper;
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function getDependencies()
{
@@ -21,16 +28,33 @@ class LoadRelationships extends Fixture implements DependentFixtureInterface
LoadPeople::class,
LoadRelations::class
];
}
public function load(ObjectManager $manager)
{
$relationship = new Relationship;
$relationship->setFromPerson($this->getReference(LoadPeople::PERSON));
$relationship->setToPerson($this->getReference(LoadPeople::PERSON));
$relationship->setRelation($this->getReference(LoadRelations::RELATIONS));
$relationship->setReverse((bool)random_int(0, 1));
for ($i = 0; $i < 15; $i++) {
$user = $this->getRandomUser();
$date = new \DateTimeImmutable();
$relationship = (new Relationship())
->setFromPerson($this->getRandomPerson($this->em))
->setToPerson($this->getRandomPerson($this->em))
->setRelation($this->getReference(LoadRelations::RELATION_KEY.
\random_int(0, count(LoadRelations::RELATIONS) - 1)))
->setReverse((bool) random_int(0, 1))
->setCreatedBy($user)
->setUpdatedBy($user)
->setCreatedAt($date)
->setUpdatedAt($date)
;
$manager->persist($relationship);
}
$manager->flush();
}
}
private function getRandomUser(): User
{
$userRef = array_rand(LoadUsers::$refs);
return $this->getReference($userRef);
}
}

View File

@@ -144,7 +144,10 @@ class PersonType extends AbstractType
}
if ($this->config['phonenumber'] === 'visible') {
$builder->add('phonenumber', TelType::class, array('required' => false));
$builder->add('phonenumber', TelType::class, array(
'required' => false,
// 'placeholder' => '+33623124554' //TODO placeholder for phone numbers
));
}
if ($this->config['mobilenumber'] === 'visible') {
@@ -167,7 +170,8 @@ class PersonType extends AbstractType
'delete_empty' => function(PersonPhone $pp = null) {
return NULL === $pp || $pp->isEmpty();
},
'error_bubbling' => false
'error_bubbling' => false,
'empty_collection_explain' => 'No additional phone numbers'
]);
if ($this->config['email'] === 'visible') {

View File

@@ -37,6 +37,6 @@ class RelationRepository implements ObjectRepository
public function getClassName(): string
{
return MaritalStatus::class;
return Relation::class;
}
}

View File

@@ -10,7 +10,6 @@ use Doctrine\Persistence\ObjectRepository;
class RelationshipRepository implements ObjectRepository
{
private EntityRepository $repository;
public function __construct(EntityManagerInterface $em)
@@ -40,9 +39,9 @@ class RelationshipRepository implements ObjectRepository
public function getClassName(): string
{
return MaritalStatus::class;
return Relationship::class;
}
public function findByPerson($personId): array
{
// return all relationships of which person is part? or only where person is the fromPerson?
@@ -56,5 +55,5 @@ class RelationshipRepository implements ObjectRepository
->getResult()
;
}
}

View File

@@ -216,13 +216,23 @@ This view should receive those arguments:
{%- if chill_person.fields.mobilenumber == 'visible' -%}
<dl>
<dt>{{ 'Mobilenumber'|trans }}&nbsp;:</dt>
<dd>{% if person.mobilenumber is not empty %}<a href="tel:{{ person.mobilenumber }}"><pre>{{ person.mobilenumber|chill_format_phonenumber }}</pre></a>{% else %}<span class="chill-no-data-statement">{{ 'No data given'|trans }}{% endif %}</dd>
<dd>{% if person.mobilenumber is not empty %}<a href="tel:{{ person.mobilenumber }}">{{ person.mobilenumber|chill_format_phonenumber }}</a>{% else %}<span class="chill-no-data-statement">{{ 'No data given'|trans }}{% endif %}</dd>
</dl>
{% endif %}
{# TODO
display collection of others phonenumbers
#}
{%- if chill_person.fields.mobilenumber == 'visible' -%}
{% if person.otherPhoneNumbers is not empty %}
<dl>
<dt>{{ 'Others phone numbers'|trans }}&nbsp;:</dt>
{% for el in person.otherPhoneNumbers %}
{% if el.phonenumber is not empty %}
<dd>{% if el.description is not empty %}{{ el.description }}&nbsp;:&nbsp;{% endif %}<a href="tel:{{ el.phonenumber }}">{{ el.phonenumber|chill_format_phonenumber }}</a></dd>
{% endif %}
{% endfor %}
</ul>
</dl>
{% endif %}
{% endif %}
{%- if chill_person.fields.contact_info == 'visible' -%}
<dl>

View File

@@ -2,29 +2,43 @@
namespace Chill\PersonBundle\Search;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\MainBundle\Search\SearchApiQuery;
use Chill\MainBundle\Search\SearchApiInterface;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\Security\Core\Security;
class SearchPersonApiProvider implements SearchApiInterface
{
private PersonRepository $personRepository;
private Security $security;
private AuthorizationHelperInterface $authorizationHelper;
public function __construct(PersonRepository $personRepository)
public function __construct(PersonRepository $personRepository, Security $security, AuthorizationHelperInterface $authorizationHelper)
{
$this->personRepository = $personRepository;
$this->security = $security;
$this->authorizationHelper = $authorizationHelper;
}
public function provideQuery(string $pattern, array $parameters): SearchApiQuery
{
return $this->addAuthorizations($this->buildBaseQuery($pattern, $parameters));
}
public function buildBaseQuery(string $pattern, array $parameters): SearchApiQuery
{
$query = new SearchApiQuery();
$query
->setSelectKey("person")
->setSelectJsonbMetadata("jsonb_build_object('id', person.id)")
->setSelectPertinence("GREATEST(".
"STRICT_WORD_SIMILARITY(LOWER(UNACCENT(?)), person.fullnamecanonical), ".
"(person.fullnamecanonical LIKE '%' || LOWER(UNACCENT(?)) || '%')::int".
")", [ $pattern, $pattern ])
->setSelectPertinence("".
"STRICT_WORD_SIMILARITY(LOWER(UNACCENT(?)), person.fullnamecanonical) + ".
"(person.fullnamecanonical LIKE '%' || LOWER(UNACCENT(?)) || '%')::int + ".
"(EXISTS (SELECT 1 FROM unnest(string_to_array(fullnamecanonical, ' ')) AS t WHERE starts_with(t, UNACCENT(LOWER(?)))))::int"
, [ $pattern, $pattern, $pattern ])
->setFromClause("chill_person_person AS person")
->setWhereClauses("LOWER(UNACCENT(?)) <<% person.fullnamecanonical OR ".
"person.fullnamecanonical LIKE '%' || LOWER(UNACCENT(?)) || '%' ", [ $pattern, $pattern ])
@@ -33,6 +47,28 @@ class SearchPersonApiProvider implements SearchApiInterface
return $query;
}
private function addAuthorizations(SearchApiQuery $query): SearchApiQuery
{
$authorizedCenters = $this->authorizationHelper
->getReachableCenters($this->security->getUser(), PersonVoter::SEE);
if ([] === $authorizedCenters) {
return $query->andWhereClause("FALSE = TRUE", []);
}
return $query
->andWhereClause(
strtr(
"person.center_id IN ({{ center_ids }})",
[
'{{ center_ids }}' => \implode(', ',
\array_fill(0, count($authorizedCenters), '?')),
]
),
\array_map(function(Center $c) {return $c->getId();}, $authorizedCenters)
);
}
public function supportsTypes(string $pattern, array $types, array $parameters): bool
{
return \in_array('person', $types);

View File

@@ -259,7 +259,8 @@ class PersonControllerUpdateTest extends WebTestCase
return array(
['firstName', 'random Value', function(Person $person) { return $person->getFirstName(); } ],
['lastName' , 'random Value', function(Person $person) { return $person->getLastName(); } ],
['placeOfBirth', 'NONE PLACE', function(Person $person) { return $person->getPlaceOfBirth(); }],
// reminder: this value is capitalized
['placeOfBirth', 'A PLACE', function(Person $person) { return $person->getPlaceOfBirth(); }],
['birthdate', '1980-12-15', function(Person $person) { return $person->getBirthdate()->format('Y-m-d'); }],
['phonenumber', '+32123456789', function(Person $person) { return $person->getPhonenumber(); }],
['memo', 'jfkdlmq jkfldmsq jkmfdsq', function(Person $person) { return $person->getMemo(); }],

View File

@@ -2,23 +2,32 @@
declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Controller;
use Chill\MainBundle\Test\PrepareClientTrait;
use Chill\PersonBundle\DataFixtures\Helper\PersonRandomHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relation;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\Request;
use Chill\PersonBundle\Repository\PersonRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class RelationshipApiControllerTest extends WebTestCase
{
public static function setUpBeforeClass()
{
static::bootKernel();
}
use PrepareClientTrait;
private KernelBrowser $client;
/**
* A cache for all relations
* @var array|null|Relation[]
*/
private ?array $relations = null;
public function setUp()
{
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'fred',
'PHP_AUTH_PW' => 'password',
));
static::bootKernel();
$this->client = $this->getClientAuthenticated();
}
/**
@@ -35,14 +44,13 @@ class RelationshipApiControllerTest extends WebTestCase
/**
* @dataProvider relationProvider
*/
public function testPostRelationship($fromPersonId, $toPersonId, $relationId, $isReverse): void
{
$this->client->request(Request::METHOD_POST,
'/api/1.0/person/relations/relationship.json',
$this->client->request(Request::METHOD_POST,
'/api/1.0/relations/relationship.json',
[],
[],
[],
[],
\json_encode([
'type' => 'relationship',
'fromPerson' => ['id' => $fromPersonId, 'type' => 'person'],
@@ -57,18 +65,72 @@ class RelationshipApiControllerTest extends WebTestCase
public function relationProvider(): array
{
//TODO: which different cases to test?
static::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$countPersons = $em->createQueryBuilder()
->select('count(p)')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->getSingleScalarResult()
;
$persons = $em->createQueryBuilder()
->select('p')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->setMaxResults(2)
->setFirstResult(\random_int(0, $countPersons - 1))
->getResult()
;
return [
[333, 334, 1, true],
[$persons[0]->getId(), $persons[1]->getId(), $this->getRandomRelation($em)->getId(), true],
];
}
private function getRandomRelation(EntityManagerInterface $em): Relation
{
if (null === $this->relations) {
$this->relations = $em->getRepository(Relation::class)
->findAll();
}
return $this->relations[\array_rand($this->relations)];
}
public function personProvider(): array
{
//TODO: which different cases to test?
static::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$countPersons = $em->createQueryBuilder()
->select('count(p)')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->getSingleScalarResult()
;
$person = $em->createQueryBuilder()
->select('p')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->setMaxResults(1)
->setFirstResult(\random_int(0, $countPersons - 1))
->getSingleResult()
;
return [
[333],
[$person->getId()],
];
}
}
}

View File

@@ -1,6 +1,7 @@
services:
Chill\PersonBundle\DataFixtures\ORM\:
autowire: true
autoconfigure: true
resource: ../../DataFixtures/ORM
tags: [ 'doctrine.fixture.orm' ]

View File

@@ -50,6 +50,8 @@ mobilenumber: numéro de téléphone portable
Accept short text message ?: La personne a donné l'autorisation d'utiliser ce no de téléphone pour l'envoi de rappel par SMS
Accept short text message: La personne a donné l'autorisation d'utiliser ce no de téléphone pour l'envoi de rappel par SMS
Other phonenumber: Autre numéro de téléphone
Others phone numbers: Autres numéros de téléphone
No additional phone numbers: Aucun numéro de téléphone supplémentaire
Description: description
Add new phone: Ajouter un numéro de téléphone
Remove phone: Supprimer