? og_privacy.889468.patch
Index: og_privacy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og_privacy/og_privacy.module,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 og_privacy.module
--- og_privacy.module	2 Sep 2010 22:37:39 -0000	1.1.2.1
+++ og_privacy.module	7 Sep 2010 22:05:36 -0000
@@ -1,6 +1,8 @@
 <?php
 // $Id: og_privacy.module,v 1.1.2.1 2010/09/02 22:37:39 grayside Exp $
 
+define('OG_PRIVACY_BATCH_SET', 10);
+
 /**
  * Implementation of hook_enable().
  */
@@ -112,3 +114,132 @@ function og_privacy_set_privacy_reason($
 function og_privacy_get_privacy_reason($node) {
   return og_privacy_set_privacy_reason($node);
 }
+
+/**
+ * Implementation of hook_spaces_og_preset_privacy_change().  
+ */
+function og_privacy_spaces_og_preset_privacy_change($privacy, $group_node, $space) {
+  if ($privacy) {
+    // If group is becoming more private, do nothing.
+    return;
+  }
+  $results = db_query("SELECT nid FROM {og_ancestry} WHERE group_nid = %d",
+            array($group_node->nid)
+  );
+  $nid = array();
+  while ($result = db_fetch_object($results)) {
+    $nid[] = $result->nid;
+  }
+  og_privacy_enforce_policies($nid, $group_node->nid);
+}
+
+/**
+ * Calculate privacy status for a set of nodes.
+ *
+ * @param $nid
+ *  NID or Array of NIDs for nodes to process.
+ * @param $gid
+ *  NID of group node.
+ */
+function og_privacy_enforce_policies($nid, $gid) {
+  if (!is_array($nid)) {
+    $nid = array($nid);
+  }
+
+  if (count($nid) < OG_PRIVACY_BATCH_SET) {
+    foreach ($nid as $item) {
+      _og_privacy_enforce_policies_by_node($item);
+    }
+  }
+  else {
+    og_privacy_enforce_policies_by_batch($nid, $gid);
+  }
+}
+
+/**
+ * Check and apply privacy status for a specified node.
+ *
+ * @param $nid
+ *  Node ID of the node to update.
+ *
+ * @return
+ *  Boolean. TRUE for Public, FALSE for Private.
+ */
+function _og_privacy_enforce_policies_by_node($nid) {
+  $node = node_load($nid);
+  if (og_privacy_node_denied($node)) {
+    db_query("UPDATE {og_access_post} SET og_public=%d WHERE nid=%d",
+      array(OG_VISIBLE_GROUPONLY, $nid)
+    );
+  }
+  else {
+    db_query("UPDATE {og_access_post} SET og_public=%d WHERE nid=%d",
+      array(OG_VISIBLE_BOTH, $nid)
+    );
+  }
+  return (bool)!$private;
+}
+
+/**
+ * Dispatch node updates to batch process.
+ *
+ * @param $nid
+ *  NIDs of all nodes to be processed.
+ * @param $gid
+ *  NID of group node.
+ */
+function og_privacy_enforce_policies_by_batch($nid, $gid) {
+  $batch = array(
+    'title' => t('Updating Group Post Privacy'), // Title to display while running.
+    'operations' => array(), // Operations to complete, in order. Defined below.
+    'finished' => '_og_privacy_batch_process_finished',
+    'init_message' => t('Initializing...'),
+    'progress_message' => t('Updated  @current out of @total posts.'),
+    'error_message' => t('Encountered an error while updating group post privacy settings.'),
+  );
+  $batch['operations'][] = array('_og_privacy_batch_process', array($nid));
+
+  batch_set($batch);
+  batch_process('node/' . $gid);
+}
+
+/**
+ * Run through a set of nodes and update them.
+ * Callback process for batch operation.
+ */
+function _og_privacy_batch_process(&$context, $nid) {
+  if (!isset($context['sandbox']['progress'])) {
+    $context['sandbox']['progress'] = 0;
+    $context['sandbox']['current_node'] = 0;
+    $context['sandbox']['max'] = count($nid);
+  }
+  if ($context['sandbox']['max'] > $context['sandbox']['current_node'] + OG_PRIVACY_BATCH_SET) {
+    $limit = OG_PRIVACY_BATCH_SET;
+  }
+  else {
+    $limit = $context['sandbox']['max'] - $context['sandbox']['current_node'];
+  }
+
+  for ($i = 0; $i < $limit; $i++) {
+    $context['sandbox']['current_node']++;
+    og_privacy_enforce_policies_by_node($nid[$context['sandbox']['current_node']]);
+    $context['results'][] = $nid[$context['sandbox']['current_node']];
+  }
+  $context['finished'] = $context['sandbox']['progress'] == $context['sandbox']['max'];
+}
+
+/**
+ * Batch 'finished' callback
+ */
+function _og_privacy_batch_process_complete($success, $results, $operations) {
+  if ($success) {
+    $message = count($results) . t('posts processed.');
+  }
+  else {
+    $error_operation = reset($operations);
+    $message = t('An error occurred while processing %error_operation with arguments: @arguments',
+      array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE))
+    );
+  }
+  drupal_set_message($message);
+}
