You are here

spaces_user.module in Spaces 5.2

File

spaces_user.module
View source
<?php

define('SPACES_USER_ENABLED', 1);

/**
 * Spaces OG must be included after the Spaces module. We check this
 * condition here -- if the check fails, at least we don't break Drupal.
 */
if (function_exists('spaces_menu')) {
  class space_user implements space {
    var $account = NULL;
    var $title = NULL;

    /**
     * Constructor
     */
    function __construct($type, $sid = NULL, $is_active = FALSE) {
      if ($sid) {
        $this->account = user_load(array(
          'uid' => $sid,
        ));
        $this->title = $this->account->name;
        $this->prefix = 'space-' . spaces_user_make_prefix($this->account->name);
      }
      else {
        $this->account = new StdClass();
      }
    }

    /**
     * Implementation of space->save().
     */
    function save() {
      if (!$this->save_once) {
        user_save($this->account);
      }
      return;
    }

    /**
     * Implementation of space->delete().
     */
    function delete() {

      // We do not delete the user here:
      // 1. to allow the user to remain and perhaps later be re-registered as a user space
      // 2. to avoid recursion
      return;
    }

    /**
     * Implementation of space->feature_access().
     */
    function feature_access($feature = NULL) {
      return true;
      if (isset($this->features[$feature])) {
        $features = spaces_features();
        if ($this->feature[$feature] == SPACES_USER_ENABLED) {
          return true;
        }
      }
      return false;
    }

    /**
     * Implementation of space->admin_access().
     */
    function admin_access() {
      global $user;
      if ($this->account->uid == $user->uid) {
        return true;
      }
      else {
        if (user_access('administer users')) {
          return true;
        }
      }
      return false;
    }

    /**
     * Implementation of space->feature_options().
     */
    function feature_options() {
      return array(
        SPACES_FEATURE_DISABLED => t('Disabled'),
        SPACES_USER_ENABLED => t('Enabled'),
      );
    }

    /**
     * Implementation of space->links().
     */
    function links(&$links) {
      if ($this
        ->admin_access()) {

        // Add settings link for administering spaces
        $links['settings'] = array(
          'title' => t('Account settings'),
          'href' => 'user/' . $this->sid . '/edit',
          'attributes' => array(
            'class' => 'settings',
          ),
        );
      }
    }

    /**
     * Implementation of space->form().
     */
    function form() {
      return;
    }

    /**
     * Implementation of space->preset_validate().
     */
    function validate($values) {
      return;
    }

    /**
     * Implementation of space->preset_submit().
     */
    function submit($values) {

      // Only process group form options on preset form
      if (!$this->sid) {
      }
      return array();
    }

    /**
     * Implementation of space->preset_enforce().
     */
    function preset_enforce($preset) {
    }

    /**
     * Implementation of space->redirect().
     */
    function redirect($op = 'home') {
      switch ($op) {
        case 'home':

          // use the menu path of the selected feature as homepage
          if ($home = $this->settings['home']) {
            $features = spaces_features();
            if (is_array($features[$home]->spaces['menu'])) {
              $home_path = key($features[$home]->spaces['menu']);
              context_prefix_goto('spaces_og', $this->sid, $home_path);
            }
          }
          else {
            global $user;
            if ($user == $this->sid) {
              context_prefix_goto('spaces_og', $this->sid, 'user/' . $this->sid . '/edit');
            }
            else {
              context_prefix_goto('spaces_og', $this->sid, 'user/' . $this->sid);
            }
          }
          break;
      }
    }

    /**
     * Implementation of space->router().
     */
    function router($op, $object = NULL, $is_active = TRUE) {
      switch ($op) {
        case 'menu':

          // User space is active
          if ($is_active) {
            global $user;
            return drupal_is_front_page() ? $this
              ->redirect('home') : true;
          }

          // User space is inactive
          return true;
        case 'node view':
          $node = $object;
          if ($is_active && $node->uid != $this->sid) {

            // @TODO: context_prefix_goto() needs to support the
            // dropping of contexts!
            clswitch('set', TRUE);
            drupal_goto('node/' . $node->nid);
          }
          return true;
        case 'node form':
          $node = $object;
          if ($is_active && $node->uid != $this->sid) {

            // @TODO: context_prefix_goto() needs to support the
            // dropping of contexts!
            clswitch('set', TRUE);
            drupal_goto($_GET['q']);
          }
          return true;
        case 'user view':
          global $user;
          $account = $object;

          // Always send user visitors to the user's space if trying
          // to view the profile.
          // !!! THIS IS VERY GREEDY -- MAY NEED RECONSIDERATION!
          if (!$is_active) {

            // @TODO: context_prefix_goto() needs to support the
            // dropping of contexts!
            clswitch('set', TRUE);
            drupal_goto('space-' . spaces_user_make_prefix($account->name));
          }
          else {
            if ($account->uid != $this->sid) {

              // @TODO: context_prefix_goto() needs to support the
              // dropping of contexts!
              clswitch('set', TRUE);
              drupal_goto($_GET['q']);
            }
          }
          return true;
      }
    }

    // Implementation of views_filter().
    function views_filter($is_active, &$query) {
      if ($is_active) {
        $query
          ->ensure_table('users');
        $query
          ->add_where("users.uid = '%s'", $this->sid);
      }
    }

  }
}

