Completed
Push — wip/steps ( b2c91c...6fd1d1 )
by Romain
02:28
created

FocusLinkViewHelper::initializeArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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\Field;
15
16
use Romm\Formz\Exceptions\ContextNotFoundException;
17
use Romm\Formz\Form\Definition\Field\Field;
18
use Romm\Formz\Form\Definition\Step\Step\Step;
19
use Romm\Formz\Service\ViewHelper\Form\FormViewHelperService;
20
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21
22
/**
23
 * @todo
24
 */
25
class FocusLinkViewHelper extends AbstractTagBasedViewHelper
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $tagName = 'a';
31
32
    /**
33
     * @var FormViewHelperService
34
     */
35
    protected $formService;
36
37
    /**
38
     * Arguments initialization: does also use universal tags.
39
     */
40
    public function initializeArguments()
41
    {
42
        $this->registerUniversalTagAttributes();
43
44
        $this->registerArgument('field', 'string', 'Name of the field.', true);
45
        $this->registerArgument('step', 'string', 'Identifier of the step, if the field is present of several steps.');
46
        $this->registerArgument('additionalParams', 'array', 'Additional parameters passed to the URI.', false, []);
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function render()
53
    {
54
        $this->checkFormContext();
55
        $this->checkFieldExists();
56
57
        $this->tag->addAttribute('href', $this->getUri());
58
        $this->tag->setContent($this->renderChildren());
59
        $this->tag->forceClosingTag(true);
60
61
        return $this->tag->render();
62
    }
63
64
    /**
65
     * Builds and returns the URI to the given step, and adds parameters that
66
     * will allow the system to understand which field to edit.
67
     *
68
     * @return string
69
     */
70
    protected function getUri()
71
    {
72
        $pageUid = null;
73
        $action = null;
74
        $controller = null;
75
        $extensionName = null;
76
77
        $arguments = $this->arguments['additionalParams'];
78
79
        $step = $this->getStep();
80
81
        if ($step) {
82
            $pageUid = $step->getPageUid();
83
            $action = $step->getAction();
84
            $controller = $step->getController();
85
            $extensionName = $step->getExtension();
86
        }
87
88
        $uriBuilder = $this->controllerContext->getUriBuilder();
89
        $uri = $uriBuilder->reset();
90
91
        if ($pageUid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pageUid of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
92
            $uri->setTargetPageUid($pageUid);
93
        }
94
95
        return $uri->uriFor($action, $arguments, $controller, $extensionName);
96
97
    }
98
99
    /**
100
     * Checks that the step name given to the view helper exists in the form
101
     * definition, and returns it.
102
     *
103
     * Will also check that the given field name is supported by the step.
104
     *
105
     * @return Step
106
     * @throws \Exception
107
     */
108
    protected function getStep()
109
    {
110
        $formDefinition = $this->formService->getFormObject()->getDefinition();
111
        $stepIdentifier = $this->arguments['step'];
112
        $step = null;
113
114
        if ($stepIdentifier) {
115
            if (false === $formDefinition->getSteps()->hasEntry($stepIdentifier)) {
116
                throw new \Exception('@todo : the step "' . $stepIdentifier . '" does not exists.'); // @todo
117
            }
118
119
            $step = $formDefinition->getSteps()->getEntry($stepIdentifier);
120
            $field = $this->getField();
121
122
            if (false === $step->supportsField($field)) {
123
                throw new \Exception('@todo : the step "' . $step->getIdentifier() . '" does not support the field "' . $field->getName() . '".'); // @todo
124
            }
125
        }
126
127
        return $step;
128
    }
129
130
    /**
131
     * @throws ContextNotFoundException
132
     */
133
    protected function checkFormContext()
134
    {
135
        if (false === $this->formService->formContextExists()) {
136
            throw new ContextNotFoundException('form context not found'); // @todo
137
        }
138
    }
139
140
    /**
141
     * Checks that the given field name exists in the form definition.
142
     */
143
    protected function checkFieldExists()
144
    {
145
        $formDefinition = $this->formService->getFormObject()->getDefinition();
146
        $fieldName = $this->arguments['field'];
147
148
        if (false === $formDefinition->hasField($fieldName)) {
149
            throw new \Exception('field not found : ' . $fieldName); // @todo
150
        }
151
    }
152
153
    /**
154
     * Returns the field instance from the argument given to the view helper.
155
     *
156
     * @return Field
157
     */
158
    protected function getField()
159
    {
160
        return $this->formService
161
            ->getFormObject()
162
            ->getDefinition()
163
            ->getField($this->arguments['field']);
164
    }
165
166
    /**
167
     * @param FormViewHelperService $service
168
     */
169
    public function injectFormService(FormViewHelperService $service)
170
    {
171
        $this->formService = $service;
172
    }
173
}
174