bfc7820b914be70fb2e171d6c5d2291ca5ab5846
[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 * Render the Uri.
42 *
43 * @param string $action Target action
44 * @param array $arguments Arguments
45 * @param string $section The anchor to be added to the URI
46 * @param string $format The requested format, e.g. ".html
47 * @param boolean $ajax TRUE if the URI should be to an AJAX widget, FALSE otherwise.
48 * @return string The rendered link
49 * @api
50 */
51 public function render($action = NULL, $arguments = array(), $section = '', $format = '', $ajax = FALSE) {
52 if ($ajax === TRUE) {
53 return $this->getAjaxUri();
54 } else {
55 return $this->getWidgetUri();
56 }
57 }
58
59 /**
60 * Get the URI for an AJAX Request.
61 *
62 * @return string the AJAX URI
63 */
64 protected function getAjaxUri() {
65 $action = $this->arguments['action'];
66 $arguments = $this->arguments['arguments'];
67 if ($action === NULL) {
68 $action = $this->controllerContext->getRequest()->getControllerActionName();
69 }
70 $arguments['id'] = $GLOBALS['TSFE']->id;
71 // TODO page type should be configurable
72 $arguments['type'] = 7076;
73 $arguments['fluid-widget-id'] = $this->controllerContext->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
74 $arguments['action'] = $action;
75 return '?' . http_build_query($arguments, NULL, '&');
76 }
77
78 /**
79 * Get the URI for a non-AJAX Request.
80 *
81 * @return string the Widget URI
82 */
83 protected function getWidgetUri() {
84 $uriBuilder = $this->controllerContext->getUriBuilder();
85 $argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
86 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : array();
87 if ($this->hasArgument('action')) {
88 $arguments['action'] = $this->arguments['action'];
89 }
90 if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
91 $arguments['format'] = $this->arguments['format'];
92 }
93 return $uriBuilder->reset()->setArguments(array($argumentPrefix => $arguments))->setSection($this->arguments['section'])->setAddQueryString(TRUE)->setArgumentsToBeExcludedFromQueryString(array($argumentPrefix, 'cHash'))->setFormat($this->arguments['format'])->build();
94 }
95 }