You are here

spaces_og.module in Spaces 6

File

spaces_og/spaces_og.module
View source
<?php

define('SPACES_OG_PRIVATE', 1);
define('SPACES_OG_PUBLIC', 2);

/**
 * 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_og implements space {
    var $group = NULL;
    var $title = NULL;

    /**
     * Constructor
     */
    function __construct($type, $sid = NULL, $is_active = FALSE) {
      if ($sid) {
        $this->group = node_load($sid);
        $this->title = $this->group->title;
        if ($is_active) {

          // Set the group context on behalf of OG
          og_set_group_context($this->group);

          // Handle theme switching for OG
          if ($this->group->og_theme) {
            global $custom_theme;
            $custom_theme = $group->og_theme;
          }
        }
      }
      else {
        $this->group = new StdClass();
      }
    }

    /**
     * Implementation of space->save().
     */
    function save() {
      og_update_group($this->group);
      return;
    }

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

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

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

    /**
     * Implementation of space->admin_access().
     */
    function admin_access() {
      global $user;
      if ($this->group && og_is_group_admin($this->group)) {
        return true;
      }
      else {
        if (user_access('administer spaces') || user_access('administer organic groups')) {
          return true;
        }
        else {
          if ($this->group->uid == $user->uid) {
            return true;
          }
        }
      }
      return false;
    }

    /**
     * Implementation of space->feature_options().
     */
    function feature_options() {
      return array(
        SPACES_FEATURE_DISABLED => t('Disabled'),
        SPACES_OG_PRIVATE => t('Private'),
        SPACES_OG_PUBLIC => t('Public'),
      );
    }

    /**
     * Implementation of space->links().
     */
    function links(&$links) {
      $links['subscribe'] = spaces_og_subscription_link();
      if ($this
        ->admin_access()) {
        $links['members'] = array(
          'title' => t('Members'),
          'href' => 'og/users/' . $this->sid,
          'attributes' => array(
            'class' => 'members',
          ),
        );

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

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

      // Only show group form options on preset form
      if (empty($this->sid)) {
        $old_gid = NULL;
        if (isset($this->group->nid)) {

          // Stupid OG will only set values when the node object has a nid set
          $old_gid = $this->group->nid;
          $this->group->nid = -1;
        }

        // Generate OG settings form
        $form = og_group_form($this->group, array());

        // @TODO figure out if it makes sense to pass $form_state to $space->form().
        if (module_exists('og_access')) {
          drupal_add_js(drupal_get_path('module', 'og_access') . '/og_access.js');
          og_access_alter_group_form($form, $this->group);
        }
        $this->group->nid = $old_gid;

        // Omit description & theme selection
        unset($form['og_description']);
        unset($form['themes']);

        // Pack the form into a fieldset
        $form['#title'] = t('Group settings');
        $form['#type'] = 'fieldset';
        return $form;
      }
    }

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

      // No need to validate
      return;
    }

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

      // Only process group form options on preset form
      if (!$this->sid) {
        $preset = array();
        $preset['og_selective'] = $values['og_selective'];
        $preset['og_register'] = $values['og_register'];
        $preset['og_directory'] = $values['og_directory'];
        $preset['og_private'] = $values['og_private'];
        return $preset;
      }
      return array();
    }

    /**
     * Implementation of space->preset_enforce().
     */
    function preset_enforce($preset) {
      $this->group->og_selective = isset($preset['og']['og_selective']) ? $preset['og']['og_selective'] : 0;
      $this->group->og_register = isset($preset['og']['og_register']) ? $preset['og']['og_register'] : 0;
      $this->group->og_directory = isset($preset['og']['og_directory']) ? $preset['og']['og_directory'] : 0;
      $this->group->og_private = isset($preset['og']['og_private']) ? $preset['og']['og_private'] : 0;
    }

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

            // 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_og',
                    'id' => $this->sid,
                  ),
                ));
              }
            }
            else {
              if ($this
                ->admin_access() && user_access('configure spaces features')) {
                drupal_set_message(t("Please setup your group by enabling at least 1 feature and choosing a homepage setting."));
                purl_goto("node/{$this->sid}/spaces/features", array(
                  'purl' => array(
                    'provider' => 'spaces_og',
                    'id' => $this->sid,
                  ),
                ));
              }
            }
          }
          else {
            drupal_goto('node/' . $this->sid . '/edit');
          }
          drupal_not_found();
          exit;
          break;
        case 'features':
          purl_goto("node/{$this->sid}/spaces/features", array(
            'purl' => array(
              'provider' => 'spaces_og',
              'id' => $this->sid,
            ),
          ));
          break;
      }
    }

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

          // Group space is active
          if ($is_active) {
            global $user;

            // Allow access
            // Group is public
            if ($this->group->og_private != 1) {
              return drupal_is_front_page() ? $this
                ->redirect('home') : true;
            }
            else {
              if (spaces_og_is_member($this->sid)) {
                return drupal_is_front_page() ? $this
                  ->redirect('home') : true;
              }
              else {
                if (!$user->uid) {

                  // Give anon users access to login pages.
                  if (arg(0) == 'user' || arg(0) == 'openid') {
                    return true;
                  }
                  drupal_goto('user/login');
                }
              }
            }

            // Deny all other access
            return false;
          }
          else {
            return true;
          }
        case 'node view':
          $node = $object;

          // Omitted node
          if (og_is_omitted_type($node->type)) {
            return true;
          }
          else {
            if (og_is_group_type($node->type)) {
              $space = spaces_load('og', $node->nid);
              $space
                ->redirect('home');
            }
            else {
              if (is_array($node->og_groups) && count($node->og_groups)) {
                reset($node->og_groups);
                $gid = current($node->og_groups);
                $node_types = spaces_content_types();

                // If node type is in the feature list, check for private/public settings.
                if (isset($node_types[$node->type])) {
                  if ($is_active) {

                    // Space and node groups match, allow access
                    if (in_array($this->sid, $node->og_groups)) {
                      return true;
                    }
                    else {
                      purl_goto($_GET['q'], array(
                        'purl' => array(
                          'provider' => 'spaces_og',
                          'id' => $gid,
                        ),
                      ));
                    }
                  }
                  else {

                    // Only allow group enabled nodes to be shown out of
                    // their group context if another space type is active
                    $space = spaces_get_space();
                    if ($space && !in_array($space->type, array(
                      'og',
                      'site',
                    ))) {
                      return true;
                    }
                    else {
                      purl_goto($_GET['q'], array(
                        'purl' => array(
                          'provider' => 'spaces_og',
                          'id' => $gid,
                        ),
                      ));
                    }
                  }
                }
              }
              elseif (user_access('administer nodes')) {
                drupal_set_message(t('This content is not assigned to a group and it not visible to non-administrative users.'));
                return true;
              }
            }
          }
          return false;
        case 'node form':
          $node = $object;

          // Omitted node
          if (og_is_omitted_type($node->type)) {
            return true;
          }
          else {
            if (og_is_group_type($node->type)) {

              // Viewing the current space group node
              if ($is_active && $node->nid == $this->sid) {
                return true;
              }

              // Otherwise, route accordingly
              $space = spaces_load('og', $node->nid, true);
              if ($space->purl) {
                purl_goto($_GET['q'], array(
                  'purl' => array(
                    'provider' => 'spaces_og',
                    'id' => $space->sid,
                  ),
                ));
              }
              else {

                // @TODO: replace this with a purl_goto() that drops all modifiers
                return true;
              }
            }
          }

          // OG-enabled nodes
          if ($is_active) {
            $node_types = spaces_content_types();
            $feature = $node_types[$node->type];
            if ($this
              ->feature_access($feature)) {
              $test_user = spaces_og_is_member($this->sid);
              $test_node = $node->nid ? in_array($this->sid, $node->og_groups) : true;

              // User and node are both members of current space
              if ($test_user && $test_node) {
                return true;
              }
              else {
                if (!$test_node) {
                  reset($node->og_groups);
                  $gid = current($node->og_groups);
                  if (spaces_og_is_member($gid) && ($space = spaces_load('og', $gid))) {
                    $options = array();
                    purl_goto($_GET['q'], array(
                      'purl' => array(
                        'provider' => 'spaces_og',
                        'id' => $space->sid,
                      ),
                    ));
                  }
                }
              }
            }
          }

          // elseif (user_access('administer nodes')) {
          //  drupal_set_message(t('This form should only be submitted within a properly configured group. Continue at your own risk.'));
          //  return true;
          // }
          return true;
        case 'user view':
          global $user;
          $account = $object;
          if ($is_active) {
            if (user_access('view users outside groups')) {
              return true;
            }
            $test_user = spaces_og_is_member($this->sid);
            $test_account = isset($account->og_groups[$this->sid]);

            // Both user and account belong to current space
            if ($test_user && $test_account) {
              return true;
            }
            else {
              if (!$test_account) {
                reset($test_account->og_groups);
                $gid = key($test_account->og_groups);
                if (spaces_og_is_member($gid) && ($space = spaces_load('og', $gid))) {
                  purl_goto($_GET['q'], array(
                    'purl' => array(
                      'provider' => 'spaces_og',
                      'id' => $space->sid,
                    ),
                  ));
                }
              }
            }

            // All other cases, deny
            return false;
          }
          else {
            $space = spaces_get_space();

            // If another space type is active, defer
            if ($space) {
              return true;
            }
            else {
              if ($account->uid == $user->uid) {
                return true;
              }
            }

            // All other cases, deny
            return false;
          }
        case 'user form':
          return true;
      }
    }

    // Spaces OG views filter
    function views_filter($is_active, &$query) {
      if ($query->base_table == 'node') {
        $table = $query
          ->ensure_table('og_ancestry');
      }
      else {
        if (!empty($query->relationships)) {
          foreach ($query->relationships as $relationship => $info) {
            if ($info['table'] == 'node') {
              $table = $query
                ->ensure_table('og_ancestry', $relationship);
              break;
            }
          }
        }
      }
      if ($table) {

        // @TODO: fix this first parameter to support grouping
        $query
          ->add_where(0, "{$table}.group_nid  = ***CURRENT_GID***");
      }
    }

  }
}

