This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
281 additions
and
3 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
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
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,77 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Romm\Formz\Middleware\Item\Step; | ||
|
||
use Romm\Formz\Middleware\Argument\Arguments; | ||
use Romm\Formz\Middleware\Item\AbstractMiddleware; | ||
use Romm\Formz\Middleware\Item\Begin\BeginSignal; | ||
use Romm\Formz\Middleware\Signal\Before; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class SkipMiddleware extends AbstractMiddleware implements Before, BeginSignal | ||
{ | ||
/** | ||
* @param Arguments $arguments | ||
*/ | ||
public function before(Arguments $arguments) | ||
{ | ||
$request = $this->getRequest(); | ||
|
||
if (!$request->hasArgument('skip') | ||
|| !$request->getArgument('skip') | ||
) { | ||
return; | ||
} | ||
|
||
$formName = $this->getFormObject()->getName(); | ||
|
||
if (!$request->hasArgument($formName)) { | ||
return; | ||
} | ||
|
||
if (!$request->hasArgument('step')) { | ||
return; | ||
} | ||
|
||
$step = $request->getArgument('step'); | ||
$definition = $this->getFormObject()->getDefinition(); | ||
|
||
if (!$definition->hasSteps() | ||
|| !$definition->getSteps()->hasEntry($step) | ||
) { | ||
return; | ||
} | ||
|
||
$currentStep = $definition->getSteps()->getEntry($step); | ||
|
||
/** @var array $formArray */ | ||
$formArray = $request->getArgument($formName); | ||
|
||
$skipSubsteps = []; | ||
|
||
if ($request->hasArgument('skipSubsteps')) { | ||
$skipSubsteps = GeneralUtility::trimExplode(',', $request->getArgument('skipSubsteps')); | ||
} | ||
|
||
if (empty($skipSubsteps)) { | ||
foreach ($currentStep->getSupportedFields() as $field) { | ||
unset($formArray[$field->getField()->getName()]); | ||
} | ||
} elseif ($currentStep->hasSubsteps()) { | ||
$substeps = $currentStep->getSubsteps(); | ||
|
||
foreach ($skipSubsteps as $substep) { | ||
if (!$substeps->hasEntry($substep)) { | ||
continue; | ||
} | ||
|
||
foreach ($substeps->getEntry($substep)->getSupportedFields() as $field) { | ||
unset($formArray[$field->getField()->getName()]); | ||
} | ||
} | ||
} | ||
|
||
$request->setArgument($formName, $formArray); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
/* | ||
* 2017 Romain CANON <romain.hydrocanon@gmail.com> | ||
* | ||
* This file is part of the TYPO3 FormZ project. | ||
* It is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License, either | ||
* version 3 of the License, or any later version. | ||
* | ||
* For the full copyright and license information, see: | ||
* http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
namespace Romm\Formz\ViewHelpers\Step; | ||
|
||
use Exception; | ||
use Romm\Formz\Exceptions\ContextNotFoundException; | ||
use Romm\Formz\Form\Definition\Step\Step\Step; | ||
use Romm\Formz\Service\ViewHelper\Form\FormViewHelperService; | ||
use Romm\Formz\ViewHelpers\FormViewHelper; | ||
use TYPO3\CMS\Extbase\Mvc\Web\Request; | ||
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; | ||
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper; | ||
|
||
class SkipViewHelper extends AbstractFormFieldViewHelper | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $tagName = 'input'; | ||
|
||
/** | ||
* @var FormViewHelperService | ||
*/ | ||
protected $formService; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function initializeArguments() | ||
{ | ||
parent::initializeArguments(); | ||
|
||
$this->registerUniversalTagAttributes(); | ||
|
||
$this->registerArgument('substep', 'string', 'Identifier of the optional substep to be skipped.'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function render(): string | ||
{ | ||
/* | ||
* First, we check if this view helper is called from within the | ||
* `FormViewHelper`, because it would not make sense anywhere else. | ||
*/ | ||
if (false === $this->formService->formContextExists()) { | ||
throw ContextNotFoundException::skipViewHelperFormContextNotFound(); | ||
} | ||
|
||
$formObject = $this->formService->getFormObject(); | ||
$formDefinition = $formObject->getDefinition(); | ||
|
||
if (false === $formDefinition->hasSteps()) { | ||
throw new Exception('No stop for this form.'); | ||
} | ||
|
||
/** @var Request $request */ | ||
$request = $this->controllerContext->getRequest(); | ||
$currentStep = $formObject->fetchCurrentStep($request)->getCurrentStep(); | ||
|
||
if ($substep = $this->arguments['substep'] ?? null) { | ||
if (false === $currentStep->hasSubsteps()) { | ||
throw new Exception('No substeps for this form.'); | ||
} | ||
|
||
$substeps = $currentStep->getSubsteps(); | ||
|
||
if (false === $substeps->hasEntry($substep)) { | ||
throw new Exception("No substep `$substep` for this form."); | ||
} | ||
|
||
$this->tag->addAttribute('fz-substep', $substep); | ||
} | ||
|
||
$this->tag->addAttribute('type', 'submit'); | ||
$this->tag->addAttribute('value', $this->getValueAttribute()); | ||
$this->tag->addAttribute('formaction', $this->formAction($currentStep)); | ||
|
||
return $this->tag->render(); | ||
} | ||
|
||
private function formAction(Step $step): string | ||
{ | ||
$formArguments = $this->viewHelperVariableContainer->get(FormViewHelper::class, 'arguments'); | ||
|
||
$pageUid = (int)$formArguments['pageUid'] > 0 ? (int)$formArguments['pageUid'] : null; | ||
|
||
/** @var UriBuilder $uriBuilder */ | ||
$uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder(); | ||
|
||
$arguments = (array)$formArguments['arguments']; | ||
|
||
$arguments['skip'] = true; | ||
$arguments['step'] = $step->getIdentifier(); | ||
|
||
$substep = $this->arguments['substep'] ?? null; | ||
|
||
if ($substep) { | ||
$arguments['skipSubsteps'] = $substep; | ||
} | ||
|
||
return $uriBuilder | ||
->reset() | ||
->setTargetPageUid($pageUid) | ||
->setTargetPageType($formArguments['pageType']) | ||
->setNoCache($formArguments['noCache']) | ||
->setUseCacheHash(!$formArguments['noCacheHash']) | ||
->setSection($formArguments['section']) | ||
->setCreateAbsoluteUri($formArguments['absolute']) | ||
->setArguments((array)$formArguments['additionalParams']) | ||
->setAddQueryString($formArguments['addQueryString']) | ||
->setAddQueryStringMethod($formArguments['addQueryStringMethod']) | ||
->setArgumentsToBeExcludedFromQueryString((array)$formArguments['argumentsToBeExcludedFromQueryString']) | ||
->setFormat($formArguments['format']) | ||
->uriFor( | ||
$formArguments['action'], | ||
$arguments, | ||
$formArguments['controller'], | ||
$formArguments['extensionName'], | ||
$formArguments['pluginName'] | ||
); | ||
} | ||
|
||
/** | ||
* @param FormViewHelperService $service | ||
*/ | ||
public function injectFormService(FormViewHelperService $service) | ||
{ | ||
$this->formService = $service; | ||
} | ||
} |
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