Completed
Push — distributed_digests ( 31c007...175d64 )
by Ilia
14:44
created

functions.php ➔ dequeue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * recursively search for the group guid
5
 *
6
 * @param integer 	$entity_guid_static
7
 * @param integer 	$entity_guid
8
 *
9
 */
10
function get_forum_in_group($entity_guid_static, $entity_guid) {
11
	$entity = get_entity($entity_guid);
12
	// (base) stop recursing when we reach group guid
13
	if ($entity instanceof ElggGroup)  
14
		return $entity_guid;
15
	else 
16
		return get_forum_in_group($entity_guid_static, $entity->getContainerGUID());
17
}
18
19
20
/**
21
 * @param integer 			$user_id 
22
 * @param string 			$name 
23
 * @param array <string> 	$values
24
 * @param string 			$label
25
 */
26
function create_checkboxes($user_id, $name, $values, $label, $id='chkboxID', $class='chkboxClass') {
27
	$user_option = elgg_get_plugin_user_setting($name, $user_id, 'cp_notifications');
28
29
30
	if (strcmp($name, 'cpn_set_digest_freq_daily') == 0) {
31
		$user_option_daily = elgg_get_plugin_user_setting('cpn_set_digest_freq_daily', $user_id, 'cp_notifications');
32
		$user_option_weekly = elgg_get_plugin_user_setting('cpn_set_digest_freq_weekly', $user_id, 'cp_notifications');
33
34
		if (!$user_option_daily && !$user_option_weekly) $user_option = 'set_digest_daily';
35
	}
36
37
	if (strcmp($name, 'cpn_set_digest_lang_en') == 0) {
38
		$user_option_en = elgg_get_plugin_user_setting('cpn_set_digest_lang_en', $user_id, 'cp_notifications');
39
		$user_option_fr = elgg_get_plugin_user_setting('cpn_set_digest_lang_fr', $user_id, 'cp_notifications');
40
41
		if (!$user_option_en && !$user_option_fr) $user_option = 'set_digest_en';
42
	}
43
44
	$is_checked = (strcmp($user_option, 'set_digest_no') == 0 || strcmp($user_option, 'set_notify_off') == 0 || !$user_option || strpos($name, 'cpn_group_') !== false) ? false : true;
45
46
	$digest_option = elgg_get_plugin_user_setting('cpn_set_digest', $user_id, 'cp_notifications');
47
	$disabled = (strcmp('set_digest_yes', $digest_option) == 0 && strpos($name, 'site') !== false) ? true : false;
48
49
	$chkbox = elgg_view('input/checkbox', array(
50
		'name' => 		"params[{$name}]",
51
		'value' => 		$values[0],
52
		'default' => 	$values[1],
53
		'label' => 		$label,
54
		'checked' => 	$is_checked,
55
		'id' =>			$id,
56
		'class' =>		$class,
57
		'disabled' => 	$disabled
58
	));
59
60
	return $chkbox;
61
}
62
63
64
/**
65
 * check if object is: public, logged in, in group (check if user is in group), in friend-circle (check if user is in friend-circle)
66
 * 
67
 * @param ElggObject $entity		the entity we will check permissions of
68
 * @param int $recipient_user_id	the user guid.. to check if user is author's circle or friend
69
 */
70
function cp_check_permissions($entity, $recipient_user_id = 0) {
71
	$access_id = $entity->access_id;
72
	if ($access_id == 2 || $access_id == 1) // public or logged-in access
73
		return true;
74
75
	if ($access_id == -2) { // author or author's friends
76
		return check_entity_relationship($recipient_user_id, 'friend', $entity->getOwnerGUID()); // returns object
77
	}
78
79
	// Note: non-group members cannot subscribe to a group...
80
	// check if user is in friend's circle
81
	if ($access_id > 2) {
82
		$user_id_list = get_members_of_access_collection($access_id, true); // returns list of id in collection
83
		if (in_array($recipient_user_id, $user_id_list))
84
			return true;
85
	}
86
87
	return false;
88
}
89
90
91
92
/**
93
 * We need to modify the headers so that emails can go out (header spoofing)
94
 *
95
 * @param string 	$event
96
 */
