context_reaction_menu.inc in Context 6
File
plugins/context_reaction_menu.inc
View source
<?php
class context_reaction_menu extends context_reaction {
function options_form($context) {
if (module_exists('menu')) {
$menus = menu_parent_options(menu_get_menus(), array(
'mlid' => 0,
));
$root_menus = array();
foreach ($menus as $key => $name) {
$id = explode(':', $key);
if ($id[1] == '0') {
$root_menus[$id[0]] = check_plain($name);
}
else {
$link = menu_link_load($id[1]);
$identifier = $link['link_path'];
$root_menu = $root_menus[$id[0]];
while (isset($menus[$root_menu][$identifier])) {
$identifier .= "'";
}
$menus[$root_menu][$identifier] = $name;
}
unset($menus[$key]);
}
array_unshift($menus, "-- " . t('None') . " --");
}
else {
$menus = array();
}
return array(
'#title' => $this->title,
'#description' => $this->description,
'#options' => $menus,
'#type' => 'select',
'#default_value' => $this
->fetch_from_context($context),
);
}
function options_form_submit($values) {
return trim($values, "'");
}
function execute(&$vars) {
if (variable_get('menu_main_links_source', 'main-menu') == variable_get('menu_secondary_links_source', 'user-menu')) {
$vars['main_menu'] = theme_get_setting('toggle_main_menu') ? $this
->menu_navigation_links(variable_get('menu_main_links_source', 'main-menu')) : $vars['main_menu'];
$vars['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? $this
->menu_navigation_links(variable_get('menu_secondary_links_source', 'secondary-links'), 1) : $vars['secondary_menu'];
}
$vars['main_menu'] = $this
->menu_set_active($vars['main_menu']);
$vars['secondary_menu'] = $this
->menu_set_active($vars['secondary_menu']);
}
function get_active_paths() {
$active_paths = array();
foreach ($this
->get_contexts() as $context) {
if (isset($context->reactions[$this->plugin])) {
$active_paths[] = $context->reactions[$this->plugin];
}
}
return $active_paths;
}
function menu_set_active($links = array(), $reset = FALSE) {
$new_links = array();
if (!empty($links)) {
$active_paths = $this
->get_active_paths();
foreach ($links as $key => $link) {
if (!empty($link['href']) && in_array($link['href'], $active_paths)) {
if (isset($link['attributes']['class'])) {
$link['attributes']['class'] .= ' active';
}
else {
$link['attributes']['class'] = 'active';
}
if (strpos(' active', $key) === FALSE) {
$new_links[$key . ' active'] = $link;
}
}
else {
$new_links[$key] = $link;
}
}
}
return $new_links;
}
function menu_navigation_links($menu_name, $level = 0) {
$original_path = $_GET['q'];
if ($active_paths = $this
->get_active_paths()) {
$path = current($active_paths);
if (menu_get_item($path)) {
menu_set_active_item($path);
}
}
$links = menu_navigation_links($menu_name, $level);
menu_set_active_item($original_path);
return $links;
}
}