View source
<?php
define('SPACES_OG_PRIVATE', 1);
define('SPACES_OG_PUBLIC', 2);
if (function_exists('spaces_menu')) {
class space_og implements space {
var $group = NULL;
var $title = NULL;
function __construct($type, $sid = NULL, $is_active = FALSE) {
if ($sid) {
$this->group = node_load($sid);
$this->title = $this->group->title;
if ($is_active) {
og_set_group_context($this->group);
if ($this->group->og_theme) {
global $custom_theme;
$custom_theme = $group->og_theme;
}
}
}
else {
$this->group = new StdClass();
}
}
function save() {
og_update_group($this->group);
return;
}
function delete() {
return;
}
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;
}
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;
}
function feature_options() {
return array(
SPACES_FEATURE_DISABLED => t('Disabled'),
SPACES_OG_PRIVATE => t('Private'),
SPACES_OG_PUBLIC => t('Public'),
);
}
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',
),
);
$links['settings'] = array(
'title' => t('Settings'),
'href' => 'node/' . $this->sid . '/edit',
'attributes' => array(
'class' => 'settings',
),
);
}
}
function form() {
if (empty($this->sid)) {
$old_gid = NULL;
if (isset($this->group->nid)) {
$old_gid = $this->group->nid;
$this->group->nid = -1;
}
$form = og_group_form($this->group, array());
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;
unset($form['og_description']);
unset($form['themes']);
$form['#title'] = t('Group settings');
$form['#type'] = 'fieldset';
return $form;
}
}
function validate($values) {
return;
}
function submit($values) {
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();
}
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;
}
function redirect($op = 'home') {
switch ($op) {
case 'home':
if (!empty($this->purl)) {
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;
}
}
function router($op, $object = NULL, $is_active = TRUE) {
switch ($op) {
case 'menu':
if ($is_active) {
global $user;
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) {
if (arg(0) == 'user' || arg(0) == 'openid') {
return true;
}
drupal_goto('user/login');
}
}
}
return false;
}
else {
return true;
}
case 'node view':
$node = $object;
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 (isset($node_types[$node->type])) {
if ($is_active) {
if (in_array($this->sid, $node->og_groups)) {
return true;
}
else {
purl_goto($_GET['q'], array(
'purl' => array(
'provider' => 'spaces_og',
'id' => $gid,
),
));
}
}
else {
$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;
if (og_is_omitted_type($node->type)) {
return true;
}
else {
if (og_is_group_type($node->type)) {
if ($is_active && $node->nid == $this->sid) {
return true;
}
$space = spaces_load('og', $node->nid, true);
if ($space->purl) {
purl_goto($_GET['q'], array(
'purl' => array(
'provider' => 'spaces_og',
'id' => $space->sid,
),
));
}
else {
return true;
}
}
}
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;
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,
),
));
}
}
}
}
}
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]);
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,
),
));
}
}
}
return false;
}
else {
$space = spaces_get_space();
if ($space) {
return true;
}
else {
if ($account->uid == $user->uid) {
return true;
}
}
return false;
}
case 'user form':
return true;
}
}
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) {
$query
->add_where(0, "{$table}.group_nid = ***CURRENT_GID***");
}
}
}
}
function spaces_og_init() {
global $conf;
$conf['og_visibility_directory'] = 2;
$conf['og_visibility_registration'] = 3;
$conf['og_private_groups'] = 3;
$conf['og_notification'] = 0;
$conf['og_audience_checkboxes'] = 0;
$conf['og_visibility'] = 2;
$conf['og_audience_required'] = 1;
$conf['og_member_pics'] = 0;
foreach (spaces_features('og') as $feature) {
if (!empty($feature->node)) {
foreach ($feature->node as $type) {
$conf['og_content_type_usage_' . $type] = 'group_post_standard';
}
}
}
}
function spaces_og_spaces_types() {
return array(
'og' => array(
'class' => 'space_og',
'title' => t('Group space'),
'custom purl' => TRUE,
'base path' => 'node/%sid',
),
);
}
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;
}
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;
}
function spaces_og_perm() {
return array(
'view users outside groups',
);
}
function spaces_og_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'form':
if ($category == 'account') {
$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'])) {
$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;
}
}
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':
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':
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;
spaces_save($space);
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;
}
}
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') {
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;
}
}
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']);
$form['purl'] = purl_form('spaces_og', $form['#node']->nid, $form['#node']->purl);
$nid = isset($form['#node']->nid) ? $form['#node']->nid : NULL;
$space = spaces_load('og', $nid);
$form['spaces_preset'] = spaces_form_presets($space);
$form['#submit'][] = '_spaces_og_group_node_form_submit';
}
function _spaces_og_group_node_form_submit($form, &$form_state) {
if (isset($form_state['values']['nid'])) {
$nid = $form_state['values']['nid'];
$space = spaces_load('og', $nid);
if ($space) {
purl_disable(TRUE);
$form_state['redirect'] = "node/{$nid}";
}
}
}
function _spaces_og_form_alter_node(&$form, $form_state) {
global $user;
$space = spaces_get_space();
$types = node_get_types();
if (!empty($types['group'])) {
$typename = $types['group']->name;
}
$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'];
}
}
$options[t('My !typenames', array(
'!typename' => $typename,
))] = $user_groups;
if (user_access('administer organic groups')) {
$options[t('All !typenames', array(
'!typename' => $typename,
))] = array_diff_key($valid_groups, $user_groups);
}
if (count($options > 1)) {
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;
}
}
}
$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',
),
);
}
_spaces_og_make_hidden($form['og_nodeapi']);
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;
}
}
}
function spaces_og_nodeform_validate($element, &$form_state) {
if ($element['#value'] == 0) {
$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),
)));
}
}
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]);
}
}
}
}
function spaces_og_ucreate($node) {
drupal_set_title(t('Add new account'));
if (module_exists('ucreate')) {
$form = drupal_get_form('ucreate_user_form');
return $form;
}
return '';
}
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);
if ($user->og_groups[$gid]) {
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;
}
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,
);
}
}
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;
}
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;
}
function spaces_og_requirements($phase) {
$requirements = array();
$t = get_t();
switch ($phase) {
case 'runtime':
$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;
}
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;
}
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;
}