Extbase:
[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 require_once(PATH_t3lib . 'interfaces/interface.t3lib_singleton.php');
16 require_once(PATH_tslib . 'class.tslib_content.php');
17
18 /**
19 * An URI Builder
20 *
21 * @package Extbase
22 * @subpackage MVC
23 * @version $Id$
24 * @internal
25 */
26 class Tx_Extbase_MVC_Web_Routing_URIBuilder {
27
28 /**
29 * An instance of tslib_cObj
30 *
31 * @var tslib_cObj
32 */
33 protected $contentObject;
34
35 /**
36 * @var Tx_Extbase_MVC_Request
37 */
38 protected $request;
39
40 /**
41 * Constructs this URI Helper
42 */
43 public function __construct(tslib_cObj $contentObject = NULL) {
44 $this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
45 }
46
47 /**
48 * Sets the current request
49 *
50 * @param Tx_Extbase_MVC_Request $request
51 * @return void
52 */
53 public function setRequest(Tx_Extbase_MVC_Request $request) {
54 $this->request = $request;
55 }
56
57 /**
58 * Creates an URI by making use of the typolink mechanism.
59 *
60 * @param integer $pageUid uid of the target page
61 * @param string $actionName Name of the action to be called
62 * @param array $arguments Additional query parameters, will be "namespaced"
63 * @param string $controllerName Name of the target controller
64 * @param string $extensionName Name of the target extension, without underscores. If NULL current ExtensionName is used.
65 * @param string $pluginName Name of the target plugin. If NULL current PluginName is used.
66 * @param integer $pageType type of the target page. See typolink.parameter
67 * @param boolean $noCache if TRUE, then no_cache=1 is appended to URI
68 * @param boolean $useCacheHash by default TRUE; if FALSE, disable the cHash
69 * @param string $section If specified, adds a given HTML anchor to the URI (#...)
70 * @param boolean $linkAccessRestrictedPages If TRUE, generates links for pages where the user does not have permission to see it
71 * @param array $additionalParams An additional params query array which will be appended to the URI (overrules $arguments)
72 * @return string the typolink URI
73 * @internal
74 */
75 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()) {
76 if ($actionName !== NULL) {
77 $arguments['action'] = $actionName;
78 }
79 if ($controllerName !== NULL) {
80 $arguments['controller'] = $controllerName;
81 } else {
82 $arguments['controller'] = $this->request->getControllerName();
83 }
84 if ($extensionName === NULL) {
85 $extensionName = $this->request->getControllerExtensionName();
86 }
87 if ($pluginName === NULL) {
88 $pluginName = $this->request->getPluginName();
89 }
90 $argumentPrefix = strtolower('tx_' . $extensionName . '_' . $pluginName);
91 $prefixedArguments = (count($arguments) > 0) ? array($argumentPrefix => $arguments) : array();
92 if (count($additionalParams) > 0) {
93 $prefixedArguments = t3lib_div::array_merge_recursive_overrule($prefixedArguments, $additionalParams);
94 }
95 $prefixedArguments = $this->convertDomainObjectsToIdentityArrays($prefixedArguments);
96
97 return $this->typolinkURI($pageUid, $prefixedArguments, $pageType, $noCache, $useCacheHash, $section, $linkAccessRestrictedPages);
98 }
99
100 /**
101 * Recursively iterates through the specified arguments and turns instances of type Tx_Extbase_DomainObject_AbstractEntity
102 * into an arrays containing the uid of the domain object.
103 *
104 * @param array $arguments The arguments to be iterated
105 * @return array The modified arguments array
106 * @internal
107 */
108 protected function convertDomainObjectsToIdentityArrays(array $arguments) {
109 foreach ($arguments as $argumentKey => $argumentValue) {
110 if ($argumentValue instanceof Tx_Extbase_DomainObject_AbstractEntity) {
111 $arguments[$argumentKey] = array('uid' => $argumentValue->getUid());
112 } elseif (is_array($argumentValue)) {
113 $arguments[$argumentKey] = $this->convertDomainObjectsToIdentityArrays($argumentValue);
114 }
115 }
116 return $arguments;
117 }
118
119 /**
120 * Get an URI from typolink_URL
121 *
122 * @param integer $pageUid uid of the target page
123 * @param array $arguments Additional query parameters, will be "namespaced"
124 * @param integer $pageType type of the target page. See typolink.parameter
125 * @param boolean $noCache if TRUE, then no_cache=1 is appended to URI
126 * @param boolean $useCacheHash by default TRUE; if FALSE, disable the cHash
127 * @param string $section If specified, adds a given HTML anchor to the URI (#...)
128 * @param boolean $linkAccessRestrictedPages If TRUE, generates links for pages where the user does not have permission to see it
129 * @return The URI
130 * @internal
131 */
132 public function typolinkURI($pageUid = NULL, array $arguments = array(), $pageType = 0, $noCache = FALSE, $useCacheHash = TRUE, $section = '', $linkAccessRestrictedPages = FALSE) {
133 if ($pageUid === NULL) {
134 $pageUid = $GLOBALS['TSFE']->id;
135 }
136
137 $typolinkConfiguration = array();
138 $typolinkConfiguration['parameter'] = $pageUid;
139 if ($pageType !== 0) {
140 $typolinkConfiguration['parameter'] .= ',' . $pageType;
141 }
142
143 if (count($arguments) > 0) {
144 $typolinkConfiguration['additionalParams'] = '&' . http_build_query($arguments, NULL, '&');
145 }
146
147 if ($noCache) {
148 $typolinkConfiguration['no_cache'] = 1;
149 // TODO: stdwrap
150 } elseif ($useCacheHash) {
151 $typolinkConfiguration['useCacheHash'] = 1;
152 }
153
154 if ($section !== '') {
155 $typolinkConfiguration['section'] = $section;
156 // TODO: stdwrap
157 }
158
159 if ($linkAccessRestrictedPages) {
160 $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
161 }
162
163 return $this->contentObject->typoLink_URL($typolinkConfiguration);
164 }
165 }
166 ?>