You are here

spaces.module in Spaces 5.2

File

spaces.module
View source
<?php

include_once drupal_get_path('module', 'spaces') . '/spaces_views.inc';
define('SPACES_ARCHIVE_TIMESTAMP', 60 * 60 * 24 * 14);

// 2 weeks
define('SPACES_FEATURE_DISABLED', 0);

/**
 * Implementation of hook_init().
 */
function spaces_init() {
  if (strpos($_GET['q'], 'admin/build/spaces') === 0) {
    include_once drupal_get_path('module', 'spaces') . '/spaces_admin.inc';
  }
}

/**
 * Implementation of hook_perm().
 */
function spaces_perm() {
  return array(
    'administer spaces',
  );
}

/**
 * Implementation of hook_context_prefix_provider().
 */
function spaces_context_prefix_provider() {
  $items = array();
  foreach (spaces_types() as $type => $info) {
    $items['spaces_' . $type] = array(
      'name' => $info['title'],
      'description' => t('Sets a spaces context.'),
      'callback' => 'spaces_init_context',
      'callback arguments' => array(
        $type,
      ),
      'example' => 'my-space',
    );
  }
  return $items;
}

/**
 * Context prefix provider callback.
 */
function spaces_init_context($type, $sid) {
  static $once;
  if (!isset($once)) {
    $once = FALSE;
  }
  if (!$once) {

    // @TODO: remove the hardcoded space type here and attempt to
    // discover through the context prefix param : (
    context_set('spaces', 'sid', $sid);
    $space = spaces_load($type, $sid, TRUE);
    spaces_set_space($space);
    $once = TRUE;
  }
}

/**
 * Implementation of hook_menu().
 */
function spaces_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/build/spaces',
      'title' => t('Spaces presets'),
      'description' => t('Create and configure spaces for your site.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_preset_default_form',
      ),
      'access' => user_access('administer spaces'),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/build/spaces/presets',
      'title' => t('Presets'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_preset_default_form',
      ),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -1,
    );
    $items[] = array(
      'path' => 'admin/build/spaces/presets/add',
      'title' => t('Add'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_preset_form',
        'add',
      ),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/spaces/presets/edit',
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_preset_form',
        'edit',
      ),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/spaces/presets/delete',
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_preset_delete_form',
      ),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/spaces/presets/disable',
      'callback' => '_spaces_preset_disable_page',
      'type' => PAGE_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/build/spaces/presets/enable',
      'callback' => '_spaces_preset_enable_page',
      'type' => PAGE_CALLBACK,
    );
  }
  else {
    spaces_router('menu');
  }
  return $items;
}

/**
 *  Implementation of hook_user().
 */
function spaces_user($op, &$edit, &$account, $category = NULL) {
  if (in_array($op, array(
    'view',
    'form',
  ))) {
    spaces_router('user view', $account);
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function spaces_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'view':
      if ($page && !$teaser && arg(0) == 'node' && arg(1) == $node->nid) {
        spaces_router('node view', $node);
      }
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function spaces_form_alter($form_id, &$form) {
  if ($form['#id'] == 'node-form' && arg(0) . '/' . arg(1) != 'admin/content') {
    spaces_router('node form', $form['#node']);
  }
}

/**
 * Implementation of hook_block()
 */
function spaces_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[1]['info'] = t('Spaces: Contextual Tools');
    $blocks[2]['info'] = t('Spaces: Navigation');
    $blocks[3]['info'] = t('Spaces: Utility links');
    return $blocks;
  }
  else {
    if ($op == 'view') {
      switch ($delta) {
        case 1:
          return _spaces_block_tools();
        case 2:
          return _spaces_block_nav();
        case 3:
          return _spaces_block_utility_links();
      }
    }
  }
}

/**
 * Implementation of hook_context_define()
 *
 *  hook_context_define provides a central method to define contextual behavior. The spaces
 *  module extends this hook in the "spaces" key namespace. Available attributes are:
 *  'label', 'description', 'options', 'options_function', '#weight'
 *
 * @return
 *   Keyed array which defines features.
 */
function spaces_context_define() {
  $items = array();
  return $items;
}

/**
 * Implementation of hook_spaces_settings().
 */
function spaces_spaces_settings() {
  return array(
    'home' => new space_setting_home(),
  );
}

/**
 * Implementation of hook_spaces_customizers().
 */
function spaces_spaces_customizers() {
  return array(
    'menu' => new space_customizer_menu(),
    'views' => new space_customizer_views(),
  );
}

/**
 * Implementation of hook_views_pre_query().
 */
function spaces_views_pre_query(&$view) {

  // Make spaces filter'd views non-cacheable
  foreach ($view->filter as $filter) {
    if ($filter['field'] == 'spaces.type') {
      $view->is_cacheable = false;
    }
  }
  if ($view->build_type == 'page' && ($space = spaces_get_space())) {
    if ($feature = context_get('spaces', 'feature')) {
      $view = space_customizer_views::customize($space, $feature, $view);
    }
  }
}

