View source
<?php
include_once 'commons_core.features.inc';
function commons_core_perm() {
return array(
'view content rss feeds',
'view group content rss feeds',
);
}
function commons_core_menu() {
return array(
'myprofile' => array(
'title' => 'My profile',
'description' => 'View your user profile',
'page callback' => 'commons_core_profile_redirect',
'access callback' => 'user_is_logged_in',
'type' => MENU_CALLBACK,
),
'community' => array(
'title' => 'Community',
'description' => 'View the groups in the community',
'page callback' => 'drupal_goto',
'page arguments' => array(
'groups',
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
),
'admin/settings/commons' => array(
'title' => 'Commons',
'description' => 'Configure settings for the Commons environment',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commons_core_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'commons_core.admin.inc',
'type' => MENU_NORMAL_ITEM,
),
);
}
function commons_core_menu_alter(&$items) {
$path = drupal_get_path('module', 'commons_core');
$skip_node_override = FALSE;
if (module_exists('page_manager')) {
if ($task = page_manager_get_task('node_view')) {
if (isset($task['disabled']) && !$task['disabled']) {
$skip_node_override = TRUE;
}
}
}
if (!$skip_node_override) {
$items['node/%node']['page callback'] = 'commons_core_node_page_view';
$items['node/%node']['file'] = 'commons_core.overrides.inc';
$items['node/%node']['file path'] = $path;
}
$items['node/%node/view']['title callback'] = 'commons_core_set_node_tab_title';
$items['node/%node/view']['title arguments'] = array(
1,
);
$items['comment/edit']['page callback'] = 'commons_core_comment_edit';
$items['comment/edit']['file'] = 'commons_core.overrides.inc';
$items['comment/edit']['file path'] = $path;
}
function commons_core_block($op = 'list', $delta = 0, $edit = array()) {
module_load_include('inc', 'commons_core', 'commons_core.blocks');
return _commons_core_block($op, $delta, $edit);
}
function commons_core_form_alter(&$form, &$form_state, $form_id) {
module_load_include('inc', 'commons_core', 'commons_core.form');
if ($form['#id'] == 'node-form') {
commons_core_node_form_alter($form, $form_state);
}
else {
if (strstr($form_id, 'views_bulk_operations_form')) {
commons_core_views_bulk_operations_form_alter($form, $form_state);
}
else {
$hook = "commons_core_form_{$form_id}_alter";
if (function_exists($hook)) {
$hook($form, $form_state);
}
}
}
}
function commons_core_mail($key, &$message, $params) {
switch ($key) {
case 'og user delete':
$message['subject'] = t('You have been removed from !group', array(
'!group' => $params['group']->title,
));
$message['body'] = t('A manager from !group has removed you from the group.', array(
'!group' => $params['group']->title,
));
break;
case 'og user insert':
global $base_url;
$link = $base_url . '/' . ($params['group']->path ? $params['group']->path : 'node/' . $params['group']->nid);
$message['subject'] = t('You have been added to !group', array(
'!group' => $params['group']->title,
));
$message['body'] = t('A manager from !group has added you to the group.', array(
'!group' => $params['group']->title,
));
$message['body'] .= "\n\n\n";
$message['body'] .= t('The group is located at !link', array(
'!link' => $link,
));
break;
}
}
function commons_core_init() {
commons_core_force_login();
if (arg(0) == 'og' && !arg(1)) {
drupal_goto('groups');
}
if (arg(0) == 'og' && arg(1) == 'my') {
drupal_goto('groups/mine');
}
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
global $user;
if ($user->uid) {
$node = node_load(arg(1));
if (og_is_group_type($node->type)) {
if (og_is_group_admin($node, $user)) {
$sql = "SELECT COUNT(uid) FROM {og_uid} WHERE nid = %d AND is_active = 0";
$count = db_result(db_query($sql, $node->nid));
if ($count) {
drupal_set_message(t('There are currently !count pending membership requests. Click !here to view them.', array(
'!count' => $count,
'!here' => l('here', 'og/users/' . $node->nid),
)));
}
}
}
}
}
commons_core_set_breadcrumb($_GET['q']);
}
function commons_core_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'validate':
_commons_core_check_group_sticky($node);
_commons_core_check_group_title($node);
break;
}
}
function commons_core_link_alter(&$links, $node, $comment = NULL) {
global $user;
if ($comment) {
if (!isset($links['comment_edit'])) {
if (comment_access('edit', $comment) || _commons_core_can_admin_comment($comment)) {
$link['comment_edit'] = array(
'title' => t('edit'),
'href' => 'comment/edit/' . $comment->cid,
);
$links = $link + $links;
}
}
}
}
function commons_core_theme() {
$registry = array(
'commons_core_user_stats_block' => array(),
'commons_core_info_block' => array(),
'commons_core_group_members_block' => array(
'arguments' => array(
'managers' => NULL,
'members' => NULL,
'link' => NULL,
),
),
'commons_core_unfollow_link' => array(
'arguments' => array(
'uid' => NULL,
'rid' => NULL,
),
),
);
foreach ($registry as $key => $entry) {
$registry[$key]['file'] = 'commons_core.theme.inc';
}
return $registry;
}
function commons_core_strongarm_alter(&$items) {
$commons_tags_vid = variable_get('commons_tags_vid', 1);
if (isset($items['tagadelic_block_tags_1']) && $commons_tags_vid != 1) {
$new_item = $items['tagadelic_block_tags_1'];
unset($items['tagadelic_block_tags_1']);
$new_item->name = 'tagadelic_block_tags_' . $commons_tags_vid;
$items['tagadelic_block_tags_' . $commons_tags_vid] = $new_item;
}
}
function commons_core_og($op, $gid, $uid, $args) {
switch ($op) {
case 'user insert':
case 'user delete':
$group = node_load($gid);
og_load_group($group);
$user = user_load($uid);
if (variable_get('commons_email_selective_group', 1)) {
if ($group->uid != $user->uid) {
if ($group->og_selective == OG_INVITE_ONLY || $group->og_selective == OG_CLOSED) {
drupal_mail('commons_core', "og {$op}", $user->mail, language_default(), array(
'group' => $group,
));
}
}
}
break;
}
}
function commons_core_og_links_alter(&$links, $node) {
unset($links['manager']);
unset($links['subscribers']);
foreach ($links as $key => $link) {
if (substr($key, 0, 7) == 'create_') {
unset($links[$key]);
}
}
}
function commons_core_content_default_fields_alter(&$fields) {
if (isset($fields['group-field_group_image'])) {
$fields['group-field_group_image']['widget']['default_image']['filepath'] = file_directory_path() . '/default-group.png';
$fields['group-field_group_image']['widget']['default_image']['destination'] = file_directory_path() . '/default-group.png';
}
}
function commons_core_field_access($op, $field, $account = NULL, $node = NULL) {
if ($field['field_name'] == 'field_group_sticky') {
switch ($op) {
case 'view':
case 'edit':
if (arg(0) == 'node' && arg(1) == 'add' && ($group = og_features_get_group_context())) {
if (!og_is_group_admin($group, $account) && !user_access('administer nodes') && !commons_core_is_community_manager($account)) {
return FALSE;
}
else {
return TRUE;
}
}
if ($node) {
$groups = og_get_node_groups($node);
if (empty($groups)) {
return FALSE;
}
if (user_access('administer nodes') || commons_core_is_community_manager($account)) {
return TRUE;
}
foreach ($groups as $nid => $group) {
if (!og_is_group_admin(node_load($nid))) {
return FALSE;
}
}
}
return TRUE;
}
}
}
function commons_core_user_relationships_remove_link_alter(&$list, $args) {
foreach ($args[3] as $rid => $relationship) {
if ($relationship->rtid == 1) {
$list[$relationship->rid] = theme('commons_core_unfollow_link', $args[0]->uid, $relationship->rid);
}
}
}
function commons_core_action_info() {
return array(
'commons_core_noderead_action' => array(
'description' => t('Mark as read'),
'type' => 'node',
'configurable' => FALSE,
'hooks' => array(
'nodeapi' => array(
'presave',
),
'comment' => array(
'insert',
'update',
),
),
),
'commons_core_nodeunread_action' => array(
'description' => t('Mark as unread'),
'type' => 'node',
'configurable' => FALSE,
'hooks' => array(
'nodeapi' => array(
'presave',
),
'comment' => array(
'insert',
'update',
),
),
),
);
}
function commons_core_views_pre_view(&$view, &$display_id, &$args) {
switch ($view->name) {
case 'tag_cloud':
$view->display_handler
->override_option('footer', l(t('View more'), 'tagadelic'));
break;
case 'recent_featured_stream':
$view->display_handler
->override_option('footer', l(t('View all featured content'), 'featured'));
break;
case 'recent_notices':
if (node_access('create', 'notice') && strstr($display_id, 'block')) {
$view->display_handler
->override_option('header', l(t('Create a notice'), 'node/add/notice'));
}
}
}
function commons_core_views_default_views_alter(&$views) {
$vid = variable_get('commons_tags_vid', 1);
$modules = features_get_info('module');
$commons_views = array();
foreach ($modules as $name => $module) {
if (substr($name, 0, 8) == 'commons_') {
if (isset($module->info['features']['views'])) {
$commons_views = array_merge($commons_views, $module->info['features']['views']);
}
}
}
foreach ($views as $name => $view) {
if (in_array($name, $commons_views)) {
if ($view->base_table == 'node') {
foreach ($view->display as $did => $display) {
if (isset($display->display_options['filters']['tid'])) {
$views[$name]->display[$did]->display_options['filters']['tid']['vid'] = $vid;
}
if (isset($display->display_options['filters']['vid'])) {
$views[$name]->display[$did]->display_options['filters']['vid']['value'] = array(
$vid => $vid,
);
}
}
}
}
}
}
function commons_core_wysiwyg_editor_settings_alter(&$settings, $context) {
if ($context['editor']['name'] == 'ckeditor') {
$settings['width'] = '95%';
}
}
function commons_core_mollom_form_list() {
$forms['commons_core_join_form'] = array(
'title' => t('Join & learn more form'),
'entity' => 'user',
);
return $forms;
}
function commons_core_mollom_form_info($form_id) {
switch ($form_id) {
case 'commons_core_join_form':
return user_mollom_form_info('user_register');
}
}
function commons_core_force_login() {
global $user;
if ($user->uid) {
return;
}
if (php_sapi_name() == 'cli') {
return;
}
if (str_replace(base_path(), '', $_SERVER['SCRIPT_NAME']) != 'index.php') {
return;
}
if (variable_get('site_offline', 0)) {
return;
}
if (!variable_get('commons_force_login', 0)) {
return;
}
if (arg(0) != 'user' || is_numeric(arg(1))) {
$path = filter_xss($_GET['q']);
if ($allowed = variable_get('commons_force_login_allowed_paths', FALSE)) {
$path_alias = drupal_get_path_alias($path);
if (drupal_match_path($path, $allowed)) {
return;
}
if ($path != $path_alias && drupal_match_path($path_alias, $allowed)) {
return;
}
}
drupal_goto('user', $path ? "destination={$path}" : NULL);
}
}
function commons_core_set_node_tab_title($node) {
if (og_is_group_type($node->type)) {
return variable_get('commons_group_tab_title', t('Content'));
}
return t('View');
}
function commons_core_is_community_manager($user = NULL) {
if (!$user) {
global $user;
}
return array_search('community manager', $user->roles);
}
function commons_core_set_breadcrumb($path) {
$breadcrumb = array();
$path = explode('/', $path);
switch ($path[0]) {
case 'taxonomy':
$breadcrumb[] = l(t('Tags'), 'tagadelic');
break;
case 'content':
if ($path[1]) {
$breadcrumb[] = l(t('Community'), 'groups');
}
break;
case 'notices':
case 'featured':
case 'userpoints':
case 'users':
case 'analytics':
case 'tagadelic':
$breadcrumb[] = l(t('Community'), 'groups');
break;
case 'groups':
if ($path[1]) {
$breadcrumb[] = l(t('Groups'), 'groups');
}
break;
case 'group':
case 'bookmarks':
case 'relationships':
case 'myuserpoints':
case 'mycontent':
$breadcrumb[] = l(t('My account'), 'user');
break;
case 'node':
if (isset($path[1]) && is_numeric($path[1])) {
if ($node = menu_get_object('node')) {
if (!empty($node->og_groups)) {
$item = menu_get_item();
$item['href'] = 'community';
menu_set_item(NULL, $item);
}
if (og_is_group_type($node->type)) {
$breadcrumb[] = l(t('Groups'), 'groups');
}
else {
switch ($node->type) {
case 'notice':
$breadcrumb[] = l(t('Notices'), 'notices');
break;
}
}
}
if (isset($path[2])) {
switch ($path[2]) {
case 'content':
case 'aggregator':
$group = node_load($path[1]);
$breadcrumb[] = l(t('Groups'), 'groups');
$breadcrumb[] = l($group->title, 'node/' . $group->nid);
break;
}
}
}
break;
case 'og':
if (isset($path[2]) && is_numeric($path[2])) {
switch ($path[1]) {
case 'users':
case 'search':
case 'subscribe':
$group = node_load($path[2]);
$breadcrumb[] = l(t('Groups'), 'groups');
$breadcrumb[] = l($group->title, 'node/' . $group->nid);
}
}
break;
}
if (!empty($breadcrumb)) {
array_unshift($breadcrumb, l(t('Home'), '<front>'));
drupal_set_breadcrumb($breadcrumb);
}
}
function commons_core_profile_redirect() {
global $user;
drupal_goto("user/{$user->uid}");
}
function _commons_core_check_group_sticky($node) {
global $user;
if ($node->field_group_sticky[0]['value'] == 'Sticky') {
$selected_groups = array();
if (isset($node->og_groups)) {
foreach ($node->og_groups as $gid) {
if ($gid) {
$selected_groups[] = $gid;
}
}
}
$old_node = node_load($node->nid);
if ($old_node->field_group_sticky[0]['value'] != 'Sticky') {
if (!empty($selected_groups)) {
if (!user_access('administer nodes') && !commons_core_is_community_manager($user)) {
foreach ($selected_groups as $gid) {
if (!og_is_group_admin(node_load($gid), $user)) {
form_set_error('og_groups', t('You can only make posts sticky for the groups that you are an admin for.'));
}
}
}
}
}
if (empty($selected_groups)) {
form_set_error('field_group_sticky', t('You must specify a group in order to make this post group sticky.'));
}
}
}
function _commons_core_check_group_title($node) {
if (variable_get('commons_force_unique_groups', 1)) {
if (og_is_group_type($node->type)) {
$sql = "SELECT n.nid FROM {node} n INNER JOIN {og} o ON o.nid = n.nid WHERE n.title = '%s' AND n.nid != %d";
$existing = db_fetch_object(db_query($sql, $node->title, $node->nid));
if ($existing) {
form_set_error('title', t('A group already exists with that name. Please choose another.'));
}
}
}
}
function commons_core_noderead_action(&$node) {
global $user;
if ($user->uid) {
db_query("DELETE FROM {history} WHERE nid = %d AND uid = %d", $node->nid, $user->uid);
$record = new stdClass();
$record->nid = $node->nid;
$record->uid = $user->uid;
$record->timestamp = time();
drupal_write_record('history', $record);
}
}
function commons_core_nodeunread_action(&$node) {
global $user;
if ($user->uid) {
db_query("DELETE FROM {history} WHERE nid = %d AND uid = %d", $node->nid, $user->uid);
}
}
function _commons_core_can_admin_comment($comment) {
global $user;
if (variable_get('commons_group_admin_edit_comments', 0)) {
if ($groups = og_get_node_groups(node_load($comment->nid))) {
foreach ($groups as $id => $group) {
if (og_is_group_admin(node_load($id), $user)) {
return TRUE;
}
}
}
}
return FALSE;
}