You are here

function messaging_store_save in Messaging 6.2

Same name and namespace in other branches
  1. 5 messaging.store.inc \messaging_store_save()
  2. 6 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

If there are many destinations they will be stored as 'multiple'

Parameters

$message: Message object

File

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

Code

function messaging_store_save($message) {

  // Normalize some values. Boolean parameters must be 0|1
  foreach (array(
    'queue',
    'log',
    'cron',
  ) as $field) {
    $message->{$field} = empty($message->{$field}) ? 0 : 1;
  }

  // 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($message->method, 'group');
  $params = !empty($message->params[$group]) ? $message->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;
  }

  // Mark for a user if there's an account parameter, produced by messaging_send_user()
  if (!empty($message->account)) {
    $message->uid = $message->account->uid;
    $message->destination = 'user:' . $message->uid;
  }

  // Save serialized destinations
  if (!empty($message->destinations)) {
    $params['destinations'] = $message->destinations;
  }

  // Check destination, but preserve field in case it is already set. I.e. 'all users'
  if (empty($message->destination) && !empty($message->destinations)) {

    // Check for multiple destinations, just store 'multiple'
    // Bulk sending modules may store each destination differently
    if (count($message->destinations) > 1) {
      $message->destination = 'multiple';
    }
    elseif ($destination = $message->destinations[0]) {
      if (is_string($destination) || is_numeric($destination)) {
        $message->destination = $destination;
      }
    }
  }

  // If there are files, serialize $file->fids in an array
  if (!empty($message->files)) {
    $params['files'] = array_keys($message->files);
  }

  // Add parameters, timestamp and save
  $message->params = $params ? $params : NULL;
  $message->created = time();
  drupal_write_record('messaging_store', $message);

  // Finally, return the message object which should have a unique 'mqid'
  return $message;
}