You are here

function privatemsg_limits_privatemsg_message_presave_alter in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg_limits/privatemsg_limits.module \privatemsg_limits_privatemsg_message_presave_alter()
  2. 7 privatemsg_limits/privatemsg_limits.module \privatemsg_limits_privatemsg_message_presave_alter()

Implements hook_privatemsg_message_presave_alter().

File

privatemsg_limits/privatemsg_limits.module, line 140
Privatemsg Quota module

Code

function privatemsg_limits_privatemsg_message_presave_alter($message) {

  // Put message into new thread if over number of messages per-thread.
  if (isset($message->thread_id) && variable_get('privatemsg_limits_messages_per_thread', 0) > 0) {

    // If we're not creating a new thread.
    if (variable_get('privatemsg_limits_messages_per_thread_action', 'create-new') != 'create-new') {
      return;
    }
    $query = "SELECT COUNT(*) FROM {pm_index} WHERE thread_id = :thread_id AND recipient = :recipient AND type IN ('hidden', 'user')";
    $result = db_query($query, array(
      ':thread_id' => $message->thread_id,
      ':recipient' => $message->author->uid,
    ))
      ->fetchField();
    if ($result >= variable_get('privatemsg_limits_messages_per_thread', 0)) {

      // If the number of messages per-thread has been exceeded, force message into new thread.
      unset($message->thread_id);
      drupal_set_message(t("Your message would have exceeded our %limit messages per conversation limit. As a result, we've created a new conversation for your message.", array(
        '%limit' => variable_get('privatemsg_limits_messages_per_thread', 0),
      )));
    }
  }
}