You are here

quickbar_extras.module in Quickbar 6

Same filename and directory in other branches
  1. 7.2 modules/quickbar_extras/quickbar_extras.module

File

modules/quickbar_extras/quickbar_extras.module
View source
<?php

/**
 * Implements hook_init().
 */
function quickbar_extras_init() {
  drupal_add_css(drupal_get_path('module', 'quickbar_extras') . '/css/quickbar_extras.css');
  if (arg(3) == 'quickbar' && arg(5) == 'edit') {
    drupal_add_js(drupal_get_path('module', 'quickbar_extras') . '/js/quickbar_extras.admin.js');
  }
}

/**
 * Implements hook_form_alter().
 */
function quickbar_extras_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'quickbar_configure_form') {
    $rid = $form['#parameters'][2];
    $menu_used = variable_get('quickbar_' . $rid, '');
    $form['quickbar_extras'] = array(
      '#type' => 'fieldset',
      '#title' => t('Extra settings'),
      '#collapsible' => 1,
      '#collapsed' => 0,
      '#attributes' => array(
        'class' => array(
          'quickbar-extras',
        ),
      ),
    );
    $settings = variable_get('quickbar_extras_settings_' . $rid, '');

    // Pulls all menu links from the database
    $sql_select = 'mlid , link_title from {menu_links}';
    $sql_where = '(menu_name = "%s") AND (plid = 0) AND (hidden = 0) AND (link_path NOT LIKE "%\\%%")';
    $sql = "select {$sql_select} where {$sql_where} order by weight ASC";
    $query = db_query($sql, $menu_used);
    $form['quickbar_extras']['organize'] = array(
      '#type' => 'fieldset',
      '#title' => t('Organize Menu Links'),
      '#collapsible' => 1,
      '#collapsed' => 0,
      '#attributes' => array(
        'class' => array(
          'quickbar-organize',
        ),
      ),
    );
    while ($record = db_fetch_array($query)) {
      $mlid = $record['mlid'];
      $title = $record['link_title'];
      $form['quickbar_extras']['organize']['mlid_' . $mlid] = array(
        '#type' => 'select',
        '#title' => $title,
        '#options' => array(
          'left' => t('Left'),
          'right' => t('Right'),
        ),
        '#default_value' => isset($settings['menu']['mlid_' . $mlid]) ? $settings['menu']['mlid_' . $mlid] : 'left',
        '#attributes' => array(
          'class' => array(
            'menu-options',
          ),
        ),
      );
    }
    $form['quickbar_extras']['user'] = array(
      '#type' => 'fieldset',
      '#title' => t('User Settings'),
      '#collapsible' => 1,
      '#collapsed' => 1,
      '#attributes' => array(
        'class' => array(
          'user-settings',
        ),
      ),
      'show_username' => array(
        '#type' => 'checkbox',
        '#title' => t('Show username in toolbar'),
        '#description' => t('If checked, the username will show on the right side of the toolbar.'),
        '#default_value' => isset($settings['show_username']) ? $settings['show_username'] : 0,
      ),
      'options' => array(
        '#type' => 'fieldset',
        '#title' => 'Username Options',
        '#attributes' => array(
          'id' => 'username-options',
        ),
        'show_username_prefix' => array(
          '#type' => 'textfield',
          '#title' => t('Prefix to username'),
          '#description' => t("This field's data will be displayed before the username. Be sure to include a space after the text if necessary."),
          '#default_value' => isset($settings['show_username_prefix']) ? $settings['show_username_prefix'] : 'Hello, ',
        ),
        'show_username_suffix' => array(
          '#type' => 'textfield',
          '#title' => t('Suffix to username'),
          '#description' => t("This field's data will be displayed after the username. Be sure to include a space before the text if necessary."),
          '#default_value' => isset($settings['show_username_suffix']) ? $settings['show_username_suffix'] : '!',
        ),
        'show_username_link' => array(
          '#type' => 'checkbox',
          '#title' => t('Username links to user profile page'),
          '#description' => t('If checked, the username will be a link that the user can use to access their profile page.'),
          '#default_value' => isset($settings['show_username_link']) ? $settings['show_username_link'] : 0,
        ),
        'show_logout_link_prefix' => array(
          '#type' => 'textfield',
          '#title' => t('Prefix to the log  link'),
          '#description' => t("This field's data will be displayed before the log out. Be sure to include a space after the text if necessary."),
          '#default_value' => isset($settings['show_logout_link_prefix']) ? $settings['show_logout_link_prefix'] : '',
        ),
        'show_logout_link_suffix' => array(
          '#type' => 'textfield',
          '#title' => t('Suffix to the log  link'),
          '#description' => t("This field's data will be displayed after the log out. Be sure to include a space before the text if necessary."),
          '#default_value' => isset($settings['show_logout_link_suffix']) ? $settings['show_logout_link_suffix'] : '',
        ),
        'show_logout_link' => array(
          '#type' => 'checkbox',
          '#title' => t('Include a Log out link'),
          '#description' => t('If checked, the username will be followed by a log out link.'),
          '#default_value' => isset($settings['show_logout_link']) ? $settings['show_logout_link'] : 0,
        ),
      ),
    );
    $form['#submit'][] = 'quickbar_extras_configure_form_submit';

    // Push submit button to bottom of form
    $form['submit']['#weight'] = 1000;
  }
}

/**
 * Submission handler for quickbar_extras_form_alter().
 */
function quickbar_extras_configure_form_submit(&$form, &$form_state) {
  $rid = $form['#parameters'][2];
  $menu_settings = array();
  foreach ($form_state['values'] as $value => $position) {
    $pos = strpos($value, 'mlid_');
    if ($pos === 0) {

      //$mlid = substr($value, 5);
      $menu_settings['menu'][$value] = $position;
    }
  }
  $user_settings = array();
  $menu_settings['show_username'] = $form_state['values']['show_username'];
  $menu_settings['show_username_prefix'] = $form_state['values']['show_username_prefix'];
  $menu_settings['show_username_suffix'] = $form_state['values']['show_username_suffix'];
  $menu_settings['show_username_link'] = $form_state['values']['show_username_link'];
  $menu_settings['show_logout_link'] = $form_state['values']['show_logout_link'];
  $menu_settings['show_logout_link_prefix'] = $form_state['values']['show_logout_link_prefix'];
  $menu_settings['show_logout_link_suffix'] = $form_state['values']['show_logout_link_suffix'];
  variable_set('quickbar_extras_settings_' . $rid, $menu_settings);
}

/**
 * Implements hook_preprocess_quickbar().
 */
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,
        );
      }
    }
  }
}

Functions