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

Commit

Permalink
Merge remote-tracking branch 'origin/wip/steps' into wip/steps-v9
Browse files Browse the repository at this point in the history
  • Loading branch information
Haythem Labbassi authored and Haythem Labbassi committed May 27, 2019
2 parents 1c2636f + 4499a91 commit a8e4987
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 37 deletions.
3 changes: 3 additions & 0 deletions Classes/AssetHandler/Html/DataAttributesAssetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Romm\Formz\Service\StringService;
use Throwable;
use Traversable;
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
use TYPO3\CMS\Extbase\Error\Result;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException;
Expand Down Expand Up @@ -232,6 +233,8 @@ protected function formatValue($value)
} elseif ($value instanceof DateTime) {
$format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
$value = $value->format($format);
} elseif ($value instanceof DomainObjectInterface) {
$value = $value->getUid();
} elseif (false === is_string($value)) {
$value = (string)$value;
}
Expand Down
13 changes: 9 additions & 4 deletions Classes/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,19 @@ protected function getRootConfigurationFromCache($cacheIdentifier)

if ($cacheInstance->has($cacheIdentifier)) {
$instance = $cacheInstance->get($cacheIdentifier);
} else {
$instance = $this->buildRootConfiguration();

if (false === $instance->getValidationResult()->hasErrors()) {
$cacheInstance->set($cacheIdentifier, $instance);
if ($instance instanceof ConfigurationObjectInstance) {

return $instance;
}
}

$instance = $this->buildRootConfiguration();

if (false === $instance->getValidationResult()->hasErrors()) {
$cacheInstance->set($cacheIdentifier, $instance);
}

return $instance;
}

Expand Down
14 changes: 13 additions & 1 deletion Classes/Controller/AjaxValidationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function processRequest(RequestInterface $request, ResponseInterface $res

$errorMessage = ExtensionService::get()->isInDebugMode()
? $this->getDebugMessageForException($exception)
: ContextService::get()->translate(self::DEFAULT_ERROR_MESSAGE_KEY);
: $this->getDefaultErrorMessage();

$error = new Error($errorMessage, 1490176818);
$this->result->addError($error);
Expand All @@ -154,6 +154,18 @@ public function processRequest(RequestInterface $request, ResponseInterface $res
$this->injectResultInResponse();
}

/**
* Will get the default error message from the form configuartion
*/
protected function getDefaultErrorMessage()
{
if ($this->formObject === null) {
return ContextService::get()->translate(self::DEFAULT_ERROR_MESSAGE_KEY);
}

return $this->formObject->getDefinition()->getSettings()->getDefaultErrorMessage();
}

/**
* Will take care of adding a new argument to the request, based on the form
* name and the form class name found in the request arguments.
Expand Down
27 changes: 15 additions & 12 deletions Classes/Controller/FormActionControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
*/
trait FormActionControllerTrait
{
/**
* @todo
*
* @var string
*/
protected $formScope = MainScope::class;

/**
* In case an exception (any exception type) is thrown during the
* middlewares execution, it can be automatically caught by FormZ, and the
Expand All @@ -31,20 +38,16 @@ trait FormActionControllerTrait
* render a view that contains a message explaining to the user that
* something went wrong.
*
* Just fill the property below with the name of an existing action of the
* Just override this method to return the name of an existing action of the
* controller. The method will have a single parameter which is the
* exception.
*
* @var string
* @return string|null
*/
protected $actionForException;

/**
* @todo
*
* @var string
*/
protected $formScope = MainScope::class;
protected function actionForException(): ?string
{
return null;
}

/**
* IMPORTANT: if you need to override this method in your own controller, do
Expand All @@ -58,14 +61,14 @@ private function initializeFormz()

$processor = ControllerProcessor::prepare($this->request, $this->arguments, $this->formScope, $settings);

if (null !== $this->actionForException) {
if (null !== $this->actionForException()) {
$vendorName = $this->request->getControllerVendorName();
$extensionName = $this->request->getControllerExtensionName();
$controllerName = $this->request->getControllerName();

$processor->setExceptionCallback(function ($exception) use ($vendorName, $controllerName, $extensionName) {
$this->request->setControllerVendorName($vendorName);
$this->forward($this->actionForException, $controllerName, $extensionName, ['exception' => $exception]);
$this->forward($this->actionForException(), $controllerName, $extensionName, ['exception' => $exception]);
});
}

Expand Down
14 changes: 14 additions & 0 deletions Classes/Form/Definition/Middleware/PresetMiddlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Romm\Formz\Middleware\Item\Step\PreviousStepMiddleware;
use Romm\Formz\Middleware\Item\Step\StepDispatchingMiddleware;
use Romm\Formz\Middleware\Item\Step\StepFetchingMiddleware;
use Romm\Formz\Middleware\Item\Step\SubstepFetchingMiddleware;
use Romm\Formz\Middleware\MiddlewareInterface;

class PresetMiddlewares implements DataPreProcessorInterface
Expand Down Expand Up @@ -59,6 +60,11 @@ class PresetMiddlewares implements DataPreProcessorInterface
*/
protected $stepDispatchingMiddleware;

/**
* @var \Romm\Formz\Middleware\Item\Step\SubstepFetchingMiddleware
*/
protected $substepFetchingMiddleware;

/**
* @var \Romm\Formz\Middleware\Item\Field\Focus\FieldFocusMiddleware
*/
Expand Down Expand Up @@ -140,6 +146,14 @@ public function getStepDispatchingMiddleware()
return $this->stepDispatchingMiddleware;
}

/**
* @return SubstepFetchingMiddleware
*/
public function getSubstepFetchingMiddleware()
{
return $this->substepFetchingMiddleware;
}

/**
* @return FieldFocusMiddleware
*/
Expand Down
8 changes: 8 additions & 0 deletions Classes/Form/FormObject/FormObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ public function getStepService(FormObject $formObject)
return $this->stepService[$hash];
}

