Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privacy/README.txt,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 README.txt
--- README.txt	31 Jan 2009 12:51:59 -0000	1.1.2.1
+++ README.txt	31 Aug 2010 05:58:37 -0000
@@ -15,6 +15,7 @@ Follow these steps:
 3. Create your content types and additional fields with the cck module
 5. Go to admin/content/privacy and enable the fields which should be able to
 keep private (only users with 'administer privacy' rights are able to do this)
+and, optionally, assign the default value of the checkbox ('on' or 'off').
 6. When a user creates a node, he can check if a field should be private or
 not.
 
Index: privacy.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privacy/privacy.admin.inc,v
retrieving revision 1.1
diff -u -p -r1.1 privacy.admin.inc
--- privacy.admin.inc	28 Jan 2009 23:38:34 -0000	1.1
+++ privacy.admin.inc	31 Aug 2010 05:58:37 -0000
@@ -47,6 +47,13 @@ function privacy_settings() {
         '#default_value' => variable_get('privacy_'. $type['type'], array()),
         '#options' => $fields,
       );
+      $form[$fieldset]['privacy_'. $type['type'] .'_defaults'] = array(
+        '#type' => 'checkboxes',
+        '#title' => t('Field defaults'),
+        '#default_value' => variable_get('privacy_'. $type['type'] .'_defaults', array()),
+        '#options' => $fields,
+        '#description' => t('By default all checkboxes will be off, meaning that they will be viewable by everyone.  This allows individual fields to be set to default to on, i.e. private.'),
+      );
     }
   }
 
Index: privacy.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privacy/privacy.install,v
retrieving revision 1.1
diff -u -p -r1.1 privacy.install
--- privacy.install	28 Jan 2009 23:38:34 -0000	1.1
+++ privacy.install	31 Aug 2010 05:58:37 -0000
@@ -45,9 +45,42 @@ function privacy_schema() {
         'not null'        => TRUE,
         'default'         => '',
       ),
+      'is_private' => array(
+        'type'            => 'int',
+        'size'            => 'tiny',
+        'not null'        => TRUE,
+      ),
     ),
     'primary key' => array('nid', 'field_name'),
+    'indexes' => array(
+      'is_private' => array('nid', 'field_name', 'is_private'),
+    ),
   );
 
   return $schema;
 }
+
+/**
+ * Implementation of hook_update_N().
+ * Add a new field to track whether specific fields are private.
+ * Update all current records to be private.
+ * Add an index to make it faster.
+ */
+function privacy_update_6101() {
+  $ret = array();
+
+  // Add the new field.
+  db_add_field($ret, 'privacy', 'is_private', array(
+    'type'            => 'int',
+    'size'            => 'tiny',
+    'not null'        => TRUE,
+  ));
+
+  // Update all current records.
+  $ret[] = update_sql("UPDATE {privacy} SET is_private=1");
+
+  // Add an index to make it faster.
+  db_add_index($ret, 'privacy', 'is_private', array('nid', 'field_name', 'is_private'));
+  
+  return $ret;
+}
Index: privacy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privacy/privacy.module,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 privacy.module
--- privacy.module	31 Jan 2009 12:35:49 -0000	1.1.2.1
+++ privacy.module	31 Aug 2010 05:58:37 -0000
@@ -48,7 +48,7 @@ function privacy_form_alter(&$form, &$fo
   }
 }
 
