View source
<?php
include_once 'commons_groups.features.inc';
function commons_groups_og_permission_alter(&$perms) {
$perms['subscribe']['title'] = t('Contribute to the group');
$perms['subscribe']['description'] = t('This value is set automatically based on the "Group Privacy Settings" field.');
$perms['subscribe without approval']['title'] = t('Contribute to the group without approval');
$perms['subscribe without approval']['description'] = t('This value is set automatically based on the "Group Privacy Settings" field.');
}
function commons_groups_form_og_ui_admin_permissions_alter(&$form, &$form_state) {
$hidden_permissions = array(
'subscribe',
'subscribe without approval',
);
foreach ($hidden_permissions as $permission) {
if (isset($form['permission'][$permission])) {
$form['permission'][$permission]['#markup'] .= ' ' . t('<strong>Disabled by the Commons Groups module.<strong>');
}
foreach ($form['checkboxes'] as $index => $elements) {
if (isset($elements['#options'][$permission])) {
unset($form['checkboxes'][$index]['#options'][$permission]);
}
}
}
}
function commons_groups_ctools_plugin_directory($module, $plugin) {
if ($module == 'entityreference') {
return "plugins/entityreference/{$plugin}";
}
}
function commons_groups_modules_enabled($modules) {
if (in_array('og_access', $modules)) {
features_revert(array(
'commons_groups' => array(
'field_base',
),
));
features_revert(array(
'commons_groups' => array(
'field_instance',
),
));
}
}
function commons_groups_help($path, $arg) {
if (variable_get('commons_groups_needs_update', FALSE)) {
$message = '<p>' . t("Drupal Commons 3.3 added a new, required field to control group privacy. Please edit your group(s) select one of the privacy options. Once all groups are\n set, an administrator can dismiss the update notice.") . '</p>';
if ($path == 'admin/content/groups/update') {
return $message;
}
elseif ($arg[0] == 'node' && $arg[2] == 'edit') {
$node = menu_get_object();
if ($node->type == 'group' && empty($node->field_og_subscribe_settings)) {
return $message;
}
}
if (user_access('edit any group content')) {
$message = t("Group privacy settings !updated.", array(
'!updated' => l('need to be updated', 'admin/content/groups/update'),
));
drupal_set_message($message, 'warning', FALSE);
}
}
}
function commons_groups_entity_view($entity, $type, $view_mode, $langcode) {
if ($view_mode == 'full' && !empty($entity->og_group_ref[LANGUAGE_NONE][0]['target_id']) && $type != 'user') {
$breadcrumb = array();
$breadcrumb[] = l(t('Home'), NULL);
$breadcrumb[] = l(t('Groups'), 'groups');
$group = node_load($entity->og_group_ref[LANGUAGE_NONE][0]['target_id']);
if (node_access('view', $group)) {
$breadcrumb[] = l($group->title, 'node/' . $group->nid);
}
drupal_set_breadcrumb($breadcrumb);
}
}
function commons_groups_form_node_form_alter(&$form, &$form_state, $form_id) {
$node = $form['#node'];
list(, , $bundle) = entity_extract_ids('node', $node);
$group_content_entity_types = commons_groups_get_group_content_entity_types();
if (isset($group_content_entity_types['node'][$bundle])) {
$form['actions']['submit']['#submit'][] = 'commons_groups_node_in_group_submit';
}
if (!empty($form['group_content_access']['#access'])) {
$form['group_content_access']['#access'] = FALSE;
}
$groups = og_get_all_group_bundle();
if (isset($groups['node']) && in_array($bundle, array_keys($groups['node']))) {
$form['field_og_subscribe_settings'][LANGUAGE_NONE]['#required'] = FALSE;
if (module_exists('og_access')) {
$form['field_og_access_default_value']['#states'] = array(
'visible' => array(
':input[name="field_og_subscribe_settings[' . LANGUAGE_NONE . ']"]' => array(
'value' => 'approval',
),
),
);
$form['#after_build'] = array(
'commons_groups_form_group_node_after_build',
);
}
$form['#attached']['css'][] = drupal_get_path('module', 'commons_groups') . '/css/commons_groups.css';
$form['group_access'][LANGUAGE_NONE]['#required'] = FALSE;
$form['group_access']['#access'] = FALSE;
}
}
function commons_groups_form_group_node_after_build($form, $form_state) {
$form['field_og_subscribe_settings'][LANGUAGE_NONE]['approval']['#suffix'] = render($form['field_og_access_default_value']);
return $form;
}
function _commons_groups_update_group_permissions($role, $permissions) {
$updated_roles =& drupal_static(__FUNCTION__);
if (!empty($updated_roles[$role->rid])) {
return;
}
if (!empty($permissions['subscribe without approval'])) {
$subscribe_type = 'anyone';
}
elseif (!empty($permissions['subscribe'])) {
$subscribe_type = 'approval';
}
else {
$subscribe_type = 'invitation';
}
$wrapper = entity_metadata_wrapper($role->group_type, $role->gid);
if ($wrapper->field_og_subscribe_settings
->value() != $subscribe_type) {
$updated_roles[$role->rid] = TRUE;
$wrapper->field_og_subscribe_settings
->set($subscribe_type);
$wrapper
->save();
}
}
function commons_groups_menu_alter(&$items) {
if (isset($items['node/%/group'])) {
$items['node/%/group']['title'] = t('Administer group');
}
}
function commons_groups_menu() {
$items['admin/content/groups/update/toggle'] = array(
'title' => 'Toggle Groups Update',
'page callback' => 'commons_groups_update_toggle',
'access arguments' => array(
'edit any group content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function commons_groups_update_toggle() {
variable_set('commons_groups_needs_update', FALSE);
return TRUE;
}
function commons_groups_block_info() {
$blocks['commons_groups_create_group'] = array(
'info' => t('"Create a group" call to action'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function commons_groups_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'commons_groups_create_group':
if (node_access('create', 'group')) {
$block['subject'] = NULL;
$block['content'] = array(
'#type' => 'link',
'#title' => t('Create a group'),
'#href' => 'node/add/group',
);
}
break;
}
return $block;
}
function commons_groups_views_pre_view(&$view, &$display_id, &$args) {
if (isset($view->display_handler->options['arguments']['gid']) && empty($args)) {
if (isset($view->display_handler->options['filters']['group_type'])) {
$filters = $view->display_handler
->get_option('filters');
unset($filters['group_type']);
$view->display_handler
->override_option('filters', $filters);
}
}
}
function commons_groups_group_contributors_count_topics($group) {
$output = '';
$view = views_get_view('commons_contributors_group');
if (!empty($view)) {
$view
->set_display('panel_pane_1');
$view
->set_arguments(array(
$group->nid,
));
$view->get_total_rows = TRUE;
$view
->execute();
if (empty($view->total_rows)) {
return '';
}
$contributors_count = $view->total_rows;
$output .= l(format_plural($contributors_count, '1 contributor', '@count contributors'), 'node/' . $group->nid . '/contributors');
}
if (!empty($group->field_topics[LANGUAGE_NONE])) {
foreach ($group->field_topics[LANGUAGE_NONE] as $item) {
$tids[] = $item['tid'];
}
$topics = taxonomy_term_load_multiple($tids);
$topics_text = ' discussing the @topics ';
$t_args = array(
'@topics' => format_plural(count($topics), 'topic', 'topics'),
);
foreach ($topics as $topic) {
$topics_text .= '!topic-' . $topic->tid;
if ($topic == end($topics)) {
$topics_text .= '.';
}
else {
$topics_text .= ', ';
}
$t_args['!topic-' . $topic->tid] = l(t($topic->name), 'taxonomy/term/' . $topic->tid);
}
$output .= t($topics_text, $t_args);
}
return $output;
}
function commons_groups_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
$group = $implementations['commons_groups'];
unset($implementations['commons_groups']);
$implementations['commons_groups'] = $group;
}
}
function commons_groups_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form' && strstr($form['#id'], 'views-exposed-form-commons-groups-directory')) {
$form['groups-keys']['#attributes'] = array(
'placeholder' => t('Separate keywords with commas'),
);
}
if (strstr($form_id, 'views_form_commons_group_moderation_page')) {
$form['select']['action::views_bulk_operations_delete_item']['#weight'] = 9;
}
if ($form_id == 'group_node_form' && is_null($form['nid']['#value'])) {
$form['actions']['submit']['#submit'][] = 'commons_groups_group_submission_message';
}
if (in_array($form_id, array(
'og_ui_admin_global_permissions',
'og_ui_admin_permissions',
))) {
$group_content_entity_types = commons_groups_get_group_content_entity_types();
if (!empty($group_content_entity_types)) {
$message = 'In addition to the permissions listed here, the Commons Groups module grants non-group members the ability to post content into groups where content in the group is public.';
drupal_set_message(t($message), 'warning');
}
}
if ($form_id == 'edit_profile_user_profile_form' || substr($form_id, -10) === '_node_form') {
$internal_fields = array(
'field_unread_invitations',
'field_unread_messages',
'user_trusted_contacts',
'og_user_group_ref',
'group_access',
);
foreach ($internal_fields as $field_name) {
if (isset($form[$field_name])) {
$form[$field_name]['#access'] = FALSE;
}
}
}
$group_content_entity_types = commons_groups_get_group_content_entity_types();
if (isset($form['#node']->type) && isset($group_content_entity_types['node'][$form['#node']->type])) {
if (isset($form['og_user_group_ref']) && !module_exists('commons_trusted_contacts')) {
$form['og_user_group_ref']['#access'] = FALSE;
}
}
}
function commons_groups_node_in_group_submit(&$form, &$form_state) {
if (isset($form_state['values']['og_group_ref'][LANGUAGE_NONE][0])) {
$group = $form_state['values']['og_group_ref'][LANGUAGE_NONE][0]['target_id'];
$form_state['redirect'] = 'node/' . $group;
}
}
function commons_groups_system_info_alter(&$info, $file, $type) {
if ($file->name == 'commons_groups') {
$group_bundles = og_get_all_group_bundle();
if (!empty($group_bundles['node'])) {
foreach ($group_bundles['node'] as $bundle => $name) {
$info['features']['field_instance'][] = "node-{$bundle}-field_og_access_default_value";
$info['features']['field_instance'][] = "node-{$bundle}-field_og_subscribe_settings";
$info['features']['field_instance'][] = "node-{$bundle}-og_roles_permissions";
$info['features']['field_instance'][] = "node-{$bundle}-body";
$info['features']['field_instance'][] = "node-{$bundle}-group_group";
$info['features']['field_instance'][] = "node-{$bundle}-group_access";
$info['features']['field_instance'][] = "node-{$bundle}-field_group_logo";
$info['features']['variable'][] = "comment_anonymous_{$bundle}";
$info['features']['variable'][] = "comment_default_mode_{$bundle}";
$info['features']['variable'][] = "comment_default_per_page_{$bundle}";
$info['features']['variable'][] = "comment_form_location_{$bundle}";
$info['features']['variable'][] = "comment_{$bundle}";
$info['features']['variable'][] = "comment_preview_{$bundle}";
$info['features']['variable'][] = "comment_subject_field_{$bundle}";
$info['features']['variable'][] = "field_bundle_settings_node__{$bundle}";
}
}
$group_content_entity_types = commons_groups_get_group_content_entity_types();
if (!empty($group_content_entity_types)) {
foreach ($group_content_entity_types as $entity_type => $bundles) {
foreach (array_keys($bundles) as $bundle) {
$info['features']['field_instance'][] = "{$entity_type}-{$bundle}-og_group_ref";
$info['features']['field_instance'][] = "{$entity_type}-{$bundle}-group_content_access";
}
}
}
$commons_groups = commons_groups_get_group_types();
if (isset($commons_groups['node'])) {
foreach ($commons_groups['node'] as $bundle => $group_info) {
$info['features']['variable'][] = "node_options_{$bundle}";
$info['features']['variable'][] = "node_preview_{$bundle}";
$info['features']['variable'][] = "node_submitted_{$bundle}";
$info['features']['variable'][] = "og_group_manager_default_rids_node_{$bundle}";
}
}
}
$commons_entity_integrations =& drupal_static(__FUNCTION__);
if (!isset($commons_entity_integrations)) {
foreach (module_implements('commons_entity_integration') as $module) {
$commons_entity_integrations[$module] = call_user_func($module . '_commons_entity_integration');
}
}
if (isset($commons_entity_integrations[$file->name])) {
foreach ($commons_entity_integrations[$file->name] as $entity_type => $integration) {
foreach ($integration as $bundle => $options) {
if (commons_groups_is_group_content($entity_type, $bundle)) {
$info['features_exclude']['dependencies']['commons_groups'] = 'commons_groups';
}
}
}
}
}
function commons_groups_default_message_type_alter(&$defaults) {
foreach (array(
'commons_activity_streams_comment_created',
'commons_activity_streams_node_created',
) as $name) {
if (!empty($defaults[$name])) {
$defaults[$name]->message_text[LANGUAGE_NONE][2] = commons_groups_message_partial_default();
}
}
}
function commons_groups_og_user_access_alter(&$perm, $context) {
$account = $context['account'];
$group_type = $context['group_type'];
$group = $context['group'];
if ($group_type != 'node') {
return;
}
if (og_is_member($group_type, $group->nid, 'user', $account, array(
OG_STATE_ACTIVE,
OG_STATE_PENDING,
OG_STATE_BLOCKED,
))) {
return;
}
$wrapper = entity_metadata_wrapper($group_type, $group);
$access_create = $account->uid && $wrapper->field_og_subscribe_settings
->value() == 'anyone';
$commons_groups_entity_types = commons_groups_get_group_content_entity_types();
foreach (array_keys($commons_groups_entity_types['node']) as $type) {
$perm["create {$type} content"] = $access_create;
}
}
function commons_groups_token_info() {
$types = array();
$tokens = array();
$types['commons-groups'] = array(
'name' => t('Commons Groups'),
'description' => t('Tokens related to the Groups functionality in Drupal Commons.'),
'needs-data' => 'node',
);
$tokens['commons-groups']['in-groups-text'] = array(
'name' => t('"In groups" text'),
'description' => t('The text (starting with "in the groups") indicating which groups a piece of content belongs to.'),
);
$tokens['node']['commons-groups-first-group'] = array(
'name' => t('First Group'),
'description' => t('First group associated with a piece of content. Useful for path aliases'),
);
$tokens['node']['commons-groups-group-contributors-count-topics'] = array(
'name' => t('Commons Groups: Group contributor count and topics'),
'description' => t('Displays text showing the number of contributors and the topics associated with a group node.'),
);
return array(
'types' => $types,
'tokens' => $tokens,
);
}
function commons_groups_tokens($type, $tokens, $data = array(), $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node'])) {
$group = $data['node'];
foreach ($tokens as $name => $original) {
if ($name == 'commons-groups-group-contributors-count-topics') {
$replacements[$original] = commons_groups_group_contributors_count_topics($group);
return $replacements;
}
}
}
if ($type == 'commons-groups') {
if (!empty($tokens['in-groups-text'])) {
$text = '';
$target_nids = array();
$related_groups = array();
$related_gids = array();
foreach ($data['message']->field_target_nodes[LANGUAGE_NONE] as $key => $value) {
$target_nids[] = $value['target_id'];
}
if (empty($target_nids)) {
$replacements['[commons-groups:in-groups-text]'] = $text;
return $replacements;
}
foreach ($target_nids as $key => $nid) {
$og_memberships_this_target = og_get_entity_groups('node', $nid);
if (!empty($og_memberships_this_target['node'])) {
$og_memberships_this_target = $og_memberships_this_target['node'];
foreach ($og_memberships_this_target as $membership_id => $gid) {
$related_gids[] = $gid;
}
}
}
if (empty($related_gids)) {
$replacements['[commons-groups:in-groups-text]'] = '';
return $replacements;
}
$related_groups = entity_load('node', $related_gids);
$related_groups = array_values($related_groups);
$replacements['[commons-groups:in-groups-text]'] = commons_groups_related_groups_text($related_groups);
return $replacements;
}
}
if ($type == 'node' && !empty($data['node'])) {
if (!empty($tokens['commons-groups-first-group'])) {
$group = $data['node'];
$text = '';
if (!empty($group->og_group_ref[LANGUAGE_NONE])) {
$wrapper = entity_metadata_wrapper('node', $group);
$groups = $wrapper->og_group_ref
->value();
$first_group = array_shift($groups);
$first_group_wrapper = entity_metadata_wrapper('node', $first_group);
$text = isset($first_group_wrapper->title_field) ? $first_group_wrapper->title_field
->value() : $first_group_wrapper
->label();
}
$replacements['[node:commons-groups-first-group]'] = $text;
return $replacements;
}
}
}
function commons_groups_message_partial_default() {
$partial = array(
'value' => '[commons-groups:in-groups-text]',
'format' => 'full_html',
'safe_value' => '[commons-groups:in-groups-text]',
);
return $partial;
}
function commons_groups_related_groups_text($related_groups) {
if (count($related_groups) == 1) {
return t(' in the !group group', array(
'!group' => l($related_groups[0]->title, 'node/' . $related_groups[0]->nid),
));
}
if (count($related_groups) == 2) {
return t(' in the !group-0 and !group-1 groups', array(
'!group-0' => l($related_groups[0]->title, 'node/' . $related_groups[0]->nid),
'!group-1' => l($related_groups[1]->title, 'node/' . $related_groups[1]->nid),
));
}
if (count($related_groups) > 2) {
$last_group = array_pop($related_groups);
$text = ' in the ';
foreach ($related_groups as $key => $this_group) {
$text .= "!group-{$key}, ";
$t_args["!group-{$key}"] = l($this_group->title, 'node/' . $this_group->nid);
}
$text .= " and !group-{$last_group->nid} groups.";
$t_args["!group-{$last_group->nid}"] = l($last_group->title, 'node/' . $last_group->nid);
return t($text, $t_args);
}
}
function commons_groups_group_submission_message($form, &$form_state) {
if ($form_state['values']['status'] !== 1) {
drupal_set_message(t('Thanks for your group submission! This group has entered the moderation queue and will be reviewed shortly.'));
}
}
function commons_groups_entityreference_default_value($entity_type, $entity, $field, $instance, $langcode) {
$items = array();
$field_name = $field['field_name'];
if (empty($_GET[$field_name]) || !is_string($_GET[$field_name])) {
return $items;
}
if (empty($instance['settings']['behaviors']['prepopulate']['status'])) {
return $items;
}
$ids = explode(',', $_GET[$field_name]);
$target_type = $field['settings']['target_type'];
entity_load($target_type, $ids);
foreach ($ids as $target_id) {
$target = entity_load_single($target_type, $target_id);
if (entity_access('view', $target_type, $target) && og_is_group_type($target_type, $target->type) && (og_user_access($target_type, $target_id, "create {$entity->type} content") || og_user_access($target_type, $target_id, "update any {$entity->type} content"))) {
$items[] = array(
'target_id' => $target_id,
);
}
}
return $items;
}
function commons_groups_strongarm_alter(&$items) {
if (isset($items['commons_radioactivity_entity_types'])) {
$items['commons_radioactivity_entity_types']->value['node']['group'] = 1;
}
}
function commons_groups_default_rules_configuration_alter(&$configs) {
if (isset($configs['rules_og_member_active'])) {
$configs['rules_og_member_active']->active = FALSE;
}
}
function commons_groups_node_presave($node) {
if (!module_exists('og_access')) {
return;
}
$wrapper = entity_metadata_wrapper('node', $node);
if (og_is_group('node', $node)) {
$private = $wrapper->field_og_subscribe_settings
->value() == 'invitation';
$wrapper->{OG_ACCESS_FIELD}
->set((int) $private);
return;
}
if (!og_is_group_content_type('node', $node->type)) {
return;
}
$private = FALSE;
foreach (array_keys(og_get_group_audience_fields('node', $node->type)) as $field) {
if (empty($node->{$field})) {
continue;
}
foreach ($wrapper->{$field} as $group_wrapper) {
if (empty($group_wrapper->field_og_access_default_value)) {
continue;
}
if ($group_wrapper->field_og_access_default_value
->value() == TRUE) {
$private = TRUE;
break 2;
}
}
}
if ($private) {
$wrapper->{OG_CONTENT_ACCESS_FIELD}
->set(OG_CONTENT_ACCESS_PRIVATE);
}
}
function commons_groups_node_update($node) {
$account = user_load($node->uid);
commons_groups_first_contribution($account, $node);
if (og_is_group('node', $node)) {
commons_groups_set_group_permissions($node);
}
}
function commons_groups_node_insert($node) {
$account = user_load($node->uid);
commons_groups_first_contribution($account, $node);
if (og_is_group('node', $node)) {
og_roles_override('node', $node->type, $node->nid);
commons_groups_set_group_permissions($node);
}
}
function commons_groups_set_group_permissions($node) {
$updated_nodes =& drupal_static(__FUNCTION__);
if (!empty($updated_nodes[$node->nid])) {
return;
}
$updated_nodes[$node->nid] = TRUE;
$wrapper = entity_metadata_wrapper('node', $node);
$permission = $wrapper->field_og_subscribe_settings
->value();
$og_roles = og_roles('node', $node->type, $node->nid);
$anon_rid = array_search(OG_ANONYMOUS_ROLE, $og_roles);
$permissions = array(
'subscribe' => $permission == 'approval',
'subscribe without approval' => $permission == 'anyone',
);
$changed = FALSE;
$old_permissions = og_role_permissions(array(
$anon_rid => OG_ANONYMOUS_ROLE,
));
foreach ($permissions as $permission => $value) {
if (empty($old_permissions[$anon_rid][$permission]) || $old_permissions[$anon_rid][$permission] != $value) {
$changed = TRUE;
}
}
if ($changed) {
og_role_change_permissions($anon_rid, $permissions);
}
}
function commons_groups_is_group_content($entity_type, $bundle) {
$commons_groups_entity_types = commons_groups_get_group_content_entity_types();
return isset($commons_groups_entity_types[$entity_type][$bundle]['is_group_content']) ? $commons_groups_entity_types[$entity_type][$bundle]['is_group_content'] : FALSE;
}
function commons_groups_get_group_content_entity_types($cache = TRUE) {
$commons_entity_integrations = commons_entity_integration_info(NULL, $cache);
if (empty($commons_entity_integrations)) {
return array();
}
foreach ($commons_entity_integrations as $entity_type => $integration) {
foreach ($integration as $bundle => $options) {
if (isset($options['is_group_content']) && $options['is_group_content'] == FALSE) {
unset($commons_entity_integrations[$entity_type][$bundle]);
}
}
if (empty($commons_entity_integrations[$entity_type])) {
unset($commons_entity_integrations[$entity_type]);
}
}
return $commons_entity_integrations;
}
function commons_groups_get_group_types() {
$commons_groups = array();
$commons_entity_integrations = commons_entity_integration_info();
if (empty($commons_entity_integrations)) {
return array();
}
foreach ($commons_entity_integrations as $entity_type => $integration) {
foreach ($integration as $bundle => $options) {
if (isset($options['is_group']) && $options['is_group'] == TRUE) {
$commons_groups[$entity_type][$bundle] = $commons_entity_integrations[$entity_type][$bundle];
}
}
}
return $commons_groups;
}
function commons_groups_first_contribution($account, $node) {
$groups = og_get_entity_groups('node', $node);
if (!empty($groups['node'])) {
$node_groups = array_values($groups['node']);
$account_groups = og_get_groups_by_user($account, 'node');
if (!$account_groups) {
$account_groups = array();
}
if ($account->uid == 0) {
return;
}
$new_groups = array_diff($node_groups, $account_groups);
if (!empty($new_groups)) {
foreach ($new_groups as $new_group_nid) {
og_group('node', $new_group_nid, array(
'entity' => $account->uid,
));
}
}
}
}
function commons_groups_preprocess_node(&$variables) {
$variables['user_picture'] = '';
if (variable_get('user_pictures', 0)) {
$node = $variables['node'];
$account = user_load($node->uid);
if (!empty($account->picture)) {
if (is_numeric($account->picture)) {
$account->picture = file_load($account->picture);
}
if (!empty($account->picture->uri)) {
$filepath = $account->picture->uri;
}
}
elseif (variable_get('user_picture_default', '')) {
$filepath = variable_get('user_picture_default', '');
}
if (isset($filepath)) {
if (module_exists('image') && file_valid_uri($filepath)) {
$alt = t("@user's picture", array(
'@user' => format_username($account),
));
$render = array(
'#theme' => 'image_formatter',
'#image_style' => '50x50',
'#item' => array(
'uri' => $filepath,
'alt' => $alt,
),
'#path' => array(
'path' => 'user/' . $account->uid,
'options' => array(
'attributes' => array(
'title' => t("View @user's profile.", array(
'@user' => format_username($account),
)),
'class' => array(
'user-picture',
),
),
),
),
);
$variables['user_picture'] = drupal_render($render);
}
}
}
}
function commons_groups_preprocess_views_view_list(&$variables, $hook) {
if ($variables['view']->name == 'commons_contributors_group' && !empty($variables['title']) && $variables['title'] == 'administrator member') {
$variables['title'] = t('Organizers');
}
}
function commons_groups_field_access($op, $field, $entity_type, $entity, $account) {
$field_name = $field['field_name'];
switch ($field_name) {
case 'og_roles_permissions':
return FALSE;
case 'field_og_access_default_value':
return $op == 'edit' && module_exists('og_access');
case 'field_og_subscribe_settings':
return $op == 'edit';
}
if (module_exists('og_access') && in_array($field_name, array(
OG_CONTENT_ACCESS_FIELD,
OG_ACCESS_FIELD,
))) {
return FALSE;
}
}
function commons_groups_field_formatter_info() {
return array(
'commons_groups_group_subscribe' => array(
'label' => t('Commons groups subscribe link'),
'field types' => array(
'list_boolean',
),
'settings' => array(
'field_name' => FALSE,
),
),
);
}
function commons_groups_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
global $user;
$account = clone $user;
if ($display['type'] != 'commons_groups_group_subscribe') {
return;
}
if (!og_is_group($entity_type, $entity)) {
return;
}
if (!empty($entity->uid) && $entity->uid == $account->uid) {
$element[0] = array(
'#markup' => t('You are the group manager'),
);
return $element;
}
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (og_is_member($entity_type, $id, 'user', $account, array(
OG_STATE_PENDING,
))) {
$element[0] = array(
'#markup' => '<div class="subscription-type">' . t('Your membership request is pending review by a group organizer.') . '</div>',
);
return $element;
}
if (og_is_member($entity_type, $id, 'user', $account, array(
OG_STATE_BLOCKED,
))) {
return;
}
if (og_is_member($entity_type, $id, 'user', $account)) {
if (og_user_access($entity_type, $id, 'unsubscribe', $account)) {
if (og_is_member($entity_type, $id, 'user', $account, array(
OG_STATE_ACTIVE,
)) && $entity->field_og_subscribe_settings[LANGUAGE_NONE][0]['value'] != 'anyone') {
$links['title'] = t('Leave group');
$links['href'] = "group/{$entity_type}/{$id}/unsubscribe";
}
}
}
else {
if (empty($settings['field_name']) && ($audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle))) {
$settings['field_name'] = $audience_field_name;
}
if (!$settings['field_name']) {
return;
}
$field_info = field_info_field($settings['field_name']);
if ($field_info['settings']['target_type'] != $entity_type) {
return;
}
if (!empty($field_info['settings']['handler_settings']['target_bundles']) && !in_array($bundle, $field_info['settings']['handler_settings']['target_bundles'])) {
return;
}
if (!og_check_field_cardinality('user', $account, $settings['field_name'])) {
$element[0] = array(
'#markup' => format_plural($field_info['cardinality'], 'You are already registered to another group', 'You are already registered to @count groups'),
);
return $element;
}
$url = "group/{$entity_type}/{$id}/subscribe";
if ($settings['field_name']) {
$url .= '/' . $settings['field_name'];
}
if (empty($entity->field_og_subscribe_settings)) {
if (!variable_get('commons_groups_needs_update', FALSE)) {
variable_set('commons_groups_needs_update', TRUE);
}
}
else {
if ($entity->field_og_subscribe_settings[LANGUAGE_NONE][0]['value'] != 'anyone') {
if ($entity->field_og_subscribe_settings[LANGUAGE_NONE][0]['value'] == 'approval') {
$subscription_type = t('Moderated group');
$links['title'] = t('Join group');
if ($account->uid) {
$links['href'] = $url;
}
else {
$links['href'] = 'user/login';
$links['options'] = array(
'query' => array(
'destination' => $url,
),
);
}
}
else {
$element[0] = array(
'#markup' => '<div class="subscription-type">' . t('Invite-only group') . '</div>',
);
return $element;
}
}
}
}
if (!empty($links['title'])) {
$links += array(
'options' => array(),
);
$element[0] = array(
'#type' => 'link',
'#title' => $links['title'],
'#href' => $links['href'],
'#options' => $links['options'],
);
if (!empty($subscription_type)) {
$element[0]['#prefix'] = '<div class="subscription-type">' . $subscription_type . '</div>';
}
return $element;
}
}
function commons_groups_views_pre_render(&$view) {
if (empty($view->args[0]) && $view->name == 'commons_bw_all') {
$view->display_handler->handlers['empty']['area']->options['content'] = t('Nobody has posted yet.');
}
}
function commons_groups_features_permission_rebuild($module, $gid) {
module_load_include('features.inc', 'og', '/includes/og_features_role');
if ($defaults = features_get_default('og_features_permission', $module)) {
drupal_static_reset();
$grant = array();
$revoke = array();
foreach ($defaults as $key => $details) {
list($group_type, $bundle, $perm) = explode(':', $key);
foreach ($details['roles'] as $role) {
$bundle_role = _og_features_role_exists($role, $group_type, $bundle);
if (empty($bundle_role)) {
og_role_save(og_role_create($role, $group_type, $gid, $bundle));
}
}
$roles = og_roles($group_type, $bundle, $gid);
foreach ($roles as $rid => $rolename) {
if (in_array($rolename, $details['roles'])) {
$grant[$rid][] = $perm;
}
else {
$revoke[$rid][] = $perm;
}
}
}
if (!empty($grant)) {
foreach ($grant as $rid => $permissions) {
og_role_grant_permissions($rid, $permissions);
}
}
if (!empty($revoke)) {
foreach ($revoke as $rid => $permissions) {
og_role_revoke_permissions($rid, $permissions);
}
}
}
}