/**
 * SPACES API =========================================================
 */

/**
 * Interface for space objects.
 */
interface space {

  /**
   * Core API-related functions
   */
  function __construct($type, $sid = NULL, $is_active = FALSE);
  function save();
  function delete();

  /**
   * Method that defines the feature options available for this space
   * type.
   *
   * @return array
   *   A FormAPI suitable options array of feature options.
   */
  function feature_options();

  /**
   * Method that allows the space type to add to/modify the utility
   * "links" provided to visitors in a space.
   *
   * @param $links
   *   The current space utility links (for use with theme_links) array
   *   passed by reference.
   */
  function links(&$links);

  /**
   * Method that provides a space-type specific check for whether the
   * provided feature is accessible by the current user.
   *
   * @param $feature
   *   The feature identifier string.
   *
   * @return bool
   *   TRUE if the user has access to this feature. FALSE if not.
   */
  function feature_access($feature = NULL);

  /**
   * Method that provides a space-type specific check for whether the
   * the space can be administered by the current user.
   *
   * @return bool
   *   TRUE if the user has admin access to this space. FALSE if not.
   */
  function admin_access();

  /**
   * Master router method that allows the space type to define routing
   * workflow rules. Currently called at the following hooks:
   *
   * hook_menu(), where $may_cache == FALSE
   * hook_nodeapi(), where $op == 'view'
   * hook_form_alter(), when editing a node
   * hook_user(), where $op == 'view'
   *
   * @param $op
   *   The current hook from which the router is being called.
   *   Can be one of the following: 'menu', 'node view', 'node form',
   *   'user view'
   * @param $object
   *   The object relevant to the current $op called. e.g. when
   *   $op == 'node view', $object is the node object.
   * @param $is_active
   *   Boolean for whether this router has been called as part of a
   *   fully instantiated space object. If FALSE, the router should
   *   not assume the space has been fully constructed and should take
   *   the appropriate actions as necessary.
   *
   * @return bool
   *   TRUE to allow the user to pass through. FALSE to return a
   *   drupal_access_denied() page to the user.
   */
  function router($op, $object = NULL, $is_active = TRUE);

  /**
   * Redirect handler that abstracts redirect logic and allows space
   * types to define their own definitions of various spaces concepts.
   *
   * @param $op
   *   The page being redirected to. Currently only $op value is 'home'
   */
  function redirect($op = 'home');

  /**
   * Allows the space type to implement custom form options on the
   * feature/preset form.
   *
   * @return array
   *   A FormAPI element array. It will be included as a child element
   *   of the master space feature form.
   */
  function form();

  /**
   * Validate handler for the space type's custom form.
   *
   * @param $values
   *   The submitted values.
   */
  function validate($values);

  /**
   * Custom submit handler for the space type's custom form. For
   * example, allows the space type to process values in preparation
   * for spaces_save().
   *
   * @param $values
   *   The submitted values.
   *
   * @return array
   *   An array of values.
   */
  function submit($values);

  /**
   * Views filter callback that allows the space type to filter views
   * when it is the current active space.
   *
   * @param $query
   *   The views query object passed by reference.
   */
  function views_filter($is_active, &$query);

  /**
   * Allows the space type to take additional action when enforcing a
   * preset against the current space.
   *
   * @param $preset
   *   A spaces preset definition.
   */
  function preset_enforce($preset);

}

/**
 * Interface for space settings.
 */
interface space_setting {
  function form($space, $value = array());
  function validate($space, $value);
  function submit($space, $value);

}

/**
 * Interface for space customizers.
 */
interface space_customizer {
  function form($space, $feature);
  function validate($space, $feature, $value);
  function submit($space, $feature, $value);
  function customize($space, $feature, $object = NULL);

}

/**
 * Provides a homepage setting for each space.
 */
class space_setting_home implements space_setting {
  var $id = 'home';
  function form($space, $value = array()) {
    $options = array(
      0 => '---',
    );
    foreach (spaces_features($space->type) as $f => $feature) {
      if ($feature->spaces['menu'] && $space->features[$f] != SPACES_FEATURE_DISABLED) {
        $options[$f] = $feature->spaces['label'];
      }
    }
    $form = array(
      '#title' => t('Homepage'),
      '#description' => t('The default page for this space.'),
      '#type' => 'select',
      '#options' => $options,
      '#validate' => array(
        'spaces_setting_validate' => array(
          $this->id,
        ),
      ),
      '#default_value' => $value ? $value : 0,
    );
    return $form;
  }
  function validate($space, $value) {

    // Exclude space "prototypes" (like that used for the preset form)
    if ($space->sid) {
      if (!$value && is_array($space->features) && array_sum($space->features) != 0) {
        form_set_error('settings][' . $this->id, t('You must select a homepage for this space.'));
      }
    }
  }
  function submit($space, $value) {
    return $value;
  }

}

