Index: cck_field_privacy.info =================================================================== --- cck_field_privacy.info (revision 30) +++ cck_field_privacy.info (working copy) @@ -2,7 +2,6 @@ name = "Content Field Privacy" description = "Allow users to control per-field privacy settings." dependencies[] = content -dependencies[] = jquery_impromptu package = CCK core = 6.x ; Information added by drupal.org packaging script on 2009-06-12 Index: cck_field_privacy.module =================================================================== --- cck_field_privacy.module (revision 30) +++ cck_field_privacy.module (working copy) @@ -77,23 +77,117 @@ function cck_field_privacy_menu() { $items = array(); - $items['admin/content/cck_field_privacy'] = array( - 'title' => 'Content field privacy', - 'description' => 'Configure field privacy controls.', - 'page callback' => 'cck_field_privacy_admin_settings', - 'access arguments' => array('administer cck field privacy'), - 'type' => MENU_NORMAL_ITEM, - ); - $items['cck_field_privacy/ajax'] = array( - 'page callback' => 'cck_field_privacy_ajax', - 'access callback' => TRUE, - 'type' => MENU_CALLBACK, - ); + $items['admin/content/cck_field_privacy'] = array( + 'title' => 'Content field privacy', + 'description' => 'Configure field privacy controls.', + 'page callback' => 'cck_field_privacy_admin_settings', + 'access arguments' => array('administer cck field privacy'), + 'type' => MENU_NORMAL_ITEM, + ); + $items['cck_field_privacy/%/%/%'] = array( + 'page callback' => 'drupal_get_form', + 'page arguments' => array('cck_field_privacy_field_form', 1, 2, 3), // args: user, field, type + 'access callback' => 'cck_field_privacy_field_form_access', + 'access arguments' => array(1), + 'type' => MENU_CALLBACK, + ); return $items; } /** + * Implementation of hook_popups(). + */ +function cck_field_privacy_popups() { + return array( + 'node/*/edit' => array( + 'a[class=privacylink]' => array( + 'width' => 450, + 'noUpdate' => TRUE, + ), + ), + 'user/*/profile' => array( + 'a[class=privacylink]' => array( + 'width' => 450, + 'noUpdate' => TRUE, + ), + ), + ); +} + +/** + * Access callback for cck_field_privacy_field_form + * @param $uid User ID for the author of the content being modified. + */ +function cck_field_privacy_field_form_access($uid) { + global $user; + return ((user_access('modify own cck field privacy') && $uid == $user->uid) || user_access('modify cck field privacy')); +} + +/** + * Field settings form. + * @return FAPI formatted form for a field's privacy setting. + */ +function cck_field_privacy_field_form(&$form_state, $uid, $field, $type) { + + // Get original setting. Decided to do a small db hit here to avoid having to rewrite links every update. + $field_pref_sql = db_query("SELECT permission FROM {cckfp} WHERE uid = %d AND type_name = '%s' AND field_name = '%s'", $uid, $type, $field); + $field_pref = (string) db_result($field_pref_sql); + if ($field_pref == "") { + $field_pref = variable_get('cckfp_'. $type .'_default', 'e'); // Set to the default state of the field + } + + $form = array(); + $form['#title'] = t('Field Privacy'); + + $form['setting'] = array( + '#type' => 'radios', + '#title' => t('Access'), + '#options' => array( + 'e' => t('Everyone'), + 'b' => t('Buddies'), + 'n' => t('Nobody'), + ), + '#default_value' => $field_pref, + '#description' => t('Set the privacy level for this field.'), + ); + + $form['save'] = array( + '#type' => 'submit', + '#value' => t('Save Setting'), + ); + + $form['uid'] = array('#type' => 'value', '#value' => $uid); + $form['field'] = array('#type' => 'value', '#value' => $field); + $form['type'] = array('#type' => 'value', '#value' => $type); + + return $form; +} + +/** + * Implementation of hook_form_submit(). + */ +function cck_field_privacy_field_form_submit($form, &$form_state) { + global $user; + if (user_access('modify cck field privacy') || (user_access('modify own cck field privacy') && $user->uid == $form_state['values']['uid'])) { + $userinfo = array(); + $userinfo['uid'] = $form_state['values']['uid']; + $field = $form_state['values']['field']; + $type = $form_state['values']['type']; + $setting = $form_state['values']['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); + if (!db_error()) { + } + else { + print db_error(); + } + } + } +} + +/** * Implementation of hook_nodeapi(). * Removes fields if a user does not have perms to view it. */ @@ -234,7 +328,7 @@ case "date_popup" : case "date_text" : if (isset($form['#field_info'][$field]['widget']['label'])) { - $form['#field_info'][$field]['widget']['label'] .= " ". t(Privacy) .""; + $form['#field_info'][$field]['widget']['label'] .= " ". t(Privacy) .""; } else { $needpadlock[$field] = TRUE; @@ -253,20 +347,8 @@ } if (!empty($needpadlock)) { // look for fields left recursively throught the form - _cck_field_privacy_add_padlock($form, $needpadlock); + _cck_field_privacy_add_padlock($form, $needpadlock, $u, $node_type); } - - // 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'); } } } @@ -278,13 +360,12 @@ /** * Recursively add a padlock where needed */ -function _cck_field_privacy_add_padlock(&$elements, &$needpadlock) { - +function _cck_field_privacy_add_padlock(&$elements, &$needpadlock, $user, $type) { // 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 - $lockimg = "". t(Privacy) .""; + $lockimg = ''. t(Privacy) .''; if (isset($elements[$field][0]['#type'])) { // content sub field first switch ($elements[$field][0]['#type']) { case "link" : // link widget @@ -344,44 +425,13 @@ if (!empty($needpadlock)) { // recursing through children elements if privacy fields left - _cck_field_privacy_add_padlock($elements[$field], $needpadlock); + _cck_field_privacy_add_padlock($elements[$field], $needpadlock, $u, $type); } } } } /** - * MENU_CALLBACK for cck_field_privacy/ajax. - * - * @return String - * Server response. - */ -function cck_field_privacy_ajax() { - 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']; - $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); - if (!db_error()) { - } - else { - print db_error(); - } - } - } - } - - exit; -} - -/** * Page for the admin settings form. * Use this instead of standard settings for because * we serialize all the data into one variable.