/**
* @return FormObject[]
*/
public function all()
{
return $this->instances;
}

/**
* @param string $className
* @return FormObjectStatic
Expand Down
9 changes: 7 additions & 2 deletions Classes/Middleware/Item/Begin/BeginMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ protected function checkFormSubmission()

if ($this->requestWasSubmitted()
&& null === $request->getOriginalRequest()
&& $request->hasArgument($formName)
) {
if (false === $request->hasArgument('fz-hash')) {
throw new \Exception('todo fz-hash'); // @todo
Expand All @@ -84,6 +83,10 @@ protected function checkFormSubmission()
throw new \Exception('todo formzData'); // @todo
}

if (!$request->hasArgument($formName)) {
$request->setArgument($formName, []);
}

$form = $this->formService->getFormInstance($formName);

$formObject->setForm($form);
Expand Down Expand Up @@ -163,6 +166,8 @@ protected function requestWasSubmitted()

$pluginNamespace = $extensionService->getPluginNamespace($request->getControllerExtensionName(), $request->getPluginName());

return isset($_POST[$pluginNamespace][$this->processor->getFormObject()->getName()]);
$formName = $_POST[$pluginNamespace]['formName'] ?? null;

return $formName === $this->processor->getFormObject()->getName();
}
}
48 changes: 38 additions & 10 deletions Classes/Middleware/Item/Step/Service/StepMiddlewareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public function getFirstInvalidStep(Step $step)
return $this->validationService->getFirstInvalidStep($step);
}

/**
* @see \Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareValidationService::getFirstNotValidatedStep
*
* @param StepDefinition $stepDefinition
* @return StepDefinition|null
*/
public function getFirstNotValidatedStep(StepDefinition $stepDefinition)
{
return $this->validationService->getFirstNotValidatedStep($stepDefinition);
}

