Skip to content

Commit

Permalink
refactor(datatable): jobs and extend dataTableTrait (ems-project#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidmattei authored Jul 28, 2024
1 parent a2545df commit 45a388c
Show file tree
Hide file tree
Showing 36 changed files with 444 additions and 593 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% block jobStatus %}
{% set p = line.data.progress %}
<div class="progress">
<div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="{{ p }}" aria-valuemin="0" aria-valuemax="100" style="{{ "width: #{p}%;" }}">
{{ "#{p}%" }}
</div>
</div>
<div>{{ line.data.status }}</div>
{% endblock jobStatus %}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
{%- endif -%}
{%- endif -%}
{% if confirm is defined and confirm != null %}
<div class="btn-group">
<div class="btn-group pull-right">
<button type="button" class="{{ confirm_class|default('btn btn-outline-danger') }} dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{%- if icon -%}
<span class="{{ icon }}"></span>&nbsp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
{% endif %}
{% if toolbar is defined or (table.sortable and table.supportsTableActions) or table.toolbarActions|length > 0 %}
<div class="box-header with-border">
<div class="btn-group pull-right">
<div class="btn-group pull-right">
{% if toolbar is defined %}
{{ toolbar }}
{% endif %}
Expand All @@ -300,8 +300,8 @@
<i class="{{ toolbarAction.icon }}"></i>&nbsp;{{ toolbarAction.labelKey|trans }}
</a>
{% endfor %}
{% if table.sortable and table.supportsTableActions %}
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#{{ table.attributeName|e('html_attr') }}_modal_reorder">
{% if attribute(form, 'reorderAction') is defined %}
<button type="button" class="btn btn-sm btn-default" data-toggle="modal" data-target="#{{ table.attributeName|e('html_attr') }}_modal_reorder">
<i class="fa fa-reorder"></i>&nbsp;{{ form.reorderAction.vars.label|trans }}
</button>
{% endif %}
Expand Down Expand Up @@ -360,24 +360,22 @@
{% if table.supportsTableActions or toolbar is defined %}
<div class="box-footer with-border">
{% if table.supportsTableActions %}
<div class="btn-group">
{% for action in table.tableActions %}
{{ form_widget(attribute(form, action.name)) }}
{% endfor %}
</div>
{% for action in table.tableActions %}
{{ form_widget(attribute(form, action.name)) }}
{% endfor %}
{% endif %}
{% if toolbar is defined %}
<div class="btn-group">
{{ toolbar }}
</div>
{% endif %}
{% for toolbarAction in table.toolbarActions %}
<div class="btn-group">
<div class="btn-group">
{% for toolbarAction in table.toolbarActions %}
<a class="{{ toolbarAction.cssClass }}" href="{{ path(toolbarAction.routeName, toolbarAction.routeParams) }}">
<i class="{{ toolbarAction.icon }}"></i>&nbsp;{{ toolbarAction.labelKey|trans }}
</a>
</div>
{% endfor %}
{% endfor %}
</div>
</div>
{% endif %}
</div>
Expand Down Expand Up @@ -407,8 +405,8 @@
</div>
<div class="modal-footer">
<div class="btn-group">
{{ form_widget(form.reorderAction, { 'attr': { 'class': 'btn btn-primary' } }) }}
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-close"></i> {{ t('action.close', [], 'emsco-core')|trans }}</button>
{{ form_widget(form.reorderAction, { 'attr': { 'class': 'btn btn-sm btn-primary' } }) }}
<button type="button" class="btn btn-sm btn-default" data-dismiss="modal"><i class="fa fa-close"></i> {{ t('action.close', [], 'emsco-core')|trans }}</button>
</div>
</div>
</div>
Expand Down

This file was deleted.

This file was deleted.

61 changes: 37 additions & 24 deletions EMS/core-bundle/src/Controller/ContentManagement/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace EMS\CoreBundle\Controller\ContentManagement;

use EMS\CommonBundle\Contracts\Log\LocalizedLoggerInterface;
use EMS\CommonBundle\Helper\Text\Encoder;
use EMS\CoreBundle\Controller\CoreControllerTrait;
use EMS\CoreBundle\Core\DataTable\DataTableFactory;
use EMS\CoreBundle\DataTable\Type\Job\JobDataTableType;
use EMS\CoreBundle\Entity\Job;
use EMS\CoreBundle\Form\Data\TableAbstract;
use EMS\CoreBundle\Form\Form\JobType;
use EMS\CoreBundle\Form\Form\TableType;
use EMS\CoreBundle\Helper\EmsCoreResponse;
use EMS\CoreBundle\Service\JobService;
use EMS\Helpers\Standard\Json;
use Psr\Log\LoggerInterface;
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
use SensioLabs\AnsiConverter\Theme\Theme;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -20,32 +25,47 @@
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\User\UserInterface;

use function Symfony\Component\Translation\t;

