Skip to content

Commit

Permalink
CS
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed May 7, 2024
1 parent 4bad770 commit ad424fb
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 36 deletions.
23 changes: 13 additions & 10 deletions src/EventListener/CompileFormFieldsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function __invoke(array $formFields, string $formId, Form $form): array

$manager = $this->formManagerFactory->forFormId((int) $form->id);

// If the manager is currently being prepared (recursive compileFormFields hook call), we abort
// If the manager is currently being prepared (recursive compileFormFields hook
// call), we abort
if ($manager->isPreparing()) {
return $formFields;
}
Expand All @@ -50,10 +51,10 @@ public function __invoke(array $formFields, string $formId, Form $form): array
return $manager->getFieldsWithoutPageBreaks();
}

// Validate whether previous form data was submitted if we're not on the first step.
// This has to be done no matter if we're in a POST request right now or not as otherwise
// you can submit a POST request without any previous step data (e.g. by deleting the session cookie
// manually)
// Validate whether previous form data was submitted if we're not on the first
// step. This has to be done no matter if we're in a POST request right now or
// not as otherwise you can submit a POST request without any previous step data
// (e.g. by deleting the session cookie manually)
if (!$manager->isFirstStep()) {
$firstInvalidStep = $manager->getFirstInvalidStep();

Expand All @@ -64,11 +65,13 @@ public function __invoke(array $formFields, string $formId, Form $form): array

$stepData = $manager->getDataOfCurrentStep();

// If there is form data submitted in this step, store the original values here no matter if we're going back or if we continue.
// Important: We do not store $_FILES here! The problem with storing $_FILES across requests is that we would need
// to move it from its tmp_name as PHP deletes files automatically after the request has finished. We could indeed
// move them here but if we did at this stage the form fields themselves would later not be able to move them
// to their own desired place. So we cannot store any file information at this stage.
// If there is form data submitted in this step, store the original values here no
// matter if we're going back or if we continue. Important: We do not store $_FILES
// here! The problem with storing $_FILES across requests is that we would need to move
// it from its tmp_name as PHP deletes files automatically after the request has
// finished. We could indeed move them here but if we did at this stage the form fields
// themselves would later not be able to move them to their own desired place. So
// we cannot store any file information at this stage.
if ($_POST) {
$stepData = $stepData->withOriginalPostData(new ParameterBag($_POST));
$manager->storeStepData($stepData);
Expand Down
15 changes: 7 additions & 8 deletions src/EventListener/PrepareFomDataListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,21 @@ public function __invoke(array &$submitted, array &$labels, array $fields, Form
if ($manager->isLastStep() && 'continue' === $pageSwitchValue) {
$allData = $manager->getDataOfAllSteps();

// Replace data by reference and then return so the default Contao
// routine kicks in
// Replace data by reference and then return so the default Contao routine kicks in
$submitted = $allData->getAllSubmitted();
$labels = $allData->getAllLabels();
$files = $allData->getAllFiles();

// Remove the page switch value field from the submitted data, so it's not passed on
// in e-mail notifications and the like. However, in intermediate steps we need it because otherwise it's not
// possible to have an empty step (e.g. only explanation fields) as the step data would be empty and
// getFirstInvalidStep() would return the one before the empty step.
// Remove the page switch value field from the submitted data, so it's not passed
// on in e-mail notifications and the like. However, in intermediate steps we
// need it because otherwise it's not possible to have an empty step (e.g. only
// explanation fields) as the step data would be empty and getFirstInvalidStep()
// would return the one before the empty step.
unset($submitted['mp_form_pageswitch']);

// Add session data for Contao 4.13
if (version_compare(ContaoCoreBundle::getVersion(), '5.0', '<')) {
// Override $_SESSION['FORM_DATA'] so it contains the data of
// previous steps as well
// Override $_SESSION['FORM_DATA'] so it contains the data of previous steps as well
$_SESSION['FORM_DATA'] = $submitted;
$_SESSION['FILES'] = $allData->getAllFiles();
}
Expand Down
8 changes: 4 additions & 4 deletions src/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ private function loadFormFieldModels(): void

if (null !== $collection) {
foreach ($collection as $formFieldModel) {
// Ignore the name of form fields which do not use a name (see contao/core-bundle #1268)
// Ignore the name of form fields which do not use a name (see
// contao/core-bundle #1268)
if (
$formFieldModel->name && isset($GLOBALS['TL_DCA']['tl_form_field']['palettes'][$formFieldModel->type])
&& preg_match('/[,;]name[,;]/', (string) $GLOBALS['TL_DCA']['tl_form_field']['palettes'][$formFieldModel->type])
Expand Down Expand Up @@ -389,7 +390,7 @@ private function prepare(): void

$this->preparing = true;

$formModel = $this->contaoFramework->getAdapter(FormModel::class)->findByPk($this->formId);
$formModel = $this->contaoFramework->getAdapter(FormModel::class)->findById($this->formId);

if (null === $formModel) {
throw new \InvalidArgumentException(sprintf('Could not load form ID "%d".', $this->formId));
Expand Down Expand Up @@ -418,8 +419,7 @@ private function prepare(): void
$this->formFieldsPerStep[$i][$k] = $formField;

if ($this->isPageBreak($formField)) {
// Set the name on the model, otherwise one has to enter it
// in the back end every time
// Set the name on the model, otherwise one has to enter it in the back end every time
$formField->name = $formField->type;

// Increase counter
Expand Down
9 changes: 4 additions & 5 deletions src/Step/FileParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
class FileParameterBag extends ParameterBag
{
/**
* PHP deletes file uploads after the request ends which is a problem
* for mp_forms as it wants to keep them across requests.
* Depending on how a form upload field is configured, however, a file might
* already have been moved to a final destination. So we only do this if
* it was uploaded in the current request.
* PHP deletes file uploads after the request ends which is a problem for mp_forms
* as it wants to keep them across requests. Depending on how a form upload field
* is configured, however, a file might already have been moved to a final
* destination. So we only do this if it was uploaded in the current request.
*/
public function set(string $name, mixed $value): self
{
Expand Down
4 changes: 2 additions & 2 deletions src/Step/StepData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ private function __construct(
}

/**
* A step is considered empty when there is no submitted data or files.
* Original post data or labels are no user submitted data.
* A step is considered empty when there is no submitted data or files. Original
* post data or labels are no user submitted data.
*/
public function isEmpty(): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public function generate(FormManager $manager): string
$info[] = $manager->getFormId();
$info[] = $manager->getSessionReference();

// Ensure the identifier changes, when the fields are updated as the settings might change
// Ensure the identifier changes, when the fields are updated as the settings
// might change
foreach ($manager->getFormFieldModels() as $fieldModel) {
$info[] = $fieldModel->tstamp;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Widget/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ private function generateTokens(): array
$summaryTokens[$k]['value'] = $tokens['file_'.$k];
}

// Add a simple summary token that outputs label plus value for everything that was submitted
// Add a simple summary token that outputs label plus value for everything that
// was submitted
$summaryToken = [];

foreach ($summaryTokens as $k => $v) {
Expand Down
4 changes: 2 additions & 2 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ protected function createFactory(FormModel $formModel, Collection $formFields, S
$request->setSession(new Session(new MockArraySessionStorage()));
$stack->push($request);

$formModelAdapter = $this->mockAdapter(['findByPk']);
$formModelAdapter = $this->mockAdapter(['findById']);
$formModelAdapter
->method('findByPk')
->method('findById')
->willReturn($formModel)
;

Expand Down
4 changes: 2 additions & 2 deletions tests/FormManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class FormManagerTest extends ContaoTestCase
public function testAccessingAllDataWithoutFormFields(): void
{
$formModel = $this->mockClassWithProperties(FormModel::class, ['id' => 42]);
$formModelAdapter = $this->mockAdapter(['findByPk']);
$formModelAdapter = $this->mockAdapter(['findById']);
$formModelAdapter
->expects($this->once())
->method('findByPk')
->method('findById')
->with(42)
->willReturn($formModel)
;
Expand Down
2 changes: 1 addition & 1 deletion tests/Step/StepDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testLabels(array $data): void
$this->assertTrue($parameters->equals($stepData->getLabels()));
}

public function parametersDataProvider(): \Generator
public static function parametersDataProvider(): iterable
{
yield [
[
Expand Down

0 comments on commit ad424fb

Please sign in to comment.