You are here

function sms_track_archive_write in SMS Framework 7

Same name and namespace in other branches
  1. 6.2 modules/sms_track/sms_track.module \sms_track_archive_write()
  2. 6 modules/sms_track/sms_track.module \sms_track_archive_write()

Writes a record to the DB table.

Parameters

int $dir: Direction of message transmission (0=outgoing, 1=incoming).

string $number: MSISDN of recipient or sender.

string $message: SMS message body.

array $options: (optional) an array of additional options.

2 calls to sms_track_archive_write()
sms_track_sms_incoming in modules/sms_track/sms_track.module
Implements hook_sms_incoming().
sms_track_sms_send_process in modules/sms_track/sms_track.module
Implements hook_sms_send_process().

File

modules/sms_track/sms_track.module, line 201
Message tracking feature module for Drupal SMS Framework.

Code

function sms_track_archive_write($dir, $number, $message, $options = array()) {
  global $user;
  $archive_dir = variable_get('sms_track_archive_dir', SMS_DIR_NONE);

  // Query the database for the uid of the user that owns the number.
  if (module_exists("sms_user")) {
    $remote_user = sms_user_get_uid($number);
  }
  else {
    $remote_user = FALSE;
  }
  if ($dir == 0) {

    // Outgoing message.
    if ($archive_dir == SMS_DIR_ALL || $archive_dir == SMS_DIR_OUT) {
      $reference = isset($options) && is_array($options) && array_key_exists('reference', $options) ? $options['reference'] : NULL;

      // Status code may be provided by send result handler.
      $status = isset($options['result']) && is_array($options['result']) ? $options['result']['status_code'] : NULL;

      // Or render a status code from a simple true/false result.
      if (!$status) {
        if (!empty($options['result'])) {
          $status = SMS_GW_OK;
        }
        else {
          $status = SMS_GW_ERR_OTHER;
        }
      }

      // Add the author and recipient
      $author = $user->uid;
      $recipient = $remote_user ? $remote_user : 0;
    }
    else {
      return;
    }
  }
  elseif ($dir == 1) {

    // Incoming message.
    if ($archive_dir == SMS_DIR_ALL || $archive_dir == SMS_DIR_IN) {
      $reference = NULL;

      // Inbound message status is always the same.
      $status = SMS_MSG_STATUS_OK;

      // Add the author and recipient.
      $author = $remote_user ? $remote_user : 0;
      $recipient = $user->uid;
    }
    else {
      return;
    }
  }
  $gw_number = isset($options) && is_array($options) && array_key_exists('gw_number', $options) ? $options['gw_number'] : NULL;
  $created = REQUEST_TIME;
  $options_z = serialize($options);

  // Write the record to the database.
  $result = $id = db_insert('sms_track')
    ->fields(array(
    'reference' => $reference,
    'dir' => $dir,
    'number' => $number,
    'gw_number' => $gw_number,
    'message' => $message,
    'status' => $status,
    'created' => $created,
    'options' => $options_z,
    'author' => $author,
    'recipient' => $recipient,
  ))
    ->execute();
  if (!$result) {
    $to_from = $dir == 0 ? 'To' : 'From';
    watchdog('sms_track', 'Failed to record message: ' . $to_from . ' ' . $number . ': ' . $message);
  }
}