name)); $token_types[$type_admin_name] = $type->name; } if (count($token_types) == 0) { drupal_set_message(t('There are no relationships defined. Please define some user relationships before configuring the activity messages.', array('@user_relationships' => url('admin/user/relationships')))); } return array( 'ops' => array( 'requested' => t('Requested'), 'approved' => t('Approved'), 'denied' => t('Denied'), 'canceled' => t('Canceled'), 'deleted' => t('Deleted') ), 'types' => $token_types, 'roles' => array( // This is what corresponds to ACTIVITY_ALL 'all' => array( '#name' => t('All (approved relationships only)'), '#description' => t('The general public.'), '#default' => array( 'approved' => t('[author-all] is now [relationship-plural] with [requestee-all]'), 'deleted' => t('[author-all] is no longer [relationship-plural] with [requestee-all]'), ), ), 'requester' => array( '#name' => t('Requester'), '#description' => t('The person who initiated the relationship.'), '#default' => array( 'requested' => t('[author] requested to be [relationship-plural] with [requestee]'), 'approved' => t('[requestee] approved the request to be [relationship-plural] with [author]'), 'denied' => t('[requestee] denied the request to be [relationship-plural] with [author]'), 'canceled' => t('[author] cancelled the request to be [relationship-plural] with [requestee]'), 'deleted' => t('[author] [is-are] no longer [relationship-plural] with [requestee]'), ), ), 'requestee' => array( '#name' => t('Requestee'), '#description' => t('The person with whom a relationship has been requested or created.'), '#default' => array( 'requested' => t('[author] requested to be [relationship-plural] with [requestee]'), 'approved' => t('[requestee] approved the request to be [relationship-plural] with [author]'), 'denied' => t('[requestee] denied the request to be [relationship-plural] with [author]'), 'canceled' => t('[author] cancelled the request to be [relationship-plural] with [requestee]'), 'deleted' => t('[author] [is-are] no longer [relationship-plural] with [requestee]'), ), ), ), ); } /** * Token module integration. Defines available default tokens. */ function user_relationshipsactivity_token_list($type = 'all') { if ($type == 'user_relationshipsactivity') { $tokens['user_relationshipsactivity'] = array( 'requestee-uid' => t('User id of the person to whom the request was issued'), 'requestee' => t('Themed username of the person who received the request'), 'requestee-all' => t('Themed username of the person who received the request'), 'requestee-name' => t('The username of the person to whom the request was issued'), 'requestee-picture' => t('The user picture of the person to whom the request was issued'), 'relationship' => t('The name of the relationship'), 'relationship-plural' => t('The plural name of the relationship'), ); return $tokens; } } /** * Token module integration. Defines available default token values. */ function user_relationshipsactivity_token_values($type, $data = NULL, $options = array()) { global $user; static $relationships; if ($type == 'user_relationshipsactivity' && !empty($data)) { $requestee = activity_user_load($data['requestee-uid']); $data['is-are'] = ($data['uid'] == $user->uid) ? 'are' : 'is' ; $data['requestee'] = theme('activity_username', $requestee, TRUE); $data['requestee-all'] = theme('activity_username', $requestee); $data['requestee-name'] = $requestee->name; $data['requestee-picture'] = theme('activity_user_picture', $requestee); if (!isset($relationships[$data['relationship-type-id']])) { $relationships[$data['relationship-type-id']] = user_relationships_type_load($data['relationship-type-id']); } $relationship = $relationships[$data['relationship-type-id']]; $data['relationship'] = $relationship->name; $data['relationship-plural'] = $relationship->plural_name; return $data; } } /** * Implementation of hook_user_relationships(). * * @param $op * The action taken on a user relationship or user relationship type * * @param $relationship * The relationship object. * * @param $category * An optional parameter that when added indicates the 'suboperation' of the operation in $op * * The array of possible $op and $category parameters are: * * $type | $category | Description * ---------------------------------------------------------------------------------------- * insert | type | before a new relationship type is created * update | type | before a relationship type is updated * delete | type | after a relationship type is deleted * load | type | after a relationship type is loaded (so you can add data to it if you'd like) * load | NULL | after a relationship is loaded * pre-save | request|update|approve | before a new relationship is created or updated * post-save | request|update|approve | after a new relationship is created or updated * delete | remove | when a relationship is removed * delete | cancel | when a relationship request is cancelled * delete | disapprove | when a relationship request is disapprove * */ function user_relationshipsactivity_user_relationships($op, $relationship, $category = NULL) { if ($category == 'type' || ($op == 'load' || $op == 'pre-save')) { return; } $relationship = array_merge((array) $relationship, (array) user_relationships_type_load($relationship->rtid)); $type = preg_replace('/\s/', '_', drupal_strtolower($relationship['name'])); switch ($op) { case 'post-save': if ($relationship['approved']) { $operation = 'approved'; $target_users_roles[ACTIVITY_ALL] = 'all'; } else { $operation = 'requested'; } break; case 'delete': switch ($category) { case 'remove': $operation = 'deleted'; $target_users_roles[ACTIVITY_ALL] = 'all'; break; case 'cancel': $operation = 'canceled'; break; case 'disapprove': $operation = 'denied'; break; } break; } // Check if both type and operation are // enabled for activity. If not then stop here if (!in_array($type, variable_get('user_relatinshipsactivity_token_types', array($type)), TRUE) || !in_array($operation, variable_get('user_relationshipsactivity_op_types', array($operation)), TRUE)) { return FALSE; } $user = user_load(array('uid' => $relationship['requester_id'])); // Privacy setting check for user voting if (activity_user_privacy_optout($user)) { return FALSE; } // There is a quirk in user_relationships in that if the $op is // delete and $category is remove, the user that is deleting // the relationship is set as the requestee when they should be // the requester. So let's swap the values in this case. // See http://drupal.org/node/254446 /* if ($op == 'delete' && $category == 'remove') { $requestee = $relationship['requester_id']; $relationship['requester_id'] = $relationship['requestee_id']; $relationship['requestee_id'] = $requestee; } */ $data = array( 'requestee-uid' => $relationship['requestee_id'], 'relationship-type-id' => $relationship['rtid'], ); $target_users_roles[$relationship['requester_id']] = 'requester'; $target_users_roles[$relationship['requestee_id']] = 'requestee'; activity_insert($user->uid, 'user_relationshipsactivity', $type, $operation, $data, $target_users_roles); } /** * Implementation of hook_menu(). */ function user_relationshipsactivity_menu($may_cache) { $items = array(); if ($may_cache) { foreach (user_relationships_types_load() as $type) { $items[] = array( 'path' => 'activity/'. preg_replace('/ /', '_', drupal_strtolower($type->plural_name)), 'title' => t('My !relationship\' activity', array('!relationship' => $type->plural_name)), 'callback' => 'user_relationshipsactivity_page', 'callback arguments' => array($type->rtid), 'type' => MENU_LOCAL_TASK, 'access' => user_access('view own activity'), ); } } return $items; } /** * user_relationships activity page callback */ function user_relationshipsactivity_page($type = NULL) { global $user; drupal_add_css(drupal_get_path('module', 'activity') .'/activity.css'); if ($type) { if (user_access('view own activity')) { $relationships = user_relationships_load( array( 'rtid' => $type, 'user' => $user->uid, 'approved' => 1 ) ); foreach ($relationships as $relationship) { if ($relationship->requester_id == $user->uid) { $uids[] = $relationship->requestee_id; } else { $uids[] = $relationship->requester_id; } } $activities = array(); if ($uids) { $activities = activity_get_activity($uids, NULL, variable_get('activity_page_pager', 20)); } $table = theme('activity_table', $activities); return theme('activity_page', $activities, $table); } } $table = theme('activity_table', array()); return theme('activity_page', array(), $table); } /** * Implementation of hook_block(). */ function user_relationshipsactivity_block($op = 'list', $delta = 0, $edit = array()) { global $user; switch ($op) { case 'list': $blocks = array(); foreach (user_relationships_types_load() as $type) { $blocks[$type->rtid] = array('info' => t("Activity - User Relationships: show activity by current user's !type.", array('!type' => $type->plural_name))); } return $blocks; break; case 'configure': $form['items'] = array( '#type' => 'select', '#title' => t('Number of activity items to display'), '#default_value' => variable_get('user_relationshipsactivity_block_'. $delta, 5), '#options' => drupal_map_assoc(range(1, 50)), ); return $form; break; case 'save': variable_set('user_relationshipsactivity_block_'. $delta, $edit['items']); break; case 'view': $type = user_relationships_type_load($delta); $relationships = user_relationships_load(array('user' => $user->uid, 'rtid' => $delta)); foreach ($relationships as $relationship) { $uids[] = $relationship->requestee_id; } $activity = activity_get_activity($uids, NULL, variable_get('user_relationshipsactivity_block_'. $delta, 5) + 1); drupal_add_css(drupal_get_path('module', 'activity') .'/activity.css'); if (count($activity) > variable_get('user_relationshipsactivity_block_'. $delta, 5)) { $more_link = theme('activity_more_link', 'activity/'. preg_replace('/ /', '_', drupal_strtolower($type->plural_name))); array_pop($activity); } $activities = array(); foreach ($activity as $item) { $activities[] = theme('activity', activity_token_replace($item), $item) . activity_delete_link($item); } if (count($activity)) { return array( 'subject' => t('My !name\' activity', array('!name' => $type->plural_name)), 'content' => theme('activity_block', $activities, $more_link) ); } break; } }