/**
 * Implementation of hook_init();
 * We're gonna bulldoze your OG settings. Get over it.
 */
function spaces_og_init() {
  global $conf;
  $conf['og_visibility_directory'] = 2;

  // Default to 'visible'.
  $conf['og_visibility_registration'] = 3;

  // Default to 'visible' on form.
  $conf['og_private_groups'] = 3;

  // Default group to 'public'.
  $conf['og_notification'] = 0;

  // Disable OG's Notifications.
  $conf['og_audience_checkboxes'] = 0;

  // Disable audience checkboxes.
  $conf['og_visibility'] = 2;

  // Default visibility to 'public'.
  $conf['og_audience_required'] = 1;

  // Require audience.
  $conf['og_member_pics'] = 0;

  // Disable pictures.
  // Force feature content types to be OG enabled.
  // Don't like this behavior? Turn off feature modules.
  foreach (spaces_features('og') as $feature) {
    if (!empty($feature->node)) {
      foreach ($feature->node as $type) {
        $conf['og_content_type_usage_' . $type] = 'group_post_standard';
      }
    }
  }
}

/**
 * Implementation of hook_spaces_types().
 */
function spaces_og_spaces_types() {
  return array(
    'og' => array(
      'class' => 'space_og',
      'title' => t('Group space'),
      'custom purl' => TRUE,
      'base path' => 'node/%sid',
    ),
  );
}

