-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin/submission): add export command filter by expression (#1085)
Co-authored-by: David mattei <[email protected]>
- Loading branch information
1 parent
f18a109
commit 52ba9b3
Showing
5 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
EMS/core-bundle/src/Command/Submission/ExportCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EMS\CoreBundle\Command\Submission; | ||
|
||
use EMS\CommonBundle\Common\Command\AbstractCommand; | ||
use EMS\CommonBundle\Contracts\SpreadsheetGeneratorServiceInterface; | ||
use EMS\CommonBundle\Service\ExpressionService; | ||
use EMS\CoreBundle\Commands; | ||
use EMS\CoreBundle\Service\Form\Submission\FormSubmissionService; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ExportCommand extends AbstractCommand | ||
{ | ||
protected static $defaultName = Commands::SUBMISSION_EXPORT; | ||
public const ARG_FIELDS = 'fields'; | ||
public const OPTION_FILTER = 'filter'; | ||
public const OPTION_FILENAME = 'filename'; | ||
|
||
/** @var string[] */ | ||
private array $fields; | ||
private ?string $filter; | ||
private ?string $filename; | ||
|
||
public function __construct( | ||
private readonly FormSubmissionService $formSubmissionService, | ||
private readonly ExpressionService $expressionService, | ||
private readonly SpreadsheetGeneratorServiceInterface $spreadsheetGeneratorService, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Extract form submissions') | ||
->addArgument( | ||
self::ARG_FIELDS, | ||
InputArgument::IS_ARRAY, | ||
'Fields to export' | ||
)->addOption( | ||
self::OPTION_FILTER, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Expression to filter submissions, e.g. "\'true\' == (data[\'recontact-optin\'] ?? \'false\')". The following variables are available: data (array), instance (string), name (string), locale (string), submission_date (date in the ISO 8601 format)' | ||
)->addOption( | ||
self::OPTION_FILENAME, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Export filename, xlsx or csv formats are supported', | ||
); | ||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output): void | ||
{ | ||
parent::initialize($input, $output); | ||
$this->fields = $this->getArgumentStringArray(self::ARG_FIELDS); | ||
$this->filter = $this->getOptionStringNull(self::OPTION_FILTER); | ||
$this->filename = $this->getOptionStringNull(self::OPTION_FILENAME); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$this->io->section('Export the form submissions'); | ||
$sheet = []; | ||
|
||
$this->io->progressStart($this->formSubmissionService->count()); | ||
foreach ($this->formSubmissionService->getUnprocessed() as $submission) { | ||
$data = [ | ||
'instance' => $submission->getInstance(), | ||
'name' => $submission->getName(), | ||
'locale' => $submission->getLocale(), | ||
'submission_date' => $submission->getCreated()->format('c'), | ||
'data' => $submission->getData() ?? [], | ||
]; | ||
if (null !== $this->filter && !$this->expressionService->evaluateToBool($this->filter, $data)) { | ||
$this->io->progressAdvance(); | ||
continue; | ||
} | ||
$line = []; | ||
foreach ($this->fields as $field) { | ||
$line[] = $data['data'][$field] ?? $data[$field] ?? ''; | ||
} | ||
$sheet[] = $line; | ||
$this->io->progressAdvance(); | ||
} | ||
$this->io->progressFinish(); | ||
|
||
if (null === $this->filename) { | ||
$this->io->table([...$this->fields], $sheet); | ||
|
||
return self::EXECUTE_SUCCESS; | ||
} | ||
|
||
$extension = \pathinfo($this->filename)['extension'] ?? ''; | ||
if (!\in_array($extension, SpreadsheetGeneratorServiceInterface::FORMAT_WRITERS)) { | ||
$this->io->error(\sprintf('File format %s is not supported', $extension)); | ||
} | ||
|
||
$config = [ | ||
SpreadsheetGeneratorServiceInterface::SHEETS => [[ | ||
'rows' => [[...$this->fields], ...$sheet], | ||
'name' => 'submissions', | ||
]], | ||
SpreadsheetGeneratorServiceInterface::CONTENT_FILENAME => 'submissions', | ||
SpreadsheetGeneratorServiceInterface::WRITER => $extension, | ||
]; | ||
$this->spreadsheetGeneratorService->generateSpreadsheetFile($config, $this->filename); | ||
$this->io->success(\sprintf('The file %s has been successfully generated', $this->filename)); | ||
|
||
return self::EXECUTE_SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters