feat(main): Initial Module
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Grid\Definition\Factory;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Grid\Action\GridActionCollection;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Action\Row\RowActionCollection;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Action\Row\Type\LinkRowAction;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Action\Type\SimpleGridAction;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Column\ColumnCollection;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\ActionColumn;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\AbstractGridDefinitionFactory;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Filter\FilterCollection;
|
||||
use PrestaShop\PrestaShop\Core\Hook\HookDispatcherInterface;
|
||||
use PrestaShopBundle\Form\Admin\Type\SearchAndResetType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
|
||||
final class CustomHtmlGridDefinitionFactory extends AbstractGridDefinitionFactory
|
||||
{
|
||||
const GRID_ID = 'customhtml';
|
||||
|
||||
public function __construct(HookDispatcherInterface $dispatcher)
|
||||
{
|
||||
parent::__construct($dispatcher);
|
||||
}
|
||||
|
||||
protected function getId(): string
|
||||
{
|
||||
return self::GRID_ID;
|
||||
}
|
||||
|
||||
protected function getName(): string
|
||||
{
|
||||
return $this->trans('Custom HTML Blocks', [], 'Modules.Wscustomhtml.Admin');
|
||||
}
|
||||
|
||||
protected function getGridActions()
|
||||
{
|
||||
return (new GridActionCollection())
|
||||
->add(
|
||||
(new SimpleGridAction('common_refresh_list'))
|
||||
->setName($this->trans('Refresh list', [], 'Admin.Catalog.Feature'))
|
||||
->setIcon('refresh')
|
||||
)
|
||||
->add(
|
||||
(new SimpleGridAction('common_show_query'))
|
||||
->setName($this->trans('Show SQL query', [], 'Admin.Catalog.Feature'))
|
||||
->setIcon('code')
|
||||
);
|
||||
}
|
||||
|
||||
protected function getColumns()
|
||||
{
|
||||
return (new ColumnCollection())
|
||||
->add(
|
||||
(new DataColumn('id_ws_customhtml'))
|
||||
->setName($this->trans('ID', [], 'Admin.Global'))
|
||||
->setOptions(['field' => 'id_ws_customhtml'])
|
||||
)
|
||||
->add(
|
||||
(new DataColumn('title'))
|
||||
->setName($this->trans('Title', [], 'Modules.Wscustomhtml.Admin'))
|
||||
->setOptions(['field' => 'title'])
|
||||
)
|
||||
->add(
|
||||
(new DataColumn('hook_name'))
|
||||
->setName($this->trans('Hook', [], 'Modules.Wscustomhtml.Admin'))
|
||||
->setOptions(['field' => 'hook_name'])
|
||||
)
|
||||
->add(
|
||||
(new DataColumn('active'))
|
||||
->setName($this->trans('Active', [], 'Admin.Global'))
|
||||
->setOptions(['field' => 'active'])
|
||||
)
|
||||
->add(
|
||||
(new DataColumn('date_add'))
|
||||
->setName($this->trans('Date added', [], 'Admin.Global'))
|
||||
->setOptions(['field' => 'date_add'])
|
||||
)
|
||||
->add(
|
||||
(new ActionColumn('actions'))
|
||||
->setName($this->trans('Actions', [], 'Admin.Global'))
|
||||
->setOptions(['actions' => $this->buildRowActions()])
|
||||
);
|
||||
}
|
||||
|
||||
protected function getFilters()
|
||||
{
|
||||
return (new FilterCollection())
|
||||
->add(
|
||||
(new Filter('id_ws_customhtml', TextType::class))
|
||||
->setAssociatedColumn('id_ws_customhtml')
|
||||
->setTypeOptions([
|
||||
'required' => false,
|
||||
'attr' => ['placeholder' => $this->trans('ID', [], 'Admin.Global')],
|
||||
])
|
||||
)
|
||||
->add(
|
||||
(new Filter('title', TextType::class))
|
||||
->setAssociatedColumn('title')
|
||||
->setTypeOptions([
|
||||
'required' => false,
|
||||
'attr' => ['placeholder' => $this->trans('Title', [], 'Modules.Wscustomhtml.Admin')],
|
||||
])
|
||||
)
|
||||
->add(
|
||||
(new Filter('hook_name', TextType::class))
|
||||
->setAssociatedColumn('hook_name')
|
||||
->setTypeOptions([
|
||||
'required' => false,
|
||||
'attr' => ['placeholder' => $this->trans('Hook', [], 'Modules.Wscustomhtml.Admin')],
|
||||
])
|
||||
)
|
||||
->add(
|
||||
(new Filter('actions', SearchAndResetType::class))
|
||||
->setAssociatedColumn('actions')
|
||||
->setTypeOptions([
|
||||
'reset_route' => 'admin_common_reset_search_by_filter_id',
|
||||
'reset_route_params' => ['filterId' => self::GRID_ID],
|
||||
'redirect_route' => 'customhtml_index',
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
private function buildRowActions(): RowActionCollection
|
||||
{
|
||||
return (new RowActionCollection())
|
||||
->add(
|
||||
(new LinkRowAction('edit'))
|
||||
->setName($this->trans('Edit', [], 'Admin.Actions'))
|
||||
->setIcon('edit')
|
||||
->setOptions([
|
||||
'route' => 'customhtml_edit',
|
||||
'route_param_name' => 'idCustomHtml',
|
||||
'route_param_field' => 'id_ws_customhtml',
|
||||
'use_inline_display' => true,
|
||||
'clickable_row' => true,
|
||||
])
|
||||
)
|
||||
->add(
|
||||
(new LinkRowAction('delete'))
|
||||
->setName($this->trans('Delete', [], 'Admin.Actions'))
|
||||
->setIcon('delete')
|
||||
->setOptions([
|
||||
'route' => 'customhtml_delete',
|
||||
'route_param_name' => 'idCustomHtml',
|
||||
'route_param_field' => 'id_ws_customhtml',
|
||||
'use_inline_display' => true,
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Grid\Filters;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Search\Filters;
|
||||
use Websmid\CustomHtml\Grid\Definition\Factory\CustomHtmlGridDefinitionFactory;
|
||||
|
||||
final class CustomHtmlFilters extends Filters
|
||||
{
|
||||
protected $filterId = CustomHtmlGridDefinitionFactory::GRID_ID;
|
||||
|
||||
public static function getDefaults(): array
|
||||
{
|
||||
return [
|
||||
'limit' => 10,
|
||||
'offset' => 0,
|
||||
'orderBy' => 'wch.id_ws_customhtml',
|
||||
'sortOrder' => 'asc',
|
||||
'filters' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?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 CustomHtmlQueryBuilder extends AbstractDoctrineQueryBuilder
|
||||
{
|
||||
private $searchCriteriaApplicator;
|
||||
private $contextLanguageId;
|
||||
private $contextShopId;
|
||||
private $contextShopGroupId;
|
||||
private $filterApplicator;
|
||||
private $configuration;
|
||||
|
||||
public function __construct(
|
||||
Connection $connection,
|
||||
string $dbPrefix,
|
||||
DoctrineSearchCriteriaApplicatorInterface $searchCriteriaApplicator,
|
||||
int $contextLanguageId,
|
||||
int $contextShopId,
|
||||
int $contextShopGroupId,
|
||||
DoctrineFilterApplicatorInterface $filterApplicator,
|
||||
Configuration $configuration
|
||||
) {
|
||||
parent::__construct($connection, $dbPrefix);
|
||||
$this->searchCriteriaApplicator = $searchCriteriaApplicator;
|
||||
$this->contextLanguageId = $contextLanguageId;
|
||||
$this->contextShopId = $contextShopId;
|
||||
$this->contextShopGroupId = $contextShopGroupId;
|
||||
$this->filterApplicator = $filterApplicator;
|
||||
$this->configuration = $configuration;
|
||||
}
|
||||
|
||||
public function getSearchQueryBuilder(SearchCriteriaInterface $searchCriteria)
|
||||
{
|
||||
$qb = $this->getBaseQuery();
|
||||
|
||||
$qb->select(
|
||||
'wch.`id_ws_customhtml`',
|
||||
'wch.`title`',
|
||||
'wch.`hook_name`',
|
||||
'wch.`active`',
|
||||
'wch.`date_add`'
|
||||
);
|
||||
|
||||
$this->applyFilters($qb, $searchCriteria);
|
||||
|
||||
$qb
|
||||
->orderBy($searchCriteria->getOrderBy(), $searchCriteria->getOrderWay())
|
||||
->setFirstResult($searchCriteria->getOffset() ?? 0)
|
||||
->setMaxResults($searchCriteria->getLimit());
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function getCountQueryBuilder(SearchCriteriaInterface $searchCriteria)
|
||||
{
|
||||
$qb = $this->getBaseQuery();
|
||||
$qb->select('COUNT(wch.`id_ws_customhtml`)');
|
||||
|
||||
$this->applyFilters($qb, $searchCriteria);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
private function getBaseQuery()
|
||||
{
|
||||
return $this->connection
|
||||
->createQueryBuilder()
|
||||
->from($this->dbPrefix . 'ws_customhtml', 'wch');
|
||||
}
|
||||
|
||||
private function applyFilters($qb, SearchCriteriaInterface $searchCriteria): void
|
||||
{
|
||||
foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
|
||||
if ($filterName === 'actions') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($filterName === 'id_ws_customhtml') {
|
||||
$qb->andWhere('wch.`id_ws_customhtml` = :id_ws_customhtml')
|
||||
->setParameter('id_ws_customhtml', (int) $filterValue);
|
||||
continue;
|
||||
}
|
||||
|
||||
$qb->andWhere('wch.`' . $filterName . '` LIKE :' . $filterName)
|
||||
->setParameter($filterName, '%' . pSQL($filterValue) . '%');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user