mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-19 16:54:25 +00:00
110 lines
3.7 KiB
PHP
110 lines
3.7 KiB
PHP
<?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\PersonBundle\Search;
|
|
|
|
use Chill\MainBundle\Search\SearchApiInterface;
|
|
use Chill\MainBundle\Search\SearchApiQuery;
|
|
use Chill\MainBundle\Search\Utils\ExtractDateFromPattern;
|
|
use Chill\MainBundle\Search\Utils\ExtractPhonenumberFromPattern;
|
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
|
use Chill\PersonBundle\Repository\Household\HouseholdRepository;
|
|
use Chill\PersonBundle\Repository\PersonACLAwareRepositoryInterface;
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
use function array_map;
|
|
use function count;
|
|
use function in_array;
|
|
|
|
class SearchHouseholdApiProvider implements SearchApiInterface
|
|
{
|
|
private AuthorizationHelperInterface $authorizationHelper;
|
|
|
|
private ExtractDateFromPattern $extractDateFromPattern;
|
|
|
|
private ExtractPhonenumberFromPattern $extractPhonenumberFromPattern;
|
|
|
|
private HouseholdRepository $householdRepository;
|
|
|
|
private PersonACLAwareRepositoryInterface $personACLAwareRepository;
|
|
|
|
private Security $security;
|
|
|
|
public function __construct(
|
|
HouseholdRepository $householdRepository,
|
|
PersonACLAwareRepositoryInterface $personACLAwareRepository,
|
|
Security $security,
|
|
AuthorizationHelperInterface $authorizationHelper,
|
|
ExtractDateFromPattern $extractDateFromPattern,
|
|
ExtractPhonenumberFromPattern $extractPhonenumberFromPattern
|
|
) {
|
|
$this->householdRepository = $householdRepository;
|
|
$this->personACLAwareRepository = $personACLAwareRepository;
|
|
$this->security = $security;
|
|
$this->authorizationHelper = $authorizationHelper;
|
|
$this->extractDateFromPattern = $extractDateFromPattern;
|
|
$this->extractPhonenumberFromPattern = $extractPhonenumberFromPattern;
|
|
}
|
|
|
|
public function getResult(string $key, array $metadata, float $pertinence)
|
|
{
|
|
return $this->householdRepository->find($metadata['id']);
|
|
}
|
|
|
|
public function prepare(array $metadatas): void
|
|
{
|
|
$ids = array_map(static fn ($m) => $m['id'], $metadatas);
|
|
|
|
$this->householdRepository->findBy(['id' => $ids]);
|
|
}
|
|
|
|
public function provideQuery(string $pattern, array $parameters): SearchApiQuery
|
|
{
|
|
$datesResult = $this->extractDateFromPattern->extractDates($pattern);
|
|
$phoneResult = $this->extractPhonenumberFromPattern->extractPhonenumber($datesResult->getFilteredSubject());
|
|
$filtered = $phoneResult->getFilteredSubject();
|
|
|
|
$query = $this->personACLAwareRepository->buildAuthorizedQuery(
|
|
$filtered,
|
|
null,
|
|
null,
|
|
count($datesResult->getFound()) > 0 ? $datesResult->getFound()[0] : null,
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
count($phoneResult->getFound()) > 0 ? $phoneResult->getFound()[0] : null
|
|
);
|
|
|
|
$query
|
|
->setDistinct(true, 'household_id')
|
|
->setFromClause(
|
|
'view_chill_person_household_address AS vcpha ' .
|
|
'JOIN chill_person_person AS person ON vcpha.person_id = person.id'
|
|
)
|
|
->setSelectKey('household')
|
|
->andWhereClause('vcpha.validTo IS NULL', [])
|
|
->setSelectJsonbMetadata("jsonb_build_object('id', vcpha.household_id)");
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function supportsResult(string $key, array $metadatas): bool
|
|
{
|
|
return 'household' === $key;
|
|
}
|
|
|
|
public function supportsTypes(string $pattern, array $types, array $parameters): bool
|
|
{
|
|
return in_array('household', $types, true);
|
|
}
|
|
}
|