/**
 * Implementation of hook_spaces_presets().
 */
function spaces_og_spaces_presets() {
  $items = array();
  $items['private'] = array(
    'type' => 'og',
    'name' => t('Private group'),
    'description' => t('Only members will be able to access this group. Membership is strictly managed by admins.'),
    'preset' => array(
      'og' => array(
        'og_selective' => OG_CLOSED,
        'og_directory' => OG_DIRECTORY_NEVER,
        'og_register' => OG_REGISTRATION_ALWAYS,
        'og_private' => defined(OG_PRIVATE_GROUPS_ALWAYS) ? OG_PRIVATE_GROUPS_ALWAYS : 1,
      ),
    ),
  );
  $items['controlled'] = array(
    'type' => 'og',
    'name' => t('Controlled group'),
    'description' => t('All users may view public content from this group. Users must request to join this group.'),
    'preset' => array(
      'og' => array(
        'og_selective' => OG_MODERATED,
        'og_directory' => OG_DIRECTORY_ALWAYS,
        'og_register' => OG_REGISTRATION_ALWAYS,
        'og_private' => defined(OG_PRIVATE_GROUPS_NEVER) ? OG_PRIVATE_GROUPS_NEVER : 0,
      ),
    ),
  );
  $items['public'] = array(
    'type' => 'og',
    'name' => t('Public group'),
    'description' => t('All users may view public content from this group. User may join this group at will.'),
    'preset' => array(
      'og' => array(
        'og_selective' => OG_OPEN,
        'og_directory' => OG_DIRECTORY_ALWAYS,
        'og_register' => OG_REGISTRATION_ALWAYS,
        'og_private' => defined(OG_PRIVATE_GROUPS_NEVER) ? OG_PRIVATE_GROUPS_NEVER : 0,
      ),
    ),
  );
  return $items;
}

