/[drupal]/contributions/modules/role_change_notify/role_change_notify.module
ViewVC logotype

Contents of /contributions/modules/role_change_notify/role_change_notify.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Sat Nov 15 02:36:03 2008 UTC (12 months, 1 week ago) by rfay
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, DRUPAL-6--1-1, DRUPAL-6--1-1-RC4, DRUPAL-6--1-1-RC1, DRUPAL-6--1-1-RC3, DRUPAL-6--1-1-RC2, DRUPAL-6--1-0-BETA1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.4: +45 -27 lines
File MIME type: text/x-php
Drupal 6 version of role_change_notify
1 <?php
2 // $Id: role_change_notify.module 14 2008-11-12 22:33:52Z rfay $
3
4 /**
5 * @file
6 * Send email to users when a new role is assigned
7 *
8 */
9
10 /**
11 * Print module description on module activation page
12 */
13 function role_change_notify_help($path, $arg) {
14 $output = '';
15
16 switch ($path) {
17 case 'admin/help#role_change_notify':
18 break;
19 case 'admin/user/role_change_notify':
20 $from = variable_get('site_mail', ini_get('sendmail_from'));
21 if (!valid_email_address($from)) {
22 drupal_set_message(t('None of these notifications will be sent unless you specify a valid site !email_address on the !site_information settings page.', array('!email_address' => '<strong>'. t('E-mail address') .'</strong>', '!site_information' => l(t('Site information'), 'admin/settings/site-information'))), 'error');
23 }
24 $output .= '<p>'. t('This page allows you to configure whether automatic emails should be set to users when a new role is assigned to them. You define if an email should be sent for each role type, and if so, you define what the subject and body of the resulting email will be.') .'</p>'
25 .'<p>'. t('For any of the settings below, you can use placeholders which will be substituted with the current values for the user, role, and site.') .' '. t('Available variables are:') .' %username, %role, %site, %uri, %uri_brief, %mailto, %date, %edit_uri, %login_uri.</p>';
26 break;
27 }
28 return $output;
29 }
30
31 /**
32 * @see hook_menu()
33 */
34 function role_change_notify_menu() {
35 $items = array();
36 global $user;
37 $items['admin/user/role_change_notify'] = array(
38 'title' => 'Role Change Notifications',
39 'description' => 'Configure email notifications when a role is added to a user account',
40 'page callback' => 'drupal_get_form',
41 'page arguments' => array('role_change_notify_settings_form'),
42 'access arguments' => array('administer site configuration'),
43 'type' => MENU_NORMAL_ITEM,
44 );
45 return $items;
46 }
47
48 /**
49 * Default subject line of email when a role is added
50 *
51 * @return string
52 */
53 function role_change_notify_role_added_subject() {
54 return t('Role %role added for %username at %site');
55 }
56
57 /**
58 * Default body of email when user is activated
59 *
60 * @return string
61 */
62 function role_change_notify_role_added_body() {
63 return t("%username,\n\nThe role \"%role\" has been added to your account at %site.\n\nYou may now use the associated privileges.");
64 }
65
66
67 /**
68 * Form to modify default values for emails to users during account status modification
69 *
70 * @return string
71 */
72 function role_change_notify_settings_form() {
73 $roles = user_roles(TRUE);
74
75 unset($roles[DRUPAL_AUTHENTICATED_RID]);
76 if (sizeof($roles) == 0) {
77 $form['noroles'] = array(
78 '#value' => "<p><em>". t("No roles have been set up except Authenticated User. \nPlease set up additional roles if you want to use role notification.") ."</em></p>",
79 );
80 return $form;
81 }
82 $form['instructions'] = array(
83 '#value' => "<b>". t("Select roles for which notification should be sent") ."</b>",
84 );
85 foreach ($roles as $roleid => $rolename) {
86 $form["role_change_notify_" . $roleid] = array(
87 '#type' => 'checkbox',
88 '#title' => t("$rolename"),
89 '#default_value' => variable_get('role_change_notify_'. $roleid, FALSE),
90 );
91 }
92 $form['role_change_notify_role_added_subject'] = array(
93 '#type' => 'textfield',
94 '#title' => t('Subject'),
95 '#default_value' => variable_get('role_change_notify_role_added_subject', role_change_notify_role_added_subject()),
96 '#size' => 72,
97 '#maxlength' => 200,
98 );
99 $form['role_change_notify_role_added_body'] = array(
100 '#type' => 'textarea',
101 '#title' => t('Body'),
102 '#default_value' => variable_get('role_change_notify_role_added_body', role_change_notify_role_added_body()),
103 '#cols' => 72,
104 '#rows' => 10,
105 '#description' => '',
106 );
107
108 return system_settings_form($form);
109 }
110
111 /**
112 * Sends user an email based on the role modification
113 *
114 * @param string $op
115 * @param array $edit
116 * @param object $user
117 */
118 function role_change_notify_user($op, &$edit, &$user) {
119
120 if ($op != 'update' || !array_key_exists('roles', $edit)) {
121 return;
122 }
123 $roles = user_roles(TRUE);
124
125 $account = user_load(array('uid' => (int)$user->uid));
126
127 $from = variable_get('site_mail', ini_get('sendmail_from'));
128 $headers = array(
129 'X-Mailer' => 'Drupal Role Change Notify module - http://drupal.org/project/role_change_notify',
130 );
131
132 $oldroles = array_keys($account->roles);
133 $newroles = array_keys($edit['roles']);
134 $rolesadded = array_diff($newroles, $oldroles);
135
136 foreach ($rolesadded as $roleid) {
137 if (variable_get("role_change_notify_$roleid", FALSE)) {
138 $role = $roles[$roleid];
139 if (valid_email_address($user->mail) && valid_email_address($from)) {
140 $variables = _role_change_notify_get_variables($user, $role);
141 $subject = strtr(variable_get('role_change_notify_role_added_subject', role_change_notify_role_added_subject()), $variables);
142 $body = strtr(variable_get('role_change_notify_role_added_body', role_change_notify_role_added_body()), $variables);
143 $language = user_preferred_language($account);
144 $context['from'] = $from;
145 $context['subject'] = $subject;
146 $context['body'] = $body;
147 $context['headers'] = $headers;
148 $params = array('context' => $context);
149 drupal_mail('role_change_notify', 'role_added', $user->mail, $language, $params);
150 drupal_set_message(t("User %user notified of added role %role", array('%user' => $user->name, '%role' => $role)));
151 }
152 }
153 }
154
155 }
156 /**
157 * Implementation of hook_mail().
158 *
159 * @param $key
160 * Key to the message tex to be used (passed in in this case)
161 * @param &$messsage
162 * The message to be altered
163 * @param $params
164 * Array of free-form items to be used in creating message
165 *
166 * @see hook_mail()
167 *
168 */
169 function role_change_notify_mail($key, &$message, $params) {
170 if ($key == 'role_added') {
171 $message['subject'] = $params['context']['subject'];
172 $message['body'] = $params['context']['body'];
173 $message['from'] = $params['context']['from'];
174 $message['headers'] = array_merge($message['headers'], $params['context']['headers']);
175 }
176 }
177
178 /**
179 * Return an array of substitution variables for email bodies and subjects.
180 *
181 * @param $user
182 * The user object of the account being notified.
183 * @param $rolename
184 * The new role being applied to the account
185 *
186 * @return
187 * Array of substitution variables.
188 */
189 function _role_change_notify_get_variables($user, $rolename) {
190 global $base_url;
191 $variables = array(
192 '%username' => $user->name,
193 '%site' => variable_get('site_name', 'drupal'),
194 '%uri' => $base_url,
195 '%uri_brief' => drupal_substr($base_url, drupal_strlen('http://')),
196 '%mailto' => $user->mail,
197 '%date' => format_date(time()),
198 '%login_uri' => url('user', array('absolute' => TRUE)),
199 '%edit_uri' => url('user/'. $user->uid .'/edit', array('absolute' => TRUE)),
200 '%role' => $rolename,
201 );
202 return $variables;
203 }
204

  ViewVC Help
Powered by ViewVC 1.1.2