You are here

spaces_user.module in Spaces 6

File

spaces_user/spaces_user.module
View source
<?php

define('SPACES_USER_ENABLED', 1);

/**
 * Spaces user 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->purl = 'space-' . spaces_user_make_purl($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) {
      if (user_access('access content') && isset($this->features[$feature]) && $this->features[$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'])) {
              reset($features[$home]->spaces['menu']);
              $item = current($features[$home]->spaces['menu']);
              $home_path = $item['href'];
              purl_goto($home_path, array(
                'purl' => array(
                  'provider' => 'spaces_user',
                  'id' => $this->sid,
                ),
              ));
            }
          }
          else {
            global $user;
            if ($user == $this->sid) {
              purl_goto("user/{$this->sid}/edit", array(
                'purl' => array(
                  'provider' => 'spaces_user',
                  'id' => $this->sid,
                ),
              ));
            }
            else {
              purl_goto("user/{$this->sid}", array(
                'purl' => array(
                  'provider' => 'spaces_user',
                  'id' => $this->sid,
                ),
              ));
            }
          }
          break;
        case 'features':
          purl_goto("user/{$this->sid}/spaces/features", array(
            'purl' => array(
              'provider' => 'spaces_user',
              'id' => $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) {
            purl_goto("node/{$node->nid}", array(
              'purl' => array(
                'disabled' => TRUE,
              ),
            ));
          }
          return true;
        case 'node form':
          $node = $object;
          if ($is_active && $node->uid != $this->sid) {
            purl_goto($_GET['q'], array(
              'purl' => array(
                'disabled' => TRUE,
              ),
            ));
          }
          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) {
            purl_goto('space-' . spaces_user_make_purl($account->name), array(
              'purl' => array(
                'disabled' => TRUE,
              ),
            ));
          }
          else {
            if ($account->uid != $this->sid) {
              purl_goto($_GET['q'], array(
                'purl' => array(
                  'disabled' => TRUE,
                ),
              ));
            }
          }
          return true;
        case 'user form':
          return true;
      }
    }

    // Implementation of views_filter().
    function views_filter($is_active, &$query) {
      if ($is_active) {
        if ($query->base_table == 'node') {
          $table = $query
            ->ensure_table('users');
        }
        else {
          if (!empty($query->relationships)) {
            foreach ($query->relationships as $relationship => $info) {
              if ($info['table'] == 'node') {
                $table = $query
                  ->ensure_table('users', $relationship);
                break;
              }
            }
          }
        }
        if ($table) {

          // @TODO: fix this first parameter to support grouping
          $query
            ->add_where(0, "{$table}.uid  = '%s'", $this->sid);
        }
      }
    }

  }
}

/**
 * Implementation of hook_menu().
 */
function spaces_user_menu() {
  $items = array();
  $spaces_items = spaces_active_space_menu('user', true, 'user/%user');
  $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,
      'base path' => 'user/%sid',
    ),
  );
}

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

/**
 * 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_purl($username) {
  return check_url(str_replace(' ', '-', strtolower($username)));
}

/**
 * Implementation of hook_context_links_alter().
 */
function spaces_user_context_links_alter(&$links) {

  // Disallow users from submitting ANY content in other people's spaces : )
  $space = spaces_get_space();
  if ($space && $space->type == 'user') {
    global $user;
    if ($user->uid != $space->sid) {
      $links = array();
    }
  }
}

Functions

Namesort descending Description
spaces_user_context_links_alter Implementation of hook_context_links_alter().
spaces_user_make_purl Helper function to make usernames more suitable for path prefixing.
spaces_user_menu Implementation of hook_menu().
spaces_user_purl_modifiers Implementation of hook_purl_modifiers().
spaces_user_spaces_types Implementation of hook_spaces_types().
spaces_user_user Implementation of hook_user().

Constants