/**
 * Customizer for feature menus.
 */
class space_customizer_menu implements space_customizer {
  var $name = 'Menu';
  function form($space, $feature) {
    $features = spaces_features();
    $f = $features[$feature];

    // Customize menus
    if (isset($f->spaces['menu']) && count($f->spaces['menu'])) {

      // Get customized values
      $feature_menu = $f->spaces['menu'];
      $feature_menu = $this
        ->customize($space, $feature, $feature_menu);
      $form = array();
      foreach ($feature_menu as $path => $item) {
        $form[$path] = array(
          '#title' => $path,
          '#type' => 'fieldset',
          '#tree' => TRUE,
        );
        $form[$path]['title'] = array(
          '#title' => t('Title'),
          '#type' => 'textfield',
          '#size' => 40,
          '#maxlength' => 255,
          '#default_value' => $item['title'],
        );
      }
    }
    return $form;
  }
  function validate($space, $feature, $value) {
    return;
  }
  function submit($space, $feature, $value) {
    $features = spaces_features();
    $feature_menu = $features[$feature]->spaces['menu'];
    foreach ($value as $path => $item) {
      if ($item == $feature_menu[$path]) {
        unset($value[$path]);
      }
    }
    return $value;
  }
  function customize($space, $feature, $menu = NULL) {
    if (isset($space->customizer[$feature]['menu'])) {
      $custom_menu = array();
      $customizer = $space->customizer[$feature]['menu'];
      foreach ($menu as $path => $item) {
        if (isset($customizer[$path])) {
          $menu[$path] = $customizer[$path];
        }
      }
    }
    return $menu;
  }

}

/**
 * Customizer for views titles and headers.
 */
class space_customizer_views implements space_customizer {
  var $name = 'Views';
  function form($space, $feature) {
    $features = spaces_features();
    $f = $features[$feature];
    $form = array();
    foreach ($f->views as $view_name) {
      $view = views_get_view($view_name);
      $view = $this
        ->customize($space, $feature, $view);

      // Only allow customization of page views for now
      // @TODO: add input format checking on headers/footers/etc.
      if ($view && $view->page == TRUE) {
        $form[$view_name] = array(
          '#title' => $view_name,
          '#type' => 'fieldset',
          '#tree' => TRUE,
        );

        // $default_page_title = isset($customizer['views'][$view_name]['page_title']) ? $customizer['views'][$view_name]['page_title'] : $view->page_title;
        $form[$view_name]['page_title'] = array(
          '#title' => t('Page title'),
          '#type' => 'textfield',
          '#size' => 40,
          '#default_value' => $view->page_title,
        );

        // $default_page_header = isset($customizer['views'][$view_name]['page_header']) ? $customizer['views'][$view_name]['page_header'] : $view->page_header;
        $form[$view_name]['page_header'] = array(
          '#title' => t('Page header'),
          '#type' => 'textarea',
          '#rows' => 2,
          '#cols' => 40,
          '#default_value' => $view->page_header,
        );
      }
    }
    return $form;
  }
  function validate($space, $feature, $value) {
    return;
  }
  function submit($space, $feature, $value) {

    // Would like to prune unset values, but it appears that views
    // caching makes it unusable...
    return $value;
  }
  function customize($space, $feature, $view = NULL) {
    if (isset($space->customizer[$feature]['views'][$view->name])) {

      // Apply customizations to the view object
      $customizer = $space->customizer[$feature]['views'][$view->name];
      foreach ($customizer as $property => $value) {
        $view->{$property} = $value;
      }
    }
    return $view;
  }

}

/**
 * Load a space.
 *
 * @param $type
 *   The type of the space to be loaded. Must be one of the keys in the
 *   array returned by spaces_types(). 
 * @param $sid
 *   The id of the space to be loaded. If omitted, a "prototype" space
 *   will be constructed.
 * @param $is_active
 *   Optional boolean flag for whether this space is active or not.
 *   Defaults to FALSE.
 *
 * @return
 *   The requested space object or FALSE if something went wrong.
 */
