3a2392b539e8b48eec693c6a2da749ed1c1d6de0
[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 }
58
59 /**
60 * Render the link.
61 *
62 * @param string $action Target action
63 * @param array $arguments Arguments
64 * @param string $section The anchor to be added to the URI
65 * @param string $format The requested format, e.g. ".html
66 * @param boolean $ajax TRUE if the URI should be to an AJAX widget, FALSE otherwise.
67 * @return string The rendered link
68 * @api
69 */
70 public function render($action = NULL, $arguments = array(), $section = '', $format = '', $ajax = FALSE) {
71 if ($ajax === TRUE) {
72 $uri = $this->getAjaxUri();
73 } else {
74 $uri = $this->getWidgetUri();
75 }
76 $this->tag->addAttribute('href', $uri);
77 $this->tag->setContent($this->renderChildren());
78 return $this->tag->render();
79 }
80
81 /**
82 * Get the URI for an AJAX Request.
83 *
84 * @return string the AJAX URI
85 */
86 protected function getAjaxUri() {
87 $action = $this->arguments['action'];
88 $arguments = $this->arguments['arguments'];
89 if ($action === NULL) {
90 $action = $this->controllerContext->getRequest()->getControllerActionName();
91 }
92 $arguments['id'] = $GLOBALS['TSFE']->id;
93 // TODO page type should be configurable
94 $arguments['type'] = 7076;
95 $arguments['fluid-widget-id'] = $this->controllerContext->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
96 $arguments['action'] = $action;
97 return '?' . http_build_query($arguments, NULL, '&');
98 }
99
100 /**
101 * Get the URI for a non-AJAX Request.
102 *
103 * @return string the Widget URI
104 */
105 protected function getWidgetUri() {
106 $uriBuilder = $this->controllerContext->getUriBuilder();
107 $argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
108 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : array();
109 if ($this->hasArgument('action')) {
110 $arguments['action'] = $this->arguments['action'];
111 }
112 if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
113 $arguments['format'] = $this->arguments['format'];
114 }
115 return $uriBuilder->reset()->setArguments(array($argumentPrefix => $arguments))->setSection($this->arguments['section'])->setAddQueryString(TRUE)->setArgumentsToBeExcludedFromQueryString(array($argumentPrefix, 'cHash'))->setFormat($this->arguments['format'])->build();
116 }
117 }