Index: privatemsg.module =================================================================== --- privatemsg.module (revision 1825) +++ privatemsg.module (working copy) @@ -153,6 +153,18 @@ function privatemsg_menu() { 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); + if (count(module_invoke_all('privatemsg_user_settings')) > 0) { + $items['messages/settings'] = array( + 'title' => 'Settings', + 'description' => 'Configure private messaging settings.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('privatemsg_user_settings'), + 'access callback' => 'privatemsg_settings_access', + 'type' => MENU_LOCAL_TASK, + 'weight' => 30, + ); + } + return $items; } @@ -173,6 +185,9 @@ function privatemsg_user_access($permiss if (!$account->uid) { // Disallow anonymous access, regardless of permissions return FALSE; } + if (privatemsg_is_disabled($account)) { + return FALSE; + } if (!user_access($permission, $account)) { return FALSE; } @@ -193,6 +208,18 @@ function privatemsg_view_access() { } /** + * Display the settings page only if there are form fragments. + * + * Modules can use privatemsg_user_access() to decide if they want to return + * the form in the hook, only display the settings page if atleast one module + * provides a setting for the current user. + * + */ +function privatemsg_settings_access() { + return (bool) count(module_invoke_all('privatemsg_user_settings')); +} + +/** * This function is called by the menu system through the %privatemsg_thread wildcard * * TODO: Really load the full thread in here @@ -948,6 +975,9 @@ function privatemsg_user($op, &$edit, &$ } } break; + case 'delete': + db_query('DELETE from {pm_disable} where uid=%d', $account->uid); + break; } } @@ -1451,3 +1481,91 @@ function _privatemsg_assemble_query($que } return FALSE; } + +/** + * Menu callback for messags/settings. + */ +function privatemsg_user_settings() +{ + $form = module_invoke_all('privatemsg_user_settings'); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save settings'), + ); + + return $form; +} + +/** + * Implementation of hook_privatemsg_user_settings(). + */ +function privatemsg_privatemsg_user_settings() { + global $user; + + $form['disable'] = array( + '#type' => 'radios', + '#title' => t('Private Message Receiving'), + '#default_value' => (int) !privatemsg_is_disabled($user), + '#options' => array(t('Disabled'), t('Enabled')), + ); + + $form['#submit'][] = 'privatemsg_disable_submit'; + + return $form; +} + +/** + * Checks the status of private messaging for provided user. + * + * @param user object to check + * @return TRUE if user has disabled private messaging, FALSE otherwise + */ +function privatemsg_is_disabled(&$account) { + + if (!$account || !$account->uid) { + return FALSE; + } + + if (!isset($account->privatemsg_disabled)) { + $account->privatemsg_disabled = (db_result(db_query('SELECT COUNT(0) FROM {pm_disable} WHERE uid = %d ', $account->uid)) <> 0); + } + + return ($account->privatemsg_disabled); +} + +function privatemsg_disable_submit($form, &$form_state) { + global $user; + + $current = $user->privatemsg_disabled; + $disabled = (!$form_state['values']['disable']); + + $user->privatemsg_disabled = $disabled; + + // only perform the save and set message if the value has changed + if ($current != $disabled) { + if ($disabled) { + db_query('INSERT into {pm_disable} values (%d)', $user->uid); + $status = t('disabled'); + } + else { + db_query('DELETE from {pm_disable} where uid = %d', $user->uid); + $status = t('enabled'); + } + + drupal_set_message(t('You have @status private messages.', array('@status' =>$status) )); + } + +} + +/** + * Implementation of hook_privatemsg_block_message. + */ + function privatemsg_privatemsg_block_message($author, $recipient) { + if (privatemsg_is_disabled($author)) { + return t('You have disabled private message sending and receiving.'); + } + if (privatemsg_is_disabled($recipient)) { + return t('!recipient has disabled private message receiving.', array('!recipient' => $recipient->name)); + } + } Index: privatemsg.install =================================================================== --- privatemsg.install (revision 1825) +++ privatemsg.install (working copy) @@ -96,6 +96,18 @@ function privatemsg_schema() { ), ); + $schema['pm_disable'] = array( + 'description' => '{pm_disable} holds the list of users that have disabled private messaging', + 'fields' => array( + 'uid' => array( + 'description' => 'ID of the user', + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + ), + ), + 'primary key' => array('uid'), + ); return $schema; } @@ -108,6 +120,29 @@ function privatemsg_uninstall() { drupal_uninstall_schema('privatemsg'); } +function privatemsg_update_6002() { + $ret = array(); + + $schema['pm_disable'] = array( + 'description' => '{pm_disable} holds the list of users that have disabled private messaging', + 'fields' => array( + 'uid' => array( + 'description' => 'ID of the user', + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + ), + ), + 'primary key' => array('uid'), + ); + + if (!(db_table_exists('pm_disable'))) { + db_create_table($ret, 'pm_disable', $schema['pm_disable']); + } + + return $ret; +} + function privatemsg_update_6001() { $ret = array();