113 lines
3.7 KiB
PHP
113 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Websmid\CustomHtml\Grid\Query;
|
|
|
|
use Doctrine\DBAL\Connection;
|
|
use PrestaShop\PrestaShop\Adapter\Configuration;
|
|
use PrestaShop\PrestaShop\Core\Grid\Query\AbstractDoctrineQueryBuilder;
|
|
use PrestaShop\PrestaShop\Core\Grid\Query\DoctrineSearchCriteriaApplicatorInterface;
|
|
use PrestaShop\PrestaShop\Core\Grid\Query\Filter\DoctrineFilterApplicatorInterface;
|
|
use PrestaShop\PrestaShop\Core\Grid\Search\SearchCriteriaInterface;
|
|
|
|
final class CustomHtmlBlockQueryBuilder extends AbstractDoctrineQueryBuilder
|
|
{
|
|
public function __construct(
|
|
Connection $connection,
|
|
string $dbPrefix,
|
|
DoctrineSearchCriteriaApplicatorInterface $searchCriteriaApplicator,
|
|
int $contextLanguageId,
|
|
int $contextShopId,
|
|
int $contextShopGroupId,
|
|
DoctrineFilterApplicatorInterface $filterApplicator,
|
|
Configuration $configuration
|
|
) {
|
|
parent::__construct($connection, $dbPrefix);
|
|
}
|
|
|
|
public function getSearchQueryBuilder(SearchCriteriaInterface $searchCriteria)
|
|
{
|
|
$qb = $this->getBaseQuery();
|
|
|
|
$qb->select(
|
|
'wchb.`id_ws_customhtml`',
|
|
'wchb.`id_ws_customhtml_group`',
|
|
'wchb.`title`',
|
|
'wchg.`title` AS group_title',
|
|
'wchb.`type`',
|
|
'wchb.`active`',
|
|
'wchb.`ordering`',
|
|
'wchb.`date_add`'
|
|
);
|
|
|
|
$this->applyFilters($qb, $searchCriteria);
|
|
|
|
$orderBy = $searchCriteria->getOrderBy() === 'position' ? 'wchb.ordering' : $searchCriteria->getOrderBy();
|
|
|
|
$qb
|
|
->orderBy($orderBy, $searchCriteria->getOrderWay())
|
|
->setFirstResult($searchCriteria->getOffset() ?? 0)
|
|
->setMaxResults($searchCriteria->getLimit());
|
|
|
|
return $qb;
|
|
}
|
|
|
|
public function getCountQueryBuilder(SearchCriteriaInterface $searchCriteria)
|
|
{
|
|
$qb = $this->getBaseQuery();
|
|
$qb->select('COUNT(wchb.`id_ws_customhtml`)');
|
|
|
|
$this->applyFilters($qb, $searchCriteria);
|
|
|
|
return $qb;
|
|
}
|
|
|
|
private function getBaseQuery()
|
|
{
|
|
return $this->connection
|
|
->createQueryBuilder()
|
|
->from($this->dbPrefix . 'ws_customhtml', 'wchb')
|
|
->leftJoin(
|
|
'wchb',
|
|
$this->dbPrefix . 'ws_customhtml_group',
|
|
'wchg',
|
|
'wchg.`id_ws_customhtml_group` = wchb.`id_ws_customhtml_group`'
|
|
);
|
|
}
|
|
|
|
private function applyFilters($qb, SearchCriteriaInterface $searchCriteria): void
|
|
{
|
|
foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
|
|
if ($filterName === 'actions') {
|
|
continue;
|
|
}
|
|
|
|
if ($filterName === 'id_ws_customhtml') {
|
|
$qb->andWhere('wchb.`id_ws_customhtml` = :id_ws_customhtml')
|
|
->setParameter('id_ws_customhtml', (int) $filterValue);
|
|
continue;
|
|
}
|
|
|
|
if ($filterName === 'id_ws_customhtml_group') {
|
|
$qb->andWhere('wchb.`id_ws_customhtml_group` = :id_ws_customhtml_group')
|
|
->setParameter('id_ws_customhtml_group', (int) $filterValue);
|
|
continue;
|
|
}
|
|
|
|
if ($filterName === 'group_title') {
|
|
$qb->andWhere('wchg.`title` LIKE :group_title')
|
|
->setParameter('group_title', '%' . pSQL($filterValue) . '%');
|
|
continue;
|
|
}
|
|
|
|
if ($filterName === 'type') {
|
|
$qb->andWhere('wchb.`type` = :type')
|
|
->setParameter('type', pSQL($filterValue));
|
|
continue;
|
|
}
|
|
|
|
$qb->andWhere('wchb.`' . $filterName . '` LIKE :' . $filterName)
|
|
->setParameter($filterName, '%' . pSQL($filterValue) . '%');
|
|
}
|
|
}
|
|
}
|