| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_user().
|
| 5 |
*/
|
| 6 |
function profile_privacy_user($op, &$edit, &$account, $category = NULL) {
|
| 7 |
switch ($op) {
|
| 8 |
case 'load':
|
| 9 |
$private_fields = profile_privacy_get_user_privacy($account->uid);
|
| 10 |
foreach ($private_fields as $field_name => $private) {
|
| 11 |
$account->{'private_'. $field_name} = $private;
|
| 12 |
}
|
| 13 |
break;
|
| 14 |
case 'view':
|
| 15 |
$private_fields = profile_privacy_get_user_privacy($account->uid);
|
| 16 |
$profile_fields = profile_privacy_get_fields();
|
| 17 |
// Do not hide any information from administers
|
| 18 |
if (!user_access('administer users')) {
|
| 19 |
foreach ($profile_fields as $field) {
|
| 20 |
// If the user has set a privacy option, this always takes precidence.
|
| 21 |
if (isset($account->{'private_'. $field->name}) && $field->privacy) {
|
| 22 |
if ($account->{'private_'. $field->name}) {
|
| 23 |
// Setting the account variable and the return field both to NULL
|
| 24 |
// ensure that no output will be generated on the profile page,
|
| 25 |
// even if it's being themed with theme_user_profile().
|
| 26 |
$account->{$field->name} = NULL;
|
| 27 |
$account->content[$field->category][$field->name] = NULL;
|
| 28 |
}
|
| 29 |
else {
|
| 30 |
// Generate the fields to be returned since profile module
|
| 31 |
// thought this field was private.
|
| 32 |
$field->visibility = PROFILE_PUBLIC;
|
| 33 |
if ($value = profile_view_field($account, $field)) {
|
| 34 |
$title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
|
| 35 |
// Create a single fieldset for each category.
|
| 36 |
if (!isset($account->content[$field->category])) {
|
| 37 |
$account->content[$field->category] = array(
|
| 38 |
'#type' => 'user_profile_category',
|
| 39 |
'#title' => $field->category,
|
| 40 |
);
|
| 41 |
}
|
| 42 |
$item = array('#title' => $title,
|
| 43 |
'#value' => $value,
|
| 44 |
'#class' => $field->name,
|
| 45 |
'#type' => 'user_profile_item',
|
| 46 |
'#weight' => $field->weight,
|
| 47 |
'#attributes' => array('class' => 'profile-'. $field->name),
|
| 48 |
);
|
| 49 |
// TODO: This currently appends the now visible field to the end
|
| 50 |
// of other profile fields, should insert in original position.
|
| 51 |
$account->content[$field->category][$field->name] = $item;
|
| 52 |
}
|
| 53 |
}
|
| 54 |
}
|
| 55 |
// Over ride the default profile behavior. If a field is only available
|
| 56 |
// to "privileged users" unset the variable entirely. This affects
|
| 57 |
// all themed versions
|
| 58 |
elseif ($field->visibility == PROFILE_PRIVATE) {
|
| 59 |
$account->{$field->name} = NULL;
|
| 60 |
$account->content[$field->category][$field->name] = NULL;
|
| 61 |
}
|
| 62 |
}
|
| 63 |
}
|
| 64 |
break;
|
| 65 |
case 'update':
|
| 66 |
case 'insert':
|
| 67 |
$fields = profile_privacy_get_fields();
|
| 68 |
foreach ($fields as $field) {
|
| 69 |
if (isset($edit['private_'. $field->name])) {
|
| 70 |
profile_privacy_set_user_field_privacy($account->uid, $field->name, !$edit['private_'. $field->name]);
|
| 71 |
// Set field to NULL to prevent user.module from saving in user table.
|
| 72 |
$edit['private_'. $field->name] = NULL;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
break;
|
| 76 |
case 'delete':
|
| 77 |
profile_privacy_delete_user_privacy($account->uid);
|
| 78 |
break;
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Implementation of hook_form_alter().
|
| 84 |
*/
|
| 85 |
function profile_privacy_form_alter(&$form, &$form_state, $form_id) {
|
| 86 |
switch ($form_id) {
|
| 87 |
case 'user_profile_form':
|
| 88 |
if ($form['_category']['#value'] != 'account') {
|
| 89 |
profile_privacy_form_alter_category($form['_category']['#value'], $form['_account']['#value'], $form);
|
| 90 |
}
|
| 91 |
break;
|
| 92 |
case 'user_register':
|
| 93 |
$form_keys = element_children($form);
|
| 94 |
foreach ($form_keys as $form_key) {
|
| 95 |
$fieldset_keys = element_children($form[$form_key]);
|
| 96 |
foreach ($fieldset_keys as $fieldset_key) {
|
| 97 |
if (preg_match('/^profile_/', $fieldset_key)) {
|
| 98 |
profile_privacy_form_alter_category($form_key, NULL, $form);
|
| 99 |
}
|
| 100 |
}
|
| 101 |
}
|
| 102 |
break;
|
| 103 |
case 'profile_field_form':
|
| 104 |
$field = profile_privacy_get_field(NULL, $form['fid']['#value']);
|
| 105 |
|
| 106 |
$privacy_checkbox['profile_privacy'] = array(
|
| 107 |
'#type' => 'checkbox',
|
| 108 |
'#title' => t('Allow user to over ride default visibility'),
|
| 109 |
'#default_value' => $field->privacy,
|
| 110 |
);
|
| 111 |
|
| 112 |
$field_keys = array_flip(array_keys($form['fields']));
|
| 113 |
$form_first = array_slice($form['fields'], 0, $field_keys['visibility']+1);
|
| 114 |
$form_last = array_slice($form['fields'], $field_keys['visibility']+1);
|
| 115 |
$form['fields'] = array_merge($form_first, $privacy_checkbox, $form_last);
|
| 116 |
$form['#submit'][] = 'profile_privacy_profile_field_submit';
|
| 117 |
break;
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
function profile_privacy_form_alter_category($category, $account, &$form) {
|
| 122 |
$profile_keys = element_children($form[$category]);
|
| 123 |
$profile_fields = profile_privacy_get_fields($category);
|
| 124 |
|
| 125 |
$form[$category]['#theme'] = 'profile_privacy_category';
|
| 126 |
|
| 127 |
foreach ($profile_keys as $key) {
|
| 128 |
if ($profile_fields[$key]->privacy) {
|
| 129 |
// Remove descriptions about current privacy rules
|
| 130 |
$form[$category][$key]['#description'] = str_replace(' '. t('The content of this field is kept private and will not be shown publicly.'), '', $form[$category][$key]['#description']);
|
| 131 |
// Create the privacy checkbox
|
| 132 |
$privacy_checkbox = array();
|
| 133 |
$privacy_checkbox['private_'. $key] = array(
|
| 134 |
'#type' => 'checkbox',
|
| 135 |
'#title' => t('Display !field publicly', array('!field' => $form[$category][$key]['#title'])),
|
| 136 |
);
|
| 137 |
if (isset($form[$category][$key]['#weight'])) {
|
| 138 |
$privacy_checkbox['#weight'] = $form[$category][$key]['#weight'] + 0.01;
|
| 139 |
}
|
| 140 |
if (!isset($account->{'private_'. $key})) {
|
| 141 |
$privacy_checkbox['private_'. $key]['#default_value'] = $profile_fields[$key]->visibility < 2 ? 0 : 1;
|
| 142 |
}
|
| 143 |
else {
|
| 144 |
$privacy_checkbox['private_'. $key]['#default_value'] = !$account->{'private_'. $key};
|
| 145 |
}
|
| 146 |
// Insert our new checkbox after the field it references
|
| 147 |
$category_keys = array_flip(array_keys($form[$category]));
|
| 148 |
$form_first = array_slice($form[$category], 0, $category_keys[$key]+1);
|
| 149 |
$form_last = array_slice($form[$category], $category_keys[$key]+1);
|
| 150 |
$form[$category] = array_merge($form_first, $privacy_checkbox, $form_last);
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
function profile_privacy_profile_field_submit($form, &$form_state) {
|
| 156 |
profile_privacy_set_field_privacy($form_state['values']['name'], $form_state['values']['profile_privacy']);
|
| 157 |
}
|
| 158 |
|
| 159 |
function profile_privacy_theme() {
|
| 160 |
return array(
|
| 161 |
'profile_privacy_category' => array(
|
| 162 |
'arguments' => array('form' => NULL)
|
| 163 |
)
|
| 164 |
);
|
| 165 |
}
|
| 166 |
|
| 167 |
function theme_profile_privacy_category($form) {
|
| 168 |
drupal_add_css(drupal_get_path('module', 'profile_privacy') .'/profile_privacy.css');
|
| 169 |
|
| 170 |
$field_keys = element_children($form);
|
| 171 |
foreach ($field_keys as $field_key) {
|
| 172 |
$form['private_'. $field_key]['#attributes']['class'] = 'profile-privacy-checkbox';
|
| 173 |
$form[$field_key]['#description'] .= drupal_render($form['private_'. $field_key]);
|
| 174 |
}
|
| 175 |
|
| 176 |
return drupal_render($form);
|
| 177 |
}
|
| 178 |
|
| 179 |
function profile_privacy_set_field_privacy($field_name, $privacy = 0) {
|
| 180 |
profile_privacy_get_fields(NULL, TRUE);
|
| 181 |
$field = profile_privacy_get_field($field_name);
|
| 182 |
db_query('DELETE FROM {profile_privacy_fields} WHERE fid = %d', $field->fid);
|
| 183 |
db_query('INSERT INTO {profile_privacy_fields} (fid, privacy) VALUES (%d, %d)', $field->fid, $privacy);
|
| 184 |
}
|
| 185 |
|
| 186 |
function profile_privacy_get_user_field_privacy($uid, $field_name) {
|
| 187 |
$user_privacy = profile_privacy_get_user_privacy($uid);
|
| 188 |
return $user_privacy[$field_name];
|
| 189 |
}
|
| 190 |
|
| 191 |
function profile_privacy_get_user_privacy($uid, $reset = FALSE) {
|
| 192 |
static $users = array();
|
| 193 |
if (!isset($users[$uid]) || $reset) {
|
| 194 |
$result = db_query('SELECT ppv.*, pf.name FROM {profile_privacy_values} ppv INNER JOIN {profile_fields} pf ON ppv.fid = pf.fid WHERE uid = %d', $uid);
|
| 195 |
while ($row = db_fetch_object($result)) {
|
| 196 |
$users[$row->uid][$row->name] = $row->private;
|
| 197 |
}
|
| 198 |
}
|
| 199 |
return isset($users[$uid]) ? $users[$uid] : array();
|
| 200 |
}
|
| 201 |
|
| 202 |
function profile_privacy_delete_user_privacy($uid) {
|
| 203 |
return db_query('DELETE FROM {profile_privacy_values} WHERE uid = %d', $uid);
|
| 204 |
}
|
| 205 |
|
| 206 |
function profile_privacy_set_user_field_privacy($uid, $field_name, $private = 0) {
|
| 207 |
$field = profile_privacy_get_field($field_name);
|
| 208 |
db_query('DELETE FROM {profile_privacy_values} WHERE fid = %d AND uid = %d', $field->fid, $uid);
|
| 209 |
// Only allow the the profile field to be hidden if privacy is enabled.
|
| 210 |
if ($field->privacy) {
|
| 211 |
db_query('INSERT INTO {profile_privacy_values} (fid, uid, private) VALUES (%d, %d, %d)', $field->fid, $uid, $private);
|
| 212 |
}
|
| 213 |
}
|
| 214 |
|
| 215 |
function profile_privacy_get_field($field_name = NULL, $fid = NULL) {
|
| 216 |
$fields = profile_privacy_get_fields();
|
| 217 |
if ($field_name) {
|
| 218 |
return $fields[$field_name];
|
| 219 |
}
|
| 220 |
|
| 221 |
foreach ($fields as $field) {
|
| 222 |
if ($field->fid == $fid) {
|
| 223 |
return $field;
|
| 224 |
}
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
function profile_privacy_get_fields($category = NULL, $reset = FALSE) {
|
| 229 |
static $fields, $category_fields;
|
| 230 |
|
| 231 |
if (!isset($fields) || $reset) {
|
| 232 |
$fields = array();
|
| 233 |
$result = db_query('SELECT ppf.*, pf.* FROM {profile_fields} pf LEFT JOIN {profile_privacy_fields} ppf ON pf.fid = ppf.fid');
|
| 234 |
while ($field = db_fetch_object($result)) {
|
| 235 |
$fields[$field->name] = $field;
|
| 236 |
$category_fields[$field->category][$field->name] = $field;
|
| 237 |
}
|
| 238 |
}
|
| 239 |
|
| 240 |
if (isset($category)) {
|
| 241 |
return $category_fields[$category];
|
| 242 |
}
|
| 243 |
return $fields;
|
| 244 |
}
|