You are here

function privatemsg_thread_change_delete in Privatemsg 6.2

Same name and namespace in other branches
  1. 6 privatemsg.module \privatemsg_thread_change_delete()
  2. 7.2 privatemsg.module \privatemsg_thread_change_delete()
  3. 7 privatemsg.module \privatemsg_thread_change_delete()

Delete or restore one or multiple threads.

Parameters

$threads: Array with thread id's or a single thread id.

$delete: Indicates if the threads should be deleted or restored. 1 => delete, 0 => restore.

$account: User object for which the threads should be deleted, defaults to the current user.

1 string reference to 'privatemsg_thread_change_delete'
privatemsg_privatemsg_thread_operations in ./privatemsg.module
Implements hook_privatemsg_thread_operations().

File

./privatemsg.module, line 2297
Allows users to send private messages to other users.

Code

function privatemsg_thread_change_delete($threads, $delete, $account = NULL) {
  if (!is_array($threads)) {
    $threads = array(
      $threads,
    );
  }
  if (empty($account)) {
    global $user;
    $account = drupal_clone($user);
  }

  // Record which messages will be deleted.
  $changed = array();
  $cond = $delete ? '=' : '>';
  $result = db_query("SELECT mid FROM {pm_index} WHERE deleted {$cond} 0 AND recipient = %d and type IN ('user', 'hidden') AND thread_id IN (" . db_placeholders($threads) . ')', array_merge(array(
    $account->uid,
  ), $threads));
  while ($row = db_fetch_object($result)) {
    $changed[] = $row->mid;
  }

  // Merge status and uid with the threads list. array_merge() will not overwrite/ignore thread_id 1.
  $delete_value = 0;
  if ($delete == TRUE) {
    $delete_value = time();
  }
  $params = array_merge(array(
    $delete_value,
    $account->uid,
  ), $threads);

  // Update the status of the threads.
  db_query("UPDATE {pm_index} SET deleted = %d WHERE recipient = %d and type IN ('user', 'hidden') AND thread_id IN (" . db_placeholders($threads) . ')', $params);

  // Allow modules to respond to the deleted changes.
  foreach ($changed as $mid) {
    module_invoke_all('privatemsg_message_status_delete', $mid, $delete, $account);
  }
  if ($delete) {
    drupal_set_message(format_plural(count($threads), 'Deleted 1 thread.', 'Deleted @count threads.'));
  }
  else {
    drupal_set_message(format_plural(count($threads), 'Restored 1 thread.', 'Restored @count threads.'));
  }
}