| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows the core contact module to send emails to site users and site roles
|
| 7 |
*
|
| 8 |
* The idea of this module is that you may want site visitors or basic users
|
| 9 |
* to contact a site admin (and you don't want to re-enter that admin's address),
|
| 10 |
* or all users of particular administrative role (such as the accounts managers).
|
| 11 |
*
|
| 12 |
* See also the Mass Contact module (http://drupal.org/project/mass_contact), which does the
|
| 13 |
* same sort of thing but from the opposite side: Mass Contact is geared to mails 'out' from admins;
|
| 14 |
* this module is geared towards mails 'in' from the public.
|
| 15 |
* Combine both to allow both basic users (with this module) and admins (with mass contact) to
|
| 16 |
* contact users by role in different scenarios.
|
| 17 |
*
|
| 18 |
* The module works by allowing fake email addresses in the contact form settings,
|
| 19 |
* of the following format:
|
| 20 |
* user-n@here for user id n
|
| 21 |
* role-n@here for role id n (find a role's id on the role edit page's url)
|
| 22 |
*/
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_help().
|
| 26 |
*/
|
| 27 |
function contact_role_help($section='') {
|
| 28 |
$output = '';
|
| 29 |
|
| 30 |
if ($section == 'admin/help#contact_role') {
|
| 31 |
$output .= '<p>'. t('The Contact Role module allows the contact form (created by the core contact module) to send mail to site users individually or by role, without having to re-enter their email addresses.') .'</p>';
|
| 32 |
$output .= '<p>'. t('Use fake email addresses of the following form:') .'</p>';
|
| 33 |
$output .= '<ul>'.
|
| 34 |
t('<li>user-n@here for a single user, where n is the user id</li>') .
|
| 35 |
t('<li>role-n@here for all users that have a particular role, where n is the role id (find a role\'s id on the role edit page\'s url)</li>')
|
| 36 |
.'</ul>';
|
| 37 |
return $output;
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of hook_mail_alter().
|
| 43 |
* intercept mail send by the contact module's site-wide page
|
| 44 |
* replace fake email addresses with individual user emails
|
| 45 |
* or emails for all users from a role
|
| 46 |
*/
|
| 47 |
function contact_role_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers) {
|
| 48 |
if ($mailkey == 'contact-page-mail') {
|
| 49 |
$recipients = explode(',', $to);
|
| 50 |
|
| 51 |
$recipients = preg_replace_callback('/^role-(.+)@here$/', _contact_role_replace_role_emails, $recipients);
|
| 52 |
$recipients = preg_replace_callback('/^user-(\d+)@here$/', _contact_role_replace_user_emails, $recipients);
|
| 53 |
|
| 54 |
$to = implode(',', $recipients);
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* callback for replacing user emails
|
| 60 |
* return a user account email from the uid the preg found
|
| 61 |
*/
|
| 62 |
function _contact_role_replace_user_emails($matches) {
|
| 63 |
// $matches[1] is the match for the first subpattern
|
| 64 |
$account = user_load(array('uid' => $matches[1]));
|
| 65 |
return $account->mail;
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* callback for replacing role emails
|
| 70 |
* return all user account emails for the rid the preg found
|
| 71 |
*/
|
| 72 |
function _contact_role_replace_role_emails($matches) {
|
| 73 |
$result = db_query("SELECT u.mail FROM {users} u INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = %d AND u.status = 1", $matches[1]);
|
| 74 |
while ($u = db_fetch_array($result)) {
|
| 75 |
$items[] = $u['mail'];
|
| 76 |
}
|
| 77 |
$recipients = implode(',', $items);
|
| 78 |
return $recipients;
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Implementation of hook_form_alter().
|
| 83 |
* Add some extra explanation text on the 'Edit contact category' form.
|
| 84 |
*/
|
| 85 |
function contact_role_form_alter($form_id, &$form) {
|
| 86 |
if ($form_id == 'contact_admin_edit') {
|
| 87 |
$form['recipients']['#description'] .= '<br />'. t("To email site users, use an email address of the form 'user-n@here' where n is the user id. To email all site users that have a particular role, use an email address of the form 'role-n@here' where n is the role id. ") . l('More help', 'admin/help/contact_role') .'.';
|
| 88 |
}
|
| 89 |
}
|