97
function cp_get_headers($event = '') { 	// $event will be null if nothing is passed into it (no default value set)
98
99
	$email_address = elgg_get_plugin_setting('cp_notifications_email_addr','cp_notifications');
100
	if (!$email_address || $email_address === '') $email_address = '[email protected]';
101
	$php_version = phpversion();
102
103
	$headers =  "From: GCconnex <{$email_address}> \r\n";
104
	$headers .= "Reply-To: GCconnex <{$email_address}> \r\n";
105
	$headers .= "Return-Path: GCconnex <{$email_address}> \r\n";
106
	$headers .= "X-Mailer: PHP/{$php_version} \r\n";
107
	$headers .= "MIME-Version: 1.0 \r\n";
108
	$headers .= "Content-type: text/html; charset=utf-8 \r\n";
109
   
110
	if ($event === 'event') {
111
		$mime_boundary = "----Meeting Booking----".MD5(TIME());
112
		$headers .= 'Content-Type: multipart/alternative; boundary='.$mime_boundary."\r\n";
113
		$headers .= "Content-class: urn:content-classes:calendarmessage\n";
114
	}
115
116
	return $headers;
117
}
118
119
120
121
/**
122
 * scans the text object for any @mentions
123
 * 
124
 * @param string 	$cp_object
125
 */
126
function cp_scan_mentions($cp_object) {
127
	$fields = array('title','description','value');
128
	foreach($fields as $field) {
129
		$content = $cp_object->$field;													// pull the information from the fields saved to object
130
		if (preg_match_all("/\@([A-Za-z1-9]*).?([A-Za-z1-9]*)/", $content, $matches)) { // find all the string that matches: @christine.yu
131
			$users_found = array();
132
			
133
			foreach ($matches[0] as $match) {
134
135
				//if (preg_match('/\s/',$match)) { 										// what if no space found? check for space
136
					$user_found = explode(' ',$match);
137
					$users_found[] = $user_found[0];
138
139
				//}
140
			}
141
			return $users_found;
142
		}
143
	}
144
	return false;
145
}
146
147
148
function isJson($string) {
149
  json_decode($string);
150
  return (json_last_error() == JSON_ERROR_NONE);
151
}
152
153
154
/**
155
 * assembles the digest then encodes the array into JSON to be saved to digest_notification table
156
 *
157
 * @param ElggUser 		$invoked_by
158
 * @param string 		$subtype
159
 * @param ElggEntity 	$entity
160
 * @param ElggUser 		$send_to
161
 * @param string 		$entity_url (default value empty)
162
 * @return Success 		true/false
163
 */
