You are here

function oa_messages_create in Open Atrium Core 7.2

Helper function to create a specific message type Fills in the wrapper fields for group and section

Parameters

string $message_type machine name of message:

int $entity entity or entity_id of entity being referenced:

string $entity_type type of entity being referenced:

string $text optional text to be added to message:

array $args optional data to send to hooks:

object $account optional user id or user object assigned to message:

boolean $send TRUE/FALSE to trigger notifications:

Return value

object the message entity created

5 calls to oa_messages_create()
oa_messages_comment_insert in modules/oa_messages/oa_messages.module
Implements hook_comment_insert().
oa_messages_entity_update in modules/oa_messages/oa_messages.module
Implements hook_entity_update().
oa_messages_node_delete in modules/oa_messages/oa_messages.module
Implements hook_node_delete().
oa_messages_og_membership_prepare in modules/oa_messages/oa_messages.module
Helper function to handle OG Membership messages
oa_messages_shutdown_send_messages in modules/oa_messages/oa_messages.module
Send any messages that have not gotten sent.

File

modules/oa_messages/oa_messages.module, line 25

Code

function oa_messages_create($message_type, $entity = NULL, $entity_type = 'node', $text = '', $args = NULL, $account = NULL, $send = TRUE) {
  global $user;

  // See if message creation is disabled.
  if (oa_messages_skip()) {
    return;
  }
  if (!empty($entity) && !is_object($entity)) {
    $entity_id = $entity;
    $entity = current(entity_load($entity_type, array(
      $entity_id,
    )));
  }
  elseif (!empty($entity) && is_object($entity)) {
    $entity_info = entity_get_info($entity_type);
    $entity_id = $entity->{$entity_info['entity keys']['id']};
  }
  else {
    $entity_id = NULL;
  }
  if (empty($account)) {

    // uid of message is current user, not the user who created the node
    $uid = $user->uid;
    $account = $user;
  }
  elseif (is_object($account)) {
    $uid = $account->uid;
  }
  else {
    $uid = $account;
    $account = user_load($account);
  }
  if (isset($entity)) {
    $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
  }
  else {
    $entity_wrapper = NULL;
  }

  // allow other modules to alter the message type to be created
  $context = array(
    'uid' => $uid,
    'account' => $account,
    'entity' => $entity,
    'entity_type' => $entity_type,
    'entity_id' => $entity_id,
    'entity_wrapper' => $entity_wrapper,
    'text' => $text,
    'arguments' => $args,
  );
  drupal_alter('oa_messages_type', $message_type, $context);
  if (empty($message_type)) {
    return;
  }
  $message = message_create($message_type, array(
    'uid' => $uid,
    'timestamp' => time(),
  ), $account);
  if (!empty($entity->og_group_ref[LANGUAGE_NONE])) {
    $message->gid = $entity->og_group_ref[LANGUAGE_NONE][0]['target_id'];
  }

  // Save reference to the node in the node reference field.
  $wrapper = entity_metadata_wrapper('message', $message);
  if (isset($entity_wrapper->{OA_SPACE_FIELD}) && isset($wrapper->field_oa_message_space)) {
    $space = $entity_wrapper->{OA_SPACE_FIELD}
      ->value();
    if (is_array($space)) {

      // if multi-value space field, just use first space for message
      $space = array_shift($space);
    }
    $wrapper->field_oa_message_space = $space;
  }
  if (isset($entity_wrapper->{OA_SECTION_FIELD}) && isset($wrapper->field_oa_message_section)) {
    $wrapper->field_oa_message_section = $entity_wrapper->{OA_SECTION_FIELD}
      ->value();
  }
  if (isset($wrapper->field_oa_message_text)) {
    $wrapper->field_oa_message_text
      ->set(array(
      'value' => $text,
      'format' => 'full_html',
    ));
  }
  if (isset($entity) && $entity_type == 'node') {
    if (isset($wrapper->field_oa_node_ref)) {
      $wrapper->field_oa_node_ref = $entity_id;
    }
  }

  // allow other modules to add stuff to the wrapper
  $context += array(
    'message_type' => $message_type,
    'message' => $message,
  );
  drupal_alter('oa_messages_create', $wrapper, $context);
  if (!empty($entity_wrapper)) {

    // Since this can be called on node_update / insert, we clear the entity
    // cache so that it gets the values assigned by other modules.
    entity_get_controller($entity_wrapper
      ->type())
      ->resetCache(array(
      $entity_wrapper
        ->getIdentifier(),
    ));
  }
  $wrapper
    ->save();
  if ($send && variable_get('oa_messages_notifications', TRUE) && module_exists('message_subscribe') && isset($entity) && $entity_type == 'node') {
    $notify_options = oa_messages_build_notify_options();
    $subscribe_options = oa_messages_build_subscribe_options($entity, $message);
    if (!empty($subscribe_options['uids'])) {
      message_subscribe_send_message('node', $entity, $message, $notify_options, $subscribe_options);
      drupal_set_message(t('Notifications sent to ') . format_plural(count($subscribe_options['uids']), '1 user', '@count users'));
    }
  }
  return $message;
}