feat(main): Make groups reorderable
This commit is contained in:
@@ -34,6 +34,16 @@ services:
|
|||||||
- '@prestashop.core.grid.filter.form_factory'
|
- '@prestashop.core.grid.filter.form_factory'
|
||||||
- '@prestashop.core.hook.dispatcher'
|
- '@prestashop.core.hook.dispatcher'
|
||||||
|
|
||||||
|
websmid.customhtml.grid.position.customhtmlgroup_position_definition:
|
||||||
|
public: true
|
||||||
|
class: 'PrestaShop\PrestaShop\Core\Grid\Position\PositionDefinition'
|
||||||
|
arguments:
|
||||||
|
- 'ws_customhtml_group'
|
||||||
|
- 'id_ws_customhtml_group'
|
||||||
|
- 'ordering'
|
||||||
|
- ~
|
||||||
|
- 0
|
||||||
|
|
||||||
websmid.customhtml.grid.definition.factory.customhtmlblock_definition_factory:
|
websmid.customhtml.grid.definition.factory.customhtmlblock_definition_factory:
|
||||||
public: true
|
public: true
|
||||||
class: 'Websmid\CustomHtml\Grid\Definition\Factory\CustomHtmlBlockGridDefinitionFactory'
|
class: 'Websmid\CustomHtml\Grid\Definition\Factory\CustomHtmlBlockGridDefinitionFactory'
|
||||||
|
|||||||
@@ -39,6 +39,12 @@ customhtmlgroup_delete:
|
|||||||
defaults:
|
defaults:
|
||||||
_controller: 'Websmid\CustomHtml\Controller\CustomHtmlGroupController::deleteAction'
|
_controller: 'Websmid\CustomHtml\Controller\CustomHtmlGroupController::deleteAction'
|
||||||
|
|
||||||
|
customhtmlgroup_update_position:
|
||||||
|
path: ws_customhtml/group/update-position
|
||||||
|
methods: [POST]
|
||||||
|
defaults:
|
||||||
|
_controller: 'Websmid\CustomHtml\Controller\CustomHtmlGroupController::updatePositionAction'
|
||||||
|
|
||||||
customhtmlblock_index:
|
customhtmlblock_index:
|
||||||
path: ws_customhtml/block
|
path: ws_customhtml/block
|
||||||
methods: [GET]
|
methods: [GET]
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
namespace Websmid\CustomHtml\Controller;
|
namespace Websmid\CustomHtml\Controller;
|
||||||
|
|
||||||
use Exception;
|
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\Controller\Admin\FrameworkBundleAdminController;
|
||||||
use PrestaShopBundle\Security\Voter\PageVoter;
|
use PrestaShopBundle\Security\Voter\PageVoter;
|
||||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
@@ -103,6 +106,27 @@ class CustomHtmlGroupController extends FrameworkBundleAdminController
|
|||||||
return $this->redirectToRoute('customhtmlgroup_index');
|
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
|
private function getToolbarButtons(?string $securitySubject): array
|
||||||
{
|
{
|
||||||
$toolbarButtons = [];
|
$toolbarButtons = [];
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Websmid\CustomHtml\Form\DataHandler;
|
|||||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
||||||
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
||||||
use Context;
|
use Context;
|
||||||
|
use Db;
|
||||||
use Hook;
|
use Hook;
|
||||||
use Module;
|
use Module;
|
||||||
use Validate;
|
use Validate;
|
||||||
@@ -17,7 +18,7 @@ final class CustomHtmlGroupDataHandler implements FormDataHandlerInterface
|
|||||||
$group->title = (string) $formData['title'];
|
$group->title = (string) $formData['title'];
|
||||||
$group->hook_name = (string) $formData['hook_name'];
|
$group->hook_name = (string) $formData['hook_name'];
|
||||||
$group->active = (int) $formData['active'];
|
$group->active = (int) $formData['active'];
|
||||||
$group->ordering = 0;
|
$group->ordering = $this->getNextOrdering();
|
||||||
|
|
||||||
if (!$group->add()) {
|
if (!$group->add()) {
|
||||||
return false;
|
return false;
|
||||||
@@ -28,6 +29,15 @@ final class CustomHtmlGroupDataHandler implements FormDataHandlerInterface
|
|||||||
return $group->id;
|
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)
|
public function update($id, array $formData)
|
||||||
{
|
{
|
||||||
$group = new CustomHtmlGroup((int) $id);
|
$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\Action\Type\SimpleGridAction;
|
||||||
use PrestaShop\PrestaShop\Core\Grid\Column\ColumnCollection;
|
use PrestaShop\PrestaShop\Core\Grid\Column\ColumnCollection;
|
||||||
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\ActionColumn;
|
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\Column\Type\DataColumn;
|
||||||
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\AbstractGridDefinitionFactory;
|
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\AbstractGridDefinitionFactory;
|
||||||
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\DeleteActionTrait;
|
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\DeleteActionTrait;
|
||||||
@@ -81,6 +82,16 @@ final class CustomHtmlGroupGridDefinitionFactory extends AbstractGridDefinitionF
|
|||||||
->setName($this->trans('Active', [], 'Admin.Global'))
|
->setName($this->trans('Active', [], 'Admin.Global'))
|
||||||
->setOptions(['field' => 'active'])
|
->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(
|
->add(
|
||||||
(new DataColumn('date_add'))
|
(new DataColumn('date_add'))
|
||||||
->setName($this->trans('Date added', [], 'Admin.Global'))
|
->setName($this->trans('Date added', [], 'Admin.Global'))
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ final class CustomHtmlGroupFilters extends Filters
|
|||||||
return [
|
return [
|
||||||
'limit' => 10,
|
'limit' => 10,
|
||||||
'offset' => 0,
|
'offset' => 0,
|
||||||
'orderBy' => 'wchg.id_ws_customhtml_group',
|
'orderBy' => 'position',
|
||||||
'sortOrder' => 'asc',
|
'sortOrder' => 'asc',
|
||||||
'filters' => [],
|
'filters' => [],
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ final class CustomHtmlGroupQueryBuilder extends AbstractDoctrineQueryBuilder
|
|||||||
'wchg.`title`',
|
'wchg.`title`',
|
||||||
'wchg.`hook_name`',
|
'wchg.`hook_name`',
|
||||||
'wchg.`active`',
|
'wchg.`active`',
|
||||||
|
'wchg.`ordering`',
|
||||||
'wchg.`date_add`',
|
'wchg.`date_add`',
|
||||||
'(SELECT COUNT(*) FROM `' . $this->dbPrefix . 'ws_customhtml` wch
|
'(SELECT COUNT(*) FROM `' . $this->dbPrefix . 'ws_customhtml` wch
|
||||||
WHERE wch.`id_ws_customhtml_group` = wchg.`id_ws_customhtml_group`) AS blocks_count'
|
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);
|
$this->applyFilters($qb, $searchCriteria);
|
||||||
|
|
||||||
|
$orderBy = $searchCriteria->getOrderBy() === 'position' ? 'wchg.ordering' : $searchCriteria->getOrderBy();
|
||||||
|
|
||||||
$qb
|
$qb
|
||||||
->orderBy($searchCriteria->getOrderBy(), $searchCriteria->getOrderWay())
|
->orderBy($orderBy, $searchCriteria->getOrderWay())
|
||||||
->setFirstResult($searchCriteria->getOffset() ?? 0)
|
->setFirstResult($searchCriteria->getOffset() ?? 0)
|
||||||
->setMaxResults($searchCriteria->getLimit());
|
->setMaxResults($searchCriteria->getLimit());
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if (!defined('_PS_VERSION_')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
function upgrade_module_1_11_0($module)
|
||||||
|
{
|
||||||
|
$groups = Db::getInstance()->executeS(
|
||||||
|
'SELECT `id_ws_customhtml_group` FROM `' . _DB_PREFIX_ . 'ws_customhtml_group` ORDER BY `id_ws_customhtml_group` ASC'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($groups as $index => $group) {
|
||||||
|
if (!Db::getInstance()->update(
|
||||||
|
'ws_customhtml_group',
|
||||||
|
['ordering' => (int) $index],
|
||||||
|
'`id_ws_customhtml_group` = ' . (int) $group['id_ws_customhtml_group']
|
||||||
|
)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
customhtmlgroupGrid.addExtension(new window.prestashop.component.GridExtensions.FiltersResetExtension());
|
customhtmlgroupGrid.addExtension(new window.prestashop.component.GridExtensions.FiltersResetExtension());
|
||||||
customhtmlgroupGrid.addExtension(new window.prestashop.component.GridExtensions.ColumnTogglingExtension());
|
customhtmlgroupGrid.addExtension(new window.prestashop.component.GridExtensions.ColumnTogglingExtension());
|
||||||
customhtmlgroupGrid.addExtension(new window.prestashop.component.GridExtensions.SubmitRowActionExtension());
|
customhtmlgroupGrid.addExtension(new window.prestashop.component.GridExtensions.SubmitRowActionExtension());
|
||||||
|
customhtmlgroupGrid.addExtension(new window.prestashop.component.GridExtensions.PositionExtension());
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
+1
-1
@@ -29,7 +29,7 @@ class Ws_CustomHtml extends Module implements WidgetInterface
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->name = 'ws_customhtml';
|
$this->name = 'ws_customhtml';
|
||||||
$this->version = '1.10.0';
|
$this->version = '1.11.0';
|
||||||
$this->author = 'Isabelle Oving-Anno | De Websmid BV';
|
$this->author = 'Isabelle Oving-Anno | De Websmid BV';
|
||||||
$this->need_instance = 0;
|
$this->need_instance = 0;
|
||||||
$this->bootstrap = true;
|
$this->bootstrap = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user