You are here

function registration_send_broadcast in Entity Registration 7

Same name and namespace in other branches
  1. 8.2 registration.module \registration_send_broadcast()
  2. 8 registration.module \registration_send_broadcast()
  3. 7.2 registration.module \registration_send_broadcast()

Send an email to all registrations for a host entity.

Parameters

string $entity_type: The host entity type.

int $entity_id: The host entity ID.

string $subject: Subject of email.

string $message: Message body of email.

2 calls to registration_send_broadcast()
registration_cron in ./registration.module
Implements hook_cron().
registration_registrations_broadcast_form_submit in includes/registration.forms.inc
Submit handler for registration_registrations_broadcast_form.

File

./registration.module, line 1067

Code

function registration_send_broadcast($entity_type, $entity_id, $subject, $message) {
  global $language;

  // Return early if there are no active registration states configured.
  $active_states = registration_get_active_states();
  if (empty($active_states)) {
    $error_message = t('There are no active registration states configured. For email to be sent to registrants, an active registration state must be specified at "admin/structure/registration/registration_states".');
    drupal_set_message($error_message, 'error');
    watchdog('registration', $error_message, WATCHDOG_ERROR);
    return;
  }

  // grab registration entity settings
  $settings = registration_entity_settings($entity_type, $entity_id);
  $from = $settings['settings']['from_address'];

  // grab all registrations
  $query = new EntityFieldQuery();
  $entities = $query
    ->entityCondition('entity_type', 'registration')
    ->propertyCondition('entity_id', $entity_id)
    ->propertyCondition('entity_type', $entity_type)
    ->propertyCondition('state', $active_states, 'IN')
    ->execute();
  if (!empty($entities)) {
    $recipients = array();
    $message_template = $message;
    $params = array(
      'subject' => $subject,
      'message' => $message,
    );

    // load registrations and build an array of recipients
    $registrations = registration_load_multiple(array_keys($entities['registration']));

    // Give other modules a chance to alter registrations (for example, did
    // someone opt-out of the reminder?
    // Invoke hook_registration_send_broadcast_alter(),
    // hook_registration_send_broadcast_ENTITY_TYPE_alter() and
    // hook_registration_send_broadcast_ENTITY_TYPE_ENTITY_ID_alter()
    $hooks = array(
      'registration_send_broadcast',
    );
    $hooks[] = 'registration_send_broadcast_' . $entity_type;
    $hooks[] = 'registration_send_broadcast_' . $entity_type . '_' . $entity_id;
    $context = array(
      'entity_type' => $entity_type,
      'entity_id' => $entity_id,
    );
    drupal_alter($hooks, $registrations, $context);

    // send the email to each registrant and communicate results
    $success_count = 0;
    foreach ($registrations as $registration) {
      $wrapper = entity_metadata_wrapper('registration', $registration);
      $recipients[] = $wrapper->mail
        ->value();
      $entity = entity_load_single($entity_type, $entity_id);
      if (module_exists('token')) {
        $message = token_replace($message_template, array(
          $entity_type => $entity,
          'registration' => $registration,
        ), array(
          'clear' => TRUE,
        ));
      }
      $params['message'] = $message;
      $result = drupal_mail('registration', 'broadcast', $wrapper->mail
        ->value(), $language, $params, $from);
      if ($result['result'] !== FALSE) {
        $success_count++;
      }
      else {
        watchdog('registration', 'Failed to send registration broadcast email to %email.', array(
          '%email' => $wrapper->mail
            ->value(),
        ), WATCHDOG_ERROR);
      }
    }
    if ($success_count) {
      drupal_set_message(t('Registration broadcast sent to @count registrants.', array(
        '@count' => $success_count,
      )));
      watchdog('registration', 'Registration broadcast sent to @count registrants.', array(
        '@count' => $success_count,
      ));
    }
  }
  else {
    drupal_set_message(t('There are no participants registered for this %type.', array(
      '%type' => $entity_type,
    )), 'warning');
  }
}