You are here

function quickbar_extras_preprocess_quickbar in Quickbar 6

Same name and namespace in other branches
  1. 7.2 modules/quickbar_extras/quickbar_extras.module \quickbar_extras_preprocess_quickbar()

Implements hook_preprocess_quickbar().

File

modules/quickbar_extras/quickbar_extras.module, line 158

Code

function quickbar_extras_preprocess_quickbar(&$vars) {
  global $user;

  //$role_weights = variable_get('quickbar_role_weights', '');
  $role_weights = unserialize(variable_get('quickbar_role_weights', ''));
  $settings = array();
  if (is_array($role_weights)) {

    // Sort roles
    asort($role_weights);

    // Get the user roles
    $roles = user_roles();

    // Get some variables we might use
    $use_machine_names = variable_get('quickbar_use_machine_names', 0);
    if ($use_machine_names) {
      $machine_roles = _quickbar_role_machine_names($roles);
    }

    // Loop through the roles looking for a role that matches the current users
    // role and also has a menu associated with it.
    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]) && ($settings = variable_get('quickbar_extras_settings_' . $rid, array()))) {
        break;
      }
    }
  }
  $menu_keys = array_keys($vars['tree'][0]['admin']);
  if (!empty($settings['menu'])) {
    $move_menus = array();

    // We are first going to pull all of the menu ids out of the variable...
    foreach ($settings['menu'] as $menu_id => $position) {

      // ... if the current menu id being checked has a value of 'right'...
      if ($position == 'right') {

        // ... then we need to look in the menu items getting ready to be displayed...
        foreach ($menu_keys as $menu_key) {
          $menu_class = explode(' ', $menu_key);
          $menu_id = str_replace('mlid_', '', $menu_id);

          // ... if there is a match, add it to our move_menu array for further processing
          if ($menu_class[0] == 'menu-' . $menu_id) {
            $move_menus[] = $menu_key;
          }
        }
      }
    }
    if (!empty($move_menus)) {
      foreach ($move_menus as $move_menu) {
        $vars['tree'][0]['right'][$move_menu] = $vars['tree'][0]['admin'][$move_menu];
        unset($vars['tree'][0]['admin'][$move_menu]);
      }
    }
    if (!empty($settings['show_username'])) {
      global $user;
      $show_username = isset($settings['show_username_link']) ? $settings['show_username_link'] : FALSE;
      $username_prefix = isset($settings['show_username_prefix']) ? $settings['show_username_prefix'] : 'Hello, ';
      $username_suffix = isset($settings['show_username_suffix']) ? $settings['show_username_suffix'] : '!';
      $show_logout = isset($settings['show_logout_link']) ? $settings['show_logout_link'] : FALSE;
      $logout_prefix = isset($settings['show_logout_link_prefix']) ? $settings['show_logout_link_prefix'] : '';
      $logout_suffix = isset($settings['show_logout_link_suffix']) ? $settings['show_logout_link_suffix'] : '';
      if ($show_username) {
        $vars['tree'][0]['right']['username'] = array(
          'href' => 'user/' . $user->uid,
          'title' => $username_prefix . $user->name . $username_suffix,
          'attributes' => array(
            'class' => 'username-link',
          ),
          'html' => TRUE,
        );
      }
      else {
        $vars['tree'][0]['right']['username'] = array(
          'title' => $username_prefix . $user->name . $username_suffix,
        );
      }
      if ($show_logout) {
        $vars['tree'][0]['right']['logout'] = array(
          'href' => 'logout',
          'title' => $logout_prefix . 'Log out' . $logout_suffix,
          'attributes' => array(
            'class' => 'logout-link',
          ),
          'html' => 1,
        );
      }
    }
  }
}