-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmtls.module
1356 lines (1134 loc) · 38.3 KB
/
cmtls.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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include_once('cmtls.features.inc');
function printr($var)
{
echo '<pre>';
print_r($var);
echo '</pre>';
}
/**
* Implements hook_init()
*
* Adds context menu styles and scripts
*
*/
function cmtls_init()
{
global $language;
// context menu
drupal_add_css(drupal_get_path('module', 'cmtls').'/css/context_menu.css');
drupal_add_js(drupal_get_path('module', 'cmtls') .'/js/jquery.contextMenu.js');
drupal_add_js(drupal_get_path('module', 'cmtls') .'/js/app.js');
drupal_add_css(drupal_get_path('module', 'cmtls').'/css/cmtls.css');
drupal_add_js(drupal_get_path('module', 'cmtls') .'/js/jquery.livequery.min.js');
// Send the Modal Frame javascript for parent windows to the page.
modalframe_parent_js();
drupal_set_html_head('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js"></script>');
drupal_add_js('var addthis_config = {
ui_language: "'.$language->language.'",
ui_cobrand: "Community Tools",
ui_click: true,
username: "communitytools",
services_compact: "email, facebook, twitter, linkedin, orkut, tumblr, blogger, delicious, digg, more"
};', 'inline');
}
/**
* Implents hook_nodeapi()
* Adds context menu edit actions (by invoking hook_cmtls_context) and adds cmtls_context_button attribute to nodes
*
* @param node $node
* @param string $op
* @param unknown_type $a3
* @param unknown_type $a4
* @return array
*/
function cmtls_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
{
global $user;
static $binds;
// some of the cck fields "forget" their value after saving
if($op == 'insert' && $user->uid && strpos($node->type, 'cmtls_') === 0)
{
db_query("DELETE FROM {cache_content} WHERE cid LIKE '%content:%d%'", $node->nid);
}
// Generate icons for uploaded files
if ($op == 'load' && strpos($node->type, 'cmtls_') === 0)
{
module_load_include('inc', 'cmtls_gallery', 'cmtls_gallery.file_types');
$file_types = cmtls_gallery_get_file_type_map();
global $base_url;
if (is_array($node->field_cmtls_files)) {
foreach ($node->field_cmtls_files as $key => &$file) {
if (is_array($file)) {
// Figure out the object type we're about to render
$file_extension = explode('.', $file['filename']);
$file_extension = !empty($file_extension) ? strtolower(array_pop($file_extension)) : '';
$object_type = isset($file_types[$file_extension]) ? $file_types[$file_extension]['type'] : '';
// Render element according to type of object
switch ($object_type) {
case 'image':
$file['imagecache_icon'] = theme('imagecache', 'small', $file['filepath'], $file['data']['description'], $file['data']['description']);
break;
case 'video':
case 'audio':
default:
$icon_name = (isset($file_types[$file_extension]) and isset($file_types[$file_extension]['icon'])) ? $file_types[$file_extension]['icon'] : strtolower($file_extension) . '.png';
// Try to get icon for the file
if (file_exists(drupal_get_path('module', 'cmtls_gallery') . '/icons/' . $icon_name)) {
$icon = $base_url.'/'.drupal_get_path('module', 'cmtls_gallery') . '/icons/' . $icon_name;
}
else {
// Try to get generic icon for file type
if (file_exists(drupal_get_path('module', 'cmtls_gallery') . '/icons/' . $file_types[$file_extension]['type'] . '.png')) {
$icon = $base_url.'/'.drupal_get_path('module', 'cmtls_gallery') . '/icons/' . $file_types[$file_extension]['type'] . '.png';
// Use default icon for file
}
else {
$icon = $base_url.'/'.drupal_get_path('module', 'cmtls_gallery') . '/icons/default.png';
}
}
$file['imagecache_icon'] = '<img src="' . $icon . '" alt="' . $file['data']['description'] . '" />';
}
$file['imagecache_object_type'] = $object_type;
}
}
}
}
if($op == 'load' && $user->uid && strpos($node->type, 'cmtls_') === 0)
{
$context = module_invoke_all('cmtls_context');
if(is_array($context['script']))
{
foreach($context['script'] as $script)
{
drupal_add_js($script);
}
}
else
{
drupal_add_js($context['script']);
}
unset($context['script']);
$context_actions = array();
$add_json = array();
foreach ($context as $node_type => &$actions) if($node_type == $node->type)
{
foreach($actions as $name => $action)
{
if(isset($action['access callback']))
{
if(isset($action['access arguments']))
{
// replace 'node' in argument list with $node
foreach($action['access arguments'] as &$arg)
{
switch ($arg)
{
case 'node':
$arg = $node;
$add_json['node'] = 1;
break;
/*
case 'user':
$arg = $user;
$add_json['user'] = 1;
break;
*/
default: break;
}
}
if(call_user_func_array($action['access callback'], $action['access arguments']))
{
$context_actions[$name] = $action;
}
}
elseif($actions['access callback']())
{
$context_actions[$name] = $action;
}
}
else
{
$context_actions[$name] = $action;
}
// add the bind
if(!isset($binds[$name]))
{
$bind = "$.fn.contextMenu.addAction({name: '".$name."', title: '".$action['title']."', bind: ".$name."});";
drupal_add_js($bind, 'inline');
$binds[$name] = 1;
}
}
}
$anchor = '';
if(sizeof($context_actions))
{
$buttons = implode(',', array_keys($context_actions));
$anchor_src = base_path().'sites/all/themes/cmtls_theme/images/edit.png';
$anchor = '<img src="'.$anchor_src.'" class="context_anchor" width="24" height="24" buttons="'.$buttons.'"';
if(isset($add_json['node']))
{
$node_json = _cmtls_node_json($node);
$anchor .= " object='".$node_json."'";
}
$anchor .= '>';
}
//printr($context_actions); // exit;
return array('cmtls_context_button' => $anchor);
}
}
/**
* Helper function to filter node attributes
*
* @return array
*/
function _cmtls_node_attributes()
{
return array(
'nid',
'uid',
'name',
'type',
'language',
'status',
'created',
);
}
function _cmtls_node_json_prepare($node)
{
$node_json = array();
foreach(_cmtls_node_attributes() as $attr)
{
$node_json[$attr] = $node->$attr;
}
return (object)$node_json;
}
/**
* Helper function to encode a node to json
*
* @param node $node
* @return string
*/
function _cmtls_node_json($node)
{
return json_encode(_cmtls_node_json_prepare($node));
}
/**
* Implements hook_form_alter
* prepare app node form for cleanup
*
* @param unknown_type $form
* @param unknown_type $form_state
*/
function cmtls_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == 'node_delete_confirm' && _cmtls_is())
//if($form_id == 'node_delete_confirm')
{
// Send the Modal Frame javascript for child windows to the page.
modalframe_child_js();
if($form['actions']['cancel'])
{
$form['actions']['cancel'] = array(
'#value' => '<a href="javascript:void(0);" onclick="Drupal.modalFrameChild.triggerParentEvent(\'childClose\', false); return false;">'.t('Cancel').'</a>',
);
}
$form['#submit'][] = 'cmtls_delete_confirm_submit';
$form['#node'] = node_load($form['nid']['#value']);
$form_state['redirect'] = NULL;
}
if($form_id == 'page_node_form' && $_GET['preselect_menu'] == 'cmtls-site-menu')
{
$form['menu']['parent']['#default_value'] = 'cmtls-site-menu:0';
$form['menu']['link_title']['#required'] = 1;
}
//printr($form); exit;
}
function cmtls_delete_confirm_submit($form, &$form_state)
{
$node = $form['#node'];
if($node->type == 'cmtls_group')
{
//$main_group = _cmtls_group_get_main();
//$redirect = base_path().'cmtls/'.$main_group->nid;
$redirect = base_path().'cmtls/list';
}
elseif ($node->type == 'cmtls_app')
{
$current_group = _cmtls_group_get_current();
$redirect = base_path().'cmtls/'.$current_group->nid;
}
else
{
$current_group = _cmtls_group_get_current();
$current_app = _cmtls_app_get_current($current_group);
$redirect = base_path().'cmtls/'.$current_group->nid.'/'.$current_app->nid;
}
// Tell the parent window to close the modal frame dialog.
modalframe_close_dialog(array(
'redirect' => $redirect,
));
}
/**
* Common install procedures
*
* @param string $module_name
*/
function cmtls_module_install($module_name)
{
// Sets the weigth below the Views module, so the preprocessors can kick in for the views templates
db_query("UPDATE {system} SET weight = 11 WHERE name = '%s'", $module_name);
}
/**
* The context edit menu for nodes hook example
*
* @return array
*/
function hook_cmtls_context()
{
return array(
'example_node_type' => array(
'example_action_function' => array(
'title' => 'Example action',
'access callback' => 'example_access',
'access arguments' => array(),
),
'example_action_function2' => array(
'title' => 'Example action 2',
),
),
'script' => 'js/example_actions.js',
);
}
/**
* Helper function to determine if this is the CT subsite
*
* @return boolean
*/
function _cmtls_is()
{
if(arg(0) == 'cmtls')
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* returns the node::cmtls_context_button attribute containing the edit button HTML
*
* @param integer $nid the node ID
* @return string
*/
function _cmtls_edit_button($node)
{
if(!is_object($node))
{
$node = node_load($node);
}
return $node->cmtls_context_button;
}
/**
* Returns the Drupal path to the node, false if no pathing function is found
* Each module should implement the node type pathing function <NODE_TYPE>_path($node, $group)
*
* @param integer $nid node ID
* @param mixed $group the group node object or the group node ID, defaults to current group
* @return mixed
*/
function cmtls_path_to_node($node, $group = NULL)
{
static $nodes;
if(!is_object($node)) $node = node_load($node);
if(isset($nodes[$node->nid])) return $nodes[$node->nid];
if($group && !is_object($group)) $group = node_load($group);
if(!$group->nid) $group = _cmtls_group_get_current();
$path_function = $node->type.'_path';
if(function_exists($path_function))
{
$nodes[$node->nid] = $path_function($node, $group);
return $nodes[$node->nid];
}
else
{
return FALSE;
}
}
function cmtls_node_edit_page($node_type, $node = NULL)
{
// Send the Modal Frame javascript for child windows to the page.
modalframe_child_js();
module_load_include('inc', 'node', 'node.pages');
if(!$node)
{
return node_add($node_type);
}
else
{
return node_page_edit($node);
}
}
function cmtls_mail($key, &$message, $params)
{
$language = $message['language'];
//$variables = user_mail_tokens($params['account'], $language);
if($key == 'cmtls_mail')
{
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
}
}
/**
* Implementation of hook_perm
*/
function cmtls_perm()
{
return array('administer cmtls');
}
/**
* Implementation of hook_menu
*/
function cmtls_menu()
{
$items = array();
$items['admin/build/cmtls'] = array(
'title' => t('Community Tools'),
'description' => t('Community Tools configuration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('cmtls_config_form'),
'access arguments' => array('administer cmtls'),
'type' => MENU_NORMAL_ITEM
);
$items['admin/build/cmtls/notifications'] = array(
'title' => t('Notifications'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
$items['cmtls/node/%node'] = array(
'title callback' => 'node_page_title',
'title arguments' => array(2),
'page callback' => 'node_page_view',
'page arguments' => array(2),
'access callback' => 'node_access',
'access arguments' => array('view', 2),
'type' => MENU_CALLBACK
);
return $items;
}
function cmtls_config_form(&$form_state)
{
$form = array();
$form['cmtls_comment_notifications'] = array(
'#type' => 'checkbox',
'#title' => t('Send notifications about new comments to users'),
'#description' => t('Notifications are sent the owner of the commented node and all commenters in that node'),
'#default_value' => variable_get('cmtls_comment_notifications', '1'),
);
$form['cmtls_daily_digest'] = array(
'#type' => 'checkbox',
'#title' => t('Send daily e-mail about new content to users'),
'#description' => t('Notifications are sent daily about new content and commented content within last 24hrs'),
'#default_value' => variable_get('cmtls_daily_digest', '1'),
);
// Make a system setting form and return
return system_settings_form($form);
}
function cmtls_block($op = 'list', $delta = 0, $edit = array())
{
global $user;
if($op == 'list')
{
$blocks = array();
$blocks['powered_by_cmtls'] = array(
'info' => t('Powered by Community Tools'),
'weight' => '10',
// Not worth caching.
'cache' => BLOCK_NO_CACHE,
);
$blocks['cmtls_site_menu'] = array(
'info' => t('Community Tools: site menu'),
'status' => 1,
'region' => 'cmtls_site_menu',
);
return $blocks;
}
if($op == 'view' && $delta == 'powered_by_cmtls')
{
$image_path = url(drupal_get_path('theme', 'cmtls_theme').'/images/ct-logo-small.png', array('absolute' => TRUE));
$block = array(
'subject' => NULL, // Don't display a title
'content' => '<a href="http://www.communitytools.info/"><img border="0" src="'.$image_path.'" title="'.t('Powered by Community Tools').'" alt="'.t('Powered by Community Tools').'"></a>',
);
return $block;
}
if($op == 'view' && $delta == 'cmtls_site_menu')
{
$menu = menu_tree_all_data('cmtls-site-menu');
//printr($menu); exit;
foreach ($menu as &$data) if(strpos($data['link']['href'], 'node/') === 0)
{
$data['link']['href'] = 'cmtls/'.$data['link']['href'];
}
// page add link for admins
if($user->uid == 1 || in_array('admin', $user->roles))
{
$menu['cmtls_site_menu_add'] = array(
'link' => array(
'link_title' => t('+ Add page'),
'link_path' => 'node/add/page',
'title' => t('+ Add page'),
'href' => 'node/add/page',
'localized_options' => array(
'query' => 'preselect_menu=cmtls-site-menu',
'attributes' => array(
'target' => '_blank',
),
),
),
);
}
//printr($menu); exit;
$block = array(
'content' => theme('cmtls_site_menu', menu_tree_output($menu)),
);
return $block;
}
}
function cmtls_cron()
{
if(!variable_get('cmtls_daily_digest', '1')) return;
$today = strtotime('today');
// send @ 12:00
$send_time = $today + 12 * 60 * 60;
$last_send = variable_get('cmtls_daily_digest_send', 1);
if(!($today > $last_send && $send_time < time()))
{
return;
}
// send only the last 24hrs
$last_send = time() - 24 * 60 * 60;
$mail_contents = array(
'new_groups' => array(),
'new_content' => array(),
'commented_content' => array(),
);
// get users and their groups
$users = array();
$users_sent = 0;
$sql = "SELECT users.uid, users.name as username, users.mail, profile_values.value AS name, users.data, users.language, (SELECT GROUP_CONCAT(sub_og_uid.nid) FROM {og_uid} AS sub_og_uid WHERE sub_og_uid.uid = users.uid) AS groups FROM {users} AS users LEFT JOIN {profile_values} AS profile_values ON users.uid = profile_values.uid WHERE profile_values.fid = 2";
$result = db_query($sql);
while ($user = db_fetch_object($result))
{
$user->groups = explode(',', $user->groups);
$user->data = unserialize($user->data);
$users[$user->uid] = $user;
}
//printr($users); exit;
// get all the groups
$groups = array();
$sql = "SELECT * FROM {node} WHERE type = 'cmtls_group' AND status = 1";
$result = db_query($sql);
while ($group = db_fetch_object($result))
{
if($group->created > $last_send)
{
$group->new = 1;
$mail_contents['new_groups'][$group->nid] = '"'.$group->title.'" by '.$users[$group->uid]->name.' ('.url('cmtls/'.$group->nid, array('absolute' => TRUE )).')';
}
else
{
$group->new = 0;
}
$groups[$group->nid] = $group;
}
$apps = array();
// get all the apps
$sql = "SELECT * FROM {node} AS node LEFT JOIN {nodehierarchy} AS nodehierarchy ON node.nid = nodehierarchy.nid WHERE node.type = 'cmtls_app' AND node.status = 1 AND nodehierarchy.parent <> 0";
$result = db_query($sql);
while ($app = db_fetch_object($result))
{
$groups[$app->parent]->apps[$app->nid] = $app;
}
// get fresh content
$sql = "SELECT node.nid, node.title, node.created, node_revisions.body AS body, nodehierarchy.parent, node_comment_statistics.comment_count, node_comment_statistics.last_comment_timestamp FROM
{node} AS node
LEFT JOIN {nodehierarchy} AS nodehierarchy ON node.nid = nodehierarchy.nid
LEFT JOIN {node_revisions} AS node_revisions ON node.vid = node_revisions.vid
LEFT JOIN {node_comment_statistics} AS node_comment_statistics ON node.nid = node_comment_statistics.nid
WHERE
node.type NOT IN ('cmtls_group', 'cmtls_app')
AND node.type LIKE 'cmtls_%'
AND node.status = 1
AND nodehierarchy.parent <> 0
AND (node.created > %d OR (node_comment_statistics.comment_count > 0 AND node_comment_statistics.last_comment_timestamp > %d))";
//printr($sql);
$new_content_group_ids = array();
$new_content_app_ids = array();
$result = db_query($sql, $last_send, $last_send);
// mark this before sending starts to avoid multiple mails per day
variable_set('cmtls_daily_digest_send', time());
while ($content = db_fetch_object($result))
{
//printr($content);
foreach ($groups as $group_id => &$group)
{
if(in_array($content->parent, array_keys($group->apps)))
{
$group->apps[$content->parent]->content[$content->nid] = $content;
if(!in_array($group_id, $new_content_group_ids))
{
$new_content_group_ids[$group_id] = $group_id;
}
if(!in_array($content->parent, $new_content_app_ids))
{
$new_content_app_ids[$content->parent] = $content->parent;
}
break;
}
}
if($content->created > $last_send)
{
$htmltotext = drupal_html_to_text($content->body);
$teaser = drupal_html_to_text(node_teaser($content->body, NULL, 200));
if($teaser && strlen($htmltotext) > strlen($teaser))
{
$teaser = preg_replace("/\n\n$/", '...', $teaser);
}
elseif($teaser)
{
$teaser = preg_replace("/\n\n$/", '', $teaser);
}
//if($teaser) $teaser .= "\n";
if($teaser) $teaser = preg_replace('/^/m', "\t", $teaser);
$mail_contents['new_content'][$content->parent][$content->nid] = '- '.$group->apps[$content->parent]->title.' > "'.$content->title.'"'.($content->comment_count && $content->last_comment_timestamp > $a_interval ? ' ('.$content->comment_count.') ' : NULL).' ('.url(cmtls_path_to_node($content->nid, $group), array('absolute' => TRUE)).')'.($teaser ? ':'."\n".$teaser : NULL);
}
else
{
$mail_contents['commented_content'][$content->parent][$content->nid] = '- '.$group->apps[$content->parent]->title.' > "'.$content->title.'"'.($content->comment_count ? ' ('.$content->comment_count.') ' : ' ').'('.url(cmtls_path_to_node($content->nid, $group), array('absolute' => TRUE)).')';
}
}
if(!empty($mail_contents['new_content']) || !empty($mail_contents['commented_content']) || !empty($mail_contents['new_groups']))
{
$site_name = variable_get('site_name', 'CT');
$main_group = _cmtls_group_get_main();
$profile_link = url('cmtls/'.$main_group->nid.'/member/', array('absolute' => TRUE));
foreach($users as &$user) if(!isset($user->data['cmtls']['notifications']['group']) || $user->data['cmtls']['notifications']['group'])
{
$user_lang = user_preferred_language($user);
// if any users groups has new content
$user_new_content_group_ids = array_intersect($user->groups, $new_content_group_ids);
if(!empty($user_new_content_group_ids) || !empty($mail_contents['new_groups']))
{
$body = '';
foreach ($user_new_content_group_ids as &$group_id)
{
$content_body = '';
$comments_body = '';
$user_new_content_app_ids = array_intersect(array_keys($groups[$group_id]->apps), $new_content_app_ids);
$i = 0;
foreach ($user_new_content_app_ids as &$app_id)
{
if(!empty($mail_contents['new_content'][$app_id]) && (!isset($user->data['cmtls']['notifications']['apps']['daily'][$app_id]) || $user->data['cmtls']['notifications']['apps']['daily'][$app_id]))
{
$i++;
$content_body .= implode("\n", $mail_contents['new_content'][$app_id])."\n";
}
}
if($i > 0)
{
$content_body = t('New content in "!groupname"', array('!groupname' => $groups[$group_id]->title), $user_lang->language).":\n".$content_body."\n";
}
$i = 0;
foreach ($user_new_content_app_ids as &$app_id)
{
if(!empty($mail_contents['commented_content'][$app_id]) && (!isset($user->data['cmtls']['notifications']['apps']['daily'][$app_id]) || $user->data['cmtls']['notifications']['apps']['daily'][$app_id]))
{
$i++;
$comments_body .= implode("\n", $mail_contents['commented_content'][$app_id])."\n";
}
}
if($i > 0)
{
$comments_body = t('New comments have been posted in "!groupname"', array('!groupname' => $groups[$group_id]->title), $user_lang->language).":\n".$comments_body."\n";
}
$body .= $content_body.$comments_body;
}
if(!empty($mail_contents['new_groups']))
{
$body .= t('The following new groups have been created', NULL, $user_lang->language).":\n";
$body .= implode("\n", $mail_contents['new_groups'])."\n";
}
if($body)
{
$message = array(
'subject' => t('!sitename daily summary', array('!sitename' => $site_name), $user_lang->language),
'body' =>
t('Hello !user_name', array('!user_name' => $user->name), $user_lang->language).','."\n\n".
t('Here\'s a summary of !sitename daily changes', array('!sitename' => $site_name), $user_lang->language).':'."\n\n".
$body.
"\n"."\n".'---'."\n".
$site_name.'. '.t('You can change your notification settings at !link', array('!link' => $profile_link.$user->uid), $user_lang->language)."\n"
,
);
drupal_mail('cmtls', 'cmtls_mail', $user->mail, $user_lang, $message);
//printr($message);
$users_sent++;
}
}
}
}
watchdog('cmtls', 'Daily digest sent to '.$users_sent.' members.');
}
/**
* Implements hook_theme()
*
* @param unknown_type $existing
* @return unknown
*/
function cmtls_theme($existing)
{
return array(
// pager
'cmtls_pager' => array(
'arguments' => array('href' => NULL),
'template' => 'cmtls-pager',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_site_menu' => array(
'arguments' => array('content' => NULL),
'template' => 'cmtls-site-menu',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_sm_share' => array(
'arguments' => array('href' => NULL),
'template' => 'cmtls-sm-share',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_tags' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-tags',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_author' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-author',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_comment_links' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-comment-links',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_created_time' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-created-time',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_geolocation' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-geolocation',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_location' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-location',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_title' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-title',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_files' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-files',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
'cmtls_node_detail_view_link' => array(
'arguments' => array('node' => NULL),
'template' => 'cmtls-node-detail-view-link',
'path' => drupal_get_path('module', 'cmtls').'/theme',
),
);
}
function cmtls_ajax_response($content, $options = array())
{
// add features
$features = cmtls_map_format_features(_cmtls_map_features('get'));
print drupal_json(array('content' => $content, 'features' => $features, 'options' => $options));
exit;
}
/**
* Add comments to fields view
*
* @param object $vars
*/
function cmtls_preprocess(&$vars, $hook)
{
if ($hook != 'cmtls_sm_share' && strpos($hook, 'views_view_fields') === 0 && isset($vars['view']) && isset($vars['fields']) && isset($vars['fields']['nid']))
{
$vars['sm_share_buttons'] = theme('cmtls_sm_share', $GLOBALS['base_url'].'/'.cmtls_path_to_node($vars['fields']['nid']->raw));
}
static $pager;
if ($hook != 'cmtls_pager' && $hook != 'cmtls_sm_share')
{
if($pager)
{
$vars['pager'] = $pager;
}
else
{
$params = $_GET;
unset($params['q']);
if(!$params['page']) $params['page'] = 1;
$pager_links = array();
foreach ($params as $key => &$value)
{
$pager_links[] = check_plain($key).'='.check_plain($value);
}
$pager = $vars['pager'] = theme(
'cmtls_pager',
'?'.implode('&', $pager_links)
);
}
}
static $region;
if($hook != 'page' && $hook != 'node' && $hook != 'cmtls_sm_share' && $hook != 'cmtls_pager' && $hook != 'block' && $hook != 'cmtls_content_header')
{
if(!$region)
{
$vars['cmtls_content_header'] = $region = theme('blocks', 'cmtls_content_header');
}
else
{
$vars['cmtls_content_header'] = $region;
}
}
}
/*
=======
* inserts values from $arr2 after (or before) $key in $arr1
* if $key is not found, values from $arr2 are appended to the end of $arr1
*
* This function uses array_merge() so be aware that values from conflicting keys
* will overwrite each other
*
* @param $arr1
* array to insert into
* @param $key
* key of $arr1 to insert after (or before)
* @param $arr2
* array whose values should be inserted
* @param $before
* boolean. insert before the given key. defaults to inserting after
* @return
* merged array
*/
function drupal_array_insert($arr1, $key, $arr2, $before = FALSE) {
$index = array_search($key, array_keys($arr1));
if($index === FALSE){
$index = count($arr1); // insert @ end of array if $key not found
}
else {
if(!$before){
$index++;
}
}
$end = array_splice($arr1, $index);
return array_merge($arr1, $arr2, $end);
}
/*
// EXAMPLE //////////////////
$arr1 = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4);
$arr2 = array('first' => 1, 'second' => 2, 'third' => 3, 'fourth' => 4);
$new_array = drupal_array_insert($arr1, 'three', $arr2);