feat(main): Make groups reorderable
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
namespace Websmid\CustomHtml\Controller;
|
||||
|
||||
use Exception;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Position\Exception\PositionUpdateException;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Position\GridPositionUpdaterInterface;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Position\PositionUpdateFactoryInterface;
|
||||
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
|
||||
use PrestaShopBundle\Security\Voter\PageVoter;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
@@ -103,6 +106,27 @@ class CustomHtmlGroupController extends FrameworkBundleAdminController
|
||||
return $this->redirectToRoute('customhtmlgroup_index');
|
||||
}
|
||||
|
||||
public function updatePositionAction(Request $request): RedirectResponse
|
||||
{
|
||||
$positionDefinition = $this->get('websmid.customhtml.grid.position.customhtmlgroup_position_definition');
|
||||
|
||||
try {
|
||||
$positionUpdateFactory = $this->get(PositionUpdateFactoryInterface::class);
|
||||
$positionUpdate = $positionUpdateFactory->buildPositionUpdate([
|
||||
'positions' => $request->request->all('positions'),
|
||||
], $positionDefinition);
|
||||
|
||||
$updater = $this->get(GridPositionUpdaterInterface::class);
|
||||
$updater->update($positionUpdate);
|
||||
|
||||
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
|
||||
} catch (PositionUpdateException $e) {
|
||||
$this->addFlash('error', $this->getErrorMessageForException($e, []));
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('customhtmlgroup_index');
|
||||
}
|
||||
|
||||
private function getToolbarButtons(?string $securitySubject): array
|
||||
{
|
||||
$toolbarButtons = [];
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Websmid\CustomHtml\Form\DataHandler;
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
||||
use Context;
|
||||
use Db;
|
||||
use Hook;
|
||||
use Module;
|
||||
use Validate;
|
||||
@@ -17,7 +18,7 @@ final class CustomHtmlGroupDataHandler implements FormDataHandlerInterface
|
||||
$group->title = (string) $formData['title'];
|
||||
$group->hook_name = (string) $formData['hook_name'];
|
||||
$group->active = (int) $formData['active'];
|
||||
$group->ordering = 0;
|
||||
$group->ordering = $this->getNextOrdering();
|
||||
|
||||
if (!$group->add()) {
|
||||
return false;
|
||||
@@ -28,6 +29,15 @@ final class CustomHtmlGroupDataHandler implements FormDataHandlerInterface
|
||||
return $group->id;
|
||||
}
|
||||
|
||||
private function getNextOrdering(): int
|
||||
{
|
||||
$maxOrdering = Db::getInstance()->getValue(
|
||||
'SELECT MAX(`ordering`) FROM `' . _DB_PREFIX_ . 'ws_customhtml_group`'
|
||||
);
|
||||
|
||||
return $maxOrdering !== false ? ((int) $maxOrdering + 1) : 0;
|
||||
}
|
||||
|
||||
public function update($id, array $formData)
|
||||
{
|
||||
$group = new CustomHtmlGroup((int) $id);
|
||||
|
||||
@@ -8,6 +8,7 @@ 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\Common\PositionColumn;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\AbstractGridDefinitionFactory;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\DeleteActionTrait;
|
||||
@@ -81,6 +82,16 @@ final class CustomHtmlGroupGridDefinitionFactory extends AbstractGridDefinitionF
|
||||
->setName($this->trans('Active', [], 'Admin.Global'))
|
||||
->setOptions(['field' => 'active'])
|
||||
)
|
||||
->add(
|
||||
(new PositionColumn('position'))
|
||||
->setName($this->trans('Position', [], 'Admin.Global'))
|
||||
->setOptions([
|
||||
'id_field' => 'id_ws_customhtml_group',
|
||||
'position_field' => 'ordering',
|
||||
'update_method' => 'POST',
|
||||
'update_route' => 'customhtmlgroup_update_position',
|
||||
])
|
||||
)
|
||||
->add(
|
||||
(new DataColumn('date_add'))
|
||||
->setName($this->trans('Date added', [], 'Admin.Global'))
|
||||
|
||||
@@ -14,7 +14,7 @@ final class CustomHtmlGroupFilters extends Filters
|
||||
return [
|
||||
'limit' => 10,
|
||||
'offset' => 0,
|
||||
'orderBy' => 'wchg.id_ws_customhtml_group',
|
||||
'orderBy' => 'position',
|
||||
'sortOrder' => 'asc',
|
||||
'filters' => [],
|
||||
];
|
||||
|
||||
@@ -46,6 +46,7 @@ final class CustomHtmlGroupQueryBuilder extends AbstractDoctrineQueryBuilder
|
||||
'wchg.`title`',
|
||||
'wchg.`hook_name`',
|
||||
'wchg.`active`',
|
||||
'wchg.`ordering`',
|
||||
'wchg.`date_add`',
|
||||
'(SELECT COUNT(*) FROM `' . $this->dbPrefix . 'ws_customhtml` wch
|
||||
WHERE wch.`id_ws_customhtml_group` = wchg.`id_ws_customhtml_group`) AS blocks_count'
|
||||
@@ -53,8 +54,10 @@ final class CustomHtmlGroupQueryBuilder extends AbstractDoctrineQueryBuilder
|
||||
|
||||
$this->applyFilters($qb, $searchCriteria);
|
||||
|
||||
$orderBy = $searchCriteria->getOrderBy() === 'position' ? 'wchg.ordering' : $searchCriteria->getOrderBy();
|
||||
|
||||
$qb
|
||||
->orderBy($searchCriteria->getOrderBy(), $searchCriteria->getOrderWay())
|
||||
->orderBy($orderBy, $searchCriteria->getOrderWay())
|
||||
->setFirstResult($searchCriteria->getOffset() ?? 0)
|
||||
->setMaxResults($searchCriteria->getLimit());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user