You are here

comment_og.module in Comment OG 6

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

File

comment_og.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function comment_og_menu() {
  $items['admin/og/comment_og'] = array(
    'title' => 'Organic groups comments configuration',
    'description' => 'Choose how group administrators can edit comments',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'comment_og_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );

  // allows group admins to edit comments
  $items['comment_og/edit'] = array(
    'title' => 'Edit comment',
    'page callback' => 'comment_og_comment_edit',
    'page arguments' => array(
      2,
      3,
    ),
    'access arguments' => array(
      'post comments',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_settings().
 */
function comment_og_settings() {
  $form['comment_og_admin_delete'] = array(
    '#type' => 'checkbox',
    '#title' => t('Group administrators can delete comments posted into their groups'),
    '#default_value' => variable_get('comment_og_admin_delete', 1),
  );
  $form['comment_og_admin_edit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Group administrators can edit comments posted into their groups'),
    '#default_value' => variable_get('comment_og_admin_edit', 1),
  );
  $form['comment_og_admin_edit_msg'] = array(
    '#type' => 'textfield',
    '#title' => t('Message to append to comments edited by group administrators'),
    '#default_value' => variable_get('comment_og_admin_edit_msg', comment_og_admin_edit_msg()),
    '#description' => t('Use the place holder %user to insert the user name of the administrator performing the editing. Example: ', array(
      '%user' => '%user',
    )) . comment_og_admin_edit_msg(),
  );
  $form['comment_og_admin_nonmember_access'] = array(
    '#type' => 'checkbox',
    '#title' => t('Group administrators can enable/disable allowing non-members to post comments into their groups'),
    '#default_value' => variable_get('comment_og_admin_nonmember_access', FALSE),
    '#description' => t('Allowance of non-members to post into groups may be configured per group. The default setting for each group is to disabled non-member comment posts. Enabling or disabling this option does not reset any changes to this setting made by group administrators in a group.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_theme().
 */
function comment_og_theme() {
  return array(
    'comment_og_edit_msg' => array(
      'arguments' => array(
        'message' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_link_alter().
 * Removes comment link for non-members
 */
function comment_og_link_alter(&$links, $node, $comment = NULL) {
  $group = og_get_group_context();
  if ($group) {
    if ($node->comment && isset($node->og_groups)) {
      if (!comment_og_access($group)) {
        unset($links['comment_add']);
      }
      if ($comment) {
        if (!comment_og_access($group)) {
          unset($links['comment_reply']);
        }
      }
    }
  }
}

/*
 * Implementation of hook_link().
 * Adds an edit or delete link if needed
 */
function comment_og_link($type, $object, $teaser = FALSE) {
  $group = og_get_group_context();
  if ($type == 'comment' && node_comment_mode($object->nid) == COMMENT_NODE_READ_WRITE) {
    if (og_is_group_admin($group) && !comment_access('edit', $object)) {

      // we add the Group ID to the URL to determine group admin status later
      $links['comment_edit'] = array(
        'title' => t('edit'),
        'href' => "comment_og/edit/{$object->cid}/{$group->nid}",
      );
    }

    // this effectively makes sure the delete link doesn't already exist
    if (og_is_group_admin($group) && !user_access('administer comments') && user_access('post comments')) {
      $links['comment_delete'] = array(
        'title' => t('delete'),
        'href' => "comment/delete/{$object->cid}/{$group->nid}",
      );
    }
  }
  return $links;
}

/*
 * Implementation of hook_form_alter().
 */
function comment_og_form_alter(&$form, $form_state, $form_id) {
  $group = og_get_group_context();

  // if group context
  if ($group) {

    /* Support disabling the comment form on a forum node */
    if ($form_id == 'comment_form' && isset($form['nid'])) {

      /*
       * If this is group context and you're not a member of this
       * group and this group doesn't allow non-members to post,
       * disable the comment form.
       */
      $og_node = og_get_group_context();
      if (!comment_og_access($og_node)) {
        $form['comment_filter']['comment'] = array(
          '#type' => 'textarea',
          '#title' => t('Comment'),
          '#rows' => 5,
          '#disabled' => TRUE,
          '#description' => t('You must be a member of this group in order to post a comment.'),
        );
        if (isset($form['subject'])) {
          unset($form['subject']);
        }
        if (isset($form['comment_filter']['format'])) {
          unset($form['comment_filter']['format']);
        }
        if (isset($form['submit'])) {
          unset($form['submit']);
        }
        if (isset($form['preview'])) {
          unset($form['preview']);
        }
        if (isset($form['author'])) {
          unset($form['author']);
        }
        if (isset($form['_author'])) {
          unset($form['_author']);
        }
      }
    }

    /* Alter the group node's form to add non-member access configuration */
    if (isset($form['#node']) && $form_id == $form['#node']->type . '_node_form' && og_is_group_type($form['#node']->type)) {
      $node = $form['#node'];
      if (variable_get('comment_og_admin_nonmember_access', FALSE)) {
        $form['comment_og_nonmember_access'] = array(
          '#title' => t('Allow non-member comments'),
          '#description' => t('Allow non-members to post comments in this group.'),
          '#type' => 'checkbox',
          '#default_value' => _comment_og_allow_nonmember_access($node->nid),
          '#access' => og_is_group_admin($node),
          '#weight' => 1 + module_exists('content') ? content_extra_field_weight($node->type, 'og_selective') : 0,
        );
      }
    }
  }
}

/**
 * Determine if a group's comments are only restricted to members.
 *
 * @param $nid
 *   The node id of group node whose comment access should be checked.
 * @return
 *   TRUE if nonmembers may post comments in the specified
 *   group. FALSE otherwise.
 */
function _comment_og_allow_nonmember_access($nid) {

  /*
   * No row for this group node results in non-TRUE return value,
   * which would be the same as having a row inserted with the default
   * value.
   */
  return (bool) db_result(db_query('SELECT grant_nonmember_access FROM {comment_og_node} con WHERE con.nid = %d', $nid));
}

/**
 * Determine if comments may be posted by the active user to the given
 * group.
 *
 * @param $og_node
 *   The group node (with a nodetype of `og') to which the user's
 *   comment post access is being tested.
 * @return
 *   TRUE if the active user may post a comment within the given
 *   group. FALSE otherwise.
 */
function comment_og_access($og_node) {
  return og_is_group_member($og_node) || _comment_og_allow_nonmember_access($og_node->nid);
}

/**
 * Implement hook_nodeapi() to save configuration changes on group
 * node forms.
 */
function comment_og_nodeapi(&$node, $op) {
  switch ($op) {
    case 'delete':
      db_query('DELETE FROM {comment_og_node} con WHERE con.nid = %d', $node->nid);
      break;
    case 'update':
      $primary_keys = array();
      if (db_result(db_query('SELECT nid FROM {comment_og_node} con WHERE con.nid = %d', $node->nid))) {
        $primary_keys[] = 'nid';
      }
      $record = array(
        'nid' => $node->nid,
        'grant_nonmember_access' => $node->comment_og_nonmember_access,
      );
      drupal_write_record('comment_og_node', $record, $primary_keys);
      break;
  }
}

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

  // allows group admins to delete comments
  $items['comment/delete'] = array(
    'title' => 'Delete comment',
    'page callback' => 'comment_delete',
    'access callback' => 'comment_og_comment_delete',
    'access arguments' => array(
      3,
    ),
    'type' => MENU_CALLBACK,
    'file' => 'comment.admin.inc',
    'file path' => drupal_get_path('module', 'comment'),
  );
}

/**
 * Returns true if the option is enabled and the acting user is a group admin
 */
function comment_og_comment_delete($gid = NULL) {
  $coad = variable_get('comment_og_admin_delete', 1);
  if ($gid && $coad) {
    $group = node_load($gid);
    og_load_group($group);
    return og_is_group_admin($group);
  }
  else {
    return user_access('administer comments');
  }
}

/**
 * Returns comment form if the option is enabled and the acting user is a group admin
 * Otherwise, revert to the default behavior
 */
function comment_og_comment_edit($cid = NULL, $gid = NULL) {
  $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d', $cid));
  $comment = drupal_unpack($comment);
  $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  $admins_can_edit = variable_get('comment_og_admin_edit', 1);
  $group = $gid ? node_load($gid) : NULL;
  if ($group) {
    og_load_group($group);

    // if the user would normally be able to edit the comment, use the default form
    if (comment_access('edit', $comment)) {
      return comment_form_box((array) $comment);
    }
    elseif ($admins_can_edit && og_is_group_admin($group)) {
      return comment_og_form_box((array) $comment, $group);
    }
  }
  else {
    drupal_access_denied();
  }
}

/**
 * Modified version of comment_form_box() used for editing by group administrators
 */
function comment_og_form_box($edit, $group) {
  return theme('box', NULL, drupal_get_form('comment_og_form', $edit, $group));
}

/**
 * Modified version of comment_form() used for editing by group administrators
 */
function comment_og_form(&$form_state, $edit, $group) {
  global $user;
  $op = isset($_POST['op']) ? $_POST['op'] : '';
  $node = node_load($edit['nid']);
  $edit += array(
    'name' => '',
    'mail' => '',
    'homepage' => '',
  );
  if ($user->uid && og_is_group_admin($group)) {
    if (!empty($edit['author'])) {
      $author = $edit['author'];
    }
    elseif (!empty($edit['name'])) {
      $author = $edit['name'];
    }
    else {
      $author = $edit['registered_name'];
    }
    $form['_author'] = array(
      '#type' => 'item',
      '#title' => t('Authored by'),
      '#value' => $author,
    );
    $form['author'] = array(
      '#type' => 'value',
      '#value' => $author,
    );
  }
  if (variable_get('comment_subject_field_' . $node->type, 1) == 1) {
    $form['subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#maxlength' => 64,
      '#default_value' => !empty($edit['subject']) ? $edit['subject'] : '',
    );
  }
  if (!empty($edit['comment'])) {
    $default = $edit['comment'];
  }
  else {
    $default = NULL;
  }
  $form['comment_filter']['comment'] = array(
    '#type' => 'textarea',
    '#title' => t('Comment'),
    '#rows' => 15,
    '#default_value' => $default,
    '#required' => TRUE,
  );
  if (!isset($edit['format'])) {
    $edit['format'] = FILTER_FORMAT_DEFAULT;
  }
  $form['comment_filter']['format'] = filter_form($edit['format']);
  $form['cid'] = array(
    '#type' => 'value',
    '#value' => !empty($edit['cid']) ? $edit['cid'] : NULL,
  );
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => !empty($edit['pid']) ? $edit['pid'] : NULL,
  );
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $edit['nid'],
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => !empty($edit['uid']) ? $edit['uid'] : 0,
  );

  // Only show save button if preview is optional or if we are in preview mode.
  // We show the save button in preview mode even if there are form errors so that
  // optional form elements (e.g., captcha) can be updated in preview mode.
  if (!form_get_errors() && (variable_get('comment_preview_' . $node->type, COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL || $op == t('Preview') || $op == t('Save'))) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#weight' => 19,
    );
  }
  $form['preview'] = array(
    '#type' => 'button',
    '#value' => t('Preview'),
    '#weight' => 20,
  );
  $form['#token'] = 'comment' . $edit['nid'] . (isset($edit['pid']) ? $edit['pid'] : '');
  if ($op == t('Preview')) {
    $form['#after_build'] = array(
      'comment_og_form_add_preview',
    );
  }
  if (empty($edit['cid']) && empty($edit['pid'])) {
    $form['#action'] = url('comment/reply/' . $edit['nid']);
  }
  return $form;
}

/*
 * Precursor to comment_og_form_add_preview()
 */
function comment_og_form_add_preview($form, $form_state) {
  global $user;

  // We assume that the acting user isn't the original comment author, so we add
  // the edit message to the end of the unfiltered message.
  $form_state['values']['comment'] .= comment_og_admin_edit_msg($user);
  return comment_form_add_preview($form, $form_state);
}

/**
 * Modified version of comment_form_submit() used for editing by group administrators
 */
function comment_og_form_submit($form, &$form_state) {
  _comment_form_submit($form_state['values']);
  global $user;

  // We assume that the acting user isn't the original comment author, so we add
  // the edit message to the end of the unfiltered message.
  $form_state['values']['comment'] .= comment_og_admin_edit_msg($user);
  if ($cid = comment_save($form_state['values'])) {
    $node = node_load($form_state['values']['nid']);

    // Add 1 to existing $node->comment count to include new comment being added.
    $comment_count = $node->comment_count + 1;
    $page = comment_new_page_count($comment_count, 1, $node);
    $form_state['redirect'] = array(
      'node/' . $node->nid,
      $page,
      "comment-{$cid}",
    );
    return;
  }
}

/**
 * Helper function that builds the "Edited by" message when a group
 * administrator edits a message
 *
 * @param user
 *    Valid user object
 * @param author
 *    Original comment author name
 * @return Either a tokenized "Edited by" message or the default
 *    message depending on the presence of @param user
 */
function comment_og_admin_edit_msg($user = NULL) {
  $default_msg = t('[This post has been modified by @user]', array(
    '@user' => '%user',
  ));
  if ($user) {

    // prepare and the message for display by replacing tokens
    $date = time();
    $message = check_plain(variable_get('comment_og_admin_edit_msg', $default_msg));
    return $message ? theme('comment_og_edit_msg', str_replace('%user', strip_tags(theme('username', $user)), $message)) : '';
  }
  else {
    return $default_msg;
  }
}

/**
 * Theme function that adds HTML to the "Edited by" message
 *
 * @param msg
 *    "Edited by" message defined by the user
 * @return themed message
 */
function theme_comment_og_edit_msg($message) {
  return $message ? "\n\n" . '<p class="edit-msg">' . $message . '</p>' : '';
}

Functions

Namesort descending Description
comment_og_access Determine if comments may be posted by the active user to the given group.
comment_og_admin_edit_msg Helper function that builds the "Edited by" message when a group administrator edits a message
comment_og_comment_delete Returns true if the option is enabled and the acting user is a group admin
comment_og_comment_edit Returns comment form if the option is enabled and the acting user is a group admin Otherwise, revert to the default behavior
comment_og_form Modified version of comment_form() used for editing by group administrators
comment_og_form_add_preview
comment_og_form_alter
comment_og_form_box Modified version of comment_form_box() used for editing by group administrators
comment_og_form_submit Modified version of comment_form_submit() used for editing by group administrators
comment_og_link
comment_og_link_alter Implementation of hook_link_alter(). Removes comment link for non-members
comment_og_menu Implementation of hook_menu().
comment_og_menu_alter Implementation of hook_menu_alter().
comment_og_nodeapi Implement hook_nodeapi() to save configuration changes on group node forms.
comment_og_settings Implementation of hook_settings().
comment_og_theme Implementation of hook_theme().
theme_comment_og_edit_msg Theme function that adds HTML to the "Edited by" message
_comment_og_allow_nonmember_access Determine if a group's comments are only restricted to members.