From 262b4c9b5ea926d7992b34b01a74b6069bee21fd Mon Sep 17 00:00:00 2001
From: Nathan Phillip Brink <ohnobinki@ohnopublishing.net>
Date: Tue, 5 Jul 2011 15:11:59 -0400
Subject: [PATCH] Fix issue reported by ohnobinki: Add support for preventing
 non-member comments per group.

---
 comment_og.install |   63 +++++++++++++++++++++++++++++++++
 comment_og.module  |   98 +++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 156 insertions(+), 5 deletions(-)
 create mode 100644 comment_og.install

diff --git a/comment_og.install b/comment_og.install
new file mode 100644
index 0000000..799e9d6
--- /dev/null
+++ b/comment_og.install
@@ -0,0 +1,63 @@
+<?php
+/**
+ * @file
+ *   Installation hooks for comment_og.
+ */
+
+/**
+ * Implementation of hook_schema().
+ */
+function comment_og_schema() {
+  $schema = array();
+
+  $schema['comment_og_node'] = array(
+    'description' => 'Stores comment permission settings for each group.',
+    'fields' => array(
+      'nid' => array(
+        'description' => 'The group\'s {og}.nid',
+        'type' => 'int',
+        'size' => 'normal',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'grant_nonmember_access' => array(
+        'description' => 'Allow non-members to post comments to this particular group',
+        'type' => 'int',
+        'size' => 'tiny',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => (int)FALSE,
+      ),
+    ),
+    'primary key' => array('nid'),
+    'indexes' => array(
+      'nid' => array('nid'),
+    ),
+    'foreign keys' => array(
+      'nid' => array(
+        'table' => 'node',
+        'columns' => array('nid' => 'nid'),
+      ),
+    ),
+  );
+
+  return $schema;
+}
+
+/**
+ * Implement hook_install().
+ */
+function comment_og_install() {
+  drupal_install_schema('comment_og');
+}
+
+/**
+ * Implement hook_update_N().
+ */
+function comment_og_update_6100(&$sandbox) {
+  $ret = array();
+
+  $ret = array_merge($ret, drupal_install_schema('comment_og'));
+
+  return $ret;
+}
diff --git a/comment_og.module b/comment_og.module
index 89c88ea..e043169 100644
--- a/comment_og.module
+++ b/comment_og.module
@@ -43,6 +43,12 @@ function comment_og_settings() {
     '#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);
 }
 
@@ -66,9 +72,9 @@ function comment_og_link_alter(&$links, $node, $comment = NULL) {
   
   if ($group) {
     if ($node->comment && isset($node->og_groups)) {
-      if (!og_is_group_member($group)) unset($links['comment_add']);
+      if (!comment_og_access($group)) unset($links['comment_add']);
       if ($comment) {
-        if (!og_is_group_member($group)) unset($links['comment_reply']);
+        if (!comment_og_access($group)) unset($links['comment_reply']);
       }
     }
   }
@@ -94,7 +100,7 @@ function comment_og_link($type, $object, $teaser = FALSE) {
       $links['comment_delete'] = array(
         'title' => t('delete'),
         'href' => "comment/delete/$object->cid/$group->nid"
-      );	 
+      ); 
     }
   }
   return $links;
@@ -108,9 +114,15 @@ function comment_og_form_alter(&$form, $form_state, $form_id) {
 
   // 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, disable the comment form
-      if (!og_is_group_member(og_get_group_context())) {
+      /*
+       * 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'),
@@ -126,6 +138,82 @@ function comment_og_form_alter(&$form, $form_state, $form_id) {
         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;
   }
 }
 
-- 
1.7.5.rc3