164
function create_digest($invoked_by, $subtype, $entity, $send_to, $entity_url = '') {
165
166
	if (elgg_is_active_plugin('wet4')) elgg_load_library('GCconnex_display_in_language');
167
	elgg_load_library('elgg:gc_notification:functions');
168
169
	$digest = get_entity($send_to->cpn_newsletter);
170
	$digest_collection = json_decode($digest->description,true);
171
172
	// default title value
173
	$content_title = $entity->title;
174
175
	if (!$entity->title && ($subtype !== 'single_zip_file_upload' && $subtype !== 'multi_file_upload')) $entity = get_entity($entity->guid);
176
177
	if ($entity instanceof ElggObject) {
178
		$content_url = (!$entity_url) ? $entity->getURL() : $entity_url;
179
180
		if (isJson($entity->title))
181
		{
182
			$content_title = json_decode($entity->title, true);
183
184
		} else {
185
186
			if ($entity->title2) 
187
				$content_title = array('en' => $entity->title, 'fr' => $entity->title2);
188
			else 
189
				$content_title = array('en' => $entity->title, 'fr' => $entity->title);
190
			
191
		}
192
193
		$content_array = array(
194
			'content_title' => $content_title,
195
			'content_url' => $content_url."?utm_source=notification_digest&utm_medium=email",
196
			'subtype' => $entity->getSubtype(),
197
			'content_author_name' => $invoked_by->name,
198
			'content_author_url' => $invoked_by->getURL()."?utm_source=notification_digest&utm_medium=email"
199
		);
200
201
	} else {
202
203
        $content_array = array(
204
            'content_title' => 'colleague requests',
205
            'content_url' => $entity,
206
            'subtype' => $subtype
207
        );
208
209
    }
210
211
	if ($subtype === "hjforumcategory" || $subtype === "hjforum") return true;
212
213
214
	switch ($subtype) {
215
216
		case 'single_zip_file_upload':
217
			$file_entity = get_entity($entity[0]);
218
			$container = $file_entity->getContainerEntity();
219
			$file_count = 0;
220
221
			$display_files = "<p><ol>";
222
			foreach ($entity as $file_num => $file) {
223
				$file_count++;
224
				$file_entity = get_entity($file);
225
				$display_files .= "<li><a href='{$file_entity->getURL()}?utm_source=notification_digest&utm_medium=email'>{$file_entity->title}</a></li>";
226
			}
227
			$display_files .= "</ol></p>";
228
			$content_array = array(
229
				'file_count'			=> $file_count,
230
				'content_title' 		=> $display_files,
231
				'subtype' 				=> 'file_upload',
232
				'content_author_name' 	=> $file_entity->getOwnerEntity()->name,
233
				'content_author_url' 	=> $file_entity->getOwnerEntity()->getURL()
234
			);
235
236
			if ($container instanceof ElggUser) {
237
				$entity_guid = $file_entity->getGUID();
238
				$user_guid = $send_to->getGUID();
239
				$entry_type = 'personal';
240
				$group_name = NULL;
241
				$action_type = 'new_post';
242
				$notification_entry = json_encode($content_array);
243
			} else {
244
				$entity_guid = $file_entity->getGUID();
245
				$user_guid = $send_to->getGUID();
246
				$entry_type = 'group';
247
				$group_name = $container->name;
248
				$action_type = 'new_post';
249
				$notification_entry = json_encode($content_array);
250
			}
251
			break;
252
253
		case 'multi_file_upload':
254
255
			$file_entity = get_entity($entity[0]);
256
			$container = $file_entity->getContainerEntity();
257
			$file_count = 0;
258
259
			$display_files = "<p><ol>";
260
			foreach ($entity as $file_num => $file) {
261
				$file_count++;
262
				$file_entity = get_entity($file);
263
				$display_files .= "<li><a href='{$file_entity->getURL()}?utm_source=notification_digest&utm_medium=email'>{$file_entity->title}</a></li>";
264
			}
265
266
			$display_files .= "</ol></p>";
267
			$content_array = array(
268
				'file_count'			=> $file_count,
269
				'content_title' 		=> $display_files,
270
				'subtype' 				=> 'file_upload',
271
				'content_author_name' 	=> $file_entity->getOwnerEntity()->name,
272
				'content_author_url' 	=> $file_entity->getOwnerEntity()->getURL()
273
			);
274
275
			if ($container instanceof ElggUser) {
276
				$entity_guid = $file_entity->getGUID();
277
				$user_guid = $send_to->getGUID();
278
				$entry_type = 'personal';
279
				$group_name = NULL;
280
				$action_type = 'new_post';
281
				$notification_entry = json_encode($content_array);
282
			} else {
283
				$entity_guid = $file_entity->getGUID();
284
				$user_guid = $send_to->getGUID();
285
				$entry_type = 'group';
286
				$group_name = $container->name;
287
				$action_type = 'new_post';
288
				$notification_entry = json_encode($content_array);
289
			}
290
291
			break;
292
293
		case 'thewire':
294
			$content_array = array(
295
				'content_description' => $entity->description,
296
				'content_url' => $content_url."?utm_source=notification_digest&utm_medium=email",
297
				'subtype' => $entity->getSubtype(),
298
				'content_author_name' => $invoked_by->name,
299
				'content_author_url' => $invoked_by->getURL(),
300
				'entity' => $entity
301
			);
302
303
			$entity_guid = $entity->guid;
304
			$user_guid = $send_to->getGUID();
305
			$entry_type = 'personal';
306
			$group_name = NULL;
307
			$action_type = 'new_post';
308
			$notification_entry = json_encode($content_array);
309
			break;
310
311
		case 'mission':
312
313
			$content_array = array(
314
				'content_title' => $entity->title,
315
				'content_author_name' => $entity->getOwnerEntity()->name,
316
				'content_url' => $entity->getURL()."?utm_source=notification_digest&utm_medium=email",
317
				'subtype' => $entity->job_type,
318
				'deadline' => $entity->deadline
319
			);
320
321
			$entity_guid = $entity->guid;
322
			$user_guid = $send_to->getGUID();
323
			$entry_type = 'mission';
324
			$group_name = NULL;
325
			$action_type = 'new_post';
326
			$notification_entry = json_encode($content_array);
327
			break;
328
329
		case 'comment':
330
		case 'discussion_reply':
331
332
333
			if ($entity->getContainerEntity() instanceof ElggGroup) {
334
335
				// string that will contain the url and the (json) string name of group
336
				$group_html = json_encode(array($entity->getContainerEntity()->getURL(), $entity->getContainerEntity()->name));
337
				$entity_guid = $entity->guid;
338
				$user_guid = $send_to->getGUID();
339
				$entry_type = 'group';
340
				$group_name = $group_html;
341
				$action_type = 'response';
342
				$notification_entry = json_encode($content_array);
343
344
			} else { 
345
346
				$entity_guid = $entity->guid;
347
				$user_guid = $send_to->getGUID();
348
				$entry_type = 'personal';
349
				$group_name = NULL;
350
				$action_type = 'response';
351
				$notification_entry = json_encode($content_array);
352
			}
353
			break;
354
355
		case 'cp_friend_request':
356
357
			$entity_guid = $invoked_by->guid;
358
			$user_guid = $send_to->getGUID();
359
			$entry_type = 'personal';
360
			$group_name = NULL;
361
			$action_type = 'friend_request';
362
			$notification_entry = json_encode($content_array);
363
364
		 	break;
365
366
		case 'cp_friend_approve':
367
368
			$entity_guid = $invoked_by->guid;
369
			$user_guid = $send_to->getGUID();
370
			$entry_type = 'personal';
371
			$group_name = NULL;
372
			$action_type = 'friend_approved';
373
			$notification_entry = json_encode($content_array);
374
			break;
375
376
		case 'cp_hjtopic':
377
		case 'cp_hjpost':
378
379
			$group_html = json_encode(array(get_entity(get_forum_in_group($entity->guid, $entity->guid))->getURL(), get_entity(get_forum_in_group($entity->guid, $entity->guid))->name));
380
381
			if ($subtype === 'cp_hjtopic') {
382
383
				$entity_guid = $entity->guid;
384
				$user_guid = $send_to->getGUID();
385
				$entry_type = 'group';
386
				$group_name = $group_html;
387
				$action_type = 'forum_topic';
388
				$notification_entry = json_encode($content_array);
389
390
			} else {
391
392
				$content_array = array(
393
					'content_title' => $entity->getContainerEntity()->title,
394
					'content_url' => $content_url."?utm_source=notification_digest&utm_medium=email",
395
					'subtype' => $entity->getSubtype(),
396
					'content_author' => $entity->getOwnerEntity()->guid
397
				);
398
399
				$entity_guid = $entity->guid;
400
				$user_guid = $send_to->getGUID();
401
				$entry_type = 'group';
402
				$group_name = $group_html;
403
				$action_type = 'forum_reply';
404
				$notification_entry = json_encode($content_array);
405
406
			}
407
			break;
408
409
410
		case 'like_comment':
411
		case 'like_reply':
412
		case 'post_likes':
413
414
			if ($subtype === "like_comment" || $subtype === "like_reply") {
415
				$content_title = $entity->getContainerEntity()->title;
416
				$content_array = array(
417
					'content_title' => $content_title,
418
					'content_url' =>  $entity->getURL()."?utm_source=notification_digest&utm_medium=email",
419
					'subtype' => $entity->getSubtype(),
420
					'content_author_name' => $invoked_by->name,
421
					'content_author_url' => $invoked_by->getURL()
422
				);
423
424
425
			} elseif ($entity->getSubtype() === 'thewire') {
426
427
				$content_array = array(
428
					'content_title' => $entity->description,
429
					'content_url' =>  $entity->getURL()."?utm_source=notification_digest&utm_medium=email",
430
					'subtype' => $entity->getSubtype(),
431
					'content_author_name' => $invoked_by->name,
432
					'content_author_url' => $invoked_by->getURL()
433
				);
434
435
			} else {
436
				$entity_title = $entity->title;
437
				if ($entity->title == '')
438
					$entity_title = $entity->name;
439
440
				$content_array = array(
441
					'content_title' => $entity_title,
442
					'content_url' =>  $entity->getURL()."?utm_source=notification_digest&utm_medium=email",
443
					'subtype' => $entity->getSubtype(),
444
					'content_author_name' => $invoked_by->name,
445
					'content_author_url' => $invoked_by->getURL()
446
				);
447
			}
448
449
			$entity_guid = $entity->guid;
450
			$user_guid = $send_to->getGUID();
451
			$entry_type = 'personal';
452
			$group_name = NULL;
453
			$action_type = 'likes';
454
			$notification_entry = json_encode($content_array);
455
			break;
456
457
458
		case 'content_revision':
459
460
			$entity_guid = $entity->guid;
461
			$user_guid = $send_to->getGUID();
462
			$entry_type = 'personal';
463
			$group_name = NULL;
464
			$action_type = 'content_revision';
465
			$notification_entry = json_encode($content_array);
466
			break;
467
468
		case 'cp_wire_share':
469
470
			$content_title = $entity->title;
471
			if (!$entity->title) $content_title = $entity->description;
472
473
			$content_array = array(
474
				'content_title' => $content_title,
475
				'content_url' => $entity->getURL()."?utm_source=notification_digest&utm_medium=email",
476
				'subtype' => $entity->getSubtype(),
477
				'content_author_name' => $invoked_by->name,
478
				'content_author_url' => $invoked_by->getURL()
479
			);
480
481
			$entity_guid = $entity->guid;
482
			$user_guid = $send_to->getGUID();
483
			$entry_type = 'personal';
484
			$group_name = NULL;
485
			$action_type = 'cp_wire_share';
486
			$notification_entry = json_encode($content_array);			
487
			break;
488
489
		case 'cp_wire_image':
490
491
			$content_array = array(
492
				'content_description' => $entity->description,
493
				'content_url' => $content_url."?utm_source=notification_digest&utm_medium=email",
494
				'subtype' => $entity->getSubtype(),
495
				'content_author_name' => $invoked_by->name,
496
				'content_author_url' => $invoked_by->getURL(),
497
				'wire_image' => thewire_image_get_attachments($entity->guid),
498
			);
499
500
			$entity_guid = $entity->guid;
501
			$user_guid = $send_to->getGUID();
502
			$entry_type = 'personal';
503
			$group_name = NULL;
504
			$action_type = 'new_post';
505
			$notification_entry = json_encode($content_array);
506
			break;
507
508
		case 'cp_wire_mention':
509
510
			$content_array = array(
511
				'content_url' => $entity->getURL()."?utm_source=notification_digest&utm_medium=email",
512
				'subtype' => 'wire_mention',
513
				'content_author' => $invoked_by->name,
514
				'content_author_url' => $invoked_by->getURL(), 
515
			);
516
517
			$entity_guid = $entity->guid;
518
			$user_guid = $send_to->getGUID();
519
			$entry_type = 'personal';
520
			$group_name = NULL;
521
			$action_type = 'mention';
522
			$notification_entry = json_encode($content_array);
523
524
			break;
525
526
		case 'cp_mention':
527
528
			$content_array = array(
529
				'content_title' => $entity->getContainerEntity()->title,
530
				'content_url' => $content_url."?utm_source=notification_digest&utm_medium=email",
531
				'subtype' => $entity->getContainerEntity()->getSubtype(),
532
				'content_author' => $invoked_by->name,
533
				'content_author_url' => $invoked_by->getURL()
534
			);
535
536
			$entity_guid = $entity->guid;
537
			$user_guid = $send_to->getGUID();
538
			$entry_type = 'personal';
539
			$group_name = NULL;
540
			$action_type = 'mention';
541
			$notification_entry = json_encode($content_array);
542
			break;
543
544
		default:
545
546
			$entity = get_entity($entity->guid);
547
548
			if ($entity->getContainerEntity() instanceof ElggGroup) {
549
			
550
				$group_title = $entity->getContainerEntity()->name;
551
				$group_html = json_encode(array($entity->getContainerEntity()->getURL(), $group_title));
552
553
				$entity_guid = $entity->guid;
554
				$user_guid = $send_to->getGUID();
555
				$entry_type = 'group';
556
				$group_name = $group_html;//"<a href='{$entity->getContainerEntity()->getURL()}?utm_source=notification_digest&utm_medium=email'>{$group_title}</a>";
557
				$action_type = 'new_post';
558
				$notification_entry = json_encode($content_array);
559
560
			}
561
			else {
562
				$entity_guid = $entity->guid;
563
				$user_guid = $send_to->getGUID();
564
				$entry_type = 'personal';
565
				$group_name = NULL;
566
				$action_type = 'new_post';
567
				$notification_entry = json_encode($content_array);
568
			}
569
570
			break;
571
	}
572
573
	// this will fix up/sanitize strings that may contain quotes, or any other (reserved) special character
574
	$group_name = base64_encode($group_name);
575
	$notification_entry = base64_encode($notification_entry);
576
577
	/// check if record exists already, if not then proceed.
578
	$query = "SELECT 1 FROM notification_digest WHERE entity_guid = {$entity_guid} AND user_guid = {$user_guid} AND notification_entry = '{$notification_entry}'  LIMIT 1";
579
	$count_row = get_data($query);
580
581
	if (count($count_row) <= 0) {
582
		/// save, then transform the information to the database (notification_digest table)
583
		$user_guid = mysql_real_escape_string($user_guid);
584
		$entry_type = mysql_real_escape_string($entry_type);
585
		$action_type = mysql_real_escape_string($action_type);
586
587
		$query = "INSERT INTO notification_digest ( entity_guid, user_guid, entry_type, group_name, action_type, notification_entry ) VALUES ( {$entity_guid}, '{$user_guid}', '{$entry_type}', '{$group_name}', '{$action_type}', '{$notification_entry}' )";
588
589
		$insert_row = insert_data($query);
590
	}
591
	
592
593
	return true;
594
}
595
596
597
598
/**
599
 * renders the correct subtype for the notification to display
600
 * 
601
 * @param string 			$subtype_name
602
 * @param optional string 	$english
603
 */
