forked from mccrodp/message_private
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_private.module
executable file
·357 lines (321 loc) · 12.8 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
<?php
/**
* @file
* Message Private with access permissions based on message fields.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* The string name for message limit variable.
*/
const MESSAGE_PRIVATE_MESSAGE_LIMIT = 'message_private_message_limit';
/**
* The assoc index to be used when fetching default limit variable.
*/
const MESSAGE_PRIVATE_DEFAULT_LIMIT = 'message_private_default_limit';
/**
* The assoc index to be used when fetching default interval variable.
*/
const MESSAGE_PRIVATE_DEFAULT_INTERVAL = 'message_private_default_interval';
/**
* The default index for settings such as role.
*/
const MESSAGE_PRIVATE_DEFAULT_INDEX = 0;
/**
* The maximum message limit.
*/
const MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX = 1000;
/**
* The minimum message limit.
*/
const MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN = 1;
/**
* The maximum message interval in minutes.
*/
const MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX = 1440;
/**
* The minimum message interval in minutes.
*/
const MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN = 1;
/**
* Implements hook_help().
*/
function message_private_help($route_name, RouteMatchInterface $arg) {
switch ($route_name) {
case 'help.page.message_private':
$output = file_get_contents(drupal_get_path('module', 'message_private') . '/README.md');
return \Drupal::moduleHandler()->moduleExists('markdown') ? \Drupal\Component\Utility\Xss::filterAdmin(\Drupal::moduleHandler()->invoke('markdown', 'filter', ['process', 0, -1, $output])) : '<h3>Message Private README</h3><pre>' . \Drupal\Component\Utility\Html::escape($output) . '</pre>';
}
}
/**
* Implements hook_entity_type_alter.
*
* Extend the message entity type by providing form handlers.
* @todo : can access handler be added to certain bundle only?
*/
function message_ui_entity_type_alter(array &$entity_types) {
if (isset($entity_types['message'])) {
/* @var $message_config \Drupal\Core\Config\Entity\ConfigEntityType */
$message_config = $entity_types['message'];
$message_config->setAccessClass('Drupal\message_private\MessagePrivateAccessControlHandler');
}
}
/**
* Access callback for Messages tab.
*
* Checks for the private_message bundle and user permissions.
*
* @param object $message
* The message object.
*
* @return bool
* TRUE if the user is allowed perform the operation, FALSE otherwise.
*/
// @todo: convert to Access class and add associated service and entry in route.
function message_private_access_callback($message) {
if ($message->type == 'private_message') {
return \Drupal::currentUser()->hasPermission('bypass private message access control') || \Drupal::currentUser()->hasPermission('view a private_message message instance');
}
return FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* This function is defined to override that provided by Message UI module. This
* allows override specific values of the form such as the cancel link. It hides
* the message_text on the edit and create form, and adds custom validation.
*/
// @todo - check form ID is matching D8 form id.
function message_private_form_message_private_message_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$entity = $form_state->getFormObject()->getEntity();
if ($entity->bundle() == 'private_message') {
$user = \Drupal::currentUser();
/* // There is no cancel link on node form, so this may be unnecessary now.
$referer = $_SERVER['HTTP_REFERER']; // Get the referrer uri from globals.
// Redirect back to referer uri is exists, otherwise to user message inbox.
// @FIXME
// l() expects a Url object, created from a route name or external URI.
// $form['actions']['cancel']['#markup'] =
l(t('Cancel'), (!empty($referer) ? $referer : 'user/'. $user->uid . '/messages'));*/
if (isset($form['text']['#type'])) {
$form['text']['#type'] = 'hidden'; // @todo - is this necessary now?
}
// @todo - owner access and validation callback needs testing.
$form['owner']['#access'] = \Drupal::currentUser()->hasPermission('bypass private message access control');
$form['#validate'][] = 'message_private_form_message_private_message_form_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.
*/
// @todo - test this is called and if form should be passed by reference.
function message_private_form_message_private_message_form_validate($form, \Drupal\Core\Form\FormStateInterface $form_state) {
// If there is an imposed message limit set in the admin settings interface.
// @FIXME
// // @FIXME
// // The correct configuration object could not be determined. You'll need to
// // rewrite this call manually.
// if (variable_get(MESSAGE_PRIVATE_MESSAGE_LIMIT, FALSE)
// && !\Drupal::currentUser()->hasPermission('bypass private message access control')) {
// $user = \Drupal::currentUser();
//
// // Get the role belonging to user with max message limit.
// $rid = _message_private_max_message_limit_role($user->roles);
// $role = user_role_load($rid);
//
// // Get the message limitation settings for this role.
// $role_name = str_replace(' ', '_', $role->name);
// $limit_name = 'message_private_' . $role_name . '_limit';
// $interval_name = 'message_private_' . $role_name . '_interval';
// $interval = variable_get($interval_name);
// $limit = variable_get($limit_name);
//
// // Calculate timestamp of the last interval.
// $current_timestamp = time();
// $interval_timestamp = strtotime('-' . $interval . ' minutes', $current_timestamp);
//
// // Get total amount of this user's messages since last interval.
// $query = new EntityFieldQuery();
// $total = $query->entityCondition('entity_type', 'message')
// ->entityCondition('bundle', 'private_message')
// ->propertyCondition('timestamp', $interval_timestamp, '>')
// ->propertyCondition('uid', $user->uid)
// ->count()
// ->execute();
//
// // Display error preventing message create when total messages over limit.
// 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(\Drupal\message\MessageInterface $message) {
// Prepare message notifications for private messages if notifications are on.
// @FIXME
// // @FIXME
// // The correct configuration object could not be determined. You'll need to
// // rewrite this call manually.
// 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.
*/
// @todo - check form ID matches D8 form id & add config for user notify field.
function message_private_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
// @FIXME
// // @FIXME
// // The correct configuration object could not be determined. You'll need to
// // rewrite this call manually.
// if (!\Drupal::currentUser()->hasPermission('bypass private message access control')
// && !variable_get(MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS, TRUE)) {
// $form['field_private_message_notify']['#access'] = FALSE;
// }
}
/**
* Implements hook_theme().
*/
// @todo - Is this still required in D8 to make twig template discoverable?
function message_private_theme() {
$info['message_private'] = array(
'render element' => 'elements',
'template' => 'message--private_message',
'base hook' => 'message_private',
);
return $info;
}
/**
* Implements hook_message_view_alter().
*/
// @todo - Is this still required in D8 to make twig template discoverable?
function message_private_message_view_alter(&$build) {
// Use template_preprocess_message_private for private_message messages only.
if (!empty($build['#bundle']) && $build['#bundle'] == 'private_message') {
$build['#theme'] = 'message_private';
}
}
/**
* Process variables for message--private_message.html.twig.
*/
// @todo - Is there a better alternative to this?
function template_preprocess_message_private(&$variables) {
// Call the parent message template function defined in message module.
if (function_exists('template_preprocess_message')) {
template_preprocess_message($variables);
}
// Save the message entity for ease of access.
if (is_object($variables['elements']['#entity'])) {
$message = $variables['elements']['#entity'];
}
// Create submitted variable containing user who created the message and date.
if (!empty($message) && $uid = $message->uid) {
$variables['date'] = format_date($message->timestamp);
// @FIXME
// theme() has been renamed to _theme() and should NEVER be called directly.
// Calling _theme() directly can alter the expected output and potentially
// introduce security issues (see https://www.drupal.org/node/2195739). You
// should use renderable arrays instead.
//
//
// @see https://www.drupal.org/node/2195739
// $variables['name'] = theme('username', array('account' => user_load($uid)));
$variables['submitted'] = t('Submitted by !username on !datetime', array(
'!username' => $variables['name'],
'!datetime' => $variables['date'],
));
}
}
/**
* 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 null|array $roles
* An assoc array of role names and ids.
*
* @return mixed
* Either a role id or null.
*/
// @todo - Is there a better location for this?
function _message_private_max_message_limit_role($roles) {
$limits = array();
// @FIXME
// // @FIXME
// // The correct configuration object could not be determined. You'll need to
// // rewrite this call manually.
// $limit = variable_get(MESSAGE_PRIVATE_DEFAULT_LIMIT);
// @FIXME
// // @FIXME
// // The correct configuration object could not be determined. You'll need to
// // rewrite this call manually.
// $interval = variable_get(MESSAGE_PRIVATE_DEFAULT_INTERVAL);
// Ensure we have existing valid numerical values for both variables.
if (!empty($limit) && ctype_digit($limit) && !empty($interval) && ctype_digit($interval)) {
$limits[MESSAGE_PRIVATE_DEFAULT_INDEX] = $interval / $limit;
}
// 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';
// @FIXME
// // @FIXME
// // The correct configuration object could not be determined. You'll need to
// // rewrite this call manually.
// $interval = variable_get($interval_name);
// @FIXME
// // @FIXME
// // The correct configuration object could not be determined. You'll need to
// // rewrite this call manually.
// $limit = variable_get($limit_name);
// Ensure we have existing valid numerical values for both variables.
if (!empty($limit) && ctype_digit($limit) && !empty($interval) && ctype_digit($interval)) {
$limits[$rid] = $interval / $limit;
}
}
// The min value corresponds to the lowest interval required per message.
return array_search(min($limits), $limits);
}