Issues (216)

Classes/Widget/AbstractWidgetViewHelper.php (1 issue)

Severity
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Widget;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
18
use ApacheSolrForTypo3\Solr\Widget\WidgetRequest as SolrFluidWidgetRequest;
19
use TYPO3\CMS\Extbase\Mvc\Web\Response;
20
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
23
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper as AbstractCoreWidgetViewHelper;
24
use TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder;
25
use TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException;
26
use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest as CoreWidgetRequest;
27
use TYPO3\CMS\Fluid\Core\Widget\WidgetContext;
28
use TYPO3\CMS\Extbase\Service\ExtensionService;
29
30
31
/**
32
 * Class AbstractWidgetViewHelper
33
 *
34
 * This is almost a exact copy of \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
35
 * because we need to switch the request object to overrule the widget prefix for url params
36
 * todo: find cleaner way to let widget listen to tx_solr[] instead of tx_solr[@widget]
37
 *
38
 * @author Frans Saris <[email protected]>
39
 * @author Timo Hund <[email protected]>
40
 */
41
abstract class AbstractWidgetViewHelper extends AbstractCoreWidgetViewHelper implements ViewHelperInterface
42
{
43
44
    /**
45
     * The Controller associated to this widget.
46
     * This needs to be filled by the individual subclass by di
47
     * annotation.
48
     *
49
     * @var AbstractWidgetController
50
     * @api
51
     */
52
    protected $controller;
53
54
    /**
55
     * If set to TRUE, it is an AJAX widget.
56
     *
57
     * @var boolean
58
     * @api
59
     */
60
    protected $ajaxWidget = false;
61
62
    /**
63
     * @var AjaxWidgetContextHolder
64
     */
65
    private $ajaxWidgetContextHolder;
66
67
    /**
68
     * @var ObjectManagerInterface
69
     */
70
    protected $objectManager;
71
72
    /**
73
     * @var ExtensionService
74
     */
75
    protected $extensionService;
76
77
    /**
78
     * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
79
     */
80
    private $widgetContext;
81
82
    /**
83
     * @param ExtensionService $extensionService
84
     */
85
    public function injectExtensionService(ExtensionService $extensionService)
86 36
    {
87
        $this->extensionService = $extensionService;
88 36
    }
89 36
90
    /**
91
     * @param AjaxWidgetContextHolder $ajaxWidgetContextHolder
92
     * @return void
93
     */
94
    public function injectAjaxWidgetContextHolder(AjaxWidgetContextHolder $ajaxWidgetContextHolder)
95 36
    {
96
        $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
97 36
    }
98 36
99
    /**
100
     * @param ObjectManagerInterface $objectManager
101
     * @return void
102
     */
103
    public function injectObjectManager(ObjectManagerInterface $objectManager)
104 36
    {
105
        $this->objectManager = $objectManager;
106 36
        $this->widgetContext = $this->objectManager->get(WidgetContext::class);
107 36
    }
108 36
109
    /**
110
     * Initialize the arguments of the ViewHelper, and call the render() method of the ViewHelper.
111
     *
112
     * @return string the rendered ViewHelper.
113
     */
114
    public function initializeArgumentsAndRender()
115 35
    {
116
        $this->validateArguments();
117 35
        $this->initialize();
118 35
        $this->initializeWidgetContext();
119 35
        return $this->callRenderMethod();
120 35
    }
121
122
    /**
123
     * @return SolrControllerContext
124
     * @throws \InvalidArgumentException
125
     */
126
    protected function getControllerContext()
127
    {
128 35
        $controllerContext = null;
129
        if (method_exists($this->renderingContext, 'getControllerContext')) {
130 35
            $controllerContext = $this->renderingContext->getControllerContext();
131 35
        }
132 35
133
        if (!$controllerContext instanceof SolrControllerContext) {
134
            throw new \InvalidArgumentException('No valid SolrControllerContext found', 1512998673);
135
        }
136
137 35
        return $controllerContext;
138
    }
139
140
    /**
141 35
     * Initialize the Widget Context, before the Render method is called.
142
     *
143
     * @return void
144
     */
145
    private function initializeWidgetContext()
146
    {
147
        $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
148
        $this->initializeWidgetIdentifier();
149 35
        $this->widgetContext->setControllerObjectName(get_class($this->controller));
150
151 35
        $extensionName = $this->getControllerContext()->getRequest()->getControllerExtensionName();
152 35
        $pluginName = $this->getControllerContext()->getRequest()->getPluginName();
153 35
        $this->widgetContext->setParentExtensionName($extensionName);
154
        $this->widgetContext->setParentPluginName($pluginName);
155 35
        $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
156 35
        $this->widgetContext->setParentPluginNamespace($pluginNamespace);
157 35
        $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
158 35
159 35
        if ($this->ajaxWidget === true) {
160 35
            $this->ajaxWidgetContextHolder->store($this->widgetContext);
161 35
        }
162
    }
163 35
164
    /**
165
     * Stores the syntax tree child nodes in the Widget Context, so they can be
166 35
     * rendered with <f:widget.renderChildren> lateron.
167
     *
168
     * @param array $childNodes The SyntaxTree Child nodes of this ViewHelper.
169
     * @return void
170
     */
171
    public function setChildNodes(array $childNodes)
172
    {
173
        $rootNode = $this->objectManager->get(RootNode::class);
174
        foreach ($childNodes as $childNode) {
175 36
            $rootNode->addChildNode($childNode);
176
        }
177 36
        $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
178 36
    }
179 36
180
    /**
181 36
     * Generate the configuration for this widget. Override to adjust.
182 36
     *
183
     * @return array
184
     * @api
185
     */
186
    protected function getWidgetConfiguration()
187
    {
188
        return $this->arguments;
189
    }
190 35
191
    /**
192 35
     * Initiate a sub request to $this->controller. Make sure to fill $this->controller
193
     * via Dependency Injection.
194
     *
195
     * @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface the response of this request.
196
     * @throws MissingControllerException
197
     */
198
    protected function initiateSubRequest()
199
    {
200
        if (!$this->controller instanceof AbstractWidgetController) {
0 ignored issues
show
$this->controller is always a sub-type of ApacheSolrForTypo3\Solr\...bstractWidgetController.
Loading history...
201
            if (isset($this->controller)) {
202 35
                throw new MissingControllerException('initiateSubRequest() can not be called if there is no valid controller extending TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController. Got "' . get_class($this->controller) . '" in class "' . get_class($this) . '".', 1289422564);
203
            }
204 35
            throw new MissingControllerException('initiateSubRequest() can not be called if there is no controller inside $this->controller. Make sure to add a corresponding injectController method to your WidgetViewHelper class "' . get_class($this) . '".', 1284401632);
205
        }
206
            /** @var $subRequest \ApacheSolrForTypo3\Solr\Widget\WidgetRequest */
207
        $subRequest = $this->objectManager->get(SolrFluidWidgetRequest::class);
208
        $subRequest->setWidgetContext($this->widgetContext);
209
210
        $this->passArgumentsToSubRequest($subRequest);
211 35
        $subResponse = $this->objectManager->get(Response::class);
212 35
        $this->controller->processRequest($subRequest, $subResponse);
213
        return $subResponse;
214 35
    }
215 35
216 35
    /**
217 35
     * Pass the arguments of the widget to the subrequest.
218
     *
219
     * @param CoreWidgetRequest $subRequest
220
     * @return void
221
     */
222
    private function passArgumentsToSubRequest(CoreWidgetRequest $subRequest)
223
    {
224
        $arguments = $this->getControllerContext()->getRequest()->getArguments();
225
226 35
        if (isset($arguments)) {
227
            if (isset($arguments['action'])) {
228 35
                $subRequest->setControllerActionName($arguments['action']);
229
                unset($arguments['action']);
230 35
            }
231 35
            $subRequest->setArguments($arguments);
232
        }
233
    }
234
235 35
    /**
236
     * The widget identifier is unique on the current page, and is used
237 35
     * in the URI as a namespace for the widget's arguments.
238
     *
239
     * @return string the widget identifier for this widget
240
     * @return void
241
     * @todo clean up, and make it somehow more routing compatible.
242
     */
243
    private function initializeWidgetIdentifier()
244
    {
245
        $this->widgetContext->setWidgetIdentifier('');
246
    }
247
}
248