604
function cp_translate_subtype($subtype_name, $english = true) {
605
	$label = '';
606
	switch($subtype_name) {
607
		case 'blog':
608
			$label = ($english) ? 'blog' : 'un blogue';
609
			break;
610
		case 'bookmarks':
611
			$label = ($english) ? 'bookmark' : 'un signet';
612
			break;
613
		case 'file':
614
			$label = ($english) ? 'file' : 'un fichier';
615
			break;
616
		case 'poll':
617
			$label = ($english) ? 'poll' : 'un sondage';
618
			break;
619
		case 'event_calendar':
620
			$label = ($english) ? 'event' : 'un événement';
621
			break;
622
		case 'album':
623
			$label = ($english) ? 'album' : 'un album';
624
			break;
625
		case 'groupforumtopic':
626
			$label = ($english) ? 'discussion' : 'une discussion';
627
			break;
628
		case 'image':
629
			$label = ($english) ? 'photo' : 'une image';
630
			break;
631
		case 'idea':
632
			$label = ($english) ? 'idea' : 'un idee';
633
			break;
634
		case 'page_top':
635
		case 'page':
636
				$label = ($english) ? 'page' : 'une page';
637
			break;
638
		case 'hjforumtopic':
639
			$label = ($english) ? 'forum topic' : 'un suget sur le forum';
640
			break;
641
		case 'hjforum':
642
			$label = ($english) ? 'forum' : 'un forum';
643
			break;
644
		case 'thewire':
645
			$label = ($english) ? 'wire' : 'un fil';
646
			break;
647
		case 'task_top':
648
			$label = ($english) ? 'task' : 'une tâche';
649
			break;
650
		case 'mission':
651
			$label = ($english) ? 'opportunity' : 'un oppourtunite';
652
			break;
653
		case 'answer':
654
			$label = ($english) ? 'answer' : 'réponse';
655
			break;
656
		case 'etherpad':
657
			$label = ($english) ? 'Doc' : 'Doc';
658
			break;
659
		default:
660
			$label = $subtype_name;
661
		break;
662
	}
663
	return $label;
664
}
665
666
667
668
669
/**
670
 * Helper function for notifications about new opportunities
671
 *
672
 * @param string 	$mission_type
673
 * @param string 	$role_type		offering or seeking
674
 *
675
 * @return int 		metastring id of the mission opt-in type
676
 */