/**
 * Implementation of hook_menu().
 */
function spaces_user_menu($may_cache) {
  $items = array();
  if (!$may_cache) {

    // Graft spaces local tasks onto user
    if (arg(0) == 'user' && is_numeric(arg(1)) && ($account = user_load(array(
      'uid' => arg(1),
    )))) {
      $space = spaces_get_space();
      if ($space->type == 'user') {
        $spaces_items = spaces_active_space_menu($space, true, 'user/' . arg(1));
        unset($spaces_items['spaces/settings']);
        $items = $items + $spaces_items;
      }
    }
  }
  return $items;
}

/**
 * Implementation of hook_spaces_types().
 */
function spaces_user_spaces_types() {
  return array(
    'user' => array(
      'class' => 'space_user',
      'title' => t('User space'),
      'custom prefixes' => FALSE,
    ),
  );
}

/**
 * Implementation of hook_context_prefix_prefixes().
 */
function spaces_user_context_prefix_prefixes($reset = FALSE) {
  static $prefixes;
  if (!isset($prefixes) || $reset) {
    $prefixes = array();
    $result = db_query("SELECT uid, name FROM {users} WHERE status = 1");
    while ($row = db_fetch_object($result)) {
      $prefixes[] = array(
        'prefix' => 'space-' . spaces_user_make_prefix($row->name),
        'id' => $row->uid,
      );
    }
  }
  return array(
    'spaces_user' => $prefixes,
  );
}

/**
 * Implementation of hook_user().
 */
function spaces_user_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'form':
      if ($category == 'account') {
        $space = spaces_load('user', $account->uid);
        $form = array(
          'spaces_preset' => spaces_form_presets($space),
        );
        return $form;
      }
      break;
    case 'insert':
    case 'update':
      if (isset($edit['preset'])) {
        $space = spaces_load('user', $account->uid);
        $space->preset = $edit['preset'];
        spaces_save($space);
      }
      break;
  }
}

/**
 * Helper function to make usernames more suitable for path prefixing.
 */
function spaces_user_make_prefix($username) {
  return check_url(strtolower($username));
}

Functions

Namesort descending Description
spaces_user_context_prefix_prefixes Implementation of hook_context_prefix_prefixes().
spaces_user_make_prefix Helper function to make usernames more suitable for path prefixing.
spaces_user_menu Implementation of hook_menu().
spaces_user_spaces_types Implementation of hook_spaces_types().
spaces_user_user Implementation of hook_user().

Constants