00001 <?php
00002
00003
00004
00005
00006 class context_reaction_menu extends context_reaction {
00007
00008
00009
00010 function options_form($context) {
00011 if (module_exists('menu')) {
00012 $menus = menu_parent_options(menu_get_menus(), array('mlid' => 0));
00013 $root_menus = array();
00014 foreach ($menus as $key => $name) {
00015 $id = explode(':', $key);
00016 if ($id[1] == '0') {
00017 $root_menus[$id[0]] = check_plain($name);
00018 }
00019 else {
00020 $link = menu_link_load($id[1]);
00021 $identifier = $link['link_path'];
00022 $root_menu = $root_menus[$id[0]];
00023 while (isset($menus[$root_menu][$identifier])) {
00024 $identifier .= "'";
00025 }
00026 $menus[$root_menu][$identifier] = $name;
00027 }
00028 unset($menus[$key]);
00029 }
00030 array_unshift($menus, "-- " . t('None') . " --");
00031 }
00032 else {
00033 $menus = array();
00034 }
00035 return array(
00036 '#title' => $this->title,
00037 '#description' => $this->description,
00038 '#options' => $menus,
00039 '#type' => 'select',
00040 '#default_value' => $this->fetch_from_context($context),
00041 );
00042 }
00043
00044
00045
00046
00047
00048 function options_form_submit($values) {
00049 return trim($values, "'");
00050 }
00051
00052
00053
00054
00055
00056 function execute(&$vars = NULL) {
00057 if (variable_get('menu_main_links_source', 'main-menu') == variable_get('menu_secondary_links_source', 'user-menu')) {
00058 $vars['main_menu'] = theme_get_setting('toggle_main_menu') ? $this->menu_navigation_links(variable_get('menu_main_links_source', 'main-menu')) : $vars['main_menu'];
00059 $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'];
00060 }
00061
00062 $vars['main_menu'] = $this->menu_set_active($vars['main_menu']);
00063 $vars['secondary_menu'] = $this->menu_set_active($vars['secondary_menu']);
00064 }
00065
00066 function get_active_paths() {
00067 $active_paths = array();
00068 foreach ($this->get_contexts() as $context) {
00069 if (isset($context->reactions[$this->plugin])) {
00070 $active_paths[] = $context->reactions[$this->plugin];
00071 }
00072 }
00073 return $active_paths;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 function menu_set_active($links = array(), $reset = FALSE) {
00090 $new_links = array();
00091 if (!empty($links)) {
00092 $active_paths = $this->get_active_paths();
00093
00094
00095
00096 foreach ($links as $key => $link) {
00097 if (!empty($link['href']) && in_array($link['href'], $active_paths)) {
00098 $link['attributes']['class'][] = 'active';
00099
00100 if (strpos(' active', $key) === FALSE) {
00101 $new_links[$key . ' active'] = $link;
00102 }
00103 }
00104 else {
00105 $new_links[$key] = $link;
00106 }
00107 }
00108 }
00109 return $new_links;
00110 }
00111
00112
00113
00114
00115
00116 function menu_navigation_links($menu_name, $level = 0) {
00117
00118 $original_path = $_GET['q'];
00119 $original_menu_trail = drupal_static('menu_set_active_trail');
00120
00121
00122 if ($active_paths = $this->get_active_paths()) {
00123 $path = current($active_paths);
00124 if (menu_get_item($path)) {
00125 menu_set_active_item($path);
00126 }
00127 }
00128
00129
00130 if (module_exists('i18n_menu')) {
00131 $links = i18n_menu_navigation_links($menu_name, $level);
00132 } else {
00133 $links = menu_navigation_links($menu_name, $level);
00134 }
00135
00136
00137 menu_set_active_item($original_path);
00138 $repair_menu_trail = &drupal_static('menu_set_active_trail');
00139 $repair_menu_trail = $original_menu_trail;
00140 return $links;
00141 }
00142 }