forked from cleverage/processuibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessExecutionCrudController.php
163 lines (147 loc) · 6.14 KB
/
ProcessExecutionCrudController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/UiProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\UiProcessBundle\Controller\Admin;
use CleverAge\UiProcessBundle\Admin\Field\ContextField;
use CleverAge\UiProcessBundle\Admin\Field\EnumField;
use CleverAge\UiProcessBundle\Admin\Filter\ProcessExecutionDurationFilter;
use CleverAge\UiProcessBundle\Entity\ProcessExecution;
use CleverAge\UiProcessBundle\Repository\ProcessExecutionRepository;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[IsGranted('ROLE_USER')]
class ProcessExecutionCrudController extends AbstractCrudController
{
public function __construct(
private readonly ProcessExecutionRepository $processExecutionRepository,
private readonly string $logDirectory,
) {
}
public static function getEntityFqcn(): string
{
return ProcessExecution::class;
}
public function configureFields(string $pageName): iterable
{
return [
TextField::new('code'),
EnumField::new('status'),
DateTimeField::new('startDate')->setFormat('Y/M/dd H:mm:ss'),
DateTimeField::new('endDate')->setFormat('Y/M/dd H:mm:ss'),
TextField::new('source')->setTemplatePath('@CleverAgeUiProcess/admin/field/process_source.html.twig'),
TextField::new('target')->setTemplatePath('@CleverAgeUiProcess/admin/field/process_target.html.twig'),
TextField::new('duration')->formatValue(function ($value, ProcessExecution $entity) {
return $entity->duration(); // returned format can be changed here
}),
ArrayField::new('report')->setTemplatePath('@CleverAgeUiProcess/admin/field/report.html.twig'),
ContextField::new('context'),
];
}
public function configureCrud(Crud $crud): Crud
{
$crud->showEntityActionsInlined();
$crud->setDefaultSort(['startDate' => 'DESC']);
return $crud;
}
public function configureActions(Actions $actions): Actions
{
return Actions::new()
->add(
Crud::PAGE_INDEX,
Action::new('showLogs', false, 'fas fa-eye')
->setHtmlAttributes(
[
'data-bs-toggle' => 'tooltip',
'data-bs-placement' => 'top',
'title' => 'Show logs stored in database',
]
)
->linkToCrudAction('showLogs')
->displayIf(fn (ProcessExecution $entity) => $this->processExecutionRepository->hasLogs($entity))
)->add(
Crud::PAGE_INDEX,
Action::new('downloadLogfile', false, 'fas fa-download')
->setHtmlAttributes(
[
'data-bs-toggle' => 'tooltip',
'data-bs-placement' => 'top',
'title' => 'Download log file',
]
)
->linkToCrudAction('downloadLogFile')
->displayIf(fn (ProcessExecution $entity) => file_exists($this->getLogFilePath($entity)))
);
}
public function showLogs(AdminContext $adminContext): RedirectResponse
{
/** @var AdminUrlGenerator $adminUrlGenerator */
$adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);
$url = $adminUrlGenerator
->setController(LogRecordCrudController::class)
->setAction('index')
->setEntityId(null)
->set(
'filters',
[
'process' => [
'comparison' => '=',
'value' => $this->getContext()?->getEntity()->getInstance()->getId(),
],
]
)
->generateUrl();
return $this->redirect($url);
}
public function downloadLogFile(
AdminContext $context,
): Response {
/** @var ProcessExecution $processExecution */
$processExecution = $context->getEntity()->getInstance();
$filepath = $this->getLogFilePath($processExecution);
$basename = basename($filepath);
$content = file_get_contents($filepath);
if (false === $content) {
throw new NotFoundHttpException('Log file not found.');
}
$response = new Response($content);
$response->headers->set('Content-Type', 'text/plain; charset=utf-8');
$response->headers->set('Content-Disposition', "attachment; filename=\"$basename\"");
return $response;
}
public function configureFilters(Filters $filters): Filters
{
return $filters
->add('code')
->add('startDate')
->add(
ProcessExecutionDurationFilter::new('duration', 'Duration (in seconds)')
->setFormTypeOption('mapped', false)
);
}
private function getLogFilePath(ProcessExecution $processExecution): string
{
return $this->logDirectory.
\DIRECTORY_SEPARATOR.$processExecution->code.
\DIRECTORY_SEPARATOR.$processExecution->logFilename
;
}
}