-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.php
291 lines (251 loc) · 10.4 KB
/
router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
defined('_JEXEC') or die;
/**
* Routing class from com_one
*/
class OneRouter extends JComponentRouterBase
{
/**
* Build the route for the com_one component
*
* Every one route needs a scheme and a task. Depending on the task, there may be a need for an additional piece
* of information. For instance, task=list needs nothing else, but task=detail and taks=edit require an identifier.
*
* @param array &$query An array of URL arguments
* @return array The URL arguments to use to assemble the subsequent URL.
* @since 3.3
*/
public function build(&$query)
{
// echo '<div class="well">Building route for com_one query<br/><pre>';
// print_r($query);
// echo '</pre>';
$segments = array();
/**
* First, check whether we have an Itemid, and see if we can reuse it for this route? This would require that
* multiple pieces of information are equal.
*/
// Get a menu item based on Itemid or currently active
$app = JFactory::getApplication();
$menu = $app->getMenu();
$params = JComponentHelper::getParams('com_one');
// We need a menu item. Either the one specified in the query, or the current active one if none specified
if (empty($query['Itemid'])) {
$menuItem = $menu->getActive();
$menuItemGiven = false;
} else {
$menuItem = $menu->getItem($query['Itemid']);
$menuItemGiven = true;
}
// Check again, if this Itemid is not a com_one item, do not reuse the Itemid
if ($menuItemGiven && isset($menuItem) && $menuItem->component != 'com_one') {
$menuItemGiven = false;
unset($query['Itemid']);
}
/**
* If there is an Itemid, we can retrieve its information to check upon. Else, we assume that info is an
* empty array.
*/
$menudata = array();
if ($menuItem) {
$menudata = $menuItem->query;
}
// We need to have a scheme in the query or it is an invalid URL
if (isset($query['scheme'])) {
$schemeName = $query['scheme'];
} else {
// no scheme, fallback to default
return $segments;
}
// We need to have a task in the query or it is an invalid URL
if (isset($query['task'])) {
$task = $query['task'];
} else {
// no task
return $segments;
}
// We MAY have a view in the query
if (isset($query['view'])) {
$view = $query['view'];
} else {
$view = '';
}
/**
* We know what the scheme and task are. The question is how we construct the route, so we need to know whether
* this is going to be a special routing defined for this scheme, or the standard routing.
*
* If the routing is special, we can find the routing by the One_Routing::getAliasForOptions($scheme,$options)
* call which needs task+view as parameters.
*
* If this fails, we need to go to the standard actions. If the scheme's controller implements the action, we
* currently treat this as just any task, meaning we have no special route creation info.
*
* One_Routing::getAliasForOptions has been changed to look through the standard actions if no scheme-specific
* route has been located.
*
* @TODO add a One_Controller::buildRoute/parseRoute pair to handle these special routes
*
* Else, we ask the standard One_Action class to tell us what the route should be. In that case, it needs to
* answer in the same way as for a special routing.
*/
$data = array('task' => $task, 'view' => $view);
$scheme = One_Repository::getScheme($schemeName);
$aliasData = One_Routing::getAliasForOptions($scheme, $data);
if ($aliasData !== null) {
unset($query['scheme']);
unset($query['task']);
unset($query['view']);
$segments = explode('/', $aliasData['alias']);
if ($aliasData['useId'] == 'true') {
if (isset($aliasData['aliasField'])) {
$aliasField = $aliasData['aliasField'];
$schemeQuery = One_Repository::selectQuery($aliasData['schemeName']);
$schemeQuery->where($schemeQuery->getScheme()->getIdentityAttribute()->getName(), 'eq', $query['id']);
$schemeQuery->setSelect(array($schemeQuery->getScheme()->getIdentityAttribute()->getName(), $aliasField));
$results = $schemeQuery->execute(false);
if (count($results)) {
$segments[] = $results[0]->$aliasField;
}
unset($query['id']);
} else {
if (isset($query['id'])) $segments[] = $query['id'];
}
}
} else {
// nothing
}
$total = count($segments);
for ($i = 0; $i < $total; $i++) {
$segments[$i] = str_replace(':', '-', $segments[$i]);
}
// echo '<pre>';
// print_r($segments);
// echo '</pre></div>';
return $segments;
}
/**
* Parse the segments of a URL.
*
* @param array &$segments The segments of the URL to parse.
*
* @return array The URL attributes to be used by the application.
*
* @since 3.3
*/
public function parse(&$segments)
{
// Get the active menu item.
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item = $menu->getActive();
if (isset($item->query['controller']) && $item->query['controller'] == 'rest') return array();
$options = One_Routing::getOptionsForAlias(implode('/', $segments));
// echo '<pre>';
// print_r($options);
// echo '</pre>';
if ($options !== null) {
$vars = array();
$vars['scheme'] = $options['schemeName'];
$vars['task'] = $options['options']['task'];
$vars['view'] = $options['options']['view'];
// check and find the alias or id field
if ($options['useId'] && null !== $options['aliasField']) {
$regularAlias = $segments[(count($segments) - 1)];
$altAlias = preg_replace('/:/', '-', $regularAlias, 1);
$schemeQuery = One_Repository::selectQuery($options['schemeName']);
if ($regularAlias == $altAlias) {
$schemeQuery->where($options['aliasField'], 'eq', $regularAlias);
} else {
$queryOr = $schemeQuery->addOr();
$queryOr->where($options['aliasField'], 'eq', $regularAlias);
$queryOr->where($options['aliasField'], 'eq', $altAlias);
}
$results = $schemeQuery->execute();
if (0 < count($results)) {
$idAttr = $schemeQuery->getScheme()->getIdentityAttribute()->getName();
$vars['id'] = $results[0]->$idAttr;
}
unset($vars['aliasfield']);
} else if ($options['useId']) {
$vars['id'] = $segments[(count($segments) - 1)];
}
// echo '<pre>';
// print_r($vars);
// echo '</pre>';
return $vars;
}
/**
* Since we have no clear routing, we can only work on the standard interpretation. Only change is that we
* can use the current menu's scheme if that is a com_one menu item.
*/
$menu_options = array();
if (($menu = JSite::getMenu()->getActive()) && $menu->component == 'com_one') {
$menuscheme = $menu->query['scheme'];
array_unshift($segments, $menuscheme);
}
$vars = array();
$count = count($segments);
switch ($count) {
case 4 :
// format : SCHEME / TASK / VIEW / ID
$vars['scheme'] = str_replace('-', ':', $segments[$count - 4]);
$vars['task'] = $segments[$count - 3];
$vars['view'] = $segments[$count - 2];
$parts = explode(',', $segments[$count - 1]);
$vars['id'] = $parts[(count($parts) - 1)];
break;
case 3 :
// format : SCHEME / detail / ID
// format : SCHEME / list / VIEW
// format : SCHEME / calendar* / VIEW
// format : SCHEME / view / VIEW
$vars['scheme'] = str_replace('-', ':', $segments[$count - 3]);
$vars['task'] = $segments[$count - 2];
$parts = explode(',', $segments[$count - 1]);
if (preg_match('/calendar(Day|Month|Week)?/', $vars['task']) > 0) {
$vars['view'] = $segments[$count - 1];
} else if (count($parts) > 1) {
if ($vars['task'] == 'detail' && preg_match('/^(month|week|day)$/', $params->get('view')) > 0)
$vars['view'] = 'detail';
$parts = explode(',', $segments[$count - 1]);
$vars['id'] = $parts[(count($parts) - 1)];
} else if ($vars['task'] == 'detail') {
$vars['view'] = 'detail';
$vars['id'] = $parts[(count($parts) - 1)];
} else if ($vars['task'] == 'edit') {
$vars['view'] = $parts[(count($parts) - 1)];
$vars['id'] = 0;
} else
$vars['view'] = $segments[$count - 1];
break;
case 2 :
$vars['scheme'] = str_replace('-', ':', $segments[$count - 2]);
$vars['task'] = $segments[$count - 1];
break;
case 1 :
$vars['scheme'] = str_replace('-', ':', $segments[$count - 1]);
$vars['task'] = 'list';
break;
default :
}
return $vars;
}
}
/**
* Content router functions
*
* These functions are proxys for the new router interface
* for old SEF extensions.
*
* @deprecated 4.0 Use Class based routers instead
*/
function OneBuildRoute(&$query)
{
$router = new OneRouter;
return $router->build($query);
}
function OneParseRoute($segments)
{
$router = new OneRouter;
return $router->parse($segments);
}