/**
 * Implementation of hook_menu().
 */
function spaces_og_menu() {
  $items = array();
  $items["og/users/%node/ucreate"] = array(
    'title' => t('Add new account'),
    'page callback' => 'spaces_og_ucreate',
    'page arguments' => array(
      2,
    ),
    'type' => MENU_LOCAL_TASK,
    'access callback' => 'spaces_admin_access',
    'access arguments' => array(
      'og',
    ),
    'weight' => 1,
  );
  $items["node/%node/spaces"] = array(
    'title' => t('Spaces'),
    'page callback' => 'spaces_features_page',
    'page arguments' => array(),
    'access callback' => '_spaces_og_admin_access',
    'access arguments' => array(
      1,
      'og',
      'features',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  $items["node/%node/spaces/features"] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function spaces_og_perm() {
  return array(
    'view users outside groups',
  );
}

/**
 * Implementation of hook_user().
 */
function spaces_og_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'form':
      if ($category == 'account') {

        // Add the groups selector to the user form.
        $form = og_user('register', $edit, $account, $category = NULL);
        $form['og_register']['#weight'] = 5;
        $form['og_register']['og_register']['#default_value'] = array_keys($account->og_groups);
        return $form;
      }
      break;
    case 'update':
      if (is_array($edit['og_register'])) {

        // Process groups selections.
        $active_groups = array_keys(array_filter($edit['og_register']));
        foreach (array_diff($active_groups, array_keys($account->og_groups)) as $gid) {
          $return = og_subscribe_user($gid, $account);
          if (!empty($return['message'])) {
            drupal_set_message($return['message']);
          }
        }
        foreach (array_diff(array_keys($edit['og_register']), $active_groups) as $gid) {
          og_delete_subscription($gid, $account->uid);
        }
      }
      break;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function spaces_og_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'prepare':
      if (og_is_group_type($node->type) && ($space = spaces_load('og', $node->nid))) {
        $node->purl = $space->purl;
      }
      else {
        if (!og_is_omitted_type($node->type)) {
          $space = spaces_get_space();
          if ($space->type == 'og') {
            _spaces_enforce_feature($space->sid, $node);
          }
        }
      }
      break;
    case 'presave':

      // switch node's group if specified
      if (!og_is_omitted_type($node->type)) {
        if (isset($node->spaces_og['gid']) && !in_array($node->spaces_og['gid'], $node->og_groups)) {
          $new_gid = $node->spaces_og['gid'];
          _spaces_enforce_feature($new_gid, $node);
        }
      }
      break;
    case 'insert':
    case 'update':

      // save PURL modifier & preset from node form information
      if (og_is_group_type($node->type)) {
        $space = spaces_load('og', $node->nid);
        $space->purl = is_array($node->purl) && !empty($node->purl['value']) ? $node->purl['value'] : $space->purl;
        $space->preset = isset($node->preset) ? $node->preset : $space->preset;

        // Save that shiz
        spaces_save($space);

        // Enforce OG preset directly on node object for inserts
        if ($op == 'insert') {
          $node->og_selective = $space->group->og_selective;
          $node->og_register = $space->group->og_register;
          $node->og_directory = $space->group->og_directory;
          $node->og_private = $space->group->og_private;
        }
      }
      break;
    case 'delete':
      if (og_is_group_type($node->type)) {
        $space = spaces_load('og', $node->nid);
        if ($space) {
          spaces_delete($space);
        }
      }
      break;
  }
}

/*
 * Implementation of hook_form_alter()
 */
function spaces_og_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    default:
      if ($form['#id'] == 'node-form' && arg(0) . '/' . arg(1) != 'admin/content') {

        // GROUP NODES
        if (og_is_group_type($form['#node']->type)) {
          _spaces_og_form_alter_group($form, $form_state);
        }
        elseif (!og_is_omitted_type($form['#node']->type)) {
          _spaces_og_form_alter_node($form, $form_state);
        }
      }
      break;
  }
}