-/*
+/**
  * Form alter a specific field
  */
 function privacy_form_alter_field(&$form, $field) {
@@ -88,17 +88,20 @@ function privacy_nodeapi(&$node, $op, $a
     case 'update':
       $active_privacy_fields = privacy_get_active_fields($node->type);
       foreach ($active_privacy_fields as $field) {
-        db_query("DELETE FROM {privacy} WHERE nid = %d AND field_name = '%s'", $node->nid, $field);
+        // Use drupal_write_record() to create or update existing records.
         $fieldname = 'privacy_'. $field;
-        if ( $node->$fieldname == 1 ) {
-          db_query("INSERT INTO {privacy} (nid, field_name) VALUES (%d, '%s')", $node->nid, $field);
-        }
+        $record = new StdClass();
+        $record->nid = $node->nid;
+        $record->field_name = $field;
+        $record->is_private = $node->$fieldname ? $node->$fieldname : 0;
+        drupal_write_record('privacy', $record, array('nid', 'field_name'));
+        unset($record);
       }
       break;
     case 'load':
-      $result = db_query("SELECT field_name FROM {privacy} WHERE nid = %d ", $node->nid);
+      $result = db_query("SELECT field_name, is_private FROM {privacy} WHERE nid = %d ", $node->nid);
       //are there any privacy fields activated ?
-      if ($result) {
+      if (!empty($result)) {
         $active_privacy_fields = privacy_get_active_fields($node->type);
         //if access to privacy fields, add a status to the $node object
         if ($user->uid == $node->uid || user_access('view all privacy')) {
@@ -108,8 +111,8 @@ function privacy_nodeapi(&$node, $op, $a
             $flip[$field] = 0;
           }
           //hide all privacy fields
-          while ($permissions = db_fetch_object($result)) {
-            $flip[$permissions->field_name] = 1;
+          while ($permissions = db_fetch_array($result)) {
+            $flip[$permissions['field_name']] = $permissions['is_private'];
           }
           //put all fields in the $node object
           foreach ($flip as $field => $value) {
@@ -120,10 +123,10 @@ function privacy_nodeapi(&$node, $op, $a
         //if no access to privacy fields, remove them
 	//TODO : check if there is another (drupal) way to to this in stead of using unset
         else {
-          while ($permissions = db_fetch_object($result)) {
-            $field = $permissions->field_name;
+          while ($permissions = db_fetch_array($result)) {
+            $field = $permissions['field_name'];
             //when field is private
-            if (in_array($field, $active_privacy_fields)) {
+            if (privacy_is_private($field, $node->nid)) {
               unset($node->$field);
             }
           }
@@ -154,16 +157,46 @@ function privacy_get_active_fields($type
 }
 
 function privacy_is_private($field, $nid) {
+  // Default to FALSE, i.e. not private.
+  $is_private = FALSE;
+
+  // If no $nid is present then this should be a node/add/* page.
   if ( $nid == 0 ) {
-    return FALSE;
+    if (arg(0) == 'node' && arg(1) == 'add') {
+      $type = arg(2);
+      if (!empty($type)) {
+        // Get the defaults for this content type.
+        $defaults = variable_get('privacy_'. check_plain(arg(2)) .'_defaults', array());
+    
+        // If the default value was set, return it.
+        if (is_array($defaults) && array_key_exists($field, $defaults)) {
+          $is_private = (bool) $defaults[$field];
+        }
+      }
+    }
   }
   else {
-    $result = db_result(db_query("SELECT nid FROM {privacy} WHERE nid = %d AND field_name = '%s' ", $nid, $field));
-    if ( $result ) {
-      return TRUE;
+    // Look for an existing setting, that the user assigned checked the box.
+    $result = db_fetch_array(db_query("SELECT is_private FROM {privacy} WHERE nid = %d AND field_name = '%s' ", $nid, $field));
+
+    // If a value was found, return it.
+    if (!empty($result)) {
+      $is_private = (bool) $result['is_private'];
     }
+
     else {
-      return FALSE;
+      // This should be a free operation.
+      $node = node_load($nid);
+      
+      // Get the defaults for this content type.
+      $defaults = variable_get('privacy_'. $node->type .'_defaults', array());
+      
+      // If the default value was set, return it.
+      if (is_array($defaults) && array_key_exists($field, $defaults)) {
+        $is_private = (bool) $defaults[$field];
+      }
     }
   }
+  
+  return $is_private;
 }
