mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
[WIP] get default data from saved exports for center and export steps
This commit is contained in:
@@ -11,27 +11,127 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Export;
|
||||
|
||||
use Chill\MainBundle\Entity\SavedExport;
|
||||
use Chill\MainBundle\Form\Type\Export\ExportType;
|
||||
use Chill\MainBundle\Form\Type\Export\FilterType;
|
||||
use Chill\MainBundle\Form\Type\Export\PickCenterType;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
|
||||
final readonly class ExportFormHelper
|
||||
{
|
||||
public function __construct(
|
||||
private ExportManager $exportManager,
|
||||
private AuthorizationHelperForCurrentUserInterface $authorizationHelper,
|
||||
private ExportManager $exportManager,
|
||||
private FormFactoryInterface $formFactory,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<"centers"> $steps
|
||||
*/
|
||||
public function getDefaultData(string $step, ExportInterface $export): array
|
||||
public function getDefaultData(string $step, ExportInterface|DirectExportInterface $export, array $options = []): array
|
||||
{
|
||||
$data = [];
|
||||
return match ($step) {
|
||||
'centers', 'generate_centers' => ['centers' => $this->authorizationHelper->getReachableCenters($export->requiredRole())],
|
||||
'export', 'generate_export' => ['export' => $this->getDefaultDataStepExport($export, $options)],
|
||||
'formatter', 'generate_formatter' => [],
|
||||
default => throw new \LogicException("step not allowed : " . $step),
|
||||
};
|
||||
}
|
||||
|
||||
if ($step === 'centers') {
|
||||
$data['centers'] = $this->authorizationHelper->getReachableCenters($export->requiredRole());
|
||||
private function getDefaultDataStepExport(ExportInterface|DirectExportInterface $export, array $options): array
|
||||
{
|
||||
$data = [
|
||||
ExportType::EXPORT_KEY => $export->getFormDefaultData(),
|
||||
ExportType::FILTER_KEY => [],
|
||||
ExportType::AGGREGATOR_KEY => [],
|
||||
ExportType::PICK_FORMATTER_KEY => [],
|
||||
];
|
||||
|
||||
$filters = $this->exportManager->getFiltersApplyingOn($export, $options['picked_centers']);
|
||||
foreach ($filters as $alias => $filter) {
|
||||
$data[ExportType::FILTER_KEY][$alias] = [
|
||||
FilterType::ENABLED_FIELD => false,
|
||||
'form' => $filter->getFormDefaultData()
|
||||
];
|
||||
}
|
||||
|
||||
$aggregators = $this->exportManager
|
||||
->getAggregatorsApplyingOn($export, $options['picked_centers']);
|
||||
foreach ($aggregators as $alias => $aggregator) {
|
||||
$data[ExportType::AGGREGATOR_KEY][$alias] = [
|
||||
'enabled' => false,
|
||||
'form' => $aggregator->getFormDefaultData(),
|
||||
];
|
||||
}
|
||||
|
||||
$allowedFormatters = $this->exportManager
|
||||
->getFormattersByTypes($export->getAllowedFormattersTypes());
|
||||
$choices = [];
|
||||
foreach (array_keys(iterator_to_array($allowedFormatters)) as $alias) {
|
||||
$choices[] = $alias;
|
||||
}
|
||||
|
||||
$data[ExportType::PICK_FORMATTER_KEY]['alias'] = match (count($choices)) {
|
||||
1 => $choices[0],
|
||||
default => null,
|
||||
};
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function savedExportDataToFormData(
|
||||
SavedExport $savedExport,
|
||||
string $step,
|
||||
array $formOptions = [],
|
||||
): array {
|
||||
return match ($step) {
|
||||
'centers', 'generate_centers' => $this->savedExportDataToFormDataStepCenter($savedExport),
|
||||
'export', 'generate_export' => $this->savedExportDataToFormDataStepExport($savedExport, $formOptions),
|
||||
'formatter', 'generate_formatter' => [],
|
||||
default => throw new \LogicException("this step is not allowed: " . $step),
|
||||
};
|
||||
}
|
||||
|
||||
private function savedExportDataToFormDataStepCenter(
|
||||
SavedExport $savedExport,
|
||||
): array {
|
||||
$builder = $this->formFactory
|
||||
->createBuilder(
|
||||
FormType::class,
|
||||
[],
|
||||
[
|
||||
'csrf_protection' => false,
|
||||
]
|
||||
);
|
||||
|
||||
$builder->add('centers', PickCenterType::class, [
|
||||
'export_alias' => $savedExport->getExportAlias(),
|
||||
]);
|
||||
$form = $builder->getForm();
|
||||
$form->submit($savedExport->getOptions()['centers']);
|
||||
|
||||
return $form->getData();
|
||||
}
|
||||
|
||||
private function savedExportDataToFormDataStepExport(
|
||||
SavedExport $savedExport,
|
||||
array $formOptions
|
||||
): array {
|
||||
$builder = $this->formFactory
|
||||
->createBuilder(
|
||||
FormType::class,
|
||||
[],
|
||||
[
|
||||
'csrf_protection' => false,
|
||||
]
|
||||
);
|
||||
|
||||
$builder->add('export', ExportType::class, [
|
||||
'export_alias' => $savedExport->getExportAlias(), ...$formOptions
|
||||
]);
|
||||
$form = $builder->getForm();
|
||||
$form->submit($savedExport->getOptions()['export']);
|
||||
|
||||
return $form->getData();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user