123 lines
3.7 KiB
PHP
123 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\PreordersBundle\Autocomplete;
|
|
|
|
use KupShop\PreordersBundle\Autocomplete\AutocompleteType as Type;
|
|
use Query\Operator as Op;
|
|
use Query\QueryBuilder;
|
|
|
|
class PreorderAutocomplete
|
|
{
|
|
/**
|
|
* @throws \Doctrine\DBAL\Driver\Exception|\Doctrine\DBAL\Exception
|
|
*/
|
|
public function autocomplete(Type $type, string $term): array
|
|
{
|
|
return match ($type) {
|
|
Type::Preorders => $this->autocompletePreorders($term),
|
|
Type::PreordersDates => $this->autocompletePreordersDates($term),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param int[] $ids
|
|
*
|
|
* @throws \Doctrine\DBAL\Driver\Exception|\Doctrine\DBAL\Exception
|
|
*/
|
|
public function preload(Type $type, array $ids): array
|
|
{
|
|
return match ($type) {
|
|
Type::Preorders => $this->preloadPreorders($ids),
|
|
Type::PreordersDates => $this->preloadPreordersDates($ids),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @throws \Doctrine\DBAL\Driver\Exception|\Doctrine\DBAL\Exception
|
|
*/
|
|
private function autocompletePreorders(string $term): array
|
|
{
|
|
return $this->createQueryBuilder(Type::Preorders)
|
|
->andWhere(
|
|
Op::orX([
|
|
Op::like(['po.name' => "%{$term}%"]),
|
|
Op::equals(['po.id' => $term]),
|
|
]))
|
|
->execute()
|
|
->fetchAllAssociative();
|
|
}
|
|
|
|
/**
|
|
* @throws \Doctrine\DBAL\Driver\Exception|\Doctrine\DBAL\Exception
|
|
*/
|
|
private function autocompletePreordersDates(string $term): array
|
|
{
|
|
return $this->createQueryBuilder(Type::PreordersDates)
|
|
->andWhere(Op::like([
|
|
'po.name' => "%{$term}%",
|
|
'pd.date_start' => "%{$term}%",
|
|
'pd.date_end' => "%{$term}%",
|
|
], 'OR'))
|
|
->execute()
|
|
->fetchAllAssociative();
|
|
}
|
|
|
|
/**
|
|
* @throws \Doctrine\DBAL\Driver\Exception|\Doctrine\DBAL\Exception
|
|
*/
|
|
private function preloadPreorders(array $ids): array
|
|
{
|
|
return $this->createQueryBuilder(Type::Preorders)
|
|
->andWhere(Op::inIntArray($ids, 'po.id'))
|
|
->execute()
|
|
->fetchAllAssociative();
|
|
}
|
|
|
|
/**
|
|
* @throws \Doctrine\DBAL\Driver\Exception|\Doctrine\DBAL\Exception
|
|
*/
|
|
private function preloadPreordersDates(array $ids): array
|
|
{
|
|
return $this->createQueryBuilder(Type::PreordersDates)
|
|
->andWhere(Op::inIntArray($ids, 'pd.id'))
|
|
->execute()
|
|
->fetchAllAssociative();
|
|
}
|
|
|
|
private function createQueryBuilder(Type $type): QueryBuilder
|
|
{
|
|
if ($type === Type::Preorders) {
|
|
return sqlQueryBuilder()
|
|
->select('po.id', 'po.name', 'po.id AS value', 'po.name AS text')
|
|
->from('preorders', 'po');
|
|
}
|
|
|
|
if ($type === Type::PreordersDates) {
|
|
$dates = <<<__SQL__
|
|
IF(
|
|
pd.date_shipment = '',
|
|
CONCAT(
|
|
DATE_FORMAT(pd.date_start, '%d.%m.%Y'),
|
|
' - ',
|
|
DATE_FORMAT(pd.date_end, '%d.%m.%Y')
|
|
),
|
|
CONCAT('(exp.: ', pd.date_shipment, ')')
|
|
)
|
|
__SQL__;
|
|
|
|
return sqlQueryBuilder()
|
|
->select(
|
|
'pd.id',
|
|
'pd.id AS value',
|
|
"CONCAT_WS(' ', po.name, {$dates}) AS text",
|
|
"CONCAT_WS(' ', po.name, {$dates}) AS name")
|
|
->from('preorders_dates', 'pd')
|
|
->leftJoin('pd', 'preorders', 'po', 'pd.id_preorder = po.id');
|
|
}
|
|
|
|
throw new \Exception('Not implemented');
|
|
}
|
|
}
|