-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforum_notification.module
More file actions
executable file
·652 lines (598 loc) · 20.6 KB
/
forum_notification.module
File metadata and controls
executable file
·652 lines (598 loc) · 20.6 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?php
/**
* @file forum_notification module file.
* author: yani@bellcom.dk
*/
function forum_notification_menu(){
$items = array();
$items['intra/medlemmer/setting'] = array(
'title' => t('Forum notifikation opdatering'),
'description' => 'Opdatering af notifikation for alle brugere',
'page callback' => 'drupal_get_form',
'page arguments' => array('forum_notification_settings_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK
);
$items['admin/structure/forum/update_users'] = array(
'title' => t('Forum notifikation opdatering'),
'description' => 'Opdatering af notifikation for alle brugere',
'page callback' => 'drupal_get_form',
'page arguments' => array('forum_notification_settings_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK
);
$items['user/%/forum_notification'] = array(
'title' => t('Forum notifikation indstilling'),
'page callback' => 'drupal_get_form',
'page arguments' => array('forum_notification_user_settings_form'),
'access callback' => 'forum_notification_user_access',
'type' => MENU_LOCAL_TASK
);
return $items;
}
/**
* Notification user setting form access.
*/
function forum_notification_user_access() {
global $user;
if (user_is_logged_in() && in_array('administrator', array_values($user->roles))) {
return TRUE;
}
if($user->uid == arg(1)) {
return TRUE;
}
return FALSE;
}
/**
* forum_notification_user_settings_form().
*
*/
function forum_notification_user_settings_form($form, $form_state) {
$form = forum_notification_form_builder();
$form['forum_user']['submit'] = array(
'#type' => 'submit',
'#value' => t('Gem!'),
'#access' => user_is_logged_in(),
'#submit' => array('forum_notification_user_settings_form_submit')
);
$form['user_id'] = array(
'#type' => 'hidden',
'#default_value' => arg(1),
);
return $form;
}
function forum_notification_user_settings_form_submit($form, &$form_state){
if(is_numeric($form_state['values']['user_id']) && $form_state['values']['user_id'] != 0) {
$notify = forum_notification_load_by_uid($form_state['values']['user_id']);
$frequency = $form_state['values']['forum_update_user_frequency'];
$forums = $form_state['values']['forum_update_user_options'];
$notify->changes_forums = serialize($forums);
$notify->frequency = $frequency;
forum_notification_save($notify);
drupal_set_message("Forum Notifikation opdateret", 'status');
}
else {
drupal_set_message("Der findes ikke brugeren", 'error');
return;
}
}
/*
* Build the forum notification form.
*/
function forum_notification_form_builder($form = NULL) {
$form['forum_update_user_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Forum Notification'),
'#collapsible' => TRUE,
);
$data = forum_forum_load();
$newdata = $init = array();
foreach ($data->forums as $tid => $item) {
$forum_name = $item->name;
$newdata[$tid] = t($forum_name);
$init[$tid] = $tid;
}
// Check if there is a record in database.
$uri = current_path();
$uri_0 = preg_split("/\//",$uri);
if (isset($uri_0[1]) && is_numeric($uri_0[1])) {
$notify = forum_notification_load_by_uid($uri_0[1]);
}
if (isset($notify->changes_forums)) {
$val_1 = unserialize($notify->changes_forums);
}
else {
$val_1 = $init;
}
$val_2 = isset($notify->frequency) ? $notify->frequency : 4;
$newdata_2 = array(
'0' => t('Never'),
'1' => t('Daily'),
'2' => t('Weekly'),
'3' => t('Monthly'),
'4' => t('Live'),
);
$form['forum_update_user_settings']['forum_update_user_frequency'] = array(
'#type' => 'select',
'#title' => t('Forum notification frequency'),
'#options' => $newdata_2,
'#description' => t('Choose notification frequency, default is weekly'),
'#default_value' => $val_2,
);
$form['forum_update_user_settings']['forum_update_user_options'] = array(
'#type' => 'checkboxes',
'#title' => t('Forum notification options'),
'#options' => $newdata,
'#description' => t('Check or uncheck the respective fields'),
'#default_value' => $val_1,
);
return $form;
}
function forum_notification_settings_form($form, $form_state) {
global $user;
$is_admin = in_array('administrator', array_values($user->roles));
$data = forum_forum_load();
$newdata = array();
foreach ($data->forums as $tid => $item) {
$forum_name = $item->name;
$newdata[$tid] = t($forum_name);
}
$form['forum_user_forum_settings']['forum_user_update_options'] = array(
'#type' => 'checkboxes',
'#title' => 'Tilføj/Slet forum(s) - alle brugere',
'#options' => $newdata,
'#description' => t('Check or uncheck the respective fields'),
);
$form['forum_user_forum_settings']['forum_user_update_options_1'] = array(
'#type' => 'checkbox',
'#prefix' =>'<b>',
'#suffix' =>'</b>',
'#title' => "<b>Bekræft ! -- ".t('This action cannot be undone.') ."</b>",
'#required' => TRUE,
'#description' => t('Alle brugere blive opdateret.'),
);
$form['forum_user_forum_settings']['forum_user_forum_settings_submit'] = array(
'#type' => 'submit',
'#value' => t('Tilføj!'),
'#access' => $is_admin,
'#submit' => array('forum_notification_settings_form_submit')
);
$form['forum_user_forum_settings']['forum_user_forum_settings_submit_delete'] = array(
'#type' => 'submit',
'#value' => t('Slet fra alle brugere!'),
'#access' => $is_admin,
'#submit' => array('forum_notification_settings_form_delete')
);
return $form;
}
function forum_notification_settings_form_submit($form, &$form_state) {
$count = forum_notification_update_all_users($operation = 1,$form_state);
if($count < 2) {
$bruger = "bruger";
}
else {
$bruger = "brugere";
}
drupal_set_message("$count $bruger opdateret", 'status');
}
function forum_notification_update_all_users($operation = 1,&$form_state){
$data = forum_forum_load();
$newdata = array();
foreach ($data->forums as $tid => $item) {
if($form_state['values']['forum_user_update_options'][$tid]) {
$newdata[$tid] = $form_state['values']['forum_user_update_options'][$tid];
}
}
$users = entity_load('user');
//$users = user_load_multiple(array(1,26));
$count = 0;
foreach ($users as $user) {
if($user->status == 0) {
continue;
}
$notify = forum_notification_load_by_uid($user->uid);
if (isset($notify->changes_forums)) {
$notify_old = $user_notify = unserialize($notify->changes_forums);
}
else {
$notify_old = $user_notify = array();
}
if(!empty($newdata)) {
foreach ($newdata as $key => $val) {
if($operation == 1 && (!array_key_exists($key,$user_notify) || $user_notify[$key] == 0)) {
$user_notify[$key] = $val;
}
elseif ($operation == 2 && (!array_key_exists($key,$user_notify) || $user_notify[$key] != 0)) {
$user_notify[$key] = 0;
}
}
}
if (count($notify_old) != count($user_notify) || array_diff($notify_old,$user_notify) != array_diff($user_notify,$notify_old)) {
$notify->changes_forums = serialize($user_notify);
forum_notification_save($notify);
$count += 1;
}
else continue;
}
return $count;
}
function forum_notification_settings_form_delete($form,&$form_state) {
$count = forum_notification_update_all_users($operation = 2,$form_state);
if($count < 2) {
$bruger = "bruger";
}
else {
$bruger = "brugere";
}
drupal_set_message("$count $bruger opdateret", 'status');
}
/*
*Implement hook_form_alter().
*/
function forum_notification_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($form_id == 'user_register_form' || ($form_id == 'user_profile_form' && $form['#user_category'] == 'account')) {
$form = forum_notification_form_builder($form);
}
}
/**
* Implements hook_user_presave().
*
* User data (forum nitification).
* This function handles existing user account, forum_notification_user_insert takes
* care of new accounts.
* @see forum_notification_user_insert()
*/
function forum_notification_user_presave(&$edit, $account, $category) {
switch ($category) {
case 'account':
// We only process existing accounts.
if (!empty($account->uid)) {
$notify = forum_notification_load_by_uid($account->uid);
if ($notify) {
$notify->uid = $account->uid;
// Update mail, status and language if they are changed.
if (isset($edit['mail'])) {
$notify->mail = $edit['mail'];
}
if (isset($edit['forum_update_user_options'])) {
$notify->changes_forums = serialize($edit['forum_update_user_options']);
}
if (isset($edit['forum_update_user_frequency'])) {
$notify->frequency = $edit['forum_update_user_frequency'];
}
forum_notification_save($notify);
}
elseif (isset($edit['mail']) && !empty($edit['mail'])) {
$query = db_insert('forum_notification')
->fields(array(
'mail' => $edit['mail'],
'uid' => $account->uid,
'frequency' => $edit['forum_update_user_frequency'],
'changes_forums' => serialize(isset($edit['forum_update_user_options']) ? $edit['forum_update_user_options'] : array()),
'created' => REQUEST_TIME,
));
$last_insert_id = $query->execute();
}
}
break;
}
}
/**
* Load a forum notification object.
*
* @param $uid
* user id.
* @return
* forum notification object, FALSE if user does not want be notified.
*
*/
function forum_notification_load_by_uid($uid) {
$result = db_query("SELECT * FROM {forum_notification} WHERE uid = $uid")->fetchObject();;
return isset($result) ? $result: FALSE;
}
/**
* Store forum notification object in the database.
*/
function forum_notification_save(&$notify) {
if (!empty($notify->nnid)) {
db_update('forum_notification')
//->key(array('tid' => $category->tid))
->condition('nnid', $notify->nnid)
->fields(array(
'nnid' => $notify->nnid,
'mail' => $notify->mail,
'uid' => $notify->uid,
'frequency' => $notify->frequency,
'changes_forums' => $notify->changes_forums,
))
->execute();
}
elseif (empty($notify->nnid)) {
$query = db_insert('forum_notification')
->fields(array(
'mail' => $notify->mail,
'uid' => $notify->uid,
'frequency' => $notify->frequency,
'changes_forums' => $notify->changes_forums,
'created' => REQUEST_TIME,
));
$last_insert_id = $query->execute();
if ($last_insert_id !== FALSE) {
$notify->nnid = $last_insert_id;
}
}
}
/**
* Implements hook_user_insert().
*
* Update uid and preferred language when the new account was already subscribed.
*/
function forum_notification_user_insert(&$edit, $account, $category) {
// Use the email address to check if new account is already notified.
$notify = forum_notification_load_by_uid($edit['uid']);
// If the user is notified, return.
if ($notify) {
return;
}
// Process forum notification check boxes.
if (isset($edit['forum_update_user_frequency'])) {
$query = db_insert('forum_notification')
->fields(array(
'mail' => $edit['mail'],
'uid' => $account->uid,
'frequency' => $edit['forum_update_user_frequency'],
'changes_forums' => serialize(isset($edit['forum_update_user_options']) ? $edit['forum_update_user_options'] : array()),
'created' => REQUEST_TIME,
));
$last_insert_id = $query->execute();
}
}
/**
* Implements hook_user_delete().
*/
function forum_notification_user_delete($account) {
// Delete notification when account is removed.
$notify = forum_notification_load_by_uid($account->uid);
if ($notify) {
forum_notification_delete($notify);
}
}
/**
* Delete notification from the database.
*
* @param $nnid
* notification object.
*
*/
function forum_notification_delete(stdClass $notify) {
db_delete('forum_notification')
->condition('uid', $notify->uid)
->execute();
}
/*
*Implement hook_menu()
*
*
function forum_notification_menu() {
// TODO: test with arguments!!!
$items = array();
$items['forum_notification/test'] = array(
'title' => 'Forum notification test',
// 'type' => MENU_CALLBACK,
// 'page callback' => 'drupal_get_form',
'page callback' => 'forum_notification_prepare_mail',
'access arguments' => array('administer site configuration'),
);
return $items;
}*/
function forum_notification_prepare_mail($last_send_time,$type) {
if (empty($last_send_time) || !isset($last_send_time)) {
return;
}
if (empty($type) || !isset($type)) $type = 1;
$time = strtotime($last_send_time);
$results = db_query("SELECT nid FROM {node} WHERE type= 'forum' and created >= $time")->fetchAll();
$new_node = array();
if ($results) {
foreach ($results as $node) {
$node = node_load($node->nid);
$tid = $node->taxonomy_forums['und'][0]['tid'];
$new_node[$tid][] = $node;
}
$result = forum_notification_sendmail($new_node,$type);
}
else {
$msg = "Der er ingen Forum notifikation mail blevet sendt. Der er ikke ny forumemner.";
watchdog(
'Forum notification',
$msg,
$variables = array(
'%runtime' => date('Y-m-d'),
'%success' => date('Y-m-d')
)
);
}
}
function forum_notification_generate_mail_body(stdClass $node) {
if (!isset($node) || empty($node)) {
return;
}
// $url = base_path();
$url = $_SERVER['HTTP_HOST'];
// Warning: Remember to add close tags.
$body = "<html><body><h3><a href='http://".$url."/node/".$node->nid."'>".$node->title."</a></h3>";
$author = user_load($node->uid)->name;
$body .= "<div>Af ".$author;
$body .= " den ".date('d.M.Y H:i:s',$node->created)."</div><br>";
// TODO yani: Set body length 400;
//$body_text = $node->body['und'][0]['value'];
if (isset($node->body['und'])) {
$body .= check_markup($node->body['und'][0]['value'],'full_html');
}
//$body_text = substr($body_text,0,400);
//$body = $body ."<p>".$body_text. " ... <a href='".$url."node".$node->nid."'>Læs mere</a></p>";
$body .= "<hr>";
return $body;
}
function forum_notification_sendmail($new_node,$type) {
if (empty($new_node)) {
return;
}
if (empty($type) || !isset($type)) $type = 1;
global $language;
//$url = base_path();
$url = $_SERVER['HTTP_HOST'];
$from = variable_get('site_mail', 'mail@itchefer.dk');
//variable_set('mail_system', array('default-system' => 'DefaultMailSystem', 'forum_notification' => 'ExampleMailSystem'));
variable_set('mail_system', array_merge(
variable_get('mail_system', array('default-system' => 'DefaultMailSystem')), array('forum_notification' => 'HTMLMailSystem')));
// Send email for each forum.
foreach ($new_node as $tid => $nodes) {
$forum = taxonomy_term_load($tid);
$forum->name;
$body = "<div style='font-size: 16px; font-weight: bold; margin: 10px 10px;'> Nye forumemner i ". $forum->name ." på itchefer.dk</div>" . "<hr />";
foreach ($nodes as $key => $node) {
$body .= forum_notification_generate_mail_body($node);
}
// TODO yani: admin.inc settings form for email body footer.
$site_name = variable_get('site_slogan','KIT@ Foreningen af Kommunale it-chefer');
$body_footer = "<div style='margin: 30px 0 30px 0'>";
$body_footer .= "Du har modtaget denne e-mail som medlem af KIT@. Du skal <a href ='http://".$url."/user"."'>logge ind</a> for at konfigurere dine indstillinger.<br><a href='http://".$url."/node'> KIT@ ". $site_name . "</a><br></div>";
$body .= $body_footer;
// Add html and body close tags, opened in _generate_mail_body.
$body .= "</html></body>";
$params = array(
'subject' => "Forum - " . $forum->name ." nye emner notifikation",
'message' => $body,
);
// Test: html mail
// Get mail from user who subscrips the forum notification.forum_notification_prepare_mail($last_send_time,$type)
$users = db_query("SELECT * FROM {forum_notification} f, {users} u WHERE f.uid = u.uid AND u.status = 1")->fetchAll();
foreach ($users as $key => $user) {
$forum_array = unserialize($user->changes_forums);
if (isset($forum_array[$tid]) && $forum_array[$tid] != 0 && $user->frequency == $type) {
$to = $user->mail;
$result = drupal_mail('forum_notification', 'notice', $to, $language, $params, $from, TRUE);
if ($result['result'] == TRUE) {
$msg = "Forum - ".$forum->name . " type - ".$type . " Mail is sent to email = %email";
watchdog(
'Forum notification', $msg, $variables = array(
'%email' => $to,
'%runtime' => date('Y-m-d'),
'%success' => date('Y-m-d')
)
);
}
else {
$msg = "Forum - ".$forum->name . " type - ".$type . "Error : Mail nid = %nid is NOT sent to email=%email";
watchdog(
'Forum notification', $msg, $variables = array(
'%email' => $to,
'%runtime' => date('Y-m-d'),
)
);
}
}
}
}
switch($type) {
case 1 :
$last_send_time_day = variable_set('last_send_time_day',date('Y-m-d'));
break;
case 2 :
$last_send_time_week = variable_set('last_send_time_week',date('Y-m-d'));
break;
case 3 :
$last_send_time_month = variable_set('last_send_time_month',date('Y-m-d'));
break;
}
//drupal_set_message('Mails udsendt.', 'status');
//drupal_goto("<front>");
}
/**
* Implement hook_mail()
*/
function forum_notification_mail($key,&$message, $params) {
global $language;
$from = variable_get('site_mail', 'mail@itchefer.dk');
$variables = array(
'!site-name' => variable_get('site_name','Itchefer.dk'),
'!subject' => $params['subject'],
'!category' => isset($params['category']['category']) ? $params['category']['category'] : '',
'!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
'!sender-name' => "itchefer.dk",
'!sender-url' => $from,
);
switch ($key) {
case 'notice':
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
$message['subject'] .= t($params['subject'], array('langcode' => $language));
$message['body'][] = $params['message'] ;
break;
}
}
/**
* Implements hook_cron().
*/
function forum_notification_cron() {
$last_send_time_day = variable_get('last_send_time_day','2011-12-31');
// Always send on monday.
$last_send_time_week = variable_get('last_send_time_week','2011-12-31');
// Alway send on the 1 day of next month.
$last_send_time_month = variable_get('last_send_time_month','2011-12-31');
$current_D = strtotime(date('Y-m-d'));
$last_time = strtotime($last_send_time_day);
$diff = $last_time + 24*60*60;
$current_w = date('W');
$current_y = date('Y');
$last_w_time = strtotime($last_send_time_week);
$last_w = date('W',$last_w_time);
$last_y = date('Y',$last_w_time);
// Send every day
if ($current_D == $diff) {
forum_notification_prepare_mail($last_send_time_day,1);
}
elseif ($current_y > $last_y) {
forum_notification_prepare_mail($last_send_time_day,1);
}
// Send every week
if ($current_w > $last_w) {
forum_notification_prepare_mail($last_send_time_week,2);
}
elseif ($current_y > $last_y) {
forum_notification_prepare_mail($last_send_time_week,2);
}
// Send every month
$last_m_time = strtotime($last_send_time_month);
$last_m = date('m',$last_m_time);
$current_m = date('m');
// date('d')=='01';
if ($current_m > $last_m) {
forum_notification_prepare_mail($last_send_time_month,3);
}
elseif ($current_y > $last_y) {
forum_notification_prepare_mail($last_send_time_month,3);
}
if (($current_D == $last_time || $current_w == $last_w || $current_m == $last_m) && ($current_y == $last_y)) {
$msg = "Der er ingen Forum notifikation mail blevet sendt. Mails har allerede sendt.";
watchdog(
'Forum notification',
$msg,
$variables = array(
'%runtime' => date('Y-m-d'),
'%success' => date('Y-m-d')
)
);
}
}
function forum_notification_callback_from_rules($nid) {
if (!isset($nid) || !is_numeric($nid)) {
return;
}
$node = node_load($nid);
$tid = $node->taxonomy_forums['und'][0]['tid'];
$array = array();
$array[$tid][]=$node;
forum_notification_sendmail($array,4);
}