85405db18ca970e788837f8b690768d4292b03f5
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Widget / LinkViewHelper.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 Links to extbase actions within widets.
25 *
26 * = Examples =
27 *
28 * <code title="URI to the show-action of the current controller">
29 * <f:widget.link action="show">link</f:widget.link>
30 * </code>
31 * <output>
32 * <a href="index.php?id=123&tx_myextension_plugin[widgetIdentifier][action]=show&tx_myextension_plugin[widgetIdentifier][controller]=Standard&cHash=xyz">link</a>
33 * (depending on the current page, widget and your TS configuration)
34 * </output>
35 *
36 * @api
37 */
38 class LinkViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
39
40 /**
41 * @var string
42 */
43 protected $tagName = 'a';
44
45 /**
46 * Initialize arguments
47 *
48 * @return void
49 * @api
50 */
51 public function initializeArguments() {
52 $this->registerUniversalTagAttributes();
53 $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
54 $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
55 $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
56 $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
57 $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
58 }
59
60 /**
61 * Render the link.
62 *
63 * @param string $action Target action
64 * @param array $arguments Arguments
65 * @param string $section The anchor to be added to the URI
66 * @param string $format The requested format, e.g. ".html
67 * @param bool $ajax TRUE if the URI should be to an AJAX widget, FALSE otherwise.
68 * @return string The rendered link
69 * @api
70 */
71 public function render($action = NULL, $arguments = array(), $section = '', $format = '', $ajax = FALSE) {
72 if ($ajax === TRUE) {
73 $uri = $this->getAjaxUri();
74 } else {
75 $uri = $this->getWidgetUri();
76 }
77 $this->tag->addAttribute('href', $uri);
78 $this->tag->setContent($this->renderChildren());
79 return $this->tag->render();
80 }
81
82 /**
83 * Get the URI for an AJAX Request.
84 *
85 * @return string the AJAX URI
86 */
87 protected function getAjaxUri() {
88 $action = $this->arguments['action'];
89 $arguments = $this->arguments['arguments'];
90 if ($action === NULL) {
91 $action = $this->controllerContext->getRequest()->getControllerActionName();
92 }
93 $arguments['id'] = $GLOBALS['TSFE']->id;
94 // TODO page type should be configurable
95 $arguments['type'] = 7076;
96 $arguments['fluid-widget-id'] = $this->controllerContext->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
97 $arguments['action'] = $action;
98 return '?' . http_build_query($arguments, NULL, '&');
99 }
100
101 /**
102 * Get the URI for a non-AJAX Request.
103 *
104 * @return string the Widget URI
105 */
106 protected function getWidgetUri() {
107 $uriBuilder = $this->controllerContext->getUriBuilder();
108 $argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
109 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : array();
110 if ($this->hasArgument('action')) {
111 $arguments['action'] = $this->arguments['action'];
112 }
113 if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
114 $arguments['format'] = $this->arguments['format'];
115 }
116 if ($this->hasArgument('addQueryStringMethod') && $this->arguments['addQueryStringMethod'] !== '') {
117 $arguments['addQueryStringMethod'] = $this->arguments['addQueryStringMethod'];
118 }
119 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();
120 }
121 }