View source
<?php
function quickbar_init() {
if (quickbar_is_enabled()) {
global $user;
$roles = user_roles();
if (is_array($roles)) {
asort($roles);
$use_machine_names = variable_get('quickbar_use_machine_names', 0);
if ($use_machine_names) {
$machine_roles = _quickbar_role_machine_names($roles);
}
$role_weights = variable_get('quickbar_role_weights', array());
$menus = variable_get('quickbar_role_menus', array());
foreach ($role_weights as $rid => $weight) {
$user_role_index = $use_machine_names ? array_search($machine_roles[$rid], $roles) : $rid;
if (!empty($user->roles[$user_role_index]) && isset($menus[$rid])) {
$settings = variable_get('quickbar_settings_' . $rid, _quickbar_default_settings());
$path = drupal_get_path('module', 'quickbar');
drupal_add_js($path . '/js/quickbar.js');
drupal_add_js(array(
'quickbar' => array(
'secondary_menu_visibility' => $settings['secondary_menu_visibility'],
),
), 'setting');
drupal_add_css($path . '/theme/quickbar.css');
if ($settings['iconset'] != '_none') {
$iconsets = module_invoke_all('quickbar_iconset_info');
drupal_add_css($iconsets[$settings['iconset']]['css']);
}
break;
}
}
}
}
}
function _quickbar_default_settings() {
return array(
'iconset' => '_none',
'sticky' => 0,
'float' => 0,
'secondary_menu_visibility' => 0,
'nofollow' => 0,
);
}
function quickbar_landing_page_access($path) {
static $paths;
if (!isset($paths)) {
$cache = cache_get('quickbar_paths');
$paths = $cache && !empty($cache->data) ? $cache->data : array();
}
if (!isset($paths[$path])) {
$item = db_fetch_array(db_query("SELECT mlid, menu_name FROM {menu_links} ml WHERE ml.link_path = '%s' AND module = 'system'", $path));
$result = db_query("\n SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*\n FROM {menu_links} ml\n LEFT JOIN {menu_router} m ON ml.router_path = m.path\n WHERE ml.plid = %d AND ml.menu_name = '%s' AND hidden = 0", $item['mlid'], $item['menu_name']);
$paths[$path] = FALSE;
while ($item = db_fetch_array($result)) {
$item['options'] = unserialize($item['options']);
if ($item['external']) {
if (!empty($item['options']['alter'])) {
drupal_alter('translated_menu_link', $item, $map);
}
if ($item['access']) {
$paths[$path] = TRUE;
break;
}
}
else {
$map = explode('/', $item['link_path']);
_menu_link_map_translate($map, $item['to_arg_functions']);
$item['href'] = implode('/', $map);
if (strpos($item['href'], '%') !== FALSE) {
continue;
}
if (!isset($item['access'])) {
if (!_menu_load_objects($item, $map)) {
continue;
}
_menu_check_access($item, $map);
}
if (!$item['access']) {
continue;
}
$paths[$path] = TRUE;
break;
}
}
cache_set('quickbar_paths', $paths);
}
return $paths[$path];
}
function quickbar_permission() {
return array(
'administer quickbar' => array(
'title' => t('Administer Quickbar'),
'description' => t('Administer Quickbar settings and configurations'),
),
);
}
function quickbar_theme($existing, $type, $theme, $path) {
$path = drupal_get_path('module', 'quickbar');
$items['quickbar'] = array(
'variables' => array(
'tree' => array(),
'rid' => NULL,
),
'template' => 'quickbar',
'path' => $path . '/theme',
'file' => 'theme.inc',
);
$items['quickbar_form'] = array(
'render element' => 'form',
'file' => 'quickbar.admin.inc',
);
return $items;
}
function quickbar_menu() {
return array(
'admin/config/user-interface/quickbar' => array(
'title' => t('Quickbar Configuration'),
'description' => t('Configure toolbars'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'quickbar_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer quickbar',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'quickbar.admin.inc',
),
'admin/config/user-interface/quickbar/%/edit' => array(
'title' => t('Configure Toolbar'),
'description' => t('Configure the toolbar for a certain role.'),
'page callback' => '_quickbar_configure_page',
'page arguments' => array(
4,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer quickbar',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'quickbar.admin.inc',
),
);
}
function quickbar_is_enabled() {
global $user;
if (module_exists('admin_select')) {
$data = unserialize($user->data);
if (isset($data['admin_select'])) {
if ($data['admin_select'] !== 'quickbar') {
return FALSE;
}
}
}
global $theme_info;
if (!isset($theme_info->info['quickbar']) || $theme_info->info['quickbar'] !== 0) {
return TRUE;
}
return FALSE;
}
function quickbar_page_build(&$page) {
$links = '';
if ($return = quickbar_available()) {
$links = quickbar_menu_tree($return['menu']);
$page['page_top']['quickbar'] = array(
'#type' => 'markup',
'#markup' => theme('quickbar', array(
'tree' => $links,
'rid' => $return['rid'],
)),
);
}
}
function quickbar_preprocess_html(&$vars) {
if ($return = quickbar_available()) {
$vars['classes_array'][] = 'quickbar-enabled';
$settings = variable_get('quickbar_settings_' . $return['rid'], array());
if (!empty($settings['sticky'])) {
$vars['classes_array'][] = 'quickbar-sticky';
}
elseif (!empty($settings['float'])) {
$vars['classes_array'][] = 'quickbar-float';
}
}
}
function quickbar_get_menu_tree($menu) {
$tree = menu_tree_all_data($menu);
foreach ($tree as $k => $item) {
if ($item['link']['link_path'] == 'admin' && !empty($item['below'])) {
unset($tree[$k]);
$tree = array_merge($tree, $item['below']);
}
}
return $tree;
}
function quickbar_available() {
if (quickbar_is_enabled()) {
global $user;
$role_weights = variable_get('quickbar_role_weights', array());
if (is_array($role_weights)) {
asort($role_weights);
$roles = user_roles();
$use_machine_names = variable_get('quickbar_use_machine_names', 0);
if ($use_machine_names) {
$machine_roles = _quickbar_role_machine_names($roles);
}
$menus = variable_get('quickbar_role_menus', array());
foreach ($role_weights as $rid => $weight) {
if (isset($menus[$rid])) {
$menu = $menus[$rid];
$user_role_index = $use_machine_names ? array_search($machine_roles[$rid], $roles) : $rid;
if (!empty($user->roles[$user_role_index]) && $menu) {
return array(
'rid' => $rid,
'menu' => $menu,
);
}
}
}
}
}
return FALSE;
}
function quickbar_menu_tree($menu) {
$links = array();
$tree = quickbar_get_menu_tree($menu);
quickbar_menu_tree_links($tree, $links);
return $links;
}
function quickbar_menu_navigation_links($tree, $admin_only = FALSE) {
$links = array();
foreach ($tree as $item) {
if (!$item['link']['hidden']) {
$class = '';
$id = str_replace('/', '-', $item['link']['href']);
$id = str_replace('<front>', 'front', $id);
$l = $item['link']['localized_options'];
$l['href'] = $item['link']['href'];
$l['title'] = "<span class='icon'></span>" . $item['link']['title'];
$l['attributes'] = array(
'id' => 'quickbar-link-' . $id,
);
$l['html'] = TRUE;
if (module_exists('menu_item_container')) {
global $base_url;
if (strpos($l['href'], 'menu-item-container') !== FALSE) {
$l['href'] = $base_url . '/' . drupal_get_path_alias(str_replace('%2F', '/', current_path())) . '#';
if (drupal_is_front_page()) {
$l['href'] = $base_url . '#';
}
}
}
$class = ' path-' . $id;
if (quickbar_in_active_trail($item['link']['href'])) {
$class .= ' active-trail';
}
$links['menu-' . $item['link']['mlid'] . $class] = $l;
}
}
return $links;
}
function quickbar_menu_tree_links($tree, &$links, $parent = 'admin', $depth = 0) {
$links[$depth][$parent] = array();
$l = quickbar_menu_navigation_links($tree, TRUE);
if (!empty($l)) {
$links[$depth][$parent] = $l;
}
foreach ($tree as $item) {
if (!$item['link']['hidden']) {
if (!empty($item['below'])) {
quickbar_menu_tree_links($item['below'], $links, $item['link']['href'], $depth + 1);
}
}
}
}
function quickbar_in_active_trail($path, $reset = FALSE) {
static $active_paths;
if (!isset($active_paths) || $reset) {
$active_paths = array();
$trail = menu_get_active_trail();
foreach ($trail as $item) {
if (!empty($item['href'])) {
$active_paths[] = $item['href'];
}
}
}
return in_array($path, $active_paths);
}
function quickbar_quickbar_iconset_info() {
$iconsets = array(
'quickbar' => array(
'title' => 'Admin 1.x',
'css' => drupal_get_path('module', 'quickbar') . '/theme/icons.css',
),
);
$themes = list_themes();
if (array_key_exists('rubik', $themes)) {
$iconsets['quickbar_rubik'] = array(
'title' => 'Rubik',
'css' => drupal_get_path('theme', 'rubik') . '/icons.css',
);
}
return $iconsets;
}
function quickbar_admin_select_info() {
$info = array();
$info['quickbar'] = array(
'title' => t('Quickbar'),
'access arguments' => array(
'access content',
),
'suppress callback' => 'quickbar_suppress',
);
return $info;
}
function quickbar_suppress() {
global $theme_info;
$theme_info->info['quickbar'] = FALSE;
}
function _quickbar_role_machine_names($roles) {
$new_roles = array();
foreach ($roles as $name) {
$machine_name = strtolower($name);
$machine_name = str_replace(' ', '-', $machine_name);
$new_roles[$machine_name] = $name;
}
return $new_roles;
}