/**
 * Group node form_alter().
 * @TODO: repair default values on node previews.
 */
function _spaces_og_form_alter_group(&$form, $form_state) {
  _spaces_og_make_hidden($form['og_selective']);
  _spaces_og_make_hidden($form['og_register']);
  _spaces_og_make_hidden($form['og_private']);
  _spaces_og_make_hidden($form['og_directory']);
  _spaces_og_make_hidden($form['themes']);

  // Add context prefix form
  $form['purl'] = purl_form('spaces_og', $form['#node']->nid, $form['#node']->purl);

  // Add presets form
  $nid = isset($form['#node']->nid) ? $form['#node']->nid : NULL;
  $space = spaces_load('og', $nid);
  $form['spaces_preset'] = spaces_form_presets($space);

  // Add custom submit handler
  $form['#submit'][] = '_spaces_og_group_node_form_submit';
}

/**
 * Custom submit handler for group node forms
 */
function _spaces_og_group_node_form_submit($form, &$form_state) {

  // Prefix might have changed -- do the redirect correctly
  if (isset($form_state['values']['nid'])) {
    $nid = $form_state['values']['nid'];
    $space = spaces_load('og', $nid);
    if ($space) {

      // Disable PURL for the current redirect
      purl_disable(TRUE);
      $form_state['redirect'] = "node/{$nid}";
    }
  }
}

/**
 * Group-enabled node form_alter()
 */
