Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Nov 21, 2017
1 parent ae03e85 commit c2561f1
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 46 deletions.
7 changes: 5 additions & 2 deletions Classes/Middleware/Item/Begin/BeginMiddleware.php
Expand Up @@ -20,6 +20,7 @@
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
use Romm\Formz\Middleware\Signal\After;
use Romm\Formz\Middleware\Signal\SignalObject;
use Romm\Formz\ViewHelpers\Step\PreviousLinkViewHelper;

final class BeginMiddleware implements BasicMiddlewareInterface
{
Expand Down Expand Up @@ -89,8 +90,10 @@ protected function checkFormSubmission()
$formzData = $request->getArgument('formzData');
$formObject->getRequestData()->fillFromHash($formzData);

$proxy = FormObjectFactory::get()->getProxy($form);
$proxy->markFormAsSubmitted();
if (!$request->hasArgument(PreviousLinkViewHelper::PREVIOUS_LINK_PARAMETER)) {
$proxy = FormObjectFactory::get()->getProxy($form);
$proxy->markFormAsSubmitted();
}

$this->injectFormHashInProxy();
}
Expand Down
35 changes: 22 additions & 13 deletions Classes/Middleware/Item/Field/Focus/FieldFocusMiddleware.php
Expand Up @@ -18,6 +18,7 @@
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
use Romm\Formz\Form\FormObject\FormObjectFactory;
use Romm\Formz\Middleware\Item\OnBeginMiddleware;
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;

/**
Expand All @@ -37,6 +38,19 @@ class FieldFocusMiddleware extends OnBeginMiddleware implements PresetMiddleware
*/
protected $field;

/**
* @var StepMiddlewareService
*/
protected $service;

/**
* Inject the step service.
*/
public function initializeMiddleware()
{
$this->service = StepMiddlewareService::get();
}

/**
* @todo
*/
Expand All @@ -62,8 +76,7 @@ protected function process()
return;
}

$firstSubstepDefinition = $this->step->getSubsteps()->getFirstSubstepDefinition();
$substepDefinition = $this->fetchFieldSubstep($firstSubstepDefinition);
$substepDefinition = $this->fetchFieldSubstep();

if ($substepDefinition) {
$stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
Expand All @@ -72,20 +85,16 @@ protected function process()
}

/**
* @param SubstepDefinition $substepDefinition
* @return SubstepDefinition|null
*/
protected function fetchFieldSubstep(SubstepDefinition $substepDefinition)
protected function fetchFieldSubstep()
{
if ($substepDefinition->getSubstep()->supportsField($this->field)) {
return $substepDefinition;
}

if ($substepDefinition->hasNextSubstep()) {
return $this->fetchFieldSubstep($substepDefinition->getNextSubstep());
}

return null;
return $this->service->findSubstepDefinition(
$this->step,
function (SubstepDefinition $substepDefinition) {
return $substepDefinition->getSubstep()->supportsField($this->field);
}
);
}

protected function fetchField()
Expand Down
47 changes: 41 additions & 6 deletions Classes/Middleware/Item/Step/PreviousStepMiddleware.php
Expand Up @@ -13,24 +13,26 @@

namespace Romm\Formz\Middleware\Item\Step;

use Romm\Formz\Form\FormObject\FormObjectFactory;
use Romm\Formz\Middleware\Argument\Arguments;
use Romm\Formz\Middleware\Item\AbstractMiddleware;
use Romm\Formz\Middleware\Item\FormValidation\FormValidationSignal;
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
use Romm\Formz\Middleware\Scope\FieldValidationScope;
use Romm\Formz\Middleware\Scope\ReadScope;
use Romm\Formz\Middleware\Signal\Before;
use Romm\Formz\Middleware\Signal\After;
use Romm\Formz\ViewHelpers\Step\PreviousLinkViewHelper;

