Index: cck_field_privacy.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck_field_privacy/cck_field_privacy.js,v
retrieving revision 1.2.2.1.2.2
diff -u -r1.2.2.1.2.2 cck_field_privacy.js
--- cck_field_privacy.js	11 Jun 2009 22:02:30 -0000	1.2.2.1.2.2
+++ cck_field_privacy.js	7 Aug 2009 21:52:42 -0000
@@ -1,7 +1,6 @@
 var cck_field_privacy = {
   init: function() {
     if (typeof(Drupal) == 'undefined' || typeof(Drupal.settings) == 'undefined') return; // abort
-
     for (field_name in Drupal.settings.cck_field_privacy.default_value) {
       $('#'+ field_name +'link').bind('click', cck_field_privacy.click); // bind padlock elements
     }
@@ -9,49 +8,73 @@
 
   click: function() {
     var field_name = $(this).attr('id').substr(0, $(this).attr('id').length-4),
-        field_state = Drupal.settings.cck_field_privacy.default_value[field_name];
-
+    field_states = Drupal.settings.cck_field_privacy.default_value[field_name];
+		var rels = Drupal.settings.cck_field_privacy.relationships;
+		// TODO: I don't like the fact checkboxes are used, should be radiobuttons!
+		//				this would also solve the issue where you can uncheck the option and submit nothing selected
+		var checkboxes = '';
+		for(var rel in rels){
+		  // don't use rels[rel] as a value; this will cause issues with translations!
+			checkboxes += "<div><label><input type='checkbox' value='"+rel+"' name='"+rels[rel]+"' />"+rels[rel]+"</label></div>";
+    }
+				
     // display prompt
     var prompt = $.prompt(
-      // @TODO: Output the list of privacy options in Drupal.settings.
-      // @TODO: Make Buddies conditional based on module_exists('buddylist').
-      //        then remove as a dependency.
-      // @TODO: Provide all available relationships when module_exists('user_relationships').
-      '<p><strong>'+Drupal.t("Privacy Settings")+'</strong></p>'+
-      '<form>'+
-      '  <div><label><input type="radio" value="e" name="privacy" /> '+Drupal.t("Everyone")+'</label></div>'+
-      '  <div><label><input type="radio" value="b" name="privacy" /> '+Drupal.t("Buddies")+'</label></div>'+
-      '  <div><label><input type="radio" value="n" name="privacy" /> '+Drupal.t("Nobody")+'</label></div>'+
-      '</form>', {
-
-      overlayspeed: 'fast',
-      promptspeed: 'fast',
-      show: 'slideDown',
-      loaded: (function(field_name) { return function() {
-        $("input[type='radio']", this).each(function() {
-          // set default radio checked on load
-          var e = $(this);
-          e.attr('checked', field_state == e.val()? 'checked' : '');
-        }).change((function(field_name) { return function() {
-          // save changes
-          var radio = $(this),
-              form = radio.parents('form'),
-              prompt = form.parents('div.jqicontainer');
-
-          Drupal.settings.cck_field_privacy.default_value[field_name] = radio.val(); // remember setting
-          $('#jqibuttonOk', prompt).trigger('click'); // automatically close prompt
-          $.post( // save changes
-            Drupal.settings.cck_field_privacy.action, {
-            user: Drupal.settings.cck_field_privacy.uid,
-            field: field_name,
-            type: Drupal.settings.cck_field_privacy.content_type,
-            setting: radio.val()
-          });
-        };})(field_name))
-      };})(field_name)
-    });
-  return false;
-  }
+    // TODO: Output the list of privacy options in Drupal.settings.
+    // TODO: Make Buddies conditional based on module_exists('buddylist').
+    //        then remove as a dependency.
+    // TODO: Provide all available relationships when module_exists('user_relationships').
+    '<p><strong>'+Drupal.t("Privacy settings")+'</strong></p>'+
+    '<form>'+checkboxes+
+    '</form>', {
+	
+	    overlayspeed: 'fast',
+	    promptspeed: 'fast',
+	    buttons: { Ok: true, Cancel: false },
+	    submit: function(v, m, f) {
+	    	if(v == true) {
+	    		var settings = new Array();
+	        for(var i in f) {
+	        	settings.push(f[i]);
+	        }
+	      	// remember setting
+	      	Drupal.settings.cck_field_privacy.default_value[field_name] = f; 
+	       	$.post( // save changes
+	        	Drupal.settings.cck_field_privacy.action, {
+	          user: Drupal.settings.cck_field_privacy.uid,
+	          field: field_name,
+	          type: Drupal.settings.cck_field_privacy.content_type,
+	          'settings[]': settings
+	        });
+	        return true;
+	      }
+	      return true;
+	    }, 
+	    show: 'slideDown',
+	    loaded: (function(field_name) {
+		  	return function() {
+	      	$("input[@type='checkbox']", this).each(function() {
+	        	// set default checked on load
+	          var e = $(this);        
+	          e.attr('checked', field_states[e.val()] == e.val()? 'checked' : '');
+					// .click instead of .change to keep the script working in IE!
+	        }).click(function(field_name) { 
+	        	var name = $(this).attr('name');    		
+	        	if ($(this).is(':checked')) {
+	  					// Uncheck all other checkboxes.
+	  					$("input[@name!='"+$(this).attr('name')+"']").each(function(){
+	  						$(this).attr('checked',false);
+	  					});
+					  }
+	       	});
+				};
+		  }(field_name)) 
+		});
+		return false;
+  } 
 };
 
