6bafa1a20e14ebcd8905cf6a91782aecc95fab99
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Web / Routing / URIBuilder.php
1 <?php
2 /* *
3 * This script is part of the TYPO3 project - inspiring people to share! *
4 * *
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. *
8 * *
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. *
13 * */
14
15 /**
16 * An URI Builder
17 *
18 * @package Extbase
19 * @subpackage MVC\Web\Routing
20 * @version $Id$
21 */
22 class Tx_Extbase_MVC_Web_Routing_URIBuilder {
23
24 /**
25 * An instance of tslib_cObj
26 *
27 * @var tslib_cObj
28 */
29 protected $contentObject;
30
31 /**
32 * @var Tx_Extbase_MVC_Request
33 */
34 protected $request;
35
36 /**
37 * Constructs this URI Helper
38 */
39 public function __construct(tslib_cObj $contentObject = NULL) {
40 $this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
41 }
42
43 /**
44 * Sets the current request
45 *
46 * @param Tx_Extbase_MVC_Request $request
47 * @return void
48 */
49 public function setRequest(Tx_Extbase_MVC_Request $request) {
50 $this->request = $request;
51 }
52
53 /**
54 * Creates an URI by making use of the typolink mechanism.
55 *
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
70 */
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;
74 }
75 if ($controllerName !== NULL) {
76 $arguments['controller'] = $controllerName;
77 } else {
78 $arguments['controller'] = $this->request->getControllerName();
79 }
80 if ($extensionName === NULL) {
81 $extensionName = $this->request->getControllerExtensionName();
82 }
83 if ($pluginName === NULL) {
84 $pluginName = $this->request->getPluginName();
85 }
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);
90 }
91 $prefixedArguments = $this->convertDomainObjectsToIdentityArrays($prefixedArguments);
92
93 return $this->typolinkURI($pageUid, $prefixedArguments, $pageType, $noCache, $useCacheHash, $section, $linkAccessRestrictedPages, $absolute);
94 }
95
96 /**
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.
99 *
100 * @param array $arguments The arguments to be iterated
101 * @return array The modified arguments array
102 */
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);
109 }
110 }
111 return $arguments;
112 }
113
114 /**
115 * Get an URI from typolink_URL
116 *
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
125 * @return The URI
126 */
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;
130 }
131
132 $typolinkConfiguration = array();
133 $typolinkConfiguration['parameter'] = $pageUid;
134 if ($pageType !== 0) {
135 $typolinkConfiguration['parameter'] .= ',' . $pageType;
136 }
137
138 if (count($arguments) > 0) {
139 $typolinkConfiguration['additionalParams'] = '&' . http_build_query($arguments, NULL, '&');
140 }
141
142 if ($noCache) {
143 $typolinkConfiguration['no_cache'] = 1;
144 // TODO: stdwrap
145 } elseif ($useCacheHash) {
146 $typolinkConfiguration['useCacheHash'] = 1;
147 }
148
149 if ($section !== '') {
150 $typolinkConfiguration['section'] = $section;
151 // TODO: stdwrap
152 }
153
154 if ($linkAccessRestrictedPages) {
155 $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
156 }
157
158 return ($absolute === TRUE ? $this->request->getBaseURI() : '') . $this->contentObject->typoLink_URL($typolinkConfiguration);
159 }
160 }
161 ?>