3 * This script is part of the TYPO3 project - inspiring people to share! *
5 * TYPO3 is free software; you can redistribute it and/or modify it under *
6 * the terms of the GNU General Public License version 2 as published by *
7 * the Free Software Foundation. *
9 * This script is distributed in the hope that it will be useful, but *
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
11 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
12 * Public License for more details. *
19 * @subpackage MVC\Web\Routing
22 class Tx_Extbase_MVC_Web_Routing_URIBuilder
{
25 * An instance of tslib_cObj
29 protected $contentObject;
32 * @var Tx_Extbase_MVC_Request
37 * Constructs this URI Helper
39 public function __construct(tslib_cObj
$contentObject = NULL) {
40 $this->contentObject
= $contentObject !== NULL ?
$contentObject : t3lib_div
::makeInstance('tslib_cObj');
44 * Sets the current request
46 * @param Tx_Extbase_MVC_Request $request
49 public function setRequest(Tx_Extbase_MVC_Request
$request) {
50 $this->request
= $request;
54 * Creates an URI by making use of the typolink mechanism.
56 * @param integer $pageUid uid of the target page
57 * @param string $actionName Name of the action to be called
58 * @param array $arguments Additional query parameters, will be "namespaced"
59 * @param string $controllerName Name of the target controller
60 * @param string $extensionName Name of the target extension, without underscores. If NULL current ExtensionName is used.
61 * @param string $pluginName Name of the target plugin. If NULL current PluginName is used.
62 * @param integer $pageType type of the target page. See typolink.parameter
63 * @param boolean $noCache if TRUE, then no_cache=1 is appended to URI
64 * @param boolean $useCacheHash by default TRUE; if FALSE, disable the cHash
65 * @param string $section If specified, adds a given HTML anchor to the URI (#...)
66 * @param boolean $linkAccessRestrictedPages If TRUE, generates links for pages where the user does not have permission to see it
67 * @param array $additionalParams An additional params query array which will be appended to the URI (overrules $arguments)
68 * @param boolean $absolute If set, the URI is prepended with the base URI
69 * @return string the typolink URI
71 public function URIFor($pageUid = NULL, $actionName = NULL, $arguments = array(), $controllerName = NULL, $extensionName = NULL, $pluginName = NULL, $pageType = 0, $noCache = FALSE, $useCacheHash = TRUE, $section = '', $linkAccessRestrictedPages = FALSE, array $additionalParams = array(), $absolute = FALSE) {
72 if ($actionName !== NULL) {
73 $arguments['action'] = $actionName;
75 if ($controllerName !== NULL) {
76 $arguments['controller'] = $controllerName;
78 $arguments['controller'] = $this->request
->getControllerName();
80 if ($extensionName === NULL) {
81 $extensionName = $this->request
->getControllerExtensionName();
83 if ($pluginName === NULL) {
84 $pluginName = $this->request
->getPluginName();
86 $argumentPrefix = strtolower('tx_' . $extensionName . '_' . $pluginName);
87 $prefixedArguments = (count($arguments) > 0) ?
array($argumentPrefix => $arguments) : array();
88 if (count($additionalParams) > 0) {
89 $prefixedArguments = t3lib_div
::array_merge_recursive_overrule($prefixedArguments, $additionalParams);
91 $prefixedArguments = $this->convertDomainObjectsToIdentityArrays($prefixedArguments);
93 return $this->typolinkURI($pageUid, $prefixedArguments, $pageType, $noCache, $useCacheHash, $section, $linkAccessRestrictedPages, $absolute);
97 * Recursively iterates through the specified arguments and turns instances of type Tx_Extbase_DomainObject_AbstractEntity
98 * into an arrays containing the uid of the domain object.
100 * @param array $arguments The arguments to be iterated
101 * @return array The modified arguments array
103 protected function convertDomainObjectsToIdentityArrays(array $arguments) {
104 foreach ($arguments as $argumentKey => $argumentValue) {
105 if ($argumentValue instanceof Tx_Extbase_DomainObject_AbstractEntity
) {
106 $arguments[$argumentKey] = array('uid' => $argumentValue->getUid());
107 } elseif (is_array($argumentValue)) {
108 $arguments[$argumentKey] = $this->convertDomainObjectsToIdentityArrays($argumentValue);
115 * Get an URI from typolink_URL
117 * @param integer $pageUid uid of the target page
118 * @param array $arguments Additional query parameters, will be "namespaced"
119 * @param integer $pageType type of the target page. See typolink.parameter
120 * @param boolean $noCache if TRUE, then no_cache=1 is appended to URI
121 * @param boolean $useCacheHash by default TRUE; if FALSE, disable the cHash
122 * @param string $section If specified, adds a given HTML anchor to the URI (#...)
123 * @param boolean $linkAccessRestrictedPages If TRUE, generates links for pages where the user does not have permission to see it
124 * @param boolean $absolute If set, the URI is prepended with the base URI
127 public function typolinkURI($pageUid = NULL, array $arguments = array(), $pageType = 0, $noCache = FALSE, $useCacheHash = TRUE, $section = '', $linkAccessRestrictedPages = FALSE, $absolute = FALSE) {
128 if ($pageUid === NULL) {
129 $pageUid = $GLOBALS['TSFE']->id
;
132 $typolinkConfiguration = array();
133 $typolinkConfiguration['parameter'] = $pageUid;
134 if ($pageType !== 0) {
135 $typolinkConfiguration['parameter'] .= ',' . $pageType;
138 if (count($arguments) > 0) {
139 $typolinkConfiguration['additionalParams'] = '&' . http_build_query($arguments, NULL, '&');
143 $typolinkConfiguration['no_cache'] = 1;
145 } elseif ($useCacheHash) {
146 $typolinkConfiguration['useCacheHash'] = 1;
149 if ($section !== '') {
150 $typolinkConfiguration['section'] = $section;
154 if ($linkAccessRestrictedPages) {
155 $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
158 return ($absolute === TRUE ?
$this->request
->getBaseURI() : '') . $this->contentObject
->typoLink_URL($typolinkConfiguration);