| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id: bandwidth.module,v 1.2 2008/10/30 17:55:27 yrocq Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Bandwidth Module |
* Bandwidth Module |
| 10 |
/** |
/** |
| 11 |
* Bandwidth management |
* Bandwidth management |
| 12 |
*/ |
*/ |
|
|
|
|
/** |
|
|
* Get or set current bandwidth |
|
|
* @param bandwidth to set |
|
|
* @return current bandwidth |
|
|
**/ |
|
|
|
|
|
function bandwidth_current_bandwidth($bandwidth='') |
|
|
{ |
|
|
static $current_bandwidth; |
|
|
|
|
|
if(bandwidth_is_valid_bandwidth($bandwidth)) { |
|
|
$current_bandwidth = $bandwidth; |
|
|
} |
|
|
|
|
|
return $current_bandwidth; |
|
|
} |
|
| 13 |
|
|
| 14 |
/** |
/** |
| 15 |
* Get prefered bandwidth for a user |
* Get prefered bandwidth for a user |
| 88 |
return in_array($bandwidth, array_keys(bandwidth_get_bandwidthes())); |
return in_array($bandwidth, array_keys(bandwidth_get_bandwidthes())); |
| 89 |
} |
} |
| 90 |
|
|
| 91 |
|
function bandwidth_path_contains_bandwidth($path) |
| 92 |
|
{ |
| 93 |
|
$path_array = explode('/',$path); |
| 94 |
|
return bandwidth_is_valid_bandwidth($path_array[0]); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
/** |
/** |
| 98 |
* Pages |
* Pages |
| 99 |
*/ |
*/ |
| 137 |
if (user_access('choose bandwidth')) { |
if (user_access('choose bandwidth')) { |
| 138 |
return array( |
return array( |
| 139 |
'subject' => t('Choose bandwidth'), |
'subject' => t('Choose bandwidth'), |
| 140 |
'content' => theme('bandwidth_choice') |
'content' => theme('bandwidth_choice', $_GET['q']) |
| 141 |
); |
); |
| 142 |
} |
} |
| 143 |
break; |
break; |
| 152 |
{ |
{ |
| 153 |
$path_array = explode('/', $_GET['q']); |
$path_array = explode('/', $_GET['q']); |
| 154 |
|
|
| 155 |
if (in_array($path_array[0], array_keys(bandwidth_get_bandwidthes()))) { |
if (bandwidth_is_valid_bandwidth($path_array[0])) { |
| 156 |
bandwidth_current_bandwidth(array_shift($path_array)); |
bandwidth_set_user_bandwidth(array_shift($path_array)); |
| 157 |
$_GET['q'] = implode('/', $path_array); |
$_GET['q'] = implode('/', $path_array); |
|
|
|
| 158 |
// Parse the path again without the leading bandwidth |
// Parse the path again without the leading bandwidth |
| 159 |
drupal_init_language(); |
drupal_init_language(); |
| 160 |
|
|
| 189 |
{ |
{ |
| 190 |
return array( |
return array( |
| 191 |
'bandwidth_choice' => array( |
'bandwidth_choice' => array( |
| 192 |
'arguments' => array(), |
'arguments' => array('path' => NULL), |
| 193 |
), |
), |
| 194 |
); |
); |
| 195 |
} |
} |
| 227 |
|
|
| 228 |
function custom_url_rewrite_outbound(&$path, &$options, $original_path) |
function custom_url_rewrite_outbound(&$path, &$options, $original_path) |
| 229 |
{ |
{ |
| 230 |
$path_array = explode('/',$path); |
// If it is a link to change bandwidth (changeBandwidth argument in the query) |
| 231 |
// Add bandwidth prefix if not already present |
// We alter the link and clear the query |
| 232 |
if (!bandwidth_is_valid_bandwidth($path_array[0])) { |
if (strstr($options['query'], 'changeBandwidth=')) { |
| 233 |
$options['prefix'] = bandwidth_get_user_bandwidth().'/'.$options['prefix']; |
$bandwidth = str_replace('changeBandwidth=', '', $options['query']); |
| 234 |
|
unset($options['query']); |
| 235 |
} |
} |
| 236 |
|
else |
| 237 |
|
{ |
| 238 |
|
if (!bandwidth_path_contains_bandwidth($path)) { |
| 239 |
|
$bandwidth = bandwidth_get_user_bandwidth(); |
| 240 |
|
} |
| 241 |
|
} |
| 242 |
|
|
| 243 |
|
$options['prefix'] = $bandwidth.'/'.$options['prefix']; |
| 244 |
} |
} |
| 245 |
|
|
| 246 |
/** |
/** |
| 247 |
* Theme functions |
* Theme functions |
| 248 |
*/ |
*/ |
| 249 |
|
|
| 250 |
function theme_bandwidth_choice() { |
function theme_bandwidth_choice($path) { |
| 251 |
|
if (bandwidth_path_contains_bandwidth($path)) { |
| 252 |
|
$path_array = explode('/', $path); |
| 253 |
|
array_shift($path); |
| 254 |
|
$path = implode('/', $path_array); |
| 255 |
|
} |
| 256 |
foreach (bandwidth_get_bandwidthes() as $key => $value) { |
foreach (bandwidth_get_bandwidthes() as $key => $value) { |
| 257 |
$bandwidthes[] = l($value,'bandwidth/'.$key, array('query' => drupal_get_destination())); |
$bandwidthes[] = l($value,$path,array("query" => array('changeBandwidth' => $key))); |
| 258 |
} |
} |
| 259 |
return theme_item_list($bandwidthes); |
return theme_item_list($bandwidthes); |
| 260 |
} |
} |