function spaces_load($type, $sid = NULL, $is_active = FALSE) {
  $types = spaces_types();
  if (isset($types[$type])) {
    $class = $types[$type]['class'];

    // Create a new space object
    $space = new $class($type, $sid, $is_active);

    // Initialize various space variables
    $space->type = $type;
    $space->features = array();
    $space->settings = array();
    $space->customizer = array();

    // Initialize space specific settings if $sid is provided
    if ($sid) {
      $space->sid = $sid;
      if ($prefix = context_prefix_api('load', array(
        'provider' => 'spaces_' . $type,
        'id' => $sid,
      ))) {
        $space->prefix = $prefix['prefix'];
      }

      // Load features
      $result = db_query('SELECT id, value FROM {spaces_features} WHERE sid = %d AND type = "%s"', $sid, $type);
      while ($row = db_fetch_object($result)) {
        $space->features[$row->id] = $row->value;
      }

      // Load settings
      $result = db_query('SELECT id, value FROM {spaces_settings} WHERE sid = %d AND type = "%s"', $sid, $type);
      while ($row = db_fetch_object($result)) {
        $space->settings[$row->id] = unserialize($row->value);
      }

      // Load customizer & preset
      $row = db_fetch_object(db_query("SELECT customizer, preset FROM {spaces} WHERE sid = %d AND type = '%s'", $space->sid, $space->type));
      $space->customizer = $row->customizer ? unserialize($row->customizer) : array();

      // Enforce preset or use default if not found
      $default_presets = variable_get('spaces_default_presets', array());
      if ($row->preset) {
        $space->preset = $row->preset;
      }
      else {
        if ($space->preset == NULL && isset($default_presets[$type])) {
          $space->preset = $default_presets[$type];
          spaces_preset_enforce($space);
        }
      }
    }
    return $space;
  }
  return false;
}

/**
 * Saves a space object's feature/setting values.
 *
 * @param $space
 *   The space object to save.
 *
 * @return
 *   Returns TRUE on success, FALSE on failure.
 */
function spaces_save($space) {
  if ($space->sid) {

    // Enforce the preset
    spaces_preset_enforce($space);

    // Update features
    db_query("DELETE FROM {spaces_features} WHERE sid = %d AND type = '%s'", $space->sid, $space->type);
    $valid = spaces_features($space->type);
    foreach ($space->features as $feature => $value) {
      if (isset($valid[$feature])) {
        $values = array(
          $space->sid,
          $space->type,
          $feature,
          $value,
        );
        db_query('INSERT INTO {spaces_features} (sid, type, id, value) VALUES (%d, "%s", "%s", "%s")', $values);
      }
    }

    // Update settings
    db_query("DELETE FROM {spaces_settings} WHERE sid = %d AND type = '%s'", $space->sid, $space->type);
    $valid = spaces_settings();
    foreach ($space->settings as $setting => $value) {
      if (isset($valid[$setting])) {
        $value = serialize($value);
        $values = array(
          $space->sid,
          $space->type,
          $setting,
          $value,
        );
        db_query('INSERT INTO {spaces_settings} (sid, type, id, value) VALUES (%d, "%s", "%s", "%s")', $values);
      }
    }

    // Update preset & customizer
    $exists = db_result(db_query("SELECT count(sid) FROM {spaces} WHERE sid = %d AND type = '%s'", $space->sid, $space->type));
    if ($exists) {
      db_query("UPDATE {spaces} SET preset = '%s', customizer = '%s' WHERE sid = %d AND type = '%s'", $space->preset, serialize($space->customizer), $space->sid, $space->type);
    }
    else {
      db_query("INSERT INTO {spaces} (sid, type, preset, customizer) VALUES(%d, '%s', '%s', '%s')", $space->sid, $space->type, $space->preset, serialize($space->customizer));
    }

    // Save context prefix if space type allows prefix customization
    $types = spaces_types();
    $save_prefix = isset($types[$space->type]['custom prefixes']) && $types[$space->type]['custom prefixes'];
    if ($space->prefix && $save_prefix) {

      // We need to concatenate the type/sid so that collisions between
      // space types do not occur.
      $prefix = array(
        'provider' => 'spaces_' . $space->type,
        'id' => $space->sid,
      );
      context_prefix_api('delete', $prefix);
      $prefix['prefix'] = $space->prefix;
      context_prefix_api('insert', $prefix);
    }

    // Allow space type to do its own saving
    $space
      ->save();
    return true;
  }
  return false;
}

/**
 * Deletes a space object's records in the database.
 *
 * @param $space
 *   The space object to delete.
 *
 * @return
 *   Returns TRUE for now.
 */
function spaces_delete($space) {

  // Remove all features and settings
  db_query("DELETE FROM {spaces} WHERE sid = %d AND type = '%s'", $space->sid, $space->type);
  db_query("DELETE FROM {spaces_features} WHERE sid = %d AND type = '%s'", $space->sid, $space->type);
  db_query("DELETE FROM {spaces_settings} WHERE sid = %d AND type = '%s'", $space->sid, $space->type);

  // Clear the prefix path from the context_prefix table
  $prefix = array(
    'provider' => 'spaces_' . $space->type,
    'id' => $space->sid,
  );
  context_prefix_api('delete', $prefix);

  // Allow space type to do its own deleting
  $space
    ->delete();
  return true;
}

/**
 * Enforces a spaces preset.
 */
