forked from yireo-joomla/pkg_simplelists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.php
210 lines (176 loc) · 6.78 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
<?php
/**
* Joomla! component SimpleLists
*
* @author Yireo
* @copyright Copyright 2011
* @license GNU Public License
* @link https://www.yireo.com/
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Require the router-helper
require_once JPATH_SITE.DS.'components'.DS.'com_simplelists'.DS.'helpers'.DS.'router.php';
/*
* Function to convert a system URL to a SEF URL
*/
function SimplelistsBuildRoute(&$query)
{
// Initialize the segments
$segments = array();
// Temporarily extract the Itemid
if(isset($query['Itemid'])) {
$Itemid = $query['Itemid'];
unset($query['Itemid']);
} else {
$Itemid = null;
}
// If this is an item view and we have to hide it
if (!empty($query['view']) && $query['view'] == 'item' && !empty($query['task']) && $query['task'] == 'hidden') {
$segments[] = 'id,'.$query['id'];
unset($query['view']);
unset($query['task']);
unset($query['tmpl']);
unset($query['id']);
return $segments;
}
// Get the menu items for this component
$items = SimplelistsHelperRouter::getMenuItems();
$params = &JComponentHelper::getParams('com_simplelists');
// Break up the slug into numeric and alias values
if (!empty($query['category_id'])) {
$query['slug'] = $query['category_id'];
if( strpos($query['category_id'], ':')) {
list($query['category_id'], $query['alias']) = explode(':', $query['category_id'], 2);
}
}
// If this is an item view
if (!empty($query['view']) && $query['view'] == 'item') {
$segments[] = 'item';
$segments[] = $query['id'];
if(isset($query['category_id'])) {
foreach ($items as $item) {
if(isset($item->query['view']) && isset($item->query['category_id']) && $item->query['view'] == 'items' && $query['category_id'] == $item->query['category_id']) {
$query['Itemid'] = $item->id;
}
}
}
unset($query['view']);
unset($query['task']);
unset($query['tmpl']);
unset($query['id']);
unset($query['slug']);
unset($query['alias']);
unset($query['category_id']);
return $segments;
}
// Search for an appropriate menu item
if (!empty($items) && isset($query['view'])) {
foreach ($items as $item) {
// Matching menu-items only makes sense if there is a "view" and an "id"
if($params->get('use_parent_url', 0) == 1) {
if (isset($item->query['view']) && isset($item->query['category_id']) && isset($query['view']) && isset($query['category_id'])) {
// Whoever knows how to rewrite the following into something readable, wins my respect
if ($query['view'] == $item->query['view'] && $query['category_id'] == $item->query['category_id']) {
if(empty($query['layout'])) {
$query['Itemid'] = $item->id;
break;
} elseif(empty($query['layout']) && empty($item->query['layout'])) {
$query['Itemid'] = $item->id;
break;
} elseif(!empty($query['layout']) && !empty($item->query['layout']) && $query['layout'] == $item->query['layout']) {
$query['Itemid'] = $item->id;
break;
}
}
}
}
}
}
// Set the alias if it is not present
if(!empty($query['category_id']) && empty($query['alias'])) {
require_once JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_simplelists'.DS.'helpers'.DS.'category.php';
$query['alias'] = SimplelistsCategoryHelper::getAlias($query['category_id']);
}
// Check if the router found an appropriate Itemid
if(!isset($query['Itemid']) || !$query['Itemid'] > 0) {
if($params->get('sef_url') == 'slug' && !empty($query['slug'])) {
$segments[] = $query['slug'];
} elseif(!empty($query['alias'])) {
$segments[] = $query['alias'];
}
}
// Re-add the router if not existing yet
if(!isset($query['Itemid']) && !empty($Itemid)) {
$query['Itemid'] = $Itemid;
}
// Set the limitstart if needed
if(isset($query['start'])) {
$segments[] = (int)$query['start'];
unset($query['start']);
}
// Unset all unneeded query-parts because they should be now either segmented or referenced from the Itemid
unset($query['view']);
unset($query['category_id']);
unset($query['alias']);
unset($query['slug']);
unset($query['layout']);
// Return the segments
return $segments;
}
/*
* Function to convert a SEF URL back to a system URL
*/
function SimplelistsParseRoute($segments)
{
$vars = array();
// First do the easiest parsing
if(preg_match('/^id\,([0-9]+)/', $segments[0])) {
$ids = explode(',', $segments[0]);
$vars['view'] = 'item';
$vars['task'] = 'hidden';
$vars['tmpl'] = 'component';
$vars['id'] = $ids[1];
return $vars;
}
// Parse an item
if($segments[0] == 'item') {
$vars['view'] = 'item';
$vars['id'] = $segments[1];
return $vars;
}
// Get the active menu item
$menu =& JSite::getMenu();
$item =& $menu->getActive();
// If the last segment is numeric, assume it's used pagination
$last = count($segments) - 1;
if(isset($segments[$last]) && is_numeric($segments[$last])) {
$vars['limitstart'] = $segments[$last];
unset($segments[$last]);
}
// Parse the segments
if(!empty($segments[0])) {
$vars['alias'] = str_replace( ':', '-', preg_replace('/^([0-9]?):/', '', $segments[0]));
$vars['category_id'] = (int)$segments[0];
$vars['view'] = 'items';
}
// If the layout is specified in the URL (which is unlikely), set it in the query
if(!empty($segments[1])) {
$vars['layout'] = $segments[1];
}
// If there is no menu-item (so no Itemid), there's nothing more to fetch
if(!isset($item)) {
return $vars;
}
// Add the menu-item elements to the query
if(isset( $item->query['layout'] )) {
$vars['layout'] = $item->query['layout'];
}
if(!isset($vars['view']) && isset( $item->query['view'] )) {
$vars['view'] = $item->query['view'];
}
if(!isset($vars['category_id']) && isset( $item->query['category_id'] )) {
$vars['category_id'] = $item->query['category_id'];
}
return $vars;
}