cb11a7f35230bb3167ea7c802ec7729fbc9a5376
[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() {
44 $this->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 string $additionalParams An additional params query string which will be appended to the URI
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, $additionalParams = '') {
76 if (is_array($arguments)) {
77 foreach ($arguments as $argumentKey => $argumentValue) {
78 if ($argumentValue instanceof Tx_Extbase_DomainObject_AbstractEntity) {
79 $arguments[$argumentKey] = array('uid' => $argumentValue->getUid());
80 }
81 }
82 }
83 if ($actionName !== NULL) {
84 $arguments['action'] = $actionName;
85 }
86 if ($controllerName !== NULL) {
87 $arguments['controller'] = $controllerName;
88 } else {
89 $arguments['controller'] = $this->request->getControllerName();
90 }
91 if ($extensionName === NULL) {
92 $extensionName = $this->request->getControllerExtensionName();
93 }
94 if ($pluginName === NULL) {
95 $pluginName = $this->request->getPluginName();
96 }
97 $argumentPrefix = strtolower('tx_' . $extensionName . '_' . $pluginName);
98 $prefixedArguments = (count($arguments) > 0) ? array($argumentPrefix => $arguments) : array();
99
100 return $this->typolinkURI($pageUid, $prefixedArguments, $pageType, $noCache, $useCacheHash, $section, $linkAccessRestrictedPages, $additionalParams);
101 }
102
103 /**
104 * Get an URI from typolink_URL
105 *
106 * @param integer $pageUid uid of the target page
107 * @param array $arguments Additional query parameters, will be "namespaced"
108 * @param integer $pageType type of the target page. See typolink.parameter
109 * @param boolean $noCache if TRUE, then no_cache=1 is appended to URI
110 * @param boolean $useCacheHash by default TRUE; if FALSE, disable the cHash
111 * @param string $section If specified, adds a given HTML anchor to the URI (#...)
112 * @param boolean $linkAccessRestrictedPages If TRUE, generates links for pages where the user does not have permission to see it
113 * @param string $additionalParams An additional params query string which will be appended to the URI
114 * @return The URI
115 * @internal
116 */
117 public function typolinkURI($pageUid = NULL, array $arguments = array(), $pageType = 0, $noCache = FALSE, $useCacheHash = TRUE, $section = '', $linkAccessRestrictedPages = FALSE, $additionalParams = '') {
118 if ($pageUid === NULL) {
119 $pageUid = $GLOBALS['TSFE']->id;
120 }
121
122 $typolinkConfiguration = array();
123 $typolinkConfiguration['parameter'] = $pageUid;
124 if ($pageType !== 0) {
125 $typolinkConfiguration['parameter'] .= ',' . $pageType;
126 }
127 $typolinkConfiguration['additionalParams'] = '';
128 if (count($arguments) > 0) {
129 $typolinkConfiguration['additionalParams'] .= '&' . http_build_query($arguments, NULL, '&');
130 }
131
132 if ($noCache) {
133 $typolinkConfiguration['no_cache'] = 1;
134 // TODO: stdwrap
135 }
136
137 if ($useCacheHash) {
138 $typolinkConfiguration['useCacheHash'] = 1;
139 }
140
141 if ($section !== '') {
142 $typolinkConfiguration['section'] = $section;
143 // TODO: stdwrap
144 }
145
146 if ($linkAccessRestrictedPages === TRUE) {
147 $typolinkConfiguration['linkAccessRestrictedPages'] = $linkAccessRestrictedPages;
148 }
149
150 if (isset($options['additionalParams'])) {
151 // TODO: Stdwrap
152 // TODO FIX THIS: $typolinkConfiguration['additionalParams'] .= $this->contentObject->stdWrap($options['additionalParams'], isset($options['additionalParams.']) ? $options['additionalParams.'] : array());
153 $typolinkConfiguration['additionalParams'] .= $additionalParams;
154 }
155
156 return $this->contentObject->typoLink_URL($typolinkConfiguration);
157 }
158 }
159 ?>