You are here

comment_og.module in Comment OG 7

Same filename and directory in other branches
  1. 5 comment_og.module
  2. 6 comment_og.module

Provides comment integration for Organic Groups.

File

comment_og.module
View source
<?php

/**
 * @file
 * Provides comment integration for Organic Groups.
 */

/**
 * Implements hook_og_permission().
 */
function comment_og_og_permission() {

  // Generate standard node permissions for all applicable node types.
  $perms = array();
  foreach (node_permissions_get_configured_types() as $type) {
    if (og_is_group_content_type('node', $type) || og_is_group_type('node', $type)) {
      $info = node_type_get_type($type);
      $type = check_plain($info->type);
      if (og_is_group_type('node', $type)) {
        $perms["post comment_node_{$type}"] = array(
          'title' => t('Post comments on %type_name', array(
            '%type_name' => $info->name,
          )),
          'description' => t('Comment on the main group node, similar to a wall post.'),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
            OG_AUTHENTICATED_ROLE,
          ),
        );
        $perms["edit comment_node_{$type}"] = array(
          'title' => t('Edit any comment on %type_name', array(
            '%type_name' => $info->name,
          )),
          'description' => t('Member can edit any comment, including those posted by others. Edited comments are then owned by the editing member.'),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
        $perms["edit own comment_node_{$type}"] = array(
          'title' => t('Edit own comments on %type_name', array(
            '%type_name' => $info->name,
          )),
          'description' => t('Member can edit only their own comments.'),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
        $perms["delete comment_node_{$type}"] = array(
          'title' => t('Delete comments on %type_name', array(
            '%type_name' => $info->name,
          )),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
        $perms["approve comment_node_{$type}"] = array(
          'title' => t('Approve comments on %type_name', array(
            '%type_name' => $info->name,
          )),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
      }
      else {
        $perms["post comment_node_{$type}"] = array(
          'title' => t('Post comments on %type_name content', array(
            '%type_name' => $info->name,
          )),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
            OG_AUTHENTICATED_ROLE,
          ),
        );
        $perms["edit comment_node_{$type}"] = array(
          'title' => t('Edit any comment on %type_name content', array(
            '%type_name' => $info->name,
          )),
          'description' => t('Member can edit any comment, including those posted by others. Edited comments are then owned by the editing member.'),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
        $perms["edit own comment_node_{$type}"] = array(
          'title' => t('Edit own comments on %type_name content', array(
            '%type_name' => $info->name,
          )),
          'description' => t('Member can edit only their own comments.'),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
        $perms["delete comment_node_{$type}"] = array(
          'title' => t('Delete comments on %type_name content', array(
            '%type_name' => $info->name,
          )),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
        $perms["approve comment_node_{$type}"] = array(
          'title' => t('Approve comments on %type_name content', array(
            '%type_name' => $info->name,
          )),
          'default role' => array(
            OG_ADMINISTRATOR_ROLE,
          ),
        );
      }
    }
  }
  return $perms;
}

/**
 * Implements hook_node_view_alter().
 */
function comment_og_node_view_alter(&$build) {

  // Remove comment link and form for non-members.
  $node = $build['#node'];
  $group = og_context();
  if ($group) {
    if (!empty($group->gid)) {

      // Using og_user_access directly since comment_og_access takes in a
      // comment object or cid.
      if (!og_user_access($group->gid, "post comment_node_" . $node->type)) {
        unset($build['links']['comment']['#links']['comment-add']);
        $build['links']['comment']['#links']['comment_forbidden'] = array(
          'title' => theme('comment_post_forbidden', array(
            'node' => $node,
          )),
          'html' => TRUE,
        );
        $build['comments']['comment_form'] = array();
      }
    }
  }
}

/**
 * Implements hook_comment_view_alter().
 */
function comment_og_comment_view_alter(&$build) {
  $links = array();
  $node = $build['#node'];
  $group = og_context();
  $comment = $build['#comment'];

  // Build the links array in group context (largely lifted from comment_link).
  if (isset($group->gid) && $node->comment == COMMENT_NODE_OPEN) {
    if (comment_og_access('delete', $comment, $group->gid)) {
      $links['comment-delete'] = array(
        'title' => t('delete'),
        'href' => "comment/{$comment->cid}/delete/{$group->gid}",
        'html' => TRUE,
      );
    }
    if (comment_og_access('edit', $comment, $group->gid)) {
      $links['comment-edit'] = array(
        'title' => t('edit'),
        'href' => "comment/{$comment->cid}/edit/{$group->gid}",
        'html' => TRUE,
      );
    }
    if (comment_og_access('post', $comment, $group->gid)) {
      $links['comment-reply'] = array(
        'title' => t('reply'),
        'href' => "comment/reply/{$comment->nid}/{$comment->cid}/{$group->gid}",
        'html' => TRUE,
      );
    }
    if (comment_og_access('approve', $comment, $group->gid)) {
      if ($comment->status == COMMENT_NOT_PUBLISHED) {
        $links['comment-approve'] = array(
          'title' => t('approve'),
          'href' => "comment/{$comment->cid}/approve/{$group->gid}",
          'html' => TRUE,
          'query' => array(
            'token' => drupal_get_token("comment/{$comment->cid}/approve/{$group->gid}"),
          ),
        );
      }
    }
    if (empty($links['comment-delete']) && empty($links['comment-edit']) && empty($links['comment-reply']) && empty($links['comment-approve'])) {
      $links['comment_forbidden']['title'] = theme('comment_post_forbidden', array(
        'node' => $node,
      ));
      $links['comment_forbidden']['html'] = TRUE;
    }
    $build['links']['comment']['#links'] = $links;
  }
}

/**
 * Implements hook_menu_alter().
 */
function comment_og_menu_alter(&$items) {

  // Every other comment path uses %, but this one loads the comment directly,
  // so we don't end up loading it twice (in the page and access callback).
  $items['comment/%comment/edit'] = array(
    'title' => 'Edit',
    'page callback' => 'comment_edit_page',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'comment_og_access',
    'access arguments' => array(
      'edit',
      1,
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
  );
  $items['comment/%/approve'] = array(
    'title' => 'Approve',
    'page callback' => 'comment_approve',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'comment_og_access',
    'access arguments' => array(
      'approve',
      1,
      3,
    ),
    'file' => 'comment.pages.inc',
    'file path' => drupal_get_path('module', 'comment'),
    'weight' => 1,
  );
  $items['comment/%/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'comment_confirm_delete_page',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'comment_og_access',
    'access arguments' => array(
      'delete',
      1,
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'comment.admin.inc',
    'file path' => drupal_get_path('module', 'comment'),
    'weight' => 2,
  );
}

/**
 * Determines whether the current user has access to a particular comment.
 *
 * Authenticated users can edit their comments as long they have not been
 * replied to. This prevents people from changing or revising their statements
 * based on the replies to their posts.
 *
 * @param string $op
 *   The operation that is to be performed on the comment. Only 'edit' is
 *   recognized now.
 * @param object $comment
 *   The comment object. A CID is also allowed.
 * @param int $gid
 *   A group ID, loaded from og_context() which cannot be called natively within
 *   this function because it causes a white screen (WSOD).
 *
 * @return bool
 *   TRUE if the current user has acces to the comment, FALSE otherwise.
 */
function comment_og_access($op, $comment, $gid = FALSE) {
  global $user;

  // Load comment object if not passed in.
  if (is_numeric($comment)) {
    $comment = comment_load($comment);
  }

  // Group context logic.
  if ($comment && $gid) {
    switch ($op) {
      case 'approve':
        return og_user_access($gid, 'approve ' . $comment->node_type) || user_access('administer comments');
      case 'delete':
        return og_user_access($gid, 'delete ' . $comment->node_type) || user_access('administer comments');
      case 'edit':
        $own = $user->uid && $user->uid == $comment->uid ? 'own ' : '';
        return og_user_access($gid, 'edit ' . $own . $comment->node_type) || user_access('administer comments');
      case 'post':
        return og_user_access($gid, 'post ' . $comment->node_type);
    }
  }
  else {
    switch ($op) {
      case 'approve':
      case 'delete':
        return user_access('administer comments');
      case 'edit':
        return $user->uid && $user->uid == $comment->uid && $comment->status == COMMENT_PUBLISHED && user_access('edit own comments') || user_access('administer comments');
      case 'post':
        return user_access('post comments');
    }
  }
}

Functions

Namesort descending Description
comment_og_access Determines whether the current user has access to a particular comment.
comment_og_comment_view_alter Implements hook_comment_view_alter().
comment_og_menu_alter Implements hook_menu_alter().
comment_og_node_view_alter Implements hook_node_view_alter().
comment_og_og_permission Implements hook_og_permission().