function _spaces_og_form_alter_node(&$form, $form_state) {
  global $user;
  $space = spaces_get_space();

  // Retrieve the content type label
  $types = node_get_types();
  if (!empty($types['group'])) {
    $typename = $types['group']->name;
  }

  // Collect groups for which this feature is enabled
  $options = array(
    0 => '--' . t('Select a !typename', array(
      '!typename' => $typename,
    )) . '--',
  );
  $valid_groups = _spaces_og_group_options($form['#node']->type);
  $user_groups = array();
  foreach (og_get_subscriptions($user->uid) as $node) {
    if (!empty($valid_groups[$node['nid']])) {
      $user_groups[$node['nid']] = $node['title'];
    }
  }

  // Give users access to only their groups
  $options[t('My !typenames', array(
    '!typename' => $typename,
  ))] = $user_groups;

  // Give admins access to all group options
  if (user_access('administer organic groups')) {
    $options[t('All !typenames', array(
      '!typename' => $typename,
    ))] = array_diff_key($valid_groups, $user_groups);
  }

  // Only show the dialogue if we have at least 1 group to target
  if (count($options > 1)) {

    // Node preview handling
    if (!empty($form['#node']->spaces_og)) {
      $default_gid = $form['#node']->spaces_og;
    }
    else {
      if (is_array($form['#node']->og_groups) && count($form['#node']->og_groups)) {
        reset($form['#node']->og_groups);
        $default_gid = key($form['#node']->og_groups);
      }
      else {
        if ($space) {
          $default_gid = $space->sid;
        }
        else {
          $default_gid = 0;

          // The invalid group
        }
      }
    }
    $form['spaces_og'] = array(
      '#type' => 'fieldset',
      '#tree' => true,
      '#title' => $typename,
    );
    $message = $form['#node']->nid ? t('Please select a !typename to move this post to.', array(
      '!typename' => strtolower($typename),
    )) : t('Please select a !typename to add this post to.', array(
      '!typename' => strtolower($typename),
    ));
    $form['spaces_og']['gid'] = array(
      '#required' => TRUE,
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $default_gid,
      '#description' => $message,
      '#element_validate' => array(
        'spaces_og_nodeform_validate',
      ),
    );
  }

  // Recurse into og_options hiding all of them.
  _spaces_og_make_hidden($form['og_nodeapi']);

  // We can only determine the privacy of this node if currently in
  // a group space. Otherwise, it will be determined by the feature
  // setting of the group targeted by the selector above.
  if ($space->type == 'og') {
    $form['spaces'] = array(
      '#title' => t('Privacy'),
      '#type' => 'fieldset',
      '#weight' => 100,
    );
    switch ($form['#node']->og_public) {
      case OG_VISIBLE_GROUPONLY:
        $form['spaces']['#description'] = t('A post of this type is always <strong>private</strong>. Only members of this !typename will see it.', array(
          '!typename' => strtolower($typename),
        ));
        break;
      case OG_VISIBLE_BOTH:
        $form['spaces']['#description'] = t('A post of this type is always <strong>public</strong>. All visitors will see it.');
        break;
    }
  }
}

/**
 * Element validator for group targeting selector.
 */
function spaces_og_nodeform_validate($element, &$form_state) {
  if ($element['#value'] == 0) {

    // Retrieve the content type label
    $types = node_get_types();
    if (!empty($types['group'])) {
      $typename = $types['group']->name;
    }
    form_error($element, t('Please choose a !typename to post to.', array(
      '!typename' => strtolower($typename),
    )));
  }
}

/**
 * Set all elements in a given form to 'value'. Using value preserves the tree and prevents
 * The element from being rendered.
 */
function _spaces_og_make_hidden(&$form) {
  if (isset($form['#type'])) {
    $form['#type'] = 'value';
    $form['#required'] = false;
  }
  if (is_array($form)) {
    foreach ($form as $key => $value) {
      if (is_array($value) && strpos($key, '#') !== 0) {
        _spaces_og_make_hidden($form[$key]);
      }
    }
  }
}

/**
 * Spaces OG wrapper
 */
function spaces_og_ucreate($node) {
  drupal_set_title(t('Add new account'));
  if (module_exists('ucreate')) {
    $form = drupal_get_form('ucreate_user_form');

    // context_set('spaces', false);
    return $form;
  }
  return '';
}

/**
 * Custom subscription link - use "join" instead of "subscribe" - make it shorter.
 */
