<?php


/**
 * Allow themable wrapping of all comments (from garland).
 */
function clameurs_comment_wrapper($content, $node) {
  if (!$content || $node->type == 'forum') {
    return '<div id="comments">'. $content .'</div>';
  }
  else {
    return '<div id="comments"><h2 class="comments">'. t('Comments :') .'</h2>'. $content .'</div>';
  }
}

/**
 * Theme output of user signature.
 *
 * @ingroup themeable
 */
function clameurs_user_signature($signature) {
  return '<div class="user-signature">'.$signature.'</div>';
}

/**
 * Returns a formatted list of recent comments to be displayed in the comment block.
 *
 * @return
 *   The comment list HTML.
 * @ingroup themeable
 */
function clameurs_comment_block() {
  $items = array();
  foreach (comment_get_recent() as $comment) {
    $items[] = l($comment->subject, 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

/**
 * Themes a single comment and related items.
 *
 * @param $comment
 *   The comment object.
 * @param $node
 *   The comment node.
 * @param $links
 *   An associative array containing control links suitable for passing into
 *   theme_links(). These are generated by modules implementing hook_link() with
 *   $type='comment'. Typical examples are links for editing and deleting
 *   comments.
 * @param $visible
 *   Switches between folded/unfolded view. If TRUE the comments are visible, if
 *   FALSE the comments are folded.
 * @ingroup themeable
 */
function clameurs_comment_view($comment, $node, $links = array(), $visible = TRUE) {
  static $first_new = TRUE;

  $output = '';
  $comment->new = node_mark($comment->nid, $comment->timestamp);
  if ($first_new && $comment->new != MARK_READ) {
    // Assign the anchor only for the first new comment. This avoids duplicate
    // id attributes on a page.
    $first_new = FALSE;
    $output .= "<a id=\"new\"></a>\n";
  }

  $output .= "<a id=\"comment-$comment->cid\"></a>\n";

  // Switch to folded/unfolded view of the comment
  if ($visible) {
    $comment->comment = check_markup($comment->comment, $comment->format, FALSE);

    // Comment API hook
    comment_invoke_comment($comment, 'view');

    $output .= theme('comment', $comment, $node, $links);
  }
  else {
    $output .= theme('comment_folded', $comment);
  }

  return $output;
}


/**
 * Theme comment controls box where the user can change the default display mode and display order of comments.
 *
 * @param $form
 *   The form structure.
 * @ingroup themeable
 */
function clameurs_comment_controls($form) {
  $output = '<div class="container-inline">';
  $output .=  drupal_render($form);
  $output .= '</div>';
  $output .= '<div class="description">'. t('Select your preferred way to display the comments and click "Save settings" to activate your changes.') .'</div>';
  return theme('box', t('Comment viewing options'), $output);
}

/**
 * Theme comment flat collapsed view.
 *
 * @param $comment
 *   The comment to be themed.
 * @param $node
 *   The comment node.
 * @ingroup themeable
 */
function clameurs_comment_flat_collapsed($comment, $node) {
  return theme('comment_view', $comment, $node, '', 0);
}

/**
 * Theme comment flat expanded view.
 *
 * @param $comment
 *   The comment to be themed.
 * @param $node
 *   The comment node.
 * @ingroup themeable
 */
function clameurs_comment_flat_expanded($comment, $node) {
  $links = module_invoke_all('link', 'comment', $comment, 0);
  drupal_alter('link', $links, $node, $comment);
  return theme('comment_view', $comment, $node, $links);
}

/**
 * Theme comment thread collapsed view.
 *
 * @param $comment
 *   The comment to be themed.
 * @param $node
 *   The comment node.
 * @ingroup themeable
 */
function clameurs_comment_thread_collapsed($comment, $node) {
  return theme('comment_view', $comment, $node, '', 0);
}

/**
 * Theme comment thread expanded view.
 *
 * @param $comment
 *   The comment to be themed.
 * @param $node
 *   The comment node.
 * @ingroup themeable
 */
function clameurs_comment_thread_expanded($comment, $node) {
  $links = module_invoke_all('link', 'comment', $comment, 0);
  drupal_alter('link', $links, $node, $comment);
  return theme('comment_view', $comment, $node, $links);
}

/**
 * Theme a "you can't post comments" notice.
 *
 * @param $node
 *   The comment node.
 * @ingroup themeable
 */
function clameurs_comment_post_forbidden($node) {
  global $user;
  static $authenticated_post_comments;

  if (!$user->uid) {
    if (!isset($authenticated_post_comments)) {
      // We only output any link if we are certain, that users get permission
      // to post comments by logging in. We also locally cache this information.
      $authenticated_post_comments = array_key_exists(DRUPAL_AUTHENTICATED_RID, user_roles(TRUE, 'post comments') + user_roles(TRUE, 'post comments without approval'));
    }

    if ($authenticated_post_comments) {
      // We cannot use drupal_get_destination() because these links
      // sometimes appear on /node and taxonomy listing pages.
      if (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
        $destination = 'destination='. rawurlencode("comment/reply/$node->nid#comment-form");
      }
      else {
        $destination = 'destination='. rawurlencode("node/$node->nid#comment-form");
      }

      if (variable_get('user_register', 1)) {
        // Users can register themselves.
        return t('<a href="@login">Login</a> or <a href="@register">register</a> to post comments', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination))));
      }
      else {
        // Only admins can add new users, no public registration.
        return t('<a href="@login">Login</a> to post comments', array('@login' => url('user/login', array('query' => $destination))));
      }
    }
  }
}


/**
 * Theme a "Submitted by ..." notice.
 *
 * @param $comment
 *   The comment.
 * @ingroup themeable
 */
function clameurs_comment_submitted($comment) {
  return t('Submitted by !username on @datetime.',
    array(
      '!username' => theme('username', $comment),
      '@datetime' => format_date($comment->timestamp)
    ));
}
