diff --git a/sites/all/modules/contrib/privatemsg/pm_email_notify/pm_email_notify.module b/sites/all/modules/contrib/privatemsg/pm_email_notify/pm_email_notify.module index 0d872c5..eaf2ffb 100644 --- a/sites/all/modules/contrib/privatemsg/pm_email_notify/pm_email_notify.module +++ b/sites/all/modules/contrib/privatemsg/pm_email_notify/pm_email_notify.module @@ -34,25 +34,63 @@ function pm_email_notify_privatemsg_message_insert($message) { 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 - $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)); - drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params, !empty($from) ? $from : NULL); + pm_email_notify_send_mail($message, $recipient); } + //check if the recipients are of role type + elseif (isset($recipient->rid)) { + $users = get_users_by_roleid($recipient->rid); + foreach ($users as $recipient) { + //check if the user has enabled email notifications + if (_pm_email_notify_is_enabled($recipient->uid)) { + pm_email_notify_send_mail($message, $recipient); + } + } + } + } +} + +/** + * @desc function to send email for the message ($message) to the recipient ($recipient) + * @param $message + * @param $recipient + */ +function pm_email_notify_send_mail($message, $recipient) { + // 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)); + drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params, !empty($from) ? $from : NULL); +} + +/** + * @desc This function returns loaded users belonging to the given role + * @param $rid ( :simple role id or an array of role ids) + * @return $users (loaded users) + */ +function get_users_by_roleid($rid) { + $uids = array(); + // Use $query for readability + $query = 'SELECT DISTINCT(ur.uid) + FROM {users_roles} AS ur + WHERE ur.rid IN (:rids)'; + $result = db_query($query, array(':rids' => $rid)); + foreach ($result as $row) { + $uids[] = $row->uid; } + $users = user_load_multiple($uids); + return $users; } /** @@ -62,8 +100,8 @@ function pm_email_notify_mail($key, &$message, $params) { switch ($key) { case 'notice': $data = array( - 'privatemsg_message' => $params['message'], - 'privatemsg_recipient' => $params['recipient'], + 'privatemsg_message' => $params['message'], + 'privatemsg_recipient' => $params['recipient'], ); $options = array( 'language' => user_preferred_language($params['recipient']), @@ -110,9 +148,9 @@ 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'])) - ->key(array('user_id' => $account->uid)) - ->execute(); + ->fields(array('email_notify_is_enabled' => $edit['pm_send_notifications'])) + ->key(array('user_id' => $account->uid)) + ->execute(); } } @@ -143,7 +181,7 @@ function pm_email_notify_form_privatemsg_admin_settings_alter(&$form, &$form_sta $form['pm_email_notify']['pm_email_notify_from'] = array( '#type' => 'textfield', '#title' => t('From e-mail address for notifications'), - '#default_value' => variable_get('pm_email_notify_from',''), + '#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.'), ); @@ -185,4 +223,4 @@ function pm_email_notify_form_privatemsg_admin_settings_alter(&$form, &$form_sta } return system_settings_form($form); -} +} \ No newline at end of file