|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Creates the target url for links |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package URL |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\url; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Creates the target url for links |
|
20
|
|
|
* |
|
21
|
|
|
* PHP Version 5 |
|
22
|
|
|
* |
|
23
|
|
|
* @category Core |
|
24
|
|
|
* @package URL |
|
25
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
26
|
|
|
* @copyright 2013 cSphere Team |
|
27
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
28
|
|
|
* @link http://www.csphere.eu |
|
29
|
|
|
**/ |
|
30
|
|
|
|
|
31
|
|
|
abstract class Link |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Checks if options are fetched already |
|
35
|
|
|
**/ |
|
36
|
|
|
private static $_init = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Stores if ajax links are activated |
|
40
|
|
|
**/ |
|
41
|
|
|
private static $_ajax = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Stores if pretty links are activated |
|
45
|
|
|
**/ |
|
46
|
|
|
private static $_pretty = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Stores the request array data |
|
50
|
|
|
**/ |
|
51
|
|
|
private static $_request = ''; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Stores the plugin part of links |
|
55
|
|
|
**/ |
|
56
|
|
|
private static $_plugin = ''; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Stores the action part of links |
|
60
|
|
|
**/ |
|
61
|
|
|
private static $_action = ''; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Stores the key part of links |
|
65
|
|
|
**/ |
|
66
|
|
|
private static $_key = ''; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Stores the value part of links |
|
70
|
|
|
**/ |
|
71
|
|
|
private static $_value = ''; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Fetch options and set flags |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
**/ |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
private static function _init() |
|
80
|
|
|
{ |
|
81
|
|
|
self::$_request = \csphere\core\http\Request::get(); |
|
82
|
|
|
|
|
83
|
|
|
// Fetch view options |
|
84
|
|
|
$loader = \csphere\core\service\Locator::get(); |
|
85
|
|
|
$view = $loader->load('view'); |
|
86
|
|
|
|
|
87
|
|
|
self::$_ajax = $view->getOption('links_ajax'); |
|
88
|
|
|
self::$_pretty = $view->getOption('links_pretty'); |
|
89
|
|
|
|
|
90
|
|
|
// Set flags |
|
91
|
|
|
if (empty(self::$_pretty) && empty(self::$_ajax)) { |
|
92
|
|
|
|
|
93
|
|
|
self::$_plugin = '?plugin='; |
|
94
|
|
|
self::$_action = '&action='; |
|
95
|
|
|
self::$_key = '&'; |
|
96
|
|
|
self::$_value = '='; |
|
97
|
|
|
|
|
98
|
|
|
} else { |
|
99
|
|
|
|
|
100
|
|
|
self::$_plugin = empty(self::$_ajax) ? '' : '#'; |
|
101
|
|
|
self::$_action = '/'; |
|
102
|
|
|
self::$_key = '/'; |
|
103
|
|
|
self::$_value = '/'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
self::$_init = true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Creates a link for the specified parameters |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $plugin Plugin |
|
113
|
|
|
* @param string $action Action |
|
114
|
|
|
* @param array $params Array of key value pairs where value can be empty |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
**/ |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
public static function href($plugin, $action, array $params = []) |
|
120
|
|
|
{ |
|
121
|
|
|
// Check and set options if init is not done yet |
|
122
|
|
|
if (self::$_init == false) { |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
self::_init(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// @TODO: Use urlencode on dynamic parts |
|
128
|
|
|
|
|
129
|
|
|
// Add plugin and action |
|
130
|
|
|
$link = self::$_request['dirname'] . self::$_plugin . $plugin; |
|
131
|
|
|
|
|
132
|
|
|
if ($action != '') { |
|
133
|
|
|
|
|
134
|
|
|
$link .= self::$_action . $action; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Add params |
|
138
|
|
|
unset($params['plugin'], $params['action'], $params['']); |
|
139
|
|
|
|
|
140
|
|
|
foreach ($params AS $key => $value) { |
|
141
|
|
|
|
|
142
|
|
|
// Only add params that are not empty |
|
143
|
|
|
if ($value != '') { |
|
144
|
|
|
|
|
145
|
|
|
$link .= self::$_key . $key . self::$_value . $value; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $link; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Creates a link for the current parameters |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
**/ |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
public static function current() |
|
159
|
|
|
{ |
|
160
|
|
|
$plugin = self::$_request['data']['plugin']; |
|
161
|
|
|
$action = self::$_request['data']['action']; |
|
162
|
|
|
$params = self::$_request['data']; |
|
163
|
|
|
|
|
164
|
|
|
// Unset plugin and action from params |
|
165
|
|
|
unset($params['plugin'], $params['action']); |
|
166
|
|
|
|
|
167
|
|
|
// Proceed with usual link creation now |
|
168
|
|
|
$result = self::href($plugin, $action, $params); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
return $result; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Combines the request data for sharing the link |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $link Unencoded local part e.g. generated by href method |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
**/ |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
public static function share($link) |
|
182
|
|
|
{ |
|
183
|
|
|
// Next to the request dns should be a slash provided by the link variable |
|
184
|
|
|
$result = self::$_request['protocol'] |
|
185
|
|
|
. '://' |
|
186
|
|
|
. self::$_request['dns'] |
|
187
|
|
|
. $link; |
|
188
|
|
|
|
|
189
|
|
|
return $result; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Prepares link urls |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $url The internal url that is used in slash syntax |
|
196
|
|
|
* |
|
197
|
|
|
* @throws \Exception |
|
198
|
|
|
* |
|
199
|
|
|
* @return string |
|
200
|
|
|
**/ |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
public static function params($url) |
|
203
|
|
|
{ |
|
204
|
|
|
$link = ''; |
|
205
|
|
|
|
|
206
|
|
|
// Determine plugin and action |
|
207
|
|
|
$target = explode('/', $url, 2); |
|
208
|
|
|
$plugin = $target[0]; |
|
209
|
|
|
|
|
210
|
|
|
if (empty($plugin)) { |
|
211
|
|
|
|
|
212
|
|
|
throw new \Exception('URL without plugin: ' . $url); |
|
213
|
|
|
|
|
214
|
|
|
} else { |
|
215
|
|
|
|
|
216
|
|
|
// Sort link parameters |
|
217
|
|
|
$params = []; |
|
218
|
|
|
|
|
219
|
|
|
if (empty($target[1])) { |
|
220
|
|
|
|
|
221
|
|
|
$action = ''; |
|
222
|
|
|
} else { |
|
223
|
|
|
|
|
224
|
|
|
// Remove action from parameters |
|
225
|
|
|
$split = explode('/', $target[1]); |
|
226
|
|
|
$action = $split[0]; |
|
227
|
|
|
|
|
228
|
|
|
unset($split[0]); |
|
229
|
|
|
|
|
230
|
|
|
// Create valid key value pairs |
|
231
|
|
|
$split_key = ''; |
|
232
|
|
|
|
|
233
|
|
|
foreach ($split AS $splitted) { |
|
234
|
|
|
|
|
235
|
|
|
if ($split_key == '') { |
|
236
|
|
|
|
|
237
|
|
|
$split_key = $splitted; |
|
238
|
|
|
$params[$split_key] = ''; |
|
239
|
|
|
|
|
240
|
|
|
} else { |
|
241
|
|
|
|
|
242
|
|
|
$params[$split_key] = $splitted; |
|
243
|
|
|
$split_key = ''; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
$link .= self::href($plugin, $action, $params); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return $link; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Transforms a slash-separated string to an key value array |
|
256
|
|
|
* |
|
257
|
|
|
* @param string $url The internal url that is used in slash syntax |
|
258
|
|
|
* |
|
259
|
|
|
* @return array |
|
260
|
|
|
**/ |
|
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
public static function transform($url) |
|
263
|
|
|
{ |
|
264
|
|
|
$params = []; |
|
265
|
|
|
$split = explode('/', $url); |
|
266
|
|
|
$splits = count($split); |
|
267
|
|
|
|
|
268
|
|
|
// Just use every second entry due to key value structure |
|
269
|
|
|
for ($i = 0; $i < $splits; $i+=2) { |
|
270
|
|
|
|
|
271
|
|
|
// Not every value might contain data |
|
272
|
|
|
$params[$split[$i]] = isset($split[($i+1)]) ? $split[($i+1)] : ''; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
// Clear empty keys |
|
276
|
|
|
unset($params['']); |
|
277
|
|
|
|
|
278
|
|
|
return $params; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|