-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotices.module
More file actions
542 lines (488 loc) · 13 KB
/
notices.module
File metadata and controls
542 lines (488 loc) · 13 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/**
* @file
* Notices.
*
* Notices Manager. Notice users about new things on website.
*/
/**
* Status constant for read notices.
*/
define('NOTICES_READ', 0);
/**
* Status constant for unread notices.
*/
define('NOTICES_UNREAD', 1);
/**
* Name form with filters.
*/
define('NOTICES_FORM_NAME', 'notices_list');
/**
* Implements hook_theme().
*/
function notices_theme() {
return array(
'notices_view' => array(
'render element' => 'elements',
'template' => 'notices-view',
'file' => 'notices.theme.inc',
),
'notices_view_teaser' => array(
'render element' => 'elements',
'template' => 'notices-view-teaser',
'file' => 'notices.theme.inc',
),
);
}
/**
* Implements hook_formfilterapi().
*/
function notices_formfilterapi($op, $session_name, &$a1 = NULL) {
if ($op == 'filters') {
$filters = array();
switch ($session_name) {
case NOTICES_FORM_NAME;
$filters['message'] = array(
'#type' => 'textfield',
'#title' => t('Message'),
'#where' => array('like' => "message"),
);
$filters['provider'] = array(
'#type' => 'select',
'#title' => t('From'),
'#where' => "provider",
'#options' => notices_getProviders(),
);
$filters['status'] = array(
'#type' => 'select',
'#title' => t('Status'),
'#where' => 'new',
'#options' => array(
1 => t('New'),
0 => t('Read'),
),
);
break;
}
return $filters;
}
}
/**
* Implements of hook_views_api().
*/
function notices_views_api() {
return array(
'api' => 2,
);
}
/**
* Returns status from all Providers.
*
* @param array $notice
* Array with status by each Providers.
*
* @return array
* Statuses notice.
*
* @ingroup api
*/
function notices_getSendStatus($notice) {
$statuses = array();
$notice = (object) $notice;
foreach (module_implements('sendNotice') as $name) {
$statuses[$name] = module_invoke($name, 'sendNotice', 'status', $notice);
}
return $statuses;
}
/**
* Returns all Providers.
*
* @return array
* Array with providers.
*
* @ingroup api
*/
function notices_getProviders() {
$res = array();
foreach (module_implements('provideNotice') as $name) {
$provider = module_invoke($name, 'provideNotice', 'settings');
if (isset($provider['title'])) {
$res[$name] = $provider['title'];
}
}
return $res;
}
/**
* Implements hook_permission().
*/
function notices_permission() {
// Add permission to admin pgapi.
return array(
'read notices' => array(
'title' => t('Read own notices'),
'description' => t('Allows to read own notices'),
),
'read all notices' => array(
'title' => t('Read all notices'),
'description' => t('Allows to read all notices'),
'restrict access' => TRUE,
),
'administer notice settings' => array(
'title' => t('Administer notices'),
'description' => t('Allows to change notices module settings'),
'restrict access' => TRUE,
),
'delete notices' => array(
'title' => t('Delete own notices'),
'description' => t('Allows to delete own notices'),
),
'delete all notices' => array(
'title' => t('Delete all notices'),
'description' => t('Allows to delete all notices'),
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_menu().
*/
function notices_menu() {
$items['notices'] = array(
'title' => 'Notices',
'title callback' => 'notices_title_callback',
'page callback' => 'notices_list',
'access callback' => 'notices_user_access',
'file' => 'notices.inc',
'menu_name' => 'user-menu',
);
$items['notices/list'] = array(
'title' => 'Notices',
'page callback' => 'notices_list',
'access callback' => 'notices_user_access',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'file' => 'notices.inc',
);
$items['notices/settings'] = array(
'title' => 'Settings',
'description' => 'Configure notice settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('notices_settings'),
'access callback' => 'notices_user_access',
'type' => MENU_LOCAL_TASK,
'file' => 'notices.inc',
);
$items['notices/mark-as-read'] = array(
'title' => 'Mark as Read',
'description' => 'Mark all notices as read.',
'page callback' => 'notices_mark_as_read',
'access callback' => 'notices_user_access',
'type' => MENU_CALLBACK,
'file' => 'notices.inc',
);
$items['notices/view/%noticeid'] = array(
'title' => 'Read message',
'page callback' => 'notices_view',
'page arguments' => array(2),
'access callback' => 'notices_view_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'weight' => -5,
'file' => 'notices.inc',
);
$items['notices/delete/%noticeid'] = array(
'title' => 'Delete message',
'page callback' => 'drupal_get_form',
'page arguments' => array('notices_delete_form', 2),
'access callback' => 'notices_delete_access',
'access arguments' => array(2),
'type' => MENU_CALLBACK,
'file' => 'notices.inc',
);
$items['admin/config/notices'] = array(
'title' => 'Notices Tools',
'description' => 'Configure notice settings.',
'position' => 'right',
'weight' => 7,
'page callback' => 'notices_admin_menu_block_page',
'access arguments' => array('administer notice settings'),
'file' => 'notices.admin.inc',
);
$items['admin/config/notices/default'] = array(
'title' => 'Notices Settings',
'description' => 'Configure private messaging settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('notices_admin_settings'),
'access arguments' => array('administer notice settings'),
'weight' => -10,
'file' => 'notices.admin.inc',
);
$items['admin/config/notices/add'] = array(
'title' => 'Add notice',
'description' => 'Create custom notice.',
'page callback' => 'drupal_get_form',
'page arguments' => array('notices_settings_add'),
'access arguments' => array('administer notice settings'),
'file' => 'notices.admin.inc',
);
$items['user/%user/notices'] = array(
'title' => 'Notices',
'page callback' => 'notices_list',
'page arguments' => array(1),
'access callback' => 'notices_user_access',
'access arguments' => array('read all notices'),
'type' => MENU_LOCAL_TASK,
'file' => 'notices.inc',
);
$items['ajax/notices/notices-remove/%/%'] = array(
'title' => 'Ajax remove notice',
'page callback' => 'notices_remove_ajax',
'page arguments' => array(3, 4),
'access callback' => 'notices_delete_access',
'access arguments' => array(3),
'type' => MENU_CALLBACK,
'file' => 'notices.ajax.inc',
);
$items['ajax/notices/notices-mark-as-read'] = array(
'title' => 'Ajax mark notice as read',
'page callback' => 'notices_mark_read_ajax',
'page arguments' => array('all'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'notices.ajax.inc',
);
$items['ajax/notices/notices-mark-as-read/%noticeid'] = array(
'title' => 'Ajax mark notice as read',
'page callback' => 'notices_mark_read_ajax',
'page arguments' => array(3),
'access callback' => 'notices_view_access',
'access arguments' => array(3),
'type' => MENU_CALLBACK,
'file' => 'notices.ajax.inc',
);
return $items;
}
/**
* Notice wrapper for user_access. Never allows anonymous user access as
* that doesn't makes sense.
*
* @param string $permission
* Permission string, defaults to read private notice.
*
* @param object $account
* User object.
*
* @return bool
* TRUE if user has access, FALSE if not.
*/
function notices_user_access($permission = 'read notices', $account = NULL) {
if ($account === NULL) {
global $user;
$account = $user;
}
// Disallow anonymous access, regardless of permissions.
if (!$account->uid) {
return FALSE;
}
if (!user_access($permission, $account)) {
return FALSE;
}
return TRUE;
}
/**
* Check access to the view notices page.
*
* Function to restrict the access of the view notices page or
* ajax call by user_id.
*/
function notices_view_access($notice) {
global $user;
if ($notice->uid != $user->uid) {
if (notices_user_access('read all notices', $user)) {
return TRUE;
}
}
elseif (notices_user_access('read notices') && (arg(1) == 'view' || arg(2) == 'notices-mark-as-read')) {
return TRUE;
}
return FALSE;
}
/**
* Check access to the delete notice.
*
* Function to restrict the access of the delete notice page or
* ajax call by user_id.
*/
function notices_delete_access($notice) {
global $user;
if ($notice->uid != $user->uid) {
if (notices_user_access('delete all notices', $user)) {
return TRUE;
}
}
elseif (notices_user_access('delete notices') && arg(1) == 'delete') {
return TRUE;
}
return FALSE;
}
/**
* Loads notice by ID.
*
* Used as menu autoload function.
*
* @param int $noticeid
* Notice ID.
*
* @return object
* Loaded notice.
*
* @ingroup api
*/
function noticeid_load($noticeid) {
$notice = db_query('SELECT * FROM {notices} WHERE noticeid = :noticeid', array(
':noticeid' => $noticeid,
))->fetchObject();
return $notice;
}
function notices_load_settings($uid) {
$settings = db_query('SELECT * FROM {notices_settings} WHERE uid = :uid', array(
':uid' => $uid,
))->fetchObject();
$account = user_load($uid);
if (!empty($settings)) {
$settings->settings = unserialize($settings->settings);
foreach (module_implements('provideNotice') as $name) {
module_invoke($name, 'provideNotice', 'settings');
if (module_exists('notices_email')) {
if(!isset($settings->settings[$name])) {
$settings->settings[$name] = array(
'notices_email' => array(
'active' => 1,
'values' => $account->mail,
),
);
}
}
$settings->is_empty = FALSE;
}
}
else {
$settings = new stdClass();
$settings->uid = $account->uid;
foreach (module_implements('provideNotice') as $name) {
module_invoke($name, 'provideNotice', 'settings');
if (module_exists('notices_email')) {
$settings->settings[$name] = array(
'notices_email' => array(
'active' => 1,
'values' => $account->mail,
),
);
}
$settings->is_empty = TRUE;
}
}
return $settings;
}
/**
* Menu item title callback for the 'notices' path.
*/
function notices_title_callback($title = NULL) {
$count = notices_unread_count();
if ($count > 0) {
return t('Notices (@count new)', array('@count' => $count));
}
return t('Notices');
}
/**
* Return number of unread notices for an account.
*
* @param object $account
* Specifiy the user for which the unread count should be loaded.
*
* @ingroup api
*/
function notices_unread_count($account = NULL) {
if (!$account || $account->uid == 0) {
global $user;
$account = $user;
}
$count = db_query('SELECT count(*) as notices FROM {notices} WHERE uid=:uid AND new =:new', array(
':uid' => $account->uid,
':new' => NOTICES_UNREAD,
))->fetchObject();
return $count->notices;
}
/**
* Mark notice as readed.
*
* @param object $notice
* Notice object.
*
* @return bool
* Create new notice or not.
*
* @ingroup api
*/
function notices_mark_read($notice) {
$notice->new = NOTICES_READ;
notices_save($notice);
}
/**
* Delete notice.
*
* @param int $noticeid
* Notice ID.
*
* @ingroup api
*/
function notices_delete($noticeid) {
$notice = noticeid_load($noticeid);
$settings = notices_load_settings($notice->uid);
if (isset($settings->settings)) {
$send_options = $settings->settings[$notice->provider];
if (is_array($send_options)) {
foreach ($send_options as $name => $status) {
module_invoke($name, 'sendNotice', 'delete', $notice);
}
}
}
db_delete('notices')
->condition('noticeid', $noticeid, '=')
->execute();
}
/**
* Save or update notice.
*
* @param object $notice
* Object notice.
*
* @ingroup api
*/
function notices_save(&$notice) {
$notice = (object) $notice;
if (isset($notice->noticeid)) {
return drupal_write_record('notices', $notice, 'noticeid');
}
else {
if (!isset($notice->format)) {
$notice->format = variable_get('filter_default_format', 1);
}
$notice->new = NOTICES_UNREAD;
$notice->timestamp = REQUEST_TIME;
$new_notice = drupal_write_record('notices', $notice);
$settings = notices_load_settings($notice->uid);
if (!empty($settings->settings)) {
$send_options = isset($settings->settings[$notice->provider]) ? $settings->settings[$notice->provider] : FALSE;
if (is_array($send_options)) {
foreach ($send_options as $name => $status) {
if ($status['active'] == 1) {
module_invoke($name, 'sendNotice', 'send', $notice, $status['values']);
}
}
//foreach
}
}
return $new_notice;
}
}