Completed
Push — wip/steps-tmp ( ae03e8 )
by Romain
02:20
created

PreviousLinkViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\ViewHelpers\Step;
15
16
use Romm\Formz\Form\FormObject\FormObjectFactory;
17
use Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareService;
18
use Romm\Formz\Service\ViewHelper\Form\FormViewHelperService;
19
use TYPO3\CMS\Extbase\Mvc\Web\Request;
20
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21
22
class PreviousLinkViewHelper extends AbstractTagBasedViewHelper
23
{
24
    const PREVIOUS_LINK_PARAMETER = 'fz-previous-step';
25
26
    /**
27
     * @var string
28
     */
29
    protected $tagName = 'a';
30
31
    /**
32
     * @var FormViewHelperService
33
     */
34
    protected $formService;
35
36
    public function initializeArguments()
37
    {
38
        parent::initializeArguments();
39
        $this->registerUniversalTagAttributes();
40
    }
41
42
    public function render()
43
    {
44
        /*
45
         * First, we check if this view helper is called from within the
46
         * `FormViewHelper`, because it would not make sense anywhere else.
47
         */
48
        if (false === $this->formService->formContextExists()) {
49
            // @todo
50
//            throw ContextNotFoundException::substepViewHelperFormContextNotFound();
51
        }
52
53
        $formObject = $this->formService->getFormObject();
54
        $formDefinition = $formObject->getDefinition();
55
56
        if (false === $formDefinition->hasSteps()) {
57
            throw new \Exception('todo'); // @todo
58
        }
59
60
        /** @var Request $request */
61
        $request = $this->controllerContext->getRequest();
62
        $currentStep = $formObject->fetchCurrentStep($request)->getCurrentStep();
63
        $stepDefinition = StepMiddlewareService::get()->getStepDefinition($currentStep);
64
65
        // @todo handle previous steps depth
66
        if (!$stepDefinition->hasPreviousDefinition()
67
            && !$currentStep->hasSubsteps()
68
        ) {
69
            return null;
70
        }
71
72
        $stepService = FormObjectFactory::get()->getStepService($formObject);
73
        $level = $stepService->getSubstepsLevel();
74
75
        $uriBuilder = $this->controllerContext->getUriBuilder();
76
        $uri = $uriBuilder
77
            ->reset()
78
            ->uriFor(null, [self::PREVIOUS_LINK_PARAMETER => $level]);
79
80
        $this->tag->addAttribute('href', $uri);
81
        $this->tag->setContent($this->renderChildren());
82
        $this->tag->forceClosingTag(true);
83
84
        return $this->tag->render();
85
    }
86
87
    /**
88
     * @param FormViewHelperService $service
89
     */
90
    public function injectFormService(FormViewHelperService $service)
91
    {
92
        $this->formService = $service;
93
    }
94
}
95