class JobController extends AbstractController
{
use CoreControllerTrait;

public function __construct(
private readonly LoggerInterface $logger,
private readonly JobService $jobService,
private readonly int $pagingSize,
private readonly DataTableFactory $dataTableFactory,
private readonly LocalizedLoggerInterface $logger,
private readonly bool $triggerJobFromWeb,
private readonly string $templateNamespace
) {
}

public function index(Request $request): Response
{
$size = $this->pagingSize;
$page = $request->query->getInt('page', 1);
$from = ($page - 1) * $size;
$total = $this->jobService->count();
$lastPage = \ceil($total / $size);

return $this->render("@$this->templateNamespace/job/index.html.twig", [
'jobs' => $this->jobService->scroll($size, $from),
'page' => $page,
'size' => $size,
'from' => $from,
'lastPage' => $lastPage,
'paginationPath' => 'job.index',
$table = $this->dataTableFactory->create(JobDataTableType::class);
$form = $this->createForm(TableType::class, $table);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
match ($this->getClickedButtonName($form)) {
TableAbstract::DELETE_ACTION => $this->jobService->deleteByIds(...$table->getSelected()),
JobDataTableType::ACTION_CLEAN => $this->jobService->clean(),
default => $this->logger->messageError(t('log.error.invalid_table_action', [], 'emsco-core'))
};

return $this->redirectToRoute('job.index');
}

return $this->render("@$this->templateNamespace/crud/overview.html.twig", [
'form' => $form->createView(),
'icon' => 'fa fa-file-text-o',
'title' => t('type.title_overview', ['type' => 'job'], 'emsco-core'),
'subTitle' => t('type.title_sub', ['type' => 'job'], 'emsco-core'),
'breadcrumb' => [
'admin' => t('key.admin', [], 'emsco-core'),
'jobs' => t('key.jobs', [], 'emsco-core'),
'page' => t('key.job_logs', [], 'emsco-core'),
],
]);
}

Expand Down Expand Up @@ -96,16 +116,9 @@ public function delete(Job $job): RedirectResponse
return $this->redirectToRoute('job.index');
}

public function clean(): RedirectResponse
{
$this->jobService->clean();

return $this->redirectToRoute('job.index');
}

public function startJob(Job $job, Request $request, UserInterface $user): Response
{
if ($job->getUser() != $user->getUserIdentifier()) {
if ($job->getUser() !== $user->getUserIdentifier()) {
throw new AccessDeniedHttpException();
}

Expand Down
51 changes: 28 additions & 23 deletions EMS/core-bundle/src/Controller/Job/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,64 @@

namespace EMS\CoreBundle\Controller\Job;

use EMS\CommonBundle\Contracts\Log\LocalizedLoggerInterface;
use EMS\CoreBundle\Controller\CoreControllerTrait;
use EMS\CoreBundle\Core\DataTable\DataTableFactory;
use EMS\CoreBundle\Core\Job\ScheduleManager;
use EMS\CoreBundle\DataTable\Type\JobScheduleDataTableType;
use EMS\CoreBundle\DataTable\Type\Job\JobScheduleDataTableType;
use EMS\CoreBundle\Entity\Schedule;
use EMS\CoreBundle\Form\Data\EntityTable;
use EMS\CoreBundle\Form\Data\TableAbstract;
use EMS\CoreBundle\Form\Form\ScheduleType;
use EMS\CoreBundle\Form\Form\TableType;
use EMS\CoreBundle\Routes;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use function Symfony\Component\Translation\t;

final class ScheduleController extends AbstractController
{
use CoreControllerTrait;

public function __construct(
private readonly ScheduleManager $scheduleManager,
private readonly LoggerInterface $logger,
private readonly DataTableFactory $dataTableFactory,
private readonly LocalizedLoggerInterface $logger,
private readonly string $templateNamespace
) {
}

public function index(Request $request): Response
{
$table = $this->dataTableFactory->create(JobScheduleDataTableType::class);
$form = $this->createForm(TableType::class, $table, [
'reorder_label' => t('type.reorder', ['type' => 'job_schedule'], 'emsco-core'),
]);

$form = $this->createForm(TableType::class, $table);
$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->scheduleManager->deleteByIds($table->getSelected());
break;
case TableType::REORDER_ACTION:
$newOrder = TableType::getReorderedKeys($form->getName(), $request);
$this->scheduleManager->reorderByIds($newOrder);
break;
default:
$this->logger->error('log.controller.schedule.unknown_action');
}
} else {
$this->logger->error('log.controller.schedule.unknown_action');
}
match ($this->getClickedButtonName($form)) {
TableAbstract::DELETE_ACTION => $this->scheduleManager->deleteByIds($table->getSelected()),
TableType::REORDER_ACTION => $this->scheduleManager->reorderByIds(
ids: TableType::getReorderedKeys($form->getName(), $request)
),
default => $this->logger->messageError(t('log.error.invalid_table_action', [], 'emsco-core'))
};

return $this->redirectToRoute(Routes::SCHEDULE_INDEX);
}

return $this->render("@$this->templateNamespace/schedule/index.html.twig", [
return $this->render("@$this->templateNamespace/crud/overview.html.twig", [
'form' => $form->createView(),
'icon' => 'fa fa-calendar-o',
'title' => t('type.title_overview', ['type' => 'job_schedule'], 'emsco-core'),
'subTitle' => t('type.title_sub', ['type' => 'job_schedule'], 'emsco-core'),
'breadcrumb' => [
'admin' => t('key.admin', [], 'emsco-core'),
'jobs' => t('key.jobs', [], 'emsco-core'),
'page' => t('key.schedule', [], 'emsco-core'),
],
]);
}

Expand Down
Loading

0 comments on commit 45a388c

Please sign in to comment.