You are here

public function Notifications_Event::send_list in Notifications 7

Send to subscriptors

Parameters

$subscriptions: List of subscriptions to send to

1 call to Notifications_Event::send_list()
Notifications_Event::send_all in ./notifications.event.inc
Send message to all subscriptions

File

./notifications.event.inc, line 521
Drupal Notifications Framework - Default class file

Class

Notifications_Event
Notifications Event class

Code

public function send_list($sids) {
  if (!$this->send_start) {
    $this->send_start = time();
  }
  $this->notif_count += count($sids);
  $subscriptions = entity_load('notifications_subscription', $sids);

  // Template to be rendered for each user
  $template = $this
    ->get_template();
  $message = $template
    ->build_message();
  $message
    ->add_event($this, array_keys($subscriptions));

  // Keep track of users sent so we don't repeat
  $results = array(
    'skip' => array(),
    'sent' => array(),
    'success' => array(),
    'error' => array(),
  );
  foreach ($subscriptions as $subscription) {
    $this->last_sid = $subscription->sid;
    if ($destination = $subscription
      ->get_destination()) {

      // If already sent for this destination (user, method, address), move on
      if (isset($this->notifications_sent[$destination
        ->index()])) {
        $results['skip'][] = $subscription->sid;
        continue;
      }

      // Mark the event as already sent for this destination
      $this->notifications_sent[$destination
        ->index()] = TRUE;
    }
    else {

      // Missing or wrong destination
      watchdog('notifications', 'Cannot find destination or missing user for subscription @sid', array(
        '@sid' => $subscription->sid,
      ), WATCHDOG_WARNING);
    }

    // Check access to all objects related with this event
    if ($destination && ($user = $subscription
      ->get_user()) && $this
      ->user_access($user)) {
      $this->notif_sent++;
      $message
        ->add_destination($destination, $subscription->send_method);
      $results['sent'][] = $subscription->sid;
    }
    else {
      $this->notif_error++;
      $results['error'][] = $subscription->sid;
    }
  }

  // This will send out all messages. It will be a bulk sending or not depending on send method
  if (!empty($results['sent'])) {
    $message
      ->send_all();
    $results['results'] = $message
      ->get_results();
    $results['success'] = count(array_filter($results['results']));
    $this->notif_success += $results['success'];
  }
  $this->send_end = time();
  return $results;
}