function spaces_preset_enforce(&$space) {
  $presets = spaces_presets($space->type);
  if (isset($space->preset) && isset($presets[$space->preset])) {
    $preset = $presets[$space->preset]['preset'];

    // Enforce features
    if (is_array($preset['features'])) {
      foreach ($preset['features'] as $feature => $value) {
        if ($preset['locked']['features'][$feature] || !isset($space->features[$feature])) {
          $space->features[$feature] = $value;
        }
      }
    }

    // Enforce settings
    if (is_array($preset['settings'])) {
      foreach ($preset['settings'] as $setting => $value) {
        if ($preset['locked']['settings'][$setting] || !isset($space->features[$setting])) {
          $space->settings[$setting] = $value;
        }
      }
    }

    // Type-specific presets
    $space
      ->preset_enforce($preset);
  }
}

/**
 * Invokes hook_spaces_types() to gather an array of space types and
 * associated classes.
 *
 * Implementing modules should provide an associate array in
 * hook_spaces_types() of the following format:
 *
 * return array(
 *   $id => array(
 *     'class' => $space_class,
 *     'title' => $space_type_name,
 *     'custom prefixes' => $bool,
 *   ),
 * );
 *
 * Where:
 *
 * $id: A unique string identifier for this space type. e.g. "og"
 * $space_class: The PHP class to construct for this space. e.g. "spaces_og"
 * $space_type_name: The human-readable name of your space type. e.g. "Group space"
 * $bool: TRUE or FALSE for whether spaces should provide a UI for
 *   users to provide a custom prefix path for each space. If FALSE,
 *   the implementing module should implement
 *   hook_context_prefix_prefixes() in order to provide prefixes
 *   programmatically.
 *
 * @param $reset
 *   Optional reset flag for clearing the static cache.
 *
 * @return
 *   An array of space types where $key => $value corresponds to the space type => space class.
 */
function spaces_types($reset = false) {
  static $spaces_types;
  if (!isset($spaces_types) || $reset) {
    $spaces_types = module_invoke_all('spaces_types');
  }
  return $spaces_types;
}

/**
 * Gather an array of spaces presets from the DB.
 *
 * @param $type
 *   Optional space type to filter results by.
 * @param $include_disabled
 *   Optional flag to return all presets, including disabled ones.
 * @param $reset
 *   Optional reset flag for clearing the static cache.
 *
 * @return
 *   An array of space types where $key => $value corresponds to the space type => space class.
 */
function spaces_presets($type = NULL, $include_disabled = FALSE, $reset = FALSE) {
  static $presets;
  if (!isset($presets)) {
    $presets = array();
    $result = db_query("SELECT * FROM {spaces_presets}");
    $disabled = variable_get('spaces_disabled_presets', array());
    while ($row = db_fetch_object($result)) {
      $presets[$row->type][$row->id] = array(
        'name' => $row->name,
        'description' => $row->description,
        'preset' => unserialize($row->value),
        'disabled' => isset($disabled[$row->type][$row->id]),
      );
    }

    // Collect presets provided by modules in code
    foreach (module_implements('spaces_presets') as $module) {
      $items = call_user_func($module . '_spaces_presets');
      foreach ($items as $id => $preset) {
        $presets[$preset['type']][$id] = array(
          'name' => $preset['name'],
          'description' => $preset['description'],
          'preset' => $preset['preset'],
          'disabled' => isset($disabled[$preset['type']][$id]),
          'module' => $module,
        );
      }
    }
  }

  // Move filtering outside main condition in order to hit the DB only once
  $return = $presets;
  if (!$include_disabled) {
    foreach (array_keys($return) as $preset_type) {
      foreach ($return[$preset_type] as $id => $preset) {
        if ($preset['disabled']) {
          unset($return[$preset_type][$id]);
        }
      }
    }
  }
  if ($type) {
    return isset($return[$type]) ? $return[$type] : array();
  }
  return $return;
}

/**
 * Wrapper function around spaces_set_space(). Retrieves the current
 * active space.
 */
function spaces_get_space() {
  return spaces_set_space();
}

/**
 * Sets the specified space as the current active space. Returns the
 * active space if no space is provided.
 *
 * @param $space
 *   The space object to set as the active space. Optional.
 * @param $reset
 *   Optional flag to reset the static cache.
 *
 * @return
 *   The active space object or FALSE if there is no active space.
 */
function spaces_set_space($space = NULL, $reset = FALSE) {
  static $current_space;
  if (!isset($current_space) || $reset) {
    $current_space = $space;
  }
  return $current_space ? $current_space : FALSE;
}

/**
 * Wrapper around implementations of $space->router. Provides
 * additional intelligence, including a global killswitch and routing
 * when no spaces are active.
 *
 * @param $op
 *   The current "hook" or "hook op" identifier for the $space->router
 *   to act on.
 * @param $object
 *   Optional object to pass to the $space->router.
 */
