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