677
function getMissionTypeMetastringid( $mission_type, $role_type ) {
678
	if ( $role_type == 'missions:offering' ){
679
		$typemap = array(
680
			'missions:micro_mission' => 'opt_in_missions',
681
			'missions:job_swap'	=> 'opt_in_swap',
682
			'missions:mentoring' => 'opt_in_mentored',
683
			'missions:job_shadowing' => 'opt_in_shadowed',
684
			'missions:assignment' => 'opt_in_assignSeek',
685
			'missions:deployment' => 'opt_in_deploySeek',
686
			'missions:job_rotation'	=> 'opt_in_rotation',
687
			'missions:skill_share' => 'opt_in_ssSeek',
688
			'missions:peer_coaching' => 'opt_in_pcSeek',
689
			'missions:job_share' => 'opt_in_jobshare',
690
			);
691
	}
692
	else {
693
		$typemap = array(
694
			'missions:micro-mission' => 'opt_in_missionCreate',
695
			'missions:job_swap' => 'opt_in_swap',
696
			'missions:mentoring' =>	'opt_in_mentoring',
697
			'missions:job_shadowing'	=>	'opt_in_shadowing',
698
			'missions:assignment'	=> 'opt_in_assignCreate',
699
			'missions:deployment'	=>	'opt_in_deployCreate',
700
			'missions:job_rotation'	=>	'opt_in_rotation',
701
			'missions:skill_share'	=>	'opt_in_ssCreate',
702
			'missions:peer_coaching'	=>	'opt_in_pcCreate',
703
			'missions:job_share'	=>	'opt_in_jobshare',
704
			);
705
	}
706
707
	return elgg_get_metastring_id( $typemap[$mission_type] );
708
}
709
710
711
/**
712
   * @param Array <string> $heading
713
   */
