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.16
diff -u -r1.3.2.2.2.16 cck_field_privacy.module
--- cck_field_privacy.module	16 Aug 2009 21:59:16 -0000	1.3.2.2.2.16
+++ cck_field_privacy.module	16 Aug 2009 22:16:58 -0000
@@ -100,7 +100,7 @@
 function cck_field_privacy_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
   global $user;
   $deny = FALSE;
-
+  
   if ($user->uid != $node->uid) {
     $node_user = user_load(array('uid' => $node->uid));
     if ($op == 'view') {
@@ -108,21 +108,25 @@
       if (variable_get('cckfp_trouble', FALSE)) {
         return;
       }
+      
       //Assume we are 'all clear' first, and see if anything changes that status
       $access_clear = TRUE;
-      $type = $node->type;
+      $node_type = $node->type;
       $permissions = array();
       $types =  variable_get('cckfp_types', NULL);
       //check node type is enabled
-      if (in_array($type, $types, TRUE)) {
+      if (in_array($node_type, $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 = unserialize($row->permission);         
-            $permissions[$row->field_name] = $row;
-          }
+        //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 = unserialize($row->permission);         
+          $permissions[$row->field_name] = $row;
+        }
+        
+        $type_fields = content_fields(NULL, $node_type);
+        
+        if (count($permissions)) {
           foreach ($permissions as $field_name => $fieldpriv) {
             //A permission setting exists, so let's handle the permissions
             foreach ($fieldpriv->permission as $priv) {
@@ -138,6 +142,13 @@
             if ($access_clear == FALSE) {
               $node->$field_name['#access'] = FALSE;
               $node->content[$field_name]['#access'] = FALSE;
+              // if the thing is part of a group, set access to false within the group
+              
+              if ($type_fields[$field_name]['display_settings']['parent'] && !is_numeric($type_fields[$field_name]['display_settings']['parent'])) {
+                $group_name = $type_fields[$field_name]['display_settings']['parent'];
+                $node->content[$group_name]['group'][$field_name]['#access'] = FALSE;
+                $node->content[$group_name]['group'][$field_name]['field']['#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.
@@ -161,13 +172,19 @@
                     if ($child_access == FALSE) {
                       //$node->$field_name[$child['field_name']]['#access'] = FALSE;
                       $node->content[$field_name]['group'][$child['field_name']]['field']['#access'] = FALSE;
+                      $node->content[$field_name]['group'][$child['field_name']]['#access'] = FALSE;
                     }
                   }
                 }
               }
             }
+            
+            // End of checking this particular group/field
           }
+          
+          // Done checking all groups/fields
         }
+        
       }
     }
   }
@@ -217,7 +234,7 @@
   if (user_access('modify own cck field privacy') || user_access('modify cck field privacy')) {
     $types = variable_get('cckfp_types', NULL);
     if (is_array($types)) {
-      if (array_key_exists($form['#node']->type, $types) || in_array($form['page']['#value']->options['content-type'], $types)) {
+      if ($types[$form['#node']->type] || in_array($form['page']['#value']->options['content-type'], $types)) {
       
         $values = unserialize(variable_get('cckfp_values', NULL));
         $node_type = $form['type']['#value'];
@@ -225,19 +242,19 @@
         // workaround : exclude user_register to avoid fields being treated
         // two times when the content profile registration module is used
 
-        if (($form_id != "user_register") && is_array($values[$node_type])) {
+        if (!(arg(0) == 'user' && arg(1) == 'register' || $form_id == "user_register") && is_array($values[$node_type])) {
 
           $privacyfields = array_filter($values[$node_type]); // get fields which need a padlock
-
+          
           if (!empty($privacyfields)) {
-
+          
             // get original author or current user (authoring new node)
             $u = (int) isset($form['#node']->uid)? $form['#node']->uid : $user->uid;
             
             // get the widgets used for fields of this node type
             $fieldtypes = content_fields(NULL, $node_type); // make use of results cached by _content_type_info, no db hit
             $needpadlock = array();
-
+            
             foreach ($privacyfields as $field => $value) {
               $prefs = array();
               // Get the current state of the field and set perms that will be used with jQ impromptu
@@ -320,8 +337,9 @@
   // only treats keys that are not properties (i.e., does not begin with '#')
   foreach (element_children($elements) as $field) {
     if (isset($elements[$field]) && $elements[$field]) {    
-      if (array_key_exists($field, $needpadlock)) { // form element is handled by cckfp
+      if ($needpadlock[$field]) { // form element is handled by cckfp
         $lockimg = "<a href='#' id='". $field ."link'><img src='". base_path() . drupal_get_path('module', 'cck_field_privacy') ."/padlock.png' alt='". t('Privacy') ."' /></a>";
+        
         if (isset($elements[$field][0]['#type'])) { // content sub field first
           switch ($elements[$field][0]['#type']) {
             case "link" : // link widget
@@ -329,16 +347,19 @@
               // with different suffix 'Title' and 'URL' : only the 'first label id' will work
               // should use the widget description or some theming on the widget output
               $elements[$field][0]['#title'] = $lockimg ." ". $elements[$field][0]['#title'];
+              $needpadlock[$field] = FALSE;
               break;
 
             case "userreference_autocomplete" :
               // ugly display if content_multiple_values is used
               // padlock shoud be shown after $elements[$field]['#title'] but it does not react
               $elements[$field][0]['#title'] .= " ". $lockimg;
+              $needpadlock[$field] = FALSE;
               break;
                     
             default : // assume field is a standard CCK field and not fieldgroup
               $elements[$field][0]['#title'] .= " ". $lockimg;
+              $needpadlock[$field] = FALSE;
               break;
           }
         }
@@ -350,6 +371,7 @@
               // but an <h2> tag without link also use this label
               // html validation error without modifiing tabs' template
               $elements[$field]['#title'] .= $lockimg;
+              $needpadlock[$field] = FALSE;
               break;
 
             case "fieldset" :
@@ -357,25 +379,30 @@
                     && ($elements[$field]['#collapsible'] == 1)) { // collapsible fieldset
                      // using description because a padlock in the clickable legend tag will not work
                     $elements[$field]['#description'] = $lockimg ." ". $elements[$field]['#description'];
+                    $needpadlock[$field] = FALSE;
+              }
+              else {
+                $elements[$field]['#title'] .= " ". $lockimg;
+                $needpadlock[$field] = FALSE;
               }
-              else { $elements[$field]['#title'] .= " ". $lockimg; }
               break;
 
             case "userreference_select" :
             case "optionwidgets_select" :
               $elements[$field]['#title'] .= " ". $lockimg;
+              $needpadlock[$field] = FALSE;
               break;
 
             default : // TODO : field probably not properly handled
               $elements[$field]['#title'] .= " ". $lockimg;
+              $needpadlock[$field] = FALSE;
               break;
           }
         }
         else { 
-          /* field type not properly handled */ 
+          /* field type not properly handled */
         }
-
-        $needpadlock[$field] = FALSE;
+        
         $needpadlock = array_filter($needpadlock);
       }
 