function spaces_og_subscription_link() {
  global $user;
  if ($user->uid && is_array($user->og_groups) && ($space = spaces_get_space())) {
    $gid = $space->sid;
    $node = node_load($gid);

    // User is a member
    if ($user->og_groups[$gid]) {

      // Do not let managers leave the group -- TODO: figure out a
      // better workflow for these situations.
      if (!og_is_group_admin($node)) {
        return array(
          'title' => t('Leave this group'),
          'href' => "og/unsubscribe/" . $node->nid,
          'query' => 'destination=' . $_GET['q'],
        );
      }
    }
    else {
      if (db_result(db_query("SELECT count(nid) FROM {og_uid} WHERE nid = %d AND uid = %d AND is_active = 0", $gid, $user->uid))) {
        return array(
          'title' => t('Cancel request to join'),
          'href' => "og/unsubscribe/" . $node->nid,
          'query' => 'destination=' . $_GET['q'],
        );
      }
      else {
        if ($node->og_selective == OG_MODERATED) {
          return array(
            'title' => t('Request to join'),
            'href' => "og/subscribe/" . $node->nid,
            'query' => 'destination=' . $_GET['q'],
          );
        }
        elseif ($node->og_selective == OG_OPEN) {
          return array(
            'title' => t('Join this group'),
            'href' => "og/subscribe/" . $node->nid,
            'query' => 'destination=' . $_GET['q'],
          );
        }
      }
    }
  }
  return;
}

/**
 * API function that enforces OG group and privacy settings on a node.
 */
function _spaces_enforce_feature($gid, &$node) {
  $map = spaces_content_types();
  $space = spaces_load('og', $gid);
  $feature_name = $map[$node->type];
  if ($space && $space
    ->feature_access($feature_name)) {
    $privacy = $space->features[$feature_name];
    switch ($privacy) {
      case SPACES_OG_PRIVATE:
        $node->og_public = OG_VISIBLE_GROUPONLY;
        break;
      case SPACES_OG_PUBLIC:
        $node->og_public = OG_VISIBLE_BOTH;
        break;
    }
    $node->og_groups = array(
      $gid => $gid,
    );
  }
}

/**
 * Tests for user membership in group
 */
function spaces_og_is_member($gid = null, $uid = null) {
  return og_is_group_member($gid);
  global $user;
  if (!$gid && ($space = spaces_get_space())) {
    $gid = $space->sid;
  }
  $account = $uid ? user_load(array(
    'uid' => $uid,
  )) : $user;
  if ($gid && $account) {
    if (user_access('administer organic groups', $account)) {
      return true;
    }
    else {
      if (is_array($account->og_groups) && $account->og_groups[$gid]) {
        return true;
      }
    }
  }
  return false;
}

/**
 * Generates an array of groups that a node could potentially
 * be a member of based on enabled spaces features and optionally
 * the specified user's groups
 */
function _spaces_og_group_options($type, $uid = 0) {
  $types = spaces_content_types();
  $group_options = array();
  $args = array(
    $types[$type],
    0,
  );
  if ($uid) {
    $join = "JOIN {og_uid} ogu ON ogu.nid = og.nid";
    $where = "AND ogu.uid = %d AND ogu.is_active >= 1";
    $args[] = $uid;
  }
  $result = db_query("SELECT og.nid, n.title\n    FROM {og}\n      JOIN {node} n ON og.nid = n.nid\n      JOIN {spaces_features} sf ON sf.sid = og.nid\n      {$join}\n    WHERE n.status = 1\n      AND sf.id = '%s'\n      AND sf.value != %d\n      {$where}\n    ORDER BY n.title ASC", $args);
  while ($group = db_fetch_object($result)) {
    $group_options[$group->nid] = $group->title;
  }
  return $group_options;
}

/**
 * Implementation of hook_requirements().
 */