/**
* @param StepDefinition $step
* @return StepDefinition
Expand Down Expand Up @@ -228,8 +239,7 @@ public function getNextSubstepDefinition(SubstepDefinition $substepDefinition)

foreach ($divergenceSteps as $divergenceStep) {
if (true === $this->getSubstepDefinitionConditionResult($divergenceStep)) {
$nextSubstep = $divergenceStep;
break;
return $divergenceStep;
}
}
}
Expand All @@ -238,14 +248,8 @@ public function getNextSubstepDefinition(SubstepDefinition $substepDefinition)
while ($substepDefinition->hasNextSubstep()) {
$substepDefinition = $substepDefinition->getNextSubstep();

if ($substepDefinition->hasActivation()) {
if (true === $this->getSubstepDefinitionConditionResult($substepDefinition)) {
$nextSubstep = $substepDefinition;
break;
}
} else {
$nextSubstep = $substepDefinition;
break;
if ($this->substepIsValid($substepDefinition)) {
return $substepDefinition;
}
}
}
Expand All @@ -258,6 +262,21 @@ public function findSubstepDefinition(Step $step, callable $callback)
return $this->findSubstepDefinitionRecursive($step->getSubsteps()->getFirstSubstepDefinition(), $callback);
}

/**
* Finds the first valid substep.
*
* @param Step $step
* @return SubstepDefinition|null
*/
public function findFirstSubstepDefinition(Step $step)
{
$firstSubstepDefinition = $step->getSubsteps()->getFirstSubstepDefinition();

return $this->substepIsValid($firstSubstepDefinition)
? $firstSubstepDefinition
: $this->getNextSubstepDefinition($firstSubstepDefinition);
}

protected function findSubstepDefinitionRecursive(SubstepDefinition $substepDefinition, callable $callback)
{
$result = $callback($substepDefinition);
Expand Down Expand Up @@ -331,6 +350,15 @@ public function getSubstepDefinitionConditionResult(SubstepDefinition $substepDe
return $tree->getPhpResult($dataObject);
}

private function substepIsValid(SubstepDefinition $substepDefinition)
{
if ($substepDefinition->hasActivation()) {
return $this->getSubstepDefinitionConditionResult($substepDefinition);
}

return true;
}

/**
* @param Step $step
* @return StepDefinition|null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ public function stepDefinitionIsValid(StepDefinition $stepDefinition)
&& $stepDefinition->getStep()->getIdentifier() === $this->persistence->getStepIdentifierAtLevel($stepLevel);
}

/**
* Get the first not validated step
*
* @param StepDefinition $stepDefinition
* @return StepDefinition|null
*/
public function getFirstNotValidatedStep(StepDefinition $stepDefinition)
{
if (false === $stepDefinition->hasPreviousDefinition()) {
/*
* No previous step definition found: the user stands on the first
* step, it always has the right to stand there.
*/
return null;
}

$previousStepDefinition = $stepDefinition->getPreviousDefinition();

if ($this->persistence->stepWasValidated($previousStepDefinition->getStep())) {
return $stepDefinition;
}

return $this->getFirstNotValidatedStep($previousStepDefinition);
}

/**
* Searches for the first invalid step among previous steps from the given
* step.
Expand All @@ -127,7 +152,6 @@ public function stepDefinitionIsValid(StepDefinition $stepDefinition)
public function getFirstInvalidStep(Step $step)
{
$firstStep = $this->service->getFirstStepDefinition();

if ($step === $firstStep->getStep()) {
/*
* The first step is always valid.
Expand Down Expand Up @@ -161,6 +185,7 @@ public function getFirstInvalidStep(Step $step)
}

foreach ($stepDefinitionsToTest as $stepDefinition) {

$step = $stepDefinition->getStep();

/*
Expand All @@ -187,9 +212,6 @@ public function getFirstInvalidStep(Step $step)
);

break;
} else {
$this->persistence->markStepAsValidated($stepDefinition);
$this->persistence->addValidatedFields($result->getValidatedFields());
}
}

Expand All @@ -213,8 +235,7 @@ public function getFirstInvalidStep(Step $step)
]
);
}



return $invalidStepDefinition;
}

Expand Down
8 changes: 8 additions & 0 deletions Classes/Middleware/Item/Step/StepFetchingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public function after(Arguments $arguments)
if ($stepToRedirect instanceof StepDefinition) {
$this->service->redirectToStep($stepToRedirect->getStep(), $this->redirect());
}

/**
* If we don't find an invalid Step we search the first not validated step
*/
$stepToRedirect = $this->service->getFirstNotValidatedStep($stepDefinition);
if ($stepToRedirect instanceof StepDefinition) {
$this->service->redirectToStep($stepToRedirect->getStep(), $this->redirect());
}
}
}
}
Expand Down

0 comments on commit a8e4987

Please sign in to comment.