forked from mccrodp/message_private
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_private.module
executable file
·360 lines (323 loc) · 11.5 KB
/
message_private.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* Message Private with access permissions based on message fields.
*/
/**
* The default index for settings such as role.
*/
define('MESSAGE_PRIVATE_DEFAULT_INDEX', 0);
/**
* The maximum message limit.
*/
define('MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX', 1000);
/**
* The minimum message limit.
*/
define('MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN', 1);
/**
* The maximum message interval in minutes.
*/
define('MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX', 1440);
/**
* The minimum message interval in minutes.
*/
define('MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN', 1);
/**
* Implements hook_help().
*/
function message_private_help($path, $arg) {
switch ($path) {
case 'admin/help#message_private':
$output = file_get_contents(drupal_get_path('module', 'message_private') .'/README.md');
return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<h3>Message Private README</h3><pre>'. check_plain($output) .'</pre>';
}
}
/**
* Implements hook_views_api().
*/
function message_private_views_api() {
list($module, $api) = func_get_args();
if ($module == 'views' && $api == 'views_default') {
return array('version' => '3.0');
}
return array();
}
/**
* Implements hook_permission().
*/
function message_private_permission() {
// Build the permissions.
$permissions = array();
$permissions['bypass private message access control'] = array(
'title' => t('Bypass message access control'),
'description' => t('Grant to the user the permission to apply CRUD option on any private messages.'),
'restrict access' => TRUE,
);
$permissions['administer message private'] = array(
'title' => t('Administer message private'),
'description' => t('Access the message private administration pages.'),
// 'restrict access' => TRUE, // May need to be added in future.
);
return $permissions;
}
/**
* Implements hook_menu().
*/
function message_private_menu() {
$items = array();
$items['admin/config/system/message-private'] = array(
'title' => 'Message private settings',
'description' => 'Configuration settings for message private module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('message_private_admin_settings'),
'file' => 'message_private.admin.inc',
'access arguments' => array('administer message private'),
);
// Add default local task so the Messages view display shows Inbox tab.
$items['user/%/messages/inbox'] = array(
'title' => 'Inbox',
'description' => 'Message Private Inbox',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Access callback for Messages tab.
*
* Checks for the private_message bundle and user permissions.
*
* @param $message
* @return bool
*/
function message_private_access_callback($message) {
if ($message->type == 'private_message') {
return user_access('bypass private message access control') || user_access('view a private_message message instance');
}
return FALSE;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function message_private_menu_local_tasks_alter(&$data, $router_item, $root_path) {
global $user;
switch ($root_path) {
case 'user/%/messages':
// Add a 'Create a new message' action link above message_private view.
$item = menu_get_item('admin/content/message/create/private-message');
$item['title'] = t('Create a new message');
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
break;
case 'message/%':
// Add a Messages tab to the private_message Message entities.
$message = menu_get_object('message', 1, $router_item['href']);
if(isset($message->type) && $message->type == 'private_message') {
$item = menu_get_item('user/' . $user->uid . '/messages');
$item['title'] = t('Messages');
$data['tabs'][0]['output'][] = array(
'#theme' => 'menu_local_task',
'#link' => $item,
);
}
break;
}
}
/**
* Implements hook_menu_alter().
*
* Override some of the menu paths set in message ui module.
*/
function message_private_menu_alter(&$items) {
$items['message/%message']['access callback'] = 'message_private_access_control';
$items['message/%message/view']['access callback'] = 'message_private_access_control';
$items['message/%message/edit']['access callback'] = 'message_private_access_control';
$items['message/%message/delete']['access callback'] = 'message_private_access_control';
return $items;
}
/**
* Message module access callback.
*
* @param string $operation
* The operation - create, view, update, delete.
* @param object|string $message
* The message object or message type.
* @param object|null $user_obj
* A user object. Optional.
*
* @return bool
* TRUE if user is allowed to perform the message operation, FALSE otherwise.
*/
function message_private_access_control($operation, $message, $user_obj = NULL) {
if (empty($user_obj)) {
global $user;
$account = user_load($user->uid);
}
else {
$user = $user_obj;
$account = user_load($user->uid);
}
// Get the message type from the function argument or from the message object.
$type = is_object($message) ? $message->type : $message;
// If this is not a private message then use the message callback provided by
// message_ui module.
if ($type != 'private_message') {
return message_ui_access_control($operation, $message);
}
else {
if (user_access('bypass private message access control', $account)) {
return TRUE;
}
// Verify that the user can apply the op.
if (user_access($operation . ' any message instance', $account)
|| user_access($operation . ' a ' . $type . ' message instance', $account)
) {
if ($type == 'private_message' && $operation != 'create') {
// Check if the user is message author.
if ($message->uid == $account->uid) {
return TRUE;
}
$users = field_get_items('message', $message, 'field_message_user_ref');
if ($users && is_array($users)) {
foreach ($users as $user_ref) {
if ($user_ref['target_id'] == $account->uid) {
return TRUE;
}
}
}
}
else {
return TRUE;
}
}
}
return FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Hide the message_text field from the message edit form. It is only useful
* post creation. i.e. - contains user data. Also add custom validate function.
*/
function message_private_form_message_ui_instance_message_manage_alter(&$form, &$form_state, $form_id) {
if ($form['#bundle'] == 'private_message') {
if (isset($form['text']['#type'])) {
$form['text']['#type'] = 'hidden';
}
$form['#validate'][] = 'message_private_form_message_ui_instance_message_manage_validate';
}
}
/**
* Validation for Private Message form.
*
* @param mixed $form
* The message form provided by message_ui.
* @param mixed $form_state
* The form state including values submitted.
*/
function message_private_form_message_ui_instance_message_manage_validate($form, &$form_state) {
// If there is an imposed message limit, set in the admin settings interface.
if (variable_get('message_private_message_limit', FALSE)) {
global $user;
$rid = _message_private_max_message_limit_role($user->roles);
$role = user_role_load($rid);
$limit_name = 'message_private_' . $role->name . '_limit';
$interval_name = 'message_private_' . $role->name . '_interval';
$interval = variable_get($interval_name);
$limit = variable_get($limit_name);
// Get total amount of messages since last interval.
$current_timestamp = time();
$interval_timestamp = strtotime('-' . $interval . ' minutes', $current_timestamp);
$query = new EntityFieldQuery();
$total = $query->entityCondition('entity_type', 'message')
->entityCondition('bundle', 'private_message')
->propertyCondition('created', $interval_timestamp, '>')
->propertyCondition('uid', $user->uid)
->count()
->execute();
// if total < limit, send message, if not display error.
if($total >= $limit) {
form_error($form, t('Message create limit reached. Please try again later.'));
}
}
}
/**
* Implements hook_message_insert().
*
* Send an email if a private message has been created.
*
* Currently addslashes() or equivalent is not used on the email,
* this may be an issue with the message module itself as I don't
* think escaping strings for email should be done in this module.
*/
function message_private_message_insert($message) {
// Prepare message notifications for private messages if notifications are on.
if ($message->type == 'private_message'
&& variable_get('message_private_email_notifications', TRUE)) {
// Use message load as the $message object has issue with mail function.
// It causes duplicate entry, possibly as mid is missing and the mail
// function message_notify_send_message tries to re-save as a new message.
$message = message_load($message->mid);
$wrapper = entity_metadata_wrapper('message', $message);
$mail = array();
$users = $wrapper->field_message_user_ref->value();
if (is_array($users)) {
foreach ($users as $user) {
$notify = field_get_items('user', $user, 'field_private_message_notify');
if (!empty($notify) && is_array($notify)) {
// Get the 1st value of the array as there is only 1 possible item.
$notify = array_shift($notify);
}
// If the user has set field for notifications, add their email.
if (isset($notify['value']) && $notify['value']) {
$mail[] = $user->mail;
}
}
}
if (!empty($mail)) {
message_notify_send_message($message, array('mail' => implode(',', $mail)));
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* If email notifications are disabled, hide the per user setting on user
* profiles, unless the user is in role with bypass access control permission.
*/
function message_private_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
if (user_access('bypass private message access control')
|| !variable_get('message_private_email_notifications', TRUE)) {
$form['field_private_message_notify']['#access'] = FALSE;
}
}
/**
* Get the role id with the maximum allowed message create limit.
*
* Using the values set for each role, calculate the the lowest time interval
* required per message: INTERVAL / LIMIT and return the role with this value.
*
* @param $roles
* @return mixed
*/
function _message_private_max_message_limit_role($roles) {
$limits = array();
// Get the default limit and interval.
$limit_name = 'message_private_default_limit';
$interval_name = 'message_private_default_interval';
$limits[MESSAGE_PRIVATE_DEFAULT_INDEX] = variable_get($interval_name) / variable_get($limit_name);
// Cycle through the roles and get each limit and interval.
foreach ($roles as $rid => $role) {
$role_name = str_replace(' ', '_', $role);
$limit_name = 'message_private_' . $role_name . '_limit';
$interval_name = 'message_private_' . $role_name . '_interval';
$interval = variable_get($interval_name);
$limit = variable_get($limit_name);
// Ensure values are greater than 0 & get interval required for one message.
if ($interval >= MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN && $limit >= MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) {
$limits[$rid] = $interval_name / $limit;
}
}
return array_search(min($limits), $limits);
}