View source
<?php
include_once 'commons_activity_streams.features.inc';
function commons_activity_streams_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-commons-activity-streams-activity-panel-pane-3') {
$form['following']['#options'][0] = t("activity I'm not following");
$form['following']['#options'][1] = t("activity I'm following");
}
if ($form_id == 'edit_profile_user_profile_form') {
static $profile_form_state;
if (!isset($profile_form_state)) {
$profile_form = array_merge(array(), $form);
$profile_form_state = array_merge(array(), $form_state);
drupal_prepare_form('edit_profile_user_profile_form', $profile_form, $profile_form_state);
drupal_process_form('edit_profile_user_profile_form', $profile_form, $profile_form_state);
$form_state['storage']['values'] = $profile_form_state['values'];
}
$form['#submit'][] = 'commons_activity_streams_user_profile_submit';
}
}
function commons_activity_streams_node_insert($node) {
$account = user_load($node->uid);
$hook = 'node_insert';
$message_type = 'commons_activity_streams_node_created';
drupal_alter('commons_activity_streams_message_selection', $message_type, $hook, $node);
$message = message_create($message_type, array(
'uid' => $account->uid,
'timestamp' => $node->created,
));
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_target_nodes[] = $node;
$wrapper
->save();
}
function commons_activity_streams_comment_insert($comment) {
$account = user_load($comment->uid);
$node = node_load($comment->nid);
$hook = 'comment_insert';
$message_type = 'commons_activity_streams_comment_created';
drupal_alter('commons_activity_streams_message_selection', $message_type, $hook, $node);
$message = message_create($message_type, array(
'uid' => $account->uid,
'timestamp' => $comment->created,
));
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_target_nodes[] = $node;
$wrapper->field_target_comments[] = $comment;
$wrapper
->save();
}
function commons_activity_streams_comment_delete($comment) {
if ($mids = commons_activity_streams_existing_messages($comment->uid, array(
$comment->cid,
), 'field_target_comments', 'commons_activity_streams_comment_created')) {
message_delete_multiple($mids);
}
}
function commons_activity_streams_message_access_alter(&$access, $context) {
if ($context['op'] != 'view') {
return;
}
$message = $context['entity'];
if (isset($message->field_target_nodes)) {
foreach ($message->field_target_nodes[LANGUAGE_NONE] as $key => $value) {
$node = node_load($value['target_id']);
if (!node_access('view', $node, $context['account'])) {
$access = FALSE;
return;
}
}
}
if (isset($message->field_target_comments)) {
foreach ($message->field_target_comments[LANGUAGE_NONE] as $key => $value) {
$comment = comment_load($value['target_id']);
if (!entity_access('view', 'comment', $comment, $context['account'])) {
$access = FALSE;
return;
}
}
}
}
function commons_activity_streams_existing_messages($acting_uid, $target_ids, $target_field, $message_type) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'message', '=')
->propertyCondition('uid', $acting_uid)
->propertyCondition('type', $message_type, '=')
->fieldCondition($target_field, 'target_id', $target_ids, 'IN')
->execute();
if (!empty($query->ordered_results)) {
$mids = array();
foreach ($query->ordered_results as $result) {
$mids[] = $result->entity_id;
}
return $mids;
}
return FALSE;
}
function commons_activity_streams_user_profile_submit($form, &$form_state) {
global $user;
$remove_keys = array(
'uid',
'name',
'pass',
'current_pass_required_values',
'current_pass',
'status',
'roles',
'notify',
'signature',
'picture_delete',
'message_subscribe_email',
'og_user_node',
'cancel',
'metatags',
'timezone',
'signature_format',
'form_token',
'form_id',
'form_build_id',
'picture_upload',
'submit',
);
$profile_values = array_diff_key($form_state['values'], array_flip($remove_keys));
ksort($profile_values);
$profile_data = serialize($profile_values);
$stored_profile_values = array_diff_key($form_state['storage']['values'], array_flip($remove_keys));
ksort($stored_profile_values);
$stored_profile_data = serialize($stored_profile_values);
$time_ago = time() - 900;
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'message')
->propertyCondition('uid', $form_state['user']->uid)
->propertyCondition('type', 'commons_activity_streams_user_profile_updated')
->propertyCondition('timestamp', $time_ago, '>')
->count();
$count = $query
->execute();
if ($user->uid != $form_state['user']->uid || $profile_data == $stored_profile_data || $count > 0) {
return;
}
$account = $form_state['user'];
$hook = 'user_profile_update';
$message_type = 'commons_activity_streams_user_profile_updated';
drupal_alter('commons_activity_streams_message_selection', $message_type, $hook, $account);
$message = message_create($message_type, array(
'uid' => $account->uid,
'timestamp' => REQUEST_TIME,
));
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_target_users[] = $account;
$wrapper
->save();
}
function commons_activity_streams_token_info() {
$tokens = array();
$styles = image_styles();
foreach ($styles as $style_name => $style) {
$tokens['picture:' . $style_name] = array(
'name' => t('Picture: @st image style', array(
'@st' => $style_name,
)),
'description' => t('Picture: @st image style', array(
'@st' => $style_name,
)),
);
}
return array(
'tokens' => array(
'user' => $tokens,
),
);
}
function commons_activity_streams_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'user' && isset($data['user'])) {
$account = $data['user'];
$username = format_username($account);
$alt = t("@name's picture", array(
'@name' => $username,
));
foreach ($tokens as $token => $original) {
if (strpos($token, 'picture:') !== FALSE) {
list(, $style) = explode(':', $token);
$path = $account->picture ? $account->picture->uri : variable_get('user_picture_default', '/profiles/commons/images/avatars/user-avatar.png');
$image_variables = array(
'path' => image_style_url($style, $path),
'alt' => $alt,
'title' => $alt,
'class' => array(
'image-style-none',
),
);
$image = theme('image', $image_variables);
$user_path = user_uri($account);
$link_options = array(
'attributes' => array(
'title' => t('View user profile.'),
),
'html' => TRUE,
);
$replacements[$original] = "<div class='user-picture'>" . l($image, $user_path['path'], $link_options) . "</div>";
}
}
}
return $replacements;
}
function commons_activity_streams_views_post_execute(&$view) {
if ($view->name == 'commons_activity_streams_activity' && isset($view->result)) {
foreach ($view->result as $key => $msg) {
if (isset($msg->mid)) {
$message = message_load($msg->mid);
if (isset($message->field_target_comments)) {
foreach ($message->field_target_comments[LANGUAGE_NONE] as $key => $value) {
$comment = comment_load($value['target_id']);
if (!entity_access('view', 'comment', $comment)) {
unset($view->result[$key]);
}
}
}
}
}
}
}