You are here

spaces_core.module in Spaces 6

Same filename and directory in other branches
  1. 5.2 spaces_core/spaces_core.module
  2. 5 spaces_core/spaces_core.module

File

spaces_core/spaces_core.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function spaces_core_menu() {
  $items = array();
  if (module_exists('book')) {
    $items['documents'] = array(
      'title' => t('Documents'),
      'description' => t('Displays the parent book node for a given spaces group.'),
      'page callback' => 'spaces_core_documents',
      'page arguments' => array(
        1,
      ),
      'access callback' => 'spaces_feature_access',
      'access arguments' => array(
        'book',
      ),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Implementation of hook_menu_alter().
 *
 * Purge router items provided by the blog module so that views-based
 * replacements will work.
 */
function spaces_core_menu_alter(&$callbacks) {
  if (module_exists('blog')) {
    $unset = array(
      'blog',
      'blog/%user/feed',
      'blog/%user_uid_optional',
    );
    foreach ($unset as $path) {
      if (!empty($callbacks[$path]) && $callbacks[$path]['module'] == 'blog') {
        unset($callbacks[$path]);
      }
    }
  }
}

/**
 * Implementation of hook_help().
 */
function spaces_core_help($page) {
  switch (context_get('spaces', 'feature')) {
    case 'blog':
      return "<p>" . t('The blog is a team discussion place where you can post and discuss information relevant to your team.') . "</p>";
    case 'book':
      return "<p>" . t('The book provides a place for you to post documents and other reference material.') . "</p>";
  }
}

/**
 * Implementation of hook_block().
 */
function spaces_core_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks['tags']['info'] = t('Spaces Core: Tag chart');
    $blocks['book']['info'] = t('Spaces Core: Book navigation');
    return $blocks;
  }
  else {
    if ($op == 'view') {
      switch ($delta) {
        case 'tags':
          return _spaces_core_block_tags();
        case 'book':
          return _spaces_core_block_book();
      }
    }
  }
}

/**
 * Implementation of hook_form_alter().
 */
function spaces_core_form_alter(&$form, $form_state, $form_id) {

  // Book mods
  if ($form['#id'] == 'node-form' && book_type_is_allowed($form['#node']->type)) {
    $node = $form['#node'];
    if (!empty($form['book'])) {

      // Fieldset mods
      $form['book']['#weight'] = !empty($form['body_field']['#weight']) ? $form['body_field']['#weight'] : 0;
      $form['book']['#collapsible'] = $form['book']['#collapsed'] = FALSE;
      if (!empty($form['book']['bid']['#options'])) {

        // Remove "none" option -- do not allow book pages to be orphaned
        unset($form['book']['bid']['#options'][0]);

        // Filter book options by current space
        if ($view = views_get_view('spaces_book_current')) {
          $view
            ->set_display();
          $view
            ->set_items_per_page(0);
          $view
            ->execute();

          // Collect books in this space into an array
          $books = array();
          $books[$node->nid] = 1;
          if (is_array($view->result) && count($view->result)) {
            foreach ($view->result as $row) {
              $books[$row->nid] = 1;
            }
          }

          // Use collected array to filter options
          foreach ($form['book']['bid']['#options'] as $k => $v) {
            if (is_numeric($k) && !isset($books[$k])) {
              unset($form['book']['bid']['#options'][$k]);
            }
          }
        }
      }
    }
  }
  switch ($form_id) {
    case 'user_edit':
      unset($form['og_settings']);

      // Remove the og email settings.
      break;
    case 'comment_form':
      if (!drupal_get_title()) {
        drupal_set_title(t('Reply'));
      }
      break;
  }
}

/**
 *  Implementation of hook_default_views().
 */
function spaces_core_views_default_views() {
  $views = array();
  if (module_load_include('inc', 'spaces_core', 'spaces_core_views') !== FALSE) {
    $default_views = _spaces_core_views_list_views();
    foreach ($default_views as $v) {
      $function = '_spaces_core_views_' . $v;
      if (function_exists($function)) {
        $view = call_user_func($function);
        if (is_object($view) && $view->name) {
          $views[$view->name] = $view;
        }
      }
      else {
        watchdog(WATCHDOG_ERROR, 'An exported view could not be loaded because the function !function was not found.', array(
          '!function' => $function,
        ));
      }
    }
  }
  return $views;
}

/**
 * Implementation of hook_context_default_contexts().
 */
function spaces_core_context_default_contexts() {
  $items = array();
  $items[] = array(
    'namespace' => 'spaces',
    'attribute' => 'feature',
    'value' => 'blog',
    'menu' => 'blog',
    'node' => array(
      'blog',
    ),
    'views' => array(
      'spaces_blog',
    ),
    'block' => array(
      array(
        'module' => 'views',
        'delta' => 'spaces_blog-block_1',
        'region' => 'right',
        'weight' => '11',
      ),
      array(
        'module' => 'views',
        'delta' => 'spaces_blog_comments-block_1',
        'region' => 'right',
        'weight' => '10',
      ),
    ),
    'spaces' => array(
      'label' => t('Blog'),
      'description' => t('A blog for team communications.'),
      'menu' => array(
        array(
          'title' => t('Blog'),
          'href' => 'blog',
        ),
      ),
      'types' => array(),
    ),
  );
  if (module_exists('book')) {
    $items[] = array(
      'namespace' => 'spaces',
      'attribute' => 'feature',
      'value' => 'book',
      'menu' => 'documents',
      'node' => array(
        'book',
      ),
      'block' => array(
        array(
          'module' => 'spaces_core',
          'delta' => 'book',
          'region' => 'right',
          'weight' => -11,
        ),
      ),
      'spaces' => array(
        'label' => t('Documents'),
        'description' => t('A document section for maintaining a knowledge base.'),
        'menu' => array(
          array(
            'title' => t('Documents'),
            'href' => 'documents',
          ),
        ),
        'types' => array(
          'og',
        ),
      ),
    );
  }
  if (module_exists('taxonomy')) {
    $items[] = array(
      'namespace' => 'spaces',
      'attribute' => 'feature',
      'value' => 'taxonomy',
      'views' => array(
        'spaces_taxonomy',
      ),
      'block' => array(
        array(
          'module' => 'spaces_core',
          'delta' => 'tags',
          'region' => 'right',
          'weight' => -11,
        ),
      ),
    );
  }
  return $items;
}

/*
 * This function acts as a fallback in case users delete or re-path the root book page at 'documents'
 */
function spaces_core_documents() {

  // og version of the book root lookup
  if ($space = spaces_get_space()) {
    if ($view = views_get_view('spaces_book_current')) {
      $view
        ->set_display();
      $view
        ->set_items_per_page(0);
      $view
        ->execute();
      if (is_array($view->result) && count($view->result)) {
        $row = array_shift($view->result);
        drupal_goto('node/' . $row->nid);
      }
    }
    context_set_by_condition('node', 'book');
    $message = t('Please add your first book page to get started.');
    $button = theme('context_links', context_links());
    $o = "<div class='spaces-empty'>{$message} <div class='buttons'>{$button}</div></div>";
    return $o;
  }
}

/**
 * Implementation of hook_context_links_alter();
 */
function spaces_core_context_links_alter(&$links) {
  if (context_get('spaces', 'feature') == 'book' && isset($links['book']) && ($space = spaces_get_space())) {
    if ($space
      ->feature_access('book') && ($node = menu_get_object())) {
      if (!empty($node->book['mlid'])) {
        $links['book']['query'] = 'parent=' . $node->book['mlid'];
      }
    }
  }
}

/**
 * BLOCKS =============================================================
 */
function _spaces_core_block_tags() {
  $space = spaces_get_space();
  if (context_get('spaces', 'feature') == 'taxonomy') {
    $terms = taxonomy_terms_parse_string(arg(2));
    $tid = $terms['tids'][0];
    if ($term = taxonomy_get_term($tid)) {
      $vocab = taxonomy_get_vocabulary($term->vid);
      $view = views_get_view('spaces_tags');
      $view->filter[1]['value'] = array(
        $term->vid,
      );
      $block['content'] = views_build_view('block', $view, array(
        $space->sid,
      ));
      $block['subject'] = $vocab->name;
      return $block;
    }
  }
}

/**
 * Spaces version of the book nav block -- shows all root books in a group
 */
function _spaces_core_block_book() {
  $space = spaces_get_space();
  $block = array();
  if ($space) {
    if ($node = menu_get_object()) {
      $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
    }

    // Set customized title
    $features = spaces_features();
    $feature_menu = space_customizer_menu::customize($space, 'book', $features['book']->spaces['menu']);
    $block['subject'] = isset($feature_menu['documents']['title']) ? $feature_menu['documents']['title'] : t('Documents');

    // Generate book tree per book node in current space
    $output = '';
    if ($view = views_get_view('spaces_book_current')) {
      $view
        ->set_display();
      $view
        ->set_items_per_page(0);
      $view
        ->execute();
      if (is_array($view->result) && count($view->result)) {
        foreach ($view->result as $row) {
          $output .= menu_tree(book_menu_name($row->nid));
        }
      }
    }
    $block['content'] = $output;
  }
  return $block;
}

Functions

Namesort descending Description
spaces_core_block Implementation of hook_block().
spaces_core_context_default_contexts Implementation of hook_context_default_contexts().
spaces_core_context_links_alter Implementation of hook_context_links_alter();
spaces_core_documents
spaces_core_form_alter Implementation of hook_form_alter().
spaces_core_help Implementation of hook_help().
spaces_core_menu Implementation of hook_menu().
spaces_core_menu_alter Implementation of hook_menu_alter().
spaces_core_views_default_views Implementation of hook_default_views().
_spaces_core_block_book Spaces version of the book nav block -- shows all root books in a group
_spaces_core_block_tags BLOCKS =============================================================