You are here

function redhen_activity_message_save in RedHen CRM 7

Save a message.

Parameters

$entity:

$type:

$op:

3 calls to redhen_activity_message_save()
redhen_activity_entity_delete in modules/redhen_activity/redhen_activity.module
Implements hook_entity_delete().
redhen_activity_entity_insert in modules/redhen_activity/redhen_activity.module
Implements hook_entity_insert().
redhen_activity_entity_update in modules/redhen_activity/redhen_activity.module
Implements hook_entity_update().

File

modules/redhen_activity/redhen_activity.module, line 388

Code

function redhen_activity_message_save($entity, $type, $op) {
  global $user;
  switch ($type) {
    case 'redhen_contact':
    case 'redhen_org':
      $message = message_create($type, array(
        'arguments' => array(
          '%name' => $entity
            ->label(),
          '!op' => $op,
        ),
      ), $user);
      $ref_field_name = "message_{$type}";
      $message->{$ref_field_name}[LANGUAGE_NONE][0] = array(
        'target_id' => $entity
          ->internalIdentifier(),
      );
      $message
        ->save();
      break;
    case 'redhen_note':
      $wrapper = entity_metadata_wrapper('redhen_note', $entity);
      $parent_entity = $wrapper->entity
        ->value();
      if ($parent_entity) {
        $items = field_get_items('redhen_note', $entity, 'redhen_note_type');
        $note_name = NULL;
        if (isset($items[0])) {
          $note_name = taxonomy_term_load($items[0]['tid'])->name;
        }
        $message = message_create('redhen_note', array(
          'arguments' => array(
            '%name' => $parent_entity
              ->label(),
            '!op' => $op,
            '%type' => $note_name,
          ),
        ), $user);
        $ref_field_name = "message_{$entity->entity_type}";
        $message->{$ref_field_name}[LANGUAGE_NONE][0] = array(
          'target_id' => $parent_entity
            ->internalIdentifier(),
        );
        $message
          ->save();
      }
      break;
    case 'relation':
      $endpoints = field_get_items('relation', $entity, 'endpoints');
      $endpoint_entities = array();
      foreach ($endpoints as $endpoint) {
        $endpoint_entities[] = entity_load_single($endpoint['entity_type'], $endpoint['entity_id']);
      }
      $relation_type = relation_type_load($entity->relation_type);

      // Save a message for each entity in the relationship.
      foreach ($endpoint_entities as $endpoint_entity) {

        // During delete operations the endpoint entity may be gone, so check
        // before creating a message:
        if ($endpoint_entity) {
          if (in_array($endpoint_entity
            ->entityType(), array(
            'redhen_contact',
            'redhen_org',
          ))) {
            $label1 = $endpoint_entities[0] ? $endpoint_entities[0]
              ->label() : t('Entity !type with an ID of !id', array(
              '!type' => $endpoints[0]['entity_type'],
              '!id' => $endpoints[0]['entity_id'],
            ));
            $label2 = $endpoint_entities[1] ? $endpoint_entities[1]
              ->label() : t('Entity !type with an ID of !id', array(
              '!type' => $endpoints[1]['entity_type'],
              '!id' => $endpoints[1]['entity_id'],
            ));
            $message = message_create('redhen_relation', array(
              'arguments' => array(
                '%name1' => $label1,
                '%name2' => $label2,
                '!op' => $op,
                '%type' => $relation_type->label,
              ),
            ), $user);
            $ref_field_name = "message_{$endpoint_entity->entityType()}";
            $message->{$ref_field_name}[LANGUAGE_NONE][0] = array(
              'target_id' => $endpoint_entity
                ->internalIdentifier(),
            );
            $message
              ->save();
          }
        }
      }
      break;
    case 'redhen_engagement':
      $wrapper = entity_metadata_wrapper('redhen_engagement', $entity);
      $score = $wrapper->engagement_score
        ->value();
      $contact = $wrapper->contact
        ->value();
      $message = message_create('redhen_engagement', array(
        'arguments' => array(
          '%name' => $contact
            ->label(),
          '!op' => $op,
          '%type' => $score
            ->label(),
          '%score' => $score->score,
        ),
      ), $user);
      $wrapper = entity_metadata_wrapper('message', $message);
      $wrapper->message_redhen_contact
        ->set($contact);
      $wrapper
        ->save();
      break;
    case 'redhen_membership':
      $wrapper = entity_metadata_wrapper('redhen_membership', $entity);
      $parent_entity = $wrapper->entity
        ->value();
      $message = message_create('redhen_membership', array(
        'arguments' => array(
          '%name' => $entity
            ->label(),
          '!op' => $op,
          '%type' => $entity->type,
          '%entity_name' => $parent_entity
            ->label(),
        ),
      ), $user);
      $ref_field_name = "message_{$parent_entity->entityType()}";
      $message->{$ref_field_name}[LANGUAGE_NONE][0] = array(
        'target_id' => $parent_entity
          ->internalIdentifier(),
      );
      $message
        ->save();
      break;
    default:
      return;
  }
}