mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-25 00:53:48 +00:00
add filter for generic doc + fix issues in filter
This commit is contained in:
@@ -14,12 +14,12 @@ namespace Chill\DocStoreBundle\GenericDoc;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
|
||||
class GenericDocDTO
|
||||
final readonly class GenericDocDTO
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $key,
|
||||
public readonly array $identifiers,
|
||||
public readonly \DateTimeImmutable $docDate,
|
||||
public string $key,
|
||||
public array $identifiers,
|
||||
public \DateTimeImmutable $docDate,
|
||||
public AccompanyingPeriod|Person $linked,
|
||||
) {
|
||||
}
|
||||
|
@@ -32,6 +32,7 @@ class Manager
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $places
|
||||
* @throws Exception
|
||||
*/
|
||||
public function countDocForAccompanyingPeriod(
|
||||
@@ -39,16 +40,16 @@ class Manager
|
||||
?\DateTimeImmutable $startDate = null,
|
||||
?\DateTimeImmutable $endDate = null,
|
||||
?string $content = null,
|
||||
?string $origin = null
|
||||
array $places = []
|
||||
): int {
|
||||
['sql' => $sql, 'params' => $params] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $origin);
|
||||
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $places);
|
||||
|
||||
if ($sql === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$countSql = "SELECT count(*) AS c FROM ({$sql}) AS sq";
|
||||
$result = $this->connection->executeQuery($countSql, $params);
|
||||
$result = $this->connection->executeQuery($countSql, $params, $types);
|
||||
|
||||
$number = $result->fetchOne();
|
||||
|
||||
@@ -60,6 +61,7 @@ class Manager
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $places places to search. When empty, search in all places
|
||||
* @throws Exception
|
||||
*/
|
||||
public function findDocForAccompanyingPeriod(
|
||||
@@ -69,15 +71,19 @@ class Manager
|
||||
?\DateTimeImmutable $startDate = null,
|
||||
?\DateTimeImmutable $endDate = null,
|
||||
?string $content = null,
|
||||
?string $origin = null
|
||||
array $places = []
|
||||
): iterable {
|
||||
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $origin);
|
||||
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod, $startDate, $endDate, $content, $places);
|
||||
|
||||
if ($sql === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$runSql = "{$sql} ORDER BY doc_date DESC LIMIT ? OFFSET ?";
|
||||
$runParams = [...$params, ...[$limit, $offset]];
|
||||
$runTypes = [...$types, ...[Types::INTEGER, Types::INTEGER]];
|
||||
|
||||
foreach($this->connection->iterateAssociative($runSql, $runParams, $runTypes) as $row) {
|
||||
foreach ($this->connection->iterateAssociative($runSql, $runParams, $runTypes) as $row) {
|
||||
yield new GenericDocDTO(
|
||||
$row['key'],
|
||||
json_decode($row['identifiers'], true, 512, JSON_THROW_ON_ERROR),
|
||||
@@ -87,14 +93,34 @@ class Manager
|
||||
}
|
||||
}
|
||||
|
||||
public function placesForAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod): array
|
||||
{
|
||||
['sql' => $sql, 'params' => $params, 'types' => $types] = $this->buildUnionQuery($accompanyingPeriod);
|
||||
|
||||
if ($sql === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$runSql = "SELECT DISTINCT key FROM ({$sql}) AS sq";
|
||||
|
||||
$keys = [];
|
||||
|
||||
foreach ($this->connection->iterateAssociative($runSql, $params, $types) as $k) {
|
||||
$keys[] = $k['key'];
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $places places to search. When empty, search in all places
|
||||
*/
|
||||
private function buildUnionQuery(
|
||||
AccompanyingPeriod|Person $linked,
|
||||
?\DateTimeImmutable $startDate = null,
|
||||
?\DateTimeImmutable $endDate = null,
|
||||
?string $content = null,
|
||||
?string $origin = null
|
||||
array $places = [],
|
||||
): array {
|
||||
$sql = [];
|
||||
$params = [];
|
||||
@@ -105,17 +131,20 @@ class Manager
|
||||
if (!$provider->isAllowedForAccompanyingPeriod($linked)) {
|
||||
continue;
|
||||
}
|
||||
$query = $provider->buildFetchQueryForAccompanyingPeriod($linked, $startDate, $endDate, $content);
|
||||
|
||||
['sql' => $q, 'params' => $p, 'types' => $t ] = $this->builder
|
||||
->toSql($provider->buildFetchQueryForAccompanyingPeriod($linked, $startDate, $endDate, $content, $origin));
|
||||
$params = [...$params, ...$p];
|
||||
$types = [...$types, ...$t];
|
||||
if ([] !== $places and !in_array($query->getSelectKeyString(), $places, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
['sql' => $q, 'params' => $p, 'types' => $t ] = $this->builder->toSql($query);
|
||||
|
||||
$sql[] = $q;
|
||||
$params = [...$params, ...$p];
|
||||
$types = [...$types, ...$t];
|
||||
}
|
||||
}
|
||||
|
||||
return ['sql' => implode(' UNION ', $sql), 'params' => $params, 'types' => $types];
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -50,7 +50,7 @@ final readonly class AccompanyingProviderCourseDocumentGenericDoc implements Gen
|
||||
|
||||
if (null !== $startDate) {
|
||||
$query->addWhereClause(
|
||||
sprintf('? >= %s', $classMetadata->getColumnName('date')),
|
||||
sprintf('? <= %s', $classMetadata->getColumnName('date')),
|
||||
[$startDate],
|
||||
[Types::DATE_IMMUTABLE]
|
||||
);
|
||||
@@ -58,7 +58,7 @@ final readonly class AccompanyingProviderCourseDocumentGenericDoc implements Gen
|
||||
|
||||
if (null !== $endDate) {
|
||||
$query->addWhereClause(
|
||||
sprintf('? < %s', $classMetadata->getColumnName('date')),
|
||||
sprintf('? >= %s', $classMetadata->getColumnName('date')),
|
||||
[$endDate],
|
||||
[Types::DATE_IMMUTABLE]
|
||||
);
|
||||
@@ -71,7 +71,7 @@ final readonly class AccompanyingProviderCourseDocumentGenericDoc implements Gen
|
||||
$classMetadata->getColumnName('title'),
|
||||
$classMetadata->getColumnName('description')
|
||||
),
|
||||
[$content, $content],
|
||||
['%' . $content . '%', '%' . $content . '%'],
|
||||
[Types::STRING, Types::STRING]
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user