714
  function render_contents($content_array, $heading = '', $language_preference = 'en') {
715
    $author = $content_array['content_author_name'];
716
717
    // this is specifically for the Micro Missions portion due to extra field
718
    $subtype = elgg_echo($content_array['subtype']);
719
    $boolSubtype = ($language_preference === 'fr') ? false : true;
720
    $subtype = cp_translate_subtype($subtype, $boolSubtype);
721
722
    /// oppourtunities does not take into account for separate bilingual titles (no option for it)
723
    if (strpos($content_array['subtype'], 'missions') !== false )
724
    	$content_title = $content_array['content_title'];
725
    else {
726
727
    	/// otherwise, set to opposite language
728
    	$content_title = $content_array['content_title'][$language_preference];
729
    	if (empty($content_title)) {
730
    		$content_language = ($language_preference === 'en') ? 'fr' : 'en';
731
    		$content_title = $content_array['content_title'][$content_language];
732
    	}
733
    }
734
735
	if ($heading === 'new_post' && $subtype === 'file_upload') {
736
737
		$rendered_content = elgg_echo('cp_notifications:mail_body:subtype:file_upload', array($author, $content_array['file_count'], $content_array['content_title']), $language_preference );
738
    	$closing_date = elgg_echo('cp_newsletter:digest:opportunities:date', $language_preference).$content_array['deadline'];
739
      	$subtype = elgg_echo($content_array['subtype'], $language_preference);
740
741
	} elseif ($content_array['deadline']) {
742
743
		$closing_date = elgg_echo('cp_newsletter:digest:opportunities:date', $language_preference).$content_array['deadline'];
744
		$subtype = elgg_echo($content_array['subtype'], $language_preference);
745
746
		$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
747
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:oppourtunity", array($author, $subtype, $url), $language_preference)." - ".$closing_date;
748
749
750
    } elseif ($heading === 'cp_wire_share') {
751
752
753
		$content_title = gc_explode_translation($content_array['content_title'],$language_preference);
754
		$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
755
		if ($subtype === 'The Wire') $subtype = "<a href='{$content_array['content_url']}'>".elgg_echo('cp_notifications:mail_body:your_wire_post', $language_preference)."</a>";
756
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:content_share:wire", array($author, $subtype), $language_preference);
757
758
    	
759
760
	}elseif ($heading === 'cp_mention' || $heading === 'mention') {
761
762
763
		if ($content_array['subtype'] === 'wire_mention') {
764
765
			$content_title = elgg_echo("cp_notifications:subtype:name:thewire", $language_preference);
766
			$author = $content_array['content_author'];
767
768
			$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
769
			$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:wire_mention", array($author, $url), $language_preference);
770
771
	    } else {
772
773
	   	  	$author = $content_array['content_author'];
774
775
			$content_title = gc_explode_translation($content_array['content_title'], $language_preference);
776
			$author = $content_array['content_author'];
777
778
			$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
779
			$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:mention", array($author, cp_translate_subtype($content_array['subtype']),$url), $language_preference);
780
781
	  	}
782
783
784
	} elseif ($heading === 'forum_reply') {
785
786
787
		$author = get_entity($content_array['content_author']);
788
789
790
		$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
791
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:hjforumpost", array($author->name, $url), $language_preference);
792
793
794
	} elseif (strcmp($heading, "content_revision") == 0) {
795
796
		$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
797
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:{$heading}", array($author, $subtype, $url), $language_preference);
798
799
800
    } elseif ($content_array['subtype'] === 'thewire' && $heading !== 'likes') {
801
802
    	if($content_array['content_description'] && (is_array($content_array['wire_image']))){
803
			$content_array['content_description'] .= elgg_echo('cp_notification_wire_image', $language_preference);
804
    	}elseif($content_array['content_description'] == '' ){
805
			$content_array['content_description'] = elgg_echo('cp_notification_wire_image_only', $language_preference);
806
		}
807
// error_log(print_r($content_array,true));
808
// error_log('print array '.print_r($content_array['wire_image'],true));
809
810
//  if(is_array($content_array['wire_image'])){
811
//  	error_log('isset');
812
//  }else{error_log('not isset');}
813
		$url = " <a href='{$content_array['content_url']}'>".$content_array['content_description']."</a>";
814
		$wire_fil = elgg_echo('cp_notifications:subtype:name:thewire', $language_preference);
815
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:{$content_array['subtype']}_digest", array($author,$wire_fil, $url), $language_preference);
816
817
818
    } elseif (strcmp($heading, "likes") === 0) {
819
820
		$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
821
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:{$heading}", array($author, $url), $language_preference);
822
823
824
    } elseif ($heading === 'response') {
825
826
		$url = "<a href='{$content_array['content_url']}'>{$content_title}</a>";
827
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:{$heading}", array($author, $url), $language_preference);
828
829
    } else {
830
831
832
		// limit 35 characters
833
		$url = "<a href='{$content_array['content_url']}'>{$content_title}</a> {$closing_date}";
834
		$boolSubtype = ($language_preference === 'fr') ? false : true;
835
		$subtype = cp_translate_subtype($content_array['subtype'], $boolSubtype);
836
		$n = "";
837
		$vowels = array('a','e','i','o','u');
838
		if (in_array($subtype{0}, $vowels)) $n = "n";
839
		$rendered_content = elgg_echo("cp_notifications:mail_body:subtype:any", array($author, "",$subtype, $url), $language_preference);
840
    }
841
842
    return $rendered_content;
843
  }
