Index: profile.module
===================================================================
RCS file: /var/www/vhosts/davidherron.com/cvsroot/drupal/modules/profile.module,v
retrieving revision 1.3
retrieving revision 1.5
diff -u -F^f -r1.3 -r1.5
--- profile.module	27 May 2006 12:11:06 -0000	1.3
+++ profile.module	28 Jul 2006 04:21:43 -0000	1.5
@@ -1,5 +1,5 @@
 <?php
-// $Id: profile.module,v 1.3 2006/05/27 12:11:06 david Exp $
+// $Id: profile.module,v 1.5 2006/07/28 04:21:43 david Exp $
 
 /**
  * @file
@@ -251,6 +251,15 @@ function profile_field_form($arg = NULL)
     '#default_value' => isset($edit['visibility']) ? $edit['visibility'] : PROFILE_PUBLIC,
     '#options' => array(PROFILE_HIDDEN => t('Hidden profile field, only accessible by administrators, modules and themes.'), PROFILE_PRIVATE => t('Private field, content only available to privileged users.'), PROFILE_PUBLIC => t('Public field, content shown on profile page but not used on member list pages.'), PROFILE_PUBLIC_LISTINGS => t('Public field, content shown on profile page and on member list pages.')),
   );
+  $all_roles = user_roles();
+  unset($all_roles[DRUPAL_ANONYMOUS_RID]);
+  $form['fields']['roles'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Roles'),
+    '#options' => $all_roles,
+    '#default_value' => isset($edit['roles']) ? unserialize($edit['roles']) : "",
+    '#description' => t('Select the roles this for which profile field should be available.')
+  );
   if ($type == 'selection' || $type == 'list' || $type == 'textfield') {
     $form['fields']['page'] = array('#type' => 'textfield',
       '#title' => t('Page title'),
@@ -320,13 +329,13 @@ function profile_field_form_validate($fo
  */
 function profile_field_form_submit($form_id, $form_values) {
   if (!isset($form_values['fid'])) {
-    db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s')", $form_values['title'], $form_values['name'], $form_values['explanation'], $form_values['category'], $form_values['type'], $form_values['weight'], $form_values['required'], $form_values['register'], $form_values['visibility'], $form_values['options'], $form_values['page']);
+    db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, options, page, roles) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s')", $form_values['title'], $form_values['name'], $form_values['explanation'], $form_values['category'], $form_values['type'], $form_values['weight'], $form_values['required'], $form_values['register'], $form_values['visibility'], $form_values['options'], $form_values['page'], serialize($form_values['roles']));
 
     drupal_set_message(t('The field has been created.'));
     watchdog('profile', t('Profile field %field added under category %category.', array('%field' => theme('placeholder', $form_values['title']), '%category' => theme('placeholder', $form_values['category']))), WATCHDOG_NOTICE, l(t('view'), 'admin/settings/profile'));
   }
   else {
-    db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, register = %d, visibility = %d, options = '%s', page = '%s' WHERE fid = %d", $form_values['title'], $form_values['name'], $form_values['explanation'], $form_values['category'], $form_values['weight'], $form_values['required'], $form_values['register'], $form_values['visibility'], $form_values['options'], $form_values['page'], $form_values['fid']);
+    db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, register = %d, visibility = %d, options = '%s', page = '%s', roles = '%s' WHERE fid = %d", $form_values['title'], $form_values['name'], $form_values['explanation'], $form_values['category'], $form_values['weight'], $form_values['required'], $form_values['register'], $form_values['visibility'], $form_values['options'], $form_values['page'], serialize($form_values['roles']), $form_values['fid']);
 
     drupal_set_message(t('The field has been updated.'));
   }