function spaces_router($op, $object = NULL) {

  // Check global killswitch
  if (spaces_router_get()) {
    $access = true;
    $types = spaces_types();

    // Run the router for the active space
    if ($space = spaces_get_space()) {
      $access = $access && $space
        ->router($op, $object);
      unset($types[$space->type]);
    }

    // Run each non-active space type's router
    foreach ($types as $type => $info) {
      $access = $access && call_user_func(array(
        $info['class'],
        'router',
      ), $op, $object, FALSE);
    }
    if (!$access && !user_access('administer spaces')) {
      drupal_access_denied();
      exit;
    }
  }
}

/**
 * Wrapper around spaces_router_get().
 */
function spaces_router_set($status) {
  return spaces_router_get($status, TRUE);
}

/**
 * Sets a static variable that is used to disable spaces routing
 * altogether -- e.g. for install/update scripts, migrations, etc.
 *
 * @param $enabled
 *   Optional boolean for enabling/disabling spaces routing.
 * @param $reset
 *   Optional boolean for resetting the static cache.
 *
 * @return
 *   Returns a boolean for whether routing is enabled/disabled.
 */
function spaces_router_get($enabled = 1, $reset = FALSE) {
  static $status;
  if (!isset($status) || $reset) {
    $status = $enabled;
  }
  return $status;
}

/**
 * Retrieve all available features.
 *
 * @param $type
 *   Optional type flag by which to filter available features.
 * @param $reset
 *   Optional boolean flag for resetting the static cache.
 *
 * @return
 *   Keyed array of potential features.
 */
function spaces_features($type = NULL, $reset = FALSE) {
  static $spaces_features;
  if (!isset($spaces_features) || $reset) {
    $spaces_features = array();
    foreach (context_ui_defaults('spaces') as $feature) {
      if ($feature->spaces) {

        // If type is specified, perform additional checks
        if ($type) {
          if (isset($feature->spaces['types']) && in_array($type, $feature->spaces['types'])) {
            $spaces_features[$feature->value] = $feature;
          }
          else {
            if (!isset($feature->spaces['types']) || !$feature->spaces['types']) {
              $spaces_features[$feature->value] = $feature;
            }
          }
        }
        else {
          $spaces_features[$feature->value] = $feature;
        }
      }
    }
  }
  return $spaces_features;
}

/**
 * Retrieve all available settings.
 *
 * @param $reset
 *   Optional boolean flag for resetting the static cache.
 *
 * @return
 *   Keyed array of potential settings.
 */
function spaces_settings($reset = FALSE) {
  static $settings;
  if (!isset($settings) || $reset) {
    $settings = array();
    foreach (module_implements('spaces_settings') as $module) {
      $function = $module . '_spaces_settings';
      $settings = array_merge($settings, $function());
    }
  }
  return $settings;
}

/**
 * Retrieve all available customizers.
 *
 * @param $reset
 *   Optional boolean flag for resetting the static cache.
 *
 * @return
 *   Keyed array of customizers.
 */
function spaces_customizers($reset = FALSE) {
  static $customizers;
  if (!isset($customizers) || $reset) {
    $customizers = array();
    $customizers = module_invoke_all('spaces_customizers');
  }
  return $customizers;
}

/**
 * Returns a content type => features map.
 *
 * @param $reset
 *   Optional boolean flag for resetting the static cache.
 * 
 * @return
 *   Keyed array where $nodetype => $feature.
 */
function spaces_content_types($reset = FALSE) {
  static $map;
  if (!isset($map) || $reset) {
    $map = array();
    $features = spaces_features();
    foreach ($features as $id => $feature) {
      if (is_array($feature->node)) {
        foreach ($feature->node as $type) {
          $map[$type] = $id;
        }
      }
    }
  }
  return $map;
}

/**
 * Returns a links array in the theme_links() format of the current
 * space's menu items for features accessible to the current user. Each
 * item has a keyed array of children items if applicable.
 *
 * @return
 *   Array of links.
 */
function spaces_features_menu() {
  static $menu;
  if (!isset($menu)) {
    $menu = array();
    $space = spaces_get_space();
    if ($space) {
      $features = spaces_features($space->type);
      $active_feature = context_get('spaces', 'feature');
      $depth = 1;

      // Loop once through features to build menu set
      foreach ($space->features as $feature => $value) {
        if ($value != SPACES_FEATURE_DISABLED && $space
          ->feature_access($feature) && isset($features[$feature]->spaces['menu'])) {

          // Customize the menu
          $feature_menu = $features[$feature]->spaces['menu'];
          $feature_menu = space_customizer_menu::customize($space, $feature, $feature_menu);

          // Collect menu items
          foreach ($feature_menu as $path => $item) {
            $args = explode('/', $path);
            $item['href'] = $path;
            $item['children'] = array();
            $item['depth'] = count($args);
            $item['args'] = $args;
            if ($item['depth'] == 1 && $feature == $active_feature) {
              $item['attributes'] = array(
                'class' => 'active',
              );
            }
            else {
              if ($item['depth'] > $depth) {
                $depth = $item['depth'];
              }
            }
            $menu[$path] = $item;
          }
        }
      }

      // Second loop to tree the menu
      while ($depth >= 1) {
        foreach ($menu as $path => $item) {
          if ($item['depth'] == $depth) {
            $args = implode('/', array_slice($item['args'], 0, $depth - 1));
            if ($depth > 1) {
              if (isset($menu[$args])) {
                unset($item['depth']);
                unset($item['args']);
                $menu[$args]['children'][$path] = $item;
              }
              unset($menu[$path]);
            }
            else {
              unset($menu[$path]['args']);
              unset($menu[$path]['depth']);
            }
          }
        }
        $depth--;
      }
    }
  }
  return $menu;
}

