c7cc1af7df39974eae4bb32cc4faaa7e5f3dfe1d
[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 script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * A view helper for creating URIs to extbase actions within widgets.
25 *
26 * = Examples =
27 *
28 * <code title="URI to the show-action of the current controller">
29 * <f:widget.uri action="show" />
30 * </code>
31 * <output>
32 * index.php?id=123&tx_myextension_plugin[widgetIdentifier][action]=show&tx_myextension_plugin[widgetIdentifier][controller]=Standard&cHash=xyz
33 * (depending on the current page, widget and your TS configuration)
34 * </output>
35 *
36 * @api
37 */
38 class UriViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
39
40 /**
41 * Initialize arguments
42 *
43 * @return void
44 * @api
45 */
46 public function initializeArguments() {
47 $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
48 }
49
50 /**
51 * Render the Uri.
52 *
53 * @param string $action Target action
54 * @param array $arguments Arguments
55 * @param string $section The anchor to be added to the URI
56 * @param string $format The requested format, e.g. ".html
57 * @param bool $ajax TRUE if the URI should be to an AJAX widget, FALSE otherwise.
58 * @return string The rendered link
59 * @api
60 */
61 public function render($action = NULL, $arguments = array(), $section = '', $format = '', $ajax = FALSE) {
62 if ($ajax === TRUE) {
63 return $this->getAjaxUri();
64 } else {
65 return $this->getWidgetUri();
66 }
67 }
68
69 /**
70 * Get the URI for an AJAX Request.
71 *
72 * @return string the AJAX URI
73 */
74 protected function getAjaxUri() {
75 $action = $this->arguments['action'];
76 $arguments = $this->arguments['arguments'];
77 if ($action === NULL) {
78 $action = $this->controllerContext->getRequest()->getControllerActionName();
79 }
80 $arguments['id'] = $GLOBALS['TSFE']->id;
81 // TODO page type should be configurable
82 $arguments['type'] = 7076;
83 $arguments['fluid-widget-id'] = $this->controllerContext->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
84 $arguments['action'] = $action;
85 return '?' . http_build_query($arguments, NULL, '&');
86 }
87
88 /**
89 * Get the URI for a non-AJAX Request.
90 *
91 * @return string the Widget URI
92 */
93 protected function getWidgetUri() {
94 $uriBuilder = $this->controllerContext->getUriBuilder();
95 $argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
96 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : array();
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 if ($this->hasArgument('addQueryStringMethod') && $this->arguments['addQueryStringMethod'] !== '') {
104 $arguments['addQueryStringMethod'] = $this->arguments['addQueryStringMethod'];
105 }
106 return $uriBuilder->reset()->setArguments(array($argumentPrefix => $arguments))->setSection($this->arguments['section'])->setAddQueryString(TRUE)->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])->setArgumentsToBeExcludedFromQueryString(array($argumentPrefix, 'cHash'))->setFormat($this->arguments['format'])->build();
107 }
108 }