You are here

function privatemsg_limits_privatemsg_message_validate in Privatemsg 7

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

Implements hook_privatemsg_message_validate().

File

privatemsg_limits/privatemsg_limits.module, line 39
Privatemsg Quota module

Code

function privatemsg_limits_privatemsg_message_validate($message, $form = FALSE) {
  $errors = array();
  if (variable_get('privatemsg_limits_recipients_enabled', FALSE)) {
    $amount = _privatemsg_limits_get_amount('recipients_amount', $message->author);
    if (!isset($message->thread_id) && $amount > 0 && count($message->recipients) > $amount) {
      $errors[] = t("You are not allowed to send a message to more than @number recipients.", array(
        '@number' => $amount,
      ));
    }
  }

  // Only check sending limit if enabled and if this is either not a reply or
  // messages should be checked and not threads. When the limit object are
  // threads, users can send an unlimited amount of replies.
  if (variable_get('privatemsg_limits_send_enabled', FALSE) && (empty($message->thread_id) || variable_get('privatemsg_limits_send_object', 'message') == 'message')) {
    $amount = _privatemsg_limits_get_amount('send_amount', $message->author);
    $used = _privatemsg_limits_get_sent($message->author, variable_get('privatemsg_limits_send_timeframe', 3600));
    if ($amount > 0 && $used >= $amount) {
      $wait_time = _privatemsg_limits_get_oldest($message->author, variable_get('privatemsg_limits_send_timeframe', 3600));
      $period = format_interval(variable_get('privatemsg_limits_send_timeframe', 3600));
      if (variable_get('privatemsg_limits_receive_object', 'message') == 'message') {
        $errors[] = t("Your message was not sent because you have exceeded your sending limit. You are allowed to send %limit messages every @period. You can send your next message in in @wait_time.", array(
          '@wait_time' => $wait_time,
          '%limit' => $amount,
          '@period' => $period,
        ));
      }
      else {
        $errors[] = t("Your message was not sent because you have exceeded your sending limit. You are allowed to start %limit conversations every @period. You can start your next conversation in in @wait_time.", array(
          '@wait_time' => $wait_time,
          '%limit' => $amount,
          '@period' => $period,
        ));
      }
    }
  }
  if (variable_get('privatemsg_limits_receive_enabled', FALSE) && (empty($message->thread_id) || variable_get('privatemsg_limits_receive_object', 'message') == 'message')) {
    $amount = _privatemsg_limits_get_amount('receive_amount', $message->author);
    $used = _privatemsg_limits_get_received($message->author);
    if ($amount > 0 && $used >= $amount) {
      if (variable_get('privatemsg_limits_receive_object', 'message') == 'message') {
        $errors[] = t("Your message mailbox is currently full. You are allowed a maximum of %limit messages in your mailbox at one time. You won't be able to send or receive new messages until you delete some existing ones.", array(
          '%limit' => $amount,
        ));
      }
      else {
        $errors[] = t("Your message mailbox is currently full. You are allowed a maximum of %limit conversations in your mailbox at one time. You won't be able to start or receive new conversations until you delete some existing ones.", array(
          '%limit' => $amount,
        ));
      }
    }
  }

  // Blocks message sending 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 blocking the message.
    if (variable_get('privatemsg_limits_messages_per_thread_action', 'create-new') == 'block-message') {
      $query = "SELECT COUNT(*) FROM {pm_index} WHERE thread_id = :thread_id AND recipient = :recipient AND type IN ('hidden', 'user')";
      $messages = db_query($query, array(
        ':thread_id' => $message->thread_id,
        ':recipient' => $message->author->uid,
      ))
        ->fetchField();
      if ($messages >= variable_get('privatemsg_limits_messages_per_thread', 0)) {

        // If the number of messages per-thread has been exceeded, block message
        // from being sent.
        $errors[] = t("This message cannot be sent because the thread already contains %limit messages (the maximum number of messages permitted per thread). To send this message, please create a new message thread.", array(
          '%limit' => variable_get('privatemsg_limits_messages_per_thread', 0),
        ));
      }
    }
  }
  if (!empty($errors)) {
    if ($form) {
      foreach ($errors as $error) {
        form_set_error('recipient', $error);
      }
    }
    else {
      return array(
        'error' => $errors,
      );
    }
  }
}