844
845
846
847
  /**
848
   * @param string  $heading
849
   *
850
   */
851
  function render_headers($heading, $user_name='', $language = "en", $number='') {
852
853
    $proper_heading = '';
854
    $number_items = ($number > 1) ? "plural" : "singular";
855
856
    switch ($heading) {
857
		case 'new_mission':	
858
		case 'new_post_in_group':
859
			$proper_heading = elgg_echo("cp_newsletter:heading:notify:new_post:group:{$number_items}", array(), $language);
860
			break;
861
		case 'personal':
862
		case 'mission':
863
		case 'group':
864
		case 'new_post':
865
		case 'cp_wire_share':
866
		case 'wire_share':
867
		case 'cp_wire_image':
868
		case 'likes':
869
		case 'friend_request':
870
		case 'content_revision':
871
			$proper_heading = elgg_echo("cp_newsletter:heading:notify:{$heading}:{$number_items}", array(), $language);
872
			break;
873
874
		case 'forum_topic':
875
		case 'forum_reply':
876
		case 'response':
877
			$proper_heading = elgg_echo("cp_newsletter:heading:notify:{$heading}:{$number_items}", array(), $language);
878
			break;
879
		case 'friend_approved':
880
			$proper_heading = elgg_echo("cp_newsletter:heading:notify:{$heading}:{$number_items}", array($user_name),$language);
881
			break;
882
		case 'cp_mention':
883
			$proper_heading = elgg_echo("cp_newsletter:heading:notify:{$heading}:{$number_items}", array(), $language);
884
			break;
885
		default:
886
			$proper_heading = $heading;
887
			if (isJSon($proper_heading)) {
888
				$group_heading = json_decode($heading, true);
889
				$proper_heading = $group_heading[1];
890
				if (isJson($proper_heading)) {
891
					$proper_heading = json_decode($proper_heading, true);
892
					$proper_heading = "<a href='{$group_heading[0]}'>$proper_heading[$language]</a>";
893
				}
894
			}
895
			
896
			break;
897
	}
898
899
    return $proper_heading;
900
}
901
902
903
904
905
/**
906
 *
907
 */
