-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSecurityContractCrudController.php
More file actions
122 lines (105 loc) · 4.84 KB
/
Copy pathSecurityContractCrudController.php
File metadata and controls
122 lines (105 loc) · 4.84 KB
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
<?php
namespace App\Controller\Admin;
use App\Entity\SecurityContract;
use App\Service\ServiceAgreementSyncService;
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminRoute;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\UrlField;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Translation\TranslatableMessage;
class SecurityContractCrudController extends AbstractCrudController
{
public function __construct(
private readonly ServiceAgreementSyncService $syncService,
private readonly AdminUrlGenerator $adminUrlGenerator,
) {
}
public static function getEntityFqcn(): string
{
return SecurityContract::class;
}
#[\Override]
public function configureCrud(Crud $crud): Crud
{
return $crud
->setDefaultSort(['project.name' => 'ASC'])
->setSearchFields(['project.name', 'hostingProvider', 'serverSize'])
->showEntityActionsInlined()
->setPageTitle(Crud::PAGE_INDEX, 'Service Agreements')
->setHelp(Crud::PAGE_INDEX, 'Service agreements are synced from Economics. Click "Sync all" to update.');
}
#[\Override]
public function configureActions(Actions $actions): Actions
{
return $actions
->disable(Action::DELETE, Action::NEW, Action::EDIT)
->add(Crud::PAGE_INDEX, Action::DETAIL)
->add(Crud::PAGE_INDEX, $this->createSyncAllAction());
}
#[\Override]
public function configureFields(string $pageName): iterable
{
yield FormField::addFieldset('Project');
yield BooleanField::new('active')->renderAsSwitch(false)->setColumns(2);
yield BooleanField::new('eol')->setLabel('EOL')->renderAsSwitch(false)->setColumns(2);
yield TextField::new('project.name')->setLabel('Project')->setColumns(8);
yield TextField::new('project.leantimeId')->setLabel('Leantime ID')->hideOnIndex();
yield TextField::new('projectGitRepos')->setLabel('GitHub repos')->hideOnIndex();
yield TextField::new('hostingProvider');
yield FormField::addFieldset('Links');
yield UrlField::new('project.leantimeUrl')->setLabel('Leantime URL')->hideOnIndex();
yield UrlField::new('documentUrl')->setLabel('Document URL')->hideOnIndex();
yield FormField::addFieldset('Contact');
yield TextField::new('clientContactName')->hideOnIndex();
yield TextField::new('clientContactEmail')->hideOnIndex();
yield FormField::addFieldset('Budget');
yield NumberField::new('monthlyPrice')->setTextAlign('right')->setColumns(6);
yield FormField::addFieldset('Infrastructure');
yield BooleanField::new('dedicatedServer')->renderAsSwitch(false)->hideOnIndex();
yield TextField::new('serverSize')->hideOnIndex();
yield FormField::addFieldset('Validity');
yield DateField::new('validFrom')->setColumns(6);
yield DateField::new('validTo')->setColumns(6);
}
#[AdminRoute]
public function syncAll(): RedirectResponse
{
try {
$result = $this->syncService->syncAll();
$this->addFlash('info', sprintf('Synced %d projects.', $result['projects']));
if (!empty($result['unmatchedRepoNames'])) {
$this->addFlash('warning', sprintf(
'Could not link %d GitHub repo name(s) to existing GitRepo entries: %s',
count($result['unmatchedRepoNames']),
implode(', ', $result['unmatchedRepoNames']),
));
}
} catch (\Throwable $e) {
$this->addFlash('error', sprintf('An error occurred while syncing: %s', $e->getMessage()));
}
return $this->redirect(
$this->adminUrlGenerator
->setController(static::class)
->setAction(Crud::PAGE_INDEX)
->generateUrl()
);
}
private function createSyncAllAction(): Action
{
return Action::new('syncAll', new TranslatableMessage('Sync all'), 'fa fa-rotate')
->createAsGlobalAction()
->linkToCrudAction('syncAll')
->setHtmlAttributes([
'onclick' => "const i=this.querySelector('i');if(i){i.classList.add('fa-spin')}this.style.pointerEvents='none';this.style.opacity='0.6'",
]);
}
}