function spaces_og_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':

      // Check that existing node types are og enabled
      $types = spaces_content_types();
      $existing_types = node_get_types();
      foreach ($types as $type => $feature) {
        if (og_is_omitted_type($type) && array_key_exists($type, $existing_types)) {
          $requirements['spaces'] = array(
            'title' => $t('Spaces OG configuration'),
            'description' => $t('The !type content type appear to be misconfigured.', array(
              '!type' => l($type, 'admin/content/types/' . $type),
            )),
            'severity' => REQUIREMENT_ERROR,
            'value' => $t('OG Misconfiguration'),
          );
          return $requirements;
        }
      }
      $requirements['spaces'] = array(
        'title' => $t('Spaces OG configuration'),
        'description' => t('The spaces og module is installed and configured properly'),
        'severity' => REQUIREMENT_OK,
        'value' => $t('Installed correctly'),
      );
  }
  return $requirements;
}

/**
 * Get drupal users
 *
 * @param $exclude_system
 *   Bool, whether to exclude system user - ie user 1
 *
 * @return
 *   Array of user objects, where key is uid.
 */
function spaces_og_get_users($exclude_system = true, $active_only = true, $group_only = true, $pager = 0) {
  $args[] = $exclude_system ? 1 : 0;
  $args[] = $active_only ? 1 : 0;
  if ($group_only && ($space = spaces_get_space())) {
    $join = 'JOIN {og_uid} ogu ON u.uid = ogu.uid';
    $where = 'AND ogu.nid = %d';
    $args[] = $space->sid;
  }
  if ($pager == 0) {
    $result = db_query("SELECT u.uid, u.name, u.mail, u.picture, u.status FROM {users} u {$join} WHERE u.uid > %d AND u.status >= %d {$where} ORDER BY name", $args);
  }
  else {
    $result = pager_query("SELECT u.uid, u.name, u.mail, u.picture, u.status FROM {users} u {$join} WHERE u.uid > %d AND u.status >= %d {$where} ORDER BY name", $pager, 0, null, $args);
  }
  $users = array();
  while ($u = db_fetch_object($result)) {
    $users[$u->uid] = $u;
  }
  return $users;
}

/**
 * Wrapper around spaces_admin_access(). Used to determine menu local
 * task visibility : |
 */
function _spaces_og_admin_access($node, $type = NULL, $op = NULL) {
  if (og_is_group_type($node->type)) {
    return spaces_admin_access($type, $op);
  }
  return false;
}

Functions

Namesort descending Description
spaces_og_form_alter
spaces_og_get_users Get drupal users
spaces_og_init Implementation of hook_init(); We're gonna bulldoze your OG settings. Get over it.
spaces_og_is_member Tests for user membership in group
spaces_og_menu Implementation of hook_menu().
spaces_og_nodeapi Implementation of hook_nodeapi().
spaces_og_nodeform_validate Element validator for group targeting selector.
spaces_og_perm Implementation of hook_perm().
spaces_og_requirements Implementation of hook_requirements().
spaces_og_spaces_presets Implementation of hook_spaces_presets().
spaces_og_spaces_types Implementation of hook_spaces_types().
spaces_og_subscription_link Custom subscription link - use "join" instead of "subscribe" - make it shorter.
spaces_og_ucreate Spaces OG wrapper
spaces_og_user Implementation of hook_user().
_spaces_enforce_feature API function that enforces OG group and privacy settings on a node.
_spaces_og_admin_access Wrapper around spaces_admin_access(). Used to determine menu local task visibility : |
_spaces_og_form_alter_group Group node form_alter(). @TODO: repair default values on node previews.
_spaces_og_form_alter_node Group-enabled node form_alter()
_spaces_og_group_node_form_submit Custom submit handler for group node forms
_spaces_og_group_options Generates an array of groups that a node could potentially be a member of based on enabled spaces features and optionally the specified user's groups
_spaces_og_make_hidden Set all elements in a given form to 'value'. Using value preserves the tree and prevents The element from being rendered.

Constants