[TASK] Move arguments to initializeArguments() in UriVH in ext:fluid
[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 parent::initializeArguments();
43 $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
44 $this->registerArgument('action', 'string', 'Target action');
45 $this->registerArgument('arguments', 'array', 'Arguments', false, array());
46 $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
47 $this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
48 $this->registerArgument('ajax', 'bool', 'TRUE if the URI should be to an AJAX widget, FALSE otherwise.', false, false);
49 }
50
51 /**
52 * Render the Uri.
53 *
54 * @return string The rendered link
55 * @api
56 */
57 public function render()
58 {
59 $ajax = $this->arguments['ajax'];
60
61 if ($ajax === true) {
62 return $this->getAjaxUri();
63 } else {
64 return $this->getWidgetUri();
65 }
66 }
67
68 /**
69 * Get the URI for an AJAX Request.
70 *
71 * @return string the AJAX URI
72 */
73 protected function getAjaxUri()
74 {
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 {
95 $uriBuilder = $this->controllerContext->getUriBuilder();
96 $argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
97 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : array();
98 if ($this->hasArgument('action')) {
99 $arguments['action'] = $this->arguments['action'];
100 }
101 if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
102 $arguments['format'] = $this->arguments['format'];
103 }
104 return $uriBuilder->reset()
105 ->setArguments(array($argumentPrefix => $arguments))
106 ->setSection($this->arguments['section'])
107 ->setAddQueryString(true)
108 ->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])
109 ->setArgumentsToBeExcludedFromQueryString(array($argumentPrefix, 'cHash'))
110 ->setFormat($this->arguments['format'])
111 ->build();
112 }
113 }