908
function information_icon($text, $url) {
909
	return "<span class='pull-right'><a title='{$text}'><i class='fa fa-info-circle icon-sel'><span class='wb-invisible'> </span></i></a></span>";
910
}
911
912
function has_group_subscriptions($group_guid, $user_guid) {
913
	$dbprefix = elgg_get_config('dbprefix');
914
	// normal objects
915
	$query = "SELECT r.guid_one, r.relationship, r.guid_two  FROM {$dbprefix}entity_relationships r LEFT JOIN {$dbprefix}entities e ON r.guid_two = e.guid LEFT JOIN (SELECT guid FROM {$dbprefix}groups_entity WHERE guid = {$group_guid}) g ON e.container_guid = g.guid WHERE r.relationship LIKE 'cp_subscribed_to_%' AND e.type = 'object' AND e.container_guid = {$group_guid} AND r.guid_one = {$user_guid} LIMIT 1";
916
917
	$subscriptions = get_data($query);
918
	if (sizeof($subscriptions) == 0) {
919
920
		// forums
921
		$query = "SELECT elgg_subtype.entity_guid, elgg_subtype.entity_subtype
922
		FROM {$dbprefix}entity_relationships r
923
		LEFT JOIN 
924
			(SELECT e.guid AS entity_guid, s.subtype AS entity_subtype FROM {$dbprefix}entities e, {$dbprefix}entity_subtypes s WHERE (s.subtype = 'hjforumtopic' OR s.subtype = 'hjforum') AND e.subtype = s.id) elgg_subtype ON elgg_subtype.entity_guid = r.guid_two 
925
		WHERE r.guid_one = {$user_guid} AND r.relationship LIKE 'cp_subscribed_to_%'";
926
927
		$forums = get_data($query);
928
929
		foreach ($forums as $forum) {
930
			if (!$forum->entity_guid) continue;
931
			$forum_entity = get_entity($forum->entity_guid);
932
			$forum_group_guid = get_forum_in_group($forum_entity->getGUID(), $forum_entity->getGUID());
933
934
			if ($forum_group_guid == $group_guid)
935
				return 1;
936
937
		}
938
		return 0;
939
	}
940
941
	return (sizeof($subscriptions) > 0);
942
}
943
944
945
946
/**
947
 * Helper functions for digest processing queue
948
 */
949
function enqueue( $user_guid ) {
950
	// add to queue
951
	$query = "INSERT INTO notification_digest_queue (user_guid) VALUES ({$user_guid})";
952
	$result = insert_data($query);
953
954
	// return 1 if there's an error
955
	return $result === false;
956
}
957
958
/* go for something more along the lines of 'at most once' in here to ensure we don't get duplicate digests going out
959
 * to get closer to 'exactly once', the digest function can enqueue failed attempts
960
 * NOTE: this won't work so well with a split read-write db setup, will need to be done more manually or as separate delete/select statements
961
 */
962
function dequeue() {
963
	$query_delete = "DELETE FROM notification_digest_queue WHERE user_guid = @uid := user_guid LIMIT 1";			// remove a row from queue and prepare the guid to be returned
964
	$query_select = "SELECT @uid";
965
	$user_guid = get_data($query_delete . ';' . $query_select)->@uid;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '@', expecting T_STRING or T_VARIABLE or '{' or '$'
Loading history...
966
	
967
	return $user_guid;
968
}
969
970