/**
 * Preset options form that can be reused by implementing modules.
 *
 * @param $space
 *   A space object.
 *
 * @return
 *   A FormAPI array structure.
 */
function spaces_form_presets($space) {
  $default_presets = variable_get('spaces_default_presets', array());
  if (isset($space->preset)) {
    $default_preset = $space->preset;
  }
  else {
    if (isset($default_presets[$space->type])) {
      $default_preset = $default_presets[$space->type];
    }
    else {
      $default_preset = NULL;
    }
  }

  // Radios for presets
  $form = array(
    '#tree' => false,
    '#theme' => 'spaces_form_presets',
  );
  $form['preset'] = array(
    '#title' => t('Preset'),
    '#type' => 'radios',
    '#required' => true,
    '#options' => array(),
    '#default_value' => $default_preset,
  );
  $form['info'] = array();
  foreach (spaces_presets($space->type) as $id => $preset) {
    $form['preset']['#options'][$id] = $preset['name'];
    $form['info'][$id] = array(
      '#type' => 'item',
      '#title' => $preset['name'],
      '#description' => $preset['description'],
    );
  }
  return $form;
}

/**
 * A mild abstraction of hook_menu() items that can be used by
 * implementing modules to embed/graft relevant spaces items into the
 * menu tree. Should only be used when the $may_cache argument of
 * hook_menu() is false.
 *
 * @param $space
 *   A space object.
 * @param $local_tasks
 *   Optional boolean flag for whether the items are fully rendered as
 *   local tasks.
 * @param $path_prefix
 *   A path to prefix the menu item paths by.
 *
 * @return
 *   An array of menu items.
 */