/**
* @todo
*/
class PreviousStepMiddleware extends AbstractMiddleware implements Before, FormValidationSignal
class PreviousStepMiddleware extends AbstractMiddleware implements After, FormInjectionSignal, PresetMiddlewareInterface
{
/**
* @var int
*/
protected $priority = self::PRIORITY_STEP + 100;
protected $priority = self::PRIORITY_STEP_FETCHING + 100;

/**
* @var StepMiddlewareService
Expand All @@ -51,11 +53,11 @@ public function initializeMiddleware()
}

/**
* @see StepFetchingMiddleware
* @see PreviousStepMiddleware
*
* @param Arguments $arguments
*/
public function before(Arguments $arguments)
public function after(Arguments $arguments)
{
$formObject = $this->getFormObject();
$this->service->reset($formObject, $this->getRequest());
Expand All @@ -73,6 +75,39 @@ public function before(Arguments $arguments)
if ($currentStep) {
$stepDefinition = $this->service->getStepDefinition($currentStep);

if ($currentStep->hasSubsteps()) {
$stepService = FormObjectFactory::get()->getStepService($this->getFormObject());
$substepsLevel = $stepService->getSubstepsLevel();

if ($substepsLevel > 1) {
$substepDefinition = $currentStep->getSubsteps()->getFirstSubstepDefinition();

while ($substepDefinition) {
$nextSubstepDefinition = $this->service->getNextSubstepDefinition($substepDefinition);

if (!$nextSubstepDefinition
|| $nextSubstepDefinition->getLevel() >= $substepsLevel - 1
) {
break;
}

$substepDefinition = $nextSubstepDefinition;
}

$stepService->setCurrentSubstepDefinition($substepDefinition);
$stepService->setCurrentStep($currentStep);
$stepService->setSubstepsLevel($substepDefinition->getLevel());
// \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($substepDefinition, __CLASS__ . ':' . __LINE__ . ' $substepDefinition');
// \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($substepDefinition->getLevel(), __CLASS__ . ':' . __LINE__ . ' $substepDefinition->getLevel()');
// \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($currentStep, __CLASS__ . ':' . __LINE__ . ' $currentStep');
// \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($stepService, __CLASS__ . ':' . __LINE__ . ' $stepService');
// die();

return;
}
}


if ($stepDefinition->hasPreviousDefinition()) {
$this->service->redirectToStep($stepDefinition->getPreviousDefinition()->getStep(), $this->redirect());
}
Expand Down
76 changes: 72 additions & 4 deletions Classes/Middleware/Item/Step/Service/StepMiddlewareService.php
Expand Up @@ -18,6 +18,7 @@
use Romm\Formz\Error\FormResult;
use Romm\Formz\Form\Definition\Step\Step\Step;
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition;
use Romm\Formz\Form\FormObject\FormObject;
use Romm\Formz\Form\FormObject\FormObjectFactory;
use Romm\Formz\Form\FormObject\Service\Step\FormStepPersistence;
Expand Down Expand Up @@ -91,7 +92,7 @@ public function getNextStep(Step $currentStep)
$nextStep = null;

if ($currentStepDefinition->hasNextStep()) {
$nextStep = $this->getNextStepDefinition($currentStepDefinition, true);
$nextStep = $this->getNextStepDefinition($currentStepDefinition);
}

return $nextStep;
Expand Down Expand Up @@ -178,10 +179,9 @@ public function getFirstInvalidStep(Step $step)

/**
* @param StepDefinition $step
* @param bool $removeConditionalSteps
* @return StepDefinition
*/
public function getNextStepDefinition(StepDefinition $step, $removeConditionalSteps = false)
public function getNextStepDefinition(StepDefinition $step)
{
$nextStep = null;

Expand All @@ -204,7 +204,7 @@ public function getNextStepDefinition(StepDefinition $step, $removeConditionalSt
if (true === $this->getStepDefinitionConditionResult($step)) {
$nextStep = $step;
break;
} elseif (true === $removeConditionalSteps) {
} else {
$this->persistence->removeStep($step);
}
} else {
Expand All @@ -217,6 +217,60 @@ public function getNextStepDefinition(StepDefinition $step, $removeConditionalSt
return $nextStep;
}

public function getNextSubstepDefinition(SubstepDefinition $substepDefinition)
{
$nextSubstep = null;

if ($substepDefinition->hasDivergence()) {
$divergenceSteps = $substepDefinition->getDivergenceSubsteps();

foreach ($divergenceSteps as $divergenceStep) {
if (true === $this->getSubstepDefinitionConditionResult($divergenceStep)) {
$nextSubstep = $divergenceStep;
break;
}
}
}

if (null === $nextSubstep) {
while ($substepDefinition->hasNextSubstep()) {
$substepDefinition = $substepDefinition->getNextSubstep();

if ($substepDefinition->hasActivation()) {
if (true === $this->getSubstepDefinitionConditionResult($substepDefinition)) {
$nextSubstep = $substepDefinition;
break;
}
} else {
$nextSubstep = $substepDefinition;
break;
}
}
}

return $nextSubstep;
}

public function findSubstepDefinition(Step $step, callable $callback)
{
return $this->findSubstepDefinitionRecursive($step->getSubsteps()->getFirstSubstepDefinition(), $callback);
}

protected function findSubstepDefinitionRecursive(SubstepDefinition $substepDefinition, callable $callback)
{
$result = $callback($substepDefinition);

if (true === $result) {
return $substepDefinition;
}

$substepDefinition = $this->getNextSubstepDefinition($substepDefinition);

return $substepDefinition
? $this->findSubstepDefinitionRecursive($substepDefinition, $callback)
: null;
}

/**
* @param StepDefinition $stepDefinition
* @param Redirect $redirect
Expand Down Expand Up @@ -261,6 +315,20 @@ public function getStepDefinitionConditionResult(StepDefinition $stepDefinition)
return $tree->getPhpResult($dataObject);
}

/**
* @param SubstepDefinition $substepDefinition
* @return bool
*/
public function getSubstepDefinitionConditionResult(SubstepDefinition $substepDefinition)
{
$conditionProcessor = ConditionProcessorFactory::getInstance()->get($this->getFormObject());
$tree = $conditionProcessor->getActivationConditionTreeForSubstep($substepDefinition);
$todo = new FormValidatorExecutor(new FormValidatorDataObject($this->getFormObject(), new FormResult(), true)); // @todo
$dataObject = new PhpConditionDataObject($this->getFormObject()->getForm(), $todo);

return $tree->getPhpResult($dataObject);
}

/**
* @param Step $step
* @return StepDefinition|null
Expand Down
2 changes: 1 addition & 1 deletion Classes/Middleware/Item/Step/StepDispatchingMiddleware.php
Expand Up @@ -31,7 +31,7 @@ class StepDispatchingMiddleware extends DefaultMiddleware implements PresetMiddl
/**
* @var int
*/
protected $priority = self::PRIORITY_STEP;
protected $priority = self::PRIORITY_STEP_DISPATCHING;

/**
* @var StepMiddlewareService
Expand Down
11 changes: 6 additions & 5 deletions Classes/Middleware/Item/Step/StepFetchingMiddleware.php
Expand Up @@ -16,11 +16,12 @@
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;
use Romm\Formz\Middleware\Argument\Arguments;
use Romm\Formz\Middleware\Item\AbstractMiddleware;
use Romm\Formz\Middleware\Item\FormValidation\FormValidationSignal;
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal;
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
use Romm\Formz\Middleware\Scope\FieldValidationScope;
use Romm\Formz\Middleware\Scope\ReadScope;
use Romm\Formz\Middleware\Signal\Before;
use Romm\Formz\Middleware\Signal\After;

/**
* This middleware will fetch the current step in the form, based on the request
Expand All @@ -30,12 +31,12 @@
* on all previous steps is done to determine the first valid step: the request
* is then redirected to this step.
*/
class StepFetchingMiddleware extends AbstractMiddleware implements Before, FormValidationSignal
class StepFetchingMiddleware extends AbstractMiddleware implements After, FormInjectionSignal, PresetMiddlewareInterface
{
/**
* @var int
*/
protected $priority = self::PRIORITY_STEP;
protected $priority = self::PRIORITY_STEP_FETCHING;

/**
* @var StepMiddlewareService
Expand All @@ -60,7 +61,7 @@ public function initializeMiddleware()
*
* @param Arguments $arguments
*/
public function before(Arguments $arguments)
public function after(Arguments $arguments)
{
$formObject = $this->getFormObject();

Expand Down
3 changes: 2 additions & 1 deletion Classes/Middleware/MiddlewareInterface.php
Expand Up @@ -21,7 +21,8 @@ interface MiddlewareInterface extends BasicMiddlewareInterface
{
const PRIORITY_INJECT_FORM = 1000;
const PRIORITY_BEHAVIOURS = 100;
const PRIORITY_STEP = -1000000;
const PRIORITY_STEP_FETCHING = 1000000;
const PRIORITY_STEP_DISPATCHING = -1000000;

/**
* @param OptionInterface $options
Expand Down
2 changes: 2 additions & 0 deletions Classes/ViewHelpers/FormViewHelper.php
Expand Up @@ -30,6 +30,7 @@
use Romm\Formz\Service\StringService;
use Romm\Formz\Service\TimeTrackerService;
use Romm\Formz\Service\ViewHelper\Form\FormViewHelperService;
use Romm\Formz\ViewHelpers\Step\PreviousLinkViewHelper;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Error\Result;
Expand Down Expand Up @@ -296,6 +297,7 @@ protected function renderHiddenReferrerFields()
->getLevel();

$result .= '<input type="hidden" fz-substeps-level="1" name="' . $this->prefixFieldName('substepsLevel') . '" value="' . $substepsLevel . '" />' . LF;
$result .= '<input type="hidden" fz-substeps-previous="1" name="' . $this->prefixFieldName(PreviousLinkViewHelper::PREVIOUS_LINK_PARAMETER) . '" disabled="1" value="1" />' . LF;
}
}

Expand Down

0 comments on commit c2561f1

Please sign in to comment.