Newer
Older
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
// $Id$
/**
* hook_menu()
*/
function context_prefix_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'type' => module_exists('context_ui') ? MENU_LOCAL_TASK : MENU_NORMAL_ITEM,
'title' => t('Context Prefixes'),
'description' => t('Displays a list of context definitions.'),
'path' => 'admin/build/context/prefix',
'callback' => 'context_prefix_admin',
'access' => user_access('administer site configuration'),
'weight' => 10,
);
}
return $items;
}
/**
* hook_init()
* Checks for any valid context prefixes in request string and sets the context appropriately
*/
function context_prefix_init() {
$q = isset($_REQUEST["q"]) ? trim($_REQUEST["q"], "/") : '';
$prefix = _context_prefix_get_prefix($q);
$items = context_prefix_items();
if (isset($items[$prefix]) && $active = $items[$prefix] ) {
context_set('context_prefix', $active->module, $active->id);
context_set('context_prefix', 'prefix', $prefix);
// if $_GET and $_REQUEST are different, the path has been aliased
// we will continue to the aliased destination
if ($_GET['q'] != $_REQUEST['q']) {
return;
}
// there is nothing beyond the path prefix -- treat as frontpage
else if ($q == $prefix) {
$_GET['q'] = variable_get('site_frontpage', 'node');
}
// pass the rest of the path onto Drupal cleanly
else {
// for now, we check arg(0) + arg(1) for the prefix
if (strpos($q, $prefix) !== 0) {
$q = explode('/', $q);
if ($q[1] == $prefix) {
unset($q[1]);
}
$q = implode('/', $q);
// reset _REQUEST as other prefixing modules (i18n) will use it and not $_GET
$_REQUEST['q'] = $_GET['q'] = $q;
}
// trim off context path and reset q
else {
$q = trim(substr($q, strlen($prefix)), '/');
$_REQUEST['q'] = $_GET['q'] = _context_prefix_get_normal_path($q, $prefix);
}
}
}
}
/**
* Page callback for the context_prefix administration page.
*/
function context_prefix_admin() {
$items = context_prefix_items();
if ($items) {
$rows = array();
foreach ($items as $item) {
$rows[] = array($item->module, $item->prefix, $item->id);
}
return theme('table', array(t('Module'), t('Prefix'), t('ID')), $rows);
}
return "<p>". t('No context prefix definitions found.') ."</p>";
}
/**
* Provides a simple API for validating, adding, and deleting context defintions.
*/
function context_prefix_api($op = 'insert', $context) {
switch ($op) {
case 'validate':
if (check_plain($context['module']) && preg_match('!^[a-z0-9_-]+$!', $context['prefix'])) {
$id = db_result(db_query("SELECT id FROM {context_prefix} WHERE prefix = '%s'", $context['prefix']));
return $id ? false : true;
}
else {
return false;
}
case 'insert':
if (context_prefix_api('validate', $context)) {
$status = db_query("INSERT INTO {context_prefix} (module, prefix, id) VALUES ('%s', '%s', %d)", $context['module'], $context['prefix'], $context['id']);
return $status;
}
return false;
case 'delete':
if ($context['prefix']) {
$param = 'prefix';
$where = $context['prefix'];
}
else if ($context['id']) {
$param = 'id';
$where = $context['id'];
}
$check = db_result(db_query("SELECT id FROM {context_prefix} WHERE module = '%s' AND $param = '%s'", $context['module'], $where));
if ($check) {
$status = db_query("DELETE FROM {context_prefix} WHERE module = '%s' AND $param = '%s'", $context['module'], $where);
return $status;
}
return false;
}
return false;
}
/**
* Returns an array of available context definitions. If provided an
* optional module argument, will only provide definitions for the
* specified module.
*/
function context_prefix_items($module = NULL) {
static $items;
static $by_module;
if (!$items) {
$items = $by_module = array();
$result = db_query("SELECT * FROM {context_prefix} ORDER BY module ASC");
while ($item = db_fetch_object($result)) {
$items[$item->prefix] = $item;
$by_module[$item->module][$item->prefix] = $item;
}
}
if ($module) {
return isset($by_module[$module]) ? $by_module[$module] : array();
}
else {
return $items;
}
}
/**
* Returns a prefix string from a url if a valid one is found
*/
function _context_prefix_get_prefix($q) {
$exploded_q = explode('/', $q);
$prefix = array_shift($exploded_q);
// skip over i18n prefix if found
if (module_exists('i18n') && function_exists('locale_supported_languages')) {
$languages = locale_supported_languages();
$languages = array_keys($languages['name']); // grab only language prefixes
// if first prefix is in languages array, throw it out and use 2nd prefix
if (array_search($path, $languages) !== false) {
$prefix = array_shift($exploded_q);
}
}
// check that this prefix is valid
$valid = context_prefix_items();
if (isset($valid[$prefix])) {
return $prefix;
}
else {
return false;
}
}
/**
* Taken from i18n
*/
function _context_prefix_get_normal_path($path, $prefix) {
// If bootstrap, drupal_lookup_path is not defined
if (!function_exists('drupal_get_headers')) {
return $path;
}
// Check alias without lang
elseif ($alias = drupal_lookup_path('source', $path)) {
return $alias;
}
else {
return $path;
}
}
/**
* Jose's very smart collision avoidance
*/
if (!function_exists('custom_url_rewrite')) {
function custom_url_rewrite($type, $path, $original) {
return context_prefix_url_rewrite($type, $path, $original);
}
}
/**
* Rewrites path with current context and removes context if searching for source path
*/
function context_prefix_url_rewrite($type, $path, $original) {
$working_path = $path; // preserve original path
if (module_exists('i18n')) {
if ($type == 'alias' && !i18n_get_lang_prefix($path)) {
$prefix[] = i18n_get_lang();
}
elseif ($type == 'source') {
if ($path == $original) {
$working_path = i18n_get_normal_path($working_path);
}
else { // Path may have been dealiased but still have language prefix
$working_path = i18n_get_lang_prefix($working_path, TRUE);
}
}
}
// by now i18n has added/removed language prefix as needed
if (!clswitch('get')) {
$context = context_get('context_prefix', 'prefix');
if ($type == 'alias' && !_context_prefix_get_prefix($working_path) && $context) {
$prefix[] = $context;
}
else if ($type == 'source') {
if (_context_prefix_get_prefix($working_path)) {
$working_path = trim(substr($working_path, strlen($context)), '/');
}
}
}
if ($working_path) {
$prefix[] = $working_path;
}
return $prefix ? implode('/', $prefix) : '';
}
/**
* Custom l wrapper for links that need to leave all group contexts
*/
function cl($text, $path, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = FALSE, $dropcontext = FALSE) {
clswitch('set', $dropcontext);
if (!$dropcontext && $path == '<front>') {
$path = context_prefix_url_rewrite('alias', '', '');
}
$l = l($text, $path, $attributes, $query, $fragment, $absolute, $html);
clswitch('reset');
return $l;
}
/**
* Returns whether the current l/url call should use context rewriting or not
*/
function clswitch($op, $absolute = null) {
static $drop;
switch ($op) {
case 'set';
$drop = $absolute;
break;
case 'get':
return $drop;
break;
case 'reset':
$drop = null;
break;
}
}
/**
* Like theme_links, but handles context warping.
* theme_links couldn't believe it.
*/
function theme_context_links($links, $attributes = array('class' => 'links')) {
$output = '';
if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = '';
// Automatically add a class to each link and also to each LI
if (isset($link['attributes']) && isset($link['attributes']['class'])) {
$link['attributes']['class'] .= ' '. $key;
$class = $key;
}
else {
$link['attributes']['class'] = $key;
$class = $key;
}
// Add first and last classes to the list of links to help out themers.
$extra_class = '';
if ($i == 1) {
$extra_class .= 'first ';
}
if ($i == $num_links) {
$extra_class .= 'last ';
}
$output .= '<li class="'. $extra_class . $class .'">';
// Is the title HTML?
$html = isset($link['html']) && $link['html'];
// Initialize fragment and query variables.
$link['query'] = isset($link['query']) ? $link['query'] : NULL;
$link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
if (isset($link['href'])) {
if ($link['warp']) {
$output .= cl($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html, TRUE);
}
else {
$output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
}
}
else if ($link['title']) {
//Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (!$html) {
$link['title'] = check_plain($link['title']);
}
$output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</li>\n";
}
$output .= '</ul>';
}
return $output;
}