mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-30 19:43: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();
|
||||
}
|
||||
}
|
||||
|
@@ -12,7 +12,6 @@ declare(strict_types=1);
|
||||
namespace Chill\MainBundle\Export;
|
||||
|
||||
use Chill\MainBundle\Form\Type\Export\ExportType;
|
||||
use Chill\MainBundle\Form\Type\Export\PickCenterType;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Generator;
|
||||
@@ -131,9 +130,9 @@ class ExportManager
|
||||
*
|
||||
* @internal This class check the interface implemented by export, and, if ´ListInterface´ is used, return an empty array
|
||||
*
|
||||
* @return AggregatorInterface[] a \Generator that contains aggretagors. The key is the filter's alias
|
||||
* @return null|iterable<string, AggregatorInterface> a \Generator that contains aggretagors. The key is the filter's alias
|
||||
*/
|
||||
public function &getAggregatorsApplyingOn(ExportInterface $export, ?array $centers = null)
|
||||
public function &getAggregatorsApplyingOn(ExportInterface $export, ?array $centers = null): ?iterable
|
||||
{
|
||||
if ($export instanceof ListInterface) {
|
||||
return;
|
||||
@@ -149,7 +148,7 @@ class ExportManager
|
||||
}
|
||||
}
|
||||
|
||||
public function addExportElementsProvider(ExportElementsProviderInterface $provider, $prefix)
|
||||
public function addExportElementsProvider(ExportElementsProviderInterface $provider, string $prefix): void
|
||||
{
|
||||
foreach ($provider->getExportElements() as $suffix => $element) {
|
||||
$alias = $prefix . '_' . $suffix;
|
||||
@@ -173,23 +172,16 @@ class ExportManager
|
||||
* add a formatter.
|
||||
*
|
||||
* @internal used by DI
|
||||
*
|
||||
* @param string $alias
|
||||
*/
|
||||
public function addFormatter(FormatterInterface $formatter, $alias)
|
||||
public function addFormatter(FormatterInterface $formatter, string $alias)
|
||||
{
|
||||
$this->formatters[$alias] = $formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a response which contains the requested data.
|
||||
*
|
||||
* @param string $exportAlias
|
||||
* @param mixed[] $data
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function generate($exportAlias, array $pickedCentersData, array $data, array $formatterData)
|
||||
public function generate(string $exportAlias, array $pickedCentersData, array $data, array $formatterData): Response
|
||||
{
|
||||
$export = $this->getExport($exportAlias);
|
||||
$centers = $this->getPickedCenters($pickedCentersData);
|
||||
@@ -288,7 +280,11 @@ class ExportManager
|
||||
return $this->aggregators[$alias];
|
||||
}
|
||||
|
||||
public function getAggregators(array $aliases)
|
||||
/**
|
||||
* @param array $aliases
|
||||
* @return iterable<string, AggregatorInterface>
|
||||
*/
|
||||
public function getAggregators(array $aliases): iterable
|
||||
{
|
||||
foreach ($aliases as $alias) {
|
||||
yield $alias => $this->getAggregator($alias);
|
||||
@@ -296,9 +292,11 @@ class ExportManager
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[] the existing type for known exports
|
||||
* Get the types for known exports
|
||||
*
|
||||
* @return list<string> the existing type for known exports
|
||||
*/
|
||||
public function getExistingExportsTypes()
|
||||
public function getExistingExportsTypes(): array
|
||||
{
|
||||
$existingTypes = [];
|
||||
|
||||
@@ -317,10 +315,8 @@ class ExportManager
|
||||
* @param string $alias
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*
|
||||
* @return ExportInterface
|
||||
*/
|
||||
public function getExport($alias)
|
||||
public function getExport($alias): ExportInterface|DirectExportInterface
|
||||
{
|
||||
if (!array_key_exists($alias, $this->exports)) {
|
||||
throw new RuntimeException("The export with alias {$alias} is not known.");
|
||||
@@ -334,9 +330,9 @@ class ExportManager
|
||||
*
|
||||
* @param bool $whereUserIsGranted if true (default), restrict to user which are granted the right to execute the export
|
||||
*
|
||||
* @return ExportInterface[] an array where export's alias are keys
|
||||
* @return iterable<string, ExportInterface|DirectExportInterface> an array where export's alias are keys
|
||||
*/
|
||||
public function getExports($whereUserIsGranted = true)
|
||||
public function getExports($whereUserIsGranted = true): iterable
|
||||
{
|
||||
foreach ($this->exports as $alias => $export) {
|
||||
if ($whereUserIsGranted) {
|
||||
@@ -354,9 +350,9 @@ class ExportManager
|
||||
*
|
||||
* @param bool $whereUserIsGranted
|
||||
*
|
||||
* @return array where keys are the groups's name and value is an array of exports
|
||||
* @return array<string, array<string, ExportInterface|DirectExportInterface>> where keys are the groups's name and value is an array of exports
|
||||
*/
|
||||
public function getExportsGrouped($whereUserIsGranted = true): array
|
||||
public function getExportsGrouped(bool $whereUserIsGranted = true): array
|
||||
{
|
||||
$groups = ['_' => []];
|
||||
|
||||
@@ -375,10 +371,8 @@ class ExportManager
|
||||
* @param string $alias
|
||||
*
|
||||
* @throws RuntimeException if the filter is not known
|
||||
*
|
||||
* @return FilterInterface
|
||||
*/
|
||||
public function getFilter($alias)
|
||||
public function getFilter(string $alias): FilterInterface
|
||||
{
|
||||
if (!array_key_exists($alias, $this->filters)) {
|
||||
throw new RuntimeException("The filter with alias {$alias} is not known.");
|
||||
@@ -390,16 +384,17 @@ class ExportManager
|
||||
/**
|
||||
* get all filters.
|
||||
*
|
||||
* @param Generator $aliases
|
||||
* @param array<string> $aliases
|
||||
* @return iterable<string, FilterInterface> $aliases
|
||||
*/
|
||||
public function getFilters(array $aliases)
|
||||
public function getFilters(array $aliases): iterable
|
||||
{
|
||||
foreach ($aliases as $alias) {
|
||||
yield $alias => $this->getFilter($alias);
|
||||
}
|
||||
}
|
||||
|
||||
public function getFormatter($alias)
|
||||
public function getFormatter(string $alias): FormatterInterface
|
||||
{
|
||||
if (!array_key_exists($alias, $this->formatters)) {
|
||||
throw new RuntimeException("The formatter with alias {$alias} is not known.");
|
||||
@@ -414,7 +409,7 @@ class ExportManager
|
||||
* @param array $data the data from the export form
|
||||
* @string the formatter alias|null
|
||||
*/
|
||||
public function getFormatterAlias(array $data)
|
||||
public function getFormatterAlias(array $data): ?string
|
||||
{
|
||||
if (array_key_exists(ExportType::PICK_FORMATTER_KEY, $data)) {
|
||||
return $data[ExportType::PICK_FORMATTER_KEY]['alias'];
|
||||
@@ -426,9 +421,9 @@ class ExportManager
|
||||
/**
|
||||
* Get all formatters which supports one of the given types.
|
||||
*
|
||||
* @return Generator
|
||||
* @return iterable<string, FormatterInterface>
|
||||
*/
|
||||
public function getFormattersByTypes(array $types)
|
||||
public function getFormattersByTypes(array $types): iterable
|
||||
{
|
||||
foreach ($this->formatters as $alias => $formatter) {
|
||||
if (in_array($formatter->getType(), $types, true)) {
|
||||
@@ -445,7 +440,7 @@ class ExportManager
|
||||
*
|
||||
* @return \Chill\MainBundle\Entity\Center[] the picked center
|
||||
*/
|
||||
public function getPickedCenters(array $data)
|
||||
public function getPickedCenters(array $data): array
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
@@ -455,9 +450,9 @@ class ExportManager
|
||||
*
|
||||
* @param array $data the data from the export form
|
||||
*
|
||||
* @return string[]
|
||||
* @return list<string>
|
||||
*/
|
||||
public function getUsedAggregatorsAliases(array $data)
|
||||
public function getUsedAggregatorsAliases(array $data): array
|
||||
{
|
||||
$aggregators = $this->retrieveUsedAggregators($data[ExportType::AGGREGATOR_KEY]);
|
||||
|
||||
@@ -471,9 +466,8 @@ class ExportManager
|
||||
* @param \Chill\MainBundle\Export\ExportElementInterface $element
|
||||
* @param DirectExportInterface|ExportInterface $export
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isGrantedForElement(ExportElementInterface $element, ?ExportElementInterface $export = null, ?array $centers = null)
|
||||
public function isGrantedForElement(ExportElementInterface $element, ?ExportElementInterface $export = null, ?array $centers = null): bool
|
||||
{
|
||||
if ($element instanceof ExportInterface || $element instanceof DirectExportInterface) {
|
||||
$role = $element->requiredRole();
|
||||
@@ -548,13 +542,12 @@ class ExportManager
|
||||
* Check for acl. If an user is not authorized to see an aggregator, throw an
|
||||
* UnauthorizedException.
|
||||
*
|
||||
* @param type $data
|
||||
* @throw UnauthorizedHttpException if the user is not authorized
|
||||
*/
|
||||
private function handleAggregators(
|
||||
ExportInterface $export,
|
||||
QueryBuilder $qb,
|
||||
$data,
|
||||
array $data,
|
||||
array $center
|
||||
) {
|
||||
$aggregators = $this->retrieveUsedAggregators($data);
|
||||
@@ -600,9 +593,9 @@ class ExportManager
|
||||
/**
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return AggregatorInterface[]
|
||||
* @return iterable<string, AggregatorInterface>
|
||||
*/
|
||||
private function retrieveUsedAggregators($data)
|
||||
private function retrieveUsedAggregators($data): iterable
|
||||
{
|
||||
if (null === $data) {
|
||||
return [];
|
||||
|
Reference in New Issue
Block a user