function spaces_active_space_menu($space, $local_tasks = FALSE, $path_prefix = '') {
  include_once drupal_get_path('module', 'spaces') . '/spaces_admin.inc';
  $types = spaces_types();
  $path_prefix = !empty($path_prefix) ? $path_prefix . '/' : '';
  if ($local_tasks == FALSE) {
    $items['spaces'] = array(
      'path' => $path_prefix . 'spaces',
      'title' => t('!space_type settings', array(
        '!space_type' => $types[$space->type]['title'],
      )),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_basic_form',
        $space,
      ),
      'access' => $space
        ->admin_access(),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  else {
    $items['spaces'] = array(
      'path' => $path_prefix . 'spaces',
      'title' => t('Spaces'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'spaces_basic_form',
        $space,
      ),
      'access' => $space
        ->admin_access(),
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
    );
  }
  $items['spaces/setup'] = array(
    'path' => $path_prefix . 'spaces/setup',
    'title' => t('Basic setup'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'spaces_basic_form',
      $space,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['spaces/features'] = array(
    'path' => $path_prefix . 'spaces/features',
    'title' => t('Features'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'spaces_features_form',
      $space,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  $items['spaces/customize'] = array(
    'path' => $path_prefix . 'spaces/customize',
    'title' => t('Customize'),
    'callback' => 'spaces_customize',
    'callback arguments' => array(
      $space,
    ),
    'access' => $space
      ->admin_access(),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
  );
  return $items;
}

/**
 * THEME FUNCTIONS ====================================================
 */

/**
 * Generates an array of utility links for the current space suitable
 * for use in theme_links().
 */
function spaces_space_links() {
  $links = array();
  if ($space = spaces_get_space()) {
    $links = array();
    $space
      ->links($links);
  }
  return $links;
}

/**
 * Generates a set of links for node types associated with the current
 * active contexts.
 */
function spaces_node_links() {
  $output = '';
  $links = _context_ui_node_links();

  // Perform additional logic if a spaces feature is active.
  if ($feature = context_get('spaces', 'feature')) {
    $space = spaces_get_space();

    // Feature is inaccessible
    if ($space && !$space
      ->feature_access($feature)) {
      $features = spaces_features($space->type);
      if (isset($features[$feature]->node)) {
        foreach ($features[$feature]->node as $type) {
          unset($links[$type]);
        }
      }
    }
  }
  foreach (module_implements('spaces_node_links_alter') as $module) {

    // It would be best to use_call_user_func(), but this makes most
    // sense passed by reference...
    $function = $module . '_spaces_node_links_alter';
    $function($links);
  }
  return theme('spaces_node_links', $links);
}

/**
 * Theme function for spaces_node_links
 */
function theme_spaces_node_links($links) {
  $output = '';
  foreach ($links as $link) {
    if ($link['custom']) {
      $output .= l($link['title'], $link['href'], array(
        'class' => 'button',
      ), isset($link['query']) ? $link['query'] : NULL);
    }
    else {
      if (!empty($link)) {
        $output .= l('+ ' . t('Add !type', array(
          '!type' => $link['title'],
        )), $link['href'], array(
          'class' => 'button',
        ), isset($link['query']) ? $link['query'] : NULL);
      }
    }
  }
  return $output;
}

/**
 * Form theme function for spaces presets.
 */
function theme_spaces_form_presets($form) {
  $output = '';

  // Render presets in a table
  $rows = array();
  foreach (element_children($form['info']) as $id) {
    unset($form['preset'][$id]['#title']);
    $row = array(
      drupal_render($form['preset'][$id]),
      drupal_render($form['info'][$id]),
    );
    $rows[] = $row;
  }
  $output .= theme('table', array(
    array(
      'data' => $form['preset']['#title'],
      'colspan' => 2,
    ),
  ), $rows);
  drupal_render($form['preset']);

  // Throw out the rest of this element
  $output .= drupal_render($form);
  return $output;
}

/**
 * BLOCKS =============================================================
 */

/**
 * Contextual tools
 */
function _spaces_block_tools() {
  $block['content'] = spaces_node_links();
  return $block;
}

/**
 * Default navigation menu - you can create your own customized version
 * using the spaces_features_menu() and theme_links() functions.
 */
function _spaces_block_nav() {
  $block = array();
  if ($space = spaces_get_space()) {
    $links = spaces_features_menu();
    $block['subject'] = $space->title;
    $block['content'] = theme('links', $links);
  }
  return $block;
}

/**
 * Utility links
 */
function _spaces_block_utility_links() {
  $block = array();
  if ($space = spaces_get_space()) {
  }
  return $block;
}

Functions

Namesort descending Description
spaces_active_space_menu A mild abstraction of hook_menu() items that can be used by implementing modules to embed/graft relevant spaces items into the menu tree. Should only be used when the $may_cache argument of hook_menu() is false.
spaces_block Implementation of hook_block()
spaces_content_types Returns a content type => features map.
spaces_context_define Implementation of hook_context_define()
spaces_context_prefix_provider Implementation of hook_context_prefix_provider().
spaces_customizers Retrieve all available customizers.
spaces_delete Deletes a space object's records in the database.
spaces_features Retrieve all available features.
spaces_features_menu Returns a links array in the theme_links() format of the current space's menu items for features accessible to the current user. Each item has a keyed array of children items if applicable.
spaces_form_alter Implementation of hook_form_alter().
spaces_form_presets Preset options form that can be reused by implementing modules.
spaces_get_space Wrapper function around spaces_set_space(). Retrieves the current active space.
spaces_init Implementation of hook_init().
spaces_init_context Context prefix provider callback.
spaces_load Load a space.
spaces_menu Implementation of hook_menu().
spaces_nodeapi Implementation of hook_nodeapi().
spaces_node_links Generates a set of links for node types associated with the current active contexts.
spaces_perm Implementation of hook_perm().
spaces_presets Gather an array of spaces presets from the DB.
spaces_preset_enforce Enforces a spaces preset.
spaces_router Wrapper around implementations of $space->router. Provides additional intelligence, including a global killswitch and routing when no spaces are active.
spaces_router_get Sets a static variable that is used to disable spaces routing altogether -- e.g. for install/update scripts, migrations, etc.
spaces_router_set Wrapper around spaces_router_get().
spaces_save Saves a space object's feature/setting values.
spaces_settings Retrieve all available settings.
spaces_set_space Sets the specified space as the current active space. Returns the active space if no space is provided.
spaces_spaces_customizers Implementation of hook_spaces_customizers().
spaces_spaces_settings Implementation of hook_spaces_settings().
spaces_space_links Generates an array of utility links for the current space suitable for use in theme_links().
spaces_types Invokes hook_spaces_types() to gather an array of space types and associated classes.
spaces_user Implementation of hook_user().
spaces_views_pre_query Implementation of hook_views_pre_query().
theme_spaces_form_presets Form theme function for spaces presets.
theme_spaces_node_links Theme function for spaces_node_links
_spaces_block_nav Default navigation menu - you can create your own customized version using the spaces_features_menu() and theme_links() functions.
_spaces_block_tools Contextual tools
_spaces_block_utility_links Utility links

Constants

Classes

Namesort descending Description
space_customizer_menu Customizer for feature menus.
space_customizer_views Customizer for views titles and headers.
space_setting_home Provides a homepage setting for each space.

Interfaces

Namesort descending Description
space Interface for space objects.
space_customizer Interface for space customizers.
space_setting Interface for space settings.