-$(cck_field_privacy.init); // onload
\ No newline at end of file
+$(cck_field_privacy.init); // onload
+Drupal.behaviors.cck_field_privacy = function(context) {
+	cck_field_privacy.init();
+}
\ No newline at end of file
Index: cck_field_privacy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck_field_privacy/cck_field_privacy.module,v
retrieving revision 1.3.2.2.2.14
diff -u -r1.3.2.2.2.14 cck_field_privacy.module
--- cck_field_privacy.module	7 Aug 2009 21:48:33 -0000	1.3.2.2.2.14
+++ cck_field_privacy.module	7 Aug 2009 22:20:35 -0000
@@ -120,30 +120,53 @@
         $permissions[$field['field_name']]->field_name = $field['field_name'];
         $permissions[$field['field_name']]->permission = $default;
       }
-      $result = db_query("SELECT field_name, permission FROM {cckfp} WHERE uid = %d AND type_name = '%s' ORDER BY field_name DESC", $node->uid, $node->type);
-      if ($result) {
-        //Put all of the permissions entries into an array so we can process them all at once.
-        while ($row = db_fetch_object($result)) {
-          $permissions[$row->field_name] = $row;
-        }
-        foreach ($permissions as $field_name => $fieldpriv) {
-          //A permission setting exists, so let's handle the permissions            
-          $access_clear = _cck_field_privacy_access_check($fieldpriv, $node, $user, $node_user);
-          if ($access_clear == FALSE) {
-            $node->$field_name['#access'] = FALSE;
-            $node->content[$field_name]['#access'] = FALSE;
+      $cckfp_types =  variable_get('cckfp_types', NULL);
+      //check node type is enabled
+      if (in_array($type, $cckfp_types, TRUE)) {
+        $result = db_query("SELECT field_name, permission FROM {cckfp} WHERE uid = %d AND type_name = '%s' ORDER BY field_name DESC", $node->uid, $node->type);
+        if ($result) {
+          //Put all of the permissions entries into an array so we can process them all at once.      
+          while ($row = db_fetch_object($result)) {
+            $row->permission = (is_array($row->permission)) ? unserialize($row->permission) : $row->permission;
+            $permissions[$row->field_name] = $row;
           }
-          else {
-            //A fieldgroup may be clear for access, but its child fields may not.  Check to see if the field is a group, then process its children.
-            if (is_array($node->content[$field_name]['group'])) {
-              $node_groups = fieldgroup_groups($node->type);
-              $group_fields = $node_groups[$field_name]['fields'];
-              foreach ($group_fields as $child) {
-                if (array_key_exists($child['field_name'], $permissions)) {
-                  $child_access = _cck_field_privacy_access_check($permissions[$child['field_name']], $node, $user, $node_user);
-                  if ($child_access == FALSE) {
-                    //$node->$field_name[$child['field_name']]['#access'] = FALSE;
-                    $node->content[$field_name]['group'][$child['field_name']]['field']['#access'] = FALSE;
+          foreach ($permissions as $field_name => $fieldpriv) {
+            //A permission setting exists, so let's handle the permissions
+            $clear = _cck_field_privacy_access_check($fieldpriv->permission, $node, $user, $node_user);
+            if ($clear) {
+              $access_clear = TRUE;
+              break;
+            }
+            else {
+              $access_clear = FALSE;
+            }
+            if ($access_clear == FALSE) {
+              $node->$field_name['#access'] = FALSE;
+              $node->content[$field_name]['#access'] = FALSE;
+            }
+            else {
+             //A fieldgroup may be clear for access, but its child fields may not.  Check to see if the field is a group, then process its children.
+              if (is_array($node->content[$field_name]['group'])) {
+                $node_groups = fieldgroup_groups($node->type);
+                $group_fields = $node_groups[$field_name]['fields'];
+              
+                foreach ($group_fields as $child) {
+                  if (array_key_exists($child['field_name'], $permissions)) {
+                    //print_r($permissions[$child['field_name']]);
+                    foreach ($permissions[$child['field_name']]->permission as $child_priv) {
+                      $child_clear = _cck_field_privacy_access_check($child_priv, $node, $user, $node_user);
+                      if ($child_clear) {
+                        $child_access = TRUE;
+                        break;
+                      }
+                      else {
+                          $child_access = FALSE;
+                      }
+                    }
+                    if ($child_access == FALSE) {
+                      //$node->$field_name[$child['field_name']]['#access'] = FALSE;
+                      $node->content[$field_name]['group'][$child['field_name']]['field']['#access'] = FALSE;
+                    }
                   }
                 }
               }
@@ -170,19 +193,21 @@
  */
 function _cck_field_privacy_access_check($permission, $node, $user, $node_user) {
   $access_clear = TRUE;
-  if ($permission->permission == 'b') {
-    foreach (module_invoke_all('cck_field_privacy_access', $node_user, $user) as $access_result ) {
-      if (!$access_result) {   
+  
+  if ($permission == 'e') {
+  }
+  else if ($permission == 'n') {
+    $access_clear = FALSE;
+  }
+  else{
+    foreach (module_invoke_all('cck_field_privacy_access', $node_user, $user, $permission) as $access_result ) {
+      if (!$access_result) {     
         $access_clear=FALSE;
       }
       continue; 
     }
   }
-  if ($permission->permission == 'e') {
-  }
-  if ($permission->permission == 'n') {
-    $access_clear = FALSE;
-  }
+  
   return $access_clear;
 }
 
@@ -222,11 +247,18 @@
 
               // Get the current state of the field and set perms that will be used with jQ impromptu
               $field_pref_sql = db_query("SELECT permission FROM {cckfp} WHERE uid = %d AND type_name = '%s' AND field_name = '%s'", $u, $node_type, $field);
-              $field_pref = (string) db_result($field_pref_sql);
+              $field_pref = unserialize((string)db_result($field_pref_sql));
               if ($field_pref == "") {
-                $field_pref = variable_get('cckfp_'. $node_type .'_default', 'e'); // Set to the default state of the field
+                 //$field_pref = variable_get('cckfp_'. $node_type .'_default', 'e'); // Set to the default state of the field
+                 $pref = variable_get('cckfp_'. $node_type .'_default', 'everyone'); // Set to the default state of the field
+                 $prefs[$pref] = $pref;
               }
-              $privacyfields[$field] = $field_pref;
+              else {
+                foreach ($field_pref as $pref) {
+                  $prefs[$pref] = $pref;
+                }
+              }
+              $privacyfields[$field] = $prefs;
               switch ($fieldtypes[$field]['widget']['type']) {
 
                 // treats special cases separately :
@@ -260,19 +292,22 @@
             if (!empty($needpadlock)) { // look for fields left recursively throught the form
               _cck_field_privacy_add_padlock($form, $needpadlock);
             }
+          //retrieve the types of relationships available
+          $rels = variable_get('cckfp_rel_type_options', 'everyone');
 
-            // include javascript and css only if needed
-            jquery_impromptu_add();
-            drupal_add_js(drupal_get_path('module', 'cck_field_privacy') .'/cck_field_privacy.js');
-            drupal_add_js(array(
-              'cck_field_privacy' => array(
-                'action' => url('cck_field_privacy/ajax'),
-                'uid' => $u,
-                'content_type' => $node_type,
-                'default_value' => $privacyfields,
-                ),
-              ), 'setting');
-          }
+          // include javascript and css only if needed
+          jquery_impromptu_add();
+          drupal_add_js(drupal_get_path('module', 'cck_field_privacy') .'/cck_field_privacy.js');
+          drupal_add_js(array(
+            'cck_field_privacy' => array(
+              'action' => url('cck_field_privacy/ajax'),
+              'uid' => $u,
+              'content_type' => $node_type,
+              'relationships' => $rels,
+              'default_value' => $privacyfields,
+              ),
+            ), 'setting');
+        }
         }
       }
     }
@@ -365,15 +400,28 @@
   global $user;
 
   if (user_access('modify cck field privacy') || (user_access('modify own cck field privacy') && $user->uid == $_POST['user'])) {
-    if (isset($_POST['user'], $_POST['field'], $_POST['type'], $_POST['setting'])) {
-      $userinfo = array();
-      $userinfo['uid'] = $_POST['user'];
+    if (isset($_POST['user'], $_POST['field'], $_POST['type'], $_POST['settings'])) {
+      $uid = $_POST['user'];
       $field = $_POST['field'];
       $type = $_POST['type'];
-      $setting = $_POST['setting'];
-      if ($userinfo != 'NULL' && $field != 'NULL' && $type != 'NULL' && $setting != 'NULL') {
-        $sql = "INSERT INTO {cckfp} (uid, field_name, type_name, permission) VALUES(%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE uid=%d, field_name='%s', type_name='%s', permission='%s'";
-        $result = db_query($sql, $userinfo['uid'], $field, $type, $setting, $userinfo['uid'], $field, $type, $setting);
+      $settings = $_POST['settings'];
+             
+      if ($uid != 'NULL' && $field != 'NULL' && $type != 'NULL' && $settings != 'NULL') {
+        $cckfp = array(
+          'uid' => $uid,
+          'field_name' => $field,
+          'type_name' => $type,
+          'permission' => $settings,         
+        );
+        
+        $select = "SELECT * FROM {cckfp} WHERE uid = %d AND field_name = '%s'";
+        $result = db_fetch_object(db_query($select, $uid, $field));
+        if ($result) {
+          drupal_write_record('cckfp', $cckfp, array('uid', 'field_name', 'type_name'));
+        }
+        else {
+          drupal_write_record('cckfp', $cckfp);
+        }
         if (!db_error()) {
         } 
         else {
@@ -450,12 +498,33 @@
             );
           }
           
-          $form['field_fields'][$enabled_type]['cckfp_' . $enabled_type . '_default'] = array(
+          $options = array(
+            'everyone' => t('everyone'),
+            'buddies' => t('buddies'),
+            'nobody' => t('nobody')
+          );
+          //if user relationship module exists grab the 
+          //relationship types
+          if (module_exists('user_relationships_api')) {
+            module_load_include('inc', 'user_relationships_api', 'user_relationships_api.api');
+            $ur_types = user_relationships_types_load();
+            
+            //build options array for form.
+            foreach ($ur_types as $rel) {
+              $options[$rel->plural_name] = $rel->plural_name;
+            }
+            unset($options['buddies']);    
+          }
+          
+          //set what options are so we can retrieve them later in the modal dialog
+          variable_set('cckfp_rel_type_options', $options);
+          
+          $form['field_fields'][$enabled_type]['cckfp_'. $enabled_type .'_default'] = array(
             '#type' => 'radios',
             '#title' => t('Default Value'),
             '#description' => t('You may choose a default privacy value for the fields in this content type.'),
-            '#default_value' => variable_get('cckfp_' . $enabled_type . '_default', 'e'),
-            '#options' => array('e' => t('Everyone'), 'b' => t('Buddies'), 'n' => t('Nobody')),
+            '#default_value' => variable_get('cckfp_'. $enabled_type .'_default', 'everyone'),
+            '#options' => $options,
             '#weight' => 1,
           );
         }
@@ -574,19 +643,23 @@
     
     if ($result) {
       while ($permissions = db_fetch_object($result)) {
-        if ($permissions->permission == 'e') {
-          continue;
-        } 
-        else if ($permissions->permission == 'n') {
-          $inaccessible_fields[$permissions->field_name] = $permissions->field_name;
-        } 
-        else if (!empty($permissions->permission)) {
-          $node_user = user_load(array('uid' => $node->uid));
-          $plugin_access_result = module_invoke($permissions->permission, 'cck_field_privacy_access', $node_user, $user);
-          //Uncomment the next line to enable debug output to the watchdog when testing views access
-          //watchdog('cckfp', 'result of module '.$permissions->permission.' check: '.print_r($plugin_access_result, TRUE));
-          if ($plugin_access_result === FALSE) {
-            $inaccessible_fields[$permissions->field_name] = $permissions->field_name;
+        if (is_array($permissions->permission)) {
+          foreach ($permissions->permission as $perm) {
+            if ($perm == 'everyone') {
+              continue;
+            } 
+            else if ($perm == 'nobody') {
+              $inaccessible_fields[$permissions->field_name] = $permissions->field_name;
+            } 
+            else if (!empty($perm)) {
+              $node_user = user_load(array('uid' => $node->uid));
+              $plugin_access_result = module_invoke($perm, 'cck_field_privacy_access', $node_user, $user);
+              //Uncomment the next line to enable debug output to the watchdog when testing views access
+              //watchdog('cckfp', 'result of module '.$permissions->permission.' check: '.print_r($plugin_access_result, TRUE));
+              if ($plugin_access_result === FALSE) {
+                $inaccessible_fields[$permissions->field_name] = $permissions->field_name;
+              }
+            }
           }
         }
       }

