You are here

function commons_activity_streams_user_profile_submit in Drupal Commons 7.3

Create an activity stream message when a user updates her profile.

1 string reference to 'commons_activity_streams_user_profile_submit'
commons_activity_streams_form_alter in modules/commons/commons_activity_streams/commons_activity_streams.module
Implements hook_form_alter().

File

modules/commons/commons_activity_streams/commons_activity_streams.module, line 151

Code

function commons_activity_streams_user_profile_submit($form, &$form_state) {
  global $user;

  // Fields to ignore in $form_state['values'] when detecting changes.
  $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);

  // Do not generate a message if
  //  - the user did not submit their own form
  //  - no changes were detected
  //  - a profile update message created within the last 15 minutes
  $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'];

  // Allow other modules to change the message type used for this event.
  $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,
  ));

  // Save reference to the node in the node reference field, and the
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->field_target_users[] = $account;
  $wrapper
    ->save();
}