You are here

function messaging_store_save in Messaging 5

Same name and namespace in other branches
  1. 6 messaging.store.inc \messaging_store_save()
  2. 6.2 messaging.store.inc \messaging_store_save()
  3. 6.3 messaging.store.inc \messaging_store_save()

Put into database storage, create one line for each destination

@ TODO See about guessing users from $destination object

Parameters

$method: Sending method

$destinations: Array of destinations, the type of elements will depend on sending method

$message: Message array

$sent: Sent timestamp when used for logging

$queue: Should be 1 when this is a regular queue entry

$log: Should be 1 when this entry is to be kept as a log

$cron: Should be 1 when this entry is to be processed on cron (queueing for push methods)

File

./messaging.store.inc, line 238
Database storage for the messaging framework

Code

function messaging_store_save($method, $destinations, $message, $sent = 0, $queue = 0, $log = 0, $cron = 1) {

  // Add some defaults so fields are populated
  $message += array(
    'account' => NULL,
    'sender' => 0,
  );

  // If sender is a user account, save sender field
  if (!empty($message['sender_account'])) {
    $message['sender'] = $message['sender_account']->uid;
  }

  // We just save the params for current sending method group
  $group = messaging_method_info($method, 'group');
  $params = !empty($params[$group]) ? $params[$group] : array();

  // And there's one more optional param that is sender_name
  if (!empty($message['sender_name'])) {
    $params['sender_name'] = $message['sender_name'];
  }
  foreach ($destinations as $destination) {

    // Mark for a user if there's an account parameter, produced by messaging_send_user()
    if ($message['account']) {
      $uid = $message['account']->uid;
    }
    elseif (is_object($destination) && isset($destination->uid)) {
      $uid = $destination->uid;
    }
    else {
      $uid = 0;
    }

    // Destination may be an object or an array, serialize if so
    if (is_object($destination) || is_array($destination->uid)) {
      $params['destination'] = $destination;
      $destination = '';
    }
    db_query("INSERT INTO {messaging_store} (method, uid, sender, destination, created, sent, queue, log, cron, subject, body, params)" . " VALUES('%s', %d, %d, '%s', %d, %d, %d, %d, %d, '%s', '%s', '%s')", $method, $uid, $message['sender'], $destination, time(), $sent, $queue, $log, $cron, $message['subject'], $message['body'], $params ? serialize($params) : '');
  }
}