You are here

function template_preprocess_oa_toolbar in Open Atrium Toolbar 7.2

Preprocess function for the oa_toolbar block.

1 call to template_preprocess_oa_toolbar()
template_preprocess_oa_breadcrumb in ./oa_toolbar.module
Preprocess function for the oa_breadcrumb.

File

./oa_toolbar.module, line 290
Provides hook implementations and functionality for oa_toolbar.

Code

function template_preprocess_oa_toolbar(&$vars) {
  global $user;
  $vars['oa_toolbar_btn_class'] = variable_get('oa_toolbar_style', 0) ? '' : 'btn-inverse';

  // Separators
  $vars['oa_toolbar_separator'] = t('>');
  $vars['home_url'] = url('<front>', array(
    'absolute' => TRUE,
  ));

  // Spaces
  $space = NULL;
  $space_id = oa_core_get_space_context();
  $node = menu_get_object();
  oa_core_add_node_class($node);
  $front_panel = drupal_is_front_page() && !(isset($node) && $node->type == OA_SPACE_TYPE);
  if ($front_panel) {
    $space_id = 0;

    // reset breadcrumbs on home page
  }
  $vars['spaces_label'] = '';
  $vars['spaces_recent'] = '';
  $vars['spaces_favorite'] = '';
  $vars['sections_label'] = '';
  $vars['sections_list'] = '';
  $vars['parent_label'] = '';
  $vars['parent_url'] = '';

  // grab list of user's spaces and sections
  $vars['space_label'] = variable_get('oa_toolbar_spacetext', t("Spaces"));
  $vars['space_url'] = url('spaces');
  $parent_id = NULL;
  $space_type = OA_SPACE_TYPE;
  $subspace_type_names = '';
  if (isset($space_id) && ($space = node_load($space_id)) && node_access('view', $space)) {
    $title = $space->title;
    $space_type = $space->type;
    $subspace_type_names = $space_type == OA_SPACE_TYPE ? t('Subspaces') : t('Subgroups');
    oa_core_add_node_class($space);
    $vars['space_label'] = check_plain($title);
    $vars['space_url'] = url('node/' . $space_id);
    if (isset($vars['show_parents'])) {
      $parents = oa_core_get_parents_with_titles($space_id, $space_type, NODE_PUBLISHED, TRUE);
      if ($parents) {

        // only show first parent
        $title = reset($parents);
        $vars['parent_label'] = $title;
        $parent_id = key($parents);
        $vars['parent_url'] = url('node/' . $parent_id);
        if (count($parents) > 1) {
          $list = oa_core_truncate_list($parents, 15);
          foreach ($list as $nid => $title) {
            $list[$nid] = l($title, 'node/' . $nid);
          }
          $vars['parent_list'] = theme('item_list', array(
            'items' => $list,
            'title' => t('Siblings'),
            'type' => 'ul',
          ));
        }
        $siblings = oa_core_get_groups_by_parent($parent_id, $space_type, NODE_PUBLISHED);
        if (count($siblings) > 1) {
          $items = oa_core_get_titles($siblings);
          $list = oa_core_truncate_list($items['links'], 15);
          $vars['sibling_list'] = theme('item_list', array(
            'items' => $list,
            'title' => $subspace_type_names,
            'type' => 'ul',
          ));
        }
      }
    }
  }
  else {
    $space = NULL;
  }
  $space_type_name = $space_type == OA_SPACE_TYPE ? t('Space') : t('Group');
  $space_type_names = $space_type == OA_SPACE_TYPE ? t('Spaces') : t('Groups');
  $all_spaces = $space_type == OA_SPACE_TYPE ? 'spaces' : 'groups';
  $show_all = TRUE;
  $spaces = NULL;
  $sort_field = 'title';
  if ($user->uid) {
    if (variable_get('oa_toolbar_recent', 1) == 1) {

      // First get recent history.  This cannot be cached, so limit to 10
      $limit = 10;
      $spaces = array_map(function ($node) {
        return $node->nid;
      }, oa_core_get_groups_by_user_access($user, FALSE, NULL, NULL, NULL, $space_type, 'history', $limit));
      if (count($spaces) < $limit) {

        // if not enough recent spaces, then add the rest based on reverse creation timestamp
        $more_spaces = array_map(function ($node) {
          return $node->nid;
        }, oa_core_get_groups_by_user_access($user, FALSE, NULL, NULL, NULL, $space_type, '-created', $limit));
        $more_spaces = array_diff($more_spaces, $spaces);
        $spaces = array_merge($spaces, $more_spaces);
      }
      $menu_title = $space_type == OA_SPACE_TYPE ? t('Recent Spaces') : t('Recent Groups');
      $sort_field = '';
    }
    else {

      // Otherwise get all subscribed spaces.  This is cached.
      // This only returns spaces with Direct membership.  No inherited subspaces
      $spaces = array_map(function ($node) {
        return $node->nid;
      }, oa_core_get_groups_by_user_access($user, FALSE, NULL, NULL, NULL, $space_type, NULL, 50));
      $menu_title = $space_type == OA_SPACE_TYPE ? t('Member Spaces') : t('Groups');
    }
  }
  if (empty($spaces)) {

    // og_get_entity_groups doesn't return anything for anonymous users
    // so return list of all public spaces
    $only_top = variable_get('oa_toolbar_toplevel', 1) == 1;
    $spaces = oa_core_get_public_spaces(array(
      OA_SPACE_TYPE => OA_SPACE_TYPE,
    ), NODE_PUBLISHED, FALSE, TRUE, $only_top);
    $menu_title = t('Public Spaces');
  }
  if (empty($spaces)) {
    $list = array();
  }
  else {
    $items = oa_core_get_titles($spaces, $space_type, $sort_field);
    $list = oa_core_truncate_list($items['links'], 20, l(t('All ' . $space_type_names . '...'), $all_spaces, array(
      'attributes' => array(
        'class' => array(
          'more-link',
        ),
      ),
    )), $show_all);
  }
  if (user_access('create ' . $space_type . ' content', $user)) {
    $jump_links = oa_toolbar_get_jump_links('space');
    $list[] = array(
      'data' => l(t('Create new ' . $space_type_name), 'node/add/' . str_replace('_', '-', $space_type), array(
        'attributes' => array(
          'class' => array(
            'more-link',
          ),
        ),
      )) . $jump_links,
      'class' => !empty($jump_links) ? array(
        'dropdown-submenu',
      ) : array(),
    );
  }
  if (module_exists('oa_sitemap')) {
    $list[] = l(t('Site map'), 'sitemap', array(
      'attributes' => array(
        'class' => array(
          'more-link',
        ),
      ),
    ));
  }
  if (!empty($list)) {
    $vars['home_spaces'] = theme('item_list', array(
      'items' => $list,
      'title' => $menu_title,
      'type' => 'ul',
    ));
  }
  $subspace_list = NULL;
  if (!empty($space) && module_exists('oa_subspaces')) {
    $subspaces = oa_core_get_groups_by_parent($space_id, $space_type, NODE_PUBLISHED);
    if (!empty($subspaces)) {
      $items = oa_core_get_titles($subspaces, $space_type);
      $subspace_list = oa_core_truncate_list($items['links'], 15);
    }
  }
  if (module_exists('oa_favorites')) {

    // get list of favorite spaces
    $items = oa_favorites_get_spaces($user);
    if (!empty($items['links'])) {
      $vars['spaces_favorite'] = theme('item_list', array(
        'items' => $items['links'],
        'title' => t('Favorite Spaces'),
        'type' => 'ul',
      ));
    }
  }
  if (!empty($space) && $space->type == OA_SPACE_TYPE) {
    $current_section = oa_core_get_section_context();
    $home_name = variable_get('oa_toolbar_hometext', '');
    $section_name = '';
    $section_url = '';
    if ($space_id) {
      $add_section = l(t('Add Section'), 'node/add/oa-section');
      $section_name = $home_name;
      $section_url = url('node/' . $space_id);
      $items = array();

      // grab the titles of the sections for the current space
      $sections = oa_core_space_sections($space_id, NODE_PUBLISHED, FALSE, array(
        'field_oa_section|tid',
      ));
      if (!empty($sections)) {

        // check for match with current section id
        foreach ($sections as $sid => $section) {
          if ($sid == $current_section) {
            oa_core_add_taxonomy_class($section->field_oa_section_tid);
            $section_name = $section->title;
            $section_url = url('node/' . $sid);
            $items[] = l($section->title, 'node/' . $sid, array(
              'attributes' => array(
                'class' => array(
                  'active',
                ),
              ),
            ));
          }
          else {
            $items[] = l($section->title, 'node/' . $sid);
          }
        }
      }
      if (module_exists('oa_subspaces') && og_user_access('node', $space_id, 'create ' . OA_SPACE_TYPE . ' content', $user)) {
        $jump_links = oa_toolbar_get_jump_links('space', array(
          OA_PARENT_SPACE => $space_id,
        ));
        $add_subspace = array(
          'data' => l(t('Create new subspace'), 'node/add/' . str_replace('_', '-', OA_SPACE_TYPE), array(
            'attributes' => array(
              'class' => array(
                'more-link',
              ),
            ),
            'query' => array(
              OA_PARENT_SPACE => $space_id,
            ),
          )) . $jump_links,
          'class' => !empty($jump_links) ? array(
            'dropdown-submenu',
          ) : array(),
        );
        if (!empty($subspace_list)) {
          $subspace_list[] = $add_subspace;
        }
        else {
          $items[] = $add_subspace;
        }
      }
      if (og_user_access('node', $space_id, "create " . OA_SECTION_TYPE . " content", $user)) {
        $jump_links = oa_toolbar_get_jump_links('section');
        $items[] = array(
          'data' => l(t('Create new section'), 'node/add/' . str_replace('_', '-', OA_SECTION_TYPE), array(
            'attributes' => array(
              'class' => array(
                'more-link',
              ),
            ),
          )) . $jump_links,
          'class' => !empty($jump_links) ? array(
            'dropdown-submenu',
          ) : array(),
        );
      }
      if (!empty($items)) {
        $vars['sections_list'] = theme('item_list', array(
          'items' => $items,
          'title' => t('Sections'),
          'type' => 'ul',
        ));
      }
    }
    if (!empty($subspace_list)) {
      $vars['subspaces'] = theme('item_list', array(
        'items' => $subspace_list,
        'title' => $subspace_type_names,
        'type' => 'ul',
      ));
    }

    // Current Section Name from current page name
    if (isset($node->type)) {
      switch ($node->type) {
        case OA_SECTION_TYPE:
          $section_name = $node->title;
          $section_url = url('node/' . $node->nid);
          break;
        case OA_SPACE_TYPE:
          $section_name = $home_name;
          $section_url = url('node/' . $node->nid);
          break;
        case OA_GROUP_TYPE:
          $section_name = $home_name;
          $section_url = url('node/' . $node->nid);
          break;
      }
    }
    $vars['section_label'] = $section_name;
    $vars['section_url'] = $section_url;
  }
  if ($front_panel) {
    $vars['subspaces'] = !empty($vars['home_spaces']) ? $vars['home_spaces'] : '';
    unset($vars['home_spaces']);
  }
  if (variable_get('oa_breadcrumb_menu', FALSE)) {
    $vars['mainmenu'] = theme('oa_navigation', array(
      'menu_name' => 'main-menu',
      'show_menu' => TRUE,
    ));
  }
}