function spaces_menu_rebuild in Spaces 6
Rebuilds the spaces menu stored in the menu_links tables via the menu API.
3 calls to spaces_menu_rebuild()
- spaces_features_menu in ./
spaces.module - Returns a links array in the theme_links() format of the current space's menu items for features accessible to the current user. Each item has a keyed array of children items if applicable.
- spaces_form_alter in ./
spaces.module - Implementation of hook_form_alter().
- spaces_init in ./
spaces.module - Implementation of hook_init().
File
- ./
spaces.module, line 1424
Code
function spaces_menu_rebuild() {
// @TODO there is probably a better API-based way to achieve this ...
$spaces_menu = array();
$result = db_query("SELECT * FROM {menu_links} WHERE menu_name = 'spaces' AND module = 'spaces'");
while ($item = db_fetch_object($result)) {
$spaces_menu[$item->link_path] = $item;
}
$cleanup = $spaces_menu;
// Loop through features and create or update menu items.
$cache = array();
$features = spaces_features();
foreach ($features as $feature) {
if (isset($feature->spaces['menu']) && count($feature->spaces['menu'])) {
foreach ($feature->spaces['menu'] as $key => $item) {
// Use the array key as the menu path if not set explicitly --
// should allow backwards compatibility with old feature
// defintions.
$path = !empty($item['href']) ? $item['href'] : $key;
// Remove this item from list of menu items to clean up
unset($cleanup[$path]);
// Load an existing menu item if it already exists
$existing_item = isset($spaces_menu[$path]) ? menu_link_load($spaces_menu[$path]->mlid) : array();
$customized = $existing_item['customized'];
$attributes = isset($item['attributes']) ? array(
'attributes' => $item['attributes'],
) : array();
$new_item = array(
'link_title' => $customized && isset($existing_item['title']) ? $existing_item['title'] : $item['title'],
'link_path' => $path,
'options' => $customized && isset($existing_item['options']) ? $existing_item['options'] : $attributes,
'menu_name' => 'spaces',
'module' => 'spaces',
);
$new_item = array_merge($existing_item, $new_item);
$mlid = menu_link_save($new_item);
if ($mlid) {
$cache[$new_item['link_path']] = $feature->value;
}
}
}
}
// Clean up stale items for features that no longer exist
foreach ($cleanup as $item) {
menu_link_delete($item->mlid);
}
cache_set('spaces_menu', $cache, 'cache');
return $cache;
}