mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Merge remote-tracking branch 'origin/136-add-search-endpoint' into 139_demandeur
This commit is contained in:
commit
a887326611
@ -73,7 +73,8 @@
|
||||
"symfony/web-profiler-bundle": "^5.0",
|
||||
"symfony/var-dumper": "4.*",
|
||||
"symfony/debug-bundle": "^5.1",
|
||||
"symfony/phpunit-bridge": "^5.2"
|
||||
"symfony/phpunit-bridge": "^5.2",
|
||||
"nelmio/alice": "^3.8"
|
||||
},
|
||||
"scripts": {
|
||||
"auto-scripts": {
|
||||
|
@ -82,7 +82,7 @@ Chill will be available at ``http://localhost:8001.`` Currently, there isn't any
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
docker-compose exec --user $(id -u) php bin/console doctrine:fixtures:load
|
||||
docker-compose exec --user $(id -u) php bin/console doctrine:fixtures:load --purge-with-truncate
|
||||
|
||||
There are several users available:
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\MainBundle\Search\UnknowSearchDomainException;
|
||||
@ -34,6 +35,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\MainBundle\Search\SearchApi;
|
||||
|
||||
/**
|
||||
* Class SearchController
|
||||
@ -42,32 +44,24 @@ use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
*/
|
||||
class SearchController extends AbstractController
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var SearchProvider
|
||||
*/
|
||||
protected $searchProvider;
|
||||
protected SearchProvider $searchProvider;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var PaginatorFactory
|
||||
*/
|
||||
protected $paginatorFactory;
|
||||
protected PaginatorFactory $paginatorFactory;
|
||||
|
||||
protected SearchApi $searchApi;
|
||||
|
||||
function __construct(
|
||||
SearchProvider $searchProvider,
|
||||
TranslatorInterface $translator,
|
||||
PaginatorFactory $paginatorFactory
|
||||
PaginatorFactory $paginatorFactory,
|
||||
SearchApi $searchApi
|
||||
) {
|
||||
$this->searchProvider = $searchProvider;
|
||||
$this->translator = $translator;
|
||||
$this->paginatorFactory = $paginatorFactory;
|
||||
$this->searchApi = $searchApi;
|
||||
}
|
||||
|
||||
|
||||
@ -152,6 +146,19 @@ class SearchController extends AbstractController
|
||||
array('results' => $results, 'pattern' => $pattern)
|
||||
);
|
||||
}
|
||||
|
||||
public function searchApi(Request $request, $_format): JsonResponse
|
||||
{
|
||||
//TODO this is an incomplete implementation
|
||||
$query = $request->query->get('q', '');
|
||||
|
||||
$results = $this->searchApi->getResults($query, 0, 150);
|
||||
$paginator = $this->paginatorFactory->create(count($results));
|
||||
|
||||
$collection = new Collection($results, $paginator);
|
||||
|
||||
return $this->json($collection);
|
||||
}
|
||||
|
||||
public function advancedSearchListAction(Request $request)
|
||||
{
|
||||
|
@ -55,11 +55,11 @@ class LoadCenters extends AbstractFixture implements OrderedFixtureInterface
|
||||
public function load(ObjectManager $manager)
|
||||
{
|
||||
foreach (static::$centers as $new) {
|
||||
$centerA = new Center();
|
||||
$centerA->setName($new['name']);
|
||||
$center = new Center();
|
||||
$center->setName($new['name']);
|
||||
|
||||
$manager->persist($centerA);
|
||||
$this->addReference($new['ref'], $centerA);
|
||||
$manager->persist($center);
|
||||
$this->addReference($new['ref'], $center);
|
||||
static::$refs[] = $new['ref'];
|
||||
}
|
||||
|
||||
|
36
src/Bundle/ChillMainBundle/Search/Model/Result.php
Normal file
36
src/Bundle/ChillMainBundle/Search/Model/Result.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Search\Model;
|
||||
|
||||
class Result
|
||||
{
|
||||
private float $relevance;
|
||||
|
||||
/**
|
||||
* mixed an arbitrary result
|
||||
*/
|
||||
private $result;
|
||||
|
||||
/**
|
||||
* @param float $relevance
|
||||
* @param $result
|
||||
*/
|
||||
public function __construct(float $relevance, $result)
|
||||
{
|
||||
$this->relevance = $relevance;
|
||||
$this->result = $result;
|
||||
}
|
||||
|
||||
public function getRelevance(): float
|
||||
{
|
||||
return $this->relevance;
|
||||
}
|
||||
|
||||
public function getResult()
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
89
src/Bundle/ChillMainBundle/Search/SearchApi.php
Normal file
89
src/Bundle/ChillMainBundle/Search/SearchApi.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Search;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
use Symfony\Component\VarDumper\Resources\functions\dump;
|
||||
|
||||
/**
|
||||
* ***Warning*** This is an incomplete implementation ***Warning***
|
||||
*/
|
||||
class SearchApi
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
private SearchProvider $search;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, SearchProvider $search)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->search = $search;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Model/Result[]
|
||||
*/
|
||||
public function getResults(string $query, int $offset, int $maxResult): array
|
||||
{
|
||||
// **warning again**: this is an incomplete implementation
|
||||
$results = [];
|
||||
|
||||
foreach ($this->getPersons($query) as $p) {
|
||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $p);
|
||||
}
|
||||
foreach ($this->getThirdParties($query) as $t) {
|
||||
$results[] = new Model\Result((float)\rand(0, 100) / 100, $t);
|
||||
}
|
||||
|
||||
\usort($results, function(Model\Result $a, Model\Result $b) {
|
||||
return ($a->getRelevance() <=> $b->getRelevance()) * -1;
|
||||
});
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function countResults(string $query): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function getThirdParties(string $query)
|
||||
{
|
||||
$thirdPartiesIds = $this->em->createQuery('SELECT t.id FROM '.ThirdParty::class.' t')
|
||||
->getScalarResult();
|
||||
$nbResults = rand(0, 15);
|
||||
|
||||
if ($nbResults === 1) {
|
||||
$nbResults++;
|
||||
} elseif ($nbResults === 0) {
|
||||
return [];
|
||||
}
|
||||
$ids = \array_map(function ($e) use ($thirdPartiesIds) { return $thirdPartiesIds[$e]['id'];},
|
||||
\array_rand($thirdPartiesIds, $nbResults));
|
||||
|
||||
$a = $this->em->getRepository(ThirdParty::class)
|
||||
->findById($ids);
|
||||
return $a;
|
||||
}
|
||||
|
||||
private function getPersons(string $query)
|
||||
{
|
||||
$params = [
|
||||
SearchInterface::SEARCH_PREVIEW_OPTION => false
|
||||
];
|
||||
$search = $this->search->getResultByName($query, 'person_regular', 0, 50, $params, 'json');
|
||||
$ids = \array_map(function($r) { return $r['id']; }, $search['results']);
|
||||
|
||||
|
||||
if (count($ids) === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->em->getRepository(Person::class)
|
||||
->findById($ids)
|
||||
;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
class AddressNormalizer implements NormalizerAwareInterface, NormalizerInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
public function normalize($address, string $format = null, array $context = [])
|
||||
{
|
||||
$data['address_id'] = $address->getId();
|
||||
$data['text'] = $address->getStreet().', '.$address->getBuildingName();
|
||||
$data['postcode']['name'] = $address->getPostCode()->getName();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null)
|
||||
{
|
||||
return $data instanceof Address;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -69,6 +69,13 @@ chill_main_search:
|
||||
requirements:
|
||||
_format: html|json
|
||||
|
||||
chill_main_search_global:
|
||||
path: '/api/1.0/search.{_format}'
|
||||
controller: Chill\MainBundle\Controller\SearchController::searchApi
|
||||
format: 'json'
|
||||
requirements:
|
||||
_format: 'json'
|
||||
|
||||
chill_main_advanced_search:
|
||||
path: /{_locale}/search/advanced/{name}
|
||||
controller: Chill\MainBundle\Controller\SearchController::advancedSearchAction
|
||||
|
@ -16,6 +16,7 @@ services:
|
||||
$searchProvider: '@chill_main.search_provider'
|
||||
$translator: '@Symfony\Contracts\Translation\TranslatorInterface'
|
||||
$paginatorFactory: '@Chill\MainBundle\Pagination\PaginatorFactory'
|
||||
$searchApi: '@Chill\MainBundle\Search\SearchApi'
|
||||
tags: ['controller.service_arguments']
|
||||
|
||||
Chill\MainBundle\Controller\PermissionsGroupController:
|
||||
|
@ -1,3 +1,10 @@
|
||||
services:
|
||||
chill_main.search_provider:
|
||||
class: Chill\MainBundle\Search\SearchProvider
|
||||
class: Chill\MainBundle\Search\SearchProvider
|
||||
|
||||
Chill\MainBundle\Search\SearchProvider: '@chill_main.search_provider'
|
||||
|
||||
Chill\MainBundle\Search\SearchApi:
|
||||
arguments:
|
||||
$em: '@Doctrine\ORM\EntityManagerInterface'
|
||||
$search: '@Chill\MainBundle\Search\SearchProvider'
|
||||
|
@ -4,6 +4,10 @@ services:
|
||||
tags:
|
||||
- { name: 'serializer.normalizer', priority: 64 }
|
||||
|
||||
Chill\MainBundle\Serializer\Normalizer\AddressNormalizer:
|
||||
tags:
|
||||
- { name: 'serializer.normalizer', priority: 64 }
|
||||
|
||||
Chill\MainBundle\Serializer\Normalizer\DateNormalizer:
|
||||
tags:
|
||||
- { name: 'serializer.normalizer', priority: 64 }
|
||||
|
@ -25,7 +25,7 @@ use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
|
||||
|
||||
/**
|
||||
* Serialize a Person entity
|
||||
@ -41,25 +41,45 @@ class PersonNormalizer implements
|
||||
|
||||
protected PersonRepository $repository;
|
||||
|
||||
private ChillEntityRenderExtension $render;
|
||||
|
||||
public const GET_PERSON = 'get_person';
|
||||
|
||||
public function __construct(PersonRepository $repository)
|
||||
public function __construct(PersonRepository $repository, ChillEntityRenderExtension $render)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->render = $render;
|
||||
}
|
||||
|
||||
public function normalize($person, string $format = null, array $context = array())
|
||||
{
|
||||
/** @var Person $person */
|
||||
return [
|
||||
'id' => $person->getId(),
|
||||
'type' => 'person',
|
||||
'person_id' => $person->getId(),
|
||||
'text' => $this->render->renderString($person),
|
||||
'firstName' => $person->getFirstName(),
|
||||
'lastName' => $person->getLastName(),
|
||||
'birthdate' => $this->normalizer->normalize($person->getBirthdate()),
|
||||
'center' => $this->normalizer->normalize($person->getCenter())
|
||||
'center' => $this->normalizer->normalize($person->getCenter()),
|
||||
'phonenumber' => $person->getPhonenumber(),
|
||||
'mobilenumber' => $person->getMobilenumber(),
|
||||
'altNames' => $this->normalizeAltNames($person->getAltNames())
|
||||
];
|
||||
}
|
||||
|
||||
protected function normalizeAltNames($altNames): array
|
||||
{
|
||||
$r = [];
|
||||
|
||||
foreach ($altNames as $n) {
|
||||
$r[] = [ 'key' => $n->getKey(), 'label' => $n->getLabel() ];
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = []): Person
|
||||
{
|
||||
if ($context[self::GET_PERSON] ?? true) {
|
||||
|
@ -3,6 +3,7 @@ services:
|
||||
Chill\PersonBundle\Serializer\Normalizer\PersonNormalizer:
|
||||
arguments:
|
||||
$repository: '@Chill\PersonBundle\Repository\PersonRepository'
|
||||
$render: '@Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension'
|
||||
tags:
|
||||
- { name: 'serializer.normalizer', priority: 64 }
|
||||
|
||||
|
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\ThirdPartyBundle\DataFixtures\ORM;
|
||||
|
||||
use Chill\MainBundle\DataFixtures\ORM\LoadCenters;
|
||||
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||
use Nelmio\Alice\Loader\NativeLoader;
|
||||
use Nelmio\Alice\ObjectSet;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class LoadThirdParty extends Fixture Implements DependentFixtureInterface
|
||||
{
|
||||
public function load(ObjectManager $manager)
|
||||
{
|
||||
$thirdParties = $this->getThirdParties()->getObjects();
|
||||
|
||||
foreach ($thirdParties as $name => $thirdParty) {
|
||||
if ('a' === $name[0]) {
|
||||
// this is an address
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->getCenters() as $center) {
|
||||
$thirdParty->addCenter($center);
|
||||
}
|
||||
|
||||
$manager->persist($thirdParty);
|
||||
}
|
||||
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
private function getCenters(): \Iterator
|
||||
{
|
||||
$references = \array_map(function($a) { return $a['ref']; },
|
||||
LoadCenters::$centers);
|
||||
$number = random_int(1, count($references));
|
||||
|
||||
if ($number === 1) {
|
||||
yield $this->getReference($references[\array_rand($references)]);
|
||||
} else {
|
||||
foreach (array_rand($references, $number) as $index) {
|
||||
yield $this->getReference($references[$index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getDependencies()
|
||||
{
|
||||
return [
|
||||
LoadCenters::class,
|
||||
LoadPostalCodes::class
|
||||
];
|
||||
}
|
||||
|
||||
private function getThirdParties(): ObjectSet
|
||||
{
|
||||
$loader = new NativeLoader();
|
||||
$objectSet = $loader->loadData([
|
||||
Address::class => [
|
||||
'address{1..75}' => [
|
||||
'street' => '<fr_FR:streetName()>',
|
||||
'streetNumber' => '<fr_FR:buildingNumber()>',
|
||||
'validFrom' => '<dateTimeBetween(\'-1 year\', \'now\')>',
|
||||
'postCode' => $this->getPostalCode()
|
||||
],
|
||||
],
|
||||
ThirdParty::class => [
|
||||
'thirdparty{1..75}' => [
|
||||
'name' => '<fr_FR:company()>',
|
||||
'telephone' => '<fr_FR:phonenumber()>',
|
||||
'email' => '<email()>',
|
||||
'comment' => '<fr_FR:realTextBetween(10, 500)>',
|
||||
'address' => '@address<current()>'
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
return $objectSet;
|
||||
}
|
||||
|
||||
private function getPostalCode(): PostalCode
|
||||
{
|
||||
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
||||
return $this->getReference($ref);
|
||||
if (count($this->postalCodesIds) === 0) {
|
||||
// fill the postal codes
|
||||
$this->em->createQuery('SELECT p.id FROM '.PostalCode::class)
|
||||
->getScalarResult();
|
||||
}
|
||||
|
||||
$id = $this->postalCodesIds[\array_rand($this->postalCodesIds)];
|
||||
|
||||
return $this->em->getRepository(PostalCode::class)
|
||||
->find($id);
|
||||
}
|
||||
|
||||
private function createAddress(): ObjectSet
|
||||
{
|
||||
$loader = new NativeLoader();
|
||||
$objectSet = $loader->loadData([
|
||||
Address::class => [
|
||||
'address1' => [
|
||||
'name' => '<fr_FR:company()>',
|
||||
'telephone' => '<fr_FR:phonenumber()>',
|
||||
'email' => '<email()>',
|
||||
'comment' => '<fr_FR:realTextBetween(10, 500)>'
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
return $objectSet;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -33,6 +33,8 @@ class ChillThirdPartyExtension extends Extension implements PrependExtensionInte
|
||||
$loader->load('services/search.yaml');
|
||||
$loader->load('services/templating.yaml');
|
||||
$loader->load('services/menu.yaml');
|
||||
$loader->load('services/fixtures.yaml');
|
||||
$loader->load('services/serializer.yaml');
|
||||
}
|
||||
|
||||
public function prepend(ContainerBuilder $container)
|
||||
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
class ThirdPartyNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
public function normalize($thirdParty, string $format = null, array $context = [])
|
||||
{
|
||||
/** @var $thirdParty ThirdParty */
|
||||
$data['type'] = 'thirdparty';
|
||||
// TODO should be replaced by a "render entity"
|
||||
$data['text'] = $thirdParty->getName();
|
||||
$data['thirdparty_id'] = $thirdParty->getId();
|
||||
$data['address'] = $this->normalizer->normalize($thirdParty->getAddress(), $format,
|
||||
[ 'address_rendering' => 'short' ]);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null)
|
||||
{
|
||||
return $data instanceof ThirdParty;
|
||||
}
|
||||
}
|
@ -1 +1,2 @@
|
||||
---
|
||||
services:
|
||||
|
@ -0,0 +1,5 @@
|
||||
---
|
||||
services:
|
||||
Chill\ThirdPartyBundle\DataFixtures\ORM\LoadThirdParty:
|
||||
tags:
|
||||
- { 'name': doctrine.fixture.orm }
|
@ -0,0 +1,5 @@
|
||||
services:
|
||||
Chill\ThirdPartyBundle\Serializer\Normalizer\:
|
||||
resource: '../../Serializer/Normalizer/'
|
||||
tags:
|
||||
- { name: 'serializer.normalizer', priority: 64 }
|
Loading…
x
Reference in New Issue
Block a user