You are here

function signature_forum_get_signature in Signatures for Forums 6

Same name and namespace in other branches
  1. 5.2 signature_forum.module \signature_forum_get_signature()

Retrieve signature for a given user.

Parameters

&$a1: Object that is either a node or comment.

Return value

Signature of the user who posted the content passed in &$a1.

2 calls to signature_forum_get_signature()
signature_forum_comment in ./signature_forum.module
Implementation of hook_comment().
signature_forum_nodeapi in ./signature_forum.module
Implementation of hook_nodeapi().

File

./signature_forum.module, line 534
Tweaks signatures in ways inspired by other traditional forum software:

Code

function signature_forum_get_signature($a1) {
  static $node_type;
  static $cache = array();
  $settings = variable_get('signature_forum_settings', signature_forum_defaults());

  // If signatures are disabled.
  if (!variable_get('user_signatures', FALSE)) {
    return;
  }

  // Get the node type, if it's not already set.
  if (!isset($node_type)) {
    if (!isset($a1->type)) {

      // This is a comment, so we need to get the node type from the DB.
      // We add the type to our comment object so it can be passed to _comment_get_display_setting().
      $a1->type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $a1->nid));
    }
    $node_type = $a1->type;
  }

  // If signatures are not shown for this node type.
  if (!$settings['signature_forum_show_for_' . $node_type]) {
    return;
  }

  // Get length of comment.
  if (isset($a1->cid) || !isset($a1->revision_timestamp)) {
    $content_length = drupal_strlen(strip_tags($a1->comment));
  }
  else {
    $content_length = drupal_strlen(strip_tags($a1->content['body']['#value']));
  }

  // Content minimum length not set, is longer than minimum or the user has a role that is an exception.
  if ($content_length >= $settings['signature_forum_min_content_length'] || signature_forum_user_exception($a1->uid, 'min_length')) {
    $load_signature = TRUE;
  }
  elseif ($settings['signature_forum_min_content_length_action'] == MIN_CONTENT_ACTION_DO_NOT_DISPLAY) {

    // We still call the theme with a blank string, a themer may still wish to
    // output something to keep their theme consistent.
    return theme('signature_forum', '');
  }

  // Check if signature is disabled for this comment/node.
  if (isset($a1->cid)) {
    $id = $a1->cid;
    $type = 'comment';
  }
  else {
    $id = $a1->nid;
    $type = 'node';
  }
  $signature_status = signature_forum_get_status($id, $type);
  if (!$signature_status) {
    return theme('signature_forum', '');
  }
  if (isset($cache[$a1->uid])) {

    // If the signature is in the cache it has been displayed before.
    if ($settings['signature_forum_show_once_options'] == SHOW_ONCE_OPTIONS_ALWAYS || signature_forum_user_exception($a1->uid, 'show_once')) {

      // Make implicit copy of signature so original, in cache, is preserved.
      $signature = (string) $cache[$a1->uid];
      $load_signature = FALSE;
    }
    else {
      $signature = '';
      $load_signature = FALSE;
    }
  }
  else {

    // We're not on the first page, or we're viewing a single comment, i.e. is_numeric(arg(2)).
    if (isset($_GET['page']) || is_numeric(arg(2))) {

      // If the signature is set to always show, the user is exempt from show once rules
      // or this is not a comment (ergo it is the node and must be the first post).
      if ($settings['signature_forum_show_once_options'] == SHOW_ONCE_OPTIONS_ALWAYS || !isset($a1->cid) || signature_forum_user_exception($a1->uid, 'show_once')) {
        $load_signature = TRUE;
      }
      else {

        // _comment_get_display_setting() requires a node object, we fake one for it.
        if (!isset($a1->type)) {
          $fake_node = new stdClass();
          $fake_node->type = db_result(db_query("SELECT type from {node} WHERE nid = %d", $a1->nid));
        }
        else {

          // If $a1 is a node (not a comment) make an implicit copy.
          $fake_node = (object) $a1;
        }

        // We cache these values to avoid extra DB queries.
        static $mode;
        static $order;
        if (is_null($mode) || is_null($order)) {
          $mode = _comment_get_display_setting('mode', $fake_node);
          $order = _comment_get_display_setting('sort', $fake_node);
        }
        unset($fake_node);

        // If only a single comment is being displayed the comment object does not
        // contain the thread field.
        if (!isset($a1->thread)) {
          $a1->thread = db_result(db_query("SELECT thread FROM {comments} WHERE cid = %d", $a1->cid));
        }

        // If there's a result from this query it means the signature has appeared
        // previously in the topic.
        $sql = "SELECT cid FROM {comments} WHERE nid = %d AND uid = %d AND status = %d";
        $query_args = array(
          $a1->nid,
          $a1->uid,
          COMMENT_PUBLISHED,
        );

        // We need to ensure any posts displayed earlier are greater than the minimum length.
        if ($settings['signature_forum_min_content_length'] > 0 && !signature_forum_user_exception($a1->uid, 'min_length')) {
          $sql .= " AND LENGTH(comment) > %d";
          $query_args[] = $settings['signature_forum_min_content_length'];
        }
        if ($order == COMMENT_ORDER_NEWEST_FIRST) {
          if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
            $sql .= " AND cid > %d\n              ORDER BY cid DESC";
            $query_args[] = $a1->cid;
          }
          else {
            $sql .= " AND thread > '%s'\n              ORDER BY thread DESC";
            $query_args[] = $a1->thread;
          }
        }
        elseif ($order == COMMENT_ORDER_OLDEST_FIRST) {
          if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
            $sql .= " AND cid < %d\n              ORDER BY cid";
            $query_args[] = $a1->cid;
          }
          else {
            $sql .= " AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < SUBSTRING('%s', 1, (LENGTH('%s') - 1))\n              ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1))";
            $query_args[] = $a1->thread;
            $query_args[] = $a1->thread;
          }
        }

        // If there is a result then this is not the first post in this topic
        // by this user.
        if (!db_result(db_query_range($sql, $query_args, 0, 1))) {
          $load_signature = TRUE;
        }
        else {
          $signature = '';

          // We add a blank entry to the cache so the above query doesn't need to
          // be run for subsequent posts by this user.
          $cache[$a1->uid] = '';
          $load_signature = FALSE;
        }

        // Note: we do not need to check the node as it would already be in the
        // $cache. The node is always shown first, regardless of comment order.
      }
    }
    else {
      $load_signature = TRUE;
    }
  }

  // Load the signature and add it to the cache.
  if ($load_signature == TRUE) {
    $signature = db_result(db_query("SELECT signature FROM {users_signature} WHERE uid = %d", $a1->uid));
    $cache[$a1->uid] = (string) $signature;
  }

  // Prevent the signature from being put through input filters if it's empty.
  if ($signature == '') {
    return theme('signature_forum', $signature);
  }

  // We tell check_markup to not check user permissions as the filter is set by
  // the administrator.
  $signature = check_markup($signature, $settings['signature_forum_format'], FALSE);

  // If the content is under the minimum length, the user is not exempt and
  // the signature is set to be run through an extra filter (this can be the only option
  // as other cases have already been handled above: there is no need to add a
  // $settings['signature_forum_show_once_options'] check in here!).
  if ($content_length < $settings['signature_forum_min_content_length'] && !signature_forum_user_exception($a1->uid, 'min_length')) {
    $signature = check_markup($signature, $settings['signature_forum_min_content_length_filter'], FALSE);
  }
  $signature = sprintf($settings['signature_forum_template'], trim($signature));
  return theme('signature_forum', $signature);
}