@@ -416,7 +425,7 @@ function profile_browse() {
 
     // Compile a list of fields to show.
     $fields = array();
-    $result = db_query('SELECT name, title, type, weight FROM {profile_fields} WHERE fid != %d AND visibility = %d ORDER BY weight', $field->fid, PROFILE_PUBLIC_LISTINGS);
+    $result = db_query('SELECT name, title, type, weight, roles FROM {profile_fields} WHERE fid != %d AND visibility = %d ORDER BY weight', $field->fid, PROFILE_PUBLIC_LISTINGS);
     while ($record = db_fetch_object($result)) {
       $fields[] = $record;
     }
@@ -469,7 +478,7 @@ function profile_browse() {
   else {
     // Compile a list of fields to show.
     $fields = array();
-    $result = db_query('SELECT name, title, type, weight FROM {profile_fields} WHERE visibility = %d ORDER BY category, weight', PROFILE_PUBLIC_LISTINGS);
+    $result = db_query('SELECT name, title, type, weight, roles FROM {profile_fields} WHERE visibility = %d ORDER BY category, weight', PROFILE_PUBLIC_LISTINGS);
     while ($record = db_fetch_object($result)) {
       $fields[] = $record;
     }
@@ -492,9 +501,9 @@ function profile_browse() {
 }
 
 function profile_load_profile(&$user) {
-  $result = db_query('SELECT f.name, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
+  $result = db_query('SELECT f.name, f.type, v.value, f.roles FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
   while ($field = db_fetch_object($result)) {
-    if (empty($user->{$field->name})) {
+    if (_profile_user_can_have_field($field, $user) && empty($user->{$field->name})) {
       $user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
     }
   }
@@ -502,13 +511,16 @@ function profile_load_profile(&$user) {
 
 function profile_save_profile(&$edit, &$user, $category) {
   if ((arg(0) == 'user' && arg(1) == 'register') || (arg(0) == 'admin' && arg(1) == 'user' && arg(2) == 'create')) {
-    $result = db_query('SELECT fid, name, type FROM {profile_fields} WHERE register = 1 AND visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
+    $result = db_query('SELECT fid, name, type, roles FROM {profile_fields} WHERE register = 1 AND visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
   }
   else {
-    $result = db_query("SELECT fid, name, type FROM {profile_fields} WHERE LOWER(category) = LOWER('%s') AND visibility != %d", $category, PROFILE_HIDDEN);
+    $result = db_query("SELECT fid, name, type, roles FROM {profile_fields} WHERE LOWER(category) = LOWER('%s') AND visibility != %d", $category, PROFILE_HIDDEN);
     // Use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
   }
   while ($field = db_fetch_object($result)) {
+    if (!_profile_user_can_have_field($field, $user)) {
+      continue;
+    }
     if (_profile_field_serialize($field->type)) {
        $edit[$field->name] = serialize($edit[$field->name]);
     }
@@ -527,7 +539,7 @@ function profile_view_field($user, $fiel
          && (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
          && !empty($field->page);
 
-  if ($value = $user->{$field->name}) {
+  if (_profile_user_can_have_field($field, $user) && $value = $user->{$field->name}) {
     switch ($field->type) {
       case 'textarea':
         return check_markup($value);
@@ -577,6 +589,9 @@ function profile_view_profile($user) {
   }
 
   while ($field = db_fetch_object($result)) {
+    if (!_profile_user_can_have_field($field, $user)) {
+      continue;
+    }
     if ($value = profile_view_field($user, $field)) {
       $description = ($field->visibility == PROFILE_PRIVATE) ? t('The content of this field is private and only visible to yourself.') : '';
       $title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
@@ -620,6 +635,9 @@ function profile_form_profile($edit, $us
   }
 
   while ($field = db_fetch_object($result)) {
+    if (!_profile_user_can_have_field($field, $user)) {
+      continue;
+    }
     $category = $field->category;
     if (!isset($fields[$category])) {
       $fields[$category] = array('#type' => 'fieldset', '#title' => $category, '#weight' => $w++);
@@ -786,3 +804,37 @@ function _profile_field_types($type = NU
 function _profile_field_serialize($type = NULL) {
   return $type == 'date';
 }
+
+
+function _profile_user_can_have_field($field, $user) {
+  // Flag to detect whether one of the roles are set for this field
+  $hadnonzero = FALSE;
+  if (isset($field->roles) && $field->roles != "") {
+    $field_roles = unserialize($field->roles);
+    if (is_array($field_roles)) {
+      unset($field_roles[DRUPAL_ANONYMOUS_RID]);
+      foreach ($field_roles as $rolenum => $roleval) {
+        if ($roleval != 0) {
+          if (isset($user->roles[$rolenum])) {
+            // If the role is set for this field, and this user has this role,
+            // then they are allowed to have this field
+            return TRUE;
+          }
+          // We're looking at a role that is set for this field
+          // So set the flag
+          $hadnonzero = TRUE;
+        }
+      }
+    }
+  }
+
+  if ($hadnonzero == FALSE) {
+    // If no roles were set for this field, then this means that the field
+    // is open for everybody to use.
+    return TRUE;
+  }
+  // Otherwise the role was set for this field, but the user did not have
+  // the role.  So they are not allowed the field.
+  return FALSE;
+}
+
