Skip to content

Commit

Permalink
refactor(datatable): querySearch datatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidmattei committed Jul 23, 2024
1 parent c170c12 commit aed4e07
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 103 deletions.

This file was deleted.

47 changes: 25 additions & 22 deletions EMS/core-bundle/src/Controller/QuerySearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@

namespace EMS\CoreBundle\Controller;

use EMS\CommonBundle\Contracts\Log\LocalizedLoggerInterface;
use EMS\CoreBundle\Core\DataTable\DataTableFactory;
use EMS\CoreBundle\DataTable\Type\QuerySearchDataTableType;
use EMS\CoreBundle\Entity\QuerySearch;
use EMS\CoreBundle\Form\Data\EntityTable;
use EMS\CoreBundle\Form\Data\TableAbstract;
use EMS\CoreBundle\Form\Form\QuerySearchType;
use EMS\CoreBundle\Form\Form\TableType;
use EMS\CoreBundle\Service\QuerySearchService;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use function Symfony\Component\Translation\t;

final class QuerySearchController extends AbstractController
{
use CoreControllerTrait;

public function __construct(
private readonly LoggerInterface $logger,
private readonly LocalizedLoggerInterface $logger,
private readonly QuerySearchService $querySearchService,
private readonly DataTableFactory $dataTableFactory,
private readonly string $templateNamespace
Expand All @@ -33,30 +35,31 @@ public function index(Request $request): Response
{
$table = $this->dataTableFactory->create(QuerySearchDataTableType::class);

$form = $this->createForm(TableType::class, $table);
$form = $this->createForm(TableType::class, $table, [
'reorder_label' => t('type.reorder', ['type' => 'query_search'], 'emsco-core'),
]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if ($form instanceof Form && ($action = $form->getClickedButton()) instanceof SubmitButton) {
switch ($action->getName()) {
case EntityTable::DELETE_ACTION:
$this->querySearchService->deleteByIds($table->getSelected());
break;
case TableType::REORDER_ACTION:
$newOrder = TableType::getReorderedKeys($form->getName(), $request);
$this->querySearchService->reorderByIds($newOrder);
break;
default:
$this->logger->error('log.controller.query_search.unknown_action');
}
} else {
$this->logger->error('log.controller.query_search.unknown_action');
}
match ($this->getClickedButtonName($form)) {
TableAbstract::DELETE_ACTION => $this->querySearchService->deleteByIds($table->getSelected()),
TableType::REORDER_ACTION => $this->querySearchService->reorderByIds(
ids: TableType::getReorderedKeys($form->getName(), $request)
),
default => $this->logger->messageError(t('log.error.invalid_table_action', [], 'emsco-core'))
};

return $this->redirectToRoute('ems_core_query_search_index');
}

return $this->render("@$this->templateNamespace/query-search/index.html.twig", [
return $this->render("@$this->templateNamespace/crud/overview.html.twig", [
'form' => $form->createView(),
'icon' => 'fa fa-list-alt',
'title' => t('admin.query_search.overview', [], 'emsco-core'),
'breadcrumb' => [
'admin' => t('admin.label', [], 'emsco-core'),
'page' => t('admin.query_search.plural', [], 'emsco-core'),
],
]);
}

Expand Down
34 changes: 29 additions & 5 deletions EMS/core-bundle/src/DataTable/Type/QuerySearchDataTableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use EMS\CoreBundle\Roles;
use EMS\CoreBundle\Service\QuerySearchService;

use function Symfony\Component\Translation\t;

class QuerySearchDataTableType extends AbstractEntityTableType
{
public function __construct(QuerySearchService $entityService)
Expand All @@ -19,12 +21,34 @@ public function __construct(QuerySearchService $entityService)

public function build(EntityTable $table): void
{
$table->addColumn('query_search.index.column.label', 'label');
$table->addColumn('query_search.index.column.name', 'name');
$table->addItemGetAction('ems_core_query_search_edit', 'query_search.actions.edit', 'pencil');
$table->addItemPostAction('ems_core_query_search_delete', 'query_search.actions.delete', 'trash', 'query_search.actions.delete_confirm');
$table->addTableAction(TableAbstract::DELETE_ACTION, 'fa fa-trash', 'query_search.actions.delete_selected', 'query_search.actions.delete_selected_confirm');
$table->setDefaultOrder('label');

$table->addColumn(t('field.label', [], 'emsco-core'), 'label');
$table->addColumn(t('field.name', [], 'emsco-core'), 'name');

$table->addItemGetAction(
route: 'ems_core_query_search_edit',
labelKey: t('action.edit', [], 'emsco-core'),
icon: 'pencil'
);
$table->addItemPostAction(
route: 'ems_core_query_search_delete',
labelKey: t('action.delete', [], 'emsco-core'),
icon: 'trash',
messageKey: t('type.delete_confirm', ['type' => 'query_search'], 'emsco-core')
)->setButtonType('outline-danger');

$table->addToolbarAction(
label: t('action.add', [], 'emsco-core'),
icon: 'fa fa-plus',
routeName: 'ems_core_query_search_add'
);
$table->addTableAction(
name: TableAbstract::DELETE_ACTION,
icon: 'fa fa-trash',
labelKey: t('action.delete_selected', [], 'emsco-core'),
confirmationKey: t('type.delete_selected_confirm', ['type' => 'query_search'], 'emsco-core')
)->setCssClass('btn btn-outline-danger');
}

public function getRoles(): array
Expand Down
2 changes: 1 addition & 1 deletion EMS/core-bundle/src/Resources/config/controllers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@
<tag name="controller.service_arguments"/>
</service>
<service id="EMS\CoreBundle\Controller\QuerySearchController" public="true">
<argument type="service" id="logger" />
<argument type="service" id="emsco.logger" />
<argument type="service" id="ems.service.query_search" />
<argument type="service" id="emsco.data_table.factory" />
<argument>%ems_core.template_namespace%</argument>
Expand Down
16 changes: 0 additions & 16 deletions EMS/core-bundle/src/Resources/translations/EMSCoreBundle.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ log:
controller:
channel:
unknown_action: 'Unknown action'
query_search:
unknown_action: 'Unknown action'
release:
unknown_action: 'Unknown action'
uploaded-file-logs:
Expand Down Expand Up @@ -582,26 +580,12 @@ form_submissions:
success: 'Form submission with id "%id%" has been deleted!'
title: 'Form Submissions'
query_search:
actions:
delete: Delete
delete_confirm: 'Delete the "%label%" QuerySearch?'
delete_selected: 'Delete selected'
delete_selected_confirm: 'Delete all selected QuerySearch?'
edit: Edit
add:
save: Create
title: 'Add a QuerySearch'
edit:
save: Save
title: 'Edit a QuerySearch'
index:
column:
alias: Alias
label: Label
name: Name
public: 'Public access'
reorder: 'Reorder QuerySearch'
title: 'List of QuerySearch'
menu: QuerySearch
release:
actions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ action:
reorder: Reorder
admin:
channel:
plural: Channels
overview: 'List of channels'
field:
public: 'Public Access'
label: 'Admin'
overview: 'List of channels'
plural: Channels
label: Admin
query_search:
overview: 'List of QuerySearch'
plural: QuerySearch
content_type:
plural: 'Content Types'
core:
Expand Down Expand Up @@ -55,20 +58,23 @@ table:
type:
delete_confirm: |-
{type, select,
channel {Delete the "{label}" channel?}
draft {Modifications made in this draft will be lost}
trash {Delete trash "{label}" ?}
other {Are you sure?}
channel {Delete the "{label}" channel?}
draft {Modifications made in this draft will be lost}
trash {Delete trash "{label}" ?}
query_search {Delete the "{label}" QuerySearch?}
other {Are you sure?}
}
delete_selected_confirm: |-
{type, select,
channel {Delete all selected channels?}
draft {All modifications made on selected drafts will lost?}
trash {Delete all selected trashes?}
other {Delete all selected?}
channel {Delete all selected channels?}
draft {All modifications made on selected drafts will lost?}
trash {Delete all selected trashes?}
query_search {Delete all selected QuerySearch?}
other {Delete all selected?}
}
reorder: |-
{type, select,
channel {Reorder channels}
other {Reorder}
channel {Reorder channels}
query_search {Reorder QuerySearch}
other {Reorder}
}
23 changes: 0 additions & 23 deletions EMS/core-bundle/src/Resources/views/query-search/index.html.twig

This file was deleted.

0 comments on commit aed4e07

Please sign in to comment.