fe7b110a1afcf6a7b9fe89cafc725c6d46b006c5
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Widget / UriViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\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 /**
18 * A view helper for creating URIs to extbase actions within widgets.
19 *
20 * = Examples =
21 *
22 * <code title="URI to the show-action of the current controller">
23 * <f:widget.uri action="show" />
24 * </code>
25 * <output>
26 * index.php?id=123&tx_myextension_plugin[widgetIdentifier][action]=show&tx_myextension_plugin[widgetIdentifier][controller]=Standard&cHash=xyz
27 * (depending on the current page, widget and your TS configuration)
28 * </output>
29 *
30 * @api
31 */
32 class UriViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
33 {
34 /**
35 * Initialize arguments
36 *
37 * @api
38 */
39 public function initializeArguments()
40 {
41 parent::initializeArguments();
42 $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
43 $this->registerArgument('action', 'string', 'Target action');
44 $this->registerArgument('arguments', 'array', 'Arguments', false, []);
45 $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
46 $this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
47 $this->registerArgument('ajax', 'bool', 'TRUE if the URI should be to an AJAX widget, FALSE otherwise.', false, false);
48 }
49
50 /**
51 * Render the Uri.
52 *
53 * @return string The rendered link
54 * @api
55 */
56 public function render()
57 {
58 $ajax = $this->arguments['ajax'];
59
60 if ($ajax === true) {
61 return $this->getAjaxUri();
62 } else {
63 return $this->getWidgetUri();
64 }
65 }
66
67 /**
68 * Get the URI for an AJAX Request.
69 *
70 * @return string the AJAX URI
71 */
72 protected function getAjaxUri()
73 {
74 $action = $this->arguments['action'];
75 $arguments = $this->arguments['arguments'];
76 if ($action === null) {
77 $action = $this->controllerContext->getRequest()->getControllerActionName();
78 }
79 $arguments['id'] = $GLOBALS['TSFE']->id;
80 // @todo page type should be configurable
81 $arguments['type'] = 7076;
82 $arguments['fluid-widget-id'] = $this->controllerContext->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
83 $arguments['action'] = $action;
84 return '?' . http_build_query($arguments, null, '&');
85 }
86
87 /**
88 * Get the URI for a non-AJAX Request.
89 *
90 * @return string the Widget URI
91 */
92 protected function getWidgetUri()
93 {
94 $uriBuilder = $this->controllerContext->getUriBuilder();
95 $argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
96 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : [];
97 if ($this->hasArgument('action')) {
98 $arguments['action'] = $this->arguments['action'];
99 }
100 if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
101 $arguments['format'] = $this->arguments['format'];
102 }
103 return $uriBuilder->reset()
104 ->setArguments([$argumentPrefix => $arguments])
105 ->setSection($this->arguments['section'])
106 ->setAddQueryString(true)
107 ->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])
108 ->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash'])
109 ->setFormat($this->arguments['format'])
110 ->build();
111 }
112 }