You are here

function _forum_access_comment_view_alter in Forum Access 7

Really implements hook_comment_view_alter().

Adds and removes action links from/to one comment.

1 call to _forum_access_comment_view_alter()
forum_access_comment_view_alter in ./forum_access.module
Implements hook_comment_view_alter().

File

./forum_access.node.inc, line 36
forum_access.node.inc

Code

function _forum_access_comment_view_alter(&$build, $tid) {
  global $user;
  $node = $build['#node'];
  $comment = $build['#comment'];

  //dpm($build, "_forum_access_comment_view_alter() tid=[$tid] cid=[$comment->cid] BEFORE:");
  $links =& $build['links']['comment']['#links'];
  if ($user->uid != 1 && !user_access('bypass node access') && !forum_access_access('create', $tid)) {
    unset($links['comment-reply']);
  }
  if (!user_access('administer comments') || !empty($user->_forum_access_moderator)) {
    $default_link_keys = array(
      'create' => array(
        'comment-reply',
      ),
      'update' => array(
        'comment-edit',
        'comment-approve',
      ),
      'delete' => array(
        'comment-delete',
      ),
    );
    forum_access_enable_moderator();
    $admin_links = comment_links($comment, $node);
    forum_access_enable_moderator(FALSE);

    //dpm($admin_links, "_forum_access_comment_view_alter() ADMINISTRATOR_LINKS for comment/$comment->cid:");
    foreach (array_keys($default_link_keys) as $op) {
      $access = (bool) forum_access_access($op, $tid);
      if ($op != 'create') {
        $perm = $op == 'update' ? 'edit' : $op;
        $access |= $user->uid == $comment->uid && user_access("{$perm} own forum content");
      }
      $available_keys = variable_get("forum_access_allowed_comment_links_for_{$op}", $default_link_keys[$op]);
      $old_links = $links;
      $links = array();
      foreach ($default_link_keys[$op] as $key) {
        if ($access && array_search($key, $available_keys) !== FALSE && isset($admin_links[$key])) {

          //dpm($key, '_forum_access_comment_view_alter() ADDED:');
          $links[$key] = $admin_links[$key];
        }
        if (!$access && !empty($old_links[$key])) {

          //dpm($key, '_forum_access_comment_view_alter() REMOVED:');
          unset($old_links[$key]);
        }
      }
      if (is_array($old_links)) {

        /** @noinspection SlowArrayOperationsInLoopInspection */
        $links = array_merge($links, $old_links);
      }
    }

    //dpm($build, "_forum_access_comment_view_alter() tid=[$tid] cid=[$comment->cid] AFTER:");
  }
}