View source
<?php
function spaces_og_ctools_plugin_api($module, $api) {
if ($module == "spaces" && $api == "spaces") {
return array(
"version" => 3,
);
}
else {
if ($module == 'spaces' && $api == 'plugins') {
return array(
'version' => 3,
);
}
}
}
function spaces_og_enable() {
$purl_types = variable_get('purl_types', array());
if (!variable_get('purl_method_spaces_og', FALSE)) {
$purl_types['path'] = 'path';
variable_set('purl_types', $purl_types);
variable_set('purl_method_spaces_og', 'path');
}
variable_set('purl_types', $purl_types);
variable_set('purl_method_spaces_og', variable_get('purl_method_spaces_og', FALSE));
$weight = db_result(db_query("SELECT weight FROM {system} WHERE type = 'module' AND name = 'og'"));
db_query("UPDATE {system} SET weight = %d WHERE name = 'spaces_og'", $weight + 1);
}
function spaces_og_purl_provider() {
$items = array();
$items["spaces_og"] = array(
'name' => t('Group space'),
'description' => t('Sets a space as the active space.'),
'callback' => 'spaces_init_space',
'callback arguments' => array(
'og',
),
'example' => 'my-space',
);
return $items;
}
function spaces_og_spaces_plugins() {
$plugins = array();
$plugins['space_og'] = array(
'handler' => array(
'path' => drupal_get_path('module', 'spaces_og') . '/plugins',
'file' => 'space_og.inc',
'class' => 'space_og',
'parent' => 'space_type_purl',
),
);
return $plugins;
}
function spaces_og_spaces_registry() {
return array(
'types' => array(
'og' => array(
'title' => t('Group space'),
'plugin' => 'space_og',
'path' => 'node/%node',
),
),
);
}
function spaces_og_spaces_presets() {
$export = array();
$spaces_presets = new stdClass();
$spaces_presets->disabled = FALSE;
$spaces_presets->api_version = 3;
$spaces_presets->name = 'og_controlled';
$spaces_presets->title = 'Controlled group';
$spaces_presets->description = 'All users may view public content from this group. Users must request to join this group.';
$spaces_presets->space_type = 'og';
$spaces_presets->value = array(
'variable' => array(
'spaces_og_selective' => '1',
'spaces_og_register' => 1,
'spaces_og_directory' => 1,
'spaces_og_private' => 0,
),
);
$export['og_controlled'] = $spaces_presets;
$spaces_presets = new stdClass();
$spaces_presets->disabled = FALSE;
$spaces_presets->api_version = 3;
$spaces_presets->name = 'og_private';
$spaces_presets->title = 'Private group';
$spaces_presets->description = 'Only members will be able to access this group. Membership is strictly managed by admins.';
$spaces_presets->space_type = 'og';
$spaces_presets->value = array(
'variable' => array(
'spaces_features' => array(),
'spaces_preset_og' => 'og_private',
'spaces_og_selective' => '3',
'spaces_og_register' => 0,
'spaces_og_directory' => 0,
'spaces_og_private' => 1,
),
'context' => array(),
);
$export['og_private'] = $spaces_presets;
$spaces_presets = new stdClass();
$spaces_presets->disabled = FALSE;
$spaces_presets->api_version = 3;
$spaces_presets->name = 'og_public';
$spaces_presets->title = 'Public group';
$spaces_presets->description = 'All users may view public content from this group. User may join this group at will.';
$spaces_presets->space_type = 'og';
$spaces_presets->value = array(
'variable' => array(
'spaces_features' => array(),
'spaces_og_selective' => '0',
'spaces_og_register' => 1,
'spaces_og_directory' => 1,
'spaces_og_private' => 0,
),
);
$export['og_public'] = $spaces_presets;
return $export;
}
function spaces_og_menu() {
$items = array();
$items['user/%user/edit/groups'] = array(
'file' => 'spaces_og.pages.inc',
'title' => 'Groups',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'spaces_og_user_groups_form',
1,
),
'access callback' => 'user_edit_access',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
);
if (module_exists('ucreate')) {
$items["og/users/%node/ucreate"] = array(
'file' => 'spaces_og.pages.inc',
'type' => MENU_LOCAL_TASK,
'title' => 'Add new account',
'page callback' => 'spaces_og_ucreate',
'page arguments' => array(
2,
),
'access callback' => 'spaces_access_admin',
'access arguments' => array(),
'weight' => 1,
);
}
return $items;
}
function spaces_og_menu_alter(&$items) {
foreach ($items as $key => $value) {
if (substr($key, 0, 20) == "node/%node/features/") {
$items[$key]['access callback'] = 'spaces_og_access_admin';
$items[$key]['access arguments'] = array(
1,
);
}
}
if (isset($items['node/%node/features'])) {
$items['node/%node/features']['access callback'] = 'spaces_og_access_admin';
$items['node/%node/features']['access arguments'] = array(
1,
);
}
if (isset($items['node/%node/overrides'])) {
$items['node/%node/overrides']['access callback'] = 'spaces_og_access_admin';
$items['node/%node/overrides']['access arguments'] = array(
1,
);
}
}
function spaces_og_access_admin($node) {
if (og_is_group_type($node->type) && ($space = spaces_load('og', $node->nid))) {
return spaces_access_admin(NULL, $space);
}
else {
return FALSE;
}
}
function spaces_og_user($op, &$edit, &$account, $category = NULL) {
global $user;
switch ($op) {
case 'categories':
$data = array();
$data[] = array(
'name' => 'groups',
'title' => t('Groups'),
'weight' => 10,
);
return $data;
}
}
function spaces_og_perm() {
return array(
'view users outside groups',
);
}
function spaces_og_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if (og_is_group_type($node->type)) {
switch ($op) {
case 'load':
$modifier = purl_load(array(
'id' => isset($node->nid) ? $node->nid : NULL,
'provider' => 'spaces_og',
));
$node->purl = $modifier['value'];
break;
case 'insert':
case 'update':
$space = spaces_load('og', $node->nid, TRUE);
if ($space) {
if (!empty($node->spaces_preset_og)) {
$space->controllers->variable
->set('spaces_preset_og', $node->spaces_preset_og);
}
$selective = $space->controllers->variable
->get('spaces_og_selective');
$node->og_selective = $selective === NULL ? OG_OPEN : $selective;
$register = $space->controllers->variable
->get('spaces_og_register');
$node->og_register = $register === NULL ? TRUE : $register;
$directory = $space->controllers->variable
->get('spaces_og_directory');
$node->og_directory = $directory === NULL ? TRUE : $directory;
$private = $space->controllers->variable
->get('spaces_og_private');
if ($op == 'update' && $private != $node->og_private) {
db_query("UPDATE {og_access_post} og_p \n INNER JOIN {og_ancestry} og_a ON og_p.nid = og_a.nid\n INNER JOIN {node} n ON og_p.nid = n.nid\n SET og_public = %d WHERE og_a.group_nid = %d", !$private, $node->nid);
node_access_needs_rebuild(TRUE);
}
$node->og_private = $private === NULL ? FALSE : $private;
og_update_group($node);
}
$modifier = array(
'provider' => 'spaces_og',
'id' => $node->nid,
);
if (is_array($node->purl) && !empty($node->purl['value'])) {
$modifier['value'] = $node->purl['value'];
purl_save($modifier);
}
else {
if (empty($node->purl)) {
$modifier['value'] = "{$node->type}-{$node->nid}";
purl_save($modifier);
}
}
break;
case 'delete':
spaces_delete('og', $node->nid);
purl_delete(array(
'provider' => 'spaces_og',
'id' => $node->nid,
));
$space = spaces_get_space();
if ($space->type == 'og' && $space->id == $node->nid) {
spaces_set_space(FALSE);
}
break;
}
}
else {
if (!og_is_omitted_type($node->type)) {
switch ($op) {
case 'validate':
if (variable_get('og_audience_required', FALSE) && empty($node->og_groups)) {
if (isset($node->spaces_og_audience) && (int) $node->spaces_og_audience !== 0) {
$node->og_groups[$node->spaces_og_audience] = $node->spaces_og_audience;
}
else {
form_set_error('spaces_og_audience', t('This content type must be posted in a group.'));
}
}
break;
case 'prepare':
case 'presave':
$space = spaces_get_space();
if ($space && $space->type == 'og') {
_spaces_og_enforce_privacy($space->id, $node);
}
else {
if (!empty($node->og_groups)) {
_spaces_og_enforce_privacy(current($node->og_groups), $node);
}
}
if (isset($node->spaces_og_audience) && (int) $node->spaces_og_audience !== 0) {
$node->og_groups[$node->spaces_og_audience] = $node->spaces_og_audience;
}
break;
}
}
}
}
function spaces_og_form_ctools_export_ui_edit_item_form_alter(&$form, $form_state) {
if (isset($form_state['item'], $form_state['item']->table, $form_state['item']->space_type) && $form_state['item']->space_type === 'og') {
module_load_include('inc', 'spaces_og', 'spaces_og.pages');
_spaces_og_form_spaces_preset_editor_alter($form, $form_state);
}
}
function spaces_og_form_node_delete_confirm_alter(&$form, $form_state) {
module_load_include('inc', 'spaces_og', 'spaces_og.pages');
$node = node_load($form['nid']['#value']);
if (og_is_group_type($node->type)) {
$form['#submit'][] = '_spaces_og_node_delete_confirm_submit';
}
elseif (og_is_group_post_type($node->type)) {
$space = spaces_get_space();
if ($space && $space->type == 'og') {
$preset_val = $space->controllers->variable
->get('site_frontpage', 'preset');
$space_val = $space->controllers->variable
->get('site_frontpage', 'space');
if (!empty($preset_val) || !empty($space_val)) {
$form['#redirect'] = url('<front>', array(
'absolute' => TRUE,
'purl' => array(
'provider' => 'spaces_og',
'id' => $space->id,
),
));
}
}
}
}
function spaces_og_form_alter(&$form, $form_state, $form_id) {
if ($form['#id'] == 'node-form' && arg(0) . '/' . arg(1) != 'admin/content') {
if (og_is_group_type($form['#node']->type)) {
module_load_include('inc', 'spaces_og', 'spaces_og.pages');
_spaces_og_form_alter_group($form, $form_state);
}
else {
if (!og_is_omitted_type($form['#node']->type)) {
module_load_include('inc', 'spaces_og', 'spaces_og.pages');
_spaces_og_form_alter_node($form, $form_state);
}
}
}
}
function spaces_og_form_group_submit($form, &$form_state) {
if (!empty($form_state['nid']) && ($space = spaces_load('og', $form_state['nid']))) {
$preset_val = $space->controllers->variable
->get('site_frontpage', 'preset');
$space_val = $space->controllers->variable
->get('site_frontpage', 'space');
if (!empty($preset_val) || !empty($space_val)) {
purl_goto($space->controllers->variable
->get('site_frontpage'), array(
'purl' => array(
'provider' => 'spaces_og',
'id' => $space->id,
),
));
}
else {
purl_goto("node/{$space->id}", array(
'purl' => array(
'provider' => 'spaces_og',
'id' => $space->id,
),
));
}
}
}
function spaces_og_subscription_link() {
global $user;
if ($user->uid && is_array($user->og_groups) && ($space = spaces_get_space())) {
$gid = $space->id;
$node = node_load($gid);
if (isset($user->og_groups[$gid])) {
if (!og_is_group_admin($node) && $node->og_selective != OG_CLOSED) {
return array(
'title' => t('Leave this group'),
'href' => "og/unsubscribe/{$node->nid}/{$user->uid}",
'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}/{$user->uid}",
'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_og_enforce_privacy($gid, &$node) {
if ($group = node_load($gid)) {
if ($group->og_private) {
$node->og_public = OG_VISIBLE_GROUPONLY;
}
else {
$node->og_public = OG_VISIBLE_BOTH;
}
$node->og_groups = array(
$gid => $gid,
);
return TRUE;
}
return FALSE;
}
function spaces_og_context_page_reaction() {
$space = spaces_get_space();
if ($space && $space->type === 'og') {
$item = menu_get_item();
if ($item['access']) {
$bc = og_get_breadcrumb($space->group);
$bc[0] = l(t('Home'), "", array(
'purl' => array(
'disabled' => TRUE,
),
));
drupal_alter('spaces_og_breadcrumb', $bc);
drupal_set_breadcrumb($bc);
}
}
}
function spaces_og_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
switch ($primary_table) {
case 'term_data':
case 'td':
case 't':
if (arg(0) !== 'admin' && strpos($query, 'r.vid = %d') === FALSE) {
$return = array();
$return['distinct'] = 1;
$return['join'] = "\n LEFT JOIN {term_node} spaces_og_tn ON {$primary_table}.{$primary_field} = spaces_og_tn.tid\n LEFT JOIN {vocabulary} spaces_og_v ON {$primary_table}.vid = spaces_og_v.vid\n LEFT JOIN {og_ancestry} spaces_og_oga ON spaces_og_tn.nid = spaces_og_oga.nid\n LEFT JOIN {node} spaces_og_n ON spaces_og_oga.group_nid = spaces_og_n.nid";
$return['where'] = "(spaces_og_oga.group_nid IS NULL)";
$return['where'] .= " OR (spaces_og_v.tags = 0)";
global $user;
if (!empty($user->og_groups)) {
$groups = array_keys($user->og_groups);
$groups = implode(', ', $groups);
$return['where'] .= " OR (spaces_og_oga.group_nid IN ({$groups}) AND spaces_og_n.status = 1)";
}
return $return;
}
break;
case 'users':
case 'u':
if (arg(0) !== 'admin' && !user_access('view users outside groups') && strpos($query, 'uid = %d') === FALSE) {
$return = array();
$return['distinct'] = 1;
$return['join'] = "\n LEFT JOIN {og_uid} spaces_og_ogu ON {$primary_table}.{$primary_field} = spaces_og_ogu.uid\n JOIN {node} spaces_og_n ON spaces_og_ogu.nid = spaces_og_n.nid";
global $user;
if (!empty($user->og_groups)) {
$groups = array_keys($user->og_groups);
$groups = implode(', ', $groups);
$return['where'] = "\n (spaces_og_ogu.nid IN ({$groups}) AND spaces_og_n.status = 1) OR\n ({$primary_table}.{$primary_field} = {$user->uid})";
}
else {
$return['where'] = "FALSE OR ({$primary_table}.{$primary_field} = {$user->uid})";
}
return $return;
}
break;
}
}
function spaces_og_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'spaces_og') . '/views',
);
}
function spaces_og_spaces_get_space_from_object($type, $object) {
if ($type == 'node') {
if (og_is_group_type($object->type)) {
return spaces_load('og', $object->nid);
}
elseif (!empty($object->og_groups)) {
reset($object->og_groups);
return spaces_load('og', current($object->og_groups));
}
}
}