-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser_warn.module
executable file
·170 lines (146 loc) · 4.96 KB
/
user_warn.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
// $Id$
/**
* @file
* User Warn module file
*
* This module allows site administrators to send a stock warning email
* to a specified user or users through the admin interface. Administrators
* can modify the default email including tokenization.
*/
define('USER_WARN_MAIL_SUBJECT', 'Administrative Warning');
define('USER_WARN_MAIL_TEXT',
'Hello [user:name],
We have been notified that you have posted comments on [site:name] that are
in violation of our terms of service. If this behavior continues your account will be suspended.
Sincerely,
[site:name]
[site:mail]');
/**
* Implement hook_help().
*/
function user_warn_help($path, $arg) {
if ($path == 'admin/help#user_warn') {
return t('User Warn allows site adminitrators to send a standard email to site users to notify them of improper behavior.');
}
}
/**
* Implement hook_menu().
*/
function user_warn_menu() {
$items = array();
$items['admin/config/people/user_warn'] = array(
'title' => 'User Warn',
'description' => 'Configuration for the User Warn module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_warn_form'),
'access arguments' => array('administer users'),
'type' => MENU_NORMAL_ITEM,
);
$items['user/%/warn'] = array(
'title' => 'Warn',
'description' => 'Send email to a user about improper site behavior.',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_warn_confirm_form', 1),
'access arguments' => array('administer users'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Form builder; Create and display the User Warn configuration settings form.
*/
function user_warn_form($form, &$form_state) {
$available_tokens = 'Available variables are: ';
$tokens = token_info();
foreach ($tokens['tokens']['site'] as $key => $value) {
$site_tokens[] = '[site:' . $key .']';
}
foreach ($tokens['tokens']['user'] as $key => $value) {
$site_tokens[] = '[user:' . $key .']';
}
$available_tokens .= implode(', ', $site_tokens);
// Text field for the email subject.
$form['user_warn_email_subject'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('user_warn_email_subject', USER_WARN_MAIL_SUBJECT),
'#title' => t('Warning email subject'),
'#size' => 40,
'#maxlength' => 120,
'#required' => TRUE,
'#description' => t('The subject of the email which will be sent to users.'),
);
// Textarea for the body of the email.
$form['user_warn_email_text'] = array(
'#type' => 'textarea',
'#rows' => 10,
'#columns' => 40,
'#default_value' => variable_get('user_warn_email_text', USER_WARN_MAIL_TEXT),
'#title' => t('Warning email text'),
'#required' => TRUE,
'#description' => t('The text of the email which will be sent to users. ') . $available_tokens,
);
// Checkbox to indicate whether admin should be BCC'd on emails.
$form['user_warn_bcc'] = array(
'#type' => 'checkbox',
'#default_value' => variable_get('user_warn_bcc', FALSE),
'#title' => t('BCC admin on all emails'),
'#description' => t('Indicates whether the admin user (as set in site configuration) should be BCC\'d on all warning emails.'),
);
return system_settings_form($form);
}
/**
* Form submit handler. Save configuration settings for User Warn module.
*/
function user_warn_form_submit($form, &$form_state) {
variable_set('user_warn_email_subject', $form_state['values']['user_warn_email_subject']);
variable_set('user_warn_email_text', $form_state['values']['user_warn_email_text']);
variable_set('user_warn_bcc', $form_state['values']['user_warn_bcc']);
drupal_set_message('The settings have been saved');
}
function user_warn_confirm_form($form, &$form_state, $uid) {
$form['account'] = array(
'#type' => 'value',
'#value' => user_load($uid),
);
return confirm_form(
$form,
t('Are you sure you want to send a warning email to this user?'),
'user/' . $uid,
t('This action can not be undone.'),
t('Send email'),
t('Cancel')
);
}
function user_warn_confirm_form_submit($form, &$form_state) {
$form_values = $form_state['values'];
$account = $form_values['account'];
drupal_mail(
'user_warn',
'warn',
$account->mail,
user_preferred_language($account),
$form_values,
variable_get('site_mail', NULL),
TRUE
);
}
/**
* Implement hook_mail().
*/
function user_warn_mail($key, &$message, $params) {
switch ($key) {
case 'warn':
$account = $params['account'];
$subject = variable_get('user_warn_email_subject', USER_WARN_MAIL_SUBJECT);
$body = variable_get('user_warn_email_text', USER_WARN_MAIL_TEXT);
if (variable_get('user_warn_bcc', FALSE)) {
$admin_mail = variable_get('site_mail', NULL);
$message['headers']['bcc'] = $admin_mail;
}
$message['to'] = $account->mail;
$message['subject'] = $subject;
$message['body'][] = token_replace($body, array('user' => $account));
break;
}
}