diff --git "a/C:\\Users\\David\\AppData\\Local\\Temp\\pm_CBEC.tmp\\pm_email_notify-2dbacba-left.module" "b/S:\\test\\drupal-7\\sites\\all\\modules\\privatemsg\\pm_email_notify\\pm_email_notify.module" index 0d872c5..d383dc4 100644 --- "a/C:\\Users\\David\\AppData\\Local\\Temp\\pm_CBEC.tmp\\pm_email_notify-2dbacba-left.module" +++ "b/S:\\test\\drupal-7\\sites\\all\\modules\\privatemsg\\pm_email_notify\\pm_email_notify.module" @@ -6,50 +6,76 @@ */ /** - * Retrieve notification setting of a user. + * Retrieve module specific user settings. * - * This function retrieves user's pm notification preference from database, - * if user preference doesn't exist - it uses default value instead + * Retrieves and statically stores the DB values per user. * * @param $uid - * User uid + * User ID. + * @param $setting + * The setting's (DB column's) field name. + * + * @return array + * The setting specified in $setting or, if $setting is omitted, all + * settings for this user (using site defaults, if the user is not yet + * in the DB). */ -function _pm_email_notify_is_enabled($uid) { - $notifications = &drupal_static(__FUNCTION__, array()); +function _pm_email_notify_get_db_setting($uid, $setting = NULL) { + $settings = &drupal_static(__FUNCTION__, array()); // Cache the result set in case this method is executed in batched operation which will perform many unnecessary repeated selects for the same user - if (!isset($notifications[$uid]) ) { - $mail_notification = db_query('SELECT email_notify_is_enabled FROM {pm_email_notify} WHERE user_id = :uid', array(':uid' => $uid))->fetchField(); - if ($mail_notification === FALSE) { //db_result returns FALSE if result was not found. - $mail_notification = variable_get('pm_email_notify_default', TRUE); + if (!isset($settings[$uid]) ) { + $db_data = db_select('pm_email_notify', 'p') + ->fields('p', array('email_notify_is_enabled', 'show_sender_mail')) + ->condition('user_id', $uid, '=') + ->execute() + ->fetchAssoc(); + // fetchAssoc() returns FALSE if result was not found. Use site defaults then. + if ($db_data === FALSE) { + $db_data = array( + 'email_notify_is_enabled' => variable_get('pm_email_notify_default', TRUE), + 'show_sender_mail' => variable_get('pm_email_notify_show_sender_mail', FALSE), + ); } - $notifications[$uid] = $mail_notification; + $settings[$uid] = $db_data; } - return $notifications[$uid]; + return isset($setting) ? $settings[$uid][$setting] : $settings[$uid]; } /** * Implements hook_privatemsg_message_insert(). */ function pm_email_notify_privatemsg_message_insert($message) { + // Prepare some data about the message author. + $show_sender_mail = &drupal_static(__FUNCTION__, array()); + if (!isset($show_sender_mail[$message->author->uid])) { + $show_sender_mail[$message->author->uid] = _pm_email_notify_get_db_setting($message->author->uid, 'show_sender_mail'); + } foreach ($message->recipients as $recipient) { - // check if recipient enabled email notifications - if (isset($recipient->uid) && _pm_email_notify_is_enabled($recipient->uid)) { - // send them a new pm notification email if they did + // Check if recipient enabled email notifications. + if (isset($recipient->uid) && _pm_email_notify_get_db_setting($recipient->uid, 'email_notify_is_enabled')) { + // Send them a new pm notification email if they did. $params['recipient'] = $recipient; $params['message'] = $message; - // token replace for email from address - $data = array( - 'privatemsg_message' => $params['message'], - 'privatemsg_recipient' => $params['recipient'], - ); - $options = array( - 'language' => user_preferred_language($params['recipient']), - // Don't sanitize output since this is used in an email, not a browser. - 'sanitize' => FALSE, - // Custom token to avoid custom token handling. - 'privatemsg-display-invalid' => FALSE, - ); - $from = trim(token_replace(variable_get('pm_email_notify_from', ''), $data, $options)); + + // Use either the author's email as "from" or the site default. + if ($show_sender_mail[$message->author->uid]) { + $from = $message->author->mail; + } + else { + // token replace for email from address + $data = array( + 'privatemsg_message' => $params['message'], + 'privatemsg_recipient' => $params['recipient'], + ); + $options = array( + 'language' => user_preferred_language($params['recipient']), + // Don't sanitize output since this is used in an email, not a browser. + 'sanitize' => FALSE, + // Custom token to avoid custom token handling. + 'privatemsg-display-invalid' => FALSE, + ); + $from = trim(token_replace(variable_get('pm_email_notify_from', ''), $data, $options)); + } drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params, !empty($from) ? $from : NULL); } } @@ -91,13 +117,27 @@ function _pm_email_notify_default_body() { */ function pm_email_notify_form_alter(&$form, &$form_state, $form_id) { if (($form_id == 'user_register_form' || $form_id == 'user_profile_form') && $form['#user_category'] == 'account' && privatemsg_user_access('read privatemsg')) { + $user_settings = _pm_email_notify_get_db_setting($form['#user']->uid); $form['privatemsg']['pm_send_notifications'] = array( '#type' => 'checkbox', '#title' => t('Receive email notification for incoming private messages'), - '#default_value' => _pm_email_notify_is_enabled($form['#user']->uid), + '#default_value' => $user_settings['email_notify_is_enabled'], + '#states' => array( + 'visible' => array( + ':input[name="pm_enable"]' => array('checked' => TRUE), + ), + ), + ); + $form['privatemsg']['pm_show_sender_mail'] = array( + '#type' => 'checkbox', + '#title' => t('Show my email address'), + '#description' => t('Use my private email address as a sender for notification mails to users I have sent private messages to. If unchecked, the notification will be sent by a generic account.'), + '#default_value' => $user_settings['show_sender_mail'], + '#weight' => 1, '#states' => array( 'visible' => array( ':input[name="pm_enable"]' => array('checked' => TRUE), + ':input[name="pm_send_notifications"]' => array('checked' => TRUE), ), ), ); @@ -110,7 +150,10 @@ function pm_email_notify_form_alter(&$form, &$form_state, $form_id) { function pm_email_notify_user_update(&$edit, $account, $category) { if (isset($edit['pm_send_notifications']) && privatemsg_user_access('read privatemsg', $account)) { db_merge('pm_email_notify') - ->fields(array('email_notify_is_enabled' => $edit['pm_send_notifications'])) + ->fields(array( + 'email_notify_is_enabled' => $edit['pm_send_notifications'], + 'show_sender_mail' => $edit['pm_show_sender_mail'], + )) ->key(array('user_id' => $account->uid)) ->execute(); } @@ -145,7 +188,15 @@ function pm_email_notify_form_privatemsg_admin_settings_alter(&$form, &$form_sta '#title' => t('From e-mail address for notifications'), '#default_value' => variable_get('pm_email_notify_from',''), '#weight' => 2, - '#description' => t('This is the e-mail address that notifications will come from. Leave blank to use the site default.'), + '#description' => t("This is the e-mail address that notifications will come from unless the sender's private email address is used. Leave blank to use the site default."), + ); + + $form['pm_email_notify']['pm_email_notify_show_sender_mail'] = array( + '#type' => 'checkbox', + '#title' => t("Use sender's email address"), + '#description' => t("Use the sender's private email address instead of a generic sender for notification mails. This is the site default setting; users may override it individually."), + '#default_value' => variable_get('pm_email_notify_show_sender_mail', 0), + '#weight' => 2, ); $form['pm